blob: 5945884af1ba38bd403e8eeab85a08c36f3b0b0a [file] [log] [blame]
Amith Yamasani992f1022010-01-25 09:17:53 -08001/*
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 static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
20
Jim Millerd90b5a82010-03-29 20:18:51 -070021import android.app.admin.DevicePolicyManager;
Amith Yamasani992f1022010-01-25 09:17:53 -080022import android.content.ContentResolver;
23import android.content.Context;
Amith Yamasani992f1022010-01-25 09:17:53 -080024import android.os.Bundle;
Amith Yamasani992f1022010-01-25 09:17:53 -080025import android.os.RemoteException;
26import android.os.ServiceManager;
27import android.preference.CheckBoxPreference;
28import android.preference.ListPreference;
29import android.preference.Preference;
Amith Yamasani992f1022010-01-25 09:17:53 -080030import android.preference.PreferenceScreen;
31import android.provider.Settings;
Amith Yamasani992f1022010-01-25 09:17:53 -080032import android.util.Log;
33import android.view.IWindowManager;
34
Amith Yamasanid7993472010-08-18 13:59:28 -070035import java.util.ArrayList;
36
37public class DisplaySettings extends SettingsPreferenceFragment implements
Amith Yamasani992f1022010-01-25 09:17:53 -080038 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 Yamasanid7993472010-08-18 13:59:28 -070055 public void onCreate(Bundle savedInstanceState) {
Amith Yamasani992f1022010-01-25 09:17:53 -080056 super.onCreate(savedInstanceState);
Amith Yamasanid7993472010-08-18 13:59:28 -070057 ContentResolver resolver = getActivity().getContentResolver();
Amith Yamasani992f1022010-01-25 09:17:53 -080058 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 Millerd90b5a82010-03-29 20:18:51 -070072 disableUnusableTimeouts(screenTimeoutPreference);
73 }
Amith Yamasani992f1022010-01-25 09:17:53 -080074
Jim Millerd90b5a82010-03-29 20:18:51 -070075 private void disableUnusableTimeouts(ListPreference screenTimeoutPreference) {
Jim Millerfc5a0222010-04-13 15:31:41 -070076 final DevicePolicyManager dpm =
Amith Yamasanid7993472010-08-18 13:59:28 -070077 (DevicePolicyManager) getActivity().getSystemService(
78 Context.DEVICE_POLICY_SERVICE);
Jim Millerfc5a0222010-04-13 15:31:41 -070079 final long maxTimeout = dpm != null ? dpm.getMaximumTimeToLock(null) : 0;
Jim Millerd90b5a82010-03-29 20:18:51 -070080 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 Millerfc5a0222010-04-13 15:31:41 -070099 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 Millerd90b5a82010-03-29 20:18:51 -0700107 }
108 screenTimeoutPreference.setEnabled(revisedEntries.size() > 0);
Amith Yamasani992f1022010-01-25 09:17:53 -0800109 }
110
111 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700112 public void onResume() {
Amith Yamasani992f1022010-01-25 09:17:53 -0800113 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}