private void save() {
    // Sanity check
    boolean doSave = true;
    for (int i = 1; i < mLevels.length; i++) {
      if (mLevels[i] <= mLevels[i - 1]) {
        Toast.makeText(this, getString(R.string.ll_bad_levels), Toast.LENGTH_SHORT).show();
        doSave = false;
        break;
      }
    }
    if (doSave) {
      Settings.System.putString(
          getContentResolver(), Settings.System.LIGHT_SENSOR_LEVELS, intArrayToString(mLevels));
      Settings.System.putString(
          getContentResolver(),
          Settings.System.LIGHT_SENSOR_LCD_VALUES,
          intArrayToString(mLcdValues));
      Settings.System.putString(
          getContentResolver(),
          Settings.System.LIGHT_SENSOR_BUTTON_VALUES,
          intArrayToString(mBtnValues));
      Settings.System.putString(
          getContentResolver(),
          Settings.System.LIGHT_SENSOR_KEYBOARD_VALUES,
          intArrayToString(mKbValues));
      long tag =
          Settings.System.getLong(getContentResolver(), Settings.System.LIGHTS_CHANGED, 0) + 1;
      Settings.System.putLong(getContentResolver(), Settings.System.LIGHTS_CHANGED, tag);

      mHasChanges = false;
      mSave.setEnabled(mHasChanges);
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ContentResolver resolver = getActivity().getContentResolver();

    addPreferencesFromResource(R.xml.display_settings);

    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();

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

    mDisplayManager = (DisplayManager) getActivity().getSystemService(Context.DISPLAY_SERVICE);
    mWifiDisplayStatus = mDisplayManager.getWifiDisplayStatus();
    mWifiDisplayPreference = (Preference) findPreference(KEY_WIFI_DISPLAY);
    if (mWifiDisplayStatus.getFeatureState() == WifiDisplayStatus.FEATURE_STATE_UNAVAILABLE) {
      getPreferenceScreen().removePreference(mWifiDisplayPreference);
      mWifiDisplayPreference = null;
    }

    mVolumeWake = (CheckBoxPreference) findPreference(KEY_VOLUME_WAKE);
    if (mVolumeWake != null) {
      if (!getResources().getBoolean(R.bool.config_show_volumeRockerWake)) {
        getPreferenceScreen().removePreference(mVolumeWake);
        getPreferenceScreen()
            .removePreference((PreferenceCategory) findPreference(KEY_WAKEUP_CATEGORY));
      } else {
        mVolumeWake.setChecked(
            Settings.System.getInt(resolver, Settings.System.VOLUME_WAKE_SCREEN, 0) == 1);
      }
    }
  }
  public static void setVoiceCallDefaultSim(
      Context context,
      ContentResolver contentResolver,
      List<SimInfoRecord> simInfos,
      long[] simIdForSlot,
      int nSimCount) {

    if (!FeatureOption.MTK_BSP_PACKAGE) {
      long defSIM = Settings.System.DEFAULT_SIM_NOT_SET;
      long oldVoiceCallDefaultSIM =
          Settings.System.getLong(
              contentResolver,
              Settings.System.VOICE_CALL_SIM_SETTING,
              Settings.System.DEFAULT_SIM_NOT_SET);
      logd("nSimCount = : " + nSimCount);
      if (nSimCount > 1) {
        defSIM = Settings.System.DEFAULT_SIM_SETTING_ALWAYS_ASK;
        if (isVoiceCallDefaultSIM(oldVoiceCallDefaultSIM)) {
          logd("setVoiceCallDefaultSim -- To : " + Settings.System.DEFAULT_SIM_SETTING_ALWAYS_ASK);
          Settings.System.putLong(
              contentResolver,
              Settings.System.VOICE_CALL_SIM_SETTING,
              Settings.System.DEFAULT_SIM_SETTING_ALWAYS_ASK);
        }
      } else if (nSimCount == 1) {
        defSIM = simInfos.get(0).mSimInfoId;
        if (!(SipManager.isVoipSupported(context) && isVoipEnabled(contentResolver))
            || (isVoiceCallDefaultSIM(oldVoiceCallDefaultSIM))) {
          logd("setVoiceCallDefaultSim -- To : " + defSIM);
          Settings.System.putLong(contentResolver, Settings.System.VOICE_CALL_SIM_SETTING, defSIM);
        }
      }

      if (isSimRemoved(oldVoiceCallDefaultSIM, simIdForSlot, PhoneConstants.GEMINI_SIM_NUM)) {
        logd("setVoiceCallDefaultSim -- To : " + defSIM);
        Settings.System.putLong(contentResolver, Settings.System.VOICE_CALL_SIM_SETTING, defSIM);
      }
    }
  }
  @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);
  }