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