Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Jason Monk | d30e426 | 2015-08-04 11:22:29 -0400 | [diff] [blame] | 19 | import static android.provider.Settings.Secure.CAMERA_GESTURE_DISABLED; |
Jason Monk | 86dd6ba | 2015-03-31 16:39:28 -0400 | [diff] [blame] | 20 | import static android.provider.Settings.Secure.DOUBLE_TAP_TO_WAKE; |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 21 | import static android.provider.Settings.Secure.DOZE_ENABLED; |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 22 | import static android.provider.Settings.Secure.WAKE_GESTURE_ENABLED; |
Adrian Roos | 244e8ce | 2014-05-13 17:00:46 +0200 | [diff] [blame] | 23 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; |
| 24 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; |
| 25 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 26 | import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; |
| 27 | |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 28 | import android.app.Activity; |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 29 | import android.app.ActivityManagerNative; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 30 | import android.app.Dialog; |
Alan Viverette | 893b300 | 2015-02-25 18:47:37 -0800 | [diff] [blame] | 31 | import android.app.UiModeManager; |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 32 | import android.app.admin.DevicePolicyManager; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 33 | import android.content.ContentResolver; |
| 34 | import android.content.Context; |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 35 | import android.content.res.Configuration; |
Christopher Tate | 8a510c5 | 2011-07-29 16:16:22 -0700 | [diff] [blame] | 36 | import android.content.res.Resources; |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 37 | import android.hardware.Sensor; |
| 38 | import android.hardware.SensorManager; |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 39 | import android.os.Build; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 40 | import android.os.Bundle; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 41 | import android.os.RemoteException; |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 42 | import android.os.SystemProperties; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 43 | import android.preference.ListPreference; |
| 44 | import android.preference.Preference; |
Jason Monk | 49b6103 | 2015-08-12 11:31:29 -0400 | [diff] [blame^] | 45 | import android.preference.Preference.OnPreferenceChangeListener; |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 46 | import android.preference.Preference.OnPreferenceClickListener; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 47 | import android.preference.PreferenceScreen; |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 48 | import android.preference.SwitchPreference; |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 49 | import android.provider.SearchIndexableResource; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 50 | import android.provider.Settings; |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 51 | import android.text.TextUtils; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 52 | import android.util.Log; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 53 | |
Jason Monk | 49b6103 | 2015-08-12 11:31:29 -0400 | [diff] [blame^] | 54 | import com.android.internal.logging.MetricsLogger; |
| 55 | import com.android.internal.view.RotationPolicy; |
| 56 | import com.android.settings.search.BaseSearchIndexProvider; |
| 57 | import com.android.settings.search.Indexable; |
| 58 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 59 | import java.util.ArrayList; |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 60 | import java.util.List; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 61 | |
| 62 | public class DisplaySettings extends SettingsPreferenceFragment implements |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 63 | Preference.OnPreferenceChangeListener, OnPreferenceClickListener, Indexable { |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 64 | 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 Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 70 | private static final String KEY_FONT_SIZE = "font_size"; |
Daniel Sandler | 6f0ea44 | 2011-11-17 10:44:51 -0800 | [diff] [blame] | 71 | private static final String KEY_SCREEN_SAVER = "screensaver"; |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 72 | private static final String KEY_LIFT_TO_WAKE = "lift_to_wake"; |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 73 | private static final String KEY_DOZE = "doze"; |
Jason Monk | 86dd6ba | 2015-03-31 16:39:28 -0400 | [diff] [blame] | 74 | private static final String KEY_TAP_TO_WAKE = "tap_to_wake"; |
Adrian Roos | 244e8ce | 2014-05-13 17:00:46 +0200 | [diff] [blame] | 75 | private static final String KEY_AUTO_BRIGHTNESS = "auto_brightness"; |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 76 | private static final String KEY_AUTO_ROTATE = "auto_rotate"; |
Alan Viverette | 893b300 | 2015-02-25 18:47:37 -0800 | [diff] [blame] | 77 | private static final String KEY_NIGHT_MODE = "night_mode"; |
Jason Monk | d30e426 | 2015-08-04 11:22:29 -0400 | [diff] [blame] | 78 | private static final String KEY_CAMERA_GESTURE = "camera_gesture"; |
Jeff Brown | 9e143f5 | 2012-09-19 20:46:07 -0700 | [diff] [blame] | 79 | |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 80 | private static final int DLG_GLOBAL_CHANGE_WARNING = 1; |
| 81 | |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 82 | private WarnedListPreference mFontSizePref; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 83 | |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 84 | private final Configuration mCurConfig = new Configuration(); |
Shuhrat Dehkanov | 7e77699 | 2013-11-01 17:39:44 +0900 | [diff] [blame] | 85 | |
Jim Miller | 9391b95 | 2011-01-07 16:55:33 -0800 | [diff] [blame] | 86 | private ListPreference mScreenTimeoutPreference; |
Alan Viverette | 893b300 | 2015-02-25 18:47:37 -0800 | [diff] [blame] | 87 | private ListPreference mNightModePreference; |
Daniel Sandler | 6f0ea44 | 2011-11-17 10:44:51 -0800 | [diff] [blame] | 88 | private Preference mScreenSaverPreference; |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 89 | private SwitchPreference mLiftToWakePreference; |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 90 | private SwitchPreference mDozePreference; |
Jason Monk | 86dd6ba | 2015-03-31 16:39:28 -0400 | [diff] [blame] | 91 | private SwitchPreference mTapToWakePreference; |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 92 | private SwitchPreference mAutoBrightnessPreference; |
Jason Monk | d30e426 | 2015-08-04 11:22:29 -0400 | [diff] [blame] | 93 | private SwitchPreference mCameraGesturePreference; |
Jim Miller | 9391b95 | 2011-01-07 16:55:33 -0800 | [diff] [blame] | 94 | |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 95 | @Override |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 96 | protected int getMetricsCategory() { |
| 97 | return MetricsLogger.DISPLAY; |
| 98 | } |
| 99 | |
| 100 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 101 | public void onCreate(Bundle savedInstanceState) { |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 102 | super.onCreate(savedInstanceState); |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 103 | final Activity activity = getActivity(); |
| 104 | final ContentResolver resolver = activity.getContentResolver(); |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 105 | |
| 106 | addPreferencesFromResource(R.xml.display_settings); |
| 107 | |
Daniel Sandler | 6f0ea44 | 2011-11-17 10:44:51 -0800 | [diff] [blame] | 108 | mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER); |
Daniel Sandler | 47c991f | 2012-02-01 14:14:18 -0500 | [diff] [blame] | 109 | if (mScreenSaverPreference != null |
| 110 | && getResources().getBoolean( |
John Spurlock | d983e1d | 2012-10-18 16:41:09 -0400 | [diff] [blame] | 111 | com.android.internal.R.bool.config_dreamsSupported) == false) { |
Daniel Sandler | 47c991f | 2012-02-01 14:14:18 -0500 | [diff] [blame] | 112 | getPreferenceScreen().removePreference(mScreenSaverPreference); |
| 113 | } |
Amith Yamasani | 78b8265 | 2013-08-16 16:35:54 -0700 | [diff] [blame] | 114 | |
Jim Miller | 9391b95 | 2011-01-07 16:55:33 -0800 | [diff] [blame] | 115 | mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT); |
| 116 | final long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT, |
| 117 | FALLBACK_SCREEN_TIMEOUT_VALUE); |
| 118 | mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout)); |
| 119 | mScreenTimeoutPreference.setOnPreferenceChangeListener(this); |
| 120 | disableUnusableTimeouts(mScreenTimeoutPreference); |
Gilles Debunne | 698c24f | 2011-09-02 15:47:24 -0700 | [diff] [blame] | 121 | updateTimeoutPreferenceDescription(currentTimeout); |
Daniel Sandler | 2943cfa | 2011-04-15 11:18:57 -0400 | [diff] [blame] | 122 | |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 123 | mFontSizePref = (WarnedListPreference) findPreference(KEY_FONT_SIZE); |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 124 | mFontSizePref.setOnPreferenceChangeListener(this); |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 125 | mFontSizePref.setOnPreferenceClickListener(this); |
Adrian Roos | 244e8ce | 2014-05-13 17:00:46 +0200 | [diff] [blame] | 126 | |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 127 | if (isAutomaticBrightnessAvailable(getResources())) { |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 128 | mAutoBrightnessPreference = (SwitchPreference) findPreference(KEY_AUTO_BRIGHTNESS); |
Adrian Roos | 244e8ce | 2014-05-13 17:00:46 +0200 | [diff] [blame] | 129 | mAutoBrightnessPreference.setOnPreferenceChangeListener(this); |
| 130 | } else { |
| 131 | removePreference(KEY_AUTO_BRIGHTNESS); |
| 132 | } |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 133 | |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 134 | if (isLiftToWakeAvailable(activity)) { |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 135 | mLiftToWakePreference = (SwitchPreference) findPreference(KEY_LIFT_TO_WAKE); |
| 136 | mLiftToWakePreference.setOnPreferenceChangeListener(this); |
| 137 | } else { |
| 138 | removePreference(KEY_LIFT_TO_WAKE); |
| 139 | } |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 140 | |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 141 | if (isDozeAvailable(activity)) { |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 142 | mDozePreference = (SwitchPreference) findPreference(KEY_DOZE); |
| 143 | mDozePreference.setOnPreferenceChangeListener(this); |
| 144 | } else { |
| 145 | removePreference(KEY_DOZE); |
| 146 | } |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 147 | |
Jason Monk | 86dd6ba | 2015-03-31 16:39:28 -0400 | [diff] [blame] | 148 | 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 Monk | d30e426 | 2015-08-04 11:22:29 -0400 | [diff] [blame] | 155 | if (isCameraGestureAvailable(getResources())) { |
| 156 | mCameraGesturePreference = (SwitchPreference) findPreference(KEY_CAMERA_GESTURE); |
| 157 | mCameraGesturePreference.setOnPreferenceChangeListener(this); |
| 158 | } else { |
| 159 | removePreference(KEY_CAMERA_GESTURE); |
| 160 | } |
| 161 | |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 162 | if (RotationPolicy.isRotationLockToggleVisible(activity)) { |
| 163 | DropDownPreference rotatePreference = |
| 164 | (DropDownPreference) findPreference(KEY_AUTO_ROTATE); |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 165 | int rotateLockedResourceId; |
| 166 | // The following block sets the string used when rotation is locked. |
| 167 | // If the device locks specifically to portrait or landscape (rather than current |
| 168 | // rotation), then we use a different string to include this information. |
| 169 | if (allowAllRotations(activity)) { |
| 170 | rotateLockedResourceId = R.string.display_auto_rotate_stay_in_current; |
| 171 | } else { |
| 172 | if (RotationPolicy.getRotationLockOrientation(activity) |
| 173 | == Configuration.ORIENTATION_PORTRAIT) { |
| 174 | rotateLockedResourceId = |
| 175 | R.string.display_auto_rotate_stay_in_portrait; |
| 176 | } else { |
| 177 | rotateLockedResourceId = |
| 178 | R.string.display_auto_rotate_stay_in_landscape; |
| 179 | } |
| 180 | } |
Jason Monk | 49b6103 | 2015-08-12 11:31:29 -0400 | [diff] [blame^] | 181 | rotatePreference.setEntries(new CharSequence[] { |
| 182 | activity.getString(R.string.display_auto_rotate_rotate), |
| 183 | activity.getString(rotateLockedResourceId), |
| 184 | }); |
| 185 | rotatePreference.setEntryValues(new CharSequence[] { "0", "1" }); |
| 186 | rotatePreference.setValueIndex(RotationPolicy.isRotationLocked(activity) ? |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 187 | 1 : 0); |
Jason Monk | 49b6103 | 2015-08-12 11:31:29 -0400 | [diff] [blame^] | 188 | rotatePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 189 | @Override |
Jason Monk | 49b6103 | 2015-08-12 11:31:29 -0400 | [diff] [blame^] | 190 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 191 | final boolean locked = Integer.parseInt((String) newValue) != 0; |
Chris Wren | ca689ae | 2015-06-09 14:24:03 -0400 | [diff] [blame] | 192 | MetricsLogger.action(getActivity(), MetricsLogger.ACTION_ROTATION_LOCK, |
Chris Wren | c7581e8 | 2015-06-09 12:07:14 -0400 | [diff] [blame] | 193 | locked); |
| 194 | RotationPolicy.setRotationLock(activity, locked); |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 195 | return true; |
| 196 | } |
| 197 | }); |
| 198 | } else { |
| 199 | removePreference(KEY_AUTO_ROTATE); |
| 200 | } |
Alan Viverette | 893b300 | 2015-02-25 18:47:37 -0800 | [diff] [blame] | 201 | |
| 202 | mNightModePreference = (ListPreference) findPreference(KEY_NIGHT_MODE); |
Jason Monk | df28360 | 2015-05-21 16:46:24 -0400 | [diff] [blame] | 203 | if (mNightModePreference != null) { |
| 204 | final UiModeManager uiManager = (UiModeManager) getSystemService( |
| 205 | Context.UI_MODE_SERVICE); |
| 206 | final int currentNightMode = uiManager.getNightMode(); |
| 207 | mNightModePreference.setValue(String.valueOf(currentNightMode)); |
| 208 | mNightModePreference.setOnPreferenceChangeListener(this); |
| 209 | } |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | private static boolean allowAllRotations(Context context) { |
| 213 | return Resources.getSystem().getBoolean( |
| 214 | com.android.internal.R.bool.config_allowAllRotations); |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | private static boolean isLiftToWakeAvailable(Context context) { |
| 218 | SensorManager sensors = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); |
| 219 | return sensors != null && sensors.getDefaultSensor(Sensor.TYPE_WAKE_GESTURE) != null; |
Jim Miller | 9391b95 | 2011-01-07 16:55:33 -0800 | [diff] [blame] | 220 | } |
| 221 | |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 222 | private static boolean isDozeAvailable(Context context) { |
| 223 | String name = Build.IS_DEBUGGABLE ? SystemProperties.get("debug.doze.component") : null; |
| 224 | if (TextUtils.isEmpty(name)) { |
| 225 | name = context.getResources().getString( |
| 226 | com.android.internal.R.string.config_dozeComponent); |
| 227 | } |
| 228 | return !TextUtils.isEmpty(name); |
| 229 | } |
| 230 | |
Jason Monk | 86dd6ba | 2015-03-31 16:39:28 -0400 | [diff] [blame] | 231 | private static boolean isTapToWakeAvailable(Resources res) { |
| 232 | return res.getBoolean(com.android.internal.R.bool.config_supportDoubleTapWake); |
| 233 | } |
| 234 | |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 235 | private static boolean isAutomaticBrightnessAvailable(Resources res) { |
| 236 | return res.getBoolean(com.android.internal.R.bool.config_automatic_brightness_available); |
| 237 | } |
| 238 | |
Jason Monk | d30e426 | 2015-08-04 11:22:29 -0400 | [diff] [blame] | 239 | private static boolean isCameraGestureAvailable(Resources res) { |
| 240 | boolean configSet = res.getInteger( |
| 241 | com.android.internal.R.integer.config_cameraLaunchGestureSensorType) != -1; |
| 242 | return configSet && |
| 243 | !SystemProperties.getBoolean("gesture.disable_camera_launch", false); |
| 244 | } |
| 245 | |
Gilles Debunne | 698c24f | 2011-09-02 15:47:24 -0700 | [diff] [blame] | 246 | private void updateTimeoutPreferenceDescription(long currentTimeout) { |
| 247 | ListPreference preference = mScreenTimeoutPreference; |
Daniel Sandler | 2943cfa | 2011-04-15 11:18:57 -0400 | [diff] [blame] | 248 | String summary; |
Gilles Debunne | 698c24f | 2011-09-02 15:47:24 -0700 | [diff] [blame] | 249 | if (currentTimeout < 0) { |
| 250 | // Unsupported value |
| 251 | summary = ""; |
Daniel Sandler | 2943cfa | 2011-04-15 11:18:57 -0400 | [diff] [blame] | 252 | } else { |
Gilles Debunne | 698c24f | 2011-09-02 15:47:24 -0700 | [diff] [blame] | 253 | final CharSequence[] entries = preference.getEntries(); |
| 254 | final CharSequence[] values = preference.getEntryValues(); |
Amith Yamasani | 64516a1 | 2012-10-22 16:38:02 -0700 | [diff] [blame] | 255 | if (entries == null || entries.length == 0) { |
| 256 | summary = ""; |
| 257 | } else { |
| 258 | int best = 0; |
| 259 | for (int i = 0; i < values.length; i++) { |
| 260 | long timeout = Long.parseLong(values[i].toString()); |
| 261 | if (currentTimeout >= timeout) { |
| 262 | best = i; |
| 263 | } |
Daniel Sandler | 2943cfa | 2011-04-15 11:18:57 -0400 | [diff] [blame] | 264 | } |
Amith Yamasani | 64516a1 | 2012-10-22 16:38:02 -0700 | [diff] [blame] | 265 | summary = preference.getContext().getString(R.string.screen_timeout_summary, |
| 266 | entries[best]); |
Jim Miller | 9391b95 | 2011-01-07 16:55:33 -0800 | [diff] [blame] | 267 | } |
| 268 | } |
Gilles Debunne | 698c24f | 2011-09-02 15:47:24 -0700 | [diff] [blame] | 269 | preference.setSummary(summary); |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 270 | } |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 271 | |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 272 | private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) { |
Jim Miller | fc5a022 | 2010-04-13 15:31:41 -0700 | [diff] [blame] | 273 | final DevicePolicyManager dpm = |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 274 | (DevicePolicyManager) getActivity().getSystemService( |
| 275 | Context.DEVICE_POLICY_SERVICE); |
Jim Miller | fc5a022 | 2010-04-13 15:31:41 -0700 | [diff] [blame] | 276 | final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0; |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 277 | if (maxTimeout == 0) { |
| 278 | return; // policy not enforced |
| 279 | } |
| 280 | final CharSequence[] entries = screenTimeoutPreference.getEntries(); |
| 281 | final CharSequence[] values = screenTimeoutPreference.getEntryValues(); |
| 282 | ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>(); |
| 283 | ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>(); |
| 284 | for (int i = 0; i < values.length; i++) { |
Gilles Debunne | 698c24f | 2011-09-02 15:47:24 -0700 | [diff] [blame] | 285 | long timeout = Long.parseLong(values[i].toString()); |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 286 | if (timeout <= maxTimeout) { |
| 287 | revisedEntries.add(entries[i]); |
| 288 | revisedValues.add(values[i]); |
| 289 | } |
| 290 | } |
| 291 | if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) { |
Amith Yamasani | 78b8265 | 2013-08-16 16:35:54 -0700 | [diff] [blame] | 292 | final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue()); |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 293 | screenTimeoutPreference.setEntries( |
| 294 | revisedEntries.toArray(new CharSequence[revisedEntries.size()])); |
| 295 | screenTimeoutPreference.setEntryValues( |
| 296 | revisedValues.toArray(new CharSequence[revisedValues.size()])); |
Jim Miller | fc5a022 | 2010-04-13 15:31:41 -0700 | [diff] [blame] | 297 | if (userPreference <= maxTimeout) { |
| 298 | screenTimeoutPreference.setValue(String.valueOf(userPreference)); |
Amith Yamasani | 78b8265 | 2013-08-16 16:35:54 -0700 | [diff] [blame] | 299 | } else if (revisedValues.size() > 0 |
| 300 | && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString()) |
| 301 | == maxTimeout) { |
| 302 | // If the last one happens to be the same as the max timeout, select that |
| 303 | screenTimeoutPreference.setValue(String.valueOf(maxTimeout)); |
Jim Miller | fc5a022 | 2010-04-13 15:31:41 -0700 | [diff] [blame] | 304 | } else { |
| 305 | // There will be no highlighted selection since nothing in the list matches |
| 306 | // maxTimeout. The user can still select anything less than maxTimeout. |
| 307 | // TODO: maybe append maxTimeout to the list and mark selected. |
| 308 | } |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 309 | } |
| 310 | screenTimeoutPreference.setEnabled(revisedEntries.size() > 0); |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Christopher Tate | 8a510c5 | 2011-07-29 16:16:22 -0700 | [diff] [blame] | 313 | int floatToIndex(float val) { |
| 314 | String[] indices = getResources().getStringArray(R.array.entryvalues_font_size); |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 315 | float lastVal = Float.parseFloat(indices[0]); |
| 316 | for (int i=1; i<indices.length; i++) { |
| 317 | float thisVal = Float.parseFloat(indices[i]); |
| 318 | if (val < (lastVal + (thisVal-lastVal)*.5f)) { |
| 319 | return i-1; |
| 320 | } |
| 321 | lastVal = thisVal; |
| 322 | } |
| 323 | return indices.length-1; |
| 324 | } |
Shuhrat Dehkanov | 7e77699 | 2013-11-01 17:39:44 +0900 | [diff] [blame] | 325 | |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 326 | public void readFontSizePreference(ListPreference pref) { |
| 327 | try { |
Gilles Debunne | 737c81c | 2011-07-08 09:40:00 -0700 | [diff] [blame] | 328 | mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration()); |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 329 | } catch (RemoteException e) { |
Gilles Debunne | 737c81c | 2011-07-08 09:40:00 -0700 | [diff] [blame] | 330 | Log.w(TAG, "Unable to retrieve font size"); |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 331 | } |
Christopher Tate | 8a510c5 | 2011-07-29 16:16:22 -0700 | [diff] [blame] | 332 | |
| 333 | // mark the appropriate item in the preferences list |
| 334 | int index = floatToIndex(mCurConfig.fontScale); |
| 335 | pref.setValueIndex(index); |
| 336 | |
| 337 | // report the current size in the summary text |
| 338 | final Resources res = getResources(); |
| 339 | String[] fontSizeNames = res.getStringArray(R.array.entries_font_size); |
| 340 | pref.setSummary(String.format(res.getString(R.string.summary_font_size), |
| 341 | fontSizeNames[index])); |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 342 | } |
Shuhrat Dehkanov | 7e77699 | 2013-11-01 17:39:44 +0900 | [diff] [blame] | 343 | |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 344 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 345 | public void onResume() { |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 346 | super.onResume(); |
Jeff Brown | 9e143f5 | 2012-09-19 20:46:07 -0700 | [diff] [blame] | 347 | updateState(); |
Amith Yamasani | 26a1f89 | 2011-02-28 17:24:31 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Override |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 351 | public Dialog onCreateDialog(int dialogId) { |
| 352 | if (dialogId == DLG_GLOBAL_CHANGE_WARNING) { |
| 353 | return Utils.buildGlobalChangeWarningDialog(getActivity(), |
| 354 | R.string.global_font_change_title, |
| 355 | new Runnable() { |
| 356 | public void run() { |
| 357 | mFontSizePref.click(); |
| 358 | } |
| 359 | }); |
| 360 | } |
| 361 | return null; |
| 362 | } |
| 363 | |
Gilles Debunne | 737c81c | 2011-07-08 09:40:00 -0700 | [diff] [blame] | 364 | private void updateState() { |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 365 | readFontSizePreference(mFontSizePref); |
Daniel Sandler | 6f0ea44 | 2011-11-17 10:44:51 -0800 | [diff] [blame] | 366 | updateScreenSaverSummary(); |
Adrian Roos | 244e8ce | 2014-05-13 17:00:46 +0200 | [diff] [blame] | 367 | |
| 368 | // Update auto brightness if it is available. |
| 369 | if (mAutoBrightnessPreference != null) { |
| 370 | int brightnessMode = Settings.System.getInt(getContentResolver(), |
| 371 | SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL); |
| 372 | mAutoBrightnessPreference.setChecked(brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL); |
| 373 | } |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 374 | |
| 375 | // Update lift-to-wake if it is available. |
| 376 | if (mLiftToWakePreference != null) { |
| 377 | int value = Settings.Secure.getInt(getContentResolver(), WAKE_GESTURE_ENABLED, 0); |
| 378 | mLiftToWakePreference.setChecked(value != 0); |
| 379 | } |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 380 | |
| 381 | // Update doze if it is available. |
| 382 | if (mDozePreference != null) { |
| 383 | int value = Settings.Secure.getInt(getContentResolver(), DOZE_ENABLED, 1); |
| 384 | mDozePreference.setChecked(value != 0); |
| 385 | } |
Jason Monk | 86dd6ba | 2015-03-31 16:39:28 -0400 | [diff] [blame] | 386 | |
| 387 | // Update tap to wake if it is available. |
| 388 | if (mTapToWakePreference != null) { |
| 389 | int value = Settings.Secure.getInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, 0); |
| 390 | mTapToWakePreference.setChecked(value != 0); |
| 391 | } |
Jason Monk | d30e426 | 2015-08-04 11:22:29 -0400 | [diff] [blame] | 392 | |
| 393 | // Update camera gesture if it is available. |
| 394 | if (mCameraGesturePreference != null) { |
| 395 | int value = Settings.Secure.getInt(getContentResolver(), CAMERA_GESTURE_DISABLED, 0); |
| 396 | mCameraGesturePreference.setChecked(value == 0); |
| 397 | } |
Daniel Sandler | 6f0ea44 | 2011-11-17 10:44:51 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | private void updateScreenSaverSummary() { |
Daniel Sandler | c825bc8 | 2012-10-18 10:33:59 -0400 | [diff] [blame] | 401 | if (mScreenSaverPreference != null) { |
| 402 | mScreenSaverPreference.setSummary( |
| 403 | DreamSettings.getSummaryTextWithDreamName(getActivity())); |
| 404 | } |
Amith Yamasani | 26a1f89 | 2011-02-28 17:24:31 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 407 | public void writeFontSizePreference(Object objValue) { |
| 408 | try { |
| 409 | mCurConfig.fontScale = Float.parseFloat(objValue.toString()); |
Dianne Hackborn | 92c8eba | 2011-07-19 14:59:01 -0700 | [diff] [blame] | 410 | ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig); |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 411 | } catch (RemoteException e) { |
Gilles Debunne | 737c81c | 2011-07-08 09:40:00 -0700 | [diff] [blame] | 412 | Log.w(TAG, "Unable to save font size"); |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 413 | } |
| 414 | } |
Amith Yamasani | 883d850 | 2011-09-20 14:41:03 -0700 | [diff] [blame] | 415 | |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 416 | @Override |
| 417 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
Daniel Sandler | 2943cfa | 2011-04-15 11:18:57 -0400 | [diff] [blame] | 418 | return super.onPreferenceTreeClick(preferenceScreen, preference); |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Jeff Brown | 4e54c2a | 2013-11-10 21:02:20 -0800 | [diff] [blame] | 421 | @Override |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 422 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
| 423 | final String key = preference.getKey(); |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 424 | if (KEY_SCREEN_TIMEOUT.equals(key)) { |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 425 | try { |
Shuhrat Dehkanov | 7e77699 | 2013-11-01 17:39:44 +0900 | [diff] [blame] | 426 | int value = Integer.parseInt((String) objValue); |
Gilles Debunne | 698c24f | 2011-09-02 15:47:24 -0700 | [diff] [blame] | 427 | Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value); |
| 428 | updateTimeoutPreferenceDescription(value); |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 429 | } catch (NumberFormatException e) { |
| 430 | Log.e(TAG, "could not persist screen timeout setting", e); |
| 431 | } |
| 432 | } |
Dianne Hackborn | 6677e56 | 2011-03-04 17:30:54 -0800 | [diff] [blame] | 433 | if (KEY_FONT_SIZE.equals(key)) { |
| 434 | writeFontSizePreference(objValue); |
| 435 | } |
Adrian Roos | 244e8ce | 2014-05-13 17:00:46 +0200 | [diff] [blame] | 436 | if (preference == mAutoBrightnessPreference) { |
| 437 | boolean auto = (Boolean) objValue; |
| 438 | Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE, |
| 439 | auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL); |
| 440 | } |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 441 | if (preference == mLiftToWakePreference) { |
| 442 | boolean value = (Boolean) objValue; |
| 443 | Settings.Secure.putInt(getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0); |
| 444 | } |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 445 | if (preference == mDozePreference) { |
| 446 | boolean value = (Boolean) objValue; |
| 447 | Settings.Secure.putInt(getContentResolver(), DOZE_ENABLED, value ? 1 : 0); |
| 448 | } |
Jason Monk | 86dd6ba | 2015-03-31 16:39:28 -0400 | [diff] [blame] | 449 | if (preference == mTapToWakePreference) { |
| 450 | boolean value = (Boolean) objValue; |
| 451 | Settings.Secure.putInt(getContentResolver(), DOUBLE_TAP_TO_WAKE, value ? 1 : 0); |
| 452 | } |
Jason Monk | d30e426 | 2015-08-04 11:22:29 -0400 | [diff] [blame] | 453 | if (preference == mCameraGesturePreference) { |
| 454 | boolean value = (Boolean) objValue; |
| 455 | Settings.Secure.putInt(getContentResolver(), CAMERA_GESTURE_DISABLED, |
| 456 | value ? 0 : 1 /* Backwards because setting is for disabling */); |
| 457 | } |
Alan Viverette | 893b300 | 2015-02-25 18:47:37 -0800 | [diff] [blame] | 458 | if (preference == mNightModePreference) { |
| 459 | try { |
| 460 | final int value = Integer.parseInt((String) objValue); |
| 461 | final UiModeManager uiManager = (UiModeManager) getSystemService( |
| 462 | Context.UI_MODE_SERVICE); |
| 463 | uiManager.setNightMode(value); |
| 464 | } catch (NumberFormatException e) { |
| 465 | Log.e(TAG, "could not persist night mode setting", e); |
| 466 | } |
| 467 | } |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 468 | return true; |
| 469 | } |
Jeff Brown | 9e143f5 | 2012-09-19 20:46:07 -0700 | [diff] [blame] | 470 | |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 471 | @Override |
| 472 | public boolean onPreferenceClick(Preference preference) { |
| 473 | if (preference == mFontSizePref) { |
| 474 | if (Utils.hasMultipleUsers(getActivity())) { |
| 475 | showDialog(DLG_GLOBAL_CHANGE_WARNING); |
| 476 | return true; |
| 477 | } else { |
| 478 | mFontSizePref.click(); |
| 479 | } |
| 480 | } |
| 481 | return false; |
| 482 | } |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 483 | |
Jason Monk | 23acc2b | 2015-04-14 15:06:39 -0400 | [diff] [blame] | 484 | @Override |
| 485 | protected int getHelpResource() { |
| 486 | return R.string.help_uri_display; |
| 487 | } |
| 488 | |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 489 | public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = |
| 490 | new BaseSearchIndexProvider() { |
| 491 | @Override |
| 492 | public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, |
| 493 | boolean enabled) { |
Fabrice Di Meglio | d407f2a | 2014-06-09 10:13:43 -0700 | [diff] [blame] | 494 | ArrayList<SearchIndexableResource> result = |
| 495 | new ArrayList<SearchIndexableResource>(); |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 496 | |
| 497 | SearchIndexableResource sir = new SearchIndexableResource(context); |
| 498 | sir.xmlResId = R.xml.display_settings; |
| 499 | result.add(sir); |
| 500 | |
| 501 | return result; |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public List<String> getNonIndexableKeys(Context context) { |
Fabrice Di Meglio | d407f2a | 2014-06-09 10:13:43 -0700 | [diff] [blame] | 506 | ArrayList<String> result = new ArrayList<String>(); |
| 507 | if (!context.getResources().getBoolean( |
| 508 | com.android.internal.R.bool.config_dreamsSupported)) { |
| 509 | result.add(KEY_SCREEN_SAVER); |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 510 | } |
Fabrice Di Meglio | d407f2a | 2014-06-09 10:13:43 -0700 | [diff] [blame] | 511 | if (!isAutomaticBrightnessAvailable(context.getResources())) { |
| 512 | result.add(KEY_AUTO_BRIGHTNESS); |
| 513 | } |
John Spurlock | 333efe9 | 2014-07-23 13:01:21 -0400 | [diff] [blame] | 514 | if (!isLiftToWakeAvailable(context)) { |
| 515 | result.add(KEY_LIFT_TO_WAKE); |
| 516 | } |
John Spurlock | 911de7f | 2014-08-04 14:53:28 -0400 | [diff] [blame] | 517 | if (!isDozeAvailable(context)) { |
| 518 | result.add(KEY_DOZE); |
| 519 | } |
Jason Monk | d3232da | 2014-08-20 13:29:20 -0400 | [diff] [blame] | 520 | if (!RotationPolicy.isRotationLockToggleVisible(context)) { |
| 521 | result.add(KEY_AUTO_ROTATE); |
| 522 | } |
Jason Monk | 181e0e9 | 2015-06-08 09:11:48 -0400 | [diff] [blame] | 523 | if (!isTapToWakeAvailable(context.getResources())) { |
| 524 | result.add(KEY_TAP_TO_WAKE); |
| 525 | } |
Jason Monk | d30e426 | 2015-08-04 11:22:29 -0400 | [diff] [blame] | 526 | if (!isCameraGestureAvailable(context.getResources())) { |
| 527 | result.add(KEY_CAMERA_GESTURE); |
| 528 | } |
Fabrice Di Meglio | d407f2a | 2014-06-09 10:13:43 -0700 | [diff] [blame] | 529 | return result; |
Adrian Roos | 8ab0954 | 2014-06-02 20:33:56 +0200 | [diff] [blame] | 530 | } |
| 531 | }; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 532 | } |