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