OSDN Git Service

Merge "Made scanning settings searchable"
[android-x86/packages-apps-Settings.git] / src / com / android / settings / DevelopmentSettings.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.settings;
18
19 import android.app.Activity;
20 import android.app.ActivityManagerNative;
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.app.admin.DevicePolicyManager;
24 import android.app.backup.IBackupManager;
25 import android.bluetooth.BluetoothAdapter;
26 import android.content.BroadcastReceiver;
27 import android.content.ContentResolver;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.content.Intent;
31 import android.content.IntentFilter;
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.content.res.Resources;
37 import android.hardware.usb.IUsbManager;
38 import android.hardware.usb.UsbManager;
39 import android.net.wifi.WifiManager;
40 import android.os.AsyncTask;
41 import android.os.BatteryManager;
42 import android.os.Build;
43 import android.os.Bundle;
44 import android.os.IBinder;
45 import android.os.Parcel;
46 import android.os.RemoteException;
47 import android.os.ServiceManager;
48 import android.os.StrictMode;
49 import android.os.SystemProperties;
50 import android.os.UserHandle;
51 import android.os.UserManager;
52 import android.preference.ListPreference;
53 import android.preference.Preference;
54 import android.preference.Preference.OnPreferenceChangeListener;
55 import android.preference.PreferenceGroup;
56 import android.preference.PreferenceScreen;
57 import android.preference.SwitchPreference;
58 import android.provider.SearchIndexableResource;
59 import android.provider.Settings;
60 import android.text.TextUtils;
61 import android.util.Log;
62 import android.view.HardwareRenderer;
63 import android.view.IWindowManager;
64 import android.view.LayoutInflater;
65 import android.view.View;
66 import android.view.ViewGroup;
67 import android.view.accessibility.AccessibilityManager;
68 import android.widget.Switch;
69 import android.widget.TextView;
70
71 import com.android.internal.logging.MetricsLogger;
72 import com.android.settings.search.BaseSearchIndexProvider;
73 import com.android.settings.search.Indexable;
74 import com.android.settings.widget.SwitchBar;
75
76 import java.util.ArrayList;
77 import java.util.Arrays;
78 import java.util.HashSet;
79 import java.util.List;
80
81 /*
82  * Displays preferences for application developers.
83  */
84 public class DevelopmentSettings extends SettingsPreferenceFragment
85         implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
86                 OnPreferenceChangeListener, SwitchBar.OnSwitchChangeListener, Indexable {
87     private static final String TAG = "DevelopmentSettings";
88
89     /**
90      * Preference file were development settings prefs are stored.
91      */
92     public static final String PREF_FILE = "development";
93
94     /**
95      * Whether to show the development settings to the user.  Default is false.
96      */
97     public static final String PREF_SHOW = "show";
98
99     private static final String ENABLE_ADB = "enable_adb";
100     private static final String CLEAR_ADB_KEYS = "clear_adb_keys";
101     private static final String ENABLE_TERMINAL = "enable_terminal";
102     private static final String KEEP_SCREEN_ON = "keep_screen_on";
103     private static final String BT_HCI_SNOOP_LOG = "bt_hci_snoop_log";
104     private static final String ENABLE_OEM_UNLOCK = "oem_unlock_enable";
105     private static final String ALLOW_MOCK_LOCATION = "allow_mock_location";
106     private static final String HDCP_CHECKING_KEY = "hdcp_checking";
107     private static final String HDCP_CHECKING_PROPERTY = "persist.sys.hdcp_checking";
108     private static final String LOCAL_BACKUP_PASSWORD = "local_backup_password";
109     private static final String HARDWARE_UI_PROPERTY = "persist.sys.ui.hw";
110     private static final String MSAA_PROPERTY = "debug.egl.force_msaa";
111     private static final String BUGREPORT = "bugreport";
112     private static final String BUGREPORT_IN_POWER_KEY = "bugreport_in_power";
113     private static final String OPENGL_TRACES_PROPERTY = "debug.egl.trace";
114
115     private static final String DEBUG_APP_KEY = "debug_app";
116     private static final String WAIT_FOR_DEBUGGER_KEY = "wait_for_debugger";
117     private static final String VERIFY_APPS_OVER_USB_KEY = "verify_apps_over_usb";
118     private static final String DEBUG_VIEW_ATTRIBUTES =  "debug_view_attributes";
119     private static final String STRICT_MODE_KEY = "strict_mode";
120     private static final String POINTER_LOCATION_KEY = "pointer_location";
121     private static final String SHOW_TOUCHES_KEY = "show_touches";
122     private static final String SHOW_SCREEN_UPDATES_KEY = "show_screen_updates";
123     private static final String DISABLE_OVERLAYS_KEY = "disable_overlays";
124     private static final String SIMULATE_COLOR_SPACE = "simulate_color_space";
125     private static final String USE_AWESOMEPLAYER_KEY = "use_awesomeplayer";
126     private static final String USB_AUDIO_KEY = "usb_audio";
127     private static final String USE_AWESOMEPLAYER_PROPERTY = "persist.sys.media.use-awesome";
128     private static final String SHOW_CPU_USAGE_KEY = "show_cpu_usage";
129     private static final String FORCE_HARDWARE_UI_KEY = "force_hw_ui";
130     private static final String FORCE_MSAA_KEY = "force_msaa";
131     private static final String TRACK_FRAME_TIME_KEY = "track_frame_time";
132     private static final String SHOW_NON_RECTANGULAR_CLIP_KEY = "show_non_rect_clip";
133     private static final String SHOW_HW_SCREEN_UPDATES_KEY = "show_hw_screen_udpates";
134     private static final String SHOW_HW_LAYERS_UPDATES_KEY = "show_hw_layers_udpates";
135     private static final String DEBUG_HW_OVERDRAW_KEY = "debug_hw_overdraw";
136     private static final String DEBUG_LAYOUT_KEY = "debug_layout";
137     private static final String FORCE_RTL_LAYOUT_KEY = "force_rtl_layout_all_locales";
138     private static final String WINDOW_ANIMATION_SCALE_KEY = "window_animation_scale";
139     private static final String TRANSITION_ANIMATION_SCALE_KEY = "transition_animation_scale";
140     private static final String ANIMATOR_DURATION_SCALE_KEY = "animator_duration_scale";
141     private static final String OVERLAY_DISPLAY_DEVICES_KEY = "overlay_display_devices";
142     private static final String ENABLE_MULTI_WINDOW_KEY = "enable_multi_window";
143     private static final String DEBUG_DEBUGGING_CATEGORY_KEY = "debug_debugging_category";
144     private static final String DEBUG_APPLICATIONS_CATEGORY_KEY = "debug_applications_category";
145     private static final String SELECT_LOGD_SIZE_KEY = "select_logd_size";
146     private static final String SELECT_LOGD_SIZE_PROPERTY = "persist.logd.size";
147     private static final String SELECT_LOGD_DEFAULT_SIZE_PROPERTY = "ro.logd.size";
148
149     private static final String DEBUG_NETWORKING_CATEGORY_KEY = "debug_networking_category";
150     private static final String WIFI_DISPLAY_CERTIFICATION_KEY = "wifi_display_certification";
151     private static final String WIFI_VERBOSE_LOGGING_KEY = "wifi_verbose_logging";
152     private static final String WIFI_AGGRESSIVE_HANDOVER_KEY = "wifi_aggressive_handover";
153     private static final String WIFI_ALLOW_SCAN_WITH_TRAFFIC_KEY = "wifi_allow_scan_with_traffic";
154     private static final String USB_CONFIGURATION_KEY = "select_usb_configuration";
155     private static final String SELECT_USB_CONFIGURATION_PROPERTY = "sys.usb.config";
156     private static final String WIFI_LEGACY_DHCP_CLIENT_KEY = "legacy_dhcp_client";
157
158     private static final String OPENGL_TRACES_KEY = "enable_opengl_traces";
159
160     private static final String IMMEDIATELY_DESTROY_ACTIVITIES_KEY
161             = "immediately_destroy_activities";
162     private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit";
163
164     private static final String SHOW_ALL_ANRS_KEY = "show_all_anrs";
165
166     private static final String PROCESS_STATS = "proc_stats";
167
168     private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
169
170     private static final String TERMINAL_APP_PACKAGE = "com.android.terminal";
171
172     private static final int RESULT_DEBUG_APP = 1000;
173
174     private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst";
175
176     private static final int REQUEST_CODE_ENABLE_OEM_UNLOCK = 0;
177
178     private static String DEFAULT_LOG_RING_BUFFER_SIZE_IN_BYTES = "262144"; // 256K
179
180     private static final String MULTI_WINDOW_SYSTEM_PROPERTY = "persist.sys.debug.multi_window";
181     private IWindowManager mWindowManager;
182     private IBackupManager mBackupManager;
183     private DevicePolicyManager mDpm;
184     private UserManager mUm;
185     private WifiManager mWifiManager;
186
187     private SwitchBar mSwitchBar;
188     private boolean mLastEnabledState;
189     private boolean mHaveDebugSettings;
190     private boolean mDontPokeProperties;
191
192     private SwitchPreference mEnableAdb;
193     private Preference mClearAdbKeys;
194     private SwitchPreference mEnableTerminal;
195     private Preference mBugreport;
196     private SwitchPreference mBugreportInPower;
197     private SwitchPreference mKeepScreenOn;
198     private SwitchPreference mBtHciSnoopLog;
199     private SwitchPreference mEnableOemUnlock;
200     private SwitchPreference mAllowMockLocation;
201     private SwitchPreference mDebugViewAttributes;
202
203     private PreferenceScreen mPassword;
204     private String mDebugApp;
205     private Preference mDebugAppPref;
206     private SwitchPreference mWaitForDebugger;
207     private SwitchPreference mVerifyAppsOverUsb;
208     private SwitchPreference mWifiDisplayCertification;
209     private SwitchPreference mWifiVerboseLogging;
210     private SwitchPreference mWifiAggressiveHandover;
211     private SwitchPreference mLegacyDhcpClient;
212
213     private SwitchPreference mWifiAllowScansWithTraffic;
214     private SwitchPreference mStrictMode;
215     private SwitchPreference mPointerLocation;
216     private SwitchPreference mShowTouches;
217     private SwitchPreference mShowScreenUpdates;
218     private SwitchPreference mDisableOverlays;
219     private SwitchPreference mEnableMultiWindow;
220     private SwitchPreference mShowCpuUsage;
221     private SwitchPreference mForceHardwareUi;
222     private SwitchPreference mForceMsaa;
223     private SwitchPreference mShowHwScreenUpdates;
224     private SwitchPreference mShowHwLayersUpdates;
225     private SwitchPreference mDebugLayout;
226     private SwitchPreference mForceRtlLayout;
227     private ListPreference mDebugHwOverdraw;
228     private ListPreference mLogdSize;
229     private ListPreference mUsbConfiguration;
230     private ListPreference mTrackFrameTime;
231     private ListPreference mShowNonRectClip;
232     private ListPreference mWindowAnimationScale;
233     private ListPreference mTransitionAnimationScale;
234     private ListPreference mAnimatorDurationScale;
235     private ListPreference mOverlayDisplayDevices;
236     private ListPreference mOpenGLTraces;
237
238     private ListPreference mSimulateColorSpace;
239
240     private SwitchPreference mUseAwesomePlayer;
241     private SwitchPreference mUSBAudio;
242     private SwitchPreference mImmediatelyDestroyActivities;
243
244     private ListPreference mAppProcessLimit;
245
246     private SwitchPreference mShowAllANRs;
247
248     private PreferenceScreen mProcessStats;
249     private final ArrayList<Preference> mAllPrefs = new ArrayList<Preference>();
250
251     private final ArrayList<SwitchPreference> mResetSwitchPrefs
252             = new ArrayList<SwitchPreference>();
253
254     private final HashSet<Preference> mDisabledPrefs = new HashSet<Preference>();
255     // To track whether a confirmation dialog was clicked.
256     private boolean mDialogClicked;
257     private Dialog mEnableDialog;
258     private Dialog mAdbDialog;
259
260     private Dialog mAdbKeysDialog;
261     private boolean mUnavailable;
262
263     @Override
264     protected int getMetricsCategory() {
265         return MetricsLogger.DEVELOPMENT;
266     }
267
268     @Override
269     public void onCreate(Bundle icicle) {
270         super.onCreate(icicle);
271
272         mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
273         mBackupManager = IBackupManager.Stub.asInterface(
274                 ServiceManager.getService(Context.BACKUP_SERVICE));
275         mDpm = (DevicePolicyManager)getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
276         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
277
278         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
279
280         if (android.os.Process.myUserHandle().getIdentifier() != UserHandle.USER_OWNER
281                 || mUm.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)) {
282             mUnavailable = true;
283             setPreferenceScreen(new PreferenceScreen(getActivity(), null));
284             return;
285         }
286
287         addPreferencesFromResource(R.xml.development_prefs);
288
289         final PreferenceGroup debugDebuggingCategory = (PreferenceGroup)
290                 findPreference(DEBUG_DEBUGGING_CATEGORY_KEY);
291         mEnableAdb = findAndInitSwitchPref(ENABLE_ADB);
292         mClearAdbKeys = findPreference(CLEAR_ADB_KEYS);
293         if (!SystemProperties.getBoolean("ro.adb.secure", false)) {
294             if (debugDebuggingCategory != null) {
295                 debugDebuggingCategory.removePreference(mClearAdbKeys);
296             }
297         }
298         mAllPrefs.add(mClearAdbKeys);
299         mEnableTerminal = findAndInitSwitchPref(ENABLE_TERMINAL);
300         if (!isPackageInstalled(getActivity(), TERMINAL_APP_PACKAGE)) {
301             debugDebuggingCategory.removePreference(mEnableTerminal);
302             mEnableTerminal = null;
303         }
304
305         mBugreport = findPreference(BUGREPORT);
306         mBugreportInPower = findAndInitSwitchPref(BUGREPORT_IN_POWER_KEY);
307         mKeepScreenOn = findAndInitSwitchPref(KEEP_SCREEN_ON);
308         mBtHciSnoopLog = findAndInitSwitchPref(BT_HCI_SNOOP_LOG);
309         mEnableOemUnlock = findAndInitSwitchPref(ENABLE_OEM_UNLOCK);
310         if (!showEnableOemUnlockPreference()) {
311             removePreference(mEnableOemUnlock);
312             mEnableOemUnlock = null;
313         }
314         mAllowMockLocation = findAndInitSwitchPref(ALLOW_MOCK_LOCATION);
315         mDebugViewAttributes = findAndInitSwitchPref(DEBUG_VIEW_ATTRIBUTES);
316         mPassword = (PreferenceScreen) findPreference(LOCAL_BACKUP_PASSWORD);
317         mAllPrefs.add(mPassword);
318
319
320         if (!android.os.Process.myUserHandle().equals(UserHandle.OWNER)) {
321             disableForUser(mEnableAdb);
322             disableForUser(mClearAdbKeys);
323             disableForUser(mEnableTerminal);
324             disableForUser(mPassword);
325         }
326
327         mDebugAppPref = findPreference(DEBUG_APP_KEY);
328         mAllPrefs.add(mDebugAppPref);
329         mWaitForDebugger = findAndInitSwitchPref(WAIT_FOR_DEBUGGER_KEY);
330         mVerifyAppsOverUsb = findAndInitSwitchPref(VERIFY_APPS_OVER_USB_KEY);
331         if (!showVerifierSetting()) {
332             if (debugDebuggingCategory != null) {
333                 debugDebuggingCategory.removePreference(mVerifyAppsOverUsb);
334             } else {
335                 mVerifyAppsOverUsb.setEnabled(false);
336             }
337         }
338         mStrictMode = findAndInitSwitchPref(STRICT_MODE_KEY);
339         mPointerLocation = findAndInitSwitchPref(POINTER_LOCATION_KEY);
340         mShowTouches = findAndInitSwitchPref(SHOW_TOUCHES_KEY);
341         mShowScreenUpdates = findAndInitSwitchPref(SHOW_SCREEN_UPDATES_KEY);
342         mDisableOverlays = findAndInitSwitchPref(DISABLE_OVERLAYS_KEY);
343         mShowCpuUsage = findAndInitSwitchPref(SHOW_CPU_USAGE_KEY);
344         mForceHardwareUi = findAndInitSwitchPref(FORCE_HARDWARE_UI_KEY);
345         mForceMsaa = findAndInitSwitchPref(FORCE_MSAA_KEY);
346         mTrackFrameTime = addListPreference(TRACK_FRAME_TIME_KEY);
347         mShowNonRectClip = addListPreference(SHOW_NON_RECTANGULAR_CLIP_KEY);
348         mShowHwScreenUpdates = findAndInitSwitchPref(SHOW_HW_SCREEN_UPDATES_KEY);
349         mShowHwLayersUpdates = findAndInitSwitchPref(SHOW_HW_LAYERS_UPDATES_KEY);
350         mDebugLayout = findAndInitSwitchPref(DEBUG_LAYOUT_KEY);
351         mForceRtlLayout = findAndInitSwitchPref(FORCE_RTL_LAYOUT_KEY);
352         mDebugHwOverdraw = addListPreference(DEBUG_HW_OVERDRAW_KEY);
353         mWifiDisplayCertification = findAndInitSwitchPref(WIFI_DISPLAY_CERTIFICATION_KEY);
354         mWifiVerboseLogging = findAndInitSwitchPref(WIFI_VERBOSE_LOGGING_KEY);
355         mWifiAggressiveHandover = findAndInitSwitchPref(WIFI_AGGRESSIVE_HANDOVER_KEY);
356         mWifiAllowScansWithTraffic = findAndInitSwitchPref(WIFI_ALLOW_SCAN_WITH_TRAFFIC_KEY);
357         mLegacyDhcpClient = findAndInitSwitchPref(WIFI_LEGACY_DHCP_CLIENT_KEY);
358         mLogdSize = addListPreference(SELECT_LOGD_SIZE_KEY);
359         mUsbConfiguration = addListPreference(USB_CONFIGURATION_KEY);
360
361         mWindowAnimationScale = addListPreference(WINDOW_ANIMATION_SCALE_KEY);
362         mTransitionAnimationScale = addListPreference(TRANSITION_ANIMATION_SCALE_KEY);
363         mAnimatorDurationScale = addListPreference(ANIMATOR_DURATION_SCALE_KEY);
364         mOverlayDisplayDevices = addListPreference(OVERLAY_DISPLAY_DEVICES_KEY);
365         mEnableMultiWindow = findAndInitSwitchPref(ENABLE_MULTI_WINDOW_KEY);
366         if (!showEnableMultiWindowPreference()) {
367             final PreferenceGroup drawingGroup =
368                     (PreferenceGroup)findPreference("debug_drawing_category");
369             if (drawingGroup != null) {
370                 drawingGroup.removePreference(mEnableMultiWindow);
371             } else {
372                 mEnableMultiWindow.setEnabled(false);
373             }
374             removePreference(mEnableMultiWindow);
375             mEnableMultiWindow = null;
376         }
377         mOpenGLTraces = addListPreference(OPENGL_TRACES_KEY);
378         mSimulateColorSpace = addListPreference(SIMULATE_COLOR_SPACE);
379         mUseAwesomePlayer = findAndInitSwitchPref(USE_AWESOMEPLAYER_KEY);
380         mUSBAudio = findAndInitSwitchPref(USB_AUDIO_KEY);
381
382         mImmediatelyDestroyActivities = (SwitchPreference) findPreference(
383                 IMMEDIATELY_DESTROY_ACTIVITIES_KEY);
384         mAllPrefs.add(mImmediatelyDestroyActivities);
385         mResetSwitchPrefs.add(mImmediatelyDestroyActivities);
386
387         mAppProcessLimit = addListPreference(APP_PROCESS_LIMIT_KEY);
388
389         mShowAllANRs = (SwitchPreference) findPreference(
390                 SHOW_ALL_ANRS_KEY);
391         mAllPrefs.add(mShowAllANRs);
392         mResetSwitchPrefs.add(mShowAllANRs);
393
394         Preference hdcpChecking = findPreference(HDCP_CHECKING_KEY);
395         if (hdcpChecking != null) {
396             mAllPrefs.add(hdcpChecking);
397             removePreferenceForProduction(hdcpChecking);
398         }
399
400         mProcessStats = (PreferenceScreen) findPreference(PROCESS_STATS);
401         mAllPrefs.add(mProcessStats);
402     }
403
404     private ListPreference addListPreference(String prefKey) {
405         ListPreference pref = (ListPreference) findPreference(prefKey);
406         mAllPrefs.add(pref);
407         pref.setOnPreferenceChangeListener(this);
408         return pref;
409     }
410
411     private void disableForUser(Preference pref) {
412         if (pref != null) {
413             pref.setEnabled(false);
414             mDisabledPrefs.add(pref);
415         }
416     }
417
418     private SwitchPreference findAndInitSwitchPref(String key) {
419         SwitchPreference pref = (SwitchPreference) findPreference(key);
420         if (pref == null) {
421             throw new IllegalArgumentException("Cannot find preference with key = " + key);
422         }
423         mAllPrefs.add(pref);
424         mResetSwitchPrefs.add(pref);
425         return pref;
426     }
427
428     @Override
429     public void onActivityCreated(Bundle savedInstanceState) {
430         super.onActivityCreated(savedInstanceState);
431
432         final SettingsActivity activity = (SettingsActivity) getActivity();
433
434         mSwitchBar = activity.getSwitchBar();
435        if (mUnavailable) {
436             mSwitchBar.setEnabled(false);
437             return;
438         }
439
440         mSwitchBar.addOnSwitchChangeListener(this);
441     }
442
443     private boolean removePreferenceForProduction(Preference preference) {
444         if ("user".equals(Build.TYPE)) {
445             removePreference(preference);
446             return true;
447         }
448         return false;
449     }
450
451     private void removePreference(Preference preference) {
452         getPreferenceScreen().removePreference(preference);
453         mAllPrefs.remove(preference);
454         mResetSwitchPrefs.remove(preference);
455     }
456
457     private void setPrefsEnabledState(boolean enabled) {
458         for (int i = 0; i < mAllPrefs.size(); i++) {
459             Preference pref = mAllPrefs.get(i);
460             pref.setEnabled(enabled && !mDisabledPrefs.contains(pref));
461         }
462         updateAllOptions();
463     }
464
465     @Override
466     public void onResume() {
467         super.onResume();
468
469         if (mUnavailable) {
470             // Show error message
471             TextView emptyView = (TextView) getView().findViewById(android.R.id.empty);
472             getListView().setEmptyView(emptyView);
473             if (emptyView != null) {
474                 emptyView.setText(R.string.development_settings_not_available);
475             }
476             return;
477         }
478
479         if (mDpm.getMaximumTimeToLock(null) > 0) {
480             // A DeviceAdmin has specified a maximum time until the device
481             // will lock...  in this case we can't allow the user to turn
482             // on "stay awake when plugged in" because that would defeat the
483             // restriction.
484             mDisabledPrefs.add(mKeepScreenOn);
485         } else {
486             mDisabledPrefs.remove(mKeepScreenOn);
487         }
488
489         final ContentResolver cr = getActivity().getContentResolver();
490         mLastEnabledState = Settings.Global.getInt(cr,
491                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
492         mSwitchBar.setChecked(mLastEnabledState);
493         setPrefsEnabledState(mLastEnabledState);
494
495         if (mHaveDebugSettings && !mLastEnabledState) {
496             // Overall debugging is disabled, but there are some debug
497             // settings that are enabled.  This is an invalid state.  Switch
498             // to debug settings being enabled, so the user knows there is
499             // stuff enabled and can turn it all off if they want.
500             Settings.Global.putInt(getActivity().getContentResolver(),
501                     Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
502             mLastEnabledState = true;
503             mSwitchBar.setChecked(mLastEnabledState);
504             setPrefsEnabledState(mLastEnabledState);
505         }
506         mSwitchBar.show();
507     }
508
509     @Override
510     public View onCreateView(LayoutInflater inflater, ViewGroup container,
511             Bundle savedInstanceState) {
512         IntentFilter filter = new IntentFilter();
513         filter.addAction(UsbManager.ACTION_USB_STATE);
514         getActivity().registerReceiver(mUsbReceiver, filter);
515         return super.onCreateView(inflater, container, savedInstanceState);
516     }
517
518     @Override
519     public void onDestroyView() {
520         super.onDestroyView();
521
522         if (mUnavailable) {
523             return;
524         }
525         mSwitchBar.removeOnSwitchChangeListener(this);
526         mSwitchBar.hide();
527         getActivity().unregisterReceiver(mUsbReceiver);
528     }
529
530     void updateSwitchPreference(SwitchPreference switchPreference, boolean value) {
531         switchPreference.setChecked(value);
532         mHaveDebugSettings |= value;
533     }
534
535     private void updateAllOptions() {
536         final Context context = getActivity();
537         final ContentResolver cr = context.getContentResolver();
538         mHaveDebugSettings = false;
539         updateSwitchPreference(mEnableAdb, Settings.Global.getInt(cr,
540                 Settings.Global.ADB_ENABLED, 0) != 0);
541         if (mEnableTerminal != null) {
542             updateSwitchPreference(mEnableTerminal,
543                     context.getPackageManager().getApplicationEnabledSetting(TERMINAL_APP_PACKAGE)
544                             == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
545         }
546         updateSwitchPreference(mBugreportInPower, Settings.Secure.getInt(cr,
547                 Settings.Secure.BUGREPORT_IN_POWER_MENU, 0) != 0);
548         updateSwitchPreference(mKeepScreenOn, Settings.Global.getInt(cr,
549                 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0) != 0);
550         updateSwitchPreference(mBtHciSnoopLog, Settings.Secure.getInt(cr,
551                 Settings.Secure.BLUETOOTH_HCI_LOG, 0) != 0);
552         if (mEnableOemUnlock != null) {
553             updateSwitchPreference(mEnableOemUnlock, Utils.isOemUnlockEnabled(getActivity()));
554         }
555         updateSwitchPreference(mAllowMockLocation, Settings.Secure.getInt(cr,
556                 Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0);
557         updateSwitchPreference(mDebugViewAttributes, Settings.Global.getInt(cr,
558                 Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0);
559         updateHdcpValues();
560         updatePasswordSummary();
561         updateDebuggerOptions();
562         updateStrictModeVisualOptions();
563         updatePointerLocationOptions();
564         updateShowTouchesOptions();
565         updateFlingerOptions();
566         updateCpuUsageOptions();
567         updateHardwareUiOptions();
568         updateMsaaOptions();
569         updateTrackFrameTimeOptions();
570         updateShowNonRectClipOptions();
571         updateShowHwScreenUpdatesOptions();
572         updateShowHwLayersUpdatesOptions();
573         updateDebugHwOverdrawOptions();
574         updateDebugLayoutOptions();
575         updateAnimationScaleOptions();
576         updateOverlayDisplayDevicesOptions();
577         if (mEnableMultiWindow != null) {
578             updateSwitchPreference(mEnableMultiWindow,
579                     SystemProperties.getBoolean(MULTI_WINDOW_SYSTEM_PROPERTY, false));
580         }
581         updateOpenGLTracesOptions();
582         updateImmediatelyDestroyActivitiesOptions();
583         updateAppProcessLimitOptions();
584         updateShowAllANRsOptions();
585         updateVerifyAppsOverUsbOptions();
586         updateBugreportOptions();
587         updateForceRtlOptions();
588         updateLogdSizeValues();
589         updateWifiDisplayCertificationOptions();
590         updateWifiVerboseLoggingOptions();
591         updateWifiAggressiveHandoverOptions();
592         updateWifiAllowScansWithTrafficOptions();
593         updateLegacyDhcpClientOptions();
594         updateSimulateColorSpace();
595         updateUseNuplayerOptions();
596         updateUSBAudioOptions();
597     }
598
599     private void resetDangerousOptions() {
600         mDontPokeProperties = true;
601         for (int i=0; i< mResetSwitchPrefs.size(); i++) {
602             SwitchPreference cb = mResetSwitchPrefs.get(i);
603             if (cb.isChecked()) {
604                 cb.setChecked(false);
605                 onPreferenceTreeClick(null, cb);
606             }
607         }
608         resetDebuggerOptions();
609         writeLogdSizeOption(null);
610         writeAnimationScaleOption(0, mWindowAnimationScale, null);
611         writeAnimationScaleOption(1, mTransitionAnimationScale, null);
612         writeAnimationScaleOption(2, mAnimatorDurationScale, null);
613         // Only poke the color space setting if we control it.
614         if (usingDevelopmentColorSpace()) {
615             writeSimulateColorSpace(-1);
616         }
617         writeOverlayDisplayDevicesOptions(null);
618         writeAppProcessLimitOptions(null);
619         mHaveDebugSettings = false;
620         updateAllOptions();
621         mDontPokeProperties = false;
622         pokeSystemProperties();
623     }
624
625     private void updateHdcpValues() {
626         ListPreference hdcpChecking = (ListPreference) findPreference(HDCP_CHECKING_KEY);
627         if (hdcpChecking != null) {
628             String currentValue = SystemProperties.get(HDCP_CHECKING_PROPERTY);
629             String[] values = getResources().getStringArray(R.array.hdcp_checking_values);
630             String[] summaries = getResources().getStringArray(R.array.hdcp_checking_summaries);
631             int index = 1; // Defaults to drm-only. Needs to match with R.array.hdcp_checking_values
632             for (int i = 0; i < values.length; i++) {
633                 if (currentValue.equals(values[i])) {
634                     index = i;
635                     break;
636                 }
637             }
638             hdcpChecking.setValue(values[index]);
639             hdcpChecking.setSummary(summaries[index]);
640             hdcpChecking.setOnPreferenceChangeListener(this);
641         }
642     }
643
644     private void updatePasswordSummary() {
645         try {
646             if (mBackupManager.hasBackupPassword()) {
647                 mPassword.setSummary(R.string.local_backup_password_summary_change);
648             } else {
649                 mPassword.setSummary(R.string.local_backup_password_summary_none);
650             }
651         } catch (RemoteException e) {
652             // Not much we can do here
653         }
654     }
655
656     private void writeBtHciSnoopLogOptions() {
657         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
658         adapter.configHciSnoopLog(mBtHciSnoopLog.isChecked());
659         Settings.Secure.putInt(getActivity().getContentResolver(),
660                 Settings.Secure.BLUETOOTH_HCI_LOG,
661                 mBtHciSnoopLog.isChecked() ? 1 : 0);
662     }
663
664     private void writeDebuggerOptions() {
665         try {
666             ActivityManagerNative.getDefault().setDebugApp(
667                 mDebugApp, mWaitForDebugger.isChecked(), true);
668         } catch (RemoteException ex) {
669         }
670     }
671
672     private static void resetDebuggerOptions() {
673         try {
674             ActivityManagerNative.getDefault().setDebugApp(
675                     null, false, true);
676         } catch (RemoteException ex) {
677         }
678     }
679
680     private void updateDebuggerOptions() {
681         mDebugApp = Settings.Global.getString(
682                 getActivity().getContentResolver(), Settings.Global.DEBUG_APP);
683         updateSwitchPreference(mWaitForDebugger, Settings.Global.getInt(
684                 getActivity().getContentResolver(), Settings.Global.WAIT_FOR_DEBUGGER, 0) != 0);
685         if (mDebugApp != null && mDebugApp.length() > 0) {
686             String label;
687             try {
688                 ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(mDebugApp,
689                         PackageManager.GET_DISABLED_COMPONENTS);
690                 CharSequence lab = getActivity().getPackageManager().getApplicationLabel(ai);
691                 label = lab != null ? lab.toString() : mDebugApp;
692             } catch (PackageManager.NameNotFoundException e) {
693                 label = mDebugApp;
694             }
695             mDebugAppPref.setSummary(getResources().getString(R.string.debug_app_set, label));
696             mWaitForDebugger.setEnabled(true);
697             mHaveDebugSettings = true;
698         } else {
699             mDebugAppPref.setSummary(getResources().getString(R.string.debug_app_not_set));
700             mWaitForDebugger.setEnabled(false);
701         }
702     }
703
704     private void updateVerifyAppsOverUsbOptions() {
705         updateSwitchPreference(mVerifyAppsOverUsb, Settings.Global.getInt(getActivity().getContentResolver(),
706                 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, 1) != 0);
707         mVerifyAppsOverUsb.setEnabled(enableVerifierSetting());
708     }
709
710     private void writeVerifyAppsOverUsbOptions() {
711         Settings.Global.putInt(getActivity().getContentResolver(),
712               Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, mVerifyAppsOverUsb.isChecked() ? 1 : 0);
713     }
714
715     private boolean enableVerifierSetting() {
716         final ContentResolver cr = getActivity().getContentResolver();
717         if (Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0) == 0) {
718             return false;
719         }
720         if (Settings.Global.getInt(cr, Settings.Global.PACKAGE_VERIFIER_ENABLE, 1) == 0) {
721             return false;
722         } else {
723             final PackageManager pm = getActivity().getPackageManager();
724             final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION);
725             verification.setType(PACKAGE_MIME_TYPE);
726             verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
727             final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(verification, 0);
728             if (receivers.size() == 0) {
729                 return false;
730             }
731         }
732         return true;
733     }
734
735     private boolean showVerifierSetting() {
736         return Settings.Global.getInt(getActivity().getContentResolver(),
737                 Settings.Global.PACKAGE_VERIFIER_SETTING_VISIBLE, 1) > 0;
738     }
739
740     private static boolean showEnableOemUnlockPreference() {
741         return !SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals("");
742     }
743
744     private static boolean showEnableMultiWindowPreference() {
745         return !"user".equals(Build.TYPE);
746     }
747
748     private void setEnableMultiWindow(boolean value) {
749         SystemProperties.set(MULTI_WINDOW_SYSTEM_PROPERTY, String.valueOf(value));
750         pokeSystemProperties();
751     }
752
753     private void updateBugreportOptions() {
754         if ("user".equals(Build.TYPE)) {
755             final ContentResolver resolver = getActivity().getContentResolver();
756             final boolean adbEnabled = Settings.Global.getInt(
757                     resolver, Settings.Global.ADB_ENABLED, 0) != 0;
758             if (adbEnabled) {
759                 mBugreport.setEnabled(true);
760                 mBugreportInPower.setEnabled(true);
761             } else {
762                 mBugreport.setEnabled(false);
763                 mBugreportInPower.setEnabled(false);
764                 mBugreportInPower.setChecked(false);
765                 Settings.Secure.putInt(resolver, Settings.Secure.BUGREPORT_IN_POWER_MENU, 0);
766             }
767         } else {
768             mBugreportInPower.setEnabled(true);
769         }
770     }
771
772     // Returns the current state of the system property that controls
773     // strictmode flashes.  One of:
774     //    0: not explicitly set one way or another
775     //    1: on
776     //    2: off
777     private static int currentStrictModeActiveIndex() {
778         if (TextUtils.isEmpty(SystemProperties.get(StrictMode.VISUAL_PROPERTY))) {
779             return 0;
780         }
781         boolean enabled = SystemProperties.getBoolean(StrictMode.VISUAL_PROPERTY, false);
782         return enabled ? 1 : 2;
783     }
784
785     private void writeStrictModeVisualOptions() {
786         try {
787             mWindowManager.setStrictModeVisualIndicatorPreference(mStrictMode.isChecked()
788                     ? "1" : "");
789         } catch (RemoteException e) {
790         }
791     }
792
793     private void updateStrictModeVisualOptions() {
794         updateSwitchPreference(mStrictMode, currentStrictModeActiveIndex() == 1);
795     }
796
797     private void writePointerLocationOptions() {
798         Settings.System.putInt(getActivity().getContentResolver(),
799                 Settings.System.POINTER_LOCATION, mPointerLocation.isChecked() ? 1 : 0);
800     }
801
802     private void updatePointerLocationOptions() {
803         updateSwitchPreference(mPointerLocation,
804                 Settings.System.getInt(getActivity().getContentResolver(),
805                 Settings.System.POINTER_LOCATION, 0) != 0);
806     }
807
808     private void writeShowTouchesOptions() {
809         Settings.System.putInt(getActivity().getContentResolver(),
810                 Settings.System.SHOW_TOUCHES, mShowTouches.isChecked() ? 1 : 0);
811     }
812
813     private void updateShowTouchesOptions() {
814         updateSwitchPreference(mShowTouches,
815                 Settings.System.getInt(getActivity().getContentResolver(),
816                 Settings.System.SHOW_TOUCHES, 0) != 0);
817     }
818
819     private void updateFlingerOptions() {
820         // magic communication with surface flinger.
821         try {
822             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
823             if (flinger != null) {
824                 Parcel data = Parcel.obtain();
825                 Parcel reply = Parcel.obtain();
826                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
827                 flinger.transact(1010, data, reply, 0);
828                 @SuppressWarnings("unused")
829                 int showCpu = reply.readInt();
830                 @SuppressWarnings("unused")
831                 int enableGL = reply.readInt();
832                 int showUpdates = reply.readInt();
833                 updateSwitchPreference(mShowScreenUpdates, showUpdates != 0);
834                 @SuppressWarnings("unused")
835                 int showBackground = reply.readInt();
836                 int disableOverlays = reply.readInt();
837                 updateSwitchPreference(mDisableOverlays, disableOverlays != 0);
838                 reply.recycle();
839                 data.recycle();
840             }
841         } catch (RemoteException ex) {
842         }
843     }
844
845     private void writeShowUpdatesOption() {
846         try {
847             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
848             if (flinger != null) {
849                 Parcel data = Parcel.obtain();
850                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
851                 final int showUpdates = mShowScreenUpdates.isChecked() ? 1 : 0;
852                 data.writeInt(showUpdates);
853                 flinger.transact(1002, data, null, 0);
854                 data.recycle();
855
856                 updateFlingerOptions();
857             }
858         } catch (RemoteException ex) {
859         }
860     }
861
862     private void writeDisableOverlaysOption() {
863         try {
864             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
865             if (flinger != null) {
866                 Parcel data = Parcel.obtain();
867                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
868                 final int disableOverlays = mDisableOverlays.isChecked() ? 1 : 0;
869                 data.writeInt(disableOverlays);
870                 flinger.transact(1008, data, null, 0);
871                 data.recycle();
872
873                 updateFlingerOptions();
874             }
875         } catch (RemoteException ex) {
876         }
877     }
878
879     private void updateHardwareUiOptions() {
880         updateSwitchPreference(mForceHardwareUi, SystemProperties.getBoolean(HARDWARE_UI_PROPERTY, false));
881     }
882
883     private void writeHardwareUiOptions() {
884         SystemProperties.set(HARDWARE_UI_PROPERTY, mForceHardwareUi.isChecked() ? "true" : "false");
885         pokeSystemProperties();
886     }
887
888     private void updateMsaaOptions() {
889         updateSwitchPreference(mForceMsaa, SystemProperties.getBoolean(MSAA_PROPERTY, false));
890     }
891
892     private void writeMsaaOptions() {
893         SystemProperties.set(MSAA_PROPERTY, mForceMsaa.isChecked() ? "true" : "false");
894         pokeSystemProperties();
895     }
896
897     private void updateTrackFrameTimeOptions() {
898         String value = SystemProperties.get(HardwareRenderer.PROFILE_PROPERTY);
899         if (value == null) {
900             value = "";
901         }
902
903         CharSequence[] values = mTrackFrameTime.getEntryValues();
904         for (int i = 0; i < values.length; i++) {
905             if (value.contentEquals(values[i])) {
906                 mTrackFrameTime.setValueIndex(i);
907                 mTrackFrameTime.setSummary(mTrackFrameTime.getEntries()[i]);
908                 return;
909             }
910         }
911         mTrackFrameTime.setValueIndex(0);
912         mTrackFrameTime.setSummary(mTrackFrameTime.getEntries()[0]);
913     }
914
915     private void writeTrackFrameTimeOptions(Object newValue) {
916         SystemProperties.set(HardwareRenderer.PROFILE_PROPERTY,
917                 newValue == null ? "" : newValue.toString());
918         pokeSystemProperties();
919         updateTrackFrameTimeOptions();
920     }
921
922     private void updateShowNonRectClipOptions() {
923         String value = SystemProperties.get(
924                 HardwareRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY);
925         if (value == null) {
926             value = "hide";
927         }
928
929         CharSequence[] values = mShowNonRectClip.getEntryValues();
930         for (int i = 0; i < values.length; i++) {
931             if (value.contentEquals(values[i])) {
932                 mShowNonRectClip.setValueIndex(i);
933                 mShowNonRectClip.setSummary(mShowNonRectClip.getEntries()[i]);
934                 return;
935             }
936         }
937         mShowNonRectClip.setValueIndex(0);
938         mShowNonRectClip.setSummary(mShowNonRectClip.getEntries()[0]);
939     }
940
941     private void writeShowNonRectClipOptions(Object newValue) {
942         SystemProperties.set(HardwareRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY,
943                 newValue == null ? "" : newValue.toString());
944         pokeSystemProperties();
945         updateShowNonRectClipOptions();
946     }
947
948     private void updateShowHwScreenUpdatesOptions() {
949         updateSwitchPreference(mShowHwScreenUpdates,
950                 SystemProperties.getBoolean(HardwareRenderer.DEBUG_DIRTY_REGIONS_PROPERTY, false));
951     }
952
953     private void writeShowHwScreenUpdatesOptions() {
954         SystemProperties.set(HardwareRenderer.DEBUG_DIRTY_REGIONS_PROPERTY,
955                 mShowHwScreenUpdates.isChecked() ? "true" : null);
956         pokeSystemProperties();
957     }
958
959     private void updateShowHwLayersUpdatesOptions() {
960         updateSwitchPreference(mShowHwLayersUpdates, SystemProperties.getBoolean(
961                 HardwareRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY, false));
962     }
963
964     private void writeShowHwLayersUpdatesOptions() {
965         SystemProperties.set(HardwareRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY,
966                 mShowHwLayersUpdates.isChecked() ? "true" : null);
967         pokeSystemProperties();
968     }
969
970     private void updateDebugHwOverdrawOptions() {
971         String value = SystemProperties.get(HardwareRenderer.DEBUG_OVERDRAW_PROPERTY);
972         if (value == null) {
973             value = "";
974         }
975
976         CharSequence[] values = mDebugHwOverdraw.getEntryValues();
977         for (int i = 0; i < values.length; i++) {
978             if (value.contentEquals(values[i])) {
979                 mDebugHwOverdraw.setValueIndex(i);
980                 mDebugHwOverdraw.setSummary(mDebugHwOverdraw.getEntries()[i]);
981                 return;
982             }
983         }
984         mDebugHwOverdraw.setValueIndex(0);
985         mDebugHwOverdraw.setSummary(mDebugHwOverdraw.getEntries()[0]);
986     }
987
988     private void writeDebugHwOverdrawOptions(Object newValue) {
989         SystemProperties.set(HardwareRenderer.DEBUG_OVERDRAW_PROPERTY,
990                 newValue == null ? "" : newValue.toString());
991         pokeSystemProperties();
992         updateDebugHwOverdrawOptions();
993     }
994
995     private void updateDebugLayoutOptions() {
996         updateSwitchPreference(mDebugLayout,
997                 SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false));
998     }
999
1000     private void writeDebugLayoutOptions() {
1001         SystemProperties.set(View.DEBUG_LAYOUT_PROPERTY,
1002                 mDebugLayout.isChecked() ? "true" : "false");
1003         pokeSystemProperties();
1004     }
1005
1006     private void updateSimulateColorSpace() {
1007         final ContentResolver cr = getContentResolver();
1008         final boolean enabled = Settings.Secure.getInt(
1009                 cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0) != 0;
1010         if (enabled) {
1011             final String mode = Integer.toString(Settings.Secure.getInt(
1012                     cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
1013                     AccessibilityManager.DALTONIZER_DISABLED));
1014             mSimulateColorSpace.setValue(mode);
1015             final int index = mSimulateColorSpace.findIndexOfValue(mode);
1016             if (index < 0) {
1017                 // We're using a mode controlled by accessibility preferences.
1018                 mSimulateColorSpace.setSummary(getString(R.string.daltonizer_type_overridden,
1019                         getString(R.string.accessibility_display_daltonizer_preference_title)));
1020             } else {
1021                 mSimulateColorSpace.setSummary("%s");
1022             }
1023         } else {
1024             mSimulateColorSpace.setValue(
1025                     Integer.toString(AccessibilityManager.DALTONIZER_DISABLED));
1026         }
1027     }
1028
1029     /**
1030      * @return <code>true</code> if the color space preference is currently
1031      *         controlled by development settings
1032      */
1033     private boolean usingDevelopmentColorSpace() {
1034         final ContentResolver cr = getContentResolver();
1035         final boolean enabled = Settings.Secure.getInt(
1036                 cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0) != 0;
1037         if (enabled) {
1038             final String mode = Integer.toString(Settings.Secure.getInt(
1039                     cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
1040                     AccessibilityManager.DALTONIZER_DISABLED));
1041             final int index = mSimulateColorSpace.findIndexOfValue(mode);
1042             if (index >= 0) {
1043                 // We're using a mode controlled by developer preferences.
1044                 return true;
1045             }
1046         }
1047         return false;
1048     }
1049
1050     private void writeSimulateColorSpace(Object value) {
1051         final ContentResolver cr = getContentResolver();
1052         final int newMode = Integer.parseInt(value.toString());
1053         if (newMode < 0) {
1054             Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0);
1055         } else {
1056             Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 1);
1057             Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, newMode);
1058         }
1059     }
1060
1061     private void updateUseNuplayerOptions() {
1062         updateSwitchPreference(
1063                 mUseAwesomePlayer, SystemProperties.getBoolean(USE_AWESOMEPLAYER_PROPERTY, false));
1064     }
1065
1066     private void writeUseAwesomePlayerOptions() {
1067         SystemProperties.set(
1068                 USE_AWESOMEPLAYER_PROPERTY, mUseAwesomePlayer.isChecked() ? "true" : "false");
1069         pokeSystemProperties();
1070     }
1071
1072     private void updateUSBAudioOptions() {
1073         updateSwitchPreference(mUSBAudio, Settings.Secure.getInt(getContentResolver(),
1074                 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 0) != 0);
1075     }
1076
1077     private void writeUSBAudioOptions() {
1078         Settings.Secure.putInt(getContentResolver(),
1079                 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED,
1080                 mUSBAudio.isChecked() ? 1 : 0);
1081     }
1082
1083     private void updateForceRtlOptions() {
1084         updateSwitchPreference(mForceRtlLayout,
1085                 Settings.Global.getInt(getActivity().getContentResolver(),
1086                 Settings.Global.DEVELOPMENT_FORCE_RTL, 0) != 0);
1087     }
1088
1089     private void writeForceRtlOptions() {
1090         boolean value = mForceRtlLayout.isChecked();
1091         Settings.Global.putInt(getActivity().getContentResolver(),
1092                 Settings.Global.DEVELOPMENT_FORCE_RTL, value ? 1 : 0);
1093         SystemProperties.set(Settings.Global.DEVELOPMENT_FORCE_RTL, value ? "1" : "0");
1094         LocalePicker.updateLocale(getActivity().getResources().getConfiguration().locale);
1095     }
1096
1097     private void updateWifiDisplayCertificationOptions() {
1098         updateSwitchPreference(mWifiDisplayCertification, Settings.Global.getInt(
1099                 getActivity().getContentResolver(),
1100                 Settings.Global.WIFI_DISPLAY_CERTIFICATION_ON, 0) != 0);
1101     }
1102
1103     private void writeWifiDisplayCertificationOptions() {
1104         Settings.Global.putInt(getActivity().getContentResolver(),
1105                 Settings.Global.WIFI_DISPLAY_CERTIFICATION_ON,
1106                 mWifiDisplayCertification.isChecked() ? 1 : 0);
1107     }
1108
1109     private void updateWifiVerboseLoggingOptions() {
1110         boolean enabled = mWifiManager.getVerboseLoggingLevel() > 0;
1111         updateSwitchPreference(mWifiVerboseLogging, enabled);
1112     }
1113
1114     private void writeWifiVerboseLoggingOptions() {
1115         mWifiManager.enableVerboseLogging(mWifiVerboseLogging.isChecked() ? 1 : 0);
1116     }
1117
1118     private void updateWifiAggressiveHandoverOptions() {
1119         boolean enabled = mWifiManager.getAggressiveHandover() > 0;
1120         updateSwitchPreference(mWifiAggressiveHandover, enabled);
1121     }
1122
1123     private void writeWifiAggressiveHandoverOptions() {
1124         mWifiManager.enableAggressiveHandover(mWifiAggressiveHandover.isChecked() ? 1 : 0);
1125     }
1126
1127     private void updateWifiAllowScansWithTrafficOptions() {
1128         boolean enabled = mWifiManager.getAllowScansWithTraffic() > 0;
1129         updateSwitchPreference(mWifiAllowScansWithTraffic, enabled);
1130     }
1131
1132     private void writeWifiAllowScansWithTrafficOptions() {
1133         mWifiManager.setAllowScansWithTraffic(mWifiAllowScansWithTraffic.isChecked() ? 1 : 0);
1134     }
1135
1136     private void updateLegacyDhcpClientOptions() {
1137         updateSwitchPreference(mLegacyDhcpClient, Settings.Global.getInt(
1138                 getActivity().getContentResolver(),
1139                 Settings.Global.LEGACY_DHCP_CLIENT, 0) != 0);
1140     }
1141
1142     private void writeLegacyDhcpClientOptions() {
1143         Settings.Global.putInt(getActivity().getContentResolver(),
1144                 Settings.Global.LEGACY_DHCP_CLIENT,
1145                 mLegacyDhcpClient.isChecked() ? 1 : 0);
1146     }
1147
1148     private void updateLogdSizeValues() {
1149         if (mLogdSize != null) {
1150             String currentValue = SystemProperties.get(SELECT_LOGD_SIZE_PROPERTY);
1151             if (currentValue == null) {
1152                 currentValue = SystemProperties.get(SELECT_LOGD_DEFAULT_SIZE_PROPERTY);
1153                 if (currentValue == null) {
1154                     currentValue = "256K";
1155                 }
1156             }
1157             String[] values = getResources().getStringArray(R.array.select_logd_size_values);
1158             String[] titles = getResources().getStringArray(R.array.select_logd_size_titles);
1159             if (SystemProperties.get("ro.config.low_ram").equals("true")) {
1160                 mLogdSize.setEntries(R.array.select_logd_size_lowram_titles);
1161                 titles = getResources().getStringArray(R.array.select_logd_size_lowram_titles);
1162             }
1163             String[] summaries = getResources().getStringArray(R.array.select_logd_size_summaries);
1164             int index = 1; // punt to second entry if not found
1165             for (int i = 0; i < titles.length; i++) {
1166                 if (currentValue.equals(values[i])
1167                         || currentValue.equals(titles[i])) {
1168                     index = i;
1169                     break;
1170                 }
1171             }
1172             mLogdSize.setValue(values[index]);
1173             mLogdSize.setSummary(summaries[index]);
1174             mLogdSize.setOnPreferenceChangeListener(this);
1175         }
1176     }
1177
1178     private void writeLogdSizeOption(Object newValue) {
1179         String currentValue = SystemProperties.get(SELECT_LOGD_DEFAULT_SIZE_PROPERTY);
1180         if (currentValue != null) {
1181             DEFAULT_LOG_RING_BUFFER_SIZE_IN_BYTES = currentValue;
1182         }
1183         final String size = (newValue != null) ?
1184                 newValue.toString() : DEFAULT_LOG_RING_BUFFER_SIZE_IN_BYTES;
1185         SystemProperties.set(SELECT_LOGD_SIZE_PROPERTY, size);
1186         pokeSystemProperties();
1187         try {
1188             Process p = Runtime.getRuntime().exec("logcat -b all -G " + size);
1189             p.waitFor();
1190             Log.i(TAG, "Logcat ring buffer sizes set to: " + size);
1191         } catch (Exception e) {
1192             Log.w(TAG, "Cannot set logcat ring buffer sizes", e);
1193         }
1194         updateLogdSizeValues();
1195     }
1196
1197     private void updateUsbConfigurationValues() {
1198         if (mUsbConfiguration != null) {
1199             String currentValue = SystemProperties.get(SELECT_USB_CONFIGURATION_PROPERTY);
1200
1201             // Ignore adb interface. The USB Manager adds or removes adb automatically
1202             // depending on if USB debugging is enabled.
1203             int adbIndex = currentValue.indexOf(",adb");
1204             if (adbIndex > 0) {
1205                 currentValue = currentValue.substring(0, adbIndex);
1206             }
1207
1208             String[] values = getResources().getStringArray(R.array.usb_configuration_values);
1209             String[] titles = getResources().getStringArray(R.array.usb_configuration_titles);
1210             int index = 1; // punt to second entry if not found
1211             for (int i = 0; i < titles.length; i++) {
1212                 if (currentValue.equals(values[i])) {
1213                     index = i;
1214                     break;
1215                 }
1216             }
1217             if (index >= 0) {
1218                 mUsbConfiguration.setValue(values[index]);
1219                 mUsbConfiguration.setSummary(titles[index]);
1220             } else {
1221                 mUsbConfiguration.setValue("");
1222                 mUsbConfiguration.setSummary("");
1223             }
1224             mUsbConfiguration.setOnPreferenceChangeListener(this);
1225         }
1226     }
1227
1228     private void writeUsbConfigurationOption(Object newValue) {
1229         UsbManager manager = (UsbManager)getActivity().getSystemService(Context.USB_SERVICE);
1230         manager.setCurrentFunction(newValue.toString(), false);
1231     }
1232
1233     private void updateCpuUsageOptions() {
1234         updateSwitchPreference(mShowCpuUsage,
1235                 Settings.Global.getInt(getActivity().getContentResolver(),
1236                 Settings.Global.SHOW_PROCESSES, 0) != 0);
1237     }
1238
1239     private void writeCpuUsageOptions() {
1240         boolean value = mShowCpuUsage.isChecked();
1241         Settings.Global.putInt(getActivity().getContentResolver(),
1242                 Settings.Global.SHOW_PROCESSES, value ? 1 : 0);
1243         Intent service = (new Intent())
1244                 .setClassName("com.android.systemui", "com.android.systemui.LoadAverageService");
1245         if (value) {
1246             getActivity().startService(service);
1247         } else {
1248             getActivity().stopService(service);
1249         }
1250     }
1251
1252     private void writeImmediatelyDestroyActivitiesOptions() {
1253         try {
1254             ActivityManagerNative.getDefault().setAlwaysFinish(
1255                     mImmediatelyDestroyActivities.isChecked());
1256         } catch (RemoteException ex) {
1257         }
1258     }
1259
1260     private void updateImmediatelyDestroyActivitiesOptions() {
1261         updateSwitchPreference(mImmediatelyDestroyActivities, Settings.Global.getInt(
1262                 getActivity().getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) != 0);
1263     }
1264
1265     private void updateAnimationScaleValue(int which, ListPreference pref) {
1266         try {
1267             float scale = mWindowManager.getAnimationScale(which);
1268             if (scale != 1) {
1269                 mHaveDebugSettings = true;
1270             }
1271             CharSequence[] values = pref.getEntryValues();
1272             for (int i=0; i<values.length; i++) {
1273                 float val = Float.parseFloat(values[i].toString());
1274                 if (scale <= val) {
1275                     pref.setValueIndex(i);
1276                     pref.setSummary(pref.getEntries()[i]);
1277                     return;
1278                 }
1279             }
1280             pref.setValueIndex(values.length-1);
1281             pref.setSummary(pref.getEntries()[0]);
1282         } catch (RemoteException e) {
1283         }
1284     }
1285
1286     private void updateAnimationScaleOptions() {
1287         updateAnimationScaleValue(0, mWindowAnimationScale);
1288         updateAnimationScaleValue(1, mTransitionAnimationScale);
1289         updateAnimationScaleValue(2, mAnimatorDurationScale);
1290     }
1291
1292     private void writeAnimationScaleOption(int which, ListPreference pref, Object newValue) {
1293         try {
1294             float scale = newValue != null ? Float.parseFloat(newValue.toString()) : 1;
1295             mWindowManager.setAnimationScale(which, scale);
1296             updateAnimationScaleValue(which, pref);
1297         } catch (RemoteException e) {
1298         }
1299     }
1300
1301     private void updateOverlayDisplayDevicesOptions() {
1302         String value = Settings.Global.getString(getActivity().getContentResolver(),
1303                 Settings.Global.OVERLAY_DISPLAY_DEVICES);
1304         if (value == null) {
1305             value = "";
1306         }
1307
1308         CharSequence[] values = mOverlayDisplayDevices.getEntryValues();
1309         for (int i = 0; i < values.length; i++) {
1310             if (value.contentEquals(values[i])) {
1311                 mOverlayDisplayDevices.setValueIndex(i);
1312                 mOverlayDisplayDevices.setSummary(mOverlayDisplayDevices.getEntries()[i]);
1313                 return;
1314             }
1315         }
1316         mOverlayDisplayDevices.setValueIndex(0);
1317         mOverlayDisplayDevices.setSummary(mOverlayDisplayDevices.getEntries()[0]);
1318     }
1319
1320     private void writeOverlayDisplayDevicesOptions(Object newValue) {
1321         Settings.Global.putString(getActivity().getContentResolver(),
1322                 Settings.Global.OVERLAY_DISPLAY_DEVICES, (String)newValue);
1323         updateOverlayDisplayDevicesOptions();
1324     }
1325
1326     private void updateOpenGLTracesOptions() {
1327         String value = SystemProperties.get(OPENGL_TRACES_PROPERTY);
1328         if (value == null) {
1329             value = "";
1330         }
1331
1332         CharSequence[] values = mOpenGLTraces.getEntryValues();
1333         for (int i = 0; i < values.length; i++) {
1334             if (value.contentEquals(values[i])) {
1335                 mOpenGLTraces.setValueIndex(i);
1336                 mOpenGLTraces.setSummary(mOpenGLTraces.getEntries()[i]);
1337                 return;
1338             }
1339         }
1340         mOpenGLTraces.setValueIndex(0);
1341         mOpenGLTraces.setSummary(mOpenGLTraces.getEntries()[0]);
1342     }
1343
1344     private void writeOpenGLTracesOptions(Object newValue) {
1345         SystemProperties.set(OPENGL_TRACES_PROPERTY, newValue == null ? "" : newValue.toString());
1346         pokeSystemProperties();
1347         updateOpenGLTracesOptions();
1348     }
1349
1350     private void updateAppProcessLimitOptions() {
1351         try {
1352             int limit = ActivityManagerNative.getDefault().getProcessLimit();
1353             CharSequence[] values = mAppProcessLimit.getEntryValues();
1354             for (int i=0; i<values.length; i++) {
1355                 int val = Integer.parseInt(values[i].toString());
1356                 if (val >= limit) {
1357                     if (i != 0) {
1358                         mHaveDebugSettings = true;
1359                     }
1360                     mAppProcessLimit.setValueIndex(i);
1361                     mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[i]);
1362                     return;
1363                 }
1364             }
1365             mAppProcessLimit.setValueIndex(0);
1366             mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[0]);
1367         } catch (RemoteException e) {
1368         }
1369     }
1370
1371     private void writeAppProcessLimitOptions(Object newValue) {
1372         try {
1373             int limit = newValue != null ? Integer.parseInt(newValue.toString()) : -1;
1374             ActivityManagerNative.getDefault().setProcessLimit(limit);
1375             updateAppProcessLimitOptions();
1376         } catch (RemoteException e) {
1377         }
1378     }
1379
1380     private void writeShowAllANRsOptions() {
1381         Settings.Secure.putInt(getActivity().getContentResolver(),
1382                 Settings.Secure.ANR_SHOW_BACKGROUND,
1383                 mShowAllANRs.isChecked() ? 1 : 0);
1384     }
1385
1386     private void updateShowAllANRsOptions() {
1387         updateSwitchPreference(mShowAllANRs, Settings.Secure.getInt(
1388                 getActivity().getContentResolver(), Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0);
1389     }
1390
1391     private void confirmEnableOemUnlock() {
1392         DialogInterface.OnClickListener onConfirmListener = new DialogInterface.OnClickListener() {
1393             @Override
1394             public void onClick(DialogInterface dialog, int which) {
1395                 Utils.setOemUnlockEnabled(getActivity(), true);
1396                 updateAllOptions();
1397             }
1398         };
1399
1400         new AlertDialog.Builder(getActivity())
1401                 .setTitle(R.string.confirm_enable_oem_unlock_title)
1402                 .setMessage(R.string.confirm_enable_oem_unlock_text)
1403                 .setPositiveButton(R.string.enable_text, onConfirmListener)
1404                 .setNegativeButton(android.R.string.cancel, null)
1405                 .create()
1406                 .show();
1407     }
1408
1409     private void confirmEnableMultiWindowMode() {
1410         DialogInterface.OnClickListener onConfirmListener = new DialogInterface.OnClickListener() {
1411             @Override
1412             public void onClick(DialogInterface dialog, int which) {
1413                 setEnableMultiWindow((which == DialogInterface.BUTTON_POSITIVE) ? true : false);
1414                 updateAllOptions();
1415             }
1416         };
1417
1418         new AlertDialog.Builder(getActivity())
1419                 .setTitle(R.string.confirm_enable_multi_window_title)
1420                 .setMessage(R.string.confirm_enable_multi_window_text)
1421                 .setPositiveButton(R.string.enable_text, onConfirmListener)
1422                 .setNegativeButton(android.R.string.cancel, onConfirmListener)
1423                 .create()
1424                 .show();
1425     }
1426
1427     @Override
1428     public void onSwitchChanged(Switch switchView, boolean isChecked) {
1429         if (switchView != mSwitchBar.getSwitch()) {
1430             return;
1431         }
1432         if (isChecked != mLastEnabledState) {
1433             if (isChecked) {
1434                 mDialogClicked = false;
1435                 if (mEnableDialog != null) dismissDialogs();
1436                 mEnableDialog = new AlertDialog.Builder(getActivity()).setMessage(
1437                         getActivity().getResources().getString(
1438                                 R.string.dev_settings_warning_message))
1439                         .setTitle(R.string.dev_settings_warning_title)
1440                         .setPositiveButton(android.R.string.yes, this)
1441                         .setNegativeButton(android.R.string.no, this)
1442                         .show();
1443                 mEnableDialog.setOnDismissListener(this);
1444             } else {
1445                 resetDangerousOptions();
1446                 Settings.Global.putInt(getActivity().getContentResolver(),
1447                         Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
1448                 mLastEnabledState = isChecked;
1449                 setPrefsEnabledState(mLastEnabledState);
1450             }
1451         }
1452     }
1453
1454     @Override
1455     public void onActivityResult(int requestCode, int resultCode, Intent data) {
1456         if (requestCode == RESULT_DEBUG_APP) {
1457             if (resultCode == Activity.RESULT_OK) {
1458                 mDebugApp = data.getAction();
1459                 writeDebuggerOptions();
1460                 updateDebuggerOptions();
1461             }
1462         } else if (requestCode == REQUEST_CODE_ENABLE_OEM_UNLOCK) {
1463             if (resultCode == Activity.RESULT_OK) {
1464                 if (mEnableOemUnlock.isChecked()) {
1465                     confirmEnableOemUnlock();
1466                 } else {
1467                     Utils.setOemUnlockEnabled(getActivity(), false);
1468                 }
1469             }
1470         } else {
1471             super.onActivityResult(requestCode, resultCode, data);
1472         }
1473     }
1474
1475     @Override
1476     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
1477         if (Utils.isMonkeyRunning()) {
1478             return false;
1479         }
1480
1481         if (preference == mEnableAdb) {
1482             if (mEnableAdb.isChecked()) {
1483                 mDialogClicked = false;
1484                 if (mAdbDialog != null) dismissDialogs();
1485                 mAdbDialog = new AlertDialog.Builder(getActivity()).setMessage(
1486                         getActivity().getResources().getString(R.string.adb_warning_message))
1487                         .setTitle(R.string.adb_warning_title)
1488                         .setPositiveButton(android.R.string.yes, this)
1489                         .setNegativeButton(android.R.string.no, this)
1490                         .show();
1491                 mAdbDialog.setOnDismissListener(this);
1492             } else {
1493                 Settings.Global.putInt(getActivity().getContentResolver(),
1494                         Settings.Global.ADB_ENABLED, 0);
1495                 mVerifyAppsOverUsb.setEnabled(false);
1496                 mVerifyAppsOverUsb.setChecked(false);
1497                 updateBugreportOptions();
1498             }
1499         } else if (preference == mClearAdbKeys) {
1500             if (mAdbKeysDialog != null) dismissDialogs();
1501             mAdbKeysDialog = new AlertDialog.Builder(getActivity())
1502                         .setMessage(R.string.adb_keys_warning_message)
1503                         .setPositiveButton(android.R.string.ok, this)
1504                         .setNegativeButton(android.R.string.cancel, null)
1505                         .show();
1506         } else if (preference == mEnableTerminal) {
1507             final PackageManager pm = getActivity().getPackageManager();
1508             pm.setApplicationEnabledSetting(TERMINAL_APP_PACKAGE,
1509                     mEnableTerminal.isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
1510                             : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
1511         } else if (preference == mBugreportInPower) {
1512             Settings.Secure.putInt(getActivity().getContentResolver(),
1513                     Settings.Secure.BUGREPORT_IN_POWER_MENU,
1514                     mBugreportInPower.isChecked() ? 1 : 0);
1515         } else if (preference == mKeepScreenOn) {
1516             Settings.Global.putInt(getActivity().getContentResolver(),
1517                     Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
1518                     mKeepScreenOn.isChecked() ?
1519                             (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);
1520         } else if (preference == mBtHciSnoopLog) {
1521             writeBtHciSnoopLogOptions();
1522         } else if (preference == mEnableOemUnlock) {
1523             if (!showKeyguardConfirmation(getResources(), REQUEST_CODE_ENABLE_OEM_UNLOCK)) {
1524                 if (mEnableOemUnlock.isChecked()) {
1525                     confirmEnableOemUnlock();
1526                 } else {
1527                     Utils.setOemUnlockEnabled(getActivity(), false);
1528                 }
1529             }
1530         } else if (preference == mAllowMockLocation) {
1531             Settings.Secure.putInt(getActivity().getContentResolver(),
1532                     Settings.Secure.ALLOW_MOCK_LOCATION,
1533                     mAllowMockLocation.isChecked() ? 1 : 0);
1534         } else if (preference == mDebugViewAttributes) {
1535             Settings.Global.putInt(getActivity().getContentResolver(),
1536                     Settings.Global.DEBUG_VIEW_ATTRIBUTES,
1537                     mDebugViewAttributes.isChecked() ? 1 : 0);
1538         } else if (preference == mDebugAppPref) {
1539             startActivityForResult(new Intent(getActivity(), AppPicker.class), RESULT_DEBUG_APP);
1540         } else if (preference == mWaitForDebugger) {
1541             writeDebuggerOptions();
1542         } else if (preference == mVerifyAppsOverUsb) {
1543             writeVerifyAppsOverUsbOptions();
1544         } else if (preference == mStrictMode) {
1545             writeStrictModeVisualOptions();
1546         } else if (preference == mPointerLocation) {
1547             writePointerLocationOptions();
1548         } else if (preference == mShowTouches) {
1549             writeShowTouchesOptions();
1550         } else if (preference == mShowScreenUpdates) {
1551             writeShowUpdatesOption();
1552         } else if (preference == mDisableOverlays) {
1553             writeDisableOverlaysOption();
1554         } else if (preference == mEnableMultiWindow) {
1555             if (mEnableMultiWindow.isChecked()) {
1556                 confirmEnableMultiWindowMode();
1557             } else {
1558                 setEnableMultiWindow(false);
1559             }
1560         } else if (preference == mShowCpuUsage) {
1561             writeCpuUsageOptions();
1562         } else if (preference == mImmediatelyDestroyActivities) {
1563             writeImmediatelyDestroyActivitiesOptions();
1564         } else if (preference == mShowAllANRs) {
1565             writeShowAllANRsOptions();
1566         } else if (preference == mForceHardwareUi) {
1567             writeHardwareUiOptions();
1568         } else if (preference == mForceMsaa) {
1569             writeMsaaOptions();
1570         } else if (preference == mShowHwScreenUpdates) {
1571             writeShowHwScreenUpdatesOptions();
1572         } else if (preference == mShowHwLayersUpdates) {
1573             writeShowHwLayersUpdatesOptions();
1574         } else if (preference == mDebugLayout) {
1575             writeDebugLayoutOptions();
1576         } else if (preference == mForceRtlLayout) {
1577             writeForceRtlOptions();
1578         } else if (preference == mWifiDisplayCertification) {
1579             writeWifiDisplayCertificationOptions();
1580         } else if (preference == mWifiVerboseLogging) {
1581             writeWifiVerboseLoggingOptions();
1582         } else if (preference == mWifiAggressiveHandover) {
1583             writeWifiAggressiveHandoverOptions();
1584         } else if (preference == mWifiAllowScansWithTraffic) {
1585             writeWifiAllowScansWithTrafficOptions();
1586         } else if (preference == mLegacyDhcpClient) {
1587             writeLegacyDhcpClientOptions();
1588         } else if (preference == mUseAwesomePlayer) {
1589             writeUseAwesomePlayerOptions();
1590         } else if (preference == mUSBAudio) {
1591             writeUSBAudioOptions();
1592         } else {
1593             return super.onPreferenceTreeClick(preferenceScreen, preference);
1594         }
1595
1596         return false;
1597     }
1598
1599     private boolean showKeyguardConfirmation(Resources resources, int requestCode) {
1600         return new ChooseLockSettingsHelper(getActivity(), this)
1601                 .launchConfirmationActivity(requestCode,
1602                         resources.getString(R.string.oem_unlock_enable_pin_prompt),
1603                         resources.getString(R.string.oem_unlock_enable_pin_description));
1604     }
1605
1606     @Override
1607     public boolean onPreferenceChange(Preference preference, Object newValue) {
1608         if (HDCP_CHECKING_KEY.equals(preference.getKey())) {
1609             SystemProperties.set(HDCP_CHECKING_PROPERTY, newValue.toString());
1610             updateHdcpValues();
1611             pokeSystemProperties();
1612             return true;
1613         } else if (preference == mLogdSize) {
1614             writeLogdSizeOption(newValue);
1615             return true;
1616         } else if (preference == mUsbConfiguration) {
1617             writeUsbConfigurationOption(newValue);
1618             return true;
1619         } else if (preference == mWindowAnimationScale) {
1620             writeAnimationScaleOption(0, mWindowAnimationScale, newValue);
1621             return true;
1622         } else if (preference == mTransitionAnimationScale) {
1623             writeAnimationScaleOption(1, mTransitionAnimationScale, newValue);
1624             return true;
1625         } else if (preference == mAnimatorDurationScale) {
1626             writeAnimationScaleOption(2, mAnimatorDurationScale, newValue);
1627             return true;
1628         } else if (preference == mOverlayDisplayDevices) {
1629             writeOverlayDisplayDevicesOptions(newValue);
1630             return true;
1631         } else if (preference == mOpenGLTraces) {
1632             writeOpenGLTracesOptions(newValue);
1633             return true;
1634         } else if (preference == mTrackFrameTime) {
1635             writeTrackFrameTimeOptions(newValue);
1636             return true;
1637         } else if (preference == mDebugHwOverdraw) {
1638             writeDebugHwOverdrawOptions(newValue);
1639             return true;
1640         } else if (preference == mShowNonRectClip) {
1641             writeShowNonRectClipOptions(newValue);
1642             return true;
1643         } else if (preference == mAppProcessLimit) {
1644             writeAppProcessLimitOptions(newValue);
1645             return true;
1646         } else if (preference == mSimulateColorSpace) {
1647             writeSimulateColorSpace(newValue);
1648             return true;
1649         }
1650         return false;
1651     }
1652
1653     private void dismissDialogs() {
1654         if (mAdbDialog != null) {
1655             mAdbDialog.dismiss();
1656             mAdbDialog = null;
1657         }
1658         if (mAdbKeysDialog != null) {
1659             mAdbKeysDialog.dismiss();
1660             mAdbKeysDialog = null;
1661         }
1662         if (mEnableDialog != null) {
1663             mEnableDialog.dismiss();
1664             mEnableDialog = null;
1665         }
1666     }
1667
1668     public void onClick(DialogInterface dialog, int which) {
1669         if (dialog == mAdbDialog) {
1670             if (which == DialogInterface.BUTTON_POSITIVE) {
1671                 mDialogClicked = true;
1672                 Settings.Global.putInt(getActivity().getContentResolver(),
1673                         Settings.Global.ADB_ENABLED, 1);
1674                 mVerifyAppsOverUsb.setEnabled(true);
1675                 updateVerifyAppsOverUsbOptions();
1676                 updateBugreportOptions();
1677             } else {
1678                 // Reset the toggle
1679                 mEnableAdb.setChecked(false);
1680             }
1681         } else if (dialog == mAdbKeysDialog) {
1682             if (which == DialogInterface.BUTTON_POSITIVE) {
1683                 try {
1684                     IBinder b = ServiceManager.getService(Context.USB_SERVICE);
1685                     IUsbManager service = IUsbManager.Stub.asInterface(b);
1686                     service.clearUsbDebuggingKeys();
1687                 } catch (RemoteException e) {
1688                     Log.e(TAG, "Unable to clear adb keys", e);
1689                 }
1690             }
1691         } else if (dialog == mEnableDialog) {
1692             if (which == DialogInterface.BUTTON_POSITIVE) {
1693                 mDialogClicked = true;
1694                 Settings.Global.putInt(getActivity().getContentResolver(),
1695                         Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
1696                 mLastEnabledState = true;
1697                 setPrefsEnabledState(mLastEnabledState);
1698             } else {
1699                 // Reset the toggle
1700                 mSwitchBar.setChecked(false);
1701             }
1702         }
1703     }
1704
1705     public void onDismiss(DialogInterface dialog) {
1706         // Assuming that onClick gets called first
1707         if (dialog == mAdbDialog) {
1708             if (!mDialogClicked) {
1709                 mEnableAdb.setChecked(false);
1710             }
1711             mAdbDialog = null;
1712         } else if (dialog == mEnableDialog) {
1713             if (!mDialogClicked) {
1714                 mSwitchBar.setChecked(false);
1715             }
1716             mEnableDialog = null;
1717         }
1718     }
1719
1720     @Override
1721     public void onDestroy() {
1722         dismissDialogs();
1723         super.onDestroy();
1724     }
1725
1726     void pokeSystemProperties() {
1727         if (!mDontPokeProperties) {
1728             //noinspection unchecked
1729             (new SystemPropPoker()).execute();
1730         }
1731     }
1732
1733     private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
1734         @Override
1735         public void onReceive(Context context, Intent intent) {
1736             updateUsbConfigurationValues();
1737         }
1738     };
1739
1740     static class SystemPropPoker extends AsyncTask<Void, Void, Void> {
1741         @Override
1742         protected Void doInBackground(Void... params) {
1743             String[] services;
1744             try {
1745                 services = ServiceManager.listServices();
1746             } catch (RemoteException e) {
1747                 return null;
1748             }
1749             for (String service : services) {
1750                 IBinder obj = ServiceManager.checkService(service);
1751                 if (obj != null) {
1752                     Parcel data = Parcel.obtain();
1753                     try {
1754                         obj.transact(IBinder.SYSPROPS_TRANSACTION, data, null, 0);
1755                     } catch (RemoteException e) {
1756                     } catch (Exception e) {
1757                         Log.i(TAG, "Someone wrote a bad service '" + service
1758                                 + "' that doesn't like to be poked: " + e);
1759                     }
1760                     data.recycle();
1761                 }
1762             }
1763             return null;
1764         }
1765     }
1766
1767     private static boolean isPackageInstalled(Context context, String packageName) {
1768         try {
1769             return context.getPackageManager().getPackageInfo(packageName, 0) != null;
1770         } catch (NameNotFoundException e) {
1771             return false;
1772         }
1773     }
1774
1775     /**
1776      * For Search.
1777      */
1778     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
1779             new BaseSearchIndexProvider() {
1780
1781                 private boolean isShowingDeveloperOptions(Context context) {
1782                     return context.getSharedPreferences(DevelopmentSettings.PREF_FILE,
1783                             Context.MODE_PRIVATE).getBoolean(
1784                                     DevelopmentSettings.PREF_SHOW,
1785                                     android.os.Build.TYPE.equals("eng"));
1786                 }
1787
1788                 @Override
1789                 public List<SearchIndexableResource> getXmlResourcesToIndex(
1790                         Context context, boolean enabled) {
1791
1792                     if (!isShowingDeveloperOptions(context)) {
1793                         return null;
1794                     }
1795
1796                     final SearchIndexableResource sir = new SearchIndexableResource(context);
1797                     sir.xmlResId = R.xml.development_prefs;
1798                     return Arrays.asList(sir);
1799                 }
1800
1801                 @Override
1802                 public List<String> getNonIndexableKeys(Context context) {
1803                     if (!isShowingDeveloperOptions(context)) {
1804                         return null;
1805                     }
1806
1807                     final List<String> keys = new ArrayList<String>();
1808                     if (!showEnableOemUnlockPreference()) {
1809                         keys.add(ENABLE_OEM_UNLOCK);
1810                     }
1811                     if (!showEnableMultiWindowPreference()) {
1812                         keys.add(ENABLE_MULTI_WINDOW_KEY);
1813                     }
1814                     return keys;
1815                 }
1816             };
1817 }