blob: 33ce5ad49c4e4d946708725a9f6d129223a243f7 [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;
Ruben Brunke4d95c02016-04-18 18:08:34 -070020import android.app.ActivityManager;
Alan Viverette893b3002015-02-25 18:47:37 -080021import android.app.UiModeManager;
Oleksandr Peletskyi8b53b1e2016-01-27 18:07:18 +010022import android.app.WallpaperManager;
Jim Millerd90b5a82010-03-29 20:18:51 -070023import android.app.admin.DevicePolicyManager;
Amith Yamasani992f1022010-01-25 09:17:53 -080024import android.content.ContentResolver;
25import android.content.Context;
Sudheer Shanka550d0682016-01-13 15:16:55 +000026import android.content.ComponentName;
Ruben Brunke4d95c02016-04-18 18:08:34 -070027import android.content.pm.PackageManager;
Dianne Hackborn6677e562011-03-04 17:30:54 -080028import android.content.res.Configuration;
Christopher Tate8a510c52011-07-29 16:16:22 -070029import android.content.res.Resources;
John Spurlock333efe92014-07-23 13:01:21 -040030import android.hardware.Sensor;
31import android.hardware.SensorManager;
John Spurlock911de7f2014-08-04 14:53:28 -040032import android.os.Build;
Amith Yamasani992f1022010-01-25 09:17:53 -080033import android.os.Bundle;
John Spurlock911de7f2014-08-04 14:53:28 -040034import android.os.SystemProperties;
Ricky Wai36cce832016-03-18 16:26:35 +000035import android.os.UserHandle;
Oleksandr Peletskyic4854872016-04-05 15:37:07 +020036import android.os.UserManager;
Adrian Roos8ab09542014-06-02 20:33:56 +020037import android.provider.SearchIndexableResource;
Amith Yamasani992f1022010-01-25 09:17:53 -080038import android.provider.Settings;
Jason Monk39b46742015-09-10 15:52:51 -040039import android.support.v14.preference.SwitchPreference;
Jason Monk1230ac82015-12-11 20:14:43 -050040import android.support.v7.preference.DropDownPreference;
Jason Monk39b46742015-09-10 15:52:51 -040041import android.support.v7.preference.ListPreference;
42import android.support.v7.preference.Preference;
43import android.support.v7.preference.Preference.OnPreferenceChangeListener;
Doris Lingfec32032016-08-26 14:29:52 -070044import android.support.v7.preference.PreferenceScreen;
John Spurlock911de7f2014-08-04 14:53:28 -040045import android.text.TextUtils;
Amith Yamasani992f1022010-01-25 09:17:53 -080046import android.util.Log;
Jason Monk1230ac82015-12-11 20:14:43 -050047
Justin Klaassenc7cd2982016-06-21 18:32:04 -070048import com.android.internal.app.NightDisplayController;
Jason Monk49b61032015-08-12 11:31:29 -040049import com.android.internal.logging.MetricsLogger;
Chris Wren9d1bfd12016-01-26 18:04:01 -050050import com.android.internal.logging.MetricsProto.MetricsEvent;
Jason Monk49b61032015-08-12 11:31:29 -040051import com.android.internal.view.RotationPolicy;
Noah Wanga6bb3d22016-01-25 21:30:08 -080052import com.android.settings.accessibility.ToggleFontSizePreferenceFragment;
Jason Monk30695812015-11-17 09:01:08 -050053import com.android.settings.dashboard.SummaryLoader;
Jason Monk49b61032015-08-12 11:31:29 -040054import com.android.settings.search.BaseSearchIndexProvider;
55import com.android.settings.search.Indexable;
Sudheer Shanka550d0682016-01-13 15:16:55 +000056import com.android.settingslib.RestrictedLockUtils;
Oleksandr Peletskyic4854872016-04-05 15:37:07 +020057import com.android.settingslib.RestrictedPreference;
Jason Monk49b61032015-08-12 11:31:29 -040058
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
Jason Monk39b46742015-09-10 15:52:51 -040062import static android.provider.Settings.Secure.CAMERA_GESTURE_DISABLED;
63import static android.provider.Settings.Secure.DOUBLE_TAP_TO_WAKE;
64import static android.provider.Settings.Secure.DOZE_ENABLED;
65import static android.provider.Settings.Secure.WAKE_GESTURE_ENABLED;
66import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
67import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
68import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
69import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
70
Sudheer Shanka550d0682016-01-13 15:16:55 +000071import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
72
Amith Yamasanid7993472010-08-18 13:59:28 -070073public class DisplaySettings extends SettingsPreferenceFragment implements
Julia Reynoldsa02bc992015-08-12 14:51:34 -040074 Preference.OnPreferenceChangeListener, Indexable {
Amith Yamasani992f1022010-01-25 09:17:53 -080075 private static final String TAG = "DisplaySettings";
76
77 /** If there is no setting in the provider, use this. */
78 private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
79
80 private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
Dianne Hackborn6677e562011-03-04 17:30:54 -080081 private static final String KEY_FONT_SIZE = "font_size";
Daniel Sandler6f0ea442011-11-17 10:44:51 -080082 private static final String KEY_SCREEN_SAVER = "screensaver";
John Spurlock333efe92014-07-23 13:01:21 -040083 private static final String KEY_LIFT_TO_WAKE = "lift_to_wake";
John Spurlock911de7f2014-08-04 14:53:28 -040084 private static final String KEY_DOZE = "doze";
Jason Monk86dd6ba2015-03-31 16:39:28 -040085 private static final String KEY_TAP_TO_WAKE = "tap_to_wake";
Adrian Roos244e8ce2014-05-13 17:00:46 +020086 private static final String KEY_AUTO_BRIGHTNESS = "auto_brightness";
Jason Monkd3232da2014-08-20 13:29:20 -040087 private static final String KEY_AUTO_ROTATE = "auto_rotate";
Justin Klaassenc7cd2982016-06-21 18:32:04 -070088 private static final String KEY_NIGHT_DISPLAY = "night_display";
Alan Viverette893b3002015-02-25 18:47:37 -080089 private static final String KEY_NIGHT_MODE = "night_mode";
Jason Monkd30e4262015-08-04 11:22:29 -040090 private static final String KEY_CAMERA_GESTURE = "camera_gesture";
Oleksandr Peletskyi8b53b1e2016-01-27 18:07:18 +010091 private static final String KEY_WALLPAPER = "wallpaper";
Ruben Brunke4d95c02016-04-18 18:08:34 -070092 private static final String KEY_VR_DISPLAY_PREF = "vr_display_pref";
Jeff Brown9e143f52012-09-19 20:46:07 -070093
Noah Wanga6bb3d22016-01-25 21:30:08 -080094 private Preference mFontSizePref;
Amith Yamasani992f1022010-01-25 09:17:53 -080095
Sudheer Shanka900adce2016-03-04 16:58:34 -080096 private TimeoutListPreference mScreenTimeoutPreference;
Alan Viverette893b3002015-02-25 18:47:37 -080097 private ListPreference mNightModePreference;
Daniel Sandler6f0ea442011-11-17 10:44:51 -080098 private Preference mScreenSaverPreference;
John Spurlock333efe92014-07-23 13:01:21 -040099 private SwitchPreference mLiftToWakePreference;
Jason Monk86dd6ba2015-03-31 16:39:28 -0400100 private SwitchPreference mTapToWakePreference;
John Spurlock333efe92014-07-23 13:01:21 -0400101 private SwitchPreference mAutoBrightnessPreference;
Jason Monkd30e4262015-08-04 11:22:29 -0400102 private SwitchPreference mCameraGesturePreference;
Doris Lingfec32032016-08-26 14:29:52 -0700103 private PreferenceScreen mDozePreference;
Jim Miller9391b952011-01-07 16:55:33 -0800104
Amith Yamasani992f1022010-01-25 09:17:53 -0800105 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700106 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500107 return MetricsEvent.DISPLAY;
Chris Wren8a963ba2015-03-20 10:29:14 -0400108 }
109
110 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700111 public void onCreate(Bundle savedInstanceState) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800112 super.onCreate(savedInstanceState);
Jason Monkd3232da2014-08-20 13:29:20 -0400113 final Activity activity = getActivity();
114 final ContentResolver resolver = activity.getContentResolver();
Amith Yamasani992f1022010-01-25 09:17:53 -0800115
116 addPreferencesFromResource(R.xml.display_settings);
117
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800118 mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
Daniel Sandler47c991f2012-02-01 14:14:18 -0500119 if (mScreenSaverPreference != null
120 && getResources().getBoolean(
John Spurlockd983e1d2012-10-18 16:41:09 -0400121 com.android.internal.R.bool.config_dreamsSupported) == false) {
Daniel Sandler47c991f2012-02-01 14:14:18 -0500122 getPreferenceScreen().removePreference(mScreenSaverPreference);
123 }
Amith Yamasani78b82652013-08-16 16:35:54 -0700124
Sudheer Shanka900adce2016-03-04 16:58:34 -0800125 mScreenTimeoutPreference = (TimeoutListPreference) findPreference(KEY_SCREEN_TIMEOUT);
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400126
Noah Wanga6bb3d22016-01-25 21:30:08 -0800127 mFontSizePref = findPreference(KEY_FONT_SIZE);
Adrian Roos244e8ce2014-05-13 17:00:46 +0200128
Adrian Roos8ab09542014-06-02 20:33:56 +0200129 if (isAutomaticBrightnessAvailable(getResources())) {
John Spurlock333efe92014-07-23 13:01:21 -0400130 mAutoBrightnessPreference = (SwitchPreference) findPreference(KEY_AUTO_BRIGHTNESS);
Adrian Roos244e8ce2014-05-13 17:00:46 +0200131 mAutoBrightnessPreference.setOnPreferenceChangeListener(this);
132 } else {
133 removePreference(KEY_AUTO_BRIGHTNESS);
134 }
John Spurlock333efe92014-07-23 13:01:21 -0400135
Justin Klaassenc7cd2982016-06-21 18:32:04 -0700136 if (!NightDisplayController.isAvailable(activity)) {
137 removePreference(KEY_NIGHT_DISPLAY);
138 }
139
Jason Monkd3232da2014-08-20 13:29:20 -0400140 if (isLiftToWakeAvailable(activity)) {
John Spurlock333efe92014-07-23 13:01:21 -0400141 mLiftToWakePreference = (SwitchPreference) findPreference(KEY_LIFT_TO_WAKE);
142 mLiftToWakePreference.setOnPreferenceChangeListener(this);
143 } else {
144 removePreference(KEY_LIFT_TO_WAKE);
145 }
John Spurlock911de7f2014-08-04 14:53:28 -0400146
Doris Lingfec32032016-08-26 14:29:52 -0700147 if (!isDozeAvailable(activity)) {
Doris Ling9b3000d2016-08-05 11:36:33 -0700148 removePreference(KEY_DOZE);
Doris Lingfec32032016-08-26 14:29:52 -0700149 } else {
150 mDozePreference = (PreferenceScreen) findPreference(KEY_DOZE);
John Spurlock911de7f2014-08-04 14:53:28 -0400151 }
Jason Monkd3232da2014-08-20 13:29:20 -0400152
Jason Monk86dd6ba2015-03-31 16:39:28 -0400153 if (isTapToWakeAvailable(getResources())) {
154 mTapToWakePreference = (SwitchPreference) findPreference(KEY_TAP_TO_WAKE);
155 mTapToWakePreference.setOnPreferenceChangeListener(this);
156 } else {
157 removePreference(KEY_TAP_TO_WAKE);
158 }
159
Jason Monkd30e4262015-08-04 11:22:29 -0400160 if (isCameraGestureAvailable(getResources())) {
161 mCameraGesturePreference = (SwitchPreference) findPreference(KEY_CAMERA_GESTURE);
162 mCameraGesturePreference.setOnPreferenceChangeListener(this);
163 } else {
164 removePreference(KEY_CAMERA_GESTURE);
165 }
166
Jason Monkd3232da2014-08-20 13:29:20 -0400167 if (RotationPolicy.isRotationLockToggleVisible(activity)) {
168 DropDownPreference rotatePreference =
169 (DropDownPreference) findPreference(KEY_AUTO_ROTATE);
Jason Monkd3232da2014-08-20 13:29:20 -0400170 int rotateLockedResourceId;
171 // The following block sets the string used when rotation is locked.
172 // If the device locks specifically to portrait or landscape (rather than current
173 // rotation), then we use a different string to include this information.
174 if (allowAllRotations(activity)) {
175 rotateLockedResourceId = R.string.display_auto_rotate_stay_in_current;
176 } else {
177 if (RotationPolicy.getRotationLockOrientation(activity)
178 == Configuration.ORIENTATION_PORTRAIT) {
179 rotateLockedResourceId =
180 R.string.display_auto_rotate_stay_in_portrait;
181 } else {
182 rotateLockedResourceId =
183 R.string.display_auto_rotate_stay_in_landscape;
184 }
185 }
Jason Monk49b61032015-08-12 11:31:29 -0400186 rotatePreference.setEntries(new CharSequence[] {
187 activity.getString(R.string.display_auto_rotate_rotate),
188 activity.getString(rotateLockedResourceId),
189 });
190 rotatePreference.setEntryValues(new CharSequence[] { "0", "1" });
191 rotatePreference.setValueIndex(RotationPolicy.isRotationLocked(activity) ?
Jason Monkd3232da2014-08-20 13:29:20 -0400192 1 : 0);
Jason Monk49b61032015-08-12 11:31:29 -0400193 rotatePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
Jason Monkd3232da2014-08-20 13:29:20 -0400194 @Override
Jason Monk49b61032015-08-12 11:31:29 -0400195 public boolean onPreferenceChange(Preference preference, Object newValue) {
196 final boolean locked = Integer.parseInt((String) newValue) != 0;
Chris Wren9d1bfd12016-01-26 18:04:01 -0500197 MetricsLogger.action(getActivity(), MetricsEvent.ACTION_ROTATION_LOCK,
Chris Wrenc7581e82015-06-09 12:07:14 -0400198 locked);
199 RotationPolicy.setRotationLock(activity, locked);
Jason Monkd3232da2014-08-20 13:29:20 -0400200 return true;
201 }
202 });
203 } else {
204 removePreference(KEY_AUTO_ROTATE);
205 }
Alan Viverette893b3002015-02-25 18:47:37 -0800206
Ruben Brunke4d95c02016-04-18 18:08:34 -0700207 if (isVrDisplayModeAvailable(activity)) {
208 DropDownPreference vrDisplayPref =
209 (DropDownPreference) findPreference(KEY_VR_DISPLAY_PREF);
210 vrDisplayPref.setEntries(new CharSequence[] {
211 activity.getString(R.string.display_vr_pref_low_persistence),
212 activity.getString(R.string.display_vr_pref_off),
213 });
214 vrDisplayPref.setEntryValues(new CharSequence[] { "0", "1" });
215
216 final Context c = activity;
217 int currentUser = ActivityManager.getCurrentUser();
218 int current = Settings.Secure.getIntForUser(c.getContentResolver(),
219 Settings.Secure.VR_DISPLAY_MODE,
220 /*default*/Settings.Secure.VR_DISPLAY_MODE_LOW_PERSISTENCE,
221 currentUser);
222 vrDisplayPref.setValueIndex(current);
223 vrDisplayPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
224 @Override
225 public boolean onPreferenceChange(Preference preference, Object newValue) {
226 int i = Integer.parseInt((String) newValue);
227 int u = ActivityManager.getCurrentUser();
228 if (!Settings.Secure.putIntForUser(c.getContentResolver(),
229 Settings.Secure.VR_DISPLAY_MODE,
230 i, u)) {
231 Log.e(TAG, "Could not change setting for " +
232 Settings.Secure.VR_DISPLAY_MODE);
233 }
234 return true;
235 }
236 });
237 } else {
238 removePreference(KEY_VR_DISPLAY_PREF);
239 }
240
Alan Viverette893b3002015-02-25 18:47:37 -0800241 mNightModePreference = (ListPreference) findPreference(KEY_NIGHT_MODE);
Jason Monkdf283602015-05-21 16:46:24 -0400242 if (mNightModePreference != null) {
243 final UiModeManager uiManager = (UiModeManager) getSystemService(
244 Context.UI_MODE_SERVICE);
245 final int currentNightMode = uiManager.getNightMode();
246 mNightModePreference.setValue(String.valueOf(currentNightMode));
247 mNightModePreference.setOnPreferenceChangeListener(this);
248 }
Jason Monkd3232da2014-08-20 13:29:20 -0400249 }
250
251 private static boolean allowAllRotations(Context context) {
252 return Resources.getSystem().getBoolean(
253 com.android.internal.R.bool.config_allowAllRotations);
John Spurlock333efe92014-07-23 13:01:21 -0400254 }
255
256 private static boolean isLiftToWakeAvailable(Context context) {
257 SensorManager sensors = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
258 return sensors != null && sensors.getDefaultSensor(Sensor.TYPE_WAKE_GESTURE) != null;
Jim Miller9391b952011-01-07 16:55:33 -0800259 }
260
John Spurlock911de7f2014-08-04 14:53:28 -0400261 private static boolean isDozeAvailable(Context context) {
262 String name = Build.IS_DEBUGGABLE ? SystemProperties.get("debug.doze.component") : null;
263 if (TextUtils.isEmpty(name)) {
264 name = context.getResources().getString(
265 com.android.internal.R.string.config_dozeComponent);
266 }
267 return !TextUtils.isEmpty(name);
268 }
269
Jason Monk86dd6ba2015-03-31 16:39:28 -0400270 private static boolean isTapToWakeAvailable(Resources res) {
271 return res.getBoolean(com.android.internal.R.bool.config_supportDoubleTapWake);
272 }
273
Adrian Roos8ab09542014-06-02 20:33:56 +0200274 private static boolean isAutomaticBrightnessAvailable(Resources res) {
275 return res.getBoolean(com.android.internal.R.bool.config_automatic_brightness_available);
276 }
277
Jason Monkd30e4262015-08-04 11:22:29 -0400278 private static boolean isCameraGestureAvailable(Resources res) {
279 boolean configSet = res.getInteger(
280 com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1;
281 return configSet &&
282 !SystemProperties.getBoolean("gesture.disable_camera_launch", false);
283 }
284
Ruben Brunke4d95c02016-04-18 18:08:34 -0700285 private static boolean isVrDisplayModeAvailable(Context context) {
286 PackageManager pm = context.getPackageManager();
287 return pm.hasSystemFeature(PackageManager.FEATURE_VR_MODE_HIGH_PERFORMANCE);
288 }
289
Gilles Debunne698c24f2011-09-02 15:47:24 -0700290 private void updateTimeoutPreferenceDescription(long currentTimeout) {
Sudheer Shanka900adce2016-03-04 16:58:34 -0800291 TimeoutListPreference preference = mScreenTimeoutPreference;
Stephen Chen2b98ffa2016-09-20 13:15:11 -0700292 final CharSequence[] entries = preference.getEntries();
293 final CharSequence[] values = preference.getEntryValues();
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400294 String summary;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000295 if (preference.isDisabledByAdmin()) {
296 summary = getString(R.string.disabled_by_policy_title);
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400297 } else {
Stephen Chen2b98ffa2016-09-20 13:15:11 -0700298 CharSequence timeoutDescription = getTimeoutDescription(
299 currentTimeout, entries, values);
300 summary = timeoutDescription == null ? ""
301 : getString(R.string.screen_timeout_summary, timeoutDescription);
Jim Miller9391b952011-01-07 16:55:33 -0800302 }
Gilles Debunne698c24f2011-09-02 15:47:24 -0700303 preference.setSummary(summary);
Jim Millerd90b5a82010-03-29 20:18:51 -0700304 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800305
Stephen Chen2b98ffa2016-09-20 13:15:11 -0700306 private static CharSequence getTimeoutDescription(
307 long currentTimeout, CharSequence[] entries, CharSequence[] values) {
308 if (currentTimeout < 0 || entries == null || values == null
309 || values.length != entries.length) {
310 return null;
311 }
312
313 for (int i = 0; i < values.length; i++) {
314 long timeout = Long.parseLong(values[i].toString());
315 if (currentTimeout == timeout) {
316 return entries[i];
317 }
318 }
319 return null;
320 }
321
Amith Yamasani992f1022010-01-25 09:17:53 -0800322 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700323 public void onResume() {
Amith Yamasani992f1022010-01-25 09:17:53 -0800324 super.onResume();
Jeff Brown9e143f52012-09-19 20:46:07 -0700325 updateState();
Sudheer Shanka550d0682016-01-13 15:16:55 +0000326
327 final long currentTimeout = Settings.System.getLong(getActivity().getContentResolver(),
328 SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
329 mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
330 mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
Sudheer Shanka900adce2016-03-04 16:58:34 -0800331 final DevicePolicyManager dpm = (DevicePolicyManager) getActivity().getSystemService(
332 Context.DEVICE_POLICY_SERVICE);
333 if (dpm != null) {
334 final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(
335 getActivity());
Ricky Wai36cce832016-03-18 16:26:35 +0000336 final long maxTimeout = dpm
337 .getMaximumTimeToLockForUserAndProfiles(UserHandle.myUserId());
Sudheer Shanka900adce2016-03-04 16:58:34 -0800338 mScreenTimeoutPreference.removeUnusableTimeouts(maxTimeout, admin);
339 }
Sudheer Shanka550d0682016-01-13 15:16:55 +0000340 updateTimeoutPreferenceDescription(currentTimeout);
Oleksandr Peletskyic4854872016-04-05 15:37:07 +0200341
342 disablePreferenceIfManaged(KEY_WALLPAPER, UserManager.DISALLOW_SET_WALLPAPER);
Amith Yamasani26a1f892011-02-28 17:24:31 -0800343 }
344
Gilles Debunne737c81c2011-07-08 09:40:00 -0700345 private void updateState() {
Noah Wanga6bb3d22016-01-25 21:30:08 -0800346 updateFontSizeSummary();
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800347 updateScreenSaverSummary();
Adrian Roos244e8ce2014-05-13 17:00:46 +0200348
349 // Update auto brightness if it is available.
350 if (mAutoBrightnessPreference != null) {
351 int brightnessMode = Settings.System.getInt(getContentResolver(),
352 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
353 mAutoBrightnessPreference.setChecked(brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL);
354 }
John Spurlock333efe92014-07-23 13:01:21 -0400355
356 // Update lift-to-wake if it is available.
357 if (mLiftToWakePreference != null) {
358 int value = Settings.Secure.getInt(getContentResolver(), WAKE_GESTURE_ENABLED, 0);
359 mLiftToWakePreference.setChecked(value != 0);
360 }
John Spurlock911de7f2014-08-04 14:53:28 -0400361
Jason Monk86dd6ba2015-03-31 16:39:28 -0400362 // Update tap to wake if it is available.
363 if (mTapToWakePreference != null) {
364 int value = Settings.Secure.getInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, 0);
365 mTapToWakePreference.setChecked(value != 0);
366 }
Jason Monkd30e4262015-08-04 11:22:29 -0400367
Jorim Jaggicc1e9f72015-09-08 14:56:40 -0700368 // Update camera gesture #1 if it is available.
Jason Monkd30e4262015-08-04 11:22:29 -0400369 if (mCameraGesturePreference != null) {
370 int value = Settings.Secure.getInt(getContentResolver(), CAMERA_GESTURE_DISABLED, 0);
371 mCameraGesturePreference.setChecked(value == 0);
372 }
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800373 }
374
375 private void updateScreenSaverSummary() {
Daniel Sandlerc825bc82012-10-18 10:33:59 -0400376 if (mScreenSaverPreference != null) {
377 mScreenSaverPreference.setSummary(
378 DreamSettings.getSummaryTextWithDreamName(getActivity()));
379 }
Amith Yamasani26a1f892011-02-28 17:24:31 -0800380 }
381
Noah Wanga6bb3d22016-01-25 21:30:08 -0800382 private void updateFontSizeSummary() {
Casey Burkhardta8b2a502016-01-31 10:53:20 -0800383 final Context context = mFontSizePref.getContext();
384 final float currentScale = Settings.System.getFloat(context.getContentResolver(),
385 Settings.System.FONT_SCALE, 1.0f);
386 final Resources res = context.getResources();
Noah Wanga6bb3d22016-01-25 21:30:08 -0800387 final String[] entries = res.getStringArray(R.array.entries_font_size);
388 final String[] strEntryValues = res.getStringArray(R.array.entryvalues_font_size);
Casey Burkhardta8b2a502016-01-31 10:53:20 -0800389 final int index = ToggleFontSizePreferenceFragment.fontSizeValueToIndex(currentScale,
390 strEntryValues);
Noah Wanga6bb3d22016-01-25 21:30:08 -0800391 mFontSizePref.setSummary(entries[index]);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800392 }
Amith Yamasani883d8502011-09-20 14:41:03 -0700393
Amith Yamasani992f1022010-01-25 09:17:53 -0800394 @Override
Amith Yamasani992f1022010-01-25 09:17:53 -0800395 public boolean onPreferenceChange(Preference preference, Object objValue) {
396 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800397 if (KEY_SCREEN_TIMEOUT.equals(key)) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800398 try {
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900399 int value = Integer.parseInt((String) objValue);
Gilles Debunne698c24f2011-09-02 15:47:24 -0700400 Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
401 updateTimeoutPreferenceDescription(value);
Amith Yamasani992f1022010-01-25 09:17:53 -0800402 } catch (NumberFormatException e) {
403 Log.e(TAG, "could not persist screen timeout setting", e);
404 }
405 }
Adrian Roos244e8ce2014-05-13 17:00:46 +0200406 if (preference == mAutoBrightnessPreference) {
407 boolean auto = (Boolean) objValue;
408 Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE,
409 auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
410 }
John Spurlock333efe92014-07-23 13:01:21 -0400411 if (preference == mLiftToWakePreference) {
412 boolean value = (Boolean) objValue;
413 Settings.Secure.putInt(getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0);
414 }
Jason Monk86dd6ba2015-03-31 16:39:28 -0400415 if (preference == mTapToWakePreference) {
416 boolean value = (Boolean) objValue;
417 Settings.Secure.putInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, value ? 1 : 0);
418 }
Jason Monkd30e4262015-08-04 11:22:29 -0400419 if (preference == mCameraGesturePreference) {
420 boolean value = (Boolean) objValue;
421 Settings.Secure.putInt(getContentResolver(), CAMERA_GESTURE_DISABLED,
422 value ? 0 : 1 /* Backwards because setting is for disabling */);
423 }
Alan Viverette893b3002015-02-25 18:47:37 -0800424 if (preference == mNightModePreference) {
425 try {
426 final int value = Integer.parseInt((String) objValue);
427 final UiModeManager uiManager = (UiModeManager) getSystemService(
428 Context.UI_MODE_SERVICE);
429 uiManager.setNightMode(value);
430 } catch (NumberFormatException e) {
431 Log.e(TAG, "could not persist night mode setting", e);
432 }
433 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800434 return true;
435 }
Jeff Brown9e143f52012-09-19 20:46:07 -0700436
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700437 @Override
Doris Lingb727e9c2016-08-01 14:56:26 -0700438 public boolean onPreferenceTreeClick(Preference preference) {
439 if (preference == mDozePreference) {
440 MetricsLogger.action(getActivity(), MetricsEvent.ACTION_AMBIENT_DISPLAY);
441 }
442 return super.onPreferenceTreeClick(preference);
443 }
444
445 @Override
Jason Monk23acc2b2015-04-14 15:06:39 -0400446 protected int getHelpResource() {
447 return R.string.help_uri_display;
448 }
449
Oleksandr Peletskyic4854872016-04-05 15:37:07 +0200450 private void disablePreferenceIfManaged(String key, String restriction) {
451 final RestrictedPreference pref = (RestrictedPreference) findPreference(key);
Oleksandr Peletskyi8b53b1e2016-01-27 18:07:18 +0100452 if (pref != null) {
Oleksandr Peletskyic4854872016-04-05 15:37:07 +0200453 pref.setDisabledByAdmin(null);
454 if (RestrictedLockUtils.hasBaseUserRestriction(getActivity(), restriction,
455 UserHandle.myUserId())) {
456 pref.setEnabled(false);
457 } else {
458 pref.checkRestrictionAndSetDisabled(restriction);
459 }
Oleksandr Peletskyi8b53b1e2016-01-27 18:07:18 +0100460 }
461 }
462
Jason Monk30695812015-11-17 09:01:08 -0500463 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
464 private final Context mContext;
465 private final SummaryLoader mLoader;
466
467 private SummaryProvider(Context context, SummaryLoader loader) {
468 mContext = context;
469 mLoader = loader;
470 }
471
472 @Override
473 public void setListening(boolean listening) {
474 if (listening) {
475 updateSummary();
476 }
477 }
478
479 private void updateSummary() {
Stephen Chen2b98ffa2016-09-20 13:15:11 -0700480 final long currentTimeout = Settings.System.getLong(mContext.getContentResolver(),
481 SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
482 final CharSequence[] entries =
483 mContext.getResources().getTextArray(R.array.screen_timeout_entries);
484 final CharSequence[] values =
485 mContext.getResources().getTextArray(R.array.screen_timeout_values);
486 final CharSequence timeoutDescription = getTimeoutDescription(
487 currentTimeout, entries, values);
488 final String summary = timeoutDescription == null ? ""
489 : mContext.getString(R.string.display_summary, timeoutDescription);
490 mLoader.setSummary(this, summary);
Jason Monk30695812015-11-17 09:01:08 -0500491 }
492 }
493
494 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
495 = new SummaryLoader.SummaryProviderFactory() {
496 @Override
497 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
498 SummaryLoader summaryLoader) {
499 return new SummaryProvider(activity, summaryLoader);
500 }
501 };
502
Adrian Roos8ab09542014-06-02 20:33:56 +0200503 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
504 new BaseSearchIndexProvider() {
505 @Override
506 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
507 boolean enabled) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700508 ArrayList<SearchIndexableResource> result =
509 new ArrayList<SearchIndexableResource>();
Adrian Roos8ab09542014-06-02 20:33:56 +0200510
511 SearchIndexableResource sir = new SearchIndexableResource(context);
512 sir.xmlResId = R.xml.display_settings;
513 result.add(sir);
514
515 return result;
516 }
517
518 @Override
519 public List<String> getNonIndexableKeys(Context context) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700520 ArrayList<String> result = new ArrayList<String>();
521 if (!context.getResources().getBoolean(
522 com.android.internal.R.bool.config_dreamsSupported)) {
523 result.add(KEY_SCREEN_SAVER);
Adrian Roos8ab09542014-06-02 20:33:56 +0200524 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700525 if (!isAutomaticBrightnessAvailable(context.getResources())) {
526 result.add(KEY_AUTO_BRIGHTNESS);
527 }
Justin Klaassenc7cd2982016-06-21 18:32:04 -0700528 if (!NightDisplayController.isAvailable(context)) {
529 result.add(KEY_NIGHT_DISPLAY);
530 }
John Spurlock333efe92014-07-23 13:01:21 -0400531 if (!isLiftToWakeAvailable(context)) {
532 result.add(KEY_LIFT_TO_WAKE);
533 }
John Spurlock911de7f2014-08-04 14:53:28 -0400534 if (!isDozeAvailable(context)) {
535 result.add(KEY_DOZE);
536 }
Jason Monkd3232da2014-08-20 13:29:20 -0400537 if (!RotationPolicy.isRotationLockToggleVisible(context)) {
538 result.add(KEY_AUTO_ROTATE);
539 }
Jason Monk181e0e92015-06-08 09:11:48 -0400540 if (!isTapToWakeAvailable(context.getResources())) {
541 result.add(KEY_TAP_TO_WAKE);
542 }
Jason Monkd30e4262015-08-04 11:22:29 -0400543 if (!isCameraGestureAvailable(context.getResources())) {
544 result.add(KEY_CAMERA_GESTURE);
545 }
Ruben Brunke4d95c02016-04-18 18:08:34 -0700546 if (!isVrDisplayModeAvailable(context)) {
547 result.add(KEY_VR_DISPLAY_PREF);
548 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700549 return result;
Adrian Roos8ab09542014-06-02 20:33:56 +0200550 }
551 };
Amith Yamasani992f1022010-01-25 09:17:53 -0800552}