blob: 75236de8c41d296c30d94a86b10709684b9f3f37 [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
Adrian Roos8ab09542014-06-02 20:33:56 +020019import com.android.settings.search.BaseSearchIndexProvider;
20import com.android.settings.search.Indexable;
21
Adrian Roos244e8ce2014-05-13 17:00:46 +020022import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
23import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
24import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
Amith Yamasani992f1022010-01-25 09:17:53 -080025import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
26
Dianne Hackborn6677e562011-03-04 17:30:54 -080027import android.app.ActivityManagerNative;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070028import android.app.Dialog;
Jim Millerd90b5a82010-03-29 20:18:51 -070029import android.app.admin.DevicePolicyManager;
Amith Yamasani992f1022010-01-25 09:17:53 -080030import android.content.ContentResolver;
31import android.content.Context;
Dianne Hackborn6677e562011-03-04 17:30:54 -080032import android.content.res.Configuration;
Christopher Tate8a510c52011-07-29 16:16:22 -070033import android.content.res.Resources;
Amith Yamasani992f1022010-01-25 09:17:53 -080034import android.os.Bundle;
Chris Wrenad1b7eb2014-02-28 16:16:59 -050035import android.os.Handler;
Amith Yamasani992f1022010-01-25 09:17:53 -080036import android.os.RemoteException;
Amith Yamasani992f1022010-01-25 09:17:53 -080037import android.preference.CheckBoxPreference;
38import android.preference.ListPreference;
39import android.preference.Preference;
Amith Yamasani9627a8e2012-09-23 12:54:14 -070040import android.preference.Preference.OnPreferenceClickListener;
Amith Yamasani992f1022010-01-25 09:17:53 -080041import android.preference.PreferenceScreen;
Adrian Roos8ab09542014-06-02 20:33:56 +020042import android.provider.SearchIndexableResource;
Amith Yamasani992f1022010-01-25 09:17:53 -080043import android.provider.Settings;
Amith Yamasani992f1022010-01-25 09:17:53 -080044import android.util.Log;
Amith Yamasani992f1022010-01-25 09:17:53 -080045
Amith Yamasanid7993472010-08-18 13:59:28 -070046import java.util.ArrayList;
Adrian Roos8ab09542014-06-02 20:33:56 +020047import java.util.List;
Amith Yamasanid7993472010-08-18 13:59:28 -070048
49public class DisplaySettings extends SettingsPreferenceFragment implements
Adrian Roos8ab09542014-06-02 20:33:56 +020050 Preference.OnPreferenceChangeListener, OnPreferenceClickListener, Indexable {
Amith Yamasani992f1022010-01-25 09:17:53 -080051 private static final String TAG = "DisplaySettings";
52
53 /** If there is no setting in the provider, use this. */
54 private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
55
56 private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
Dianne Hackborn6677e562011-03-04 17:30:54 -080057 private static final String KEY_FONT_SIZE = "font_size";
Daniel Sandler6f0ea442011-11-17 10:44:51 -080058 private static final String KEY_SCREEN_SAVER = "screensaver";
Adrian Roos244e8ce2014-05-13 17:00:46 +020059 private static final String KEY_AUTO_BRIGHTNESS = "auto_brightness";
Jeff Brown9e143f52012-09-19 20:46:07 -070060
Amith Yamasani9627a8e2012-09-23 12:54:14 -070061 private static final int DLG_GLOBAL_CHANGE_WARNING = 1;
62
Amith Yamasani9627a8e2012-09-23 12:54:14 -070063 private WarnedListPreference mFontSizePref;
Amith Yamasani992f1022010-01-25 09:17:53 -080064
Dianne Hackborn6677e562011-03-04 17:30:54 -080065 private final Configuration mCurConfig = new Configuration();
Chris Wrenad1b7eb2014-02-28 16:16:59 -050066 private final Handler mHandler = new Handler();
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +090067
Jim Miller9391b952011-01-07 16:55:33 -080068 private ListPreference mScreenTimeoutPreference;
Daniel Sandler6f0ea442011-11-17 10:44:51 -080069 private Preference mScreenSaverPreference;
Adrian Roos244e8ce2014-05-13 17:00:46 +020070 private CheckBoxPreference mAutoBrightnessPreference;
Jim Miller9391b952011-01-07 16:55:33 -080071
Amith Yamasani992f1022010-01-25 09:17:53 -080072 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070073 public void onCreate(Bundle savedInstanceState) {
Amith Yamasani992f1022010-01-25 09:17:53 -080074 super.onCreate(savedInstanceState);
Chris Wrenad1b7eb2014-02-28 16:16:59 -050075 final ContentResolver resolver = getActivity().getContentResolver();
Amith Yamasani992f1022010-01-25 09:17:53 -080076
77 addPreferencesFromResource(R.xml.display_settings);
78
Daniel Sandler6f0ea442011-11-17 10:44:51 -080079 mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
Daniel Sandler47c991f2012-02-01 14:14:18 -050080 if (mScreenSaverPreference != null
81 && getResources().getBoolean(
John Spurlockd983e1d2012-10-18 16:41:09 -040082 com.android.internal.R.bool.config_dreamsSupported) == false) {
Daniel Sandler47c991f2012-02-01 14:14:18 -050083 getPreferenceScreen().removePreference(mScreenSaverPreference);
84 }
Amith Yamasani78b82652013-08-16 16:35:54 -070085
Jim Miller9391b952011-01-07 16:55:33 -080086 mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
87 final long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT,
88 FALLBACK_SCREEN_TIMEOUT_VALUE);
89 mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
90 mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
91 disableUnusableTimeouts(mScreenTimeoutPreference);
Gilles Debunne698c24f2011-09-02 15:47:24 -070092 updateTimeoutPreferenceDescription(currentTimeout);
Daniel Sandler2943cfa2011-04-15 11:18:57 -040093
Amith Yamasani9627a8e2012-09-23 12:54:14 -070094 mFontSizePref = (WarnedListPreference) findPreference(KEY_FONT_SIZE);
Dianne Hackborn6677e562011-03-04 17:30:54 -080095 mFontSizePref.setOnPreferenceChangeListener(this);
Amith Yamasani9627a8e2012-09-23 12:54:14 -070096 mFontSizePref.setOnPreferenceClickListener(this);
Adrian Roos244e8ce2014-05-13 17:00:46 +020097
Adrian Roos8ab09542014-06-02 20:33:56 +020098 if (isAutomaticBrightnessAvailable(getResources())) {
Adrian Roos244e8ce2014-05-13 17:00:46 +020099 mAutoBrightnessPreference = (CheckBoxPreference) findPreference(KEY_AUTO_BRIGHTNESS);
100 mAutoBrightnessPreference.setOnPreferenceChangeListener(this);
101 } else {
102 removePreference(KEY_AUTO_BRIGHTNESS);
103 }
Jim Miller9391b952011-01-07 16:55:33 -0800104 }
105
Adrian Roos8ab09542014-06-02 20:33:56 +0200106 private static boolean isAutomaticBrightnessAvailable(Resources res) {
107 return res.getBoolean(com.android.internal.R.bool.config_automatic_brightness_available);
108 }
109
Gilles Debunne698c24f2011-09-02 15:47:24 -0700110 private void updateTimeoutPreferenceDescription(long currentTimeout) {
111 ListPreference preference = mScreenTimeoutPreference;
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400112 String summary;
Gilles Debunne698c24f2011-09-02 15:47:24 -0700113 if (currentTimeout < 0) {
114 // Unsupported value
115 summary = "";
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400116 } else {
Gilles Debunne698c24f2011-09-02 15:47:24 -0700117 final CharSequence[] entries = preference.getEntries();
118 final CharSequence[] values = preference.getEntryValues();
Amith Yamasani64516a12012-10-22 16:38:02 -0700119 if (entries == null || entries.length == 0) {
120 summary = "";
121 } else {
122 int best = 0;
123 for (int i = 0; i < values.length; i++) {
124 long timeout = Long.parseLong(values[i].toString());
125 if (currentTimeout >= timeout) {
126 best = i;
127 }
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400128 }
Amith Yamasani64516a12012-10-22 16:38:02 -0700129 summary = preference.getContext().getString(R.string.screen_timeout_summary,
130 entries[best]);
Jim Miller9391b952011-01-07 16:55:33 -0800131 }
132 }
Gilles Debunne698c24f2011-09-02 15:47:24 -0700133 preference.setSummary(summary);
Jim Millerd90b5a82010-03-29 20:18:51 -0700134 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800135
Jim Millerd90b5a82010-03-29 20:18:51 -0700136 private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) {
Jim Millerfc5a0222010-04-13 15:31:41 -0700137 final DevicePolicyManager dpm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700138 (DevicePolicyManager) getActivity().getSystemService(
139 Context.DEVICE_POLICY_SERVICE);
Jim Millerfc5a0222010-04-13 15:31:41 -0700140 final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0;
Jim Millerd90b5a82010-03-29 20:18:51 -0700141 if (maxTimeout == 0) {
142 return; // policy not enforced
143 }
144 final CharSequence[] entries = screenTimeoutPreference.getEntries();
145 final CharSequence[] values = screenTimeoutPreference.getEntryValues();
146 ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
147 ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
148 for (int i = 0; i < values.length; i++) {
Gilles Debunne698c24f2011-09-02 15:47:24 -0700149 long timeout = Long.parseLong(values[i].toString());
Jim Millerd90b5a82010-03-29 20:18:51 -0700150 if (timeout <= maxTimeout) {
151 revisedEntries.add(entries[i]);
152 revisedValues.add(values[i]);
153 }
154 }
155 if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) {
Amith Yamasani78b82652013-08-16 16:35:54 -0700156 final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue());
Jim Millerd90b5a82010-03-29 20:18:51 -0700157 screenTimeoutPreference.setEntries(
158 revisedEntries.toArray(new CharSequence[revisedEntries.size()]));
159 screenTimeoutPreference.setEntryValues(
160 revisedValues.toArray(new CharSequence[revisedValues.size()]));
Jim Millerfc5a0222010-04-13 15:31:41 -0700161 if (userPreference <= maxTimeout) {
162 screenTimeoutPreference.setValue(String.valueOf(userPreference));
Amith Yamasani78b82652013-08-16 16:35:54 -0700163 } else if (revisedValues.size() > 0
164 && Long.parseLong(revisedValues.get(revisedValues.size() - 1).toString())
165 == maxTimeout) {
166 // If the last one happens to be the same as the max timeout, select that
167 screenTimeoutPreference.setValue(String.valueOf(maxTimeout));
Jim Millerfc5a0222010-04-13 15:31:41 -0700168 } else {
169 // There will be no highlighted selection since nothing in the list matches
170 // maxTimeout. The user can still select anything less than maxTimeout.
171 // TODO: maybe append maxTimeout to the list and mark selected.
172 }
Jim Millerd90b5a82010-03-29 20:18:51 -0700173 }
174 screenTimeoutPreference.setEnabled(revisedEntries.size() > 0);
Amith Yamasani992f1022010-01-25 09:17:53 -0800175 }
176
Christopher Tate8a510c52011-07-29 16:16:22 -0700177 int floatToIndex(float val) {
178 String[] indices = getResources().getStringArray(R.array.entryvalues_font_size);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800179 float lastVal = Float.parseFloat(indices[0]);
180 for (int i=1; i<indices.length; i++) {
181 float thisVal = Float.parseFloat(indices[i]);
182 if (val < (lastVal + (thisVal-lastVal)*.5f)) {
183 return i-1;
184 }
185 lastVal = thisVal;
186 }
187 return indices.length-1;
188 }
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900189
Dianne Hackborn6677e562011-03-04 17:30:54 -0800190 public void readFontSizePreference(ListPreference pref) {
191 try {
Gilles Debunne737c81c2011-07-08 09:40:00 -0700192 mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
Dianne Hackborn6677e562011-03-04 17:30:54 -0800193 } catch (RemoteException e) {
Gilles Debunne737c81c2011-07-08 09:40:00 -0700194 Log.w(TAG, "Unable to retrieve font size");
Dianne Hackborn6677e562011-03-04 17:30:54 -0800195 }
Christopher Tate8a510c52011-07-29 16:16:22 -0700196
197 // mark the appropriate item in the preferences list
198 int index = floatToIndex(mCurConfig.fontScale);
199 pref.setValueIndex(index);
200
201 // report the current size in the summary text
202 final Resources res = getResources();
203 String[] fontSizeNames = res.getStringArray(R.array.entries_font_size);
204 pref.setSummary(String.format(res.getString(R.string.summary_font_size),
205 fontSizeNames[index]));
Dianne Hackborn6677e562011-03-04 17:30:54 -0800206 }
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900207
Amith Yamasani992f1022010-01-25 09:17:53 -0800208 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700209 public void onResume() {
Amith Yamasani992f1022010-01-25 09:17:53 -0800210 super.onResume();
Jeff Brown9e143f52012-09-19 20:46:07 -0700211 updateState();
Amith Yamasani26a1f892011-02-28 17:24:31 -0800212 }
213
214 @Override
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700215 public Dialog onCreateDialog(int dialogId) {
216 if (dialogId == DLG_GLOBAL_CHANGE_WARNING) {
217 return Utils.buildGlobalChangeWarningDialog(getActivity(),
218 R.string.global_font_change_title,
219 new Runnable() {
220 public void run() {
221 mFontSizePref.click();
222 }
223 });
224 }
225 return null;
226 }
227
Gilles Debunne737c81c2011-07-08 09:40:00 -0700228 private void updateState() {
Dianne Hackborn6677e562011-03-04 17:30:54 -0800229 readFontSizePreference(mFontSizePref);
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800230 updateScreenSaverSummary();
Adrian Roos244e8ce2014-05-13 17:00:46 +0200231
232 // Update auto brightness if it is available.
233 if (mAutoBrightnessPreference != null) {
234 int brightnessMode = Settings.System.getInt(getContentResolver(),
235 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
236 mAutoBrightnessPreference.setChecked(brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL);
237 }
Daniel Sandler6f0ea442011-11-17 10:44:51 -0800238 }
239
240 private void updateScreenSaverSummary() {
Daniel Sandlerc825bc82012-10-18 10:33:59 -0400241 if (mScreenSaverPreference != null) {
242 mScreenSaverPreference.setSummary(
243 DreamSettings.getSummaryTextWithDreamName(getActivity()));
244 }
Amith Yamasani26a1f892011-02-28 17:24:31 -0800245 }
246
Dianne Hackborn6677e562011-03-04 17:30:54 -0800247 public void writeFontSizePreference(Object objValue) {
248 try {
249 mCurConfig.fontScale = Float.parseFloat(objValue.toString());
Dianne Hackborn92c8eba2011-07-19 14:59:01 -0700250 ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
Dianne Hackborn6677e562011-03-04 17:30:54 -0800251 } catch (RemoteException e) {
Gilles Debunne737c81c2011-07-08 09:40:00 -0700252 Log.w(TAG, "Unable to save font size");
Dianne Hackborn6677e562011-03-04 17:30:54 -0800253 }
254 }
Amith Yamasani883d8502011-09-20 14:41:03 -0700255
Amith Yamasani992f1022010-01-25 09:17:53 -0800256 @Override
257 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Daniel Sandler2943cfa2011-04-15 11:18:57 -0400258 return super.onPreferenceTreeClick(preferenceScreen, preference);
Amith Yamasani992f1022010-01-25 09:17:53 -0800259 }
260
Jeff Brown4e54c2a2013-11-10 21:02:20 -0800261 @Override
Amith Yamasani992f1022010-01-25 09:17:53 -0800262 public boolean onPreferenceChange(Preference preference, Object objValue) {
263 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800264 if (KEY_SCREEN_TIMEOUT.equals(key)) {
Amith Yamasani992f1022010-01-25 09:17:53 -0800265 try {
Shuhrat Dehkanov7e776992013-11-01 17:39:44 +0900266 int value = Integer.parseInt((String) objValue);
Gilles Debunne698c24f2011-09-02 15:47:24 -0700267 Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
268 updateTimeoutPreferenceDescription(value);
Amith Yamasani992f1022010-01-25 09:17:53 -0800269 } catch (NumberFormatException e) {
270 Log.e(TAG, "could not persist screen timeout setting", e);
271 }
272 }
Dianne Hackborn6677e562011-03-04 17:30:54 -0800273 if (KEY_FONT_SIZE.equals(key)) {
274 writeFontSizePreference(objValue);
275 }
Adrian Roos244e8ce2014-05-13 17:00:46 +0200276 if (preference == mAutoBrightnessPreference) {
277 boolean auto = (Boolean) objValue;
278 Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE,
279 auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
280 }
Amith Yamasani992f1022010-01-25 09:17:53 -0800281 return true;
282 }
Jeff Brown9e143f52012-09-19 20:46:07 -0700283
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700284 @Override
285 public boolean onPreferenceClick(Preference preference) {
286 if (preference == mFontSizePref) {
287 if (Utils.hasMultipleUsers(getActivity())) {
288 showDialog(DLG_GLOBAL_CHANGE_WARNING);
289 return true;
290 } else {
291 mFontSizePref.click();
292 }
293 }
294 return false;
295 }
Adrian Roos8ab09542014-06-02 20:33:56 +0200296
297 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
298 new BaseSearchIndexProvider() {
299 @Override
300 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
301 boolean enabled) {
302 ArrayList<SearchIndexableResource> result = new ArrayList<>(1);
303
304 SearchIndexableResource sir = new SearchIndexableResource(context);
305 sir.xmlResId = R.xml.display_settings;
306 result.add(sir);
307
308 return result;
309 }
310
311 @Override
312 public List<String> getNonIndexableKeys(Context context) {
313 ArrayList<String> nonIndexableKeys = new ArrayList<>(1);
314 if (!isAutomaticBrightnessAvailable(context.getResources())) {
315 nonIndexableKeys.add(KEY_AUTO_BRIGHTNESS);
316 }
317 return nonIndexableKeys;
318 }
319 };
Amith Yamasani992f1022010-01-25 09:17:53 -0800320}