@Override
        public boolean onPreferenceChange(Preference preference, Object value) {
          String stringValue = value.toString();

          if (preference instanceof ListPreference) {
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);

            preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
          } else if (preference instanceof RingtonePreference) {
            if (TextUtils.isEmpty(stringValue)) {
              preference.setSummary(R.string.pref_ringtone_silent);
            } else {
              Ringtone ringtone =
                  RingtoneManager.getRingtone(preference.getContext(), Uri.parse(stringValue));

              if (ringtone == null) {
                preference.setSummary(null);
              } else {
                String name = ringtone.getTitle(preference.getContext());
                preference.setSummary(name);
              }
            }
          } else {
            preference.setSummary(stringValue);
          }
          return true;
        }
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
          String stringValue = value.toString();

          if (preference instanceof RingtonePreference) {
            // For ringtone preferences, look up the correct display value
            // using RingtoneManager.

            Ringtone ringtone =
                RingtoneManager.getRingtone(preference.getContext(), Uri.parse(stringValue));

            if (ringtone == null) {
              // Clear the summary if there was a lookup error.
              preference.setSummary(null);
            } else {
              // Set the summary to reflect the new ringtone display
              // name.
              String name = ringtone.getTitle(preference.getContext());
              preference.setSummary(name);
            }

          } else {
            // For all other preferences, set the summary to the value's
            // simple string representation.
            preference.setSummary(stringValue);
          }
          return true;
        }
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
          // Do not bind toggles.
          if (preference instanceof CheckBoxPreference
              || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
                  && preference instanceof TwoStatePreference)) {
            return true;
          }

          String stringValue = value.toString();

          if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list.
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);

            // Set the summary to reflect the new value.
            preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);

          } else if (preference instanceof RingtonePreference) {
            // For ringtone preferences, look up the correct display value
            // using RingtoneManager.
            if (TextUtils.isEmpty(stringValue)) {
              // Empty values correspond to 'silent' (no ringtone).
              preference.setSummary(R.string.ringtone_silent);
            } else {
              Ringtone ringtone =
                  RingtoneManager.getRingtone(preference.getContext(), Uri.parse(stringValue));

              if (ringtone == null) {
                // Clear the summary if there was a lookup error.
                preference.setSummary(null);
              } else {
                // Set the summary to reflect the new ringtone display
                // name.
                String name = ringtone.getTitle(preference.getContext());
                preference.setSummary(name);
              }
            }

          } else if (preference instanceof EditTextPreference) {
            EditTextPreference textPreference = (EditTextPreference) preference;
            int inputType = textPreference.getEditText().getInputType();
            if (inputType == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)) {
              preference.setSummary(stringValue.replaceAll(".", "*"));
            } else {
              preference.setSummary(stringValue);
            }
          } else {
            // For all other preferences, set the summary to the value's
            // simple string representation.
            preference.setSummary(stringValue);
          }
          return true;
        }
 private void bindPreferenceSummaryToValue(Preference preference) {
   preference.setOnPreferenceChangeListener(this);
   onPreferenceChange(
       preference,
       PreferenceManager.getDefaultSharedPreferences(preference.getContext())
           .getString(preference.getKey(), ""));
 }
 @Override
 public boolean onPreferenceClick(Preference preference) {
   assert preference.getKey().equals(PREF_SERVER_PROFILE_EDIT_LINK);
   ((Preferences) preference.getContext())
       .showUrl(R.string.autofill_edit_profile, R.string.autofill_manage_wallet_addresses_url);
   return true;
 }
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
          String stringValue = value.toString();

          if (preference instanceof ListPreference) {
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);
            preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
          } else {
            preference.setSummary(stringValue);
          }

          // detect errors
          if (preference.getKey().equals("sos_uri")) {
            try {
              URL url = new URL(value.toString());
              if (!url.getProtocol().equals("http") && !url.getProtocol().equals("https"))
                throw new Exception("SOS URL must be HTTP or HTTPS");
            } catch (Exception e) {
              AlertDialog.Builder dlgAlert = new AlertDialog.Builder(preference.getContext());
              dlgAlert.setMessage("Invalid SOS URL");
              dlgAlert.setTitle(e.getMessage());
              dlgAlert.setPositiveButton("OK", null);
              dlgAlert.setCancelable(true);
              dlgAlert.create().show();
            }
          }

          return true;
        }
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
          String stringValue = value.toString();

          if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list.
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);

            // Set the summary to reflect the new value.
            preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);

          } else if (preference instanceof RingtonePreference) {
            // For ringtone preferences, look up the correct display value
            // using RingtoneManager.
            if (TextUtils.isEmpty(stringValue)) {
              // Empty values correspond to 'silent' (no ringtone).
              preference.setSummary(R.string.pref_ringtone_silent);

            } else {
              Ringtone ringtone =
                  RingtoneManager.getRingtone(preference.getContext(), Uri.parse(stringValue));

              if (ringtone == null) {
                // Clear the summary if there was a lookup error.
                preference.setSummary(null);
              } else {
                // Set the summary to reflect the new ringtone display
                // name.
                String name = ringtone.getTitle(preference.getContext());
                preference.setSummary(name);
              }
            }

          } else if (preference instanceof EditTextPreference) {
            // For all other preferences, set the summary to the value's
            // simple string representation.

            preference.setSummary(stringValue);
          } else {
            preference.setSummary(stringValue);
          }
          return true;
        }
  /**
   * Attaches a listener so the summary is always updated with the preference value. Also fires the
   * listener once, to initialize the summary (so it shows up before the value is changed.)
   */
  private void bindPreferenceSummaryToValue(Preference preference) {
    // set the listener for value changes
    preference.setOnPreferenceChangeListener(this);

    // trigger the listener imediatly with the preferences current value
    onPreferenceChange(
        preference,
        PreferenceManager.getDefaultSharedPreferences(preference.getContext())
            .getString(preference.getKey(), ""));
  }
  /**
   * Attaches a listener so the summary is always updated with the preference value. Also fires the
   * listener once, to initialize the summary (so it shows up before the value is changed.)
   */
  private void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(this);

    // Set the preference summaries
    setPreferenceSummary(
        preference,
        PreferenceManager.getDefaultSharedPreferences(preference.getContext())
            .getString(preference.getKey(), ""));
  }
  /**
   * Attaches a listener so the summary is always updated with the preference value. Also fires the
   * listener once, to initialize the summary (so it shows up before the value is changed.)
   */
  private void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(this);

    // Trigger the listener immediately with the preference's current value.
    String strPref =
        PreferenceManager.getDefaultSharedPreferences(preference.getContext())
            .getString(preference.getKey(), "");
    onPreferenceChange(preference, strPref);
  }
  /*
   * Binds a preference's summary to its value. More specifically, when the
   * preference's value is changed, its summary (line of text below the
   * preference title) is updated to reflect the value. The summary is also
   * immediately updated upon calling this method. The exact display format is
   * dependent on the type of preference.
   *
   * @see #sBindPreferenceSummaryToValueListener
   */
  private static void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

    // Trigger the listener immediately with the preference's
    // current value.
    sBindPreferenceSummaryToValueListener.onPreferenceChange(
        preference,
        PreferenceManager.getDefaultSharedPreferences(preference.getContext())
            .getString(preference.getKey(), ""));
  }
  private void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(this);

    // Trigger the listener immediately with the preference's
    // current value.
    onPreferenceChange(
        preference,
        PreferenceManager.getDefaultSharedPreferences(preference.getContext())
            .getString(
                preference.getKey(), getResources().getString(R.string.default_sorting_method)));
  }
  /**
   * When the preference's value is changed, trigger the given listener. The listener is also
   * immediately called with the preference's current value upon calling this method.
   */
  public static void setAndCallPreferenceChangeListener(
      Preference preference, Preference.OnPreferenceChangeListener listener) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(listener);

    // Trigger the listener immediately with the preference's
    // current value.
    listener.onPreferenceChange(
        preference,
        PreferenceManager.getDefaultSharedPreferences(preference.getContext())
            .getString(preference.getKey(), ""));
  }
 public static int getPersistedInt(Preference pref, int defaultReturnValue) {
   OrionApplication appContext = (OrionApplication) pref.getContext().getApplicationContext();
   LastPageInfo info = appContext.getCurrentBookParameters();
   if (info != null) {
     try {
       Field f = info.getClass().getDeclaredField(pref.getKey());
       Integer value = (Integer) f.get(info);
       return value;
     } catch (Exception e) {
       Common.d(e);
     }
   }
   return defaultReturnValue;
 }
 public static String getPersistedString(Preference pref, String defaultReturnValue) {
   LastPageInfo info =
       ((OrionApplication) pref.getContext().getApplicationContext()).getCurrentBookParameters();
   if (info != null) {
     try {
       Field f = info.getClass().getDeclaredField(pref.getKey());
       String value = f.get(info).toString();
       return value;
     } catch (Exception e) {
       Common.d(e);
     }
   }
   return defaultReturnValue;
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    Preference pref = findPreference(getString(R.string.pref_startScreen_key));
    // Set the listener to watch the change
    pref.setOnPreferenceChangeListener(this);
    // Initialize the summary
    setPreferenceSummary(
        pref,
        PreferenceManager.getDefaultSharedPreferences(pref.getContext())
            .getString(pref.getKey(), ""));
  }
  /**
   * Attaches a listener so the summary is always updated with the preference value. Also fires the
   * listener once, to initialize the summary (so it shows up before the value is changed.)
   */
  private void bindPreferenceSummaryToValue(Preference preference) {
    mBindingPreference = true;

    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(this);

    // Trigger the listener immediately with the preference's
    // current value.
    onPreferenceChange(
        preference,
        PreferenceManager.getDefaultSharedPreferences(preference.getContext())
            .getString(preference.getKey(), ""));

    mBindingPreference = false;
  }
 private void updateActionSummary(Preference pref, ActionInfo.Record record) {
   ActionInfo info = new ActionInfo(record);
   // name
   if (Strings.isNullOrEmpty(info.getName())) {
     if (record.type == ActionInfo.TYPE_NONE) {
       pref.setSummary(getString(R.string.none));
     } else {
       pref.setSummary(getString(R.string.not_found));
     }
   } else {
     pref.setSummary(info.getName());
   }
   // icon
   pref.setIcon(info.newIconDrawable(pref.getContext()));
 }
 @Override
 public boolean onPreferenceChange(Preference preference, Object objValue) {
   final String key = preference.getKey();
   if (KEY_SCREEN_TIMEOUT.equals(key)) {
     try {
       int value = Integer.parseInt((String) objValue);
       Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
       updateTimeoutPreferenceDescription(value);
     } catch (NumberFormatException e) {
       Log.e(TAG, "could not persist screen timeout setting", e);
     }
   }
   if (KEY_LCD_DENSITY.equals(key)) {
     try {
       int value = Integer.parseInt((String) objValue);
       writeLcdDensityPreference(preference.getContext(), value);
       updateLcdDensityPreferenceDescription(value);
     } catch (NumberFormatException e) {
       Log.e(TAG, "could not persist display density setting", e);
     }
   }
   if (KEY_FONT_SIZE.equals(key)) {
     writeFontSizePreference(objValue);
   }
   if (preference == mAutoBrightnessPreference) {
     boolean auto = (Boolean) objValue;
     Settings.System.putInt(
         getContentResolver(),
         SCREEN_BRIGHTNESS_MODE,
         auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
   }
   if (preference == mLiftToWakePreference) {
     boolean value = (Boolean) objValue;
     Settings.Secure.putInt(getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0);
   }
   if (preference == mProximityWakePreference) {
     boolean value = (Boolean) objValue;
     Settings.System.putInt(
         getContentResolver(), Settings.System.PROXIMITY_ON_WAKE, value ? 1 : 0);
   }
   return true;
 }
 public static boolean persistValue(Preference pref, String value) {
   OrionApplication appContext = (OrionApplication) pref.getContext().getApplicationContext();
   LastPageInfo info = appContext.getCurrentBookParameters();
   if (info != null) {
     try {
       Field f = info.getClass().getDeclaredField(pref.getKey());
       Class clazz = f.getType();
       Object resultValue = value;
       if (int.class.equals(clazz)) {
         resultValue = Integer.valueOf(value);
       }
       f.set(info, resultValue);
       ((OrionApplication) appContext.getApplicationContext())
           .processBookOptionChange(pref.getKey(), resultValue);
       return true;
     } catch (Exception e) {
       Common.d(e);
     }
   }
   return false;
 }
  protected void initializePreference(Preference preference) {
    if (preference instanceof PreferenceGroup) {
      PreferenceGroup group = (PreferenceGroup) preference;
      for (int i = 0; i < group.getPreferenceCount(); i++) {
        initializePreference(group.getPreference(i));
      }
      updatePreferences(group, null);
    } else {
      Object value = null;
      if (preference instanceof ListPreference) {
        value = ((ListPreference) preference).getValue();
      } else if (preference instanceof CheckBoxPreference) {
        value = ((CheckBoxPreference) preference).isChecked();
      } else if (preference instanceof EditTextPreference) {
        value = ((EditTextPreference) preference).getText();
      } else if (preference instanceof RingtonePreference) {
        value =
            PreferenceManager.getDefaultSharedPreferences(preference.getContext())
                .getString(preference.getKey(), null);
      } else if (preference instanceof TimePreference) {
        value = ((TimePreference) preference).getMillisOfDay();
      }

      updatePreferences(preference, value);

      if (preference.getOnPreferenceChangeListener() == null) {
        preference.setOnPreferenceChangeListener(
            new OnPreferenceChangeListener() {
              @Override
              public boolean onPreferenceChange(Preference myPreference, Object newValue) {
                updatePreferences(myPreference, newValue);
                return true;
              }
            });
      }
    }
  }
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
          String stringValue = value.toString();
          if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list.
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);

            // Set the summary to reflect the new value.
            preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);

          } else if (preference instanceof RingtonePreference) {
            // For ringtone preferences, look up the correct display value
            // using RingtoneManager.
            if (TextUtils.isEmpty(stringValue)) {
              // Empty values correspond to 'silent' (no ringtone).
              // preference.setSummary(R.string.pref_ringtone_silent);

            } else {
              Ringtone ringtone =
                  RingtoneManager.getRingtone(preference.getContext(), Uri.parse(stringValue));

              if (ringtone == null) {
                // Clear the summary if there was a lookup error.
                preference.setSummary(null);
              } else {
                // Set the summary to reflect the new ringtone display
                // name.
                String name = ringtone.getTitle(preference.getContext());
                preference.setSummary(name);
              }
            }
          } else if (preference instanceof CheckBoxPreference) {

            SettingHelpers.p("Changing checkbox preference");

            if ("showDebugNotifications".equals(preference.getKey())) {
              SettingHelpers.p("show debug");
              boolean val = (Boolean) value;
              if (val) {
                SettingHelpers.setMultiProcessBooleanValue(
                    "showDebugNotifications",
                    true,
                    preference.getContext().getApplicationContext());
                Toast.makeText(
                        preference.getContext().getApplicationContext(),
                        "Showing now notifications",
                        Toast.LENGTH_LONG)
                    .show();
              } else {
                SettingHelpers.setMultiProcessBooleanValue(
                    "showDebugNotifications",
                    false,
                    preference.getContext().getApplicationContext());
              }
            }

            if ("enableContextDataService".equals(preference.getKey())) {
              boolean val = (Boolean) value;
              if (val) {
                SettingHelpers.p("context data service enabled");
                SettingHelpers.setMultiProcessBooleanValue(
                    "enableContextDataService",
                    true,
                    preference.getContext().getApplicationContext());
                Toast.makeText(
                        preference.getContext().getApplicationContext(),
                        "Context Data Service enabled",
                        Toast.LENGTH_LONG)
                    .show();
              } else {
                SettingHelpers.p("context data service disabled");
                SettingHelpers.setMultiProcessBooleanValue(
                    "enableContextDataService",
                    false,
                    preference.getContext().getApplicationContext());
              }
            }

          } else {
            // For all other preferences, set the summary to the value's
            // simple string representation.
            preference.setSummary(stringValue);
            SettingHelpers.setStringValue(
                preference.getKey(), stringValue, preference.getContext());
          }
          return true;
        }