blob: d35c11b39a0b0f9e1b686ca02132519a333d2e82 [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;
John Spurlock911de7f2014-08-04 14:53:28 -040044import android.text.TextUtils;
Amith Yamasani992f1022010-01-25 09:17:53 -080045import android.util.Log;
Jason Monk1230ac82015-12-11 20:14:43 -050046
Justin Klaassenc7cd2982016-06-21 18:32:04 -070047import com.android.internal.app.NightDisplayController;
Jason Monk49b61032015-08-12 11:31:29 -040048import com.android.internal.logging.MetricsLogger;
Chris Wren9d1bfd12016-01-26 18:04:01 -050049import com.android.internal.logging.MetricsProto.MetricsEvent;
Jason Monk49b61032015-08-12 11:31:29 -040050import com.android.internal.view.RotationPolicy;
Noah Wanga6bb3d22016-01-25 21:30:08 -080051import com.android.settings.accessibility.ToggleFontSizePreferenceFragment;
Jason Monk30695812015-11-17 09:01:08 -050052import com.android.settings.dashboard.SummaryLoader;
Jason Monk49b61032015-08-12 11:31:29 -040053import com.android.settings.search.BaseSearchIndexProvider;
54import com.android.settings.search.Indexable;
Sudheer Shanka550d0682016-01-13 15:16:55 +000055import com.android.settingslib.RestrictedLockUtils;
Oleksandr Peletskyic4854872016-04-05 15:37:07 +020056import com.android.settingslib.RestrictedPreference;
Jason Monk49b61032015-08-12 11:31:29 -040057
Amith Yamasanid7993472010-08-18 13:59:28 -070058import java.util.ArrayList;
Adrian Roos8ab09542014-06-02 20:33:56 +020059import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070060
Jason Monk39b46742015-09-10 15:52:51 -040061import static android.provider.Settings.Secure.CAMERA_GESTURE_DISABLED;
62import static android.provider.Settings.Secure.DOUBLE_TAP_TO_WAKE;
63import static android.provider.Settings.Secure.DOZE_ENABLED;
64import static android.provider.Settings.Secure.WAKE_GESTURE_ENABLED;
65import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
66import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
67import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
68import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
69
Sudheer Shanka550d0682016-01-13 15:16:55 +000070import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
71
Amith Yamasanid7993472010-08-18 13:59:28 -070072public class DisplaySettings extends SettingsPreferenceFragment implements
Julia Reynoldsa02bc992015-08-12 14:51:34 -040073 Preference.OnPreferenceChangeListener, Indexable {
Amith Yamasani992f1022010-01-25 09:17:53 -080074 private static final String TAG = "DisplaySettings";
75
76 /** If there is no setting in the provider, use this. */
77 private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
78
79 private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
Dianne Hackborn6677e562011-03-04 17:30:54 -080080 private static final String KEY_FONT_SIZE = "font_size";
Daniel Sandler6f0ea442011-11-17 10:44:51 -080081 private static final String KEY_SCREEN_SAVER = "screensaver";
John Spurlock333efe92014-07-23 13:01:21 -040082 private static final String KEY_LIFT_TO_WAKE = "lift_to_wake";
John Spurlock911de7f2014-08-04 14:53:28 -040083 private static final String KEY_DOZE = "doze";
Jason Monk86dd6ba2015-03-31 16:39:28 -040084 private static final String KEY_TAP_TO_WAKE = "tap_to_wake";
Adrian Roos244e8ce2014-05-13 17:00:46 +020085 private static final String KEY_AUTO_BRIGHTNESS = "auto_brightness";
Jason Monkd3232da2014-08-20 13:29:20 -040086 private static final String KEY_AUTO_ROTATE = "auto_rotate";
Justin Klaassenc7cd2982016-06-21 18:32:04 -070087 private static final String KEY_NIGHT_DISPLAY = "night_display";
Alan Viverette893b3002015-02-25 18:47:37 -080088 private static final String KEY_NIGHT_MODE = "night_mode";
Jason Monkd30e4262015-08-04 11:22:29 -040089 private static final String KEY_CAMERA_GESTURE = "camera_gesture";
Oleksandr Peletskyi8b53b1e2016-01-27 18:07:18 +010090 private static final String KEY_WALLPAPER = "wallpaper";
Ruben Brunke4d95c02016-04-18 18:08:34 -070091 private static final String KEY_VR_DISPLAY_PREF = "vr_display_pref";
Jeff Brown9e143f52012-09-19 20:46:07 -070092
Noah Wanga6bb3d22016-01-25 21:30:08 -080093 private Preference mFontSizePref;
Amith Yamasani992f1022010-01-25 09:17:53 -080094
Sudheer Shanka900adce2016-03-04 16:58:34 -080095 private TimeoutListPreference mScreenTimeoutPreference;
Alan Viverette893b3002015-02-25 18:47:37 -080096 private ListPreference mNightModePreference;
Daniel Sandler6f0ea442011-11-17 10:44:51 -080097 private Preference mScreenSaverPreference;
John Spurlock333efe92014-07-23 13:01:21 -040098 private SwitchPreference mLiftToWakePreference;
Doris Ling9b3000d2016-08-05 11:36:33 -070099 private SwitchPreference mDozePreference;
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;
Jim Miller9391b952011-01-07 16:55:33 -0800103
Amith Yamasani992f1022010-01-25 09:17:53 -0800104 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700105 public int getMetricsCategory() {
Chris Wren9d1bfd12016-01-26 18:04:01 -0500106 return MetricsEvent.DISPLAY;
Chris Wren8a963ba2015-03-20 10:29:14 -0400107 }
108
109 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700110 public void onCreate(Bundle savedInstanceState) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800111 super.onCreate(savedInstanceState);
Jason Monkd3232da2014-08-20 13:29:20 -0400112 final Activity activity = getActivity();
113 final ContentResolver resolver = activity.getContentResolver();
Amith Yamasani992f1022010-01-25 09:17:53 -0800114
115 addPreferencesFromResource(R.xml.display_settings);
116
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800117 mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
Daniel Sandler47c991f2012-02-01 14:14:18 -0500118 if (mScreenSaverPreference != null
119 && getResources().getBoolean(
John Spurlockd983e1d2012-10-18 16:41:09 -0400120 com.android.internal.R.bool.config_dreamsSupported) == false) {
Daniel Sandler47c991f2012-02-01 14:14:18 -0500121 getPreferenceScreen().removePreference(mScreenSaverPreference);
122 }
Amith Yamasani78b82652013-08-16 16:35:54 -0700123
Sudheer Shanka900adce2016-03-04 16:58:34 -0800124 mScreenTimeoutPreference = (TimeoutListPreference) findPreference(KEY_SCREEN_TIMEOUT);
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400125
Noah Wanga6bb3d22016-01-25 21:30:08 -0800126 mFontSizePref = findPreference(KEY_FONT_SIZE);
Adrian Roos244e8ce2014-05-13 17:00:46 +0200127
Adrian Roos8ab09542014-06-02 20:33:56 +0200128 if (isAutomaticBrightnessAvailable(getResources())) {
John Spurlock333efe92014-07-23 13:01:21 -0400129 mAutoBrightnessPreference = (SwitchPreference) findPreference(KEY_AUTO_BRIGHTNESS);
Adrian Roos244e8ce2014-05-13 17:00:46 +0200130 mAutoBrightnessPreference.setOnPreferenceChangeListener(this);
131 } else {
132 removePreference(KEY_AUTO_BRIGHTNESS);
133 }
John Spurlock333efe92014-07-23 13:01:21 -0400134
Justin Klaassenc7cd2982016-06-21 18:32:04 -0700135 if (!NightDisplayController.isAvailable(activity)) {
136 removePreference(KEY_NIGHT_DISPLAY);
137 }
138
Jason Monkd3232da2014-08-20 13:29:20 -0400139 if (isLiftToWakeAvailable(activity)) {
John Spurlock333efe92014-07-23 13:01:21 -0400140 mLiftToWakePreference = (SwitchPreference) findPreference(KEY_LIFT_TO_WAKE);
141 mLiftToWakePreference.setOnPreferenceChangeListener(this);
142 } else {
143 removePreference(KEY_LIFT_TO_WAKE);
144 }
John Spurlock911de7f2014-08-04 14:53:28 -0400145
Doris Ling9b3000d2016-08-05 11:36:33 -0700146 if (isDozeAvailable(activity)) {
147 mDozePreference = (SwitchPreference) findPreference(KEY_DOZE);
148 mDozePreference.setOnPreferenceChangeListener(this);
Doris Lingb727e9c2016-08-01 14:56:26 -0700149 } else {
Doris Ling9b3000d2016-08-05 11:36:33 -0700150 removePreference(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
Doris Ling9b3000d2016-08-05 11:36:33 -0700363 // Update doze if it is available.
364 if (mDozePreference != null) {
365 int value = Settings.Secure.getInt(getContentResolver(), DOZE_ENABLED, 1);
366 mDozePreference.setChecked(value != 0);
367 }
368
Jorim Jaggicc1e9f72015-09-08 14:56:40 -0700369 // Update camera gesture #1 if it is available.
Jason Monkd30e4262015-08-04 11:22:29 -0400370 if (mCameraGesturePreference != null) {
371 int value = Settings.Secure.getInt(getContentResolver(), CAMERA_GESTURE_DISABLED, 0);
372 mCameraGesturePreference.setChecked(value == 0);
373 }
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800374 }
375
376 private void updateScreenSaverSummary() {
Daniel Sandlerc825bc82012-10-18 10:33:59 -0400377 if (mScreenSaverPreference != null) {
378 mScreenSaverPreference.setSummary(
379 DreamSettings.getSummaryTextWithDreamName(getActivity()));
380 }
Amith Yamasani26a1f892011-02-28 17:24:31 -0800381 }
382
Noah Wanga6bb3d22016-01-25 21:30:08 -0800383 private void updateFontSizeSummary() {
Casey Burkhardta8b2a502016-01-31 10:53:20 -0800384 final Context context = mFontSizePref.getContext();
385 final float currentScale = Settings.System.getFloat(context.getContentResolver(),
386 Settings.System.FONT_SCALE, 1.0f);
387 final Resources res = context.getResources();
Noah Wanga6bb3d22016-01-25 21:30:08 -0800388 final String[] entries = res.getStringArray(R.array.entries_font_size);
389 final String[] strEntryValues = res.getStringArray(R.array.entryvalues_font_size);
Casey Burkhardta8b2a502016-01-31 10:53:20 -0800390 final int index = ToggleFontSizePreferenceFragment.fontSizeValueToIndex(currentScale,
391 strEntryValues);
Noah Wanga6bb3d22016-01-25 21:30:08 -0800392 mFontSizePref.setSummary(entries[index]);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800393 }
Amith Yamasani883d8502011-09-20 14:41:03 -0700394
Amith Yamasani992f1022010-01-25 09:17:53 -0800395 @Override
Amith Yamasani992f1022010-01-25 09:17:53 -0800396 public boolean onPreferenceChange(Preference preference, Object objValue) {
397 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800398 if (KEY_SCREEN_TIMEOUT.equals(key)) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800399 try {
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900400 int value = Integer.parseInt((String) objValue);
Gilles Debunne698c24f2011-09-02 15:47:24 -0700401 Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
402 updateTimeoutPreferenceDescription(value);
Amith Yamasani992f1022010-01-25 09:17:53 -0800403 } catch (NumberFormatException e) {
404 Log.e(TAG, "could not persist screen timeout setting", e);
405 }
406 }
Adrian Roos244e8ce2014-05-13 17:00:46 +0200407 if (preference == mAutoBrightnessPreference) {
408 boolean auto = (Boolean) objValue;
409 Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE,
410 auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
411 }
John Spurlock333efe92014-07-23 13:01:21 -0400412 if (preference == mLiftToWakePreference) {
413 boolean value = (Boolean) objValue;
414 Settings.Secure.putInt(getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0);
415 }
Doris Ling9b3000d2016-08-05 11:36:33 -0700416 if (preference == mDozePreference) {
417 boolean value = (Boolean) objValue;
418 Settings.Secure.putInt(getContentResolver(), DOZE_ENABLED, value ? 1 : 0);
419 }
Jason Monk86dd6ba2015-03-31 16:39:28 -0400420 if (preference == mTapToWakePreference) {
421 boolean value = (Boolean) objValue;
422 Settings.Secure.putInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, value ? 1 : 0);
423 }
Jason Monkd30e4262015-08-04 11:22:29 -0400424 if (preference == mCameraGesturePreference) {
425 boolean value = (Boolean) objValue;
426 Settings.Secure.putInt(getContentResolver(), CAMERA_GESTURE_DISABLED,
427 value ? 0 : 1 /* Backwards because setting is for disabling */);
428 }
Alan Viverette893b3002015-02-25 18:47:37 -0800429 if (preference == mNightModePreference) {
430 try {
431 final int value = Integer.parseInt((String) objValue);
432 final UiModeManager uiManager = (UiModeManager) getSystemService(
433 Context.UI_MODE_SERVICE);
434 uiManager.setNightMode(value);
435 } catch (NumberFormatException e) {
436 Log.e(TAG, "could not persist night mode setting", e);
437 }
438 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800439 return true;
440 }
Jeff Brown9e143f52012-09-19 20:46:07 -0700441
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700442 @Override
Doris Lingb727e9c2016-08-01 14:56:26 -0700443 public boolean onPreferenceTreeClick(Preference preference) {
444 if (preference == mDozePreference) {
445 MetricsLogger.action(getActivity(), MetricsEvent.ACTION_AMBIENT_DISPLAY);
446 }
447 return super.onPreferenceTreeClick(preference);
448 }
449
450 @Override
Jason Monk23acc2b2015-04-14 15:06:39 -0400451 protected int getHelpResource() {
452 return R.string.help_uri_display;
453 }
454
Oleksandr Peletskyic4854872016-04-05 15:37:07 +0200455 private void disablePreferenceIfManaged(String key, String restriction) {
456 final RestrictedPreference pref = (RestrictedPreference) findPreference(key);
Oleksandr Peletskyi8b53b1e2016-01-27 18:07:18 +0100457 if (pref != null) {
Oleksandr Peletskyic4854872016-04-05 15:37:07 +0200458 pref.setDisabledByAdmin(null);
459 if (RestrictedLockUtils.hasBaseUserRestriction(getActivity(), restriction,
460 UserHandle.myUserId())) {
461 pref.setEnabled(false);
462 } else {
463 pref.checkRestrictionAndSetDisabled(restriction);
464 }
Oleksandr Peletskyi8b53b1e2016-01-27 18:07:18 +0100465 }
466 }
467
Jason Monk30695812015-11-17 09:01:08 -0500468 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
469 private final Context mContext;
470 private final SummaryLoader mLoader;
471
472 private SummaryProvider(Context context, SummaryLoader loader) {
473 mContext = context;
474 mLoader = loader;
475 }
476
477 @Override
478 public void setListening(boolean listening) {
479 if (listening) {
480 updateSummary();
481 }
482 }
483
484 private void updateSummary() {
485 boolean auto = Settings.System.getInt(mContext.getContentResolver(),
486 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
487 == SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
488 mLoader.setSummary(this, mContext.getString(auto ? R.string.display_summary_on
489 : R.string.display_summary_off));
490 }
491 }
492
493 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
494 = new SummaryLoader.SummaryProviderFactory() {
495 @Override
496 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
497 SummaryLoader summaryLoader) {
498 return new SummaryProvider(activity, summaryLoader);
499 }
500 };
501
Adrian Roos8ab09542014-06-02 20:33:56 +0200502 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
503 new BaseSearchIndexProvider() {
504 @Override
505 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
506 boolean enabled) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700507 ArrayList<SearchIndexableResource> result =
508 new ArrayList<SearchIndexableResource>();
Adrian Roos8ab09542014-06-02 20:33:56 +0200509
510 SearchIndexableResource sir = new SearchIndexableResource(context);
511 sir.xmlResId = R.xml.display_settings;
512 result.add(sir);
513
514 return result;
515 }
516
517 @Override
518 public List<String> getNonIndexableKeys(Context context) {
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700519 ArrayList<String> result = new ArrayList<String>();
520 if (!context.getResources().getBoolean(
521 com.android.internal.R.bool.config_dreamsSupported)) {
522 result.add(KEY_SCREEN_SAVER);
Adrian Roos8ab09542014-06-02 20:33:56 +0200523 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700524 if (!isAutomaticBrightnessAvailable(context.getResources())) {
525 result.add(KEY_AUTO_BRIGHTNESS);
526 }
Justin Klaassenc7cd2982016-06-21 18:32:04 -0700527 if (!NightDisplayController.isAvailable(context)) {
528 result.add(KEY_NIGHT_DISPLAY);
529 }
John Spurlock333efe92014-07-23 13:01:21 -0400530 if (!isLiftToWakeAvailable(context)) {
531 result.add(KEY_LIFT_TO_WAKE);
532 }
John Spurlock911de7f2014-08-04 14:53:28 -0400533 if (!isDozeAvailable(context)) {
534 result.add(KEY_DOZE);
535 }
Jason Monkd3232da2014-08-20 13:29:20 -0400536 if (!RotationPolicy.isRotationLockToggleVisible(context)) {
537 result.add(KEY_AUTO_ROTATE);
538 }
Jason Monk181e0e92015-06-08 09:11:48 -0400539 if (!isTapToWakeAvailable(context.getResources())) {
540 result.add(KEY_TAP_TO_WAKE);
541 }
Jason Monkd30e4262015-08-04 11:22:29 -0400542 if (!isCameraGestureAvailable(context.getResources())) {
543 result.add(KEY_CAMERA_GESTURE);
544 }
Ruben Brunke4d95c02016-04-18 18:08:34 -0700545 if (!isVrDisplayModeAvailable(context)) {
546 result.add(KEY_VR_DISPLAY_PREF);
547 }
Fabrice Di Megliod407f2a2014-06-09 10:13:43 -0700548 return result;
Adrian Roos8ab09542014-06-02 20:33:56 +0200549 }
550 };
Amith Yamasani992f1022010-01-25 09:17:53 -0800551}