示例#1
0
  @SuppressLint("InflateParams")
  private void showNewNetworkProxyDialog(final ProxyAdapter proxyAdapter) {
    final int originalSelection = networkProxy.isSet() ? ProxyAdapter.PROXY : ProxyAdapter.NONE;

    View view =
        LayoutInflater.from(getContext()).inflate(R.layout.debug_drawer_network_proxy, null);
    final EditText hostView = findById(view, R.id.debug_drawer_network_proxy_host);

    String host = networkProxy.get();
    if (!Strings.isBlank(host)) {
      hostView.setText(host); // Set the current host.
      hostView.setSelection(0, host.length()); // Pre-select it for editing.

      // Show the keyboard. Post this to the next frame when the dialog has been attached.
      hostView.post(
          new Runnable() {
            @Override
            public void run() {
              Keyboards.showKeyboard(hostView);
            }
          });
    }

    new AlertDialog.Builder(getContext()) //
        .setTitle("Set Network Proxy")
        .setView(view)
        .setNegativeButton(
            "Cancel",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int i) {
                networkProxyView.setSelection(originalSelection);
                dialog.cancel();
              }
            })
        .setPositiveButton(
            "Use",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int i) {
                String theHost = hostView.getText().toString();
                if (!Strings.isBlank(theHost)) {
                  networkProxy.set(theHost); // Persist across restarts.
                  proxyAdapter.notifyDataSetChanged(); // Tell the spinner to update.
                  networkProxyView.setSelection(ProxyAdapter.PROXY); // And show the proxy.

                  Proxy proxy = networkProxy.getProxy();
                  client.setProxy(proxy);
                } else {
                  networkProxyView.setSelection(originalSelection);
                }
              }
            })
        .setOnCancelListener(
            new DialogInterface.OnCancelListener() {
              @Override
              public void onCancel(DialogInterface dialogInterface) {
                networkProxyView.setSelection(originalSelection);
              }
            })
        .show();
  }
示例#2
0
  private void setupNetworkSection() {
    final ApiEndpoints currentEndpoint = ApiEndpoints.from(networkEndpoint.get());
    final EnumAdapter<ApiEndpoints> endpointAdapter =
        new EnumAdapter<>(getContext(), ApiEndpoints.class);
    endpointView.setAdapter(endpointAdapter);
    endpointView.setSelection(currentEndpoint.ordinal());
    endpointView.setOnItemSelectedListener(
        new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            ApiEndpoints selected = endpointAdapter.getItem(position);
            if (selected != currentEndpoint) {
              if (selected == ApiEndpoints.CUSTOM) {
                Timber.d("Custom network endpoint selected. Prompting for URL.");
                showCustomEndpointDialog(currentEndpoint.ordinal(), "http://");
              } else {
                setEndpointAndRelaunch(selected.url);
              }
            } else {
              Timber.d("Ignoring re-selection of network endpoint %s", selected);
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> adapterView) {}
        });

    final NetworkDelayAdapter delayAdapter = new NetworkDelayAdapter(getContext());
    networkDelayView.setAdapter(delayAdapter);
    networkDelayView.setSelection(
        NetworkDelayAdapter.getPositionForValue(mockRestAdapter.getDelay()));
    networkDelayView.setOnItemSelectedListener(
        new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            long selected = delayAdapter.getItem(position);
            if (selected != mockRestAdapter.getDelay()) {
              Timber.d("Setting network delay to %sms", selected);
              mockRestAdapter.setDelay(selected);
            } else {
              Timber.d("Ignoring re-selection of network delay %sms", selected);
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> adapterView) {}
        });

    final NetworkVarianceAdapter varianceAdapter = new NetworkVarianceAdapter(getContext());
    networkVarianceView.setAdapter(varianceAdapter);
    networkVarianceView.setSelection(
        NetworkVarianceAdapter.getPositionForValue(mockRestAdapter.getVariancePercentage()));
    networkVarianceView.setOnItemSelectedListener(
        new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            int selected = varianceAdapter.getItem(position);
            if (selected != mockRestAdapter.getVariancePercentage()) {
              Timber.d("Setting network variance to %s%%", selected);
              mockRestAdapter.setVariancePercentage(selected);
            } else {
              Timber.d("Ignoring re-selection of network variance %s%%", selected);
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> adapterView) {}
        });

    final NetworkErrorAdapter errorAdapter = new NetworkErrorAdapter(getContext());
    networkErrorView.setAdapter(errorAdapter);
    networkErrorView.setSelection(
        NetworkErrorAdapter.getPositionForValue(mockRestAdapter.getErrorPercentage()));
    networkErrorView.setOnItemSelectedListener(
        new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            int selected = errorAdapter.getItem(position);
            if (selected != mockRestAdapter.getErrorPercentage()) {
              Timber.d("Setting network error to %s%%", selected);
              mockRestAdapter.setErrorPercentage(selected);
            } else {
              Timber.d("Ignoring re-selection of network error %s%%", selected);
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> adapterView) {}
        });

    int currentProxyPosition = networkProxy.isSet() ? ProxyAdapter.PROXY : ProxyAdapter.NONE;
    final ProxyAdapter proxyAdapter = new ProxyAdapter(getContext(), networkProxy);
    networkProxyView.setAdapter(proxyAdapter);
    networkProxyView.setSelection(currentProxyPosition);
    networkProxyView.setOnItemSelectedListener(
        new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            if (position == ProxyAdapter.NONE) {
              Timber.d("Clearing network proxy");
              // TODO: Keep the custom proxy around so you can easily switch back and forth.
              networkProxy.delete();
              client.setProxy(null);
            } else if (networkProxy.isSet() && position == ProxyAdapter.PROXY) {
              Timber.d("Ignoring re-selection of network proxy %s", networkProxy.get());
            } else {
              Timber.d("New network proxy selected. Prompting for host.");
              showNewNetworkProxyDialog(proxyAdapter);
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> adapterView) {}
        });

    // Only show the endpoint editor when a custom endpoint is in use.
    endpointEditView.setVisibility(currentEndpoint == ApiEndpoints.CUSTOM ? VISIBLE : GONE);

    if (currentEndpoint == ApiEndpoints.INTERNAL) {
      // Disable network proxy if we are in mock mode.
      networkProxyView.setEnabled(false);
      networkLoggingView.setEnabled(false);
    } else {
      // Disable network controls if we are not in mock mode.
      networkDelayView.setEnabled(false);
      networkVarianceView.setEnabled(false);
      networkErrorView.setEnabled(false);
    }

    // We use the JSON rest adapter as the source of truth for the log level.
    final EnumAdapter<RestAdapter.LogLevel> loggingAdapter =
        new EnumAdapter<>(getContext(), RestAdapter.LogLevel.class);
    networkLoggingView.setAdapter(loggingAdapter);
    networkLoggingView.setSelection(restAdapter.getLogLevel().ordinal());
    networkLoggingView.setOnItemSelectedListener(
        new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            RestAdapter.LogLevel selected = loggingAdapter.getItem(position);
            if (selected != restAdapter.getLogLevel()) {
              Timber.d("Setting logging level to %s", selected);
              restAdapter.setLogLevel(selected);
            } else {
              Timber.d("Ignoring re-selection of logging level " + selected);
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> adapterView) {}
        });
  }