/**
   * Restore the properties associated with this preference on boot
   *
   * @param ctx A valid context
   */
  public static void restore(Context ctx) {
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    CMHardwareManager hardware = CMHardwareManager.getInstance(ctx);
    if (hardware.isSupported(FEATURE_TAP_TO_WAKE)) {
      final boolean enabled = prefs.getBoolean(KEY_TAP_TO_WAKE, hardware.get(FEATURE_TAP_TO_WAKE));

      if (!hardware.set(FEATURE_TAP_TO_WAKE, enabled)) {
        Log.e(TAG, "Failed to restore tap-to-wake settings.");
      } else {
        Log.d(TAG, "Tap-to-wake settings restored.");
      }
    }
  }
 @Override
 public List<String> getNonIndexableKeys(Context context) {
   CMHardwareManager hardware = CMHardwareManager.getInstance(context);
   ArrayList<String> result = new ArrayList<String>();
   if (!context
       .getResources()
       .getBoolean(com.android.internal.R.bool.config_dreamsSupported)) {
     result.add(KEY_SCREEN_SAVER);
   }
   if (!hardware.isSupported(FEATURE_TAP_TO_WAKE)) {
     result.add(KEY_TAP_TO_WAKE);
   }
   if (!isAutomaticBrightnessAvailable(context.getResources())) {
     result.add(KEY_AUTO_BRIGHTNESS);
   }
   if (!isLiftToWakeAvailable(context)) {
     result.add(KEY_LIFT_TO_WAKE);
   }
   if (!Utils.isDozeAvailable(context)) {
     result.add(KEY_DOZE_FRAGMENT);
   }
   if (!RotationPolicy.isRotationLockToggleVisible(context)) {
     result.add(KEY_DISPLAY_ROTATION);
   }
   return result;
 }
  @Override
  public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    if (preference == mTapToWake) {
      return mHardware.set(FEATURE_TAP_TO_WAKE, mTapToWake.isChecked());
    } else if (preference == mWakeWhenPluggedOrUnplugged) {
      Settings.Global.putInt(
          getContentResolver(),
          Settings.Global.WAKE_WHEN_PLUGGED_OR_UNPLUGGED,
          mWakeWhenPluggedOrUnplugged.isChecked() ? 1 : 0);
      return true;
    }

    return super.onPreferenceTreeClick(preferenceScreen, preference);
  }
  @Override
  public void onResume() {
    super.onResume();
    updateDisplayRotationPreferenceDescription();

    if (mTapToWake != null) {
      mTapToWake.setChecked(mHardware.get(FEATURE_TAP_TO_WAKE));
    }

    RotationPolicy.registerRotationPolicyListener(getActivity(), mRotationPolicyListener);

    final ContentResolver resolver = getContentResolver();

    boolean dozeEnabled =
        Settings.Secure.getInt(getContentResolver(), Settings.Secure.DOZE_ENABLED, 1) != 0;
    if (mDozeFragement != null) {
      mDozeFragement.setSummary(
          dozeEnabled ? R.string.summary_doze_enabled : R.string.summary_doze_disabled);
    }

    // Display rotation observer
    resolver.registerContentObserver(
        Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
        true,
        mAccelerometerRotationObserver);

    // Default value for wake-on-plug behavior from config.xml
    boolean wakeUpWhenPluggedOrUnpluggedConfig =
        getResources().getBoolean(com.android.internal.R.bool.config_unplugTurnsOnScreen);

    mWakeWhenPluggedOrUnplugged.setChecked(
        Settings.Global.getInt(
                getContentResolver(),
                Settings.Global.WAKE_WHEN_PLUGGED_OR_UNPLUGGED,
                (wakeUpWhenPluggedOrUnpluggedConfig ? 1 : 0))
            == 1);

    updateState();
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Activity activity = getActivity();
    final ContentResolver resolver = activity.getContentResolver();
    mHardware = CMHardwareManager.getInstance(activity);
    addPreferencesFromResource(R.xml.display_settings);

    PreferenceCategory displayPrefs = (PreferenceCategory) findPreference(KEY_CATEGORY_DISPLAY);

    mDisplayRotationPreference = (PreferenceScreen) findPreference(KEY_DISPLAY_ROTATION);

    mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
    if (mScreenSaverPreference != null
        && getResources().getBoolean(com.android.internal.R.bool.config_dreamsSupported) == false) {
      getPreferenceScreen().removePreference(mScreenSaverPreference);
    }

    mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
    final long currentTimeout =
        Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
    mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
    mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
    disableUnusableTimeouts(mScreenTimeoutPreference);
    updateTimeoutPreferenceDescription(currentTimeout);
    updateDisplayRotationPreferenceDescription();

    mLcdDensityPreference = (ListPreference) findPreference(KEY_LCD_DENSITY);
    if (mLcdDensityPreference != null) {
      if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
        displayPrefs.removePreference(mLcdDensityPreference);
      } else {
        int defaultDensity = getDefaultDensity();
        int currentDensity = getCurrentDensity();
        if (currentDensity < 10 || currentDensity >= 1000) {
          // Unsupported value, force default
          currentDensity = defaultDensity;
        }

        int factor = defaultDensity >= 480 ? 40 : 20;
        int minimumDensity = defaultDensity - 4 * factor;
        int currentIndex = -1;
        String[] densityEntries = new String[7];
        String[] densityValues = new String[7];
        for (int idx = 0; idx < 7; ++idx) {
          int val = minimumDensity + factor * idx;
          int valueFormatResId =
              val == defaultDensity
                  ? R.string.lcd_density_default_value_format
                  : R.string.lcd_density_value_format;

          densityEntries[idx] = getString(valueFormatResId, val);
          densityValues[idx] = Integer.toString(val);
          if (currentDensity == val) {
            currentIndex = idx;
          }
        }
        mLcdDensityPreference.setEntries(densityEntries);
        mLcdDensityPreference.setEntryValues(densityValues);
        if (currentIndex != -1) {
          mLcdDensityPreference.setValueIndex(currentIndex);
        }
        mLcdDensityPreference.setOnPreferenceChangeListener(this);
        updateLcdDensityPreferenceDescription(currentDensity);
      }
    }

    mFontSizePref = (FontDialogPreference) findPreference(KEY_FONT_SIZE);
    mFontSizePref.setOnPreferenceChangeListener(this);
    mFontSizePref.setOnPreferenceClickListener(this);

    if (isAutomaticBrightnessAvailable(getResources())) {
      mAutoBrightnessPreference = (SwitchPreference) findPreference(KEY_AUTO_BRIGHTNESS);
      mAutoBrightnessPreference.setOnPreferenceChangeListener(this);
    } else {
      removePreference(KEY_AUTO_BRIGHTNESS);
    }

    if (isLiftToWakeAvailable(activity)) {
      mLiftToWakePreference = (SwitchPreference) findPreference(KEY_LIFT_TO_WAKE);
      mLiftToWakePreference.setOnPreferenceChangeListener(this);
    } else {
      removePreference(KEY_LIFT_TO_WAKE);
    }

    PreferenceCategory advancedPrefs = (PreferenceCategory) findPreference(CATEGORY_ADVANCED);

    mDozeFragement = (PreferenceScreen) findPreference(KEY_DOZE_FRAGMENT);
    if (!Utils.isDozeAvailable(activity)) {
      getPreferenceScreen().removePreference(mDozeFragement);
      mDozeFragement = null;
    }

    mTapToWake = (SwitchPreference) findPreference(KEY_TAP_TO_WAKE);
    if (!mHardware.isSupported(FEATURE_TAP_TO_WAKE)) {
      advancedPrefs.removePreference(mTapToWake);
      mTapToWake = null;
    }

    mProximityWakePreference = (SwitchPreference) findPreference(KEY_PROXIMITY_WAKE);
    boolean proximityCheckOnWake =
        getResources().getBoolean(com.android.internal.R.bool.config_proximityCheckOnWake);
    if (mProximityWakePreference != null && proximityCheckOnWake) {
      mProximityWakePreference.setOnPreferenceChangeListener(this);
    } else {
      if (advancedPrefs != null && mProximityWakePreference != null) {
        advancedPrefs.removePreference(mProximityWakePreference);
        Settings.System.putInt(getContentResolver(), Settings.System.PROXIMITY_ON_WAKE, 0);
      }
    }

    mWakeWhenPluggedOrUnplugged =
        (SwitchPreference) findPreference(KEY_WAKE_WHEN_PLUGGED_OR_UNPLUGGED);
  }