OSDN Git Service

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