blob: 87634fc1a56e916b6e6448ca566d7d6f2b666170 [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 Lingb727e9c2016-08-01 14:56:26 -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 Lingb727e9c2016-08-01 14:56:26 -0700103 private PreferenceScreen mDozePreference;
Jim Miller9391b952011-01-07 16:55:33 -0800104
Amith Yamasani992f1022010-01-25 09:17:53 -0800105 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -0400106 protected 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 Lingd0c8fd72016-06-14 11:21:57 -0700147 if (!isDozeAvailable(activity)) {
John Spurlock911de7f2014-08-04 14:53:28 -0400148 removePreference(KEY_DOZE);
Doris Lingb727e9c2016-08-01 14:56:26 -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;
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400292 String summary;
Sudheer Shanka550d0682016-01-13 15:16:55 +0000293 if (preference.isDisabledByAdmin()) {
294 summary = getString(R.string.disabled_by_policy_title);
295 } else if (currentTimeout < 0) {
Gilles Debunne698c24f2011-09-02 15:47:24 -0700296 // Unsupported value
297 summary = "";
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400298 } else {
Gilles Debunne698c24f2011-09-02 15:47:24 -0700299 final CharSequence[] entries = preference.getEntries();
300 final CharSequence[] values = preference.getEntryValues();
Amith Yamasani64516a12012-10-22 16:38:02 -0700301 if (entries == null || entries.length == 0) {
302 summary = "";
303 } else {
304 int best = 0;
305 for (int i = 0; i < values.length; i++) {
306 long timeout = Long.parseLong(values[i].toString());
307 if (currentTimeout >= timeout) {
308 best = i;
309 }
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400310 }
Sudheer Shanka550d0682016-01-13 15:16:55 +0000311 summary = getString(R.string.screen_timeout_summary, entries[best]);
Jim Miller9391b952011-01-07 16:55:33 -0800312 }
313 }
Gilles Debunne698c24f2011-09-02 15:47:24 -0700314 preference.setSummary(summary);
Jim Millerd90b5a82010-03-29 20:18:51 -0700315 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800316
Amith Yamasani992f1022010-01-25 09:17:53 -0800317 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700318 public void onResume() {
Amith Yamasani992f1022010-01-25 09:17:53 -0800319 super.onResume();
Jeff Brown9e143f52012-09-19 20:46:07 -0700320 updateState();
Sudheer Shanka550d0682016-01-13 15:16:55 +0000321
322 final long currentTimeout = Settings.System.getLong(getActivity().getContentResolver(),
323 SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
324 mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
325 mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
Sudheer Shanka900adce2016-03-04 16:58:34 -0800326 final DevicePolicyManager dpm = (DevicePolicyManager) getActivity().getSystemService(
327 Context.DEVICE_POLICY_SERVICE);
328 if (dpm != null) {
329 final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(
330 getActivity());
Ricky Wai36cce832016-03-18 16:26:35 +0000331 final long maxTimeout = dpm
332 .getMaximumTimeToLockForUserAndProfiles(UserHandle.myUserId());
Sudheer Shanka900adce2016-03-04 16:58:34 -0800333 mScreenTimeoutPreference.removeUnusableTimeouts(maxTimeout, admin);
334 }
Sudheer Shanka550d0682016-01-13 15:16:55 +0000335 updateTimeoutPreferenceDescription(currentTimeout);
Oleksandr Peletskyic4854872016-04-05 15:37:07 +0200336
337 disablePreferenceIfManaged(KEY_WALLPAPER, UserManager.DISALLOW_SET_WALLPAPER);
Amith Yamasani26a1f892011-02-28 17:24:31 -0800338 }
339
Gilles Debunne737c81c2011-07-08 09:40:00 -0700340 private void updateState() {
Noah Wanga6bb3d22016-01-25 21:30:08 -0800341 updateFontSizeSummary();
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800342 updateScreenSaverSummary();
Adrian Roos244e8ce2014-05-13 17:00:46 +0200343
344 // Update auto brightness if it is available.
345 if (mAutoBrightnessPreference != null) {
346 int brightnessMode = Settings.System.getInt(getContentResolver(),
347 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
348 mAutoBrightnessPreference.setChecked(brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL);
349 }
John Spurlock333efe92014-07-23 13:01:21 -0400350
351 // Update lift-to-wake if it is available.
352 if (mLiftToWakePreference != null) {
353 int value = Settings.Secure.getInt(getContentResolver(), WAKE_GESTURE_ENABLED, 0);
354 mLiftToWakePreference.setChecked(value != 0);
355 }
John Spurlock911de7f2014-08-04 14:53:28 -0400356
Jason Monk86dd6ba2015-03-31 16:39:28 -0400357 // Update tap to wake if it is available.
358 if (mTapToWakePreference != null) {
359 int value = Settings.Secure.getInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, 0);
360 mTapToWakePreference.setChecked(value != 0);
361 }
Jason Monkd30e4262015-08-04 11:22:29 -0400362
Jorim Jaggicc1e9f72015-09-08 14:56:40 -0700363 // Update camera gesture #1 if it is available.
Jason Monkd30e4262015-08-04 11:22:29 -0400364 if (mCameraGesturePreference != null) {
365 int value = Settings.Secure.getInt(getContentResolver(), CAMERA_GESTURE_DISABLED, 0);
366 mCameraGesturePreference.setChecked(value == 0);
367 }
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800368 }
369
370 private void updateScreenSaverSummary() {
Daniel Sandlerc825bc82012-10-18 10:33:59 -0400371 if (mScreenSaverPreference != null) {
372 mScreenSaverPreference.setSummary(
373 DreamSettings.getSummaryTextWithDreamName(getActivity()));
374 }
Amith Yamasani26a1f892011-02-28 17:24:31 -0800375 }
376
Noah Wanga6bb3d22016-01-25 21:30:08 -0800377 private void updateFontSizeSummary() {
Casey Burkhardta8b2a502016-01-31 10:53:20 -0800378 final Context context = mFontSizePref.getContext();
379 final float currentScale = Settings.System.getFloat(context.getContentResolver(),
380 Settings.System.FONT_SCALE, 1.0f);
381 final Resources res = context.getResources();
Noah Wanga6bb3d22016-01-25 21:30:08 -0800382 final String[] entries = res.getStringArray(R.array.entries_font_size);
383 final String[] strEntryValues = res.getStringArray(R.array.entryvalues_font_size);
Casey Burkhardta8b2a502016-01-31 10:53:20 -0800384 final int index = ToggleFontSizePreferenceFragment.fontSizeValueToIndex(currentScale,
385 strEntryValues);
Noah Wanga6bb3d22016-01-25 21:30:08 -0800386 mFontSizePref.setSummary(entries[index]);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800387 }
Amith Yamasani883d8502011-09-20 14:41:03 -0700388
Amith Yamasani992f1022010-01-25 09:17:53 -0800389 @Override
Amith Yamasani992f1022010-01-25 09:17:53 -0800390 public boolean onPreferenceChange(Preference preference, Object objValue) {
391 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800392 if (KEY_SCREEN_TIMEOUT.equals(key)) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800393 try {
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900394 int value = Integer.parseInt((String) objValue);
Gilles Debunne698c24f2011-09-02 15:47:24 -0700395 Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
396 updateTimeoutPreferenceDescription(value);
Amith Yamasani992f1022010-01-25 09:17:53 -0800397 } catch (NumberFormatException e) {
398 Log.e(TAG, "could not persist screen timeout setting", e);
399 }
400 }
Adrian Roos244e8ce2014-05-13 17:00:46 +0200401 if (preference == mAutoBrightnessPreference) {
402 boolean auto = (Boolean) objValue;
403 Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE,
404 auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
405 }
John Spurlock333efe92014-07-23 13:01:21 -0400406 if (preference == mLiftToWakePreference) {
407 boolean value = (Boolean) objValue;
408 Settings.Secure.putInt(getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0);
409 }
Jason Monk86dd6ba2015-03-31 16:39:28 -0400410 if (preference == mTapToWakePreference) {
411 boolean value = (Boolean) objValue;
412 Settings.Secure.putInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, value ? 1 : 0);
413 }
Jason Monkd30e4262015-08-04 11:22:29 -0400414 if (preference == mCameraGesturePreference) {
415 boolean value = (Boolean) objValue;
416 Settings.Secure.putInt(getContentResolver(), CAMERA_GESTURE_DISABLED,
417 value ? 0 : 1 /* Backwards because setting is for disabling */);
418 }
Alan Viverette893b3002015-02-25 18:47:37 -0800419 if (preference == mNightModePreference) {
420 try {
421 final int value = Integer.parseInt((String) objValue);
422 final UiModeManager uiManager = (UiModeManager) getSystemService(
423 Context.UI_MODE_SERVICE);
424 uiManager.setNightMode(value);
425 } catch (NumberFormatException e) {
426 Log.e(TAG, "could not persist night mode setting", e);
427 }
428 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800429 return true;
430 }
Jeff Brown9e143f52012-09-19 20:46:07 -0700431
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700432 @Override
Doris Lingb727e9c2016-08-01 14:56:26 -0700433 public boolean onPreferenceTreeClick(Preference preference) {
434 if (preference == mDozePreference) {
435 MetricsLogger.action(getActivity(), MetricsEvent.ACTION_AMBIENT_DISPLAY);
436 }
437 return super.onPreferenceTreeClick(preference);
438 }
439
440 @Override
Jason Monk23acc2b2015-04-14 15:06:39 -0400441 protected int getHelpResource() {
442 return R.string.help_uri_display;
443 }
444
Oleksandr Peletskyic4854872016-04-05 15:37:07 +0200445 private void disablePreferenceIfManaged(String key, String restriction) {
446 final RestrictedPreference pref = (RestrictedPreference) findPreference(key);
Oleksandr Peletskyi8b53b1e2016-01-27 18:07:18 +0100447 if (pref != null) {
Oleksandr Peletskyic4854872016-04-05 15:37:07 +0200448 pref.setDisabledByAdmin(null);
449 if (RestrictedLockUtils.hasBaseUserRestriction(getActivity(), restriction,
450 UserHandle.myUserId())) {
451 pref.setEnabled(false);
452 } else {
453 pref.checkRestrictionAndSetDisabled(restriction);
454 }
Oleksandr Peletskyi8b53b1e2016-01-27 18:07:18 +0100455 }
456 }
457
Jason Monk30695812015-11-17 09:01:08 -0500458 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
459 private final Context mContext;
460 private final SummaryLoader mLoader;
461
462 private SummaryProvider(Context context, SummaryLoader loader) {
463 mContext = context;
464 mLoader = loader;
465 }
466
467 @Override
468 public void setListening(boolean listening) {
469 if (listening) {
470 updateSummary();
471 }
472 }
473
474 private void updateSummary() {
475 boolean auto = Settings.System.getInt(mContext.getContentResolver(),
476 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
477 == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
478 mLoader.setSummary(this, mContext.getString(auto ? R.string.display_summary_on
479 : R.string.display_summary_off));
480 }
481 }
482
483 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
484 = new SummaryLoader.SummaryProviderFactory() {
485 @Override
486 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
487 SummaryLoader summaryLoader) {
488 return new SummaryProvider(activity, summaryLoader);
489 }
490 };
491
Adrian Roos8ab09542014-06-02 20:33:56 +0200492 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
493 new BaseSearchIndexProvider() {
494 @Override
495 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
496 boolean enabled) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700497 ArrayList<SearchIndexableResource> result =
498 new ArrayList<SearchIndexableResource>();
Adrian Roos8ab09542014-06-02 20:33:56 +0200499
500 SearchIndexableResource sir = new SearchIndexableResource(context);
501 sir.xmlResId = R.xml.display_settings;
502 result.add(sir);
503
504 return result;
505 }
506
507 @Override
508 public List<String> getNonIndexableKeys(Context context) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700509 ArrayList<String> result = new ArrayList<String>();
510 if (!context.getResources().getBoolean(
511 com.android.internal.R.bool.config_dreamsSupported)) {
512 result.add(KEY_SCREEN_SAVER);
Adrian Roos8ab09542014-06-02 20:33:56 +0200513 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700514 if (!isAutomaticBrightnessAvailable(context.getResources())) {
515 result.add(KEY_AUTO_BRIGHTNESS);
516 }
Justin Klaassenc7cd2982016-06-21 18:32:04 -0700517 if (!NightDisplayController.isAvailable(context)) {
518 result.add(KEY_NIGHT_DISPLAY);
519 }
John Spurlock333efe92014-07-23 13:01:21 -0400520 if (!isLiftToWakeAvailable(context)) {
521 result.add(KEY_LIFT_TO_WAKE);
522 }
John Spurlock911de7f2014-08-04 14:53:28 -0400523 if (!isDozeAvailable(context)) {
524 result.add(KEY_DOZE);
525 }
Jason Monkd3232da2014-08-20 13:29:20 -0400526 if (!RotationPolicy.isRotationLockToggleVisible(context)) {
527 result.add(KEY_AUTO_ROTATE);
528 }
Jason Monk181e0e92015-06-08 09:11:48 -0400529 if (!isTapToWakeAvailable(context.getResources())) {
530 result.add(KEY_TAP_TO_WAKE);
531 }
Jason Monkd30e4262015-08-04 11:22:29 -0400532 if (!isCameraGestureAvailable(context.getResources())) {
533 result.add(KEY_CAMERA_GESTURE);
534 }
Ruben Brunke4d95c02016-04-18 18:08:34 -0700535 if (!isVrDisplayModeAvailable(context)) {
536 result.add(KEY_VR_DISPLAY_PREF);
537 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700538 return result;
Adrian Roos8ab09542014-06-02 20:33:56 +0200539 }
540 };
Amith Yamasani992f1022010-01-25 09:17:53 -0800541}