blob: 6d99a35ce561c50a70dc87d2a48fdb5b2ce83b44 [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 Monkd3232da2014-08-20 13:29:20 -040019import android.app.Activity;
Dianne Hackborn6677e562011-03-04 17:30:54 -080020import android.app.ActivityManagerNative;
Alan Viverette893b3002015-02-25 18:47:37 -080021import android.app.UiModeManager;
Jim Millerd90b5a82010-03-29 20:18:51 -070022import android.app.admin.DevicePolicyManager;
Amith Yamasani992f1022010-01-25 09:17:53 -080023import android.content.ContentResolver;
24import android.content.Context;
Dianne Hackborn6677e562011-03-04 17:30:54 -080025import android.content.res.Configuration;
Christopher Tate8a510c52011-07-29 16:16:22 -070026import android.content.res.Resources;
John Spurlock333efe92014-07-23 13:01:21 -040027import android.hardware.Sensor;
28import android.hardware.SensorManager;
John Spurlock911de7f2014-08-04 14:53:28 -040029import android.os.Build;
Amith Yamasani992f1022010-01-25 09:17:53 -080030import android.os.Bundle;
Amith Yamasani992f1022010-01-25 09:17:53 -080031import android.os.RemoteException;
John Spurlock911de7f2014-08-04 14:53:28 -040032import android.os.SystemProperties;
Adrian Roos8ab09542014-06-02 20:33:56 +020033import android.provider.SearchIndexableResource;
Amith Yamasani992f1022010-01-25 09:17:53 -080034import android.provider.Settings;
Jason Monk39b46742015-09-10 15:52:51 -040035import android.support.v14.preference.SwitchPreference;
Jason Monk1230ac82015-12-11 20:14:43 -050036import android.support.v7.preference.DropDownPreference;
Jason Monk39b46742015-09-10 15:52:51 -040037import android.support.v7.preference.ListPreference;
38import android.support.v7.preference.Preference;
39import android.support.v7.preference.Preference.OnPreferenceChangeListener;
John Spurlock911de7f2014-08-04 14:53:28 -040040import android.text.TextUtils;
Amith Yamasani992f1022010-01-25 09:17:53 -080041import android.util.Log;
Jason Monk1230ac82015-12-11 20:14:43 -050042
Jason Monk49b61032015-08-12 11:31:29 -040043import com.android.internal.logging.MetricsLogger;
44import com.android.internal.view.RotationPolicy;
Jason Monk30695812015-11-17 09:01:08 -050045import com.android.settings.dashboard.SummaryLoader;
Jason Monk49b61032015-08-12 11:31:29 -040046import com.android.settings.search.BaseSearchIndexProvider;
47import com.android.settings.search.Indexable;
48
Amith Yamasanid7993472010-08-18 13:59:28 -070049import java.util.ArrayList;
Adrian Roos8ab09542014-06-02 20:33:56 +020050import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070051
Jason Monk39b46742015-09-10 15:52:51 -040052import static android.provider.Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED;
53import static android.provider.Settings.Secure.CAMERA_GESTURE_DISABLED;
54import static android.provider.Settings.Secure.DOUBLE_TAP_TO_WAKE;
55import static android.provider.Settings.Secure.DOZE_ENABLED;
56import static android.provider.Settings.Secure.WAKE_GESTURE_ENABLED;
57import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
58import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
59import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
60import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
61
Amith Yamasanid7993472010-08-18 13:59:28 -070062public class DisplaySettings extends SettingsPreferenceFragment implements
Julia Reynoldsa02bc992015-08-12 14:51:34 -040063 Preference.OnPreferenceChangeListener, 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";
Jorim Jaggicc1e9f72015-09-08 14:56:40 -070079 private static final String KEY_CAMERA_DOUBLE_TAP_POWER_GESTURE
80 = "camera_double_tap_power_gesture";
Jeff Brown9e143f52012-09-19 20:46:07 -070081
Julia Reynoldsa02bc992015-08-12 14:51:34 -040082 private DropDownPreference 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;
Jorim Jaggicc1e9f72015-09-08 14:56:40 -070094 private SwitchPreference mCameraDoubleTapPowerGesturePreference;
Jim Miller9391b952011-01-07 16:55:33 -080095
Amith Yamasani992f1022010-01-25 09:17:53 -080096 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -040097 protected int getMetricsCategory() {
98 return MetricsLogger.DISPLAY;
99 }
100
101 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700102 public void onCreate(Bundle savedInstanceState) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800103 super.onCreate(savedInstanceState);
Jason Monkd3232da2014-08-20 13:29:20 -0400104 final Activity activity = getActivity();
105 final ContentResolver resolver = activity.getContentResolver();
Amith Yamasani992f1022010-01-25 09:17:53 -0800106
107 addPreferencesFromResource(R.xml.display_settings);
108
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800109 mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
Daniel Sandler47c991f2012-02-01 14:14:18 -0500110 if (mScreenSaverPreference != null
111 && getResources().getBoolean(
John Spurlockd983e1d2012-10-18 16:41:09 -0400112 com.android.internal.R.bool.config_dreamsSupported) == false) {
Daniel Sandler47c991f2012-02-01 14:14:18 -0500113 getPreferenceScreen().removePreference(mScreenSaverPreference);
114 }
Amith Yamasani78b82652013-08-16 16:35:54 -0700115
Jim Miller9391b952011-01-07 16:55:33 -0800116 mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
117 final long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT,
118 FALLBACK_SCREEN_TIMEOUT_VALUE);
119 mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
120 mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
121 disableUnusableTimeouts(mScreenTimeoutPreference);
Gilles Debunne698c24f2011-09-02 15:47:24 -0700122 updateTimeoutPreferenceDescription(currentTimeout);
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400123
Julia Reynoldsa02bc992015-08-12 14:51:34 -0400124 mFontSizePref = (DropDownPreference) findPreference(KEY_FONT_SIZE);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800125 mFontSizePref.setOnPreferenceChangeListener(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
Jorim Jaggicc1e9f72015-09-08 14:56:40 -0700162 if (isCameraDoubleTapPowerGestureAvailable(getResources())) {
163 mCameraDoubleTapPowerGesturePreference
164 = (SwitchPreference) findPreference(KEY_CAMERA_DOUBLE_TAP_POWER_GESTURE);
165 mCameraDoubleTapPowerGesturePreference.setOnPreferenceChangeListener(this);
166 } else {
167 removePreference(KEY_CAMERA_DOUBLE_TAP_POWER_GESTURE);
168 }
169
Jason Monkd3232da2014-08-20 13:29:20 -0400170 if (RotationPolicy.isRotationLockToggleVisible(activity)) {
171 DropDownPreference rotatePreference =
172 (DropDownPreference) findPreference(KEY_AUTO_ROTATE);
Jason Monkd3232da2014-08-20 13:29:20 -0400173 int rotateLockedResourceId;
174 // The following block sets the string used when rotation is locked.
175 // If the device locks specifically to portrait or landscape (rather than current
176 // rotation), then we use a different string to include this information.
177 if (allowAllRotations(activity)) {
178 rotateLockedResourceId = R.string.display_auto_rotate_stay_in_current;
179 } else {
180 if (RotationPolicy.getRotationLockOrientation(activity)
181 == Configuration.ORIENTATION_PORTRAIT) {
182 rotateLockedResourceId =
183 R.string.display_auto_rotate_stay_in_portrait;
184 } else {
185 rotateLockedResourceId =
186 R.string.display_auto_rotate_stay_in_landscape;
187 }
188 }
Jason Monk49b61032015-08-12 11:31:29 -0400189 rotatePreference.setEntries(new CharSequence[] {
190 activity.getString(R.string.display_auto_rotate_rotate),
191 activity.getString(rotateLockedResourceId),
192 });
193 rotatePreference.setEntryValues(new CharSequence[] { "0", "1" });
194 rotatePreference.setValueIndex(RotationPolicy.isRotationLocked(activity) ?
Jason Monkd3232da2014-08-20 13:29:20 -0400195 1 : 0);
Jason Monk49b61032015-08-12 11:31:29 -0400196 rotatePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
Jason Monkd3232da2014-08-20 13:29:20 -0400197 @Override
Jason Monk49b61032015-08-12 11:31:29 -0400198 public boolean onPreferenceChange(Preference preference, Object newValue) {
199 final boolean locked = Integer.parseInt((String) newValue) != 0;
Chris Wrenca689ae2015-06-09 14:24:03 -0400200 MetricsLogger.action(getActivity(), MetricsLogger.ACTION_ROTATION_LOCK,
Chris Wrenc7581e82015-06-09 12:07:14 -0400201 locked);
202 RotationPolicy.setRotationLock(activity, locked);
Jason Monkd3232da2014-08-20 13:29:20 -0400203 return true;
204 }
205 });
206 } else {
207 removePreference(KEY_AUTO_ROTATE);
208 }
Alan Viverette893b3002015-02-25 18:47:37 -0800209
210 mNightModePreference = (ListPreference) findPreference(KEY_NIGHT_MODE);
Jason Monkdf283602015-05-21 16:46:24 -0400211 if (mNightModePreference != null) {
212 final UiModeManager uiManager = (UiModeManager) getSystemService(
213 Context.UI_MODE_SERVICE);
214 final int currentNightMode = uiManager.getNightMode();
215 mNightModePreference.setValue(String.valueOf(currentNightMode));
216 mNightModePreference.setOnPreferenceChangeListener(this);
217 }
Jason Monkd3232da2014-08-20 13:29:20 -0400218 }
219
220 private static boolean allowAllRotations(Context context) {
221 return Resources.getSystem().getBoolean(
222 com.android.internal.R.bool.config_allowAllRotations);
John Spurlock333efe92014-07-23 13:01:21 -0400223 }
224
225 private static boolean isLiftToWakeAvailable(Context context) {
226 SensorManager sensors = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
227 return sensors != null && sensors.getDefaultSensor(Sensor.TYPE_WAKE_GESTURE) != null;
Jim Miller9391b952011-01-07 16:55:33 -0800228 }
229
John Spurlock911de7f2014-08-04 14:53:28 -0400230 private static boolean isDozeAvailable(Context context) {
231 String name = Build.IS_DEBUGGABLE ? SystemProperties.get("debug.doze.component") : null;
232 if (TextUtils.isEmpty(name)) {
233 name = context.getResources().getString(
234 com.android.internal.R.string.config_dozeComponent);
235 }
236 return !TextUtils.isEmpty(name);
237 }
238
Jason Monk86dd6ba2015-03-31 16:39:28 -0400239 private static boolean isTapToWakeAvailable(Resources res) {
240 return res.getBoolean(com.android.internal.R.bool.config_supportDoubleTapWake);
241 }
242
Adrian Roos8ab09542014-06-02 20:33:56 +0200243 private static boolean isAutomaticBrightnessAvailable(Resources res) {
244 return res.getBoolean(com.android.internal.R.bool.config_automatic_brightness_available);
245 }
246
Jason Monkd30e4262015-08-04 11:22:29 -0400247 private static boolean isCameraGestureAvailable(Resources res) {
248 boolean configSet = res.getInteger(
249 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
250 return configSet &&
251 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
252 }
253
Jorim Jaggicc1e9f72015-09-08 14:56:40 -0700254 private static boolean isCameraDoubleTapPowerGestureAvailable(Resources res) {
255 return res.getBoolean(
256 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
257 }
258
Gilles Debunne698c24f2011-09-02 15:47:24 -0700259 private void updateTimeoutPreferenceDescription(long currentTimeout) {
260 ListPreference preference = mScreenTimeoutPreference;
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400261 String summary;
Gilles Debunne698c24f2011-09-02 15:47:24 -0700262 if (currentTimeout < 0) {
263 // Unsupported value
264 summary = "";
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400265 } else {
Gilles Debunne698c24f2011-09-02 15:47:24 -0700266 final CharSequence[] entries = preference.getEntries();
267 final CharSequence[] values = preference.getEntryValues();
Amith Yamasani64516a12012-10-22 16:38:02 -0700268 if (entries == null || entries.length == 0) {
269 summary = "";
270 } else {
271 int best = 0;
272 for (int i = 0; i < values.length; i++) {
273 long timeout = Long.parseLong(values[i].toString());
274 if (currentTimeout >= timeout) {
275 best = i;
276 }
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400277 }
Amith Yamasani64516a12012-10-22 16:38:02 -0700278 summary = preference.getContext().getString(R.string.screen_timeout_summary,
279 entries[best]);
Jim Miller9391b952011-01-07 16:55:33 -0800280 }
281 }
Gilles Debunne698c24f2011-09-02 15:47:24 -0700282 preference.setSummary(summary);
Jim Millerd90b5a82010-03-29 20:18:51 -0700283 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800284
Jim Millerd90b5a82010-03-29 20:18:51 -0700285 private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) {
Jim Millerfc5a0222010-04-13 15:31:41 -0700286 final DevicePolicyManager dpm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700287 (DevicePolicyManager) getActivity().getSystemService(
288 Context.DEVICE_POLICY_SERVICE);
Jim Millerfc5a0222010-04-13 15:31:41 -0700289 final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0;
Jim Millerd90b5a82010-03-29 20:18:51 -0700290 if (maxTimeout == 0) {
291 return; // policy not enforced
292 }
293 final CharSequence[] entries = screenTimeoutPreference.getEntries();
294 final CharSequence[] values = screenTimeoutPreference.getEntryValues();
295 ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
296 ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
297 for (int i = 0; i < values.length; i++) {
Gilles Debunne698c24f2011-09-02 15:47:24 -0700298 long timeout = Long.parseLong(values[i].toString());
Jim Millerd90b5a82010-03-29 20:18:51 -0700299 if (timeout <= maxTimeout) {
300 revisedEntries.add(entries[i]);
301 revisedValues.add(values[i]);
302 }
303 }
304 if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) {
Amith Yamasani78b82652013-08-16 16:35:54 -0700305 final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue());
Jim Millerd90b5a82010-03-29 20:18:51 -0700306 screenTimeoutPreference.setEntries(
307 revisedEntries.toArray(new CharSequence[revisedEntries.size()]));
308 screenTimeoutPreference.setEntryValues(
309 revisedValues.toArray(new CharSequence[revisedValues.size()]));
Jim Millerfc5a0222010-04-13 15:31:41 -0700310 if (userPreference <= maxTimeout) {
311 screenTimeoutPreference.setValue(String.valueOf(userPreference));
Amith Yamasani78b82652013-08-16 16:35:54 -0700312 } else if (revisedValues.size() > 0
313 && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString())
314 == maxTimeout) {
315 // If the last one happens to be the same as the max timeout, select that
316 screenTimeoutPreference.setValue(String.valueOf(maxTimeout));
Jim Millerfc5a0222010-04-13 15:31:41 -0700317 } else {
318 // There will be no highlighted selection since nothing in the list matches
319 // maxTimeout. The user can still select anything less than maxTimeout.
320 // TODO: maybe append maxTimeout to the list and mark selected.
321 }
Jim Millerd90b5a82010-03-29 20:18:51 -0700322 }
323 screenTimeoutPreference.setEnabled(revisedEntries.size() > 0);
Amith Yamasani992f1022010-01-25 09:17:53 -0800324 }
325
Christopher Tate8a510c52011-07-29 16:16:22 -0700326 int floatToIndex(float val) {
327 String[] indices = getResources().getStringArray(R.array.entryvalues_font_size);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800328 float lastVal = Float.parseFloat(indices[0]);
329 for (int i=1; i<indices.length; i++) {
330 float thisVal = Float.parseFloat(indices[i]);
331 if (val < (lastVal + (thisVal-lastVal)*.5f)) {
332 return i-1;
333 }
334 lastVal = thisVal;
335 }
336 return indices.length-1;
337 }
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900338
Dianne Hackborn6677e562011-03-04 17:30:54 -0800339 public void readFontSizePreference(ListPreference pref) {
340 try {
Gilles Debunne737c81c2011-07-08 09:40:00 -0700341 mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
Dianne Hackborn6677e562011-03-04 17:30:54 -0800342 } catch (RemoteException e) {
Gilles Debunne737c81c2011-07-08 09:40:00 -0700343 Log.w(TAG, "Unable to retrieve font size");
Dianne Hackborn6677e562011-03-04 17:30:54 -0800344 }
Christopher Tate8a510c52011-07-29 16:16:22 -0700345
346 // mark the appropriate item in the preferences list
347 int index = floatToIndex(mCurConfig.fontScale);
348 pref.setValueIndex(index);
349
350 // report the current size in the summary text
351 final Resources res = getResources();
352 String[] fontSizeNames = res.getStringArray(R.array.entries_font_size);
353 pref.setSummary(String.format(res.getString(R.string.summary_font_size),
354 fontSizeNames[index]));
Dianne Hackborn6677e562011-03-04 17:30:54 -0800355 }
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900356
Amith Yamasani992f1022010-01-25 09:17:53 -0800357 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700358 public void onResume() {
Amith Yamasani992f1022010-01-25 09:17:53 -0800359 super.onResume();
Jeff Brown9e143f52012-09-19 20:46:07 -0700360 updateState();
Amith Yamasani26a1f892011-02-28 17:24:31 -0800361 }
362
Gilles Debunne737c81c2011-07-08 09:40:00 -0700363 private void updateState() {
Dianne Hackborn6677e562011-03-04 17:30:54 -0800364 readFontSizePreference(mFontSizePref);
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800365 updateScreenSaverSummary();
Adrian Roos244e8ce2014-05-13 17:00:46 +0200366
367 // Update auto brightness if it is available.
368 if (mAutoBrightnessPreference != null) {
369 int brightnessMode = Settings.System.getInt(getContentResolver(),
370 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
371 mAutoBrightnessPreference.setChecked(brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL);
372 }
John Spurlock333efe92014-07-23 13:01:21 -0400373
374 // Update lift-to-wake if it is available.
375 if (mLiftToWakePreference != null) {
376 int value = Settings.Secure.getInt(getContentResolver(), WAKE_GESTURE_ENABLED, 0);
377 mLiftToWakePreference.setChecked(value != 0);
378 }
John Spurlock911de7f2014-08-04 14:53:28 -0400379
380 // Update doze if it is available.
381 if (mDozePreference != null) {
382 int value = Settings.Secure.getInt(getContentResolver(), DOZE_ENABLED, 1);
383 mDozePreference.setChecked(value != 0);
384 }
Jason Monk86dd6ba2015-03-31 16:39:28 -0400385
386 // Update tap to wake if it is available.
387 if (mTapToWakePreference != null) {
388 int value = Settings.Secure.getInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, 0);
389 mTapToWakePreference.setChecked(value != 0);
390 }
Jason Monkd30e4262015-08-04 11:22:29 -0400391
Jorim Jaggicc1e9f72015-09-08 14:56:40 -0700392 // Update camera gesture #1 if it is available.
Jason Monkd30e4262015-08-04 11:22:29 -0400393 if (mCameraGesturePreference != null) {
394 int value = Settings.Secure.getInt(getContentResolver(), CAMERA_GESTURE_DISABLED, 0);
395 mCameraGesturePreference.setChecked(value == 0);
396 }
Jorim Jaggicc1e9f72015-09-08 14:56:40 -0700397
398 // Update camera gesture #2 if it is available.
399 if (mCameraDoubleTapPowerGesturePreference != null) {
400 int value = Settings.Secure.getInt(
401 getContentResolver(), CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0);
402 mCameraDoubleTapPowerGesturePreference.setChecked(value == 0);
403 }
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800404 }
405
406 private void updateScreenSaverSummary() {
Daniel Sandlerc825bc82012-10-18 10:33:59 -0400407 if (mScreenSaverPreference != null) {
408 mScreenSaverPreference.setSummary(
409 DreamSettings.getSummaryTextWithDreamName(getActivity()));
410 }
Amith Yamasani26a1f892011-02-28 17:24:31 -0800411 }
412
Dianne Hackborn6677e562011-03-04 17:30:54 -0800413 public void writeFontSizePreference(Object objValue) {
414 try {
415 mCurConfig.fontScale = Float.parseFloat(objValue.toString());
Dianne Hackborn92c8eba2011-07-19 14:59:01 -0700416 ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800417 } catch (RemoteException e) {
Gilles Debunne737c81c2011-07-08 09:40:00 -0700418 Log.w(TAG, "Unable to save font size");
Dianne Hackborn6677e562011-03-04 17:30:54 -0800419 }
420 }
Amith Yamasani883d8502011-09-20 14:41:03 -0700421
Amith Yamasani992f1022010-01-25 09:17:53 -0800422 @Override
Amith Yamasani992f1022010-01-25 09:17:53 -0800423 public boolean onPreferenceChange(Preference preference, Object objValue) {
424 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800425 if (KEY_SCREEN_TIMEOUT.equals(key)) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800426 try {
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900427 int value = Integer.parseInt((String) objValue);
Gilles Debunne698c24f2011-09-02 15:47:24 -0700428 Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
429 updateTimeoutPreferenceDescription(value);
Amith Yamasani992f1022010-01-25 09:17:53 -0800430 } catch (NumberFormatException e) {
431 Log.e(TAG, "could not persist screen timeout setting", e);
432 }
433 }
Dianne Hackborn6677e562011-03-04 17:30:54 -0800434 if (KEY_FONT_SIZE.equals(key)) {
435 writeFontSizePreference(objValue);
436 }
Adrian Roos244e8ce2014-05-13 17:00:46 +0200437 if (preference == mAutoBrightnessPreference) {
438 boolean auto = (Boolean) objValue;
439 Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE,
440 auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
441 }
John Spurlock333efe92014-07-23 13:01:21 -0400442 if (preference == mLiftToWakePreference) {
443 boolean value = (Boolean) objValue;
444 Settings.Secure.putInt(getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0);
445 }
John Spurlock911de7f2014-08-04 14:53:28 -0400446 if (preference == mDozePreference) {
447 boolean value = (Boolean) objValue;
448 Settings.Secure.putInt(getContentResolver(), DOZE_ENABLED, value ? 1 : 0);
449 }
Jason Monk86dd6ba2015-03-31 16:39:28 -0400450 if (preference == mTapToWakePreference) {
451 boolean value = (Boolean) objValue;
452 Settings.Secure.putInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, value ? 1 : 0);
453 }
Jason Monkd30e4262015-08-04 11:22:29 -0400454 if (preference == mCameraGesturePreference) {
455 boolean value = (Boolean) objValue;
456 Settings.Secure.putInt(getContentResolver(), CAMERA_GESTURE_DISABLED,
457 value ? 0 : 1 /* Backwards because setting is for disabling */);
458 }
Jorim Jaggicc1e9f72015-09-08 14:56:40 -0700459 if (preference == mCameraDoubleTapPowerGesturePreference) {
460 boolean value = (Boolean) objValue;
461 Settings.Secure.putInt(getContentResolver(), CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED,
462 value ? 0 : 1 /* Backwards because setting is for disabling */);
463 }
Alan Viverette893b3002015-02-25 18:47:37 -0800464 if (preference == mNightModePreference) {
465 try {
466 final int value = Integer.parseInt((String) objValue);
467 final UiModeManager uiManager = (UiModeManager) getSystemService(
468 Context.UI_MODE_SERVICE);
469 uiManager.setNightMode(value);
470 } catch (NumberFormatException e) {
471 Log.e(TAG, "could not persist night mode setting", e);
472 }
473 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800474 return true;
475 }
Jeff Brown9e143f52012-09-19 20:46:07 -0700476
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700477 @Override
Jason Monk23acc2b2015-04-14 15:06:39 -0400478 protected int getHelpResource() {
479 return R.string.help_uri_display;
480 }
481
Jason Monk30695812015-11-17 09:01:08 -0500482 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
483 private final Context mContext;
484 private final SummaryLoader mLoader;
485
486 private SummaryProvider(Context context, SummaryLoader loader) {
487 mContext = context;
488 mLoader = loader;
489 }
490
491 @Override
492 public void setListening(boolean listening) {
493 if (listening) {
494 updateSummary();
495 }
496 }
497
498 private void updateSummary() {
499 boolean auto = Settings.System.getInt(mContext.getContentResolver(),
500 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
501 == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
502 mLoader.setSummary(this, mContext.getString(auto ? R.string.display_summary_on
503 : R.string.display_summary_off));
504 }
505 }
506
507 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
508 = new SummaryLoader.SummaryProviderFactory() {
509 @Override
510 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
511 SummaryLoader summaryLoader) {
512 return new SummaryProvider(activity, summaryLoader);
513 }
514 };
515
Adrian Roos8ab09542014-06-02 20:33:56 +0200516 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
517 new BaseSearchIndexProvider() {
518 @Override
519 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
520 boolean enabled) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700521 ArrayList<SearchIndexableResource> result =
522 new ArrayList<SearchIndexableResource>();
Adrian Roos8ab09542014-06-02 20:33:56 +0200523
524 SearchIndexableResource sir = new SearchIndexableResource(context);
525 sir.xmlResId = R.xml.display_settings;
526 result.add(sir);
527
528 return result;
529 }
530
531 @Override
532 public List<String> getNonIndexableKeys(Context context) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700533 ArrayList<String> result = new ArrayList<String>();
534 if (!context.getResources().getBoolean(
535 com.android.internal.R.bool.config_dreamsSupported)) {
536 result.add(KEY_SCREEN_SAVER);
Adrian Roos8ab09542014-06-02 20:33:56 +0200537 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700538 if (!isAutomaticBrightnessAvailable(context.getResources())) {
539 result.add(KEY_AUTO_BRIGHTNESS);
540 }
John Spurlock333efe92014-07-23 13:01:21 -0400541 if (!isLiftToWakeAvailable(context)) {
542 result.add(KEY_LIFT_TO_WAKE);
543 }
John Spurlock911de7f2014-08-04 14:53:28 -0400544 if (!isDozeAvailable(context)) {
545 result.add(KEY_DOZE);
546 }
Jason Monkd3232da2014-08-20 13:29:20 -0400547 if (!RotationPolicy.isRotationLockToggleVisible(context)) {
548 result.add(KEY_AUTO_ROTATE);
549 }
Jason Monk181e0e92015-06-08 09:11:48 -0400550 if (!isTapToWakeAvailable(context.getResources())) {
551 result.add(KEY_TAP_TO_WAKE);
552 }
Jason Monkd30e4262015-08-04 11:22:29 -0400553 if (!isCameraGestureAvailable(context.getResources())) {
554 result.add(KEY_CAMERA_GESTURE);
555 }
Jorim Jaggicc1e9f72015-09-08 14:56:40 -0700556 if (!isCameraDoubleTapPowerGestureAvailable(context.getResources())) {
557 result.add(KEY_CAMERA_DOUBLE_TAP_POWER_GESTURE);
558 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700559 return result;
Adrian Roos8ab09542014-06-02 20:33:56 +0200560 }
561 };
Amith Yamasani992f1022010-01-25 09:17:53 -0800562}