@Override
 public void onPause() {
   mSettingsPackageMonitor.unregister();
   mSettingsContentObserver.unregister(getContentResolver());
   if (RotationPolicy.isRotationSupported(getActivity())) {
     RotationPolicy.unregisterRotationPolicyListener(getActivity(), mRotationPolicyListener);
   }
   super.onPause();
 }
  @Override
  public void onResume() {
    super.onResume();
    loadInstalledServices();
    updateAllPreferences();

    mSettingsPackageMonitor.register(getActivity(), getActivity().getMainLooper(), false);
    mSettingsContentObserver.register(getContentResolver());
    if (RotationPolicy.isRotationSupported(getActivity())) {
      RotationPolicy.registerRotationPolicyListener(getActivity(), mRotationPolicyListener);
    }
  }
  private void initializeAllPreferences() {
    mServicesCategory = (PreferenceCategory) findPreference(SERVICES_CATEGORY);
    mSystemsCategory = (PreferenceCategory) findPreference(SYSTEM_CATEGORY);

    // Large text.
    mToggleLargeTextPreference = (SwitchPreference) findPreference(TOGGLE_LARGE_TEXT_PREFERENCE);

    // Text contrast.
    mToggleHighTextContrastPreference =
        (SwitchPreference) findPreference(TOGGLE_HIGH_TEXT_CONTRAST_PREFERENCE);

    // Display inversion.
    mToggleInversionPreference = (SwitchPreference) findPreference(TOGGLE_INVERSION_PREFERENCE);
    mToggleInversionPreference.setOnPreferenceChangeListener(this);

    // Power button ends calls.
    mTogglePowerButtonEndsCallPreference =
        (SwitchPreference) findPreference(TOGGLE_POWER_BUTTON_ENDS_CALL_PREFERENCE);
    if (!KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_POWER)
        || !Utils.isVoiceCapable(getActivity())) {
      mSystemsCategory.removePreference(mTogglePowerButtonEndsCallPreference);
    }

    // Lock screen rotation.
    mToggleLockScreenRotationPreference =
        (SwitchPreference) findPreference(TOGGLE_LOCK_SCREEN_ROTATION_PREFERENCE);
    if (!RotationPolicy.isRotationSupported(getActivity())) {
      mSystemsCategory.removePreference(mToggleLockScreenRotationPreference);
    }

    // Speak passwords.
    mToggleSpeakPasswordPreference =
        (SwitchPreference) findPreference(TOGGLE_SPEAK_PASSWORD_PREFERENCE);

    // Long press timeout.
    mSelectLongPressTimeoutPreference =
        (ListPreference) findPreference(SELECT_LONG_PRESS_TIMEOUT_PREFERENCE);
    mSelectLongPressTimeoutPreference.setOnPreferenceChangeListener(this);
    if (mLongPressTimeoutValuetoTitleMap.size() == 0) {
      String[] timeoutValues =
          getResources().getStringArray(R.array.long_press_timeout_selector_values);
      mLongPressTimeoutDefault = Integer.parseInt(timeoutValues[0]);
      String[] timeoutTitles =
          getResources().getStringArray(R.array.long_press_timeout_selector_titles);
      final int timeoutValueCount = timeoutValues.length;
      for (int i = 0; i < timeoutValueCount; i++) {
        mLongPressTimeoutValuetoTitleMap.put(timeoutValues[i], timeoutTitles[i]);
      }
    }

    // Captioning.
    mCaptioningPreferenceScreen = (PreferenceScreen) findPreference(CAPTIONING_PREFERENCE_SCREEN);

    // Display magnification.
    mDisplayMagnificationPreferenceScreen =
        (PreferenceScreen) findPreference(DISPLAY_MAGNIFICATION_PREFERENCE_SCREEN);

    // Display color adjustments.
    mDisplayDaltonizerPreferenceScreen =
        (PreferenceScreen) findPreference(DISPLAY_DALTONIZER_PREFERENCE_SCREEN);

    // Global gesture.
    mGlobalGesturePreferenceScreen =
        (PreferenceScreen) findPreference(ENABLE_ACCESSIBILITY_GESTURE_PREFERENCE_SCREEN);
    final int longPressOnPowerBehavior =
        getActivity()
            .getResources()
            .getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior);
    final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
    if (!KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_POWER)
        || longPressOnPowerBehavior != LONG_PRESS_POWER_GLOBAL_ACTIONS) {
      // Remove accessibility shortcut if power key is not present
      // nor long press power does not show global actions menu.
      mSystemsCategory.removePreference(mGlobalGesturePreferenceScreen);
    }
  }