2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
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;
47 * Displays preferences for application developers.
49 public class DevelopmentSettings extends PreferenceFragment
50 implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
51 OnPreferenceChangeListener {
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";
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";
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";
72 private static final String SHOW_ALL_ANRS_KEY = "show_all_anrs";
74 private IWindowManager mWindowManager;
75 private IBackupManager mBackupManager;
77 private CheckBoxPreference mEnableAdb;
78 private CheckBoxPreference mKeepScreenOn;
79 private CheckBoxPreference mAllowMockLocation;
80 private PreferenceScreen mPassword;
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;
90 private CheckBoxPreference mImmediatelyDestroyActivities;
91 private ListPreference mAppProcessLimit;
93 private CheckBoxPreference mShowAllANRs;
95 // To track whether Yes was clicked in the adb warning dialog
96 private boolean mOkClicked;
98 private Dialog mOkDialog;
101 public void onCreate(Bundle icicle) {
102 super.onCreate(icicle);
104 mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
105 mBackupManager = IBackupManager.Stub.asInterface(
106 ServiceManager.getService(Context.BACKUP_SERVICE));
108 addPreferencesFromResource(R.xml.development_prefs);
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);
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);
125 mImmediatelyDestroyActivities = (CheckBoxPreference) findPreference(
126 IMMEDIATELY_DESTROY_ACTIVITIES_KEY);
127 mAppProcessLimit = (ListPreference) findPreference(APP_PROCESS_LIMIT_KEY);
128 mAppProcessLimit.setOnPreferenceChangeListener(this);
130 mShowAllANRs = (CheckBoxPreference) findPreference(
133 removeHdcpOptionsForProduction();
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);
147 public void onResume() {
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);
158 updatePasswordSummary();
159 updateStrictModeVisualOptions();
160 updatePointerLocationOptions();
161 updateShowTouchesOptions();
162 updateFlingerOptions();
163 updateCpuUsageOptions();
164 updateAnimationScaleOptions();
165 updateImmediatelyDestroyActivitiesOptions();
166 updateAppProcessLimitOptions();
167 updateShowAllANRsOptions();
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])) {
183 hdcpChecking.setValue(values[index]);
184 hdcpChecking.setSummary(summaries[index]);
185 hdcpChecking.setOnPreferenceChangeListener(this);
189 private void updatePasswordSummary() {
191 if (mBackupManager.hasBackupPassword()) {
192 mPassword.setSummary(R.string.local_backup_password_summary_change);
194 mPassword.setSummary(R.string.local_backup_password_summary_none);
196 } catch (RemoteException e) {
197 // Not much we can do here
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
206 private int currentStrictModeActiveIndex() {
207 if (TextUtils.isEmpty(SystemProperties.get(StrictMode.VISUAL_PROPERTY))) {
210 boolean enabled = SystemProperties.getBoolean(StrictMode.VISUAL_PROPERTY, false);
211 return enabled ? 1 : 2;
214 private void writeStrictModeVisualOptions() {
216 mWindowManager.setStrictModeVisualIndicatorPreference(mStrictMode.isChecked()
218 } catch (RemoteException e) {
222 private void updateStrictModeVisualOptions() {
223 mStrictMode.setChecked(currentStrictModeActiveIndex() == 1);
226 private void writePointerLocationOptions() {
227 Settings.System.putInt(getActivity().getContentResolver(),
228 Settings.System.POINTER_LOCATION, mPointerLocation.isChecked() ? 1 : 0);
231 private void updatePointerLocationOptions() {
232 mPointerLocation.setChecked(Settings.System.getInt(getActivity().getContentResolver(),
233 Settings.System.POINTER_LOCATION, 0) != 0);
236 private void writeShowTouchesOptions() {
237 Settings.System.putInt(getActivity().getContentResolver(),
238 Settings.System.SHOW_TOUCHES, mShowTouches.isChecked() ? 1 : 0);
241 private void updateShowTouchesOptions() {
242 mShowTouches.setChecked(Settings.System.getInt(getActivity().getContentResolver(),
243 Settings.System.SHOW_TOUCHES, 0) != 0);
246 private void updateFlingerOptions() {
247 // magic communication with surface flinger.
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();
266 } catch (RemoteException ex) {
270 private void writeFlingerOptions() {
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);
280 updateFlingerOptions();
282 } catch (RemoteException ex) {
286 private void updateCpuUsageOptions() {
287 mShowCpuUsage.setChecked(Settings.System.getInt(getActivity().getContentResolver(),
288 Settings.System.SHOW_PROCESSES, 0) != 0);
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");
298 getActivity().startService(service);
300 getActivity().stopService(service);
304 private void writeImmediatelyDestroyActivitiesOptions() {
306 ActivityManagerNative.getDefault().setAlwaysFinish(
307 mImmediatelyDestroyActivities.isChecked());
308 } catch (RemoteException ex) {
312 private void updateImmediatelyDestroyActivitiesOptions() {
313 mImmediatelyDestroyActivities.setChecked(Settings.System.getInt(
314 getActivity().getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0);
317 private void updateAnimationScaleValue(int which, ListPreference pref) {
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());
324 pref.setValueIndex(i);
325 pref.setSummary(pref.getEntries()[i]);
329 pref.setValueIndex(values.length-1);
330 pref.setSummary(pref.getEntries()[0]);
331 } catch (RemoteException e) {
335 private void updateAnimationScaleOptions() {
336 updateAnimationScaleValue(0, mWindowAnimationScale);
337 updateAnimationScaleValue(1, mTransitionAnimationScale);
340 private void writeAnimationScaleOption(int which, ListPreference pref, Object newValue) {
342 float scale = Float.parseFloat(newValue.toString());
343 mWindowManager.setAnimationScale(which, scale);
344 updateAnimationScaleValue(which, pref);
345 } catch (RemoteException e) {
349 private void updateAppProcessLimitOptions() {
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());
356 mAppProcessLimit.setValueIndex(i);
357 mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[i]);
361 mAppProcessLimit.setValueIndex(0);
362 mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[0]);
363 } catch (RemoteException e) {
367 private void writeAppProcessLimitOptions(Object newValue) {
369 int limit = Integer.parseInt(newValue.toString());
370 ActivityManagerNative.getDefault().setProcessLimit(limit);
371 updateAppProcessLimitOptions();
372 } catch (RemoteException e) {
376 private void writeShowAllANRsOptions() {
377 Settings.Secure.putInt(getActivity().getContentResolver(),
378 Settings.Secure.ANR_SHOW_BACKGROUND,
379 mShowAllANRs.isChecked() ? 1 : 0);
382 private void updateShowAllANRsOptions() {
383 mShowAllANRs.setChecked(Settings.Secure.getInt(
384 getActivity().getContentResolver(), Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0);
388 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
390 if (Utils.isMonkeyRunning()) {
394 if (preference == mEnableAdb) {
395 if (mEnableAdb.isChecked()) {
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)
405 mOkDialog.setOnDismissListener(this);
407 Settings.Secure.putInt(getActivity().getContentResolver(),
408 Settings.Secure.ADB_ENABLED, 0);
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();
439 public boolean onPreferenceChange(Preference preference, Object newValue) {
440 if (HDCP_CHECKING_KEY.equals(preference.getKey())) {
441 SystemProperties.set(HDCP_CHECKING_PROPERTY, newValue.toString());
444 } else if (preference == mWindowAnimationScale) {
445 writeAnimationScaleOption(0, mWindowAnimationScale, newValue);
447 } else if (preference == mTransitionAnimationScale) {
448 writeAnimationScaleOption(1, mTransitionAnimationScale, newValue);
450 } else if (preference == mAppProcessLimit) {
451 writeAppProcessLimitOptions(newValue);
457 private void dismissDialog() {
458 if (mOkDialog == null) return;
463 public void onClick(DialogInterface dialog, int which) {
464 if (which == DialogInterface.BUTTON_POSITIVE) {
466 Settings.Secure.putInt(getActivity().getContentResolver(),
467 Settings.Secure.ADB_ENABLED, 1);
470 mEnableAdb.setChecked(false);
474 public void onDismiss(DialogInterface dialog) {
475 // Assuming that onClick gets called first
477 mEnableAdb.setChecked(false);
482 public void onDestroy() {