2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2014 The CyanogenMod Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package com.android.settings;
19 import com.android.internal.logging.MetricsLogger;
21 import android.app.AlertDialog;
22 import android.content.DialogInterface;
23 import android.content.SharedPreferences;
24 import android.preference.CheckBoxPreference;
26 import android.os.UserHandle;
27 import android.view.Display;
28 import android.view.IWindowManager;
29 import android.view.WindowManager;
30 import android.view.WindowManagerGlobal;
31 import android.view.WindowManagerImpl;
32 import android.widget.Toast;
33 import com.android.internal.view.RotationPolicy;
34 import com.android.settings.DropDownPreference.Callback;
35 import com.android.settings.search.BaseSearchIndexProvider;
36 import com.android.settings.search.Indexable;
38 import static android.provider.Settings.Secure.CAMERA_GESTURE_DISABLED;
39 import static android.provider.Settings.Secure.DOUBLE_TAP_TO_WAKE;
40 import static android.provider.Settings.Secure.DOZE_ENABLED;
41 import static android.provider.Settings.Secure.WAKE_GESTURE_ENABLED;
42 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
43 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
44 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
45 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
47 import android.app.Activity;
48 import android.app.ActivityManagerNative;
49 import android.app.Dialog;
50 import android.app.IActivityManager;
51 import android.app.ProgressDialog;
52 import android.app.UiModeManager;
53 import android.app.admin.DevicePolicyManager;
54 import android.content.ContentResolver;
55 import android.content.Context;
56 import android.content.res.Configuration;
57 import android.content.res.Resources;
58 import android.hardware.Sensor;
59 import android.hardware.SensorManager;
60 import android.os.AsyncTask;
61 import android.os.Build;
62 import android.database.ContentObserver;
63 import android.os.Bundle;
64 import android.os.Handler;
65 import android.os.RemoteException;
66 import android.os.ServiceManager;
67 import android.os.SystemProperties;
68 import android.preference.ListPreference;
69 import android.preference.Preference;
70 import android.preference.PreferenceManager;
71 import android.preference.Preference.OnPreferenceClickListener;
72 import android.preference.PreferenceCategory;
73 import android.preference.PreferenceManager;
74 import android.preference.PreferenceScreen;
75 import android.preference.SwitchPreference;
76 import android.provider.SearchIndexableResource;
77 import android.provider.Settings;
78 import android.text.TextUtils;
79 import android.util.DisplayMetrics;
80 import android.util.Log;
82 import java.util.ArrayList;
83 import java.util.List;
84 import com.android.settings.Utils;
85 import com.android.settings.cyanogenmod.DisplayRotation;
87 import cyanogenmod.hardware.CMHardwareManager;
88 import cyanogenmod.hardware.LiveDisplayManager;
89 import cyanogenmod.providers.CMSettings;
91 public class DisplaySettings extends SettingsPreferenceFragment implements
92 Preference.OnPreferenceChangeListener, OnPreferenceClickListener, Indexable {
93 private static final String TAG = "DisplaySettings";
95 /** If there is no setting in the provider, use this. */
96 private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
98 private static final String KEY_CATEGORY_LIGHTS = "lights";
99 private static final String KEY_CATEGORY_DISPLAY = "display";
100 private static final String KEY_CATEGORY_INTERFACE = "interface";
101 private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
102 private static final String KEY_LCD_DENSITY = "lcd_density";
103 private static final String KEY_FONT_SIZE = "font_size";
104 private static final String KEY_SCREEN_SAVER = "screensaver";
105 private static final String KEY_LIFT_TO_WAKE = "lift_to_wake";
106 private static final String KEY_DOZE = "doze";
107 private static final String KEY_TAP_TO_WAKE = "tap_to_wake";
108 private static final String KEY_AUTO_BRIGHTNESS = "auto_brightness";
109 private static final String KEY_AUTO_ROTATE = "auto_rotate";
110 private static final String KEY_NIGHT_MODE = "night_mode";
111 private static final String KEY_CAMERA_GESTURE = "camera_gesture";
112 private static final String KEY_PROXIMITY_WAKE = "proximity_on_wake";
113 private static final String KEY_DISPLAY_ROTATION = "display_rotation";
114 private static final String KEY_WAKE_WHEN_PLUGGED_OR_UNPLUGGED = "wake_when_plugged_or_unplugged";
115 private static final String KEY_NOTIFICATION_LIGHT = "notification_light";
116 private static final String KEY_BATTERY_LIGHT = "battery_light";
117 private static final String KEY_LIVEDISPLAY = "live_display";
118 private static final String KEY_HIGH_TOUCH_SENSITIVITY = "high_touch_sensitivity";
120 private static final int DLG_GLOBAL_CHANGE_WARNING = 1;
122 private ListPreference mLcdDensityPreference;
123 private FontDialogPreference mFontSizePref;
124 private PreferenceScreen mDisplayRotationPreference;
125 private PreferenceScreen mLiveDisplayPreference;
127 private final Configuration mCurConfig = new Configuration();
129 private ListPreference mScreenTimeoutPreference;
130 private ListPreference mNightModePreference;
131 private Preference mScreenSaverPreference;
132 private SwitchPreference mAccelerometer;
133 private SwitchPreference mLiftToWakePreference;
134 private SwitchPreference mDozePreference;
135 private SwitchPreference mTapToWakePreference;
136 private SwitchPreference mHighTouchSensitivity;
137 private SwitchPreference mProximityCheckOnWakePreference;
138 private SwitchPreference mAutoBrightnessPreference;
139 private SwitchPreference mWakeWhenPluggedOrUnplugged;
141 private CMHardwareManager mHardware;
143 private ContentObserver mAccelerometerRotationObserver =
144 new ContentObserver(new Handler()) {
146 public void onChange(boolean selfChange) {
147 updateDisplayRotationPreferenceDescription();
148 updateAccelerometerRotationSwitch();
152 private final RotationPolicy.RotationPolicyListener mRotationPolicyListener =
153 new RotationPolicy.RotationPolicyListener() {
155 public void onChange() {
156 updateDisplayRotationPreferenceDescription();
159 private SwitchPreference mCameraGesturePreference;
162 protected int getMetricsCategory() {
163 return MetricsLogger.DISPLAY;
167 public void onCreate(Bundle savedInstanceState) {
168 super.onCreate(savedInstanceState);
169 final Activity activity = getActivity();
170 final ContentResolver resolver = activity.getContentResolver();
171 addPreferencesFromResource(R.xml.display);
173 mHardware = CMHardwareManager.getInstance(activity);
175 PreferenceCategory displayPrefs = (PreferenceCategory)
176 findPreference(KEY_CATEGORY_DISPLAY);
177 PreferenceCategory interfacePrefs = (PreferenceCategory)
178 findPreference(KEY_CATEGORY_INTERFACE);
179 mDisplayRotationPreference = (PreferenceScreen) findPreference(KEY_DISPLAY_ROTATION);
180 mAccelerometer = (SwitchPreference) findPreference(DisplayRotation.KEY_ACCELEROMETER);
181 if (mAccelerometer != null) {
182 mAccelerometer.setPersistent(false);
185 mLiveDisplayPreference = (PreferenceScreen) findPreference(KEY_LIVEDISPLAY);
186 if (!LiveDisplayManager.getInstance(getActivity()).getConfig().isAvailable()) {
187 displayPrefs.removePreference(mLiveDisplayPreference);
190 mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
191 if (mScreenSaverPreference != null
192 && getResources().getBoolean(
193 com.android.internal.R.bool.config_dreamsSupported) == false) {
194 interfacePrefs.removePreference(mScreenSaverPreference);
197 mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
198 final long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT,
199 FALLBACK_SCREEN_TIMEOUT_VALUE);
200 mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
201 mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
202 disableUnusableTimeouts(mScreenTimeoutPreference);
203 updateTimeoutPreferenceDescription(currentTimeout);
204 updateDisplayRotationPreferenceDescription();
206 mLcdDensityPreference = (ListPreference) findPreference(KEY_LCD_DENSITY);
207 if (mLcdDensityPreference != null) {
208 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
209 interfacePrefs.removePreference(mLcdDensityPreference);
211 int defaultDensity = getDefaultDensity();
212 int currentDensity = getCurrentDensity();
213 if (currentDensity < 10 || currentDensity >= 1000) {
214 // Unsupported value, force default
215 currentDensity = defaultDensity;
218 int factor = defaultDensity >= 480 ? 40 : 20;
219 int minimumDensity = defaultDensity - 4 * factor;
220 int currentIndex = -1;
221 String[] densityEntries = new String[7];
222 String[] densityValues = new String[7];
223 for (int idx = 0; idx < 7; ++idx) {
224 int val = minimumDensity + factor * idx;
225 int valueFormatResId = val == defaultDensity
226 ? R.string.lcd_density_default_value_format
227 : R.string.lcd_density_value_format;
229 densityEntries[idx] = getString(valueFormatResId, val);
230 densityValues[idx] = Integer.toString(val);
231 if (currentDensity == val) {
235 mLcdDensityPreference.setEntries(densityEntries);
236 mLcdDensityPreference.setEntryValues(densityValues);
237 if (currentIndex != -1) {
238 mLcdDensityPreference.setValueIndex(currentIndex);
240 mLcdDensityPreference.setOnPreferenceChangeListener(this);
241 updateLcdDensityPreferenceDescription(currentDensity);
245 mFontSizePref = (FontDialogPreference) findPreference(KEY_FONT_SIZE);
246 mFontSizePref.setOnPreferenceChangeListener(this);
247 mFontSizePref.setOnPreferenceClickListener(this);
249 mAutoBrightnessPreference = (SwitchPreference) findPreference(KEY_AUTO_BRIGHTNESS);
250 if (mAutoBrightnessPreference != null && isAutomaticBrightnessAvailable(getResources())) {
251 mAutoBrightnessPreference.setOnPreferenceChangeListener(this);
253 if (displayPrefs != null && mAutoBrightnessPreference != null) {
254 displayPrefs.removePreference(mAutoBrightnessPreference);
255 mAutoBrightnessPreference = null;
259 mLiftToWakePreference = (SwitchPreference) findPreference(KEY_LIFT_TO_WAKE);
260 if (mLiftToWakePreference != null && isLiftToWakeAvailable(activity)) {
261 mLiftToWakePreference.setOnPreferenceChangeListener(this);
263 if (displayPrefs != null && mLiftToWakePreference != null) {
264 displayPrefs.removePreference(mLiftToWakePreference);
265 mLiftToWakePreference = null;
269 mDozePreference = (SwitchPreference) findPreference(KEY_DOZE);
270 if (mDozePreference != null && Utils.isDozeAvailable(activity)) {
271 mDozePreference.setOnPreferenceChangeListener(this);
273 if (displayPrefs != null && mDozePreference != null) {
274 displayPrefs.removePreference(mDozePreference);
278 mCameraGesturePreference = (SwitchPreference) findPreference(KEY_CAMERA_GESTURE);
279 if (mCameraGesturePreference != null && isCameraGestureAvailable(getResources())) {
280 mCameraGesturePreference.setOnPreferenceChangeListener(this);
282 if (displayPrefs != null && mCameraGesturePreference != null) {
283 displayPrefs.removePreference(mCameraGesturePreference);
287 mNightModePreference = (ListPreference) findPreference(KEY_NIGHT_MODE);
288 if (mNightModePreference != null) {
289 final UiModeManager uiManager = (UiModeManager) getSystemService(
290 Context.UI_MODE_SERVICE);
291 final int currentNightMode = uiManager.getNightMode();
292 mNightModePreference.setValue(String.valueOf(currentNightMode));
293 mNightModePreference.setOnPreferenceChangeListener(this);
296 mTapToWakePreference = (SwitchPreference) findPreference(KEY_TAP_TO_WAKE);
297 if (mTapToWakePreference != null && isTapToWakeAvailable(getResources())) {
298 mTapToWakePreference.setOnPreferenceChangeListener(this);
300 if (displayPrefs != null && mTapToWakePreference != null) {
301 displayPrefs.removePreference(mTapToWakePreference);
305 mHighTouchSensitivity = (SwitchPreference) findPreference(KEY_HIGH_TOUCH_SENSITIVITY);
306 if (!mHardware.isSupported(
307 CMHardwareManager.FEATURE_HIGH_TOUCH_SENSITIVITY)) {
308 displayPrefs.removePreference(mHighTouchSensitivity);
309 mHighTouchSensitivity = null;
311 mHighTouchSensitivity.setChecked(
312 mHardware.get(CMHardwareManager.FEATURE_HIGH_TOUCH_SENSITIVITY));
315 mProximityCheckOnWakePreference = (SwitchPreference) findPreference(KEY_PROXIMITY_WAKE);
316 boolean proximityCheckOnWake = getResources().getBoolean(
317 org.cyanogenmod.platform.internal.R.bool.config_proximityCheckOnWake);
318 if (!proximityCheckOnWake) {
319 if (displayPrefs != null && mProximityCheckOnWakePreference != null) {
320 displayPrefs.removePreference(mProximityCheckOnWakePreference);
322 CMSettings.System.putInt(getContentResolver(), CMSettings.System.PROXIMITY_ON_WAKE, 0);
324 boolean proximityCheckOnWakeDefault = getResources().getBoolean(
325 org.cyanogenmod.platform.internal.R.bool.config_proximityCheckOnWakeEnabledByDefault);
326 mProximityCheckOnWakePreference.setChecked(CMSettings.System.getInt(getContentResolver(),
327 CMSettings.System.PROXIMITY_ON_WAKE,
328 (proximityCheckOnWakeDefault ? 1 : 0)) == 1);
331 mWakeWhenPluggedOrUnplugged =
332 (SwitchPreference) findPreference(KEY_WAKE_WHEN_PLUGGED_OR_UNPLUGGED);
333 initPulse((PreferenceCategory) findPreference(KEY_CATEGORY_LIGHTS));
336 private int getDefaultDensity() {
337 IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.checkService(
338 Context.WINDOW_SERVICE));
340 return wm.getInitialDisplayDensity(Display.DEFAULT_DISPLAY);
341 } catch (RemoteException e) {
344 return DisplayMetrics.DENSITY_DEVICE;
347 private int getCurrentDensity() {
348 IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.checkService(
349 Context.WINDOW_SERVICE));
351 return wm.getBaseDisplayDensity(Display.DEFAULT_DISPLAY);
352 } catch (RemoteException e) {
355 return DisplayMetrics.DENSITY_DEVICE;
358 private static boolean allowAllRotations(Context context) {
359 return Resources.getSystem().getBoolean(
360 com.android.internal.R.bool.config_allowAllRotations);
363 private static boolean isLiftToWakeAvailable(Context context) {
364 SensorManager sensors = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
365 return sensors != null && sensors.getDefaultSensor(Sensor.TYPE_WAKE_GESTURE) != null;
368 private static boolean isTapToWakeAvailable(Resources res) {
369 return res.getBoolean(com.android.internal.R.bool.config_supportDoubleTapWake);
372 private static boolean isAutomaticBrightnessAvailable(Resources res) {
373 return res.getBoolean(com.android.internal.R.bool.config_automatic_brightness_available);
376 private void updateAccelerometerRotationSwitch() {
377 if (mAccelerometer != null) {
378 mAccelerometer.setChecked(!RotationPolicy.isRotationLocked(getActivity()));
381 private void updateDisplayRotationPreferenceDescription() {
382 if (mDisplayRotationPreference == null) {
383 // The preference was removed, do nothing
387 // We have a preference, lets update the summary
388 boolean rotationEnabled = Settings.System.getInt(getContentResolver(),
389 Settings.System.ACCELEROMETER_ROTATION, 0) != 0;
391 if (!rotationEnabled) {
392 mDisplayRotationPreference.setSummary(R.string.display_rotation_disabled);
396 StringBuilder summary = new StringBuilder();
397 int mode = Settings.System.getInt(getContentResolver(),
398 Settings.System.ACCELEROMETER_ROTATION_ANGLES,
399 DisplayRotation.ROTATION_0_MODE
400 | DisplayRotation.ROTATION_90_MODE
401 | DisplayRotation.ROTATION_270_MODE);
402 ArrayList<String> rotationList = new ArrayList<String>();
405 if ((mode & DisplayRotation.ROTATION_0_MODE) != 0) {
406 rotationList.add("0");
408 if ((mode & DisplayRotation.ROTATION_90_MODE) != 0) {
409 rotationList.add("90");
411 if ((mode & DisplayRotation.ROTATION_180_MODE) != 0) {
412 rotationList.add("180");
414 if ((mode & DisplayRotation.ROTATION_270_MODE) != 0) {
415 rotationList.add("270");
417 for (int i = 0; i < rotationList.size(); i++) {
418 summary.append(delim).append(rotationList.get(i));
419 if ((rotationList.size() - i) > 2) {
425 summary.append(" " + getString(R.string.display_rotation_unit));
426 mDisplayRotationPreference.setSummary(summary);
429 private static boolean isCameraGestureAvailable(Resources res) {
430 boolean configSet = res.getInteger(
431 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
433 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
436 private void updateTimeoutPreferenceDescription(long currentTimeout) {
437 ListPreference preference = mScreenTimeoutPreference;
439 if (currentTimeout < 0) {
443 final CharSequence[] entries = preference.getEntries();
444 final CharSequence[] values = preference.getEntryValues();
445 if (entries == null || entries.length == 0) {
449 for (int i = 0; i < values.length; i++) {
450 long timeout = Long.parseLong(values[i].toString());
451 if (currentTimeout >= timeout) {
455 summary = preference.getContext().getString(R.string.screen_timeout_summary,
459 preference.setSummary(summary);
462 private void updateLcdDensityPreferenceDescription(int currentDensity) {
463 final int summaryResId = currentDensity == getDefaultDensity()
464 ? R.string.lcd_density_default_value_format : R.string.lcd_density_value_format;
465 mLcdDensityPreference.setSummary(getString(summaryResId, currentDensity));
468 private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) {
469 final DevicePolicyManager dpm =
470 (DevicePolicyManager) getActivity().getSystemService(
471 Context.DEVICE_POLICY_SERVICE);
472 final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0;
473 if (maxTimeout == 0) {
474 return; // policy not enforced
476 final CharSequence[] entries = screenTimeoutPreference.getEntries();
477 final CharSequence[] values = screenTimeoutPreference.getEntryValues();
478 ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
479 ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
480 for (int i = 0; i < values.length; i++) {
481 long timeout = Long.parseLong(values[i].toString());
482 if (timeout <= maxTimeout) {
483 revisedEntries.add(entries[i]);
484 revisedValues.add(values[i]);
487 if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) {
488 final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue());
489 screenTimeoutPreference.setEntries(
490 revisedEntries.toArray(new CharSequence[revisedEntries.size()]));
491 screenTimeoutPreference.setEntryValues(
492 revisedValues.toArray(new CharSequence[revisedValues.size()]));
493 if (userPreference <= maxTimeout) {
494 screenTimeoutPreference.setValue(String.valueOf(userPreference));
495 } else if (revisedValues.size() > 0
496 && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString())
498 // If the last one happens to be the same as the max timeout, select that
499 screenTimeoutPreference.setValue(String.valueOf(maxTimeout));
501 // There will be no highlighted selection since nothing in the list matches
502 // maxTimeout. The user can still select anything less than maxTimeout.
503 // TODO: maybe append maxTimeout to the list and mark selected.
506 screenTimeoutPreference.setEnabled(revisedEntries.size() > 0);
510 public void onResume() {
512 updateDisplayRotationPreferenceDescription();
514 RotationPolicy.registerRotationPolicyListener(getActivity(),
515 mRotationPolicyListener);
517 final ContentResolver resolver = getContentResolver();
519 // Display rotation observer
520 resolver.registerContentObserver(
521 Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true,
522 mAccelerometerRotationObserver);
524 // Default value for wake-on-plug behavior from config.xml
525 boolean wakeUpWhenPluggedOrUnpluggedConfig = getResources().getBoolean(
526 com.android.internal.R.bool.config_unplugTurnsOnScreen);
528 if (mWakeWhenPluggedOrUnplugged != null) {
529 mWakeWhenPluggedOrUnplugged.setChecked(CMSettings.Global.getInt(getContentResolver(),
530 CMSettings.Global.WAKE_WHEN_PLUGGED_OR_UNPLUGGED,
531 (wakeUpWhenPluggedOrUnpluggedConfig ? 1 : 0)) == 1);
535 updateAccelerometerRotationSwitch();
539 public void onPause() {
542 RotationPolicy.unregisterRotationPolicyListener(getActivity(),
543 mRotationPolicyListener);
545 // Display rotation observer
546 getContentResolver().unregisterContentObserver(mAccelerometerRotationObserver);
550 public Dialog onCreateDialog(int dialogId) {
551 if (dialogId == DLG_GLOBAL_CHANGE_WARNING) {
552 return Utils.buildGlobalChangeWarningDialog(getActivity(),
553 R.string.global_font_change_title,
556 mFontSizePref.click();
563 private void updateState() {
564 readFontSizePreference(mFontSizePref);
565 updateScreenSaverSummary();
567 // Update auto brightness if it is available.
568 if (mAutoBrightnessPreference != null) {
569 int brightnessMode = Settings.System.getInt(getContentResolver(),
570 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
571 mAutoBrightnessPreference.setChecked(brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL);
574 // Update lift-to-wake if it is available.
575 if (mLiftToWakePreference != null) {
576 int value = Settings.Secure.getInt(getContentResolver(), WAKE_GESTURE_ENABLED, 0);
577 mLiftToWakePreference.setChecked(value != 0);
580 // Update doze if it is available.
581 if (mDozePreference != null) {
582 int value = Settings.Secure.getInt(getContentResolver(), DOZE_ENABLED, 1);
583 mDozePreference.setChecked(value != 0);
586 // Update tap to wake if it is available.
587 if (mTapToWakePreference != null) {
588 int value = Settings.Secure.getInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, 0);
589 mTapToWakePreference.setChecked(value != 0);
592 // Update camera gesture #1 if it is available.
593 if (mCameraGesturePreference != null) {
594 int value = Settings.Secure.getInt(getContentResolver(), CAMERA_GESTURE_DISABLED, 0);
595 mCameraGesturePreference.setChecked(value == 0);
599 private void updateScreenSaverSummary() {
600 if (mScreenSaverPreference != null) {
601 mScreenSaverPreference.setSummary(
602 DreamSettings.getSummaryTextWithDreamName(getActivity()));
606 private void writeLcdDensityPreference(final Context context, final int density) {
607 final IActivityManager am = ActivityManagerNative.asInterface(
608 ServiceManager.checkService("activity"));
609 final IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.checkService(
610 Context.WINDOW_SERVICE));
611 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
613 protected void onPreExecute() {
614 ProgressDialog dialog = new ProgressDialog(context);
615 dialog.setMessage(getResources().getString(R.string.restarting_ui));
616 dialog.setCancelable(false);
617 dialog.setIndeterminate(true);
621 protected Void doInBackground(Void... params) {
622 // Give the user a second to see the dialog
625 } catch (InterruptedException e) {
630 wm.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, density);
631 } catch (RemoteException e) {
632 Log.e(TAG, "Failed to set density to " + density, e);
638 } catch (RemoteException e) {
639 Log.e(TAG, "Failed to restart");
647 // === Pulse notification light ===
649 private void initPulse(PreferenceCategory parent) {
650 if (!getResources().getBoolean(
651 com.android.internal.R.bool.config_intrusiveNotificationLed)) {
652 parent.removePreference(parent.findPreference(KEY_NOTIFICATION_LIGHT));
654 if (!getResources().getBoolean(
655 com.android.internal.R.bool.config_intrusiveBatteryLed)
656 || UserHandle.myUserId() != UserHandle.USER_OWNER) {
657 parent.removePreference(parent.findPreference(KEY_BATTERY_LIGHT));
659 if (parent.getPreferenceCount() == 0) {
660 getPreferenceScreen().removePreference(parent);
664 * Reads the current font size and sets the value in the summary text
666 public void readFontSizePreference(Preference pref) {
668 mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
669 } catch (RemoteException e) {
670 Log.w(TAG, "Unable to retrieve font size");
673 // report the current size in the summary text
674 final Resources res = getResources();
675 String fontDesc = FontDialogPreference.getFontSizeDescription(res, mCurConfig.fontScale);
676 pref.setSummary(getString(R.string.summary_font_size, fontDesc));
679 public void writeFontSizePreference(Object objValue) {
681 mCurConfig.fontScale = Float.parseFloat(objValue.toString());
682 ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
683 } catch (RemoteException e) {
684 Log.w(TAG, "Unable to save font size");
689 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
690 if (preference == mWakeWhenPluggedOrUnplugged) {
691 CMSettings.Global.putInt(getContentResolver(),
692 CMSettings.Global.WAKE_WHEN_PLUGGED_OR_UNPLUGGED,
693 mWakeWhenPluggedOrUnplugged.isChecked() ? 1 : 0);
695 } else if (preference == mAccelerometer) {
696 RotationPolicy.setRotationLockForAccessibility(getActivity(),
697 !mAccelerometer.isChecked());
698 } else if (preference == mHighTouchSensitivity) {
699 boolean mHighTouchSensitivityEnable = mHighTouchSensitivity.isChecked();
700 CMSettings.System.putInt(getActivity().getContentResolver(),
701 CMSettings.System.HIGH_TOUCH_SENSITIVITY_ENABLE,
702 mHighTouchSensitivityEnable ? 1 : 0);
706 return super.onPreferenceTreeClick(preferenceScreen, preference);
710 public boolean onPreferenceChange(Preference preference, Object objValue) {
711 final String key = preference.getKey();
712 if (KEY_SCREEN_TIMEOUT.equals(key)) {
714 int value = Integer.parseInt((String) objValue);
715 Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
716 updateTimeoutPreferenceDescription(value);
717 } catch (NumberFormatException e) {
718 Log.e(TAG, "could not persist screen timeout setting", e);
721 if (KEY_LCD_DENSITY.equals(key)) {
722 String newValue = (String) objValue;
723 String oldValue = mLcdDensityPreference.getValue();
724 if (!TextUtils.equals(newValue, oldValue)) {
725 showLcdConfirmationDialog((String) objValue);
729 if (KEY_FONT_SIZE.equals(key)) {
730 writeFontSizePreference(objValue);
732 if (preference == mAutoBrightnessPreference) {
733 boolean auto = (Boolean) objValue;
734 Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE,
735 auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
737 if (preference == mLiftToWakePreference) {
738 boolean value = (Boolean) objValue;
739 Settings.Secure.putInt(getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0);
741 if (preference == mDozePreference) {
742 boolean value = (Boolean) objValue;
743 Settings.Secure.putInt(getContentResolver(), DOZE_ENABLED, value ? 1 : 0);
745 if (preference == mTapToWakePreference) {
746 boolean value = (Boolean) objValue;
747 Settings.Secure.putInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, value ? 1 : 0);
749 if (preference == mCameraGesturePreference) {
750 boolean value = (Boolean) objValue;
751 Settings.Secure.putInt(getContentResolver(), CAMERA_GESTURE_DISABLED,
752 value ? 0 : 1 /* Backwards because setting is for disabling */);
754 if (preference == mNightModePreference) {
756 final int value = Integer.parseInt((String) objValue);
757 final UiModeManager uiManager = (UiModeManager) getSystemService(
758 Context.UI_MODE_SERVICE);
759 uiManager.setNightMode(value);
760 } catch (NumberFormatException e) {
761 Log.e(TAG, "could not persist night mode setting", e);
767 private void showLcdConfirmationDialog(final String lcdDensity) {
768 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
769 builder.setTitle(R.string.lcd_density);
770 builder.setMessage(R.string.lcd_density_prompt_message);
771 builder.setPositiveButton(R.string.print_restart,
772 new DialogInterface.OnClickListener() {
773 public void onClick(DialogInterface dialog, int id) {
775 int value = Integer.parseInt(lcdDensity);
776 writeLcdDensityPreference(getActivity(), value);
777 updateLcdDensityPreferenceDescription(value);
778 } catch (NumberFormatException e) {
779 Log.e(TAG, "could not persist display density setting", e);
783 builder.setNegativeButton(android.R.string.cancel, null);
788 public boolean onPreferenceClick(Preference preference) {
789 if (preference == mFontSizePref) {
790 if (Utils.hasMultipleUsers(getActivity())) {
791 showDialog(DLG_GLOBAL_CHANGE_WARNING);
794 mFontSizePref.click();
801 protected int getHelpResource() {
802 return R.string.help_uri_display;
805 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
806 new BaseSearchIndexProvider() {
809 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
811 ArrayList<SearchIndexableResource> result =
812 new ArrayList<SearchIndexableResource>();
814 SearchIndexableResource sir = new SearchIndexableResource(context);
815 sir.xmlResId = R.xml.display;
822 public List<String> getNonIndexableKeys(Context context) {
823 final CMHardwareManager hardware = CMHardwareManager.getInstance(context);
825 ArrayList<String> result = new ArrayList<String>();
826 if (!context.getResources().getBoolean(
827 com.android.internal.R.bool.config_dreamsSupported)) {
828 result.add(KEY_SCREEN_SAVER);
830 if (!context.getResources().getBoolean(
831 com.android.internal.R.bool.config_intrusiveNotificationLed)) {
832 result.add(KEY_NOTIFICATION_LIGHT);
834 if (!context.getResources().getBoolean(
835 com.android.internal.R.bool.config_intrusiveBatteryLed)) {
836 result.add(KEY_BATTERY_LIGHT);
838 if (!isAutomaticBrightnessAvailable(context.getResources())) {
839 result.add(KEY_AUTO_BRIGHTNESS);
841 if (!isLiftToWakeAvailable(context)) {
842 result.add(KEY_LIFT_TO_WAKE);
844 if (!Utils.isDozeAvailable(context)) {
845 result.add(KEY_DOZE);
847 if (!isTapToWakeAvailable(context.getResources())) {
848 result.add(KEY_TAP_TO_WAKE);
850 if (!context.getResources().getBoolean(
851 org.cyanogenmod.platform.internal.R.bool.config_proximityCheckOnWake)) {
852 result.add(KEY_PROXIMITY_WAKE);
854 if (!isCameraGestureAvailable(context.getResources())) {
855 result.add(KEY_CAMERA_GESTURE);
857 if (hardware.isSupported(CMHardwareManager.FEATURE_HIGH_TOUCH_SENSITIVITY)) {
858 result.add(KEY_HIGH_TOUCH_SENSITIVITY);
864 public static void restore(Context context) {
865 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
866 final CMHardwareManager hardware = CMHardwareManager.getInstance(context);
867 if (hardware.isSupported(CMHardwareManager.FEATURE_HIGH_TOUCH_SENSITIVITY)) {
868 final boolean enabled = prefs.getBoolean(KEY_HIGH_TOUCH_SENSITIVITY,
869 hardware.get(CMHardwareManager.FEATURE_HIGH_TOUCH_SENSITIVITY));
870 CMSettings.System.putInt(context.getContentResolver(),
871 CMSettings.System.HIGH_TOUCH_SENSITIVITY_ENABLE,