blob: 275d465e6a7a78272e758656cd2c9a9d5e6354ea [file] [log] [blame]
Amith Yamasani992f1022010-01-25 09:17:53 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jason Monkd30e4262015-08-04 11:22:29 -040019import static android.provider.Settings.Secure.CAMERA_GESTURE_DISABLED;
Jason Monk86dd6ba2015-03-31 16:39:28 -040020import static android.provider.Settings.Secure.DOUBLE_TAP_TO_WAKE;
John Spurlock911de7f2014-08-04 14:53:28 -040021import static android.provider.Settings.Secure.DOZE_ENABLED;
John Spurlock333efe92014-07-23 13:01:21 -040022import static android.provider.Settings.Secure.WAKE_GESTURE_ENABLED;
Adrian Roos244e8ce2014-05-13 17:00:46 +020023import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
24import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
25import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
Amith Yamasani992f1022010-01-25 09:17:53 -080026import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
27
Jason Monkd3232da2014-08-20 13:29:20 -040028import android.app.Activity;
Dianne Hackborn6677e562011-03-04 17:30:54 -080029import android.app.ActivityManagerNative;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070030import android.app.Dialog;
Alan Viverette893b3002015-02-25 18:47:37 -080031import android.app.UiModeManager;
Jim Millerd90b5a82010-03-29 20:18:51 -070032import android.app.admin.DevicePolicyManager;
Amith Yamasani992f1022010-01-25 09:17:53 -080033import android.content.ContentResolver;
34import android.content.Context;
Dianne Hackborn6677e562011-03-04 17:30:54 -080035import android.content.res.Configuration;
Christopher Tate8a510c52011-07-29 16:16:22 -070036import android.content.res.Resources;
John Spurlock333efe92014-07-23 13:01:21 -040037import android.hardware.Sensor;
38import android.hardware.SensorManager;
John Spurlock911de7f2014-08-04 14:53:28 -040039import android.os.Build;
Amith Yamasani992f1022010-01-25 09:17:53 -080040import android.os.Bundle;
Amith Yamasani992f1022010-01-25 09:17:53 -080041import android.os.RemoteException;
John Spurlock911de7f2014-08-04 14:53:28 -040042import android.os.SystemProperties;
Amith Yamasani992f1022010-01-25 09:17:53 -080043import android.preference.ListPreference;
44import android.preference.Preference;
Jason Monk49b61032015-08-12 11:31:29 -040045import android.preference.Preference.OnPreferenceChangeListener;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070046import android.preference.Preference.OnPreferenceClickListener;
Amith Yamasani992f1022010-01-25 09:17:53 -080047import android.preference.PreferenceScreen;
John Spurlock333efe92014-07-23 13:01:21 -040048import android.preference.SwitchPreference;
Adrian Roos8ab09542014-06-02 20:33:56 +020049import android.provider.SearchIndexableResource;
Amith Yamasani992f1022010-01-25 09:17:53 -080050import android.provider.Settings;
John Spurlock911de7f2014-08-04 14:53:28 -040051import android.text.TextUtils;
Amith Yamasani992f1022010-01-25 09:17:53 -080052import android.util.Log;
Amith Yamasani992f1022010-01-25 09:17:53 -080053
Jason Monk49b61032015-08-12 11:31:29 -040054import com.android.internal.logging.MetricsLogger;
55import com.android.internal.view.RotationPolicy;
56import com.android.settings.search.BaseSearchIndexProvider;
57import com.android.settings.search.Indexable;
58
Amith Yamasanid7993472010-08-18 13:59:28 -070059import java.util.ArrayList;
Adrian Roos8ab09542014-06-02 20:33:56 +020060import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070061
62public class DisplaySettings extends SettingsPreferenceFragment implements
Adrian Roos8ab09542014-06-02 20:33:56 +020063 Preference.OnPreferenceChangeListener, OnPreferenceClickListener, Indexable {
Amith Yamasani992f1022010-01-25 09:17:53 -080064 private static final String TAG = "DisplaySettings";
65
66 /** If there is no setting in the provider, use this. */
67 private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
68
69 private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
Dianne Hackborn6677e562011-03-04 17:30:54 -080070 private static final String KEY_FONT_SIZE = "font_size";
Daniel Sandler6f0ea442011-11-17 10:44:51 -080071 private static final String KEY_SCREEN_SAVER = "screensaver";
John Spurlock333efe92014-07-23 13:01:21 -040072 private static final String KEY_LIFT_TO_WAKE = "lift_to_wake";
John Spurlock911de7f2014-08-04 14:53:28 -040073 private static final String KEY_DOZE = "doze";
Jason Monk86dd6ba2015-03-31 16:39:28 -040074 private static final String KEY_TAP_TO_WAKE = "tap_to_wake";
Adrian Roos244e8ce2014-05-13 17:00:46 +020075 private static final String KEY_AUTO_BRIGHTNESS = "auto_brightness";
Jason Monkd3232da2014-08-20 13:29:20 -040076 private static final String KEY_AUTO_ROTATE = "auto_rotate";
Alan Viverette893b3002015-02-25 18:47:37 -080077 private static final String KEY_NIGHT_MODE = "night_mode";
Jason Monkd30e4262015-08-04 11:22:29 -040078 private static final String KEY_CAMERA_GESTURE = "camera_gesture";
Jeff Brown9e143f52012-09-19 20:46:07 -070079
Amith Yamasani9627a8e2012-09-23 12:54:14 -070080 private static final int DLG_GLOBAL_CHANGE_WARNING = 1;
81
Amith Yamasani9627a8e2012-09-23 12:54:14 -070082 private WarnedListPreference mFontSizePref;
Amith Yamasani992f1022010-01-25 09:17:53 -080083
Dianne Hackborn6677e562011-03-04 17:30:54 -080084 private final Configuration mCurConfig = new Configuration();
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +090085
Jim Miller9391b952011-01-07 16:55:33 -080086 private ListPreference mScreenTimeoutPreference;
Alan Viverette893b3002015-02-25 18:47:37 -080087 private ListPreference mNightModePreference;
Daniel Sandler6f0ea442011-11-17 10:44:51 -080088 private Preference mScreenSaverPreference;
John Spurlock333efe92014-07-23 13:01:21 -040089 private SwitchPreference mLiftToWakePreference;
John Spurlock911de7f2014-08-04 14:53:28 -040090 private SwitchPreference mDozePreference;
Jason Monk86dd6ba2015-03-31 16:39:28 -040091 private SwitchPreference mTapToWakePreference;
John Spurlock333efe92014-07-23 13:01:21 -040092 private SwitchPreference mAutoBrightnessPreference;
Jason Monkd30e4262015-08-04 11:22:29 -040093 private SwitchPreference mCameraGesturePreference;
Jim Miller9391b952011-01-07 16:55:33 -080094
Amith Yamasani992f1022010-01-25 09:17:53 -080095 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -040096 protected int getMetricsCategory() {
97 return MetricsLogger.DISPLAY;
98 }
99
100 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700101 public void onCreate(Bundle savedInstanceState) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800102 super.onCreate(savedInstanceState);
Jason Monkd3232da2014-08-20 13:29:20 -0400103 final Activity activity = getActivity();
104 final ContentResolver resolver = activity.getContentResolver();
Amith Yamasani992f1022010-01-25 09:17:53 -0800105
106 addPreferencesFromResource(R.xml.display_settings);
107
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800108 mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
Daniel Sandler47c991f2012-02-01 14:14:18 -0500109 if (mScreenSaverPreference != null
110 && getResources().getBoolean(
John Spurlockd983e1d2012-10-18 16:41:09 -0400111 com.android.internal.R.bool.config_dreamsSupported) == false) {
Daniel Sandler47c991f2012-02-01 14:14:18 -0500112 getPreferenceScreen().removePreference(mScreenSaverPreference);
113 }
Amith Yamasani78b82652013-08-16 16:35:54 -0700114
Jim Miller9391b952011-01-07 16:55:33 -0800115 mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
116 final long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT,
117 FALLBACK_SCREEN_TIMEOUT_VALUE);
118 mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
119 mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
120 disableUnusableTimeouts(mScreenTimeoutPreference);
Gilles Debunne698c24f2011-09-02 15:47:24 -0700121 updateTimeoutPreferenceDescription(currentTimeout);
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400122
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700123 mFontSizePref = (WarnedListPreference) findPreference(KEY_FONT_SIZE);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800124 mFontSizePref.setOnPreferenceChangeListener(this);
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700125 mFontSizePref.setOnPreferenceClickListener(this);
Adrian Roos244e8ce2014-05-13 17:00:46 +0200126
Adrian Roos8ab09542014-06-02 20:33:56 +0200127 if (isAutomaticBrightnessAvailable(getResources())) {
John Spurlock333efe92014-07-23 13:01:21 -0400128 mAutoBrightnessPreference = (SwitchPreference) findPreference(KEY_AUTO_BRIGHTNESS);
Adrian Roos244e8ce2014-05-13 17:00:46 +0200129 mAutoBrightnessPreference.setOnPreferenceChangeListener(this);
130 } else {
131 removePreference(KEY_AUTO_BRIGHTNESS);
132 }
John Spurlock333efe92014-07-23 13:01:21 -0400133
Jason Monkd3232da2014-08-20 13:29:20 -0400134 if (isLiftToWakeAvailable(activity)) {
John Spurlock333efe92014-07-23 13:01:21 -0400135 mLiftToWakePreference = (SwitchPreference) findPreference(KEY_LIFT_TO_WAKE);
136 mLiftToWakePreference.setOnPreferenceChangeListener(this);
137 } else {
138 removePreference(KEY_LIFT_TO_WAKE);
139 }
John Spurlock911de7f2014-08-04 14:53:28 -0400140
Jason Monkd3232da2014-08-20 13:29:20 -0400141 if (isDozeAvailable(activity)) {
John Spurlock911de7f2014-08-04 14:53:28 -0400142 mDozePreference = (SwitchPreference) findPreference(KEY_DOZE);
143 mDozePreference.setOnPreferenceChangeListener(this);
144 } else {
145 removePreference(KEY_DOZE);
146 }
Jason Monkd3232da2014-08-20 13:29:20 -0400147
Jason Monk86dd6ba2015-03-31 16:39:28 -0400148 if (isTapToWakeAvailable(getResources())) {
149 mTapToWakePreference = (SwitchPreference) findPreference(KEY_TAP_TO_WAKE);
150 mTapToWakePreference.setOnPreferenceChangeListener(this);
151 } else {
152 removePreference(KEY_TAP_TO_WAKE);
153 }
154
Jason Monkd30e4262015-08-04 11:22:29 -0400155 if (isCameraGestureAvailable(getResources())) {
156 mCameraGesturePreference = (SwitchPreference) findPreference(KEY_CAMERA_GESTURE);
157 mCameraGesturePreference.setOnPreferenceChangeListener(this);
158 } else {
159 removePreference(KEY_CAMERA_GESTURE);
160 }
161
Jason Monkd3232da2014-08-20 13:29:20 -0400162 if (RotationPolicy.isRotationLockToggleVisible(activity)) {
163 DropDownPreference rotatePreference =
164 (DropDownPreference) findPreference(KEY_AUTO_ROTATE);
Jason Monkd3232da2014-08-20 13:29:20 -0400165 int rotateLockedResourceId;
166 // The following block sets the string used when rotation is locked.
167 // If the device locks specifically to portrait or landscape (rather than current
168 // rotation), then we use a different string to include this information.
169 if (allowAllRotations(activity)) {
170 rotateLockedResourceId = R.string.display_auto_rotate_stay_in_current;
171 } else {
172 if (RotationPolicy.getRotationLockOrientation(activity)
173 == Configuration.ORIENTATION_PORTRAIT) {
174 rotateLockedResourceId =
175 R.string.display_auto_rotate_stay_in_portrait;
176 } else {
177 rotateLockedResourceId =
178 R.string.display_auto_rotate_stay_in_landscape;
179 }
180 }
Jason Monk49b61032015-08-12 11:31:29 -0400181 rotatePreference.setEntries(new CharSequence[] {
182 activity.getString(R.string.display_auto_rotate_rotate),
183 activity.getString(rotateLockedResourceId),
184 });
185 rotatePreference.setEntryValues(new CharSequence[] { "0", "1" });
186 rotatePreference.setValueIndex(RotationPolicy.isRotationLocked(activity) ?
Jason Monkd3232da2014-08-20 13:29:20 -0400187 1 : 0);
Jason Monk49b61032015-08-12 11:31:29 -0400188 rotatePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
Jason Monkd3232da2014-08-20 13:29:20 -0400189 @Override
Jason Monk49b61032015-08-12 11:31:29 -0400190 public boolean onPreferenceChange(Preference preference, Object newValue) {
191 final boolean locked = Integer.parseInt((String) newValue) != 0;
Chris Wrenca689ae2015-06-09 14:24:03 -0400192 MetricsLogger.action(getActivity(), MetricsLogger.ACTION_ROTATION_LOCK,
Chris Wrenc7581e82015-06-09 12:07:14 -0400193 locked);
194 RotationPolicy.setRotationLock(activity, locked);
Jason Monkd3232da2014-08-20 13:29:20 -0400195 return true;
196 }
197 });
198 } else {
199 removePreference(KEY_AUTO_ROTATE);
200 }
Alan Viverette893b3002015-02-25 18:47:37 -0800201
202 mNightModePreference = (ListPreference) findPreference(KEY_NIGHT_MODE);
Jason Monkdf283602015-05-21 16:46:24 -0400203 if (mNightModePreference != null) {
204 final UiModeManager uiManager = (UiModeManager) getSystemService(
205 Context.UI_MODE_SERVICE);
206 final int currentNightMode = uiManager.getNightMode();
207 mNightModePreference.setValue(String.valueOf(currentNightMode));
208 mNightModePreference.setOnPreferenceChangeListener(this);
209 }
Jason Monkd3232da2014-08-20 13:29:20 -0400210 }
211
212 private static boolean allowAllRotations(Context context) {
213 return Resources.getSystem().getBoolean(
214 com.android.internal.R.bool.config_allowAllRotations);
John Spurlock333efe92014-07-23 13:01:21 -0400215 }
216
217 private static boolean isLiftToWakeAvailable(Context context) {
218 SensorManager sensors = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
219 return sensors != null && sensors.getDefaultSensor(Sensor.TYPE_WAKE_GESTURE) != null;
Jim Miller9391b952011-01-07 16:55:33 -0800220 }
221
John Spurlock911de7f2014-08-04 14:53:28 -0400222 private static boolean isDozeAvailable(Context context) {
223 String name = Build.IS_DEBUGGABLE ? SystemProperties.get("debug.doze.component") : null;
224 if (TextUtils.isEmpty(name)) {
225 name = context.getResources().getString(
226 com.android.internal.R.string.config_dozeComponent);
227 }
228 return !TextUtils.isEmpty(name);
229 }
230
Jason Monk86dd6ba2015-03-31 16:39:28 -0400231 private static boolean isTapToWakeAvailable(Resources res) {
232 return res.getBoolean(com.android.internal.R.bool.config_supportDoubleTapWake);
233 }
234
Adrian Roos8ab09542014-06-02 20:33:56 +0200235 private static boolean isAutomaticBrightnessAvailable(Resources res) {
236 return res.getBoolean(com.android.internal.R.bool.config_automatic_brightness_available);
237 }
238
Jason Monkd30e4262015-08-04 11:22:29 -0400239 private static boolean isCameraGestureAvailable(Resources res) {
240 boolean configSet = res.getInteger(
241 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
242 return configSet &&
243 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
244 }
245
Gilles Debunne698c24f2011-09-02 15:47:24 -0700246 private void updateTimeoutPreferenceDescription(long currentTimeout) {
247 ListPreference preference = mScreenTimeoutPreference;
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400248 String summary;
Gilles Debunne698c24f2011-09-02 15:47:24 -0700249 if (currentTimeout < 0) {
250 // Unsupported value
251 summary = "";
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400252 } else {
Gilles Debunne698c24f2011-09-02 15:47:24 -0700253 final CharSequence[] entries = preference.getEntries();
254 final CharSequence[] values = preference.getEntryValues();
Amith Yamasani64516a12012-10-22 16:38:02 -0700255 if (entries == null || entries.length == 0) {
256 summary = "";
257 } else {
258 int best = 0;
259 for (int i = 0; i < values.length; i++) {
260 long timeout = Long.parseLong(values[i].toString());
261 if (currentTimeout >= timeout) {
262 best = i;
263 }
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400264 }
Amith Yamasani64516a12012-10-22 16:38:02 -0700265 summary = preference.getContext().getString(R.string.screen_timeout_summary,
266 entries[best]);
Jim Miller9391b952011-01-07 16:55:33 -0800267 }
268 }
Gilles Debunne698c24f2011-09-02 15:47:24 -0700269 preference.setSummary(summary);
Jim Millerd90b5a82010-03-29 20:18:51 -0700270 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800271
Jim Millerd90b5a82010-03-29 20:18:51 -0700272 private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) {
Jim Millerfc5a0222010-04-13 15:31:41 -0700273 final DevicePolicyManager dpm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700274 (DevicePolicyManager) getActivity().getSystemService(
275 Context.DEVICE_POLICY_SERVICE);
Jim Millerfc5a0222010-04-13 15:31:41 -0700276 final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0;
Jim Millerd90b5a82010-03-29 20:18:51 -0700277 if (maxTimeout == 0) {
278 return; // policy not enforced
279 }
280 final CharSequence[] entries = screenTimeoutPreference.getEntries();
281 final CharSequence[] values = screenTimeoutPreference.getEntryValues();
282 ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
283 ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
284 for (int i = 0; i < values.length; i++) {
Gilles Debunne698c24f2011-09-02 15:47:24 -0700285 long timeout = Long.parseLong(values[i].toString());
Jim Millerd90b5a82010-03-29 20:18:51 -0700286 if (timeout <= maxTimeout) {
287 revisedEntries.add(entries[i]);
288 revisedValues.add(values[i]);
289 }
290 }
291 if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) {
Amith Yamasani78b82652013-08-16 16:35:54 -0700292 final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue());
Jim Millerd90b5a82010-03-29 20:18:51 -0700293 screenTimeoutPreference.setEntries(
294 revisedEntries.toArray(new CharSequence[revisedEntries.size()]));
295 screenTimeoutPreference.setEntryValues(
296 revisedValues.toArray(new CharSequence[revisedValues.size()]));
Jim Millerfc5a0222010-04-13 15:31:41 -0700297 if (userPreference <= maxTimeout) {
298 screenTimeoutPreference.setValue(String.valueOf(userPreference));
Amith Yamasani78b82652013-08-16 16:35:54 -0700299 } else if (revisedValues.size() > 0
300 && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString())
301 == maxTimeout) {
302 // If the last one happens to be the same as the max timeout, select that
303 screenTimeoutPreference.setValue(String.valueOf(maxTimeout));
Jim Millerfc5a0222010-04-13 15:31:41 -0700304 } else {
305 // There will be no highlighted selection since nothing in the list matches
306 // maxTimeout. The user can still select anything less than maxTimeout.
307 // TODO: maybe append maxTimeout to the list and mark selected.
308 }
Jim Millerd90b5a82010-03-29 20:18:51 -0700309 }
310 screenTimeoutPreference.setEnabled(revisedEntries.size() > 0);
Amith Yamasani992f1022010-01-25 09:17:53 -0800311 }
312
Christopher Tate8a510c52011-07-29 16:16:22 -0700313 int floatToIndex(float val) {
314 String[] indices = getResources().getStringArray(R.array.entryvalues_font_size);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800315 float lastVal = Float.parseFloat(indices[0]);
316 for (int i=1; i<indices.length; i++) {
317 float thisVal = Float.parseFloat(indices[i]);
318 if (val < (lastVal + (thisVal-lastVal)*.5f)) {
319 return i-1;
320 }
321 lastVal = thisVal;
322 }
323 return indices.length-1;
324 }
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900325
Dianne Hackborn6677e562011-03-04 17:30:54 -0800326 public void readFontSizePreference(ListPreference pref) {
327 try {
Gilles Debunne737c81c2011-07-08 09:40:00 -0700328 mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
Dianne Hackborn6677e562011-03-04 17:30:54 -0800329 } catch (RemoteException e) {
Gilles Debunne737c81c2011-07-08 09:40:00 -0700330 Log.w(TAG, "Unable to retrieve font size");
Dianne Hackborn6677e562011-03-04 17:30:54 -0800331 }
Christopher Tate8a510c52011-07-29 16:16:22 -0700332
333 // mark the appropriate item in the preferences list
334 int index = floatToIndex(mCurConfig.fontScale);
335 pref.setValueIndex(index);
336
337 // report the current size in the summary text
338 final Resources res = getResources();
339 String[] fontSizeNames = res.getStringArray(R.array.entries_font_size);
340 pref.setSummary(String.format(res.getString(R.string.summary_font_size),
341 fontSizeNames[index]));
Dianne Hackborn6677e562011-03-04 17:30:54 -0800342 }
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900343
Amith Yamasani992f1022010-01-25 09:17:53 -0800344 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700345 public void onResume() {
Amith Yamasani992f1022010-01-25 09:17:53 -0800346 super.onResume();
Jeff Brown9e143f52012-09-19 20:46:07 -0700347 updateState();
Amith Yamasani26a1f892011-02-28 17:24:31 -0800348 }
349
350 @Override
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700351 public Dialog onCreateDialog(int dialogId) {
352 if (dialogId == DLG_GLOBAL_CHANGE_WARNING) {
353 return Utils.buildGlobalChangeWarningDialog(getActivity(),
354 R.string.global_font_change_title,
355 new Runnable() {
356 public void run() {
357 mFontSizePref.click();
358 }
359 });
360 }
361 return null;
362 }
363
Gilles Debunne737c81c2011-07-08 09:40:00 -0700364 private void updateState() {
Dianne Hackborn6677e562011-03-04 17:30:54 -0800365 readFontSizePreference(mFontSizePref);
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800366 updateScreenSaverSummary();
Adrian Roos244e8ce2014-05-13 17:00:46 +0200367
368 // Update auto brightness if it is available.
369 if (mAutoBrightnessPreference != null) {
370 int brightnessMode = Settings.System.getInt(getContentResolver(),
371 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
372 mAutoBrightnessPreference.setChecked(brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL);
373 }
John Spurlock333efe92014-07-23 13:01:21 -0400374
375 // Update lift-to-wake if it is available.
376 if (mLiftToWakePreference != null) {
377 int value = Settings.Secure.getInt(getContentResolver(), WAKE_GESTURE_ENABLED, 0);
378 mLiftToWakePreference.setChecked(value != 0);
379 }
John Spurlock911de7f2014-08-04 14:53:28 -0400380
381 // Update doze if it is available.
382 if (mDozePreference != null) {
383 int value = Settings.Secure.getInt(getContentResolver(), DOZE_ENABLED, 1);
384 mDozePreference.setChecked(value != 0);
385 }
Jason Monk86dd6ba2015-03-31 16:39:28 -0400386
387 // Update tap to wake if it is available.
388 if (mTapToWakePreference != null) {
389 int value = Settings.Secure.getInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, 0);
390 mTapToWakePreference.setChecked(value != 0);
391 }
Jason Monkd30e4262015-08-04 11:22:29 -0400392
393 // Update camera gesture if it is available.
394 if (mCameraGesturePreference != null) {
395 int value = Settings.Secure.getInt(getContentResolver(), CAMERA_GESTURE_DISABLED, 0);
396 mCameraGesturePreference.setChecked(value == 0);
397 }
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800398 }
399
400 private void updateScreenSaverSummary() {
Daniel Sandlerc825bc82012-10-18 10:33:59 -0400401 if (mScreenSaverPreference != null) {
402 mScreenSaverPreference.setSummary(
403 DreamSettings.getSummaryTextWithDreamName(getActivity()));
404 }
Amith Yamasani26a1f892011-02-28 17:24:31 -0800405 }
406
Dianne Hackborn6677e562011-03-04 17:30:54 -0800407 public void writeFontSizePreference(Object objValue) {
408 try {
409 mCurConfig.fontScale = Float.parseFloat(objValue.toString());
Dianne Hackborn92c8eba2011-07-19 14:59:01 -0700410 ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800411 } catch (RemoteException e) {
Gilles Debunne737c81c2011-07-08 09:40:00 -0700412 Log.w(TAG, "Unable to save font size");
Dianne Hackborn6677e562011-03-04 17:30:54 -0800413 }
414 }
Amith Yamasani883d8502011-09-20 14:41:03 -0700415
Amith Yamasani992f1022010-01-25 09:17:53 -0800416 @Override
417 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400418 return super.onPreferenceTreeClick(preferenceScreen, preference);
Amith Yamasani992f1022010-01-25 09:17:53 -0800419 }
420
Jeff Brown4e54c2a2013-11-10 21:02:20 -0800421 @Override
Amith Yamasani992f1022010-01-25 09:17:53 -0800422 public boolean onPreferenceChange(Preference preference, Object objValue) {
423 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800424 if (KEY_SCREEN_TIMEOUT.equals(key)) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800425 try {
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900426 int value = Integer.parseInt((String) objValue);
Gilles Debunne698c24f2011-09-02 15:47:24 -0700427 Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
428 updateTimeoutPreferenceDescription(value);
Amith Yamasani992f1022010-01-25 09:17:53 -0800429 } catch (NumberFormatException e) {
430 Log.e(TAG, "could not persist screen timeout setting", e);
431 }
432 }
Dianne Hackborn6677e562011-03-04 17:30:54 -0800433 if (KEY_FONT_SIZE.equals(key)) {
434 writeFontSizePreference(objValue);
435 }
Adrian Roos244e8ce2014-05-13 17:00:46 +0200436 if (preference == mAutoBrightnessPreference) {
437 boolean auto = (Boolean) objValue;
438 Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE,
439 auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
440 }
John Spurlock333efe92014-07-23 13:01:21 -0400441 if (preference == mLiftToWakePreference) {
442 boolean value = (Boolean) objValue;
443 Settings.Secure.putInt(getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0);
444 }
John Spurlock911de7f2014-08-04 14:53:28 -0400445 if (preference == mDozePreference) {
446 boolean value = (Boolean) objValue;
447 Settings.Secure.putInt(getContentResolver(), DOZE_ENABLED, value ? 1 : 0);
448 }
Jason Monk86dd6ba2015-03-31 16:39:28 -0400449 if (preference == mTapToWakePreference) {
450 boolean value = (Boolean) objValue;
451 Settings.Secure.putInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, value ? 1 : 0);
452 }
Jason Monkd30e4262015-08-04 11:22:29 -0400453 if (preference == mCameraGesturePreference) {
454 boolean value = (Boolean) objValue;
455 Settings.Secure.putInt(getContentResolver(), CAMERA_GESTURE_DISABLED,
456 value ? 0 : 1 /* Backwards because setting is for disabling */);
457 }
Alan Viverette893b3002015-02-25 18:47:37 -0800458 if (preference == mNightModePreference) {
459 try {
460 final int value = Integer.parseInt((String) objValue);
461 final UiModeManager uiManager = (UiModeManager) getSystemService(
462 Context.UI_MODE_SERVICE);
463 uiManager.setNightMode(value);
464 } catch (NumberFormatException e) {
465 Log.e(TAG, "could not persist night mode setting", e);
466 }
467 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800468 return true;
469 }
Jeff Brown9e143f52012-09-19 20:46:07 -0700470
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700471 @Override
472 public boolean onPreferenceClick(Preference preference) {
473 if (preference == mFontSizePref) {
474 if (Utils.hasMultipleUsers(getActivity())) {
475 showDialog(DLG_GLOBAL_CHANGE_WARNING);
476 return true;
477 } else {
478 mFontSizePref.click();
479 }
480 }
481 return false;
482 }
Adrian Roos8ab09542014-06-02 20:33:56 +0200483
Jason Monk23acc2b2015-04-14 15:06:39 -0400484 @Override
485 protected int getHelpResource() {
486 return R.string.help_uri_display;
487 }
488
Adrian Roos8ab09542014-06-02 20:33:56 +0200489 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
490 new BaseSearchIndexProvider() {
491 @Override
492 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
493 boolean enabled) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700494 ArrayList<SearchIndexableResource> result =
495 new ArrayList<SearchIndexableResource>();
Adrian Roos8ab09542014-06-02 20:33:56 +0200496
497 SearchIndexableResource sir = new SearchIndexableResource(context);
498 sir.xmlResId = R.xml.display_settings;
499 result.add(sir);
500
501 return result;
502 }
503
504 @Override
505 public List<String> getNonIndexableKeys(Context context) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700506 ArrayList<String> result = new ArrayList<String>();
507 if (!context.getResources().getBoolean(
508 com.android.internal.R.bool.config_dreamsSupported)) {
509 result.add(KEY_SCREEN_SAVER);
Adrian Roos8ab09542014-06-02 20:33:56 +0200510 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700511 if (!isAutomaticBrightnessAvailable(context.getResources())) {
512 result.add(KEY_AUTO_BRIGHTNESS);
513 }
John Spurlock333efe92014-07-23 13:01:21 -0400514 if (!isLiftToWakeAvailable(context)) {
515 result.add(KEY_LIFT_TO_WAKE);
516 }
John Spurlock911de7f2014-08-04 14:53:28 -0400517 if (!isDozeAvailable(context)) {
518 result.add(KEY_DOZE);
519 }
Jason Monkd3232da2014-08-20 13:29:20 -0400520 if (!RotationPolicy.isRotationLockToggleVisible(context)) {
521 result.add(KEY_AUTO_ROTATE);
522 }
Jason Monk181e0e92015-06-08 09:11:48 -0400523 if (!isTapToWakeAvailable(context.getResources())) {
524 result.add(KEY_TAP_TO_WAKE);
525 }
Jason Monkd30e4262015-08-04 11:22:29 -0400526 if (!isCameraGestureAvailable(context.getResources())) {
527 result.add(KEY_CAMERA_GESTURE);
528 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700529 return result;
Adrian Roos8ab09542014-06-02 20:33:56 +0200530 }
531 };
Amith Yamasani992f1022010-01-25 09:17:53 -0800532}