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 java.util.ArrayList; 22 23 import android.app.admin.DevicePolicyManager; 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.os.Bundle; 27 import android.os.RemoteException; 28 import android.os.ServiceManager; 29 import android.preference.CheckBoxPreference; 30 import android.preference.ListPreference; 31 import android.preference.Preference; 32 import android.preference.PreferenceActivity; 33 import android.preference.PreferenceScreen; 34 import android.provider.Settings; 35 import android.util.Log; 36 import android.view.IWindowManager; 37 38 public class DisplaySettings extends PreferenceActivity implements 39 Preference.OnPreferenceChangeListener { 40 private static final String TAG = "DisplaySettings"; 41 42 /** If there is no setting in the provider, use this. */ 43 private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000; 44 45 private static final String KEY_SCREEN_TIMEOUT = "screen_timeout"; 46 private static final String KEY_ANIMATIONS = "animations"; 47 private static final String KEY_ACCELEROMETER = "accelerometer"; 48 49 private ListPreference mAnimations; 50 private CheckBoxPreference mAccelerometer; 51 private float[] mAnimationScales; 52 53 private IWindowManager mWindowManager; 54 55 @Override 56 protected void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 ContentResolver resolver = getContentResolver(); 59 mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window")); 60 61 addPreferencesFromResource(R.xml.display_settings); 62 63 mAnimations = (ListPreference) findPreference(KEY_ANIMATIONS); 64 mAnimations.setOnPreferenceChangeListener(this); 65 mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER); 66 mAccelerometer.setPersistent(false); 67 68 ListPreference screenTimeoutPreference = 69 (ListPreference) findPreference(KEY_SCREEN_TIMEOUT); 70 screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt( 71 resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE))); 72 screenTimeoutPreference.setOnPreferenceChangeListener(this); 73 disableUnusableTimeouts(screenTimeoutPreference); 74 } 75 76 private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) { 77 final DevicePolicyManager dpm = 78 (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); 79 final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0; 80 if (maxTimeout == 0) { 81 return; // policy not enforced 82 } 83 final CharSequence[] entries = screenTimeoutPreference.getEntries(); 84 final CharSequence[] values = screenTimeoutPreference.getEntryValues(); 85 ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>(); 86 ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>(); 87 for (int i = 0; i < values.length; i++) { 88 long timeout = Long.valueOf(values[i].toString()); 89 if (timeout <= maxTimeout) { 90 revisedEntries.add(entries[i]); 91 revisedValues.add(values[i]); 92 } 93 } 94 if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) { 95 screenTimeoutPreference.setEntries( 96 revisedEntries.toArray(new CharSequence[revisedEntries.size()])); 97 screenTimeoutPreference.setEntryValues( 98 revisedValues.toArray(new CharSequence[revisedValues.size()])); 99 final int userPreference = Integer.valueOf(screenTimeoutPreference.getValue()); 100 if (userPreference <= maxTimeout) { 101 screenTimeoutPreference.setValue(String.valueOf(userPreference)); 102 } else { 103 // There will be no highlighted selection since nothing in the list matches 104 // maxTimeout. The user can still select anything less than maxTimeout. 105 // TODO: maybe append maxTimeout to the list and mark selected. 106 } 107 } 108 screenTimeoutPreference.setEnabled(revisedEntries.size() > 0); 109 } 110 111 @Override 112 protected void onResume() { 113 super.onResume(); 114 115 updateState(true); 116 } 117 118 private void updateState(boolean force) { 119 int animations = 0; 120 try { 121 mAnimationScales = mWindowManager.getAnimationScales(); 122 } catch (RemoteException e) { 123 } 124 if (mAnimationScales != null) { 125 if (mAnimationScales.length >= 1) { 126 animations = ((int)(mAnimationScales[0]+.5f)) % 10; 127 } 128 if (mAnimationScales.length >= 2) { 129 animations += (((int)(mAnimationScales[1]+.5f)) & 0x7) * 10; 130 } 131 } 132 int idx = 0; 133 int best = 0; 134 CharSequence[] aents = mAnimations.getEntryValues(); 135 for (int i=0; i<aents.length; i++) { 136 int val = Integer.parseInt(aents[i].toString()); 137 if (val <= animations && val > best) { 138 best = val; 139 idx = i; 140 } 141 } 142 mAnimations.setValueIndex(idx); 143 updateAnimationsSummary(mAnimations.getValue()); 144 mAccelerometer.setChecked(Settings.System.getInt( 145 getContentResolver(), 146 Settings.System.ACCELEROMETER_ROTATION, 0) != 0); 147 } 148 149 private void updateAnimationsSummary(Object value) { 150 CharSequence[] summaries = getResources().getTextArray(R.array.animations_summaries); 151 CharSequence[] values = mAnimations.getEntryValues(); 152 for (int i=0; i<values.length; i++) { 153 //Log.i("foo", "Comparing entry "+ values[i] + " to current " 154 // + mAnimations.getValue()); 155 if (values[i].equals(value)) { 156 mAnimations.setSummary(summaries[i]); 157 break; 158 } 159 } 160 } 161 162 @Override 163 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 164 if (preference == mAccelerometer) { 165 Settings.System.putInt(getContentResolver(), 166 Settings.System.ACCELEROMETER_ROTATION, 167 mAccelerometer.isChecked() ? 1 : 0); 168 } 169 return true; 170 } 171 172 public boolean onPreferenceChange(Preference preference, Object objValue) { 173 final String key = preference.getKey(); 174 if (KEY_ANIMATIONS.equals(key)) { 175 try { 176 int value = Integer.parseInt((String) objValue); 177 if (mAnimationScales.length >= 1) { 178 mAnimationScales[0] = value%10; 179 } 180 if (mAnimationScales.length >= 2) { 181 mAnimationScales[1] = (value/10)%10; 182 } 183 try { 184 mWindowManager.setAnimationScales(mAnimationScales); 185 } catch (RemoteException e) { 186 } 187 updateAnimationsSummary(objValue); 188 } catch (NumberFormatException e) { 189 Log.e(TAG, "could not persist animation setting", e); 190 } 191 192 } 193 if (KEY_SCREEN_TIMEOUT.equals(key)) { 194 int value = Integer.parseInt((String) objValue); 195 try { 196 Settings.System.putInt(getContentResolver(), 197 SCREEN_OFF_TIMEOUT, value); 198 } catch (NumberFormatException e) { 199 Log.e(TAG, "could not persist screen timeout setting", e); 200 } 201 } 202 203 return true; 204 } 205 } 206