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