Home | History | Annotate | Download | only in settings
      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.ActionBar;
     20 import android.app.Activity;
     21 import android.app.ActivityManagerNative;
     22 import android.app.AlertDialog;
     23 import android.app.Dialog;
     24 import android.app.admin.DevicePolicyManager;
     25 import android.app.backup.IBackupManager;
     26 import android.bluetooth.BluetoothAdapter;
     27 import android.content.ContentResolver;
     28 import android.content.Context;
     29 import android.content.DialogInterface;
     30 import android.content.DialogInterface.OnClickListener;
     31 import android.content.Intent;
     32 import android.content.pm.ApplicationInfo;
     33 import android.content.pm.PackageManager;
     34 import android.content.pm.PackageManager.NameNotFoundException;
     35 import android.content.pm.ResolveInfo;
     36 import android.hardware.usb.IUsbManager;
     37 import android.os.AsyncTask;
     38 import android.os.BatteryManager;
     39 import android.os.Build;
     40 import android.os.Bundle;
     41 import android.os.IBinder;
     42 import android.os.Parcel;
     43 import android.os.PowerManager;
     44 import android.os.RemoteException;
     45 import android.os.ServiceManager;
     46 import android.os.StrictMode;
     47 import android.os.SystemProperties;
     48 import android.os.UserHandle;
     49 import android.preference.CheckBoxPreference;
     50 import android.preference.ListPreference;
     51 import android.preference.Preference;
     52 import android.preference.Preference.OnPreferenceChangeListener;
     53 import android.preference.PreferenceGroup;
     54 import android.preference.PreferenceScreen;
     55 import android.provider.Settings;
     56 import android.text.TextUtils;
     57 import android.util.Log;
     58 import android.view.Gravity;
     59 import android.view.HardwareRenderer;
     60 import android.view.IWindowManager;
     61 import android.view.View;
     62 import android.widget.CompoundButton;
     63 import android.widget.Switch;
     64 import android.widget.TextView;
     65 
     66 import dalvik.system.VMRuntime;
     67 
     68 import java.io.File;
     69 import java.util.ArrayList;
     70 import java.util.HashSet;
     71 import java.util.List;
     72 
     73 /*
     74  * Displays preferences for application developers.
     75  */
     76 public class DevelopmentSettings extends RestrictedSettingsFragment
     77         implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
     78                 OnPreferenceChangeListener, CompoundButton.OnCheckedChangeListener {
     79     private static final String TAG = "DevelopmentSettings";
     80 
     81     /**
     82      * Preference file were development settings prefs are stored.
     83      */
     84     public static final String PREF_FILE = "development";
     85 
     86     /**
     87      * Whether to show the development settings to the user.  Default is false.
     88      */
     89     public static final String PREF_SHOW = "show";
     90 
     91     private static final String ENABLE_ADB = "enable_adb";
     92     private static final String CLEAR_ADB_KEYS = "clear_adb_keys";
     93     private static final String ENABLE_TERMINAL = "enable_terminal";
     94     private static final String KEEP_SCREEN_ON = "keep_screen_on";
     95     private static final String BT_HCI_SNOOP_LOG = "bt_hci_snoop_log";
     96     private static final String SELECT_RUNTIME_KEY = "select_runtime";
     97     private static final String SELECT_RUNTIME_PROPERTY = "persist.sys.dalvik.vm.lib";
     98     private static final String ALLOW_MOCK_LOCATION = "allow_mock_location";
     99     private static final String HDCP_CHECKING_KEY = "hdcp_checking";
    100     private static final String HDCP_CHECKING_PROPERTY = "persist.sys.hdcp_checking";
    101     private static final String LOCAL_BACKUP_PASSWORD = "local_backup_password";
    102     private static final String HARDWARE_UI_PROPERTY = "persist.sys.ui.hw";
    103     private static final String MSAA_PROPERTY = "debug.egl.force_msaa";
    104     private static final String BUGREPORT = "bugreport";
    105     private static final String BUGREPORT_IN_POWER_KEY = "bugreport_in_power";
    106     private static final String OPENGL_TRACES_PROPERTY = "debug.egl.trace";
    107 
    108     private static final String DEBUG_APP_KEY = "debug_app";
    109     private static final String WAIT_FOR_DEBUGGER_KEY = "wait_for_debugger";
    110     private static final String VERIFY_APPS_OVER_USB_KEY = "verify_apps_over_usb";
    111     private static final String STRICT_MODE_KEY = "strict_mode";
    112     private static final String POINTER_LOCATION_KEY = "pointer_location";
    113     private static final String SHOW_TOUCHES_KEY = "show_touches";
    114     private static final String SHOW_SCREEN_UPDATES_KEY = "show_screen_updates";
    115     private static final String DISABLE_OVERLAYS_KEY = "disable_overlays";
    116     private static final String SHOW_CPU_USAGE_KEY = "show_cpu_usage";
    117     private static final String FORCE_HARDWARE_UI_KEY = "force_hw_ui";
    118     private static final String FORCE_MSAA_KEY = "force_msaa";
    119     private static final String TRACK_FRAME_TIME_KEY = "track_frame_time";
    120     private static final String SHOW_NON_RECTANGULAR_CLIP_KEY = "show_non_rect_clip";
    121     private static final String SHOW_HW_SCREEN_UPDATES_KEY = "show_hw_screen_udpates";
    122     private static final String SHOW_HW_LAYERS_UPDATES_KEY = "show_hw_layers_udpates";
    123     private static final String DEBUG_HW_OVERDRAW_KEY = "debug_hw_overdraw";
    124     private static final String DEBUG_LAYOUT_KEY = "debug_layout";
    125     private static final String FORCE_RTL_LAYOUT_KEY = "force_rtl_layout_all_locales";
    126     private static final String WINDOW_ANIMATION_SCALE_KEY = "window_animation_scale";
    127     private static final String TRANSITION_ANIMATION_SCALE_KEY = "transition_animation_scale";
    128     private static final String ANIMATOR_DURATION_SCALE_KEY = "animator_duration_scale";
    129     private static final String OVERLAY_DISPLAY_DEVICES_KEY = "overlay_display_devices";
    130     private static final String DEBUG_DEBUGGING_CATEGORY_KEY = "debug_debugging_category";
    131     private static final String DEBUG_APPLICATIONS_CATEGORY_KEY = "debug_applications_category";
    132     private static final String WIFI_DISPLAY_CERTIFICATION_KEY = "wifi_display_certification";
    133 
    134     private static final String OPENGL_TRACES_KEY = "enable_opengl_traces";
    135 
    136     private static final String IMMEDIATELY_DESTROY_ACTIVITIES_KEY
    137             = "immediately_destroy_activities";
    138     private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit";
    139 
    140     private static final String SHOW_ALL_ANRS_KEY = "show_all_anrs";
    141 
    142     private static final String TAG_CONFIRM_ENFORCE = "confirm_enforce";
    143 
    144     private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
    145 
    146     private static final String TERMINAL_APP_PACKAGE = "com.android.terminal";
    147 
    148     private static final int RESULT_DEBUG_APP = 1000;
    149 
    150     private IWindowManager mWindowManager;
    151     private IBackupManager mBackupManager;
    152     private DevicePolicyManager mDpm;
    153 
    154     private Switch mEnabledSwitch;
    155     private boolean mLastEnabledState;
    156     private boolean mHaveDebugSettings;
    157     private boolean mDontPokeProperties;
    158 
    159     private CheckBoxPreference mEnableAdb;
    160     private Preference mClearAdbKeys;
    161     private CheckBoxPreference mEnableTerminal;
    162     private Preference mBugreport;
    163     private CheckBoxPreference mBugreportInPower;
    164     private CheckBoxPreference mKeepScreenOn;
    165     private CheckBoxPreference mBtHciSnoopLog;
    166     private CheckBoxPreference mAllowMockLocation;
    167     private PreferenceScreen mPassword;
    168 
    169     private String mDebugApp;
    170     private Preference mDebugAppPref;
    171     private CheckBoxPreference mWaitForDebugger;
    172     private CheckBoxPreference mVerifyAppsOverUsb;
    173     private CheckBoxPreference mWifiDisplayCertification;
    174 
    175     private CheckBoxPreference mStrictMode;
    176     private CheckBoxPreference mPointerLocation;
    177     private CheckBoxPreference mShowTouches;
    178     private CheckBoxPreference mShowScreenUpdates;
    179     private CheckBoxPreference mDisableOverlays;
    180     private CheckBoxPreference mShowCpuUsage;
    181     private CheckBoxPreference mForceHardwareUi;
    182     private CheckBoxPreference mForceMsaa;
    183     private CheckBoxPreference mShowHwScreenUpdates;
    184     private CheckBoxPreference mShowHwLayersUpdates;
    185     private CheckBoxPreference mDebugLayout;
    186     private CheckBoxPreference mForceRtlLayout;
    187     private ListPreference mDebugHwOverdraw;
    188     private ListPreference mTrackFrameTime;
    189     private ListPreference mShowNonRectClip;
    190     private ListPreference mWindowAnimationScale;
    191     private ListPreference mTransitionAnimationScale;
    192     private ListPreference mAnimatorDurationScale;
    193     private ListPreference mOverlayDisplayDevices;
    194     private ListPreference mOpenGLTraces;
    195 
    196     private CheckBoxPreference mImmediatelyDestroyActivities;
    197     private ListPreference mAppProcessLimit;
    198 
    199     private CheckBoxPreference mShowAllANRs;
    200 
    201     private final ArrayList<Preference> mAllPrefs = new ArrayList<Preference>();
    202     private final ArrayList<CheckBoxPreference> mResetCbPrefs
    203             = new ArrayList<CheckBoxPreference>();
    204 
    205     private final HashSet<Preference> mDisabledPrefs = new HashSet<Preference>();
    206 
    207     // To track whether a confirmation dialog was clicked.
    208     private boolean mDialogClicked;
    209     private Dialog mEnableDialog;
    210     private Dialog mAdbDialog;
    211     private Dialog mAdbKeysDialog;
    212 
    213     private boolean mUnavailable;
    214 
    215     public DevelopmentSettings() {
    216         super(RESTRICTIONS_PIN_SET);
    217     }
    218 
    219     @Override
    220     public void onCreate(Bundle icicle) {
    221         super.onCreate(icicle);
    222 
    223         mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
    224         mBackupManager = IBackupManager.Stub.asInterface(
    225                 ServiceManager.getService(Context.BACKUP_SERVICE));
    226         mDpm = (DevicePolicyManager)getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
    227 
    228         if (android.os.Process.myUserHandle().getIdentifier() != UserHandle.USER_OWNER) {
    229             mUnavailable = true;
    230             setPreferenceScreen(new PreferenceScreen(getActivity(), null));
    231             return;
    232         }
    233 
    234         addPreferencesFromResource(R.xml.development_prefs);
    235 
    236         final PreferenceGroup debugDebuggingCategory = (PreferenceGroup)
    237                 findPreference(DEBUG_DEBUGGING_CATEGORY_KEY);
    238 
    239         mEnableAdb = findAndInitCheckboxPref(ENABLE_ADB);
    240         mClearAdbKeys = findPreference(CLEAR_ADB_KEYS);
    241         if (!SystemProperties.getBoolean("ro.adb.secure", false)) {
    242             if (debugDebuggingCategory != null) {
    243                 debugDebuggingCategory.removePreference(mClearAdbKeys);
    244             }
    245         }
    246         mEnableTerminal = findAndInitCheckboxPref(ENABLE_TERMINAL);
    247         if (!isPackageInstalled(getActivity(), TERMINAL_APP_PACKAGE)) {
    248             debugDebuggingCategory.removePreference(mEnableTerminal);
    249             mEnableTerminal = null;
    250         }
    251 
    252         mBugreport = findPreference(BUGREPORT);
    253         mBugreportInPower = findAndInitCheckboxPref(BUGREPORT_IN_POWER_KEY);
    254         mKeepScreenOn = findAndInitCheckboxPref(KEEP_SCREEN_ON);
    255         mBtHciSnoopLog = findAndInitCheckboxPref(BT_HCI_SNOOP_LOG);
    256         mAllowMockLocation = findAndInitCheckboxPref(ALLOW_MOCK_LOCATION);
    257         mPassword = (PreferenceScreen) findPreference(LOCAL_BACKUP_PASSWORD);
    258         mAllPrefs.add(mPassword);
    259 
    260         if (!android.os.Process.myUserHandle().equals(UserHandle.OWNER)) {
    261             disableForUser(mEnableAdb);
    262             disableForUser(mClearAdbKeys);
    263             disableForUser(mEnableTerminal);
    264             disableForUser(mPassword);
    265         }
    266 
    267         mDebugAppPref = findPreference(DEBUG_APP_KEY);
    268         mAllPrefs.add(mDebugAppPref);
    269         mWaitForDebugger = findAndInitCheckboxPref(WAIT_FOR_DEBUGGER_KEY);
    270         mVerifyAppsOverUsb = findAndInitCheckboxPref(VERIFY_APPS_OVER_USB_KEY);
    271         if (!showVerifierSetting()) {
    272             if (debugDebuggingCategory != null) {
    273                 debugDebuggingCategory.removePreference(mVerifyAppsOverUsb);
    274             } else {
    275                 mVerifyAppsOverUsb.setEnabled(false);
    276             }
    277         }
    278         mStrictMode = findAndInitCheckboxPref(STRICT_MODE_KEY);
    279         mPointerLocation = findAndInitCheckboxPref(POINTER_LOCATION_KEY);
    280         mShowTouches = findAndInitCheckboxPref(SHOW_TOUCHES_KEY);
    281         mShowScreenUpdates = findAndInitCheckboxPref(SHOW_SCREEN_UPDATES_KEY);
    282         mDisableOverlays = findAndInitCheckboxPref(DISABLE_OVERLAYS_KEY);
    283         mShowCpuUsage = findAndInitCheckboxPref(SHOW_CPU_USAGE_KEY);
    284         mForceHardwareUi = findAndInitCheckboxPref(FORCE_HARDWARE_UI_KEY);
    285         mForceMsaa = findAndInitCheckboxPref(FORCE_MSAA_KEY);
    286         mTrackFrameTime = addListPreference(TRACK_FRAME_TIME_KEY);
    287         mShowNonRectClip = addListPreference(SHOW_NON_RECTANGULAR_CLIP_KEY);
    288         mShowHwScreenUpdates = findAndInitCheckboxPref(SHOW_HW_SCREEN_UPDATES_KEY);
    289         mShowHwLayersUpdates = findAndInitCheckboxPref(SHOW_HW_LAYERS_UPDATES_KEY);
    290         mDebugLayout = findAndInitCheckboxPref(DEBUG_LAYOUT_KEY);
    291         mForceRtlLayout = findAndInitCheckboxPref(FORCE_RTL_LAYOUT_KEY);
    292         mDebugHwOverdraw = addListPreference(DEBUG_HW_OVERDRAW_KEY);
    293         mWifiDisplayCertification = findAndInitCheckboxPref(WIFI_DISPLAY_CERTIFICATION_KEY);
    294         mWindowAnimationScale = addListPreference(WINDOW_ANIMATION_SCALE_KEY);
    295         mTransitionAnimationScale = addListPreference(TRANSITION_ANIMATION_SCALE_KEY);
    296         mAnimatorDurationScale = addListPreference(ANIMATOR_DURATION_SCALE_KEY);
    297         mOverlayDisplayDevices = addListPreference(OVERLAY_DISPLAY_DEVICES_KEY);
    298         mOpenGLTraces = addListPreference(OPENGL_TRACES_KEY);
    299 
    300         mImmediatelyDestroyActivities = (CheckBoxPreference) findPreference(
    301                 IMMEDIATELY_DESTROY_ACTIVITIES_KEY);
    302         mAllPrefs.add(mImmediatelyDestroyActivities);
    303         mResetCbPrefs.add(mImmediatelyDestroyActivities);
    304         mAppProcessLimit = addListPreference(APP_PROCESS_LIMIT_KEY);
    305 
    306         mShowAllANRs = (CheckBoxPreference) findPreference(
    307                 SHOW_ALL_ANRS_KEY);
    308         mAllPrefs.add(mShowAllANRs);
    309         mResetCbPrefs.add(mShowAllANRs);
    310 
    311         Preference selectRuntime = findPreference(SELECT_RUNTIME_KEY);
    312         if (selectRuntime != null) {
    313             mAllPrefs.add(selectRuntime);
    314             filterRuntimeOptions(selectRuntime);
    315         }
    316 
    317         Preference hdcpChecking = findPreference(HDCP_CHECKING_KEY);
    318         if (hdcpChecking != null) {
    319             mAllPrefs.add(hdcpChecking);
    320             removePreferenceForProduction(hdcpChecking);
    321         }
    322     }
    323 
    324     private ListPreference addListPreference(String prefKey) {
    325         ListPreference pref = (ListPreference) findPreference(prefKey);
    326         mAllPrefs.add(pref);
    327         pref.setOnPreferenceChangeListener(this);
    328         return pref;
    329     }
    330 
    331     private void disableForUser(Preference pref) {
    332         if (pref != null) {
    333             pref.setEnabled(false);
    334             mDisabledPrefs.add(pref);
    335         }
    336     }
    337 
    338     private CheckBoxPreference findAndInitCheckboxPref(String key) {
    339         CheckBoxPreference pref = (CheckBoxPreference) findPreference(key);
    340         if (pref == null) {
    341             throw new IllegalArgumentException("Cannot find preference with key = " + key);
    342         }
    343         mAllPrefs.add(pref);
    344         mResetCbPrefs.add(pref);
    345         return pref;
    346     }
    347 
    348     @Override
    349     public void onActivityCreated(Bundle savedInstanceState) {
    350         super.onActivityCreated(savedInstanceState);
    351 
    352         final Activity activity = getActivity();
    353         mEnabledSwitch = new Switch(activity);
    354 
    355         final int padding = activity.getResources().getDimensionPixelSize(
    356                 R.dimen.action_bar_switch_padding);
    357         mEnabledSwitch.setPaddingRelative(0, 0, padding, 0);
    358         if (mUnavailable) {
    359             mEnabledSwitch.setEnabled(false);
    360             return;
    361         }
    362         mEnabledSwitch.setOnCheckedChangeListener(this);
    363     }
    364 
    365     @Override
    366     public void onStart() {
    367         super.onStart();
    368         final Activity activity = getActivity();
    369         activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
    370                 ActionBar.DISPLAY_SHOW_CUSTOM);
    371         activity.getActionBar().setCustomView(mEnabledSwitch, new ActionBar.LayoutParams(
    372                 ActionBar.LayoutParams.WRAP_CONTENT,
    373                 ActionBar.LayoutParams.WRAP_CONTENT,
    374                 Gravity.CENTER_VERTICAL | Gravity.END));
    375     }
    376 
    377     @Override
    378     public void onStop() {
    379         super.onStop();
    380         final Activity activity = getActivity();
    381         activity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
    382         activity.getActionBar().setCustomView(null);
    383     }
    384 
    385     private boolean removePreferenceForProduction(Preference preference) {
    386         if ("user".equals(Build.TYPE)) {
    387             removePreference(preference);
    388             return true;
    389         }
    390         return false;
    391     }
    392 
    393     private void removePreference(Preference preference) {
    394         getPreferenceScreen().removePreference(preference);
    395         mAllPrefs.remove(preference);
    396     }
    397 
    398     private void setPrefsEnabledState(boolean enabled) {
    399         for (int i = 0; i < mAllPrefs.size(); i++) {
    400             Preference pref = mAllPrefs.get(i);
    401             pref.setEnabled(enabled && !mDisabledPrefs.contains(pref));
    402         }
    403         updateAllOptions();
    404     }
    405 
    406     @Override
    407     public void onResume() {
    408         super.onResume();
    409 
    410         if (mUnavailable) {
    411             // Show error message
    412             TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
    413             getListView().setEmptyView(emptyView);
    414             if (emptyView != null) {
    415                 emptyView.setText(R.string.development_settings_not_available);
    416             }
    417             return;
    418         }
    419 
    420         if (mDpm.getMaximumTimeToLock(null) > 0) {
    421             // A DeviceAdmin has specified a maximum time until the device
    422             // will lock...  in this case we can't allow the user to turn
    423             // on "stay awake when plugged in" because that would defeat the
    424             // restriction.
    425             mDisabledPrefs.add(mKeepScreenOn);
    426         } else {
    427             mDisabledPrefs.remove(mKeepScreenOn);
    428         }
    429 
    430         final ContentResolver cr = getActivity().getContentResolver();
    431         mLastEnabledState = Settings.Global.getInt(cr,
    432                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
    433         mEnabledSwitch.setChecked(mLastEnabledState);
    434         setPrefsEnabledState(mLastEnabledState);
    435 
    436         if (mHaveDebugSettings && !mLastEnabledState) {
    437             // Overall debugging is disabled, but there are some debug
    438             // settings that are enabled.  This is an invalid state.  Switch
    439             // to debug settings being enabled, so the user knows there is
    440             // stuff enabled and can turn it all off if they want.
    441             Settings.Global.putInt(getActivity().getContentResolver(),
    442                     Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
    443             mLastEnabledState = true;
    444             mEnabledSwitch.setChecked(mLastEnabledState);
    445             setPrefsEnabledState(mLastEnabledState);
    446         }
    447     }
    448 
    449     void updateCheckBox(CheckBoxPreference checkBox, boolean value) {
    450         checkBox.setChecked(value);
    451         mHaveDebugSettings |= value;
    452     }
    453 
    454     private void updateAllOptions() {
    455         final Context context = getActivity();
    456         final ContentResolver cr = context.getContentResolver();
    457         mHaveDebugSettings = false;
    458         updateCheckBox(mEnableAdb, Settings.Global.getInt(cr,
    459                 Settings.Global.ADB_ENABLED, 0) != 0);
    460         if (mEnableTerminal != null) {
    461             updateCheckBox(mEnableTerminal,
    462                     context.getPackageManager().getApplicationEnabledSetting(TERMINAL_APP_PACKAGE)
    463                     == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
    464         }
    465         updateCheckBox(mBugreportInPower, Settings.Secure.getInt(cr,
    466                 Settings.Secure.BUGREPORT_IN_POWER_MENU, 0) != 0);
    467         updateCheckBox(mKeepScreenOn, Settings.Global.getInt(cr,
    468                 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0) != 0);
    469         updateCheckBox(mBtHciSnoopLog, Settings.Secure.getInt(cr,
    470                 Settings.Secure.BLUETOOTH_HCI_LOG, 0) != 0);
    471         updateCheckBox(mAllowMockLocation, Settings.Secure.getInt(cr,
    472                 Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0);
    473         updateRuntimeValue();
    474         updateHdcpValues();
    475         updatePasswordSummary();
    476         updateDebuggerOptions();
    477         updateStrictModeVisualOptions();
    478         updatePointerLocationOptions();
    479         updateShowTouchesOptions();
    480         updateFlingerOptions();
    481         updateCpuUsageOptions();
    482         updateHardwareUiOptions();
    483         updateMsaaOptions();
    484         updateTrackFrameTimeOptions();
    485         updateShowNonRectClipOptions();
    486         updateShowHwScreenUpdatesOptions();
    487         updateShowHwLayersUpdatesOptions();
    488         updateDebugHwOverdrawOptions();
    489         updateDebugLayoutOptions();
    490         updateAnimationScaleOptions();
    491         updateOverlayDisplayDevicesOptions();
    492         updateOpenGLTracesOptions();
    493         updateImmediatelyDestroyActivitiesOptions();
    494         updateAppProcessLimitOptions();
    495         updateShowAllANRsOptions();
    496         updateVerifyAppsOverUsbOptions();
    497         updateBugreportOptions();
    498         updateForceRtlOptions();
    499         updateWifiDisplayCertificationOptions();
    500     }
    501 
    502     private void resetDangerousOptions() {
    503         mDontPokeProperties = true;
    504         for (int i=0; i<mResetCbPrefs.size(); i++) {
    505             CheckBoxPreference cb = mResetCbPrefs.get(i);
    506             if (cb.isChecked()) {
    507                 cb.setChecked(false);
    508                 onPreferenceTreeClick(null, cb);
    509             }
    510         }
    511         resetDebuggerOptions();
    512         writeAnimationScaleOption(0, mWindowAnimationScale, null);
    513         writeAnimationScaleOption(1, mTransitionAnimationScale, null);
    514         writeAnimationScaleOption(2, mAnimatorDurationScale, null);
    515         writeOverlayDisplayDevicesOptions(null);
    516         writeAppProcessLimitOptions(null);
    517         mHaveDebugSettings = false;
    518         updateAllOptions();
    519         mDontPokeProperties = false;
    520         pokeSystemProperties();
    521     }
    522 
    523     void filterRuntimeOptions(Preference selectRuntime) {
    524         ListPreference pref = (ListPreference) selectRuntime;
    525         ArrayList<String> validValues = new ArrayList<String>();
    526         ArrayList<String> validSummaries = new ArrayList<String>();
    527         String[] values = getResources().getStringArray(R.array.select_runtime_values);
    528         String[] summaries = getResources().getStringArray(R.array.select_runtime_summaries);
    529         for (int i = 0; i < values.length; i++) {
    530             String value = values[i];
    531             String summary = summaries[i];
    532             if (new File("/system/lib/" + value).exists()) {
    533                 validValues.add(value);
    534                 validSummaries.add(summary);
    535             }
    536         }
    537         int count = validValues.size();
    538         if (count <= 1) {
    539             // no choices, so remove preference
    540             removePreference(selectRuntime);
    541         } else {
    542             pref.setEntryValues(validValues.toArray(new String[count]));
    543             pref.setEntries(validSummaries.toArray(new String[count]));
    544         }
    545     }
    546 
    547     private String currentRuntimeValue() {
    548         return SystemProperties.get(SELECT_RUNTIME_PROPERTY, VMRuntime.getRuntime().vmLibrary());
    549     }
    550 
    551     private void updateRuntimeValue() {
    552         ListPreference selectRuntime = (ListPreference) findPreference(SELECT_RUNTIME_KEY);
    553         if (selectRuntime != null) {
    554             String currentValue = currentRuntimeValue();
    555             String[] values = getResources().getStringArray(R.array.select_runtime_values);
    556             String[] summaries = getResources().getStringArray(R.array.select_runtime_summaries);
    557             int index = 0;
    558             for (int i = 0; i < values.length; i++) {
    559                 if (currentValue.equals(values[i])) {
    560                     index = i;
    561                     break;
    562                 }
    563             }
    564             selectRuntime.setValue(values[index]);
    565             selectRuntime.setSummary(summaries[index]);
    566             selectRuntime.setOnPreferenceChangeListener(this);
    567         }
    568     }
    569 
    570     private void updateHdcpValues() {
    571         ListPreference hdcpChecking = (ListPreference) findPreference(HDCP_CHECKING_KEY);
    572         if (hdcpChecking != null) {
    573             String currentValue = SystemProperties.get(HDCP_CHECKING_PROPERTY);
    574             String[] values = getResources().getStringArray(R.array.hdcp_checking_values);
    575             String[] summaries = getResources().getStringArray(R.array.hdcp_checking_summaries);
    576             int index = 1; // Defaults to drm-only. Needs to match with R.array.hdcp_checking_values
    577             for (int i = 0; i < values.length; i++) {
    578                 if (currentValue.equals(values[i])) {
    579                     index = i;
    580                     break;
    581                 }
    582             }
    583             hdcpChecking.setValue(values[index]);
    584             hdcpChecking.setSummary(summaries[index]);
    585             hdcpChecking.setOnPreferenceChangeListener(this);
    586         }
    587     }
    588 
    589     private void updatePasswordSummary() {
    590         try {
    591             if (mBackupManager.hasBackupPassword()) {
    592                 mPassword.setSummary(R.string.local_backup_password_summary_change);
    593             } else {
    594                 mPassword.setSummary(R.string.local_backup_password_summary_none);
    595             }
    596         } catch (RemoteException e) {
    597             // Not much we can do here
    598         }
    599     }
    600 
    601     private void writeBtHciSnoopLogOptions() {
    602         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    603         adapter.configHciSnoopLog(mBtHciSnoopLog.isChecked());
    604         Settings.Secure.putInt(getActivity().getContentResolver(),
    605                 Settings.Secure.BLUETOOTH_HCI_LOG,
    606                 mBtHciSnoopLog.isChecked() ? 1 : 0);
    607     }
    608 
    609     private void writeDebuggerOptions() {
    610         try {
    611             ActivityManagerNative.getDefault().setDebugApp(
    612                 mDebugApp, mWaitForDebugger.isChecked(), true);
    613         } catch (RemoteException ex) {
    614         }
    615     }
    616 
    617     private static void resetDebuggerOptions() {
    618         try {
    619             ActivityManagerNative.getDefault().setDebugApp(
    620                     null, false, true);
    621         } catch (RemoteException ex) {
    622         }
    623     }
    624 
    625     private void updateDebuggerOptions() {
    626         mDebugApp = Settings.Global.getString(
    627                 getActivity().getContentResolver(), Settings.Global.DEBUG_APP);
    628         updateCheckBox(mWaitForDebugger, Settings.Global.getInt(
    629                 getActivity().getContentResolver(), Settings.Global.WAIT_FOR_DEBUGGER, 0) != 0);
    630         if (mDebugApp != null && mDebugApp.length() > 0) {
    631             String label;
    632             try {
    633                 ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(mDebugApp,
    634                         PackageManager.GET_DISABLED_COMPONENTS);
    635                 CharSequence lab = getActivity().getPackageManager().getApplicationLabel(ai);
    636                 label = lab != null ? lab.toString() : mDebugApp;
    637             } catch (PackageManager.NameNotFoundException e) {
    638                 label = mDebugApp;
    639             }
    640             mDebugAppPref.setSummary(getResources().getString(R.string.debug_app_set, label));
    641             mWaitForDebugger.setEnabled(true);
    642             mHaveDebugSettings = true;
    643         } else {
    644             mDebugAppPref.setSummary(getResources().getString(R.string.debug_app_not_set));
    645             mWaitForDebugger.setEnabled(false);
    646         }
    647     }
    648 
    649     private void updateVerifyAppsOverUsbOptions() {
    650         updateCheckBox(mVerifyAppsOverUsb, Settings.Global.getInt(getActivity().getContentResolver(),
    651                 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, 1) != 0);
    652         mVerifyAppsOverUsb.setEnabled(enableVerifierSetting());
    653     }
    654 
    655     private void writeVerifyAppsOverUsbOptions() {
    656         Settings.Global.putInt(getActivity().getContentResolver(),
    657               Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, mVerifyAppsOverUsb.isChecked() ? 1 : 0);
    658     }
    659 
    660     private boolean enableVerifierSetting() {
    661         final ContentResolver cr = getActivity().getContentResolver();
    662         if (Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0) == 0) {
    663             return false;
    664         }
    665         if (Settings.Global.getInt(cr, Settings.Global.PACKAGE_VERIFIER_ENABLE, 1) == 0) {
    666             return false;
    667         } else {
    668             final PackageManager pm = getActivity().getPackageManager();
    669             final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION);
    670             verification.setType(PACKAGE_MIME_TYPE);
    671             verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    672             final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(verification, 0);
    673             if (receivers.size() == 0) {
    674                 return false;
    675             }
    676         }
    677         return true;
    678     }
    679 
    680     private boolean showVerifierSetting() {
    681         return Settings.Global.getInt(getActivity().getContentResolver(),
    682                 Settings.Global.PACKAGE_VERIFIER_SETTING_VISIBLE, 1) > 0;
    683     }
    684 
    685     private void updateBugreportOptions() {
    686         if ("user".equals(Build.TYPE)) {
    687             final ContentResolver resolver = getActivity().getContentResolver();
    688             final boolean adbEnabled = Settings.Global.getInt(
    689                     resolver, Settings.Global.ADB_ENABLED, 0) != 0;
    690             if (adbEnabled) {
    691                 mBugreport.setEnabled(true);
    692                 mBugreportInPower.setEnabled(true);
    693             } else {
    694                 mBugreport.setEnabled(false);
    695                 mBugreportInPower.setEnabled(false);
    696                 mBugreportInPower.setChecked(false);
    697                 Settings.Secure.putInt(resolver, Settings.Secure.BUGREPORT_IN_POWER_MENU, 0);
    698             }
    699         } else {
    700             mBugreportInPower.setEnabled(true);
    701         }
    702     }
    703 
    704     // Returns the current state of the system property that controls
    705     // strictmode flashes.  One of:
    706     //    0: not explicitly set one way or another
    707     //    1: on
    708     //    2: off
    709     private static int currentStrictModeActiveIndex() {
    710         if (TextUtils.isEmpty(SystemProperties.get(StrictMode.VISUAL_PROPERTY))) {
    711             return 0;
    712         }
    713         boolean enabled = SystemProperties.getBoolean(StrictMode.VISUAL_PROPERTY, false);
    714         return enabled ? 1 : 2;
    715     }
    716 
    717     private void writeStrictModeVisualOptions() {
    718         try {
    719             mWindowManager.setStrictModeVisualIndicatorPreference(mStrictMode.isChecked()
    720                     ? "1" : "");
    721         } catch (RemoteException e) {
    722         }
    723     }
    724 
    725     private void updateStrictModeVisualOptions() {
    726         updateCheckBox(mStrictMode, currentStrictModeActiveIndex() == 1);
    727     }
    728 
    729     private void writePointerLocationOptions() {
    730         Settings.System.putInt(getActivity().getContentResolver(),
    731                 Settings.System.POINTER_LOCATION, mPointerLocation.isChecked() ? 1 : 0);
    732     }
    733 
    734     private void updatePointerLocationOptions() {
    735         updateCheckBox(mPointerLocation, Settings.System.getInt(getActivity().getContentResolver(),
    736                 Settings.System.POINTER_LOCATION, 0) != 0);
    737     }
    738 
    739     private void writeShowTouchesOptions() {
    740         Settings.System.putInt(getActivity().getContentResolver(),
    741                 Settings.System.SHOW_TOUCHES, mShowTouches.isChecked() ? 1 : 0);
    742     }
    743 
    744     private void updateShowTouchesOptions() {
    745         updateCheckBox(mShowTouches, Settings.System.getInt(getActivity().getContentResolver(),
    746                 Settings.System.SHOW_TOUCHES, 0) != 0);
    747     }
    748 
    749     private void updateFlingerOptions() {
    750         // magic communication with surface flinger.
    751         try {
    752             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
    753             if (flinger != null) {
    754                 Parcel data = Parcel.obtain();
    755                 Parcel reply = Parcel.obtain();
    756                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
    757                 flinger.transact(1010, data, reply, 0);
    758                 @SuppressWarnings("unused")
    759                 int showCpu = reply.readInt();
    760                 @SuppressWarnings("unused")
    761                 int enableGL = reply.readInt();
    762                 int showUpdates = reply.readInt();
    763                 updateCheckBox(mShowScreenUpdates, showUpdates != 0);
    764                 @SuppressWarnings("unused")
    765                 int showBackground = reply.readInt();
    766                 int disableOverlays = reply.readInt();
    767                 updateCheckBox(mDisableOverlays, disableOverlays != 0);
    768                 reply.recycle();
    769                 data.recycle();
    770             }
    771         } catch (RemoteException ex) {
    772         }
    773     }
    774 
    775     private void writeShowUpdatesOption() {
    776         try {
    777             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
    778             if (flinger != null) {
    779                 Parcel data = Parcel.obtain();
    780                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
    781                 final int showUpdates = mShowScreenUpdates.isChecked() ? 1 : 0;
    782                 data.writeInt(showUpdates);
    783                 flinger.transact(1002, data, null, 0);
    784                 data.recycle();
    785 
    786                 updateFlingerOptions();
    787             }
    788         } catch (RemoteException ex) {
    789         }
    790     }
    791 
    792     private void writeDisableOverlaysOption() {
    793         try {
    794             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
    795             if (flinger != null) {
    796                 Parcel data = Parcel.obtain();
    797                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
    798                 final int disableOverlays = mDisableOverlays.isChecked() ? 1 : 0;
    799                 data.writeInt(disableOverlays);
    800                 flinger.transact(1008, data, null, 0);
    801                 data.recycle();
    802 
    803                 updateFlingerOptions();
    804             }
    805         } catch (RemoteException ex) {
    806         }
    807     }
    808 
    809     private void updateHardwareUiOptions() {
    810         updateCheckBox(mForceHardwareUi, SystemProperties.getBoolean(HARDWARE_UI_PROPERTY, false));
    811     }
    812 
    813     private void writeHardwareUiOptions() {
    814         SystemProperties.set(HARDWARE_UI_PROPERTY, mForceHardwareUi.isChecked() ? "true" : "false");
    815         pokeSystemProperties();
    816     }
    817 
    818     private void updateMsaaOptions() {
    819         updateCheckBox(mForceMsaa, SystemProperties.getBoolean(MSAA_PROPERTY, false));
    820     }
    821 
    822     private void writeMsaaOptions() {
    823         SystemProperties.set(MSAA_PROPERTY, mForceMsaa.isChecked() ? "true" : "false");
    824         pokeSystemProperties();
    825     }
    826 
    827     private void updateTrackFrameTimeOptions() {
    828         String value = SystemProperties.get(HardwareRenderer.PROFILE_PROPERTY);
    829         if (value == null) {
    830             value = "";
    831         }
    832 
    833         CharSequence[] values = mTrackFrameTime.getEntryValues();
    834         for (int i = 0; i < values.length; i++) {
    835             if (value.contentEquals(values[i])) {
    836                 mTrackFrameTime.setValueIndex(i);
    837                 mTrackFrameTime.setSummary(mTrackFrameTime.getEntries()[i]);
    838                 return;
    839             }
    840         }
    841         mTrackFrameTime.setValueIndex(0);
    842         mTrackFrameTime.setSummary(mTrackFrameTime.getEntries()[0]);
    843     }
    844 
    845     private void writeTrackFrameTimeOptions(Object newValue) {
    846         SystemProperties.set(HardwareRenderer.PROFILE_PROPERTY,
    847                 newValue == null ? "" : newValue.toString());
    848         pokeSystemProperties();
    849         updateTrackFrameTimeOptions();
    850     }
    851 
    852     private void updateShowNonRectClipOptions() {
    853         String value = SystemProperties.get(
    854                 HardwareRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY);
    855         if (value == null) {
    856             value = "hide";
    857         }
    858 
    859         CharSequence[] values = mShowNonRectClip.getEntryValues();
    860         for (int i = 0; i < values.length; i++) {
    861             if (value.contentEquals(values[i])) {
    862                 mShowNonRectClip.setValueIndex(i);
    863                 mShowNonRectClip.setSummary(mShowNonRectClip.getEntries()[i]);
    864                 return;
    865             }
    866         }
    867         mShowNonRectClip.setValueIndex(0);
    868         mShowNonRectClip.setSummary(mShowNonRectClip.getEntries()[0]);
    869     }
    870 
    871     private void writeShowNonRectClipOptions(Object newValue) {
    872         SystemProperties.set(HardwareRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY,
    873                 newValue == null ? "" : newValue.toString());
    874         pokeSystemProperties();
    875         updateShowNonRectClipOptions();
    876     }
    877 
    878     private void updateShowHwScreenUpdatesOptions() {
    879         updateCheckBox(mShowHwScreenUpdates,
    880                 SystemProperties.getBoolean(HardwareRenderer.DEBUG_DIRTY_REGIONS_PROPERTY, false));
    881     }
    882 
    883     private void writeShowHwScreenUpdatesOptions() {
    884         SystemProperties.set(HardwareRenderer.DEBUG_DIRTY_REGIONS_PROPERTY,
    885                 mShowHwScreenUpdates.isChecked() ? "true" : null);
    886         pokeSystemProperties();
    887     }
    888 
    889     private void updateShowHwLayersUpdatesOptions() {
    890         updateCheckBox(mShowHwLayersUpdates, SystemProperties.getBoolean(
    891                 HardwareRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY, false));
    892     }
    893 
    894     private void writeShowHwLayersUpdatesOptions() {
    895         SystemProperties.set(HardwareRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY,
    896                 mShowHwLayersUpdates.isChecked() ? "true" : null);
    897         pokeSystemProperties();
    898     }
    899 
    900     private void updateDebugHwOverdrawOptions() {
    901         String value = SystemProperties.get(HardwareRenderer.DEBUG_OVERDRAW_PROPERTY);
    902         if (value == null) {
    903             value = "";
    904         }
    905 
    906         CharSequence[] values = mDebugHwOverdraw.getEntryValues();
    907         for (int i = 0; i < values.length; i++) {
    908             if (value.contentEquals(values[i])) {
    909                 mDebugHwOverdraw.setValueIndex(i);
    910                 mDebugHwOverdraw.setSummary(mDebugHwOverdraw.getEntries()[i]);
    911                 return;
    912             }
    913         }
    914         mDebugHwOverdraw.setValueIndex(0);
    915         mDebugHwOverdraw.setSummary(mDebugHwOverdraw.getEntries()[0]);
    916     }
    917 
    918     private void writeDebugHwOverdrawOptions(Object newValue) {
    919         SystemProperties.set(HardwareRenderer.DEBUG_OVERDRAW_PROPERTY,
    920                 newValue == null ? "" : newValue.toString());
    921         pokeSystemProperties();
    922         updateDebugHwOverdrawOptions();
    923     }
    924 
    925     private void updateDebugLayoutOptions() {
    926         updateCheckBox(mDebugLayout,
    927                 SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false));
    928     }
    929 
    930     private void writeDebugLayoutOptions() {
    931         SystemProperties.set(View.DEBUG_LAYOUT_PROPERTY,
    932                 mDebugLayout.isChecked() ? "true" : "false");
    933         pokeSystemProperties();
    934     }
    935 
    936     private void updateForceRtlOptions() {
    937         updateCheckBox(mForceRtlLayout, Settings.Global.getInt(getActivity().getContentResolver(),
    938                 Settings.Global.DEVELOPMENT_FORCE_RTL, 0) != 0);
    939     }
    940 
    941     private void writeForceRtlOptions() {
    942         boolean value = mForceRtlLayout.isChecked();
    943         Settings.Global.putInt(getActivity().getContentResolver(),
    944                 Settings.Global.DEVELOPMENT_FORCE_RTL, value ? 1 : 0);
    945         SystemProperties.set(Settings.Global.DEVELOPMENT_FORCE_RTL, value ? "1" : "0");
    946         LocalePicker.updateLocale(getActivity().getResources().getConfiguration().locale);
    947     }
    948 
    949     private void updateWifiDisplayCertificationOptions() {
    950         updateCheckBox(mWifiDisplayCertification, Settings.Global.getInt(
    951                 getActivity().getContentResolver(),
    952                 Settings.Global.WIFI_DISPLAY_CERTIFICATION_ON, 0) != 0);
    953     }
    954 
    955     private void writeWifiDisplayCertificationOptions() {
    956         Settings.Global.putInt(getActivity().getContentResolver(),
    957                 Settings.Global.WIFI_DISPLAY_CERTIFICATION_ON,
    958                 mWifiDisplayCertification.isChecked() ? 1 : 0);
    959     }
    960 
    961     private void updateCpuUsageOptions() {
    962         updateCheckBox(mShowCpuUsage, Settings.Global.getInt(getActivity().getContentResolver(),
    963                 Settings.Global.SHOW_PROCESSES, 0) != 0);
    964     }
    965 
    966     private void writeCpuUsageOptions() {
    967         boolean value = mShowCpuUsage.isChecked();
    968         Settings.Global.putInt(getActivity().getContentResolver(),
    969                 Settings.Global.SHOW_PROCESSES, value ? 1 : 0);
    970         Intent service = (new Intent())
    971                 .setClassName("com.android.systemui", "com.android.systemui.LoadAverageService");
    972         if (value) {
    973             getActivity().startService(service);
    974         } else {
    975             getActivity().stopService(service);
    976         }
    977     }
    978 
    979     private void writeImmediatelyDestroyActivitiesOptions() {
    980         try {
    981             ActivityManagerNative.getDefault().setAlwaysFinish(
    982                     mImmediatelyDestroyActivities.isChecked());
    983         } catch (RemoteException ex) {
    984         }
    985     }
    986 
    987     private void updateImmediatelyDestroyActivitiesOptions() {
    988         updateCheckBox(mImmediatelyDestroyActivities, Settings.Global.getInt(
    989             getActivity().getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) != 0);
    990     }
    991 
    992     private void updateAnimationScaleValue(int which, ListPreference pref) {
    993         try {
    994             float scale = mWindowManager.getAnimationScale(which);
    995             if (scale != 1) {
    996                 mHaveDebugSettings = true;
    997             }
    998             CharSequence[] values = pref.getEntryValues();
    999             for (int i=0; i<values.length; i++) {
   1000                 float val = Float.parseFloat(values[i].toString());
   1001                 if (scale <= val) {
   1002                     pref.setValueIndex(i);
   1003                     pref.setSummary(pref.getEntries()[i]);
   1004                     return;
   1005                 }
   1006             }
   1007             pref.setValueIndex(values.length-1);
   1008             pref.setSummary(pref.getEntries()[0]);
   1009         } catch (RemoteException e) {
   1010         }
   1011     }
   1012 
   1013     private void updateAnimationScaleOptions() {
   1014         updateAnimationScaleValue(0, mWindowAnimationScale);
   1015         updateAnimationScaleValue(1, mTransitionAnimationScale);
   1016         updateAnimationScaleValue(2, mAnimatorDurationScale);
   1017     }
   1018 
   1019     private void writeAnimationScaleOption(int which, ListPreference pref, Object newValue) {
   1020         try {
   1021             float scale = newValue != null ? Float.parseFloat(newValue.toString()) : 1;
   1022             mWindowManager.setAnimationScale(which, scale);
   1023             updateAnimationScaleValue(which, pref);
   1024         } catch (RemoteException e) {
   1025         }
   1026     }
   1027 
   1028     private void updateOverlayDisplayDevicesOptions() {
   1029         String value = Settings.Global.getString(getActivity().getContentResolver(),
   1030                 Settings.Global.OVERLAY_DISPLAY_DEVICES);
   1031         if (value == null) {
   1032             value = "";
   1033         }
   1034 
   1035         CharSequence[] values = mOverlayDisplayDevices.getEntryValues();
   1036         for (int i = 0; i < values.length; i++) {
   1037             if (value.contentEquals(values[i])) {
   1038                 mOverlayDisplayDevices.setValueIndex(i);
   1039                 mOverlayDisplayDevices.setSummary(mOverlayDisplayDevices.getEntries()[i]);
   1040                 return;
   1041             }
   1042         }
   1043         mOverlayDisplayDevices.setValueIndex(0);
   1044         mOverlayDisplayDevices.setSummary(mOverlayDisplayDevices.getEntries()[0]);
   1045     }
   1046 
   1047     private void writeOverlayDisplayDevicesOptions(Object newValue) {
   1048         Settings.Global.putString(getActivity().getContentResolver(),
   1049                 Settings.Global.OVERLAY_DISPLAY_DEVICES, (String)newValue);
   1050         updateOverlayDisplayDevicesOptions();
   1051     }
   1052 
   1053     private void updateOpenGLTracesOptions() {
   1054         String value = SystemProperties.get(OPENGL_TRACES_PROPERTY);
   1055         if (value == null) {
   1056             value = "";
   1057         }
   1058 
   1059         CharSequence[] values = mOpenGLTraces.getEntryValues();
   1060         for (int i = 0; i < values.length; i++) {
   1061             if (value.contentEquals(values[i])) {
   1062                 mOpenGLTraces.setValueIndex(i);
   1063                 mOpenGLTraces.setSummary(mOpenGLTraces.getEntries()[i]);
   1064                 return;
   1065             }
   1066         }
   1067         mOpenGLTraces.setValueIndex(0);
   1068         mOpenGLTraces.setSummary(mOpenGLTraces.getEntries()[0]);
   1069     }
   1070 
   1071     private void writeOpenGLTracesOptions(Object newValue) {
   1072         SystemProperties.set(OPENGL_TRACES_PROPERTY, newValue == null ? "" : newValue.toString());
   1073         pokeSystemProperties();
   1074         updateOpenGLTracesOptions();
   1075     }
   1076 
   1077     private void updateAppProcessLimitOptions() {
   1078         try {
   1079             int limit = ActivityManagerNative.getDefault().getProcessLimit();
   1080             CharSequence[] values = mAppProcessLimit.getEntryValues();
   1081             for (int i=0; i<values.length; i++) {
   1082                 int val = Integer.parseInt(values[i].toString());
   1083                 if (val >= limit) {
   1084                     if (i != 0) {
   1085                         mHaveDebugSettings = true;
   1086                     }
   1087                     mAppProcessLimit.setValueIndex(i);
   1088                     mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[i]);
   1089                     return;
   1090                 }
   1091             }
   1092             mAppProcessLimit.setValueIndex(0);
   1093             mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[0]);
   1094         } catch (RemoteException e) {
   1095         }
   1096     }
   1097 
   1098     private void writeAppProcessLimitOptions(Object newValue) {
   1099         try {
   1100             int limit = newValue != null ? Integer.parseInt(newValue.toString()) : -1;
   1101             ActivityManagerNative.getDefault().setProcessLimit(limit);
   1102             updateAppProcessLimitOptions();
   1103         } catch (RemoteException e) {
   1104         }
   1105     }
   1106 
   1107     private void writeShowAllANRsOptions() {
   1108         Settings.Secure.putInt(getActivity().getContentResolver(),
   1109                 Settings.Secure.ANR_SHOW_BACKGROUND,
   1110                 mShowAllANRs.isChecked() ? 1 : 0);
   1111     }
   1112 
   1113     private void updateShowAllANRsOptions() {
   1114         updateCheckBox(mShowAllANRs, Settings.Secure.getInt(
   1115             getActivity().getContentResolver(), Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0);
   1116     }
   1117 
   1118     @Override
   1119     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   1120         if (buttonView == mEnabledSwitch) {
   1121             if (isChecked != mLastEnabledState) {
   1122                 if (isChecked) {
   1123                     mDialogClicked = false;
   1124                     if (mEnableDialog != null) dismissDialogs();
   1125                     mEnableDialog = new AlertDialog.Builder(getActivity()).setMessage(
   1126                             getActivity().getResources().getString(
   1127                                     R.string.dev_settings_warning_message))
   1128                             .setTitle(R.string.dev_settings_warning_title)
   1129                             .setIconAttribute(android.R.attr.alertDialogIcon)
   1130                             .setPositiveButton(android.R.string.yes, this)
   1131                             .setNegativeButton(android.R.string.no, this)
   1132                             .show();
   1133                     mEnableDialog.setOnDismissListener(this);
   1134                 } else {
   1135                     resetDangerousOptions();
   1136                     Settings.Global.putInt(getActivity().getContentResolver(),
   1137                             Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
   1138                     mLastEnabledState = isChecked;
   1139                     setPrefsEnabledState(mLastEnabledState);
   1140                 }
   1141             }
   1142         }
   1143     }
   1144 
   1145     @Override
   1146     public void onActivityResult(int requestCode, int resultCode, Intent data) {
   1147         if (requestCode == RESULT_DEBUG_APP) {
   1148             if (resultCode == Activity.RESULT_OK) {
   1149                 mDebugApp = data.getAction();
   1150                 writeDebuggerOptions();
   1151                 updateDebuggerOptions();
   1152             }
   1153         } else {
   1154             super.onActivityResult(requestCode, resultCode, data);
   1155         }
   1156     }
   1157 
   1158     @Override
   1159     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
   1160         if (Utils.isMonkeyRunning()) {
   1161             return false;
   1162         }
   1163 
   1164         if (preference == mEnableAdb) {
   1165             if (mEnableAdb.isChecked()) {
   1166                 mDialogClicked = false;
   1167                 if (mAdbDialog != null) dismissDialogs();
   1168                 mAdbDialog = new AlertDialog.Builder(getActivity()).setMessage(
   1169                         getActivity().getResources().getString(R.string.adb_warning_message))
   1170                         .setTitle(R.string.adb_warning_title)
   1171                         .setIconAttribute(android.R.attr.alertDialogIcon)
   1172                         .setPositiveButton(android.R.string.yes, this)
   1173                         .setNegativeButton(android.R.string.no, this)
   1174                         .show();
   1175                 mAdbDialog.setOnDismissListener(this);
   1176             } else {
   1177                 Settings.Global.putInt(getActivity().getContentResolver(),
   1178                         Settings.Global.ADB_ENABLED, 0);
   1179                 mVerifyAppsOverUsb.setEnabled(false);
   1180                 mVerifyAppsOverUsb.setChecked(false);
   1181                 updateBugreportOptions();
   1182             }
   1183         } else if (preference == mClearAdbKeys) {
   1184             if (mAdbKeysDialog != null) dismissDialogs();
   1185             mAdbKeysDialog = new AlertDialog.Builder(getActivity())
   1186                         .setMessage(R.string.adb_keys_warning_message)
   1187                         .setPositiveButton(android.R.string.ok, this)
   1188                         .setNegativeButton(android.R.string.cancel, null)
   1189                         .show();
   1190         } else if (preference == mEnableTerminal) {
   1191             final PackageManager pm = getActivity().getPackageManager();
   1192             pm.setApplicationEnabledSetting(TERMINAL_APP_PACKAGE,
   1193                     mEnableTerminal.isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
   1194                             : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
   1195         } else if (preference == mBugreportInPower) {
   1196             Settings.Secure.putInt(getActivity().getContentResolver(),
   1197                     Settings.Secure.BUGREPORT_IN_POWER_MENU,
   1198                     mBugreportInPower.isChecked() ? 1 : 0);
   1199         } else if (preference == mKeepScreenOn) {
   1200             Settings.Global.putInt(getActivity().getContentResolver(),
   1201                     Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
   1202                     mKeepScreenOn.isChecked() ?
   1203                     (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);
   1204         } else if (preference == mBtHciSnoopLog) {
   1205             writeBtHciSnoopLogOptions();
   1206         } else if (preference == mAllowMockLocation) {
   1207             Settings.Secure.putInt(getActivity().getContentResolver(),
   1208                     Settings.Secure.ALLOW_MOCK_LOCATION,
   1209                     mAllowMockLocation.isChecked() ? 1 : 0);
   1210         } else if (preference == mDebugAppPref) {
   1211             startActivityForResult(new Intent(getActivity(), AppPicker.class), RESULT_DEBUG_APP);
   1212         } else if (preference == mWaitForDebugger) {
   1213             writeDebuggerOptions();
   1214         } else if (preference == mVerifyAppsOverUsb) {
   1215             writeVerifyAppsOverUsbOptions();
   1216         } else if (preference == mStrictMode) {
   1217             writeStrictModeVisualOptions();
   1218         } else if (preference == mPointerLocation) {
   1219             writePointerLocationOptions();
   1220         } else if (preference == mShowTouches) {
   1221             writeShowTouchesOptions();
   1222         } else if (preference == mShowScreenUpdates) {
   1223             writeShowUpdatesOption();
   1224         } else if (preference == mDisableOverlays) {
   1225             writeDisableOverlaysOption();
   1226         } else if (preference == mShowCpuUsage) {
   1227             writeCpuUsageOptions();
   1228         } else if (preference == mImmediatelyDestroyActivities) {
   1229             writeImmediatelyDestroyActivitiesOptions();
   1230         } else if (preference == mShowAllANRs) {
   1231             writeShowAllANRsOptions();
   1232         } else if (preference == mForceHardwareUi) {
   1233             writeHardwareUiOptions();
   1234         } else if (preference == mForceMsaa) {
   1235             writeMsaaOptions();
   1236         } else if (preference == mShowHwScreenUpdates) {
   1237             writeShowHwScreenUpdatesOptions();
   1238         } else if (preference == mShowHwLayersUpdates) {
   1239             writeShowHwLayersUpdatesOptions();
   1240         } else if (preference == mDebugLayout) {
   1241             writeDebugLayoutOptions();
   1242         } else if (preference == mForceRtlLayout) {
   1243             writeForceRtlOptions();
   1244         } else if (preference == mWifiDisplayCertification) {
   1245             writeWifiDisplayCertificationOptions();
   1246         } else {
   1247             return super.onPreferenceTreeClick(preferenceScreen, preference);
   1248         }
   1249 
   1250         return false;
   1251     }
   1252 
   1253     @Override
   1254     public boolean onPreferenceChange(Preference preference, Object newValue) {
   1255         if (SELECT_RUNTIME_KEY.equals(preference.getKey())) {
   1256             final String oldRuntimeValue = VMRuntime.getRuntime().vmLibrary();
   1257             final String newRuntimeValue = newValue.toString();
   1258             if (!newRuntimeValue.equals(oldRuntimeValue)) {
   1259                 final Context context = getActivity();
   1260                 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
   1261                 builder.setMessage(context.getResources().getString(R.string.select_runtime_warning_message,
   1262                                                                     oldRuntimeValue, newRuntimeValue));
   1263                 builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
   1264                     @Override
   1265                     public void onClick(DialogInterface dialog, int which) {
   1266                         SystemProperties.set(SELECT_RUNTIME_PROPERTY, newRuntimeValue);
   1267                         pokeSystemProperties();
   1268                         PowerManager pm = (PowerManager)
   1269                                 context.getSystemService(Context.POWER_SERVICE);
   1270                         pm.reboot(null);
   1271                     }
   1272                 });
   1273                 builder.setNegativeButton(android.R.string.cancel, new OnClickListener() {
   1274                     @Override
   1275                     public void onClick(DialogInterface dialog, int which) {
   1276                         updateRuntimeValue();
   1277                     }
   1278                 });
   1279                 builder.show();
   1280             }
   1281             return true;
   1282         } else if (HDCP_CHECKING_KEY.equals(preference.getKey())) {
   1283             SystemProperties.set(HDCP_CHECKING_PROPERTY, newValue.toString());
   1284             updateHdcpValues();
   1285             pokeSystemProperties();
   1286             return true;
   1287         } else if (preference == mWindowAnimationScale) {
   1288             writeAnimationScaleOption(0, mWindowAnimationScale, newValue);
   1289             return true;
   1290         } else if (preference == mTransitionAnimationScale) {
   1291             writeAnimationScaleOption(1, mTransitionAnimationScale, newValue);
   1292             return true;
   1293         } else if (preference == mAnimatorDurationScale) {
   1294             writeAnimationScaleOption(2, mAnimatorDurationScale, newValue);
   1295             return true;
   1296         } else if (preference == mOverlayDisplayDevices) {
   1297             writeOverlayDisplayDevicesOptions(newValue);
   1298             return true;
   1299         } else if (preference == mOpenGLTraces) {
   1300             writeOpenGLTracesOptions(newValue);
   1301             return true;
   1302         } else if (preference == mTrackFrameTime) {
   1303             writeTrackFrameTimeOptions(newValue);
   1304             return true;
   1305         } else if (preference == mDebugHwOverdraw) {
   1306             writeDebugHwOverdrawOptions(newValue);
   1307             return true;
   1308         } else if (preference == mShowNonRectClip) {
   1309             writeShowNonRectClipOptions(newValue);
   1310             return true;
   1311         } else if (preference == mAppProcessLimit) {
   1312             writeAppProcessLimitOptions(newValue);
   1313             return true;
   1314         }
   1315         return false;
   1316     }
   1317 
   1318     private void dismissDialogs() {
   1319         if (mAdbDialog != null) {
   1320             mAdbDialog.dismiss();
   1321             mAdbDialog = null;
   1322         }
   1323         if (mAdbKeysDialog != null) {
   1324             mAdbKeysDialog.dismiss();
   1325             mAdbKeysDialog = null;
   1326         }
   1327         if (mEnableDialog != null) {
   1328             mEnableDialog.dismiss();
   1329             mEnableDialog = null;
   1330         }
   1331     }
   1332 
   1333     public void onClick(DialogInterface dialog, int which) {
   1334         if (dialog == mAdbDialog) {
   1335             if (which == DialogInterface.BUTTON_POSITIVE) {
   1336                 mDialogClicked = true;
   1337                 Settings.Global.putInt(getActivity().getContentResolver(),
   1338                         Settings.Global.ADB_ENABLED, 1);
   1339                 mVerifyAppsOverUsb.setEnabled(true);
   1340                 updateVerifyAppsOverUsbOptions();
   1341                 updateBugreportOptions();
   1342             } else {
   1343                 // Reset the toggle
   1344                 mEnableAdb.setChecked(false);
   1345             }
   1346         } else if (dialog == mAdbKeysDialog) {
   1347             if (which == DialogInterface.BUTTON_POSITIVE) {
   1348                 try {
   1349                     IBinder b = ServiceManager.getService(Context.USB_SERVICE);
   1350                     IUsbManager service = IUsbManager.Stub.asInterface(b);
   1351                     service.clearUsbDebuggingKeys();
   1352                 } catch (RemoteException e) {
   1353                     Log.e(TAG, "Unable to clear adb keys", e);
   1354                 }
   1355             }
   1356         } else if (dialog == mEnableDialog) {
   1357             if (which == DialogInterface.BUTTON_POSITIVE) {
   1358                 mDialogClicked = true;
   1359                 Settings.Global.putInt(getActivity().getContentResolver(),
   1360                         Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
   1361                 mLastEnabledState = true;
   1362                 setPrefsEnabledState(mLastEnabledState);
   1363             } else {
   1364                 // Reset the toggle
   1365                 mEnabledSwitch.setChecked(false);
   1366             }
   1367         }
   1368     }
   1369 
   1370     public void onDismiss(DialogInterface dialog) {
   1371         // Assuming that onClick gets called first
   1372         if (dialog == mAdbDialog) {
   1373             if (!mDialogClicked) {
   1374                 mEnableAdb.setChecked(false);
   1375             }
   1376             mAdbDialog = null;
   1377         } else if (dialog == mEnableDialog) {
   1378             if (!mDialogClicked) {
   1379                 mEnabledSwitch.setChecked(false);
   1380             }
   1381             mEnableDialog = null;
   1382         }
   1383     }
   1384 
   1385     @Override
   1386     public void onDestroy() {
   1387         dismissDialogs();
   1388         super.onDestroy();
   1389     }
   1390 
   1391     void pokeSystemProperties() {
   1392         if (!mDontPokeProperties) {
   1393             //noinspection unchecked
   1394             (new SystemPropPoker()).execute();
   1395         }
   1396     }
   1397 
   1398     static class SystemPropPoker extends AsyncTask<Void, Void, Void> {
   1399         @Override
   1400         protected Void doInBackground(Void... params) {
   1401             String[] services;
   1402             try {
   1403                 services = ServiceManager.listServices();
   1404             } catch (RemoteException e) {
   1405                 return null;
   1406             }
   1407             for (String service : services) {
   1408                 IBinder obj = ServiceManager.checkService(service);
   1409                 if (obj != null) {
   1410                     Parcel data = Parcel.obtain();
   1411                     try {
   1412                         obj.transact(IBinder.SYSPROPS_TRANSACTION, data, null, 0);
   1413                     } catch (RemoteException e) {
   1414                     } catch (Exception e) {
   1415                         Log.i(TAG, "Someone wrote a bad service '" + service
   1416                                 + "' that doesn't like to be poked: " + e);
   1417                     }
   1418                     data.recycle();
   1419                 }
   1420             }
   1421             return null;
   1422         }
   1423     }
   1424 
   1425     private static boolean isPackageInstalled(Context context, String packageName) {
   1426         try {
   1427             return context.getPackageManager().getPackageInfo(packageName, 0) != null;
   1428         } catch (NameNotFoundException e) {
   1429             return false;
   1430         }
   1431     }
   1432 }
   1433