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