@Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == R.id.config) { Barricade.getInstance().launchConfigActivity(this); return true; } return super.onOptionsItemSelected(item); }
private void checkChanged(boolean isChecked) { Barricade.getInstance().setEnabled(isChecked); }
/** * Create Barricade instance if not exist and return it * It should be called once in project, next call will be ignored * * @return Barricade instance */ public Barricade install() { if (instance == null) { instance = new Barricade(fileManager, barricadeConfig, delay, application); } else { Logger.getLogger(TAG).info("Barricade already installed, install() will be ignored."); } return instance; } }
@Override public Response intercept(Chain chain) throws IOException { if (barricade.isEnabled()) { Request request = chain.request(); List<String> pathSegments = request.url().pathSegments(); String endpoint = pathSegments.get(pathSegments.size() - 1); Response response = barricade.getResponse(chain, endpoint); if (response != null) { try { Thread.sleep(barricade.getDelay()); } catch (InterruptedException e) { Logger.getLogger(Barricade.TAG).severe(e.getMessage()); } return response; } else { Logger.getLogger(Barricade.TAG).severe("No response found, making actual request"); return chain.proceed(request); } } else { return chain.proceed(chain.request()); } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_barricade); initUi(); barricadeConfig = Barricade.getInstance().getConfig(); setEndpointsView(); }
private void showEditDialog() { View dialogView = getLayoutInflater().inflate(R.layout.dialog_edit_global_delay, container, false); final EditText delayEditText = (EditText) dialogView.findViewById(R.id.delay_value_edittext); delayEditText.setText(String.format(Locale.US, "%d", Barricade.getInstance().getDelay())); new AlertDialog.Builder(this).setView(dialogView) .setTitle(R.string.edit_delay) .setPositiveButton(R.string.set, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String value = delayEditText.getText().toString(); if (value.isEmpty()) { delayEditText.setError(getString(R.string.required)); } else { Barricade.getInstance().setDelay(Long.parseLong(value)); Toast.makeText(BarricadeActivity.this, R.string.updated, Toast.LENGTH_LONG).show(); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .show(); }
@Test public void canSetBarricadeResponseAtRunTime() throws IOException { barricade.setEnabled(true).setResponse(BarricadeConfig.Endpoints.RANDOM, BarricadeConfig.Responses.Random.FAILURE); Response<Joke> response = getApiService().getRandomJoke().execute(); assertThat(response.isSuccessful()).isFalse(); assertThat(response.code()).isEqualTo(401); }
@Test public void canFetchRandomJokeFromApi() throws IOException { barricade.setEnabled(false); Response<Joke> response = getApiService().getRandomJoke().execute(); assertThat(response.isSuccessful()).isTrue(); assertThat(response.code()).isEqualTo(200); assertThat(response.body()).isNotNull(); Joke joke = response.body(); assertThat(joke.id).isNotNull(); assertThat(joke.id).isNotEmpty(); assertThat(joke.value).isNotNull(); assertThat(joke.value).isNotEmpty(); assertThat(joke.iconUrl).isNotNull(); assertThat(joke.iconUrl).isNotEmpty(); }
/** * Resets any configuration changes done at run-time */ public void reset() { HashMap<String, BarricadeResponseSet> configs = getConfig(); for (String key : configs.keySet()) { BarricadeResponseSet set = configs.get(key); set.defaultIndex = set.originalDefaultIndex; } this.delay = DEFAULT_DELAY; }
@After public void teardown() { barricade.reset(); }
/** * Returns a barricaded response for an endpoint * * @param chain OkHttp Interceptor chain * @param endpoint Endpoint that is being hit * @return Barricaded response (if available), null otherwise */ okhttp3.Response getResponse(Interceptor.Chain chain, String endpoint) { BarricadeResponse barricadeResponse = barricadeConfig.getResponseForEndpoint(endpoint); if (barricadeResponse == null) { return null; } String fileResponse = getResponseFromFile(endpoint, barricadeResponse.responseFileName); return new okhttp3.Response.Builder().code(barricadeResponse.statusCode).message("Barricade OK") .request(chain.request()) .protocol(Protocol.HTTP_1_0) .body(ResponseBody.create(MediaType.parse(barricadeResponse.contentType), fileResponse.getBytes())) .addHeader("content-type", barricadeResponse.contentType) .build(); }
@Test public void canFetchRandomJokeFromBarricade() throws IOException { barricade.setEnabled(true); Response<Joke> response = getApiService().getRandomJoke().execute(); assertThat(response.isSuccessful()).isTrue(); assertThat(response.code()).isEqualTo(200); assertThat(response.body()).isNotNull(); Joke joke = response.body(); assertThat(joke.id).isNotNull(); assertThat(joke.id).isNotEmpty(); assertThat(joke.id).isEqualTo("gX6RQU9EQxC4oZd6kVeiSw"); assertThat(joke.value).isNotNull(); assertThat(joke.value).isNotEmpty(); assertThat(joke.value).isEqualTo("When Chuck Norris was a kid, he made his mom eat her vegetables"); assertThat(joke.iconUrl).isNotNull(); assertThat(joke.iconUrl).isNotEmpty(); assertThat(joke.iconUrl).isEqualTo("https://assets.chucknorris.host/img/avatar/chuck-norris.png"); }
/** * Change response to be returned for an endpoint * * @param endPoint The endpoint whose response you want to change. Use BarricadeConfig$EndPoints * to * get endpoint strings rather than passing string directly * @param defaultIndex The index of the response you want to get for endPoint. Use * BarricadeConfig$Responses to get responses for an endpoint instead of passing an int directly */ public Barricade setResponse(String endPoint, int defaultIndex) { if (getConfig().containsKey(endPoint)) { getConfig().get(endPoint).defaultIndex = defaultIndex; return this; } else { throw new IllegalArgumentException(endPoint + " doesn't exist"); } }
@Override public void onSensorChanged(SensorEvent sensorEvent) { long curTime = System.currentTimeMillis(); if ((curTime - lastUpdate) > 100) { long diffTime = (curTime - lastUpdate); lastUpdate = curTime; float x, y, z; x = sensorEvent.values[0]; y = sensorEvent.values[1]; z = sensorEvent.values[2]; float speed = Math.abs(x + y + z - lastX - lastY - lastZ) / diffTime * 10000; if (speed > SHAKE_THRESHOLD) { shakeCount++; } else { shakeCount = 0; } if (shakeCount >= 2) { shakeCount = 0; Barricade.getInstance().launchConfigActivity(application); } lastX = x; lastY = y; lastZ = z; } }