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