Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 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 | |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 21 | import android.app.admin.DevicePolicyManager; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 24 | import android.os.Bundle; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | import android.os.ServiceManager; |
| 27 | import android.preference.CheckBoxPreference; |
| 28 | import android.preference.ListPreference; |
| 29 | import android.preference.Preference; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 30 | import android.preference.PreferenceScreen; |
| 31 | import android.provider.Settings; |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | import android.view.IWindowManager; |
| 34 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame^] | 35 | import java.util.ArrayList; |
| 36 | |
| 37 | public class DisplaySettings extends SettingsPreferenceFragment implements |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 38 | Preference.OnPreferenceChangeListener { |
| 39 | private static final String TAG = "DisplaySettings"; |
| 40 | |
| 41 | /** If there is no setting in the provider, use this. */ |
| 42 | private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000; |
| 43 | |
| 44 | private static final String KEY_SCREEN_TIMEOUT = "screen_timeout"; |
| 45 | private static final String KEY_ANIMATIONS = "animations"; |
| 46 | private static final String KEY_ACCELEROMETER = "accelerometer"; |
| 47 | |
| 48 | private ListPreference mAnimations; |
| 49 | private CheckBoxPreference mAccelerometer; |
| 50 | private float[] mAnimationScales; |
| 51 | |
| 52 | private IWindowManager mWindowManager; |
| 53 | |
| 54 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame^] | 55 | public void onCreate(Bundle savedInstanceState) { |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 56 | super.onCreate(savedInstanceState); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame^] | 57 | ContentResolver resolver = getActivity().getContentResolver(); |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 58 | mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window")); |
| 59 | |
| 60 | addPreferencesFromResource(R.xml.display_settings); |
| 61 | |
| 62 | mAnimations = (ListPreference) findPreference(KEY_ANIMATIONS); |
| 63 | mAnimations.setOnPreferenceChangeListener(this); |
| 64 | mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER); |
| 65 | mAccelerometer.setPersistent(false); |
| 66 | |
| 67 | ListPreference screenTimeoutPreference = |
| 68 | (ListPreference) findPreference(KEY_SCREEN_TIMEOUT); |
| 69 | screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt( |
| 70 | resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE))); |
| 71 | screenTimeoutPreference.setOnPreferenceChangeListener(this); |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 72 | disableUnusableTimeouts(screenTimeoutPreference); |
| 73 | } |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 74 | |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 75 | private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) { |
Jim Miller | fc5a022 | 2010-04-13 15:31:41 -0700 | [diff] [blame] | 76 | final DevicePolicyManager dpm = |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame^] | 77 | (DevicePolicyManager) getActivity().getSystemService( |
| 78 | Context.DEVICE_POLICY_SERVICE); |
Jim Miller | fc5a022 | 2010-04-13 15:31:41 -0700 | [diff] [blame] | 79 | final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0; |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 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()])); |
Jim Miller | fc5a022 | 2010-04-13 15:31:41 -0700 | [diff] [blame] | 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 | } |
Jim Miller | d90b5a8 | 2010-03-29 20:18:51 -0700 | [diff] [blame] | 107 | } |
| 108 | screenTimeoutPreference.setEnabled(revisedEntries.size() > 0); |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame^] | 112 | public void onResume() { |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 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 | } |