• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
20 
21 import android.app.ActivityManagerNative;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.content.res.Configuration;
26 import android.content.res.Resources;
27 import android.os.Bundle;
28 import android.os.RemoteException;
29 import android.preference.CheckBoxPreference;
30 import android.preference.ListPreference;
31 import android.preference.Preference;
32 import android.preference.PreferenceScreen;
33 import android.provider.Settings;
34 import android.provider.Settings.SettingNotFoundException;
35 import android.util.Log;
36 
37 import com.android.internal.view.RotationPolicy;
38 import com.android.settings.DreamSettings;
39 
40 import java.util.ArrayList;
41 
42 public class DisplaySettings extends SettingsPreferenceFragment implements
43         Preference.OnPreferenceChangeListener {
44     private static final String TAG = "DisplaySettings";
45 
46     /** If there is no setting in the provider, use this. */
47     private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
48 
49     private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
50     private static final String KEY_ACCELEROMETER = "accelerometer";
51     private static final String KEY_FONT_SIZE = "font_size";
52     private static final String KEY_NOTIFICATION_PULSE = "notification_pulse";
53     private static final String KEY_SCREEN_SAVER = "screensaver";
54 
55     private CheckBoxPreference mAccelerometer;
56     private ListPreference mFontSizePref;
57     private CheckBoxPreference mNotificationPulse;
58 
59     private final Configuration mCurConfig = new Configuration();
60 
61     private ListPreference mScreenTimeoutPreference;
62     private Preference mScreenSaverPreference;
63 
64     private final RotationPolicy.RotationPolicyListener mRotationPolicyListener =
65             new RotationPolicy.RotationPolicyListener() {
66         @Override
67         public void onChange() {
68             updateAccelerometerRotationCheckbox();
69         }
70     };
71 
72     @Override
onCreate(Bundle savedInstanceState)73     public void onCreate(Bundle savedInstanceState) {
74         super.onCreate(savedInstanceState);
75         ContentResolver resolver = getActivity().getContentResolver();
76 
77         addPreferencesFromResource(R.xml.display_settings);
78 
79         mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);
80         mAccelerometer.setPersistent(false);
81         if (RotationPolicy.isRotationLockToggleSupported(getActivity())) {
82             // If rotation lock is supported, then we do not provide this option in
83             // Display settings.  However, is still available in Accessibility settings.
84             getPreferenceScreen().removePreference(mAccelerometer);
85         }
86 
87         mScreenSaverPreference = findPreference(KEY_SCREEN_SAVER);
88         if (mScreenSaverPreference != null
89                 && getResources().getBoolean(
90                         com.android.internal.R.bool.config_enableDreams) == false) {
91             getPreferenceScreen().removePreference(mScreenSaverPreference);
92         }
93 
94         mScreenTimeoutPreference = (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
95         final long currentTimeout = Settings.System.getLong(resolver, SCREEN_OFF_TIMEOUT,
96                 FALLBACK_SCREEN_TIMEOUT_VALUE);
97         mScreenTimeoutPreference.setValue(String.valueOf(currentTimeout));
98         mScreenTimeoutPreference.setOnPreferenceChangeListener(this);
99         disableUnusableTimeouts(mScreenTimeoutPreference);
100         updateTimeoutPreferenceDescription(currentTimeout);
101 
102         mFontSizePref = (ListPreference) findPreference(KEY_FONT_SIZE);
103         mFontSizePref.setOnPreferenceChangeListener(this);
104         mNotificationPulse = (CheckBoxPreference) findPreference(KEY_NOTIFICATION_PULSE);
105         if (mNotificationPulse != null
106                 && getResources().getBoolean(
107                         com.android.internal.R.bool.config_intrusiveNotificationLed) == false) {
108             getPreferenceScreen().removePreference(mNotificationPulse);
109         } else {
110             try {
111                 mNotificationPulse.setChecked(Settings.System.getInt(resolver,
112                         Settings.System.NOTIFICATION_LIGHT_PULSE) == 1);
113                 mNotificationPulse.setOnPreferenceChangeListener(this);
114             } catch (SettingNotFoundException snfe) {
115                 Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found");
116             }
117         }
118 
119     }
120 
updateTimeoutPreferenceDescription(long currentTimeout)121     private void updateTimeoutPreferenceDescription(long currentTimeout) {
122         ListPreference preference = mScreenTimeoutPreference;
123         String summary;
124         if (currentTimeout < 0) {
125             // Unsupported value
126             summary = "";
127         } else {
128             final CharSequence[] entries = preference.getEntries();
129             final CharSequence[] values = preference.getEntryValues();
130             int best = 0;
131             for (int i = 0; i < values.length; i++) {
132                 long timeout = Long.parseLong(values[i].toString());
133                 if (currentTimeout >= timeout) {
134                     best = i;
135                 }
136             }
137             summary = preference.getContext().getString(R.string.screen_timeout_summary,
138                     entries[best]);
139         }
140         preference.setSummary(summary);
141     }
142 
disableUnusableTimeouts(ListPreference screenTimeoutPreference)143     private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) {
144         final DevicePolicyManager dpm =
145                 (DevicePolicyManager) getActivity().getSystemService(
146                 Context.DEVICE_POLICY_SERVICE);
147         final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0;
148         if (maxTimeout == 0) {
149             return; // policy not enforced
150         }
151         final CharSequence[] entries = screenTimeoutPreference.getEntries();
152         final CharSequence[] values = screenTimeoutPreference.getEntryValues();
153         ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
154         ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
155         for (int i = 0; i < values.length; i++) {
156             long timeout = Long.parseLong(values[i].toString());
157             if (timeout <= maxTimeout) {
158                 revisedEntries.add(entries[i]);
159                 revisedValues.add(values[i]);
160             }
161         }
162         if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) {
163             screenTimeoutPreference.setEntries(
164                     revisedEntries.toArray(new CharSequence[revisedEntries.size()]));
165             screenTimeoutPreference.setEntryValues(
166                     revisedValues.toArray(new CharSequence[revisedValues.size()]));
167             final int userPreference = Integer.parseInt(screenTimeoutPreference.getValue());
168             if (userPreference <= maxTimeout) {
169                 screenTimeoutPreference.setValue(String.valueOf(userPreference));
170             } else {
171                 // There will be no highlighted selection since nothing in the list matches
172                 // maxTimeout. The user can still select anything less than maxTimeout.
173                 // TODO: maybe append maxTimeout to the list and mark selected.
174             }
175         }
176         screenTimeoutPreference.setEnabled(revisedEntries.size() > 0);
177     }
178 
floatToIndex(float val)179     int floatToIndex(float val) {
180         String[] indices = getResources().getStringArray(R.array.entryvalues_font_size);
181         float lastVal = Float.parseFloat(indices[0]);
182         for (int i=1; i<indices.length; i++) {
183             float thisVal = Float.parseFloat(indices[i]);
184             if (val < (lastVal + (thisVal-lastVal)*.5f)) {
185                 return i-1;
186             }
187             lastVal = thisVal;
188         }
189         return indices.length-1;
190     }
191 
readFontSizePreference(ListPreference pref)192     public void readFontSizePreference(ListPreference pref) {
193         try {
194             mCurConfig.updateFrom(ActivityManagerNative.getDefault().getConfiguration());
195         } catch (RemoteException e) {
196             Log.w(TAG, "Unable to retrieve font size");
197         }
198 
199         // mark the appropriate item in the preferences list
200         int index = floatToIndex(mCurConfig.fontScale);
201         pref.setValueIndex(index);
202 
203         // report the current size in the summary text
204         final Resources res = getResources();
205         String[] fontSizeNames = res.getStringArray(R.array.entries_font_size);
206         pref.setSummary(String.format(res.getString(R.string.summary_font_size),
207                 fontSizeNames[index]));
208     }
209 
210     @Override
onResume()211     public void onResume() {
212         super.onResume();
213 
214         updateState();
215 
216         RotationPolicy.registerRotationPolicyListener(getActivity(),
217                 mRotationPolicyListener);
218     }
219 
220     @Override
onPause()221     public void onPause() {
222         super.onPause();
223 
224         RotationPolicy.unregisterRotationPolicyListener(getActivity(),
225                 mRotationPolicyListener);
226     }
227 
updateState()228     private void updateState() {
229         updateAccelerometerRotationCheckbox();
230         readFontSizePreference(mFontSizePref);
231         updateScreenSaverSummary();
232     }
233 
updateScreenSaverSummary()234     private void updateScreenSaverSummary() {
235         mScreenSaverPreference.setSummary(
236             DreamSettings.isScreenSaverEnabled(mScreenSaverPreference.getContext())
237                 ? R.string.screensaver_settings_summary_on
238                 : R.string.screensaver_settings_summary_off);
239     }
240 
updateAccelerometerRotationCheckbox()241     private void updateAccelerometerRotationCheckbox() {
242         if (getActivity() == null) return;
243 
244         mAccelerometer.setChecked(!RotationPolicy.isRotationLocked(getActivity()));
245     }
246 
writeFontSizePreference(Object objValue)247     public void writeFontSizePreference(Object objValue) {
248         try {
249             mCurConfig.fontScale = Float.parseFloat(objValue.toString());
250             ActivityManagerNative.getDefault().updatePersistentConfiguration(mCurConfig);
251         } catch (RemoteException e) {
252             Log.w(TAG, "Unable to save font size");
253         }
254     }
255 
256     @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)257     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
258         if (preference == mAccelerometer) {
259             RotationPolicy.setRotationLockForAccessibility(
260                     getActivity(), !mAccelerometer.isChecked());
261         } else if (preference == mNotificationPulse) {
262             boolean value = mNotificationPulse.isChecked();
263             Settings.System.putInt(getContentResolver(), Settings.System.NOTIFICATION_LIGHT_PULSE,
264                     value ? 1 : 0);
265             return true;
266         }
267         return super.onPreferenceTreeClick(preferenceScreen, preference);
268     }
269 
onPreferenceChange(Preference preference, Object objValue)270     public boolean onPreferenceChange(Preference preference, Object objValue) {
271         final String key = preference.getKey();
272         if (KEY_SCREEN_TIMEOUT.equals(key)) {
273             int value = Integer.parseInt((String) objValue);
274             try {
275                 Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, value);
276                 updateTimeoutPreferenceDescription(value);
277             } catch (NumberFormatException e) {
278                 Log.e(TAG, "could not persist screen timeout setting", e);
279             }
280         }
281         if (KEY_FONT_SIZE.equals(key)) {
282             writeFontSizePreference(objValue);
283         }
284 
285         return true;
286     }
287 }
288