Example #1
0
 @Nullable
 private BackendConfig getCustomBackend() {
   SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
   String backend = prefs.getString(CUSTOM_BACKEND_PREFERENCE, null);
   if (backend != null) {
     try {
       return BackendConfig.byName().apply(backend);
     } catch (NoSuchElementException ex) {
       Timber.w("Could not find backend with name: %s", backend);
     }
   }
   return null;
 }
Example #2
0
 private void saveBackendConfig(BackendConfig backend) {
   PreferenceManager.getDefaultSharedPreferences(context)
       .edit()
       .putString(CUSTOM_BACKEND_PREFERENCE, backend.environment())
       .commit();
 }
Example #3
0
public class BackendPicker {

  private static final String CUSTOM_BACKEND_PREFERENCE = "custom_backend_pref";
  private final Context context;

  private final String[] backends =
      new String[] {
        BackendConfig.StagingBackend().environment(), BackendConfig.ProdBackend().environment()
      };

  public BackendPicker(Context context) {
    this.context = context;
  }

  public void withBackend(Activity activity, final Callback<Void> callback) {
    if (shouldShowBackendPicker()) {
      showDialog(activity, callback);
    } else {
      callback.callback(null);
    }
  }

  public void withBackend(final Callback<Void> callback) {
    BackendConfig be = getBackendConfig();
    if (be != null) {
      ZMessaging.useBackend(be);
      callback.callback(null);
    }
  }

  private void showDialog(Activity activity, final Callback<Void> callback) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setTitle("Select Backend");
    builder.setItems(
        backends,
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            BackendConfig be = BackendConfig.byName().apply(backends[which]);
            ZMessaging.useBackend(be);
            saveBackendConfig(be);
            callback.callback(null);
          }
        });
    builder.setCancelable(false);
    builder.create().show();
  }

  private boolean shouldShowBackendPicker() {
    if (!BuildConfig.SHOW_BACKEND_PICKER) {
      return false;
    }
    return !PreferenceManager.getDefaultSharedPreferences(context)
        .contains(CUSTOM_BACKEND_PREFERENCE);
  }

  @Nullable
  private BackendConfig getBackendConfig() {
    return BuildConfig.SHOW_BACKEND_PICKER ? getCustomBackend() : BuildConfigUtils.defaultBackend();
  }

  private void saveBackendConfig(BackendConfig backend) {
    PreferenceManager.getDefaultSharedPreferences(context)
        .edit()
        .putString(CUSTOM_BACKEND_PREFERENCE, backend.environment())
        .commit();
  }

  @Nullable
  private BackendConfig getCustomBackend() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    String backend = prefs.getString(CUSTOM_BACKEND_PREFERENCE, null);
    if (backend != null) {
      try {
        return BackendConfig.byName().apply(backend);
      } catch (NoSuchElementException ex) {
        Timber.w("Could not find backend with name: %s", backend);
      }
    }
    return null;
  }
}