@Override
  public boolean onPreferenceChange(Preference preference, Object newValue) {
    if (preference.equals(customApiUrlPref) && newValue instanceof String) {
      String apiUrl = (String) newValue;

      if (!TextUtils.isEmpty(apiUrl)) {
        // User entered a custom API Url, so set the region info to null
        Application.get().setCurrentRegion(null);
        if (BuildConfig.DEBUG) {
          Log.d(TAG, "User entered new API URL, set region to null.");
        }
      } else {
        // User cleared the API URL preference value, so re-initialize regions
        if (BuildConfig.DEBUG) {
          Log.d(TAG, "User entered blank API URL, re-initializing regions...");
        }
        NavHelp.goHome(this);
      }
    } else if (preference.equals(analyticsPref) && newValue instanceof Boolean) {
      Boolean isAnalyticsActive = (Boolean) newValue;
      // Report if the analytics turns off, just before shared preference changed
      if (!isAnalyticsActive) {
        ObaAnalytics.reportEventWithCategory(
            ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(),
            getString(R.string.analytics_action_edit_general),
            getString(R.string.analytics_label_analytic_preference)
                + (isAnalyticsActive ? "YES" : "NO"));
        GoogleAnalytics.getInstance(getBaseContext()).dispatchLocalHits();
      }
    }
    return true;
  }
 @Override
 public boolean onPreferenceClick(Preference pref) {
   if (pref.equals(regionPref)) {
     RegionsActivity.start(this);
   } else if (pref.equals(donatePref)) {
     ObaAnalytics.reportEventWithCategory(
         ObaAnalytics.ObaEventCategory.UI_ACTION.toString(),
         getString(R.string.analytics_action_button_press),
         getString(R.string.analytics_label_button_press_donate));
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.donate_url)));
     startActivity(intent);
   }
   return true;
 }
 @Override
 protected void onStart() {
   super.onStart();
   ObaAnalytics.reportActivityStart(this);
 }
  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    SharedPreferences settings = Application.getPrefs();
    // Listening to changes to a custom Preference doesn't seem to work, so we can listen to changes
    // to the shared pref value instead
    if (key.equals(getString(R.string.preference_key_experimental_regions))) {
      boolean experimentalServers =
          settings.getBoolean(getString(R.string.preference_key_experimental_regions), false);
      if (BuildConfig.DEBUG) {
        Log.d(TAG, "Experimental regions shared preference changed to " + experimentalServers);
      }

      /*
      Force a refresh of the regions list, but don't using blocking progress dialog
      inside the ObaRegionsTask AsyncTask.
      We need to use our own Action Bar progress bar here so its linked to this activity,
      which will survive orientation changes.
      */
      setSupportProgressBarIndeterminateVisibility(true);
      ObaRegionsTask task = new ObaRegionsTask(this, this, true, false);
      task.execute();

      // Wait to change the region preference description until the task callback
      // Analytics
      if (experimentalServers) {
        ObaAnalytics.reportEventWithCategory(
            ObaAnalytics.ObaEventCategory.UI_ACTION.toString(),
            getString(R.string.analytics_action_button_press),
            getString(R.string.analytics_label_button_press_experimental_on));
      } else {
        ObaAnalytics.reportEventWithCategory(
            ObaAnalytics.ObaEventCategory.UI_ACTION.toString(),
            getString(R.string.analytics_action_button_press),
            getString(R.string.analytics_label_button_press_experimental_off));
      }
    } else if (key.equals(getString(R.string.preference_key_oba_api_url))) {
      // Change the region preference description to show we're not using a region
      changePreferenceSummary(key);
    } else if (key.equalsIgnoreCase(getString(R.string.preference_key_preferred_units))) {
      // Change the preferred units description
      changePreferenceSummary(key);
    } else if (key.equalsIgnoreCase(getString(R.string.preference_key_auto_select_region))) {
      // Analytics
      boolean autoSelect =
          settings.getBoolean(getString(R.string.preference_key_auto_select_region), false);
      if (autoSelect) {
        ObaAnalytics.reportEventWithCategory(
            ObaAnalytics.ObaEventCategory.UI_ACTION.toString(),
            getString(R.string.analytics_action_button_press),
            getString(R.string.analytics_label_button_press_auto));
      } else {
        ObaAnalytics.reportEventWithCategory(
            ObaAnalytics.ObaEventCategory.UI_ACTION.toString(),
            getString(R.string.analytics_action_button_press),
            getString(R.string.analytics_label_button_press_manual));
      }
    } else if (key.equalsIgnoreCase(getString(R.string.preferences_key_analytics))) {
      Boolean isAnalyticsActive =
          settings.getBoolean(
              Application.get().getString(R.string.preferences_key_analytics), Boolean.FALSE);
      // Report if the analytics turns on, just after shared preference changed
      if (isAnalyticsActive)
        ObaAnalytics.reportEventWithCategory(
            ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(),
            getString(R.string.analytics_action_edit_general),
            getString(R.string.analytics_label_analytic_preference)
                + (isAnalyticsActive ? "YES" : "NO"));
    }
  }