OSDN Git Service

bdbec978fd5f2c5eb1c38946c47cd308cb845b97
[android-x86/packages-apps-Settings.git] / src / com / android / settings / DevelopmentSettings.java
1 /*
2  * Copyright (C) 2008 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 android.app.ActivityManagerNative;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.backup.IBackupManager;
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.os.BatteryManager;
28 import android.os.Build;
29 import android.os.Bundle;
30 import android.os.IBinder;
31 import android.os.Parcel;
32 import android.os.RemoteException;
33 import android.os.ServiceManager;
34 import android.os.StrictMode;
35 import android.os.SystemProperties;
36 import android.preference.CheckBoxPreference;
37 import android.preference.ListPreference;
38 import android.preference.Preference;
39 import android.preference.PreferenceFragment;
40 import android.preference.PreferenceScreen;
41 import android.preference.Preference.OnPreferenceChangeListener;
42 import android.provider.Settings;
43 import android.text.TextUtils;
44 import android.view.IWindowManager;
45
46 /*
47  * Displays preferences for application developers.
48  */
49 public class DevelopmentSettings extends PreferenceFragment
50         implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
51                 OnPreferenceChangeListener {
52
53     private static final String ENABLE_ADB = "enable_adb";
54     private static final String KEEP_SCREEN_ON = "keep_screen_on";
55     private static final String ALLOW_MOCK_LOCATION = "allow_mock_location";
56     private static final String HDCP_CHECKING_KEY = "hdcp_checking";
57     private static final String HDCP_CHECKING_PROPERTY = "persist.sys.hdcp_checking";
58     private static final String LOCAL_BACKUP_PASSWORD = "local_backup_password";
59
60     private static final String STRICT_MODE_KEY = "strict_mode";
61     private static final String POINTER_LOCATION_KEY = "pointer_location";
62     private static final String SHOW_TOUCHES_KEY = "show_touches";
63     private static final String SHOW_SCREEN_UPDATES_KEY = "show_screen_updates";
64     private static final String SHOW_CPU_USAGE_KEY = "show_cpu_usage";
65     private static final String WINDOW_ANIMATION_SCALE_KEY = "window_animation_scale";
66     private static final String TRANSITION_ANIMATION_SCALE_KEY = "transition_animation_scale";
67
68     private static final String IMMEDIATELY_DESTROY_ACTIVITIES_KEY
69             = "immediately_destroy_activities";
70     private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit";
71
72     private static final String SHOW_ALL_ANRS_KEY = "show_all_anrs";
73
74     private IWindowManager mWindowManager;
75     private IBackupManager mBackupManager;
76
77     private CheckBoxPreference mEnableAdb;
78     private CheckBoxPreference mKeepScreenOn;
79     private CheckBoxPreference mAllowMockLocation;
80     private PreferenceScreen mPassword;
81
82     private CheckBoxPreference mStrictMode;
83     private CheckBoxPreference mPointerLocation;
84     private CheckBoxPreference mShowTouches;
85     private CheckBoxPreference mShowScreenUpdates;
86     private CheckBoxPreference mShowCpuUsage;
87     private ListPreference mWindowAnimationScale;
88     private ListPreference mTransitionAnimationScale;
89
90     private CheckBoxPreference mImmediatelyDestroyActivities;
91     private ListPreference mAppProcessLimit;
92
93     private CheckBoxPreference mShowAllANRs;
94
95     // To track whether Yes was clicked in the adb warning dialog
96     private boolean mOkClicked;
97
98     private Dialog mOkDialog;
99
100     @Override
101     public void onCreate(Bundle icicle) {
102         super.onCreate(icicle);
103
104         mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
105         mBackupManager = IBackupManager.Stub.asInterface(
106                 ServiceManager.getService(Context.BACKUP_SERVICE));
107
108         addPreferencesFromResource(R.xml.development_prefs);
109
110         mEnableAdb = (CheckBoxPreference) findPreference(ENABLE_ADB);
111         mKeepScreenOn = (CheckBoxPreference) findPreference(KEEP_SCREEN_ON);
112         mAllowMockLocation = (CheckBoxPreference) findPreference(ALLOW_MOCK_LOCATION);
113         mPassword = (PreferenceScreen) findPreference(LOCAL_BACKUP_PASSWORD);
114
115         mStrictMode = (CheckBoxPreference) findPreference(STRICT_MODE_KEY);
116         mPointerLocation = (CheckBoxPreference) findPreference(POINTER_LOCATION_KEY);
117         mShowTouches = (CheckBoxPreference) findPreference(SHOW_TOUCHES_KEY);
118         mShowScreenUpdates = (CheckBoxPreference) findPreference(SHOW_SCREEN_UPDATES_KEY);
119         mShowCpuUsage = (CheckBoxPreference) findPreference(SHOW_CPU_USAGE_KEY);
120         mWindowAnimationScale = (ListPreference) findPreference(WINDOW_ANIMATION_SCALE_KEY);
121         mWindowAnimationScale.setOnPreferenceChangeListener(this);
122         mTransitionAnimationScale = (ListPreference) findPreference(TRANSITION_ANIMATION_SCALE_KEY);
123         mTransitionAnimationScale.setOnPreferenceChangeListener(this);
124
125         mImmediatelyDestroyActivities = (CheckBoxPreference) findPreference(
126                 IMMEDIATELY_DESTROY_ACTIVITIES_KEY);
127         mAppProcessLimit = (ListPreference) findPreference(APP_PROCESS_LIMIT_KEY);
128         mAppProcessLimit.setOnPreferenceChangeListener(this);
129
130         mShowAllANRs = (CheckBoxPreference) findPreference(
131                 SHOW_ALL_ANRS_KEY);
132
133         removeHdcpOptionsForProduction();
134     }
135
136     private void removeHdcpOptionsForProduction() {
137         if ("user".equals(Build.TYPE)) {
138             Preference hdcpChecking = findPreference(HDCP_CHECKING_KEY);
139             if (hdcpChecking != null) {
140                 // Remove the preference
141                 getPreferenceScreen().removePreference(hdcpChecking);
142             }
143         }
144     }
145
146     @Override
147     public void onResume() {
148         super.onResume();
149
150         final ContentResolver cr = getActivity().getContentResolver();
151         mEnableAdb.setChecked(Settings.Secure.getInt(cr,
152                 Settings.Secure.ADB_ENABLED, 0) != 0);
153         mKeepScreenOn.setChecked(Settings.System.getInt(cr,
154                 Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0) != 0);
155         mAllowMockLocation.setChecked(Settings.Secure.getInt(cr,
156                 Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0);
157         updateHdcpValues();
158         updatePasswordSummary();
159         updateStrictModeVisualOptions();
160         updatePointerLocationOptions();
161         updateShowTouchesOptions();
162         updateFlingerOptions();
163         updateCpuUsageOptions();
164         updateAnimationScaleOptions();
165         updateImmediatelyDestroyActivitiesOptions();
166         updateAppProcessLimitOptions();
167         updateShowAllANRsOptions();
168     }
169
170     private void updateHdcpValues() {
171         int index = 1; // Defaults to drm-only. Needs to match with R.array.hdcp_checking_values
172         ListPreference hdcpChecking = (ListPreference) findPreference(HDCP_CHECKING_KEY);
173         if (hdcpChecking != null) {
174             String currentValue = SystemProperties.get(HDCP_CHECKING_PROPERTY);
175             String[] values = getResources().getStringArray(R.array.hdcp_checking_values);
176             String[] summaries = getResources().getStringArray(R.array.hdcp_checking_summaries);
177             for (int i = 0; i < values.length; i++) {
178                 if (currentValue.equals(values[i])) {
179                     index = i;
180                     break;
181                 }
182             }
183             hdcpChecking.setValue(values[index]);
184             hdcpChecking.setSummary(summaries[index]);
185             hdcpChecking.setOnPreferenceChangeListener(this);
186         }
187     }
188
189     private void updatePasswordSummary() {
190         try {
191             if (mBackupManager.hasBackupPassword()) {
192                 mPassword.setSummary(R.string.local_backup_password_summary_change);
193             } else {
194                 mPassword.setSummary(R.string.local_backup_password_summary_none);
195             }
196         } catch (RemoteException e) {
197             // Not much we can do here
198         }
199     }
200
201     // Returns the current state of the system property that controls
202     // strictmode flashes.  One of:
203     //    0: not explicitly set one way or another
204     //    1: on
205     //    2: off
206     private int currentStrictModeActiveIndex() {
207         if (TextUtils.isEmpty(SystemProperties.get(StrictMode.VISUAL_PROPERTY))) {
208             return 0;
209         }
210         boolean enabled = SystemProperties.getBoolean(StrictMode.VISUAL_PROPERTY, false);
211         return enabled ? 1 : 2;
212     }
213
214     private void writeStrictModeVisualOptions() {
215         try {
216             mWindowManager.setStrictModeVisualIndicatorPreference(mStrictMode.isChecked()
217                     ? "1" : "");
218         } catch (RemoteException e) {
219         }
220     }
221
222     private void updateStrictModeVisualOptions() {
223         mStrictMode.setChecked(currentStrictModeActiveIndex() == 1);
224     }
225
226     private void writePointerLocationOptions() {
227         Settings.System.putInt(getActivity().getContentResolver(),
228                 Settings.System.POINTER_LOCATION, mPointerLocation.isChecked() ? 1 : 0);
229     }
230
231     private void updatePointerLocationOptions() {
232         mPointerLocation.setChecked(Settings.System.getInt(getActivity().getContentResolver(),
233                 Settings.System.POINTER_LOCATION, 0) != 0);
234     }
235
236     private void writeShowTouchesOptions() {
237         Settings.System.putInt(getActivity().getContentResolver(),
238                 Settings.System.SHOW_TOUCHES, mShowTouches.isChecked() ? 1 : 0);
239     }
240
241     private void updateShowTouchesOptions() {
242         mShowTouches.setChecked(Settings.System.getInt(getActivity().getContentResolver(),
243                 Settings.System.SHOW_TOUCHES, 0) != 0);
244     }
245
246     private void updateFlingerOptions() {
247         // magic communication with surface flinger.
248         try {
249             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
250             if (flinger != null) {
251                 Parcel data = Parcel.obtain();
252                 Parcel reply = Parcel.obtain();
253                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
254                 flinger.transact(1010, data, reply, 0);
255                 @SuppressWarnings("unused")
256                 int showCpu = reply.readInt();
257                 @SuppressWarnings("unused")
258                 int enableGL = reply.readInt();
259                 int showUpdates = reply.readInt();
260                 mShowScreenUpdates.setChecked(showUpdates != 0);
261                 @SuppressWarnings("unused")
262                 int showBackground = reply.readInt();
263                 reply.recycle();
264                 data.recycle();
265             }
266         } catch (RemoteException ex) {
267         }
268     }
269
270     private void writeFlingerOptions() {
271         try {
272             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
273             if (flinger != null) {
274                 Parcel data = Parcel.obtain();
275                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
276                 data.writeInt(mShowScreenUpdates.isChecked() ? 1 : 0);
277                 flinger.transact(1002, data, null, 0);
278                 data.recycle();
279
280                 updateFlingerOptions();
281             }
282         } catch (RemoteException ex) {
283         }
284     }
285
286     private void updateCpuUsageOptions() {
287         mShowCpuUsage.setChecked(Settings.System.getInt(getActivity().getContentResolver(),
288                 Settings.System.SHOW_PROCESSES, 0) != 0);
289     }
290
291     private void writeCpuUsageOptions() {
292         boolean value = mShowCpuUsage.isChecked();
293         Settings.System.putInt(getActivity().getContentResolver(),
294                 Settings.System.SHOW_PROCESSES, value ? 1 : 0);
295         Intent service = (new Intent())
296                 .setClassName("com.android.systemui", "com.android.systemui.LoadAverageService");
297         if (value) {
298             getActivity().startService(service);
299         } else {
300             getActivity().stopService(service);
301         }
302     }
303
304     private void writeImmediatelyDestroyActivitiesOptions() {
305         try {
306             ActivityManagerNative.getDefault().setAlwaysFinish(
307                     mImmediatelyDestroyActivities.isChecked());
308         } catch (RemoteException ex) {
309         }
310     }
311
312     private void updateImmediatelyDestroyActivitiesOptions() {
313         mImmediatelyDestroyActivities.setChecked(Settings.System.getInt(
314             getActivity().getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0);
315     }
316
317     private void updateAnimationScaleValue(int which, ListPreference pref) {
318         try {
319             float scale = mWindowManager.getAnimationScale(which);
320             CharSequence[] values = pref.getEntryValues();
321             for (int i=0; i<values.length; i++) {
322                 float val = Float.parseFloat(values[i].toString());
323                 if (scale <= val) {
324                     pref.setValueIndex(i);
325                     pref.setSummary(pref.getEntries()[i]);
326                     return;
327                 }
328             }
329             pref.setValueIndex(values.length-1);
330             pref.setSummary(pref.getEntries()[0]);
331         } catch (RemoteException e) {
332         }
333     }
334
335     private void updateAnimationScaleOptions() {
336         updateAnimationScaleValue(0, mWindowAnimationScale);
337         updateAnimationScaleValue(1, mTransitionAnimationScale);
338     }
339
340     private void writeAnimationScaleOption(int which, ListPreference pref, Object newValue) {
341         try {
342             float scale = Float.parseFloat(newValue.toString());
343             mWindowManager.setAnimationScale(which, scale);
344             updateAnimationScaleValue(which, pref);
345         } catch (RemoteException e) {
346         }
347     }
348
349     private void updateAppProcessLimitOptions() {
350         try {
351             int limit = ActivityManagerNative.getDefault().getProcessLimit();
352             CharSequence[] values = mAppProcessLimit.getEntryValues();
353             for (int i=0; i<values.length; i++) {
354                 int val = Integer.parseInt(values[i].toString());
355                 if (val >= limit) {
356                     mAppProcessLimit.setValueIndex(i);
357                     mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[i]);
358                     return;
359                 }
360             }
361             mAppProcessLimit.setValueIndex(0);
362             mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[0]);
363         } catch (RemoteException e) {
364         }
365     }
366
367     private void writeAppProcessLimitOptions(Object newValue) {
368         try {
369             int limit = Integer.parseInt(newValue.toString());
370             ActivityManagerNative.getDefault().setProcessLimit(limit);
371             updateAppProcessLimitOptions();
372         } catch (RemoteException e) {
373         }
374     }
375
376     private void writeShowAllANRsOptions() {
377         Settings.Secure.putInt(getActivity().getContentResolver(),
378                 Settings.Secure.ANR_SHOW_BACKGROUND,
379                 mShowAllANRs.isChecked() ? 1 : 0);
380     }
381
382     private void updateShowAllANRsOptions() {
383         mShowAllANRs.setChecked(Settings.Secure.getInt(
384             getActivity().getContentResolver(), Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0);
385     }
386
387     @Override
388     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
389
390         if (Utils.isMonkeyRunning()) {
391             return false;
392         }
393
394         if (preference == mEnableAdb) {
395             if (mEnableAdb.isChecked()) {
396                 mOkClicked = false;
397                 if (mOkDialog != null) dismissDialog();
398                 mOkDialog = new AlertDialog.Builder(getActivity()).setMessage(
399                         getActivity().getResources().getString(R.string.adb_warning_message))
400                         .setTitle(R.string.adb_warning_title)
401                         .setIcon(android.R.drawable.ic_dialog_alert)
402                         .setPositiveButton(android.R.string.yes, this)
403                         .setNegativeButton(android.R.string.no, this)
404                         .show();
405                 mOkDialog.setOnDismissListener(this);
406             } else {
407                 Settings.Secure.putInt(getActivity().getContentResolver(),
408                         Settings.Secure.ADB_ENABLED, 0);
409             }
410         } else if (preference == mKeepScreenOn) {
411             Settings.System.putInt(getActivity().getContentResolver(),
412                     Settings.System.STAY_ON_WHILE_PLUGGED_IN, 
413                     mKeepScreenOn.isChecked() ? 
414                     (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);
415         } else if (preference == mAllowMockLocation) {
416             Settings.Secure.putInt(getActivity().getContentResolver(),
417                     Settings.Secure.ALLOW_MOCK_LOCATION,
418                     mAllowMockLocation.isChecked() ? 1 : 0);
419         } else if (preference == mStrictMode) {
420             writeStrictModeVisualOptions();
421         } else if (preference == mPointerLocation) {
422             writePointerLocationOptions();
423         } else if (preference == mShowTouches) {
424             writeShowTouchesOptions();
425         } else if (preference == mShowScreenUpdates) {
426             writeFlingerOptions();
427         } else if (preference == mShowCpuUsage) {
428             writeCpuUsageOptions();
429         } else if (preference == mImmediatelyDestroyActivities) {
430             writeImmediatelyDestroyActivitiesOptions();
431         } else if (preference == mShowAllANRs) {
432             writeShowAllANRsOptions();
433         }
434
435         return false;
436     }
437
438     @Override
439     public boolean onPreferenceChange(Preference preference, Object newValue) {
440         if (HDCP_CHECKING_KEY.equals(preference.getKey())) {
441             SystemProperties.set(HDCP_CHECKING_PROPERTY, newValue.toString());
442             updateHdcpValues();
443             return true;
444         } else if (preference == mWindowAnimationScale) {
445             writeAnimationScaleOption(0, mWindowAnimationScale, newValue);
446             return true;
447         } else if (preference == mTransitionAnimationScale) {
448             writeAnimationScaleOption(1, mTransitionAnimationScale, newValue);
449             return true;
450         } else if (preference == mAppProcessLimit) {
451             writeAppProcessLimitOptions(newValue);
452             return true;
453         }
454         return false;
455     }
456
457     private void dismissDialog() {
458         if (mOkDialog == null) return;
459         mOkDialog.dismiss();
460         mOkDialog = null;
461     }
462
463     public void onClick(DialogInterface dialog, int which) {
464         if (which == DialogInterface.BUTTON_POSITIVE) {
465             mOkClicked = true;
466             Settings.Secure.putInt(getActivity().getContentResolver(),
467                     Settings.Secure.ADB_ENABLED, 1);
468         } else {
469             // Reset the toggle
470             mEnableAdb.setChecked(false);
471         }
472     }
473
474     public void onDismiss(DialogInterface dialog) {
475         // Assuming that onClick gets called first
476         if (!mOkClicked) {
477             mEnableAdb.setChecked(false);
478         }
479     }
480
481     @Override
482     public void onDestroy() {
483         dismissDialog();
484         super.onDestroy();
485     }
486 }