Search in sources :

Example 1 with ApiEndpoints

use of com.jakewharton.u2020.data.ApiEndpoints in project sbt-android by scala-android.

the class DebugView method setupNetworkSection.

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());
    RxAdapterView.itemSelections(endpointView).map(endpointAdapter::getItem).filter(item -> item != currentEndpoint).subscribe(selected -> {
        if (selected == ApiEndpoints.CUSTOM) {
            Timber.d("Custom network endpoint selected. Prompting for URL.");
            showCustomEndpointDialog(currentEndpoint.ordinal(), "http://");
        } else {
            setEndpointAndRelaunch(selected.url);
        }
    });
    final NetworkDelayAdapter delayAdapter = new NetworkDelayAdapter(getContext());
    networkDelayView.setAdapter(delayAdapter);
    networkDelayView.setSelection(NetworkDelayAdapter.getPositionForValue(behavior.delay(MILLISECONDS)));
    RxAdapterView.itemSelections(networkDelayView).map(delayAdapter::getItem).filter(item -> item != behavior.delay(MILLISECONDS)).subscribe(selected -> {
        Timber.d("Setting network delay to %sms", selected);
        behavior.setDelay(selected, MILLISECONDS);
        networkDelay.set(selected);
    });
    final NetworkVarianceAdapter varianceAdapter = new NetworkVarianceAdapter(getContext());
    networkVarianceView.setAdapter(varianceAdapter);
    networkVarianceView.setSelection(NetworkVarianceAdapter.getPositionForValue(behavior.variancePercent()));
    RxAdapterView.itemSelections(networkVarianceView).map(varianceAdapter::getItem).filter(item -> item != behavior.variancePercent()).subscribe(selected -> {
        Timber.d("Setting network variance to %s%%", selected);
        behavior.setVariancePercent(selected);
        networkVariancePercent.set(selected);
    });
    final NetworkErrorAdapter errorAdapter = new NetworkErrorAdapter(getContext());
    networkErrorView.setAdapter(errorAdapter);
    networkErrorView.setSelection(NetworkErrorAdapter.getPositionForValue(behavior.failurePercent()));
    RxAdapterView.itemSelections(networkErrorView).map(errorAdapter::getItem).filter(item -> item != behavior.failurePercent()).subscribe(selected -> {
        Timber.d("Setting network error to %s%%", selected);
        behavior.setFailurePercent(selected);
        networkFailurePercent.set(selected);
    });
    int currentProxyPosition = networkProxyAddress.isSet() ? ProxyAdapter.PROXY : ProxyAdapter.NONE;
    final ProxyAdapter proxyAdapter = new ProxyAdapter(getContext(), networkProxyAddress);
    networkProxyView.setAdapter(proxyAdapter);
    networkProxyView.setSelection(currentProxyPosition);
    RxAdapterView.itemSelections(networkProxyView).filter(position -> !networkProxyAddress.isSet() || position != ProxyAdapter.PROXY).subscribe(position -> {
        if (position == ProxyAdapter.NONE) {
            Timber.d("Clearing network proxy");
            // TODO: Keep the custom proxy around so you can easily switch back and forth.
            networkProxyAddress.delete();
            client.setProxy(null);
            apiClient.setProxy(null);
        } else if (networkProxyAddress.isSet() && position == ProxyAdapter.PROXY) {
            Timber.d("Ignoring re-selection of network proxy %s", networkProxyAddress.get());
        } else {
            Timber.d("New network proxy selected. Prompting for host.");
            showNewNetworkProxyDialog(proxyAdapter);
        }
    });
    // Only show the endpoint editor when a custom endpoint is in use.
    endpointEditView.setVisibility(currentEndpoint == ApiEndpoints.CUSTOM ? VISIBLE : GONE);
    if (currentEndpoint == ApiEndpoints.MOCK_MODE) {
        // 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(retrofit.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 != retrofit.getLogLevel()) {
// Timber.d("Setting logging level to %s", selected);
// retrofit.setLogLevel(selected);
// } else {
// Timber.d("Ignoring re-selection of logging level " + selected);
// }
// }
// 
// @Override public void onNothingSelected(AdapterView<?> adapterView) {
// }
// });
}
Also used : BuildConfig(com.jakewharton.u2020.BuildConfig) LinearLayout(android.widget.LinearLayout) Switch(android.widget.Switch) CaptureIntents(com.jakewharton.u2020.data.CaptureIntents) FrameLayout(android.widget.FrameLayout) Cache(com.squareup.okhttp.Cache) OnClick(butterknife.OnClick) NetworkDelay(com.jakewharton.u2020.data.NetworkDelay) Picasso(com.squareup.picasso.Picasso) AttributeSet(android.util.AttributeSet) AnimationSpeed(com.jakewharton.u2020.data.AnimationSpeed) Proxy(java.net.Proxy) InetSocketAddressPreferenceAdapter(com.jakewharton.u2020.data.prefs.InetSocketAddressPreferenceAdapter) Locale(java.util.Locale) Strings(com.jakewharton.u2020.util.Strings) ContextThemeWrapper(android.view.ContextThemeWrapper) PicassoDebugging(com.jakewharton.u2020.data.PicassoDebugging) TemporalAccessor(org.threeten.bp.temporal.TemporalAccessor) View(android.view.View) Bind(butterknife.Bind) Method(java.lang.reflect.Method) LumberYard(com.jakewharton.u2020.data.LumberYard) DebugAction(com.jakewharton.u2020.ui.debug.ContextualDebugActions.DebugAction) NetworkVariancePercent(com.jakewharton.u2020.data.NetworkVariancePercent) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) R(com.jakewharton.u2020.R) InetSocketAddress(java.net.InetSocketAddress) DisplayMetrics(android.util.DisplayMetrics) Timber(timber.log.Timber) ApiEndpoint(com.jakewharton.u2020.data.ApiEndpoint) ButterKnife.findById(butterknife.ButterKnife.findById) TextView(android.widget.TextView) Application(android.app.Application) MockRepositoriesResponse(com.jakewharton.u2020.data.api.MockRepositoriesResponse) DateTimeFormatter(org.threeten.bp.format.DateTimeFormatter) Context(android.content.Context) RxAdapterView(com.jakewharton.rxbinding.widget.RxAdapterView) EnumAdapter(com.jakewharton.u2020.ui.misc.EnumAdapter) ButterKnife(butterknife.ButterKnife) IsMockMode(com.jakewharton.u2020.data.IsMockMode) NetworkFailurePercent(com.jakewharton.u2020.data.NetworkFailurePercent) PixelRatioEnabled(com.jakewharton.u2020.data.PixelRatioEnabled) ProcessPhoenix(com.jakewharton.processphoenix.ProcessPhoenix) Inject(javax.inject.Inject) LogsDialog(com.jakewharton.u2020.ui.logs.LogsDialog) NetworkBehavior(retrofit.mock.NetworkBehavior) OkHttpClient(com.squareup.okhttp.OkHttpClient) ApiEndpoints(com.jakewharton.u2020.data.ApiEndpoints) Injector(com.jakewharton.u2020.data.Injector) ScalpelEnabled(com.jakewharton.u2020.data.ScalpelEnabled) Keyboards(com.jakewharton.u2020.util.Keyboards) ZoneId(org.threeten.bp.ZoneId) Named(javax.inject.Named) Build(android.os.Build) ScalpelWireframeEnabled(com.jakewharton.u2020.data.ScalpelWireframeEnabled) MockGithubService(com.jakewharton.u2020.data.api.MockGithubService) LayoutInflater(android.view.LayoutInflater) Preference(com.f2prateek.rx.preferences.Preference) PixelGridEnabled(com.jakewharton.u2020.data.PixelGridEnabled) Spinner(android.widget.Spinner) AlertDialog(android.support.v7.app.AlertDialog) StatsSnapshot(com.squareup.picasso.StatsSnapshot) ISO_INSTANT(org.threeten.bp.format.DateTimeFormatter.ISO_INSTANT) EditText(android.widget.EditText) ValueAnimator(android.animation.ValueAnimator) ApiEndpoint(com.jakewharton.u2020.data.ApiEndpoint) EnumAdapter(com.jakewharton.u2020.ui.misc.EnumAdapter) ApiEndpoints(com.jakewharton.u2020.data.ApiEndpoints)

Example 2 with ApiEndpoints

use of com.jakewharton.u2020.data.ApiEndpoints in project u2020 by JakeWharton.

the class DebugView method setupNetworkSection.

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());
    RxAdapterView.itemSelections(endpointView).map(endpointAdapter::getItem).filter(item -> item != currentEndpoint).subscribe(selected -> {
        if (selected == ApiEndpoints.CUSTOM) {
            Timber.d("Custom network endpoint selected. Prompting for URL.");
            showCustomEndpointDialog(currentEndpoint.ordinal(), "http://");
        } else {
            setEndpointAndRelaunch(selected.url);
        }
    });
    final NetworkDelayAdapter delayAdapter = new NetworkDelayAdapter(getContext());
    networkDelayView.setAdapter(delayAdapter);
    networkDelayView.setSelection(NetworkDelayAdapter.getPositionForValue(behavior.delay(MILLISECONDS)));
    RxAdapterView.itemSelections(networkDelayView).map(delayAdapter::getItem).filter(item -> item != behavior.delay(MILLISECONDS)).subscribe(selected -> {
        Timber.d("Setting network delay to %sms", selected);
        behavior.setDelay(selected, MILLISECONDS);
        networkDelay.set(selected);
    });
    final NetworkVarianceAdapter varianceAdapter = new NetworkVarianceAdapter(getContext());
    networkVarianceView.setAdapter(varianceAdapter);
    networkVarianceView.setSelection(NetworkVarianceAdapter.getPositionForValue(behavior.variancePercent()));
    RxAdapterView.itemSelections(networkVarianceView).map(varianceAdapter::getItem).filter(item -> item != behavior.variancePercent()).subscribe(selected -> {
        Timber.d("Setting network variance to %s%%", selected);
        behavior.setVariancePercent(selected);
        networkVariancePercent.set(selected);
    });
    final NetworkPercentageAdapter failureAdapter = new NetworkPercentageAdapter(getContext());
    networkFailureView.setAdapter(failureAdapter);
    networkFailureView.setSelection(NetworkPercentageAdapter.getPositionForValue(behavior.failurePercent()));
    RxAdapterView.itemSelections(networkFailureView).map(failureAdapter::getItem).filter(item -> item != behavior.failurePercent()).subscribe(selected -> {
        Timber.d("Setting network failure to %s%%", selected);
        behavior.setFailurePercent(selected);
        networkFailurePercent.set(selected);
    });
    final NetworkPercentageAdapter errorAdapter = new NetworkPercentageAdapter(getContext());
    networkErrorView.setAdapter(errorAdapter);
    networkErrorView.setSelection(NetworkPercentageAdapter.getPositionForValue(behavior.errorPercent()));
    RxAdapterView.itemSelections(networkErrorView).map(errorAdapter::getItem).filter(item -> item != behavior.errorPercent()).subscribe(selected -> {
        Timber.d("Setting network error to %s%%", selected);
        behavior.setErrorPercent(selected);
        networkErrorPercent.set(selected);
    });
    final EnumAdapter<NetworkErrorCode> errorCodeAdapter = new EnumAdapter<>(getContext(), NetworkErrorCode.class);
    networkErrorCodeView.setAdapter(errorCodeAdapter);
    networkErrorCodeView.setSelection(networkErrorCode.get().ordinal());
    RxAdapterView.itemSelections(networkErrorCodeView).map(errorCodeAdapter::getItem).filter(item -> item != networkErrorCode.get()).subscribe(selected -> {
        Timber.d("Setting network error code to %s%%", selected);
        networkErrorCode.set(selected);
    // No need to update 'behavior' as the factory already pulls from the preference.
    });
    int currentProxyPosition = networkProxyAddress.isSet() ? ProxyAdapter.PROXY : ProxyAdapter.NONE;
    final ProxyAdapter proxyAdapter = new ProxyAdapter(getContext(), networkProxyAddress);
    networkProxyView.setAdapter(proxyAdapter);
    networkProxyView.setSelection(currentProxyPosition);
    RxAdapterView.itemSelections(networkProxyView).filter(position -> !networkProxyAddress.isSet() || position != ProxyAdapter.PROXY).subscribe(position -> {
        if (position == ProxyAdapter.NONE) {
            // Only clear the proxy and restart if one was previously set.
            if (currentProxyPosition != ProxyAdapter.NONE) {
                Timber.d("Clearing network proxy");
                // TODO: Keep the custom proxy around so you can easily switch back and forth.
                networkProxyAddress.delete();
                // Force a restart to re-initialize the app without a proxy.
                ProcessPhoenix.triggerRebirth(getContext());
            }
        } else if (networkProxyAddress.isSet() && position == ProxyAdapter.PROXY) {
            Timber.d("Ignoring re-selection of network proxy %s", networkProxyAddress.get());
        } else {
            Timber.d("New network proxy selected. Prompting for host.");
            showNewNetworkProxyDialog(proxyAdapter);
        }
    });
    // Only show the endpoint editor when a custom endpoint is in use.
    endpointEditView.setVisibility(currentEndpoint == ApiEndpoints.CUSTOM ? VISIBLE : GONE);
    if (currentEndpoint == ApiEndpoints.MOCK_MODE) {
        // 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);
        networkFailureView.setEnabled(false);
        networkErrorView.setEnabled(false);
        networkErrorCodeView.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(retrofit.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 != retrofit.getLogLevel()) {
// Timber.d("Setting logging level to %s", selected);
// retrofit.setLogLevel(selected);
// } else {
// Timber.d("Ignoring re-selection of logging level " + selected);
// }
// }
// 
// @Override public void onNothingSelected(AdapterView<?> adapterView) {
// }
// });
}
Also used : BuildConfig(com.jakewharton.u2020.BuildConfig) LinearLayout(android.widget.LinearLayout) Switch(android.widget.Switch) Cache(okhttp3.Cache) CaptureIntents(com.jakewharton.u2020.data.CaptureIntents) FrameLayout(android.widget.FrameLayout) OnClick(butterknife.OnClick) BindView(butterknife.BindView) NetworkDelay(com.jakewharton.u2020.data.NetworkDelay) Picasso(com.squareup.picasso.Picasso) AttributeSet(android.util.AttributeSet) AnimationSpeed(com.jakewharton.u2020.data.AnimationSpeed) InetSocketAddressPreferenceAdapter(com.jakewharton.u2020.data.prefs.InetSocketAddressPreferenceAdapter) Locale(java.util.Locale) Strings(com.jakewharton.u2020.util.Strings) ContextThemeWrapper(android.view.ContextThemeWrapper) PicassoDebugging(com.jakewharton.u2020.data.PicassoDebugging) TemporalAccessor(org.threeten.bp.temporal.TemporalAccessor) View(android.view.View) MockResponseSupplier(com.jakewharton.u2020.data.api.MockResponseSupplier) Method(java.lang.reflect.Method) LumberYard(com.jakewharton.u2020.data.LumberYard) DebugAction(com.jakewharton.u2020.ui.debug.ContextualDebugActions.DebugAction) NetworkVariancePercent(com.jakewharton.u2020.data.NetworkVariancePercent) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) R(com.jakewharton.u2020.R) InetSocketAddress(java.net.InetSocketAddress) DisplayMetrics(android.util.DisplayMetrics) Timber(timber.log.Timber) ApiEndpoint(com.jakewharton.u2020.data.ApiEndpoint) TextView(android.widget.TextView) Application(android.app.Application) MockRepositoriesResponse(com.jakewharton.u2020.data.api.MockRepositoriesResponse) DateTimeFormatter(org.threeten.bp.format.DateTimeFormatter) Context(android.content.Context) RxAdapterView(com.jakewharton.rxbinding.widget.RxAdapterView) EnumAdapter(com.jakewharton.u2020.ui.misc.EnumAdapter) ButterKnife(butterknife.ButterKnife) NetworkBehavior(retrofit2.mock.NetworkBehavior) Intent(android.content.Intent) IsMockMode(com.jakewharton.u2020.data.IsMockMode) NetworkErrorCode(com.jakewharton.u2020.data.NetworkErrorCode) NetworkFailurePercent(com.jakewharton.u2020.data.NetworkFailurePercent) PixelRatioEnabled(com.jakewharton.u2020.data.PixelRatioEnabled) DisplayLeakActivity(com.squareup.leakcanary.internal.DisplayLeakActivity) ProcessPhoenix(com.jakewharton.processphoenix.ProcessPhoenix) NetworkErrorPercent(com.jakewharton.u2020.data.NetworkErrorPercent) Inject(javax.inject.Inject) LogsDialog(com.jakewharton.u2020.ui.logs.LogsDialog) Instant(org.threeten.bp.Instant) ApiEndpoints(com.jakewharton.u2020.data.ApiEndpoints) Injector(com.jakewharton.u2020.data.Injector) ScalpelEnabled(com.jakewharton.u2020.data.ScalpelEnabled) Keyboards(com.jakewharton.u2020.util.Keyboards) ZoneId(org.threeten.bp.ZoneId) Named(javax.inject.Named) Build(android.os.Build) ScalpelWireframeEnabled(com.jakewharton.u2020.data.ScalpelWireframeEnabled) LayoutInflater(android.view.LayoutInflater) Preference(com.f2prateek.rx.preferences.Preference) PixelGridEnabled(com.jakewharton.u2020.data.PixelGridEnabled) Spinner(android.widget.Spinner) AlertDialog(android.support.v7.app.AlertDialog) OkHttpClient(okhttp3.OkHttpClient) StatsSnapshot(com.squareup.picasso.StatsSnapshot) EditText(android.widget.EditText) ValueAnimator(android.animation.ValueAnimator) NetworkErrorCode(com.jakewharton.u2020.data.NetworkErrorCode) ApiEndpoint(com.jakewharton.u2020.data.ApiEndpoint) EnumAdapter(com.jakewharton.u2020.ui.misc.EnumAdapter) ApiEndpoints(com.jakewharton.u2020.data.ApiEndpoints)

Aggregations

ValueAnimator (android.animation.ValueAnimator)2 Application (android.app.Application)2 Context (android.content.Context)2 Build (android.os.Build)2 AlertDialog (android.support.v7.app.AlertDialog)2 AttributeSet (android.util.AttributeSet)2 DisplayMetrics (android.util.DisplayMetrics)2 ContextThemeWrapper (android.view.ContextThemeWrapper)2 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 EditText (android.widget.EditText)2 FrameLayout (android.widget.FrameLayout)2 LinearLayout (android.widget.LinearLayout)2 Spinner (android.widget.Spinner)2 Switch (android.widget.Switch)2 TextView (android.widget.TextView)2 ButterKnife (butterknife.ButterKnife)2 OnClick (butterknife.OnClick)2 Preference (com.f2prateek.rx.preferences.Preference)2 ProcessPhoenix (com.jakewharton.processphoenix.ProcessPhoenix)2