DevelopmentSettings.java revision e44f44d67913ae74900656ee2ef1d2f690c63d1c
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
17package com.android.settings;
18
19import java.util.ArrayList;
20
21import android.app.ActionBar;
22import android.app.Activity;
23import android.app.ActivityManagerNative;
24import android.app.AlertDialog;
25import android.app.Dialog;
26import android.app.backup.IBackupManager;
27import android.content.ContentResolver;
28import android.content.Context;
29import android.content.DialogInterface;
30import android.content.Intent;
31import android.content.pm.PackageManager;
32import android.content.pm.VerifierDeviceIdentity;
33import android.os.BatteryManager;
34import android.os.Build;
35import android.os.Bundle;
36import android.os.IBinder;
37import android.os.Parcel;
38import android.os.RemoteException;
39import android.os.ServiceManager;
40import android.os.StrictMode;
41import android.os.SystemProperties;
42import android.preference.CheckBoxPreference;
43import android.preference.ListPreference;
44import android.preference.Preference;
45import android.preference.PreferenceFragment;
46import android.preference.PreferenceScreen;
47import android.preference.Preference.OnPreferenceChangeListener;
48import android.provider.Settings;
49import android.text.TextUtils;
50import android.view.Gravity;
51import android.view.IWindowManager;
52import android.widget.CompoundButton;
53import android.widget.Switch;
54
55/*
56 * Displays preferences for application developers.
57 */
58public class DevelopmentSettings extends PreferenceFragment
59        implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
60                OnPreferenceChangeListener, CompoundButton.OnCheckedChangeListener {
61
62    private static final String ENABLE_ADB = "enable_adb";
63
64    private static final String VERIFIER_DEVICE_IDENTIFIER = "verifier_device_identifier";
65    private static final String KEEP_SCREEN_ON = "keep_screen_on";
66    private static final String ALLOW_MOCK_LOCATION = "allow_mock_location";
67    private static final String HDCP_CHECKING_KEY = "hdcp_checking";
68    private static final String HDCP_CHECKING_PROPERTY = "persist.sys.hdcp_checking";
69    private static final String LOCAL_BACKUP_PASSWORD = "local_backup_password";
70    private static final String HARDWARE_UI_PROPERTY = "persist.sys.ui.hw";
71
72    private static final String STRICT_MODE_KEY = "strict_mode";
73    private static final String POINTER_LOCATION_KEY = "pointer_location";
74    private static final String SHOW_TOUCHES_KEY = "show_touches";
75    private static final String SHOW_SCREEN_UPDATES_KEY = "show_screen_updates";
76    private static final String DISABLE_OVERLAYS_KEY = "disable_overlays";
77    private static final String SHOW_CPU_USAGE_KEY = "show_cpu_usage";
78    private static final String FORCE_HARDWARE_UI_KEY = "force_hw_ui";
79    private static final String WINDOW_ANIMATION_SCALE_KEY = "window_animation_scale";
80    private static final String TRANSITION_ANIMATION_SCALE_KEY = "transition_animation_scale";
81    private static final String ANIMATOR_DURATION_SCALE_KEY = "animator_duration_scale";
82
83    private static final String IMMEDIATELY_DESTROY_ACTIVITIES_KEY
84            = "immediately_destroy_activities";
85    private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit";
86
87    private static final String SHOW_ALL_ANRS_KEY = "show_all_anrs";
88
89    private IWindowManager mWindowManager;
90    private IBackupManager mBackupManager;
91
92    private Switch mEnabledSwitch;
93    private boolean mLastEnabledState;
94
95    private CheckBoxPreference mEnableAdb;
96    private CheckBoxPreference mKeepScreenOn;
97    private CheckBoxPreference mAllowMockLocation;
98    private PreferenceScreen mPassword;
99
100    private CheckBoxPreference mStrictMode;
101    private CheckBoxPreference mPointerLocation;
102    private CheckBoxPreference mShowTouches;
103    private CheckBoxPreference mShowScreenUpdates;
104    private CheckBoxPreference mDisableOverlays;
105    private CheckBoxPreference mShowCpuUsage;
106    private CheckBoxPreference mForceHardwareUi;
107    private ListPreference mWindowAnimationScale;
108    private ListPreference mTransitionAnimationScale;
109    private ListPreference mAnimatorDurationScale;
110
111    private CheckBoxPreference mImmediatelyDestroyActivities;
112    private ListPreference mAppProcessLimit;
113
114    private CheckBoxPreference mShowAllANRs;
115
116    private final ArrayList<Preference> mAllPrefs = new ArrayList<Preference>();
117    private final ArrayList<CheckBoxPreference> mResetCbPrefs
118            = new ArrayList<CheckBoxPreference>();
119
120    // To track whether a confirmation dialog was clicked.
121    private boolean mDialogClicked;
122    private Dialog mEnableDialog;
123    private Dialog mAdbDialog;
124
125    @Override
126    public void onCreate(Bundle icicle) {
127        super.onCreate(icicle);
128
129        mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
130        mBackupManager = IBackupManager.Stub.asInterface(
131                ServiceManager.getService(Context.BACKUP_SERVICE));
132
133        addPreferencesFromResource(R.xml.development_prefs);
134
135        mEnableAdb = (CheckBoxPreference) findPreference(ENABLE_ADB);
136        mAllPrefs.add(mEnableAdb);
137        mResetCbPrefs.add(mEnableAdb);
138        mKeepScreenOn = (CheckBoxPreference) findPreference(KEEP_SCREEN_ON);
139        mAllPrefs.add(mKeepScreenOn);
140        mResetCbPrefs.add(mKeepScreenOn);
141        mAllowMockLocation = (CheckBoxPreference) findPreference(ALLOW_MOCK_LOCATION);
142        mAllPrefs.add(mAllowMockLocation);
143        mResetCbPrefs.add(mAllowMockLocation);
144        mPassword = (PreferenceScreen) findPreference(LOCAL_BACKUP_PASSWORD);
145        mAllPrefs.add(mPassword);
146
147        mStrictMode = (CheckBoxPreference) findPreference(STRICT_MODE_KEY);
148        mAllPrefs.add(mStrictMode);
149        mResetCbPrefs.add(mStrictMode);
150        mResetCbPrefs.add(mAllowMockLocation);
151        mPointerLocation = (CheckBoxPreference) findPreference(POINTER_LOCATION_KEY);
152        mAllPrefs.add(mPointerLocation);
153        mResetCbPrefs.add(mPointerLocation);
154        mShowTouches = (CheckBoxPreference) findPreference(SHOW_TOUCHES_KEY);
155        mAllPrefs.add(mShowTouches);
156        mResetCbPrefs.add(mShowTouches);
157        mShowScreenUpdates = (CheckBoxPreference) findPreference(SHOW_SCREEN_UPDATES_KEY);
158        mAllPrefs.add(mShowScreenUpdates);
159        mResetCbPrefs.add(mShowScreenUpdates);
160        mDisableOverlays = (CheckBoxPreference) findPreference(DISABLE_OVERLAYS_KEY);
161        mAllPrefs.add(mDisableOverlays);
162        mResetCbPrefs.add(mDisableOverlays);
163        mShowCpuUsage = (CheckBoxPreference) findPreference(SHOW_CPU_USAGE_KEY);
164        mAllPrefs.add(mShowCpuUsage);
165        mResetCbPrefs.add(mShowCpuUsage);
166        mForceHardwareUi = (CheckBoxPreference) findPreference(FORCE_HARDWARE_UI_KEY);
167        mAllPrefs.add(mForceHardwareUi);
168        mResetCbPrefs.add(mForceHardwareUi);
169        mWindowAnimationScale = (ListPreference) findPreference(WINDOW_ANIMATION_SCALE_KEY);
170        mAllPrefs.add(mWindowAnimationScale);
171        mWindowAnimationScale.setOnPreferenceChangeListener(this);
172        mTransitionAnimationScale = (ListPreference) findPreference(TRANSITION_ANIMATION_SCALE_KEY);
173        mAllPrefs.add(mTransitionAnimationScale);
174        mTransitionAnimationScale.setOnPreferenceChangeListener(this);
175        mAnimatorDurationScale = (ListPreference) findPreference(ANIMATOR_DURATION_SCALE_KEY);
176        mAllPrefs.add(mAnimatorDurationScale);
177        mAnimatorDurationScale.setOnPreferenceChangeListener(this);
178
179        mImmediatelyDestroyActivities = (CheckBoxPreference) findPreference(
180                IMMEDIATELY_DESTROY_ACTIVITIES_KEY);
181        mAllPrefs.add(mImmediatelyDestroyActivities);
182        mResetCbPrefs.add(mImmediatelyDestroyActivities);
183        mAppProcessLimit = (ListPreference) findPreference(APP_PROCESS_LIMIT_KEY);
184        mAllPrefs.add(mAppProcessLimit);
185        mAppProcessLimit.setOnPreferenceChangeListener(this);
186
187        mShowAllANRs = (CheckBoxPreference) findPreference(
188                SHOW_ALL_ANRS_KEY);
189        mAllPrefs.add(mShowAllANRs);
190        mResetCbPrefs.add(mShowAllANRs);
191
192        final Preference verifierDeviceIdentifier = findPreference(VERIFIER_DEVICE_IDENTIFIER);
193        final PackageManager pm = getActivity().getPackageManager();
194        final VerifierDeviceIdentity verifierIndentity = pm.getVerifierDeviceIdentity();
195        if (verifierIndentity != null) {
196            verifierDeviceIdentifier.setSummary(verifierIndentity.toString());
197        }
198
199        Preference hdcpChecking = findPreference(HDCP_CHECKING_KEY);
200        if (hdcpChecking != null) {
201            mAllPrefs.add(hdcpChecking);
202        }
203        removeHdcpOptionsForProduction();
204    }
205
206    @Override
207    public void onActivityCreated(Bundle savedInstanceState) {
208        super.onActivityCreated(savedInstanceState);
209
210        final Activity activity = getActivity();
211        mEnabledSwitch = new Switch(activity);
212
213        final int padding = activity.getResources().getDimensionPixelSize(
214                R.dimen.action_bar_switch_padding);
215        mEnabledSwitch.setPadding(0, 0, padding, 0);
216        mEnabledSwitch.setOnCheckedChangeListener(this);
217    }
218
219    @Override
220    public void onStart() {
221        super.onStart();
222        final Activity activity = getActivity();
223        activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
224                ActionBar.DISPLAY_SHOW_CUSTOM);
225        activity.getActionBar().setCustomView(mEnabledSwitch, new ActionBar.LayoutParams(
226                ActionBar.LayoutParams.WRAP_CONTENT,
227                ActionBar.LayoutParams.WRAP_CONTENT,
228                Gravity.CENTER_VERTICAL | Gravity.RIGHT));
229    }
230
231    @Override
232    public void onStop() {
233        super.onStop();
234        final Activity activity = getActivity();
235        activity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
236        activity.getActionBar().setCustomView(null);
237    }
238
239    private void removeHdcpOptionsForProduction() {
240        if ("user".equals(Build.TYPE)) {
241            Preference hdcpChecking = findPreference(HDCP_CHECKING_KEY);
242            if (hdcpChecking != null) {
243                // Remove the preference
244                getPreferenceScreen().removePreference(hdcpChecking);
245                mAllPrefs.remove(hdcpChecking);
246            }
247        }
248    }
249
250    private void setPrefsEnabledState(boolean enabled) {
251        for (int i=0; i<mAllPrefs.size(); i++) {
252            mAllPrefs.get(i).setEnabled(enabled);
253        }
254    }
255
256    @Override
257    public void onResume() {
258        super.onResume();
259
260        final ContentResolver cr = getActivity().getContentResolver();
261        mLastEnabledState = Settings.Secure.getInt(cr,
262                Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
263        mEnabledSwitch.setChecked(mLastEnabledState);
264        setPrefsEnabledState(mLastEnabledState);
265        updateAllOptions();
266    }
267
268    private void updateAllOptions() {
269        final ContentResolver cr = getActivity().getContentResolver();
270        mEnableAdb.setChecked(Settings.Secure.getInt(cr,
271                Settings.Secure.ADB_ENABLED, 0) != 0);
272        mKeepScreenOn.setChecked(Settings.System.getInt(cr,
273                Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0) != 0);
274        mAllowMockLocation.setChecked(Settings.Secure.getInt(cr,
275                Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0);
276        updateHdcpValues();
277        updatePasswordSummary();
278        updateStrictModeVisualOptions();
279        updatePointerLocationOptions();
280        updateShowTouchesOptions();
281        updateFlingerOptions();
282        updateCpuUsageOptions();
283        updateHardwareUiOptions();
284        updateAnimationScaleOptions();
285        updateImmediatelyDestroyActivitiesOptions();
286        updateAppProcessLimitOptions();
287        updateShowAllANRsOptions();
288    }
289
290    private void resetDangerousOptions() {
291        for (int i=0; i<mResetCbPrefs.size(); i++) {
292            CheckBoxPreference cb = mResetCbPrefs.get(i);
293            if (cb.isChecked()) {
294                cb.setChecked(false);
295                onPreferenceTreeClick(null, cb);
296            }
297        }
298        writeAnimationScaleOption(0, mWindowAnimationScale, null);
299        writeAnimationScaleOption(1, mTransitionAnimationScale, null);
300        writeAnimationScaleOption(2, mAnimatorDurationScale, null);
301        writeAppProcessLimitOptions(null);
302        updateAllOptions();
303    }
304
305    private void updateHdcpValues() {
306        int index = 1; // Defaults to drm-only. Needs to match with R.array.hdcp_checking_values
307        ListPreference hdcpChecking = (ListPreference) findPreference(HDCP_CHECKING_KEY);
308        if (hdcpChecking != null) {
309            String currentValue = SystemProperties.get(HDCP_CHECKING_PROPERTY);
310            String[] values = getResources().getStringArray(R.array.hdcp_checking_values);
311            String[] summaries = getResources().getStringArray(R.array.hdcp_checking_summaries);
312            for (int i = 0; i < values.length; i++) {
313                if (currentValue.equals(values[i])) {
314                    index = i;
315                    break;
316                }
317            }
318            hdcpChecking.setValue(values[index]);
319            hdcpChecking.setSummary(summaries[index]);
320            hdcpChecking.setOnPreferenceChangeListener(this);
321        }
322    }
323
324    private void updatePasswordSummary() {
325        try {
326            if (mBackupManager.hasBackupPassword()) {
327                mPassword.setSummary(R.string.local_backup_password_summary_change);
328            } else {
329                mPassword.setSummary(R.string.local_backup_password_summary_none);
330            }
331        } catch (RemoteException e) {
332            // Not much we can do here
333        }
334    }
335
336    // Returns the current state of the system property that controls
337    // strictmode flashes.  One of:
338    //    0: not explicitly set one way or another
339    //    1: on
340    //    2: off
341    private int currentStrictModeActiveIndex() {
342        if (TextUtils.isEmpty(SystemProperties.get(StrictMode.VISUAL_PROPERTY))) {
343            return 0;
344        }
345        boolean enabled = SystemProperties.getBoolean(StrictMode.VISUAL_PROPERTY, false);
346        return enabled ? 1 : 2;
347    }
348
349    private void writeStrictModeVisualOptions() {
350        try {
351            mWindowManager.setStrictModeVisualIndicatorPreference(mStrictMode.isChecked()
352                    ? "1" : "");
353        } catch (RemoteException e) {
354        }
355    }
356
357    private void updateStrictModeVisualOptions() {
358        mStrictMode.setChecked(currentStrictModeActiveIndex() == 1);
359    }
360
361    private void writePointerLocationOptions() {
362        Settings.System.putInt(getActivity().getContentResolver(),
363                Settings.System.POINTER_LOCATION, mPointerLocation.isChecked() ? 1 : 0);
364    }
365
366    private void updatePointerLocationOptions() {
367        mPointerLocation.setChecked(Settings.System.getInt(getActivity().getContentResolver(),
368                Settings.System.POINTER_LOCATION, 0) != 0);
369    }
370
371    private void writeShowTouchesOptions() {
372        Settings.System.putInt(getActivity().getContentResolver(),
373                Settings.System.SHOW_TOUCHES, mShowTouches.isChecked() ? 1 : 0);
374    }
375
376    private void updateShowTouchesOptions() {
377        mShowTouches.setChecked(Settings.System.getInt(getActivity().getContentResolver(),
378                Settings.System.SHOW_TOUCHES, 0) != 0);
379    }
380
381    private void updateFlingerOptions() {
382        // magic communication with surface flinger.
383        try {
384            IBinder flinger = ServiceManager.getService("SurfaceFlinger");
385            if (flinger != null) {
386                Parcel data = Parcel.obtain();
387                Parcel reply = Parcel.obtain();
388                data.writeInterfaceToken("android.ui.ISurfaceComposer");
389                flinger.transact(1010, data, reply, 0);
390                @SuppressWarnings("unused")
391                int showCpu = reply.readInt();
392                @SuppressWarnings("unused")
393                int enableGL = reply.readInt();
394                int showUpdates = reply.readInt();
395                mShowScreenUpdates.setChecked(showUpdates != 0);
396                @SuppressWarnings("unused")
397                int showBackground = reply.readInt();
398                int disableOverlays = reply.readInt();
399                mDisableOverlays.setChecked(disableOverlays != 0);
400                reply.recycle();
401                data.recycle();
402            }
403        } catch (RemoteException ex) {
404        }
405    }
406
407    private void writeShowUpdatesOption() {
408        try {
409            IBinder flinger = ServiceManager.getService("SurfaceFlinger");
410            if (flinger != null) {
411                Parcel data = Parcel.obtain();
412                data.writeInterfaceToken("android.ui.ISurfaceComposer");
413                final int showUpdates = mShowScreenUpdates.isChecked() ? 1 : 0;
414                data.writeInt(showUpdates);
415                flinger.transact(1002, data, null, 0);
416                data.recycle();
417
418                updateFlingerOptions();
419            }
420        } catch (RemoteException ex) {
421        }
422    }
423
424    private void writeDisableOverlaysOption() {
425        try {
426            IBinder flinger = ServiceManager.getService("SurfaceFlinger");
427            if (flinger != null) {
428                Parcel data = Parcel.obtain();
429                data.writeInterfaceToken("android.ui.ISurfaceComposer");
430                final int disableOverlays = mDisableOverlays.isChecked() ? 1 : 0;
431                data.writeInt(disableOverlays);
432                flinger.transact(1008, data, null, 0);
433                data.recycle();
434
435                updateFlingerOptions();
436            }
437        } catch (RemoteException ex) {
438        }
439    }
440
441    private void updateHardwareUiOptions() {
442        mForceHardwareUi.setChecked(SystemProperties.getBoolean(HARDWARE_UI_PROPERTY, false));
443    }
444
445    private void writeHardwareUiOptions() {
446        SystemProperties.set(HARDWARE_UI_PROPERTY, mForceHardwareUi.isChecked() ? "true" : "false");
447    }
448
449    private void updateCpuUsageOptions() {
450        mShowCpuUsage.setChecked(Settings.System.getInt(getActivity().getContentResolver(),
451                Settings.System.SHOW_PROCESSES, 0) != 0);
452    }
453
454    private void writeCpuUsageOptions() {
455        boolean value = mShowCpuUsage.isChecked();
456        Settings.System.putInt(getActivity().getContentResolver(),
457                Settings.System.SHOW_PROCESSES, value ? 1 : 0);
458        Intent service = (new Intent())
459                .setClassName("com.android.systemui", "com.android.systemui.LoadAverageService");
460        if (value) {
461            getActivity().startService(service);
462        } else {
463            getActivity().stopService(service);
464        }
465    }
466
467    private void writeImmediatelyDestroyActivitiesOptions() {
468        try {
469            ActivityManagerNative.getDefault().setAlwaysFinish(
470                    mImmediatelyDestroyActivities.isChecked());
471        } catch (RemoteException ex) {
472        }
473    }
474
475    private void updateImmediatelyDestroyActivitiesOptions() {
476        mImmediatelyDestroyActivities.setChecked(Settings.System.getInt(
477            getActivity().getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0);
478    }
479
480    private void updateAnimationScaleValue(int which, ListPreference pref) {
481        try {
482            float scale = mWindowManager.getAnimationScale(which);
483            CharSequence[] values = pref.getEntryValues();
484            for (int i=0; i<values.length; i++) {
485                float val = Float.parseFloat(values[i].toString());
486                if (scale <= val) {
487                    pref.setValueIndex(i);
488                    pref.setSummary(pref.getEntries()[i]);
489                    return;
490                }
491            }
492            pref.setValueIndex(values.length-1);
493            pref.setSummary(pref.getEntries()[0]);
494        } catch (RemoteException e) {
495        }
496    }
497
498    private void updateAnimationScaleOptions() {
499        updateAnimationScaleValue(0, mWindowAnimationScale);
500        updateAnimationScaleValue(1, mTransitionAnimationScale);
501        updateAnimationScaleValue(2, mAnimatorDurationScale);
502    }
503
504    private void writeAnimationScaleOption(int which, ListPreference pref, Object newValue) {
505        try {
506            float scale = newValue != null ? Float.parseFloat(newValue.toString()) : 1;
507            mWindowManager.setAnimationScale(which, scale);
508            updateAnimationScaleValue(which, pref);
509        } catch (RemoteException e) {
510        }
511    }
512
513    private void updateAppProcessLimitOptions() {
514        try {
515            int limit = ActivityManagerNative.getDefault().getProcessLimit();
516            CharSequence[] values = mAppProcessLimit.getEntryValues();
517            for (int i=0; i<values.length; i++) {
518                int val = Integer.parseInt(values[i].toString());
519                if (val >= limit) {
520                    mAppProcessLimit.setValueIndex(i);
521                    mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[i]);
522                    return;
523                }
524            }
525            mAppProcessLimit.setValueIndex(0);
526            mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[0]);
527        } catch (RemoteException e) {
528        }
529    }
530
531    private void writeAppProcessLimitOptions(Object newValue) {
532        try {
533            int limit = newValue != null ? Integer.parseInt(newValue.toString()) : -1;
534            ActivityManagerNative.getDefault().setProcessLimit(limit);
535            updateAppProcessLimitOptions();
536        } catch (RemoteException e) {
537        }
538    }
539
540    private void writeShowAllANRsOptions() {
541        Settings.Secure.putInt(getActivity().getContentResolver(),
542                Settings.Secure.ANR_SHOW_BACKGROUND,
543                mShowAllANRs.isChecked() ? 1 : 0);
544    }
545
546    private void updateShowAllANRsOptions() {
547        mShowAllANRs.setChecked(Settings.Secure.getInt(
548            getActivity().getContentResolver(), Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0);
549    }
550
551    @Override
552    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
553        if (buttonView == mEnabledSwitch) {
554            if (isChecked != mLastEnabledState) {
555                if (isChecked) {
556                    mDialogClicked = false;
557                    if (mEnableDialog != null) dismissDialogs();
558                    mEnableDialog = new AlertDialog.Builder(getActivity()).setMessage(
559                            getActivity().getResources().getString(
560                                    R.string.dev_settings_warning_message))
561                            .setTitle(R.string.dev_settings_warning_title)
562                            .setIcon(android.R.drawable.ic_dialog_alert)
563                            .setPositiveButton(android.R.string.yes, this)
564                            .setNegativeButton(android.R.string.no, this)
565                            .show();
566                    mEnableDialog.setOnDismissListener(this);
567                } else {
568                    resetDangerousOptions();
569                    Settings.Secure.putInt(getActivity().getContentResolver(),
570                            Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED, 0);
571                    mLastEnabledState = isChecked;
572                    setPrefsEnabledState(mLastEnabledState);
573                }
574            }
575        }
576    }
577
578    @Override
579    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
580
581        if (Utils.isMonkeyRunning()) {
582            return false;
583        }
584
585        if (preference == mEnableAdb) {
586            if (mEnableAdb.isChecked()) {
587                mDialogClicked = false;
588                if (mAdbDialog != null) dismissDialogs();
589                mAdbDialog = new AlertDialog.Builder(getActivity()).setMessage(
590                        getActivity().getResources().getString(R.string.adb_warning_message))
591                        .setTitle(R.string.adb_warning_title)
592                        .setIcon(android.R.drawable.ic_dialog_alert)
593                        .setPositiveButton(android.R.string.yes, this)
594                        .setNegativeButton(android.R.string.no, this)
595                        .show();
596                mAdbDialog.setOnDismissListener(this);
597            } else {
598                Settings.Secure.putInt(getActivity().getContentResolver(),
599                        Settings.Secure.ADB_ENABLED, 0);
600            }
601        } else if (preference == mKeepScreenOn) {
602            Settings.System.putInt(getActivity().getContentResolver(),
603                    Settings.System.STAY_ON_WHILE_PLUGGED_IN,
604                    mKeepScreenOn.isChecked() ?
605                    (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);
606        } else if (preference == mAllowMockLocation) {
607            Settings.Secure.putInt(getActivity().getContentResolver(),
608                    Settings.Secure.ALLOW_MOCK_LOCATION,
609                    mAllowMockLocation.isChecked() ? 1 : 0);
610        } else if (preference == mStrictMode) {
611            writeStrictModeVisualOptions();
612        } else if (preference == mPointerLocation) {
613            writePointerLocationOptions();
614        } else if (preference == mShowTouches) {
615            writeShowTouchesOptions();
616        } else if (preference == mShowScreenUpdates) {
617            writeShowUpdatesOption();
618        } else if (preference == mDisableOverlays) {
619            writeDisableOverlaysOption();
620        } else if (preference == mShowCpuUsage) {
621            writeCpuUsageOptions();
622        } else if (preference == mImmediatelyDestroyActivities) {
623            writeImmediatelyDestroyActivitiesOptions();
624        } else if (preference == mShowAllANRs) {
625            writeShowAllANRsOptions();
626        } else if (preference == mForceHardwareUi) {
627            writeHardwareUiOptions();
628        }
629
630        return false;
631    }
632
633    @Override
634    public boolean onPreferenceChange(Preference preference, Object newValue) {
635        if (HDCP_CHECKING_KEY.equals(preference.getKey())) {
636            SystemProperties.set(HDCP_CHECKING_PROPERTY, newValue.toString());
637            updateHdcpValues();
638            return true;
639        } else if (preference == mWindowAnimationScale) {
640            writeAnimationScaleOption(0, mWindowAnimationScale, newValue);
641            return true;
642        } else if (preference == mTransitionAnimationScale) {
643            writeAnimationScaleOption(1, mTransitionAnimationScale, newValue);
644            return true;
645        } else if (preference == mAnimatorDurationScale) {
646            writeAnimationScaleOption(2, mAnimatorDurationScale, newValue);
647            return true;
648        } else if (preference == mAppProcessLimit) {
649            writeAppProcessLimitOptions(newValue);
650            return true;
651        }
652        return false;
653    }
654
655    private void dismissDialogs() {
656        if (mAdbDialog != null) {
657            mAdbDialog.dismiss();
658            mAdbDialog = null;
659        }
660        if (mEnableDialog != null) {
661            mEnableDialog.dismiss();
662            mEnableDialog = null;
663        }
664    }
665
666    public void onClick(DialogInterface dialog, int which) {
667        if (dialog == mAdbDialog) {
668            if (which == DialogInterface.BUTTON_POSITIVE) {
669                mDialogClicked = true;
670                Settings.Secure.putInt(getActivity().getContentResolver(),
671                        Settings.Secure.ADB_ENABLED, 1);
672            } else {
673                // Reset the toggle
674                mEnableAdb.setChecked(false);
675            }
676        } else if (dialog == mEnableDialog) {
677            if (which == DialogInterface.BUTTON_POSITIVE) {
678                mDialogClicked = true;
679                Settings.Secure.putInt(getActivity().getContentResolver(),
680                        Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED, 1);
681                mLastEnabledState = true;
682                setPrefsEnabledState(mLastEnabledState);
683            } else {
684                // Reset the toggle
685                mEnabledSwitch.setChecked(false);
686            }
687        }
688    }
689
690    public void onDismiss(DialogInterface dialog) {
691        // Assuming that onClick gets called first
692        if (dialog == mAdbDialog) {
693            if (!mDialogClicked) {
694                mEnableAdb.setChecked(false);
695            }
696            mAdbDialog = null;
697        } else if (dialog == mEnableDialog) {
698            if (!mDialogClicked) {
699                mEnabledSwitch.setChecked(false);
700            }
701            mEnableDialog = null;
702        }
703    }
704
705    @Override
706    public void onDestroy() {
707        dismissDialogs();
708        super.onDestroy();
709    }
710}
711