OSDN Git Service

Merge "First step in refactoring Index.java"
[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.Manifest;
20 import android.app.Activity;
21 import android.app.ActivityManager;
22 import android.app.AlertDialog;
23 import android.app.AppOpsManager;
24 import android.app.AppOpsManager.PackageOps;
25 import android.app.Dialog;
26 import android.app.admin.DevicePolicyManager;
27 import android.app.backup.IBackupManager;
28 import android.bluetooth.BluetoothAdapter;
29 import android.content.BroadcastReceiver;
30 import android.content.ComponentName;
31 import android.content.ContentResolver;
32 import android.content.Context;
33 import android.content.DialogInterface;
34 import android.content.Intent;
35 import android.content.IntentFilter;
36 import android.content.pm.ApplicationInfo;
37 import android.content.pm.IShortcutService;
38 import android.content.pm.PackageManager;
39 import android.content.pm.PackageManager.NameNotFoundException;
40 import android.content.pm.ResolveInfo;
41 import android.content.res.Resources;
42 import android.hardware.usb.IUsbManager;
43 import android.hardware.usb.UsbManager;
44 import android.net.wifi.WifiManager;
45 import android.os.AsyncTask;
46 import android.os.BatteryManager;
47 import android.os.Build;
48 import android.os.Bundle;
49 import android.os.IBinder;
50 import android.os.Parcel;
51 import android.os.RemoteException;
52 import android.os.ServiceManager;
53 import android.os.StrictMode;
54 import android.os.SystemProperties;
55 import android.os.UserHandle;
56 import android.os.UserManager;
57 import android.os.storage.IStorageManager;
58 import android.provider.SearchIndexableResource;
59 import android.provider.Settings;
60 import android.service.persistentdata.PersistentDataBlockManager;
61 import android.support.annotation.VisibleForTesting;
62 import android.support.v14.preference.SwitchPreference;
63 import android.support.v7.preference.ListPreference;
64 import android.support.v7.preference.Preference;
65 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
66 import android.support.v7.preference.PreferenceGroup;
67 import android.support.v7.preference.PreferenceScreen;
68 import android.telephony.TelephonyManager;
69 import android.text.TextUtils;
70 import android.util.Log;
71 import android.view.IWindowManager;
72 import android.view.LayoutInflater;
73 import android.view.ThreadedRenderer;
74 import android.view.View;
75 import android.view.ViewGroup;
76 import android.view.accessibility.AccessibilityManager;
77 import android.webkit.IWebViewUpdateService;
78 import android.webkit.WebViewProviderInfo;
79 import android.widget.Switch;
80 import android.widget.Toast;
81
82 import com.android.internal.app.LocalePicker;
83 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
84 import com.android.settings.applications.BackgroundCheckSummary;
85 import com.android.settings.dashboard.DashboardFeatureProvider;
86 import com.android.settings.development.BugReportPreferenceController;
87 import com.android.settings.development.BugReportInPowerPreferenceController;
88 import com.android.settings.fuelgauge.InactiveApps;
89 import com.android.settings.overlay.FeatureFactory;
90 import com.android.settings.search.BaseSearchIndexProvider;
91 import com.android.settings.search.Indexable;
92 import com.android.settings.widget.SwitchBar;
93 import com.android.settingslib.RestrictedLockUtils;
94 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
95 import com.android.settingslib.RestrictedSwitchPreference;
96 import com.android.settingslib.drawer.CategoryKey;
97
98 import java.util.ArrayList;
99 import java.util.Arrays;
100 import java.util.HashSet;
101 import java.util.List;
102
103 /*
104  * Displays preferences for application developers.
105  */
106 public class DevelopmentSettings extends RestrictedSettingsFragment
107         implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
108         OnPreferenceChangeListener, SwitchBar.OnSwitchChangeListener, Indexable {
109     private static final String TAG = "DevelopmentSettings";
110
111     /**
112      * Preference file were development settings prefs are stored.
113      */
114     public static final String PREF_FILE = "development";
115
116     /**
117      * Whether to show the development settings to the user.  Default is false.
118      */
119     public static final String PREF_SHOW = "show";
120
121     private static final String ENABLE_ADB = "enable_adb";
122     private static final String CLEAR_ADB_KEYS = "clear_adb_keys";
123     private static final String ENABLE_TERMINAL = "enable_terminal";
124     private static final String KEEP_SCREEN_ON = "keep_screen_on";
125     private static final String BT_HCI_SNOOP_LOG = "bt_hci_snoop_log";
126     private static final String WEBVIEW_PROVIDER_KEY = "select_webview_provider";
127     private static final String WEBVIEW_MULTIPROCESS_KEY = "enable_webview_multiprocess";
128     private static final String ENABLE_OEM_UNLOCK = "oem_unlock_enable";
129     private static final String HDCP_CHECKING_KEY = "hdcp_checking";
130     private static final String HDCP_CHECKING_PROPERTY = "persist.sys.hdcp_checking";
131     private static final String LOCAL_BACKUP_PASSWORD = "local_backup_password";
132     private static final String HARDWARE_UI_PROPERTY = "persist.sys.ui.hw";
133     private static final String MSAA_PROPERTY = "debug.egl.force_msaa";
134     private static final String OPENGL_TRACES_PROPERTY = "debug.egl.trace";
135     private static final String TUNER_UI_KEY = "tuner_ui";
136     private static final String COLOR_TEMPERATURE_PROPERTY = "persist.sys.debug.color_temp";
137
138     private static final String DEBUG_APP_KEY = "debug_app";
139     private static final String WAIT_FOR_DEBUGGER_KEY = "wait_for_debugger";
140     private static final String MOCK_LOCATION_APP_KEY = "mock_location_app";
141     private static final String VERIFY_APPS_OVER_USB_KEY = "verify_apps_over_usb";
142     private static final String DEBUG_VIEW_ATTRIBUTES = "debug_view_attributes";
143     private static final String FORCE_ALLOW_ON_EXTERNAL_KEY = "force_allow_on_external";
144     private static final String STRICT_MODE_KEY = "strict_mode";
145     private static final String POINTER_LOCATION_KEY = "pointer_location";
146     private static final String SHOW_TOUCHES_KEY = "show_touches";
147     private static final String SHOW_SCREEN_UPDATES_KEY = "show_screen_updates";
148     private static final String DISABLE_OVERLAYS_KEY = "disable_overlays";
149     private static final String SIMULATE_COLOR_SPACE = "simulate_color_space";
150     private static final String USB_AUDIO_KEY = "usb_audio";
151     private static final String FORCE_HARDWARE_UI_KEY = "force_hw_ui";
152     private static final String FORCE_MSAA_KEY = "force_msaa";
153     private static final String TRACK_FRAME_TIME_KEY = "track_frame_time";
154     private static final String SHOW_NON_RECTANGULAR_CLIP_KEY = "show_non_rect_clip";
155     private static final String SHOW_HW_SCREEN_UPDATES_KEY = "show_hw_screen_udpates";
156     private static final String SHOW_HW_LAYERS_UPDATES_KEY = "show_hw_layers_udpates";
157     private static final String DEBUG_HW_OVERDRAW_KEY = "debug_hw_overdraw";
158     private static final String DEBUG_LAYOUT_KEY = "debug_layout";
159     private static final String FORCE_RTL_LAYOUT_KEY = "force_rtl_layout_all_locales";
160     private static final String WINDOW_ANIMATION_SCALE_KEY = "window_animation_scale";
161     private static final String TRANSITION_ANIMATION_SCALE_KEY = "transition_animation_scale";
162     private static final String ANIMATOR_DURATION_SCALE_KEY = "animator_duration_scale";
163     private static final String OVERLAY_DISPLAY_DEVICES_KEY = "overlay_display_devices";
164     private static final String DEBUG_DEBUGGING_CATEGORY_KEY = "debug_debugging_category";
165     private static final String SELECT_LOGD_SIZE_KEY = "select_logd_size";
166     private static final String SELECT_LOGD_SIZE_PROPERTY = "persist.logd.size";
167     private static final String SELECT_LOGD_TAG_PROPERTY = "persist.log.tag";
168     // Tricky, isLoggable only checks for first character, assumes silence
169     private static final String SELECT_LOGD_TAG_SILENCE = "Settings";
170     private static final String SELECT_LOGD_SNET_TAG_PROPERTY = "persist.log.tag.snet_event_log";
171     private static final String SELECT_LOGD_RUNTIME_SNET_TAG_PROPERTY = "log.tag.snet_event_log";
172     private static final String SELECT_LOGD_DEFAULT_SIZE_PROPERTY = "ro.logd.size";
173     private static final String SELECT_LOGD_DEFAULT_SIZE_VALUE = "262144";
174     private static final String SELECT_LOGD_SVELTE_DEFAULT_SIZE_VALUE = "65536";
175     // 32768 is merely a menu marker, 64K is our lowest log buffer size we replace it with.
176     private static final String SELECT_LOGD_MINIMUM_SIZE_VALUE = "65536";
177     private static final String SELECT_LOGD_OFF_SIZE_MARKER_VALUE = "32768";
178     private static final String SELECT_LOGPERSIST_KEY = "select_logpersist";
179     private static final String SELECT_LOGPERSIST_PROPERTY = "persist.logd.logpersistd";
180     private static final String ACTUAL_LOGPERSIST_PROPERTY = "logd.logpersistd";
181     private static final String SELECT_LOGPERSIST_PROPERTY_SERVICE = "logcatd";
182     private static final String SELECT_LOGPERSIST_PROPERTY_CLEAR = "clear";
183     private static final String SELECT_LOGPERSIST_PROPERTY_STOP = "stop";
184     private static final String SELECT_LOGPERSIST_PROPERTY_BUFFER =
185             "persist.logd.logpersistd.buffer";
186     private static final String ACTUAL_LOGPERSIST_PROPERTY_BUFFER = "logd.logpersistd.buffer";
187     private static final String ACTUAL_LOGPERSIST_PROPERTY_ENABLE = "logd.logpersistd.enable";
188
189     private static final String WIFI_DISPLAY_CERTIFICATION_KEY = "wifi_display_certification";
190     private static final String WIFI_VERBOSE_LOGGING_KEY = "wifi_verbose_logging";
191     private static final String WIFI_AGGRESSIVE_HANDOVER_KEY = "wifi_aggressive_handover";
192     private static final String WIFI_ALLOW_SCAN_WITH_TRAFFIC_KEY = "wifi_allow_scan_with_traffic";
193     private static final String USB_CONFIGURATION_KEY = "select_usb_configuration";
194     private static final String MOBILE_DATA_ALWAYS_ON = "mobile_data_always_on";
195     private static final String KEY_COLOR_MODE = "color_mode";
196     private static final String FORCE_RESIZABLE_KEY = "force_resizable_activities";
197     private static final String COLOR_TEMPERATURE_KEY = "color_temperature";
198
199     private static final String BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_KEY =
200             "bluetooth_disable_absolute_volume";
201     private static final String BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY =
202             "persist.bluetooth.disableabsvol";
203
204     private static final String INACTIVE_APPS_KEY = "inactive_apps";
205
206     private static final String IMMEDIATELY_DESTROY_ACTIVITIES_KEY
207             = "immediately_destroy_activities";
208     private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit";
209
210     private static final String BACKGROUND_CHECK_KEY = "background_check";
211
212     private static final String SHOW_ALL_ANRS_KEY = "show_all_anrs";
213
214     private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
215
216     private static final String TERMINAL_APP_PACKAGE = "com.android.terminal";
217
218     private static final String KEY_CONVERT_FBE = "convert_to_file_encryption";
219
220     private static final String OTA_DISABLE_AUTOMATIC_UPDATE_KEY = "ota_disable_automatic_update";
221
222     private static final int RESULT_DEBUG_APP = 1000;
223     private static final int RESULT_MOCK_LOCATION_APP = 1001;
224
225     private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst";
226     private static final String FLASH_LOCKED_PROP = "ro.boot.flash.locked";
227
228     private static final String SHORTCUT_MANAGER_RESET_KEY = "reset_shortcut_manager_throttling";
229
230     private static final int REQUEST_CODE_ENABLE_OEM_UNLOCK = 0;
231
232     private static final int[] MOCK_LOCATION_APP_OPS = new int[]{AppOpsManager.OP_MOCK_LOCATION};
233
234     private IWindowManager mWindowManager;
235     private IBackupManager mBackupManager;
236     private IWebViewUpdateService mWebViewUpdateService;
237     private DevicePolicyManager mDpm;
238     private UserManager mUm;
239     private WifiManager mWifiManager;
240     private PersistentDataBlockManager mOemUnlockManager;
241     private TelephonyManager mTelephonyManager;
242
243     private SwitchBar mSwitchBar;
244     private boolean mLastEnabledState;
245     private boolean mHaveDebugSettings;
246     private boolean mDontPokeProperties;
247
248     private SwitchPreference mEnableAdb;
249     private Preference mClearAdbKeys;
250     private SwitchPreference mEnableTerminal;
251     private RestrictedSwitchPreference mKeepScreenOn;
252     private SwitchPreference mBtHciSnoopLog;
253     private RestrictedSwitchPreference mEnableOemUnlock;
254     private SwitchPreference mDebugViewAttributes;
255     private SwitchPreference mForceAllowOnExternal;
256
257     private PreferenceScreen mPassword;
258     private String mDebugApp;
259     private Preference mDebugAppPref;
260
261     private String mMockLocationApp;
262     private Preference mMockLocationAppPref;
263
264     private SwitchPreference mWaitForDebugger;
265     private SwitchPreference mVerifyAppsOverUsb;
266     private SwitchPreference mWifiDisplayCertification;
267     private SwitchPreference mWifiVerboseLogging;
268     private SwitchPreference mWifiAggressiveHandover;
269     private SwitchPreference mMobileDataAlwaysOn;
270     private SwitchPreference mBluetoothDisableAbsVolume;
271     private SwitchPreference mOtaDisableAutomaticUpdate;
272
273     private SwitchPreference mWifiAllowScansWithTraffic;
274     private SwitchPreference mStrictMode;
275     private SwitchPreference mPointerLocation;
276     private SwitchPreference mShowTouches;
277     private SwitchPreference mShowScreenUpdates;
278     private SwitchPreference mDisableOverlays;
279     private SwitchPreference mForceHardwareUi;
280     private SwitchPreference mForceMsaa;
281     private SwitchPreference mShowHwScreenUpdates;
282     private SwitchPreference mShowHwLayersUpdates;
283     private SwitchPreference mDebugLayout;
284     private SwitchPreference mForceRtlLayout;
285     private ListPreference mDebugHwOverdraw;
286     private ListPreference mLogdSize;
287     private ListPreference mLogpersist;
288     private ListPreference mUsbConfiguration;
289     private ListPreference mTrackFrameTime;
290     private ListPreference mShowNonRectClip;
291     private ListPreference mWindowAnimationScale;
292     private ListPreference mTransitionAnimationScale;
293     private ListPreference mAnimatorDurationScale;
294     private ListPreference mOverlayDisplayDevices;
295
296     private SwitchPreference mWebViewMultiprocess;
297     private ListPreference mWebViewProvider;
298
299     private ListPreference mSimulateColorSpace;
300
301     private SwitchPreference mUSBAudio;
302     private SwitchPreference mImmediatelyDestroyActivities;
303
304     private ListPreference mAppProcessLimit;
305
306     private SwitchPreference mShowAllANRs;
307
308     private ColorModePreference mColorModePreference;
309
310     private SwitchPreference mForceResizable;
311
312     private SwitchPreference mColorTemperaturePreference;
313
314     private final ArrayList<Preference> mAllPrefs = new ArrayList<Preference>();
315
316     private final ArrayList<SwitchPreference> mResetSwitchPrefs
317             = new ArrayList<SwitchPreference>();
318
319     private final HashSet<Preference> mDisabledPrefs = new HashSet<Preference>();
320     // To track whether a confirmation dialog was clicked.
321     private boolean mDialogClicked;
322     private Dialog mEnableDialog;
323     private Dialog mAdbDialog;
324
325     private Dialog mAdbKeysDialog;
326     private boolean mUnavailable;
327
328     private boolean mLogpersistCleared;
329     private Dialog mLogpersistClearDialog;
330     private DashboardFeatureProvider mDashboardFeatureProvider;
331     private BugReportPreferenceController mBugReportController;
332     private BugReportInPowerPreferenceController mBugReportInPowerController;
333
334     public DevelopmentSettings() {
335         super(UserManager.DISALLOW_DEBUGGING_FEATURES);
336     }
337
338     @Override
339     public int getMetricsCategory() {
340         return MetricsEvent.DEVELOPMENT;
341     }
342
343     @Override
344     public void onAttach(Context context) {
345         super.onAttach(context);
346         mDashboardFeatureProvider = FeatureFactory.getFactory(context)
347                 .getDashboardFeatureProvider(context);
348     }
349
350     @Override
351     public void onCreate(Bundle icicle) {
352         super.onCreate(icicle);
353
354         mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
355         mBackupManager = IBackupManager.Stub.asInterface(
356                 ServiceManager.getService(Context.BACKUP_SERVICE));
357         mWebViewUpdateService =
358                 IWebViewUpdateService.Stub.asInterface(ServiceManager.getService("webviewupdate"));
359         mOemUnlockManager = (PersistentDataBlockManager) getActivity()
360                 .getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
361         mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
362
363         mDpm = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
364         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
365
366         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
367
368         mBugReportController = new BugReportPreferenceController(getActivity());
369         mBugReportInPowerController = new BugReportInPowerPreferenceController(getActivity());
370
371         setIfOnlyAvailableForAdmins(true);
372         if (isUiRestricted() || !Utils.isDeviceProvisioned(getActivity())) {
373             // Block access to developer options if the user is not the owner, if user policy
374             // restricts it, or if the device has not been provisioned
375             mUnavailable = true;
376             setPreferenceScreen(new PreferenceScreen(getPrefContext(), null));
377             return;
378         }
379
380         addPreferencesFromResource(R.xml.development_prefs);
381
382         final PreferenceGroup debugDebuggingCategory = (PreferenceGroup)
383                 findPreference(DEBUG_DEBUGGING_CATEGORY_KEY);
384         mEnableAdb = findAndInitSwitchPref(ENABLE_ADB);
385         mClearAdbKeys = findPreference(CLEAR_ADB_KEYS);
386         if (!SystemProperties.getBoolean("ro.adb.secure", false)) {
387             if (debugDebuggingCategory != null) {
388                 debugDebuggingCategory.removePreference(mClearAdbKeys);
389             }
390         }
391         mAllPrefs.add(mClearAdbKeys);
392         mEnableTerminal = findAndInitSwitchPref(ENABLE_TERMINAL);
393         if (!isPackageInstalled(getActivity(), TERMINAL_APP_PACKAGE)) {
394             debugDebuggingCategory.removePreference(mEnableTerminal);
395             mEnableTerminal = null;
396         }
397
398         mBugReportController.displayPreference(getPreferenceScreen());
399         mBugReportInPowerController.displayPreference(getPreferenceScreen());
400
401         mKeepScreenOn = (RestrictedSwitchPreference) findAndInitSwitchPref(KEEP_SCREEN_ON);
402         mBtHciSnoopLog = findAndInitSwitchPref(BT_HCI_SNOOP_LOG);
403         mEnableOemUnlock = (RestrictedSwitchPreference) findAndInitSwitchPref(ENABLE_OEM_UNLOCK);
404         if (!showEnableOemUnlockPreference()) {
405             removePreference(mEnableOemUnlock);
406             mEnableOemUnlock = null;
407         }
408
409         mDebugViewAttributes = findAndInitSwitchPref(DEBUG_VIEW_ATTRIBUTES);
410         mForceAllowOnExternal = findAndInitSwitchPref(FORCE_ALLOW_ON_EXTERNAL_KEY);
411         mPassword = (PreferenceScreen) findPreference(LOCAL_BACKUP_PASSWORD);
412         mAllPrefs.add(mPassword);
413
414         if (!mUm.isAdminUser()) {
415             disableForUser(mEnableAdb);
416             disableForUser(mClearAdbKeys);
417             disableForUser(mEnableTerminal);
418             disableForUser(mPassword);
419         }
420
421         mDebugAppPref = findPreference(DEBUG_APP_KEY);
422         mAllPrefs.add(mDebugAppPref);
423         mWaitForDebugger = findAndInitSwitchPref(WAIT_FOR_DEBUGGER_KEY);
424
425         mMockLocationAppPref = findPreference(MOCK_LOCATION_APP_KEY);
426         mAllPrefs.add(mMockLocationAppPref);
427
428         mVerifyAppsOverUsb = findAndInitSwitchPref(VERIFY_APPS_OVER_USB_KEY);
429         if (!showVerifierSetting()) {
430             if (debugDebuggingCategory != null) {
431                 debugDebuggingCategory.removePreference(mVerifyAppsOverUsb);
432             } else {
433                 mVerifyAppsOverUsb.setEnabled(false);
434             }
435         }
436         mStrictMode = findAndInitSwitchPref(STRICT_MODE_KEY);
437         mPointerLocation = findAndInitSwitchPref(POINTER_LOCATION_KEY);
438         mShowTouches = findAndInitSwitchPref(SHOW_TOUCHES_KEY);
439         mShowScreenUpdates = findAndInitSwitchPref(SHOW_SCREEN_UPDATES_KEY);
440         mDisableOverlays = findAndInitSwitchPref(DISABLE_OVERLAYS_KEY);
441         mForceHardwareUi = findAndInitSwitchPref(FORCE_HARDWARE_UI_KEY);
442         mForceMsaa = findAndInitSwitchPref(FORCE_MSAA_KEY);
443         mTrackFrameTime = addListPreference(TRACK_FRAME_TIME_KEY);
444         mShowNonRectClip = addListPreference(SHOW_NON_RECTANGULAR_CLIP_KEY);
445         mShowHwScreenUpdates = findAndInitSwitchPref(SHOW_HW_SCREEN_UPDATES_KEY);
446         mShowHwLayersUpdates = findAndInitSwitchPref(SHOW_HW_LAYERS_UPDATES_KEY);
447         mDebugLayout = findAndInitSwitchPref(DEBUG_LAYOUT_KEY);
448         mForceRtlLayout = findAndInitSwitchPref(FORCE_RTL_LAYOUT_KEY);
449         mDebugHwOverdraw = addListPreference(DEBUG_HW_OVERDRAW_KEY);
450         mWifiDisplayCertification = findAndInitSwitchPref(WIFI_DISPLAY_CERTIFICATION_KEY);
451         mWifiVerboseLogging = findAndInitSwitchPref(WIFI_VERBOSE_LOGGING_KEY);
452         mWifiAggressiveHandover = findAndInitSwitchPref(WIFI_AGGRESSIVE_HANDOVER_KEY);
453         mWifiAllowScansWithTraffic = findAndInitSwitchPref(WIFI_ALLOW_SCAN_WITH_TRAFFIC_KEY);
454         mMobileDataAlwaysOn = findAndInitSwitchPref(MOBILE_DATA_ALWAYS_ON);
455         mLogdSize = addListPreference(SELECT_LOGD_SIZE_KEY);
456         if ("1".equals(SystemProperties.get("ro.debuggable", "0"))) {
457             mLogpersist = addListPreference(SELECT_LOGPERSIST_KEY);
458         } else {
459             mLogpersist = (ListPreference) findPreference(SELECT_LOGPERSIST_KEY);
460             if (mLogpersist != null) {
461                 mLogpersist.setEnabled(false);
462                 if (debugDebuggingCategory != null) {
463                     debugDebuggingCategory.removePreference(mLogpersist);
464                 }
465             }
466             mLogpersist = null;
467         }
468         mUsbConfiguration = addListPreference(USB_CONFIGURATION_KEY);
469         mWebViewProvider = addListPreference(WEBVIEW_PROVIDER_KEY);
470         mWebViewMultiprocess = findAndInitSwitchPref(WEBVIEW_MULTIPROCESS_KEY);
471         mBluetoothDisableAbsVolume = findAndInitSwitchPref(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_KEY);
472
473         mWindowAnimationScale = addListPreference(WINDOW_ANIMATION_SCALE_KEY);
474         mTransitionAnimationScale = addListPreference(TRANSITION_ANIMATION_SCALE_KEY);
475         mAnimatorDurationScale = addListPreference(ANIMATOR_DURATION_SCALE_KEY);
476         mOverlayDisplayDevices = addListPreference(OVERLAY_DISPLAY_DEVICES_KEY);
477         mSimulateColorSpace = addListPreference(SIMULATE_COLOR_SPACE);
478         mUSBAudio = findAndInitSwitchPref(USB_AUDIO_KEY);
479         mForceResizable = findAndInitSwitchPref(FORCE_RESIZABLE_KEY);
480
481         mImmediatelyDestroyActivities = (SwitchPreference) findPreference(
482                 IMMEDIATELY_DESTROY_ACTIVITIES_KEY);
483         mAllPrefs.add(mImmediatelyDestroyActivities);
484         mResetSwitchPrefs.add(mImmediatelyDestroyActivities);
485
486         mAppProcessLimit = addListPreference(APP_PROCESS_LIMIT_KEY);
487
488         mShowAllANRs = (SwitchPreference) findPreference(
489                 SHOW_ALL_ANRS_KEY);
490         mAllPrefs.add(mShowAllANRs);
491         mResetSwitchPrefs.add(mShowAllANRs);
492
493         Preference hdcpChecking = findPreference(HDCP_CHECKING_KEY);
494         if (hdcpChecking != null) {
495             mAllPrefs.add(hdcpChecking);
496             removePreferenceForProduction(hdcpChecking);
497         }
498
499         PreferenceScreen convertFbePreference =
500                 (PreferenceScreen) findPreference(KEY_CONVERT_FBE);
501
502         try {
503             IBinder service = ServiceManager.getService("mount");
504             IStorageManager storageManager = IStorageManager.Stub.asInterface(service);
505             if (!storageManager.isConvertibleToFBE()) {
506                 removePreference(KEY_CONVERT_FBE);
507             } else if ("file".equals(SystemProperties.get("ro.crypto.type", "none"))) {
508                 convertFbePreference.setEnabled(false);
509                 convertFbePreference.setSummary(getResources()
510                         .getString(R.string.convert_to_file_encryption_done));
511             }
512         } catch (RemoteException e) {
513             removePreference(KEY_CONVERT_FBE);
514         }
515
516         mOtaDisableAutomaticUpdate = findAndInitSwitchPref(OTA_DISABLE_AUTOMATIC_UPDATE_KEY);
517
518         mColorModePreference = (ColorModePreference) findPreference(KEY_COLOR_MODE);
519         mColorModePreference.updateCurrentAndSupported();
520         if (mColorModePreference.getColorModeCount() < 2) {
521             removePreference(KEY_COLOR_MODE);
522             mColorModePreference = null;
523         }
524         updateWebViewProviderOptions();
525
526         mColorTemperaturePreference = (SwitchPreference) findPreference(COLOR_TEMPERATURE_KEY);
527         if (getResources().getBoolean(R.bool.config_enableColorTemperature)) {
528             mAllPrefs.add(mColorTemperaturePreference);
529             mResetSwitchPrefs.add(mColorTemperaturePreference);
530         } else {
531             removePreference(COLOR_TEMPERATURE_KEY);
532             mColorTemperaturePreference = null;
533         }
534
535         addDashboardCategoryPreferences();
536     }
537
538     @VisibleForTesting
539     void addDashboardCategoryPreferences() {
540         final PreferenceScreen screen = getPreferenceScreen();
541         final List<Preference> tilePrefs = mDashboardFeatureProvider.getPreferencesForCategory(
542                 getActivity(), getPrefContext(), CategoryKey.CATEGORY_SYSTEM_DEVELOPMENT);
543         if (tilePrefs != null) {
544             for (Preference preference : tilePrefs) {
545                 screen.addPreference(preference);
546             }
547         }
548     }
549
550     private ListPreference addListPreference(String prefKey) {
551         ListPreference pref = (ListPreference) findPreference(prefKey);
552         mAllPrefs.add(pref);
553         pref.setOnPreferenceChangeListener(this);
554         return pref;
555     }
556
557     private void disableForUser(Preference pref) {
558         if (pref != null) {
559             pref.setEnabled(false);
560             mDisabledPrefs.add(pref);
561         }
562     }
563
564     private SwitchPreference findAndInitSwitchPref(String key) {
565         SwitchPreference pref = (SwitchPreference) findPreference(key);
566         if (pref == null) {
567             throw new IllegalArgumentException("Cannot find preference with key = " + key);
568         }
569         mAllPrefs.add(pref);
570         mResetSwitchPrefs.add(pref);
571         return pref;
572     }
573
574     @Override
575     public void onActivityCreated(Bundle savedInstanceState) {
576         super.onActivityCreated(savedInstanceState);
577
578         final SettingsActivity activity = (SettingsActivity) getActivity();
579
580         mSwitchBar = activity.getSwitchBar();
581         if (mUnavailable) {
582             mSwitchBar.setEnabled(false);
583             return;
584         }
585
586         mSwitchBar.addOnSwitchChangeListener(this);
587     }
588
589     private boolean removePreferenceForProduction(Preference preference) {
590         if ("user".equals(Build.TYPE)) {
591             removePreference(preference);
592             return true;
593         }
594         return false;
595     }
596
597     private void removePreference(Preference preference) {
598         getPreferenceScreen().removePreference(preference);
599         mAllPrefs.remove(preference);
600         mResetSwitchPrefs.remove(preference);
601     }
602
603     private void setPrefsEnabledState(boolean enabled) {
604         for (int i = 0; i < mAllPrefs.size(); i++) {
605             Preference pref = mAllPrefs.get(i);
606             pref.setEnabled(enabled && !mDisabledPrefs.contains(pref));
607         }
608         mBugReportInPowerController.enablePreference(enabled);
609         updateAllOptions();
610     }
611
612     @Override
613     public void onResume() {
614         super.onResume();
615
616         if (mUnavailable) {
617             // Show error message
618             if (!isUiRestrictedByOnlyAdmin()) {
619                 getEmptyTextView().setText(R.string.development_settings_not_available);
620             }
621             getPreferenceScreen().removeAll();
622             return;
623         }
624
625         // A DeviceAdmin has specified a maximum time until the device
626         // will lock...  in this case we can't allow the user to turn
627         // on "stay awake when plugged in" because that would defeat the
628         // restriction.
629         final EnforcedAdmin admin = RestrictedLockUtils.checkIfMaximumTimeToLockIsSet(
630                 getActivity());
631         mKeepScreenOn.setDisabledByAdmin(admin);
632         if (admin == null) {
633             mDisabledPrefs.remove(mKeepScreenOn);
634         } else {
635             mDisabledPrefs.add(mKeepScreenOn);
636         }
637
638         final ContentResolver cr = getActivity().getContentResolver();
639         mLastEnabledState = Settings.Global.getInt(cr,
640                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
641         mSwitchBar.setChecked(mLastEnabledState);
642         setPrefsEnabledState(mLastEnabledState);
643
644         if (mHaveDebugSettings && !mLastEnabledState) {
645             // Overall debugging is disabled, but there are some debug
646             // settings that are enabled.  This is an invalid state.  Switch
647             // to debug settings being enabled, so the user knows there is
648             // stuff enabled and can turn it all off if they want.
649             Settings.Global.putInt(getActivity().getContentResolver(),
650                     Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
651             mLastEnabledState = true;
652             mSwitchBar.setChecked(mLastEnabledState);
653             setPrefsEnabledState(mLastEnabledState);
654         }
655         mSwitchBar.show();
656
657         if (mColorModePreference != null) {
658             mColorModePreference.startListening();
659             mColorModePreference.updateCurrentAndSupported();
660         }
661     }
662
663     @Override
664     public void onPause() {
665         super.onPause();
666         if (mColorModePreference != null) {
667             mColorModePreference.stopListening();
668         }
669     }
670
671     @Override
672     public View onCreateView(LayoutInflater inflater, ViewGroup container,
673             Bundle savedInstanceState) {
674         IntentFilter filter = new IntentFilter();
675         filter.addAction(UsbManager.ACTION_USB_STATE);
676         if (getActivity().registerReceiver(mUsbReceiver, filter) == null) {
677             updateUsbConfigurationValues();
678         }
679         return super.onCreateView(inflater, container, savedInstanceState);
680     }
681
682     @Override
683     public void onDestroyView() {
684         super.onDestroyView();
685
686         if (mUnavailable) {
687             return;
688         }
689         mSwitchBar.removeOnSwitchChangeListener(this);
690         mSwitchBar.hide();
691         getActivity().unregisterReceiver(mUsbReceiver);
692     }
693
694     void updateSwitchPreference(SwitchPreference switchPreference, boolean value) {
695         switchPreference.setChecked(value);
696         mHaveDebugSettings |= value;
697     }
698
699     private void updateAllOptions() {
700         final Context context = getActivity();
701         final ContentResolver cr = context.getContentResolver();
702         mHaveDebugSettings = false;
703         updateSwitchPreference(mEnableAdb, Settings.Global.getInt(cr,
704                 Settings.Global.ADB_ENABLED, 0) != 0);
705         if (mEnableTerminal != null) {
706             updateSwitchPreference(mEnableTerminal,
707                     context.getPackageManager().getApplicationEnabledSetting(TERMINAL_APP_PACKAGE)
708                             == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
709         }
710         mHaveDebugSettings |= mBugReportInPowerController.updatePreference();
711         updateSwitchPreference(mKeepScreenOn, Settings.Global.getInt(cr,
712                 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0) != 0);
713         updateSwitchPreference(mBtHciSnoopLog, Settings.Secure.getInt(cr,
714                 Settings.Secure.BLUETOOTH_HCI_LOG, 0) != 0);
715         updateSwitchPreference(mDebugViewAttributes, Settings.Global.getInt(cr,
716                 Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0);
717         updateSwitchPreference(mForceAllowOnExternal, Settings.Global.getInt(cr,
718                 Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0);
719         updateHdcpValues();
720         updatePasswordSummary();
721         updateDebuggerOptions();
722         updateMockLocation();
723         updateStrictModeVisualOptions();
724         updatePointerLocationOptions();
725         updateShowTouchesOptions();
726         updateFlingerOptions();
727         updateHardwareUiOptions();
728         updateMsaaOptions();
729         updateTrackFrameTimeOptions();
730         updateShowNonRectClipOptions();
731         updateShowHwScreenUpdatesOptions();
732         updateShowHwLayersUpdatesOptions();
733         updateDebugHwOverdrawOptions();
734         updateDebugLayoutOptions();
735         updateAnimationScaleOptions();
736         updateOverlayDisplayDevicesOptions();
737         updateImmediatelyDestroyActivitiesOptions();
738         updateAppProcessLimitOptions();
739         updateShowAllANRsOptions();
740         updateVerifyAppsOverUsbOptions();
741         updateOtaDisableAutomaticUpdateOptions();
742         updateBugreportOptions();
743         updateForceRtlOptions();
744         updateLogdSizeValues();
745         updateLogpersistValues();
746         updateWifiDisplayCertificationOptions();
747         updateWifiVerboseLoggingOptions();
748         updateWifiAggressiveHandoverOptions();
749         updateWifiAllowScansWithTrafficOptions();
750         updateMobileDataAlwaysOnOptions();
751         updateSimulateColorSpace();
752         updateUSBAudioOptions();
753         updateForceResizableOptions();
754         updateWebViewMultiprocessOptions();
755         updateWebViewProviderOptions();
756         updateOemUnlockOptions();
757         if (mColorTemperaturePreference != null) {
758             updateColorTemperature();
759         }
760         updateBluetoothDisableAbsVolumeOptions();
761     }
762
763     private void resetDangerousOptions() {
764         mDontPokeProperties = true;
765         for (int i = 0; i < mResetSwitchPrefs.size(); i++) {
766             SwitchPreference cb = mResetSwitchPrefs.get(i);
767             if (cb.isChecked()) {
768                 cb.setChecked(false);
769                 onPreferenceTreeClick(cb);
770             }
771         }
772         mBugReportInPowerController.resetPreference();
773         resetDebuggerOptions();
774         writeLogpersistOption(null, true);
775         writeLogdSizeOption(null);
776         writeAnimationScaleOption(0, mWindowAnimationScale, null);
777         writeAnimationScaleOption(1, mTransitionAnimationScale, null);
778         writeAnimationScaleOption(2, mAnimatorDurationScale, null);
779         // Only poke the color space setting if we control it.
780         if (usingDevelopmentColorSpace()) {
781             writeSimulateColorSpace(-1);
782         }
783         writeOverlayDisplayDevicesOptions(null);
784         writeAppProcessLimitOptions(null);
785         mHaveDebugSettings = false;
786         updateAllOptions();
787         mDontPokeProperties = false;
788         pokeSystemProperties();
789     }
790
791     private void updateWebViewProviderOptions() {
792         try {
793             WebViewProviderInfo[] providers = mWebViewUpdateService.getValidWebViewPackages();
794             if (providers == null) {
795                 Log.e(TAG, "No WebView providers available");
796                 return;
797             }
798             ArrayList<String> options = new ArrayList<String>();
799             ArrayList<String> values = new ArrayList<String>();
800             for (int n = 0; n < providers.length; n++) {
801                 if (Utils.isPackageEnabled(getActivity(), providers[n].packageName)) {
802                     options.add(providers[n].description);
803                     values.add(providers[n].packageName);
804                 }
805             }
806             mWebViewProvider.setEntries(options.toArray(new String[options.size()]));
807             mWebViewProvider.setEntryValues(values.toArray(new String[values.size()]));
808
809             String value = mWebViewUpdateService.getCurrentWebViewPackageName();
810             if (value == null) {
811                 value = "";
812             }
813
814             for (int i = 0; i < values.size(); i++) {
815                 if (value.contentEquals(values.get(i))) {
816                     mWebViewProvider.setValueIndex(i);
817                     return;
818                 }
819             }
820         } catch (RemoteException e) {
821         }
822     }
823
824     private void updateWebViewMultiprocessOptions() {
825         updateSwitchPreference(mWebViewMultiprocess,
826                 Settings.Global.getInt(getActivity().getContentResolver(),
827                         Settings.Global.WEBVIEW_MULTIPROCESS, 0) != 0);
828     }
829
830     private void writeWebViewMultiprocessOptions() {
831         boolean value = mWebViewMultiprocess.isChecked();
832         Settings.Global.putInt(getActivity().getContentResolver(),
833                 Settings.Global.WEBVIEW_MULTIPROCESS, value ? 1 : 0);
834
835         try {
836             String wv_package = mWebViewUpdateService.getCurrentWebViewPackageName();
837             ActivityManager.getService().killPackageDependents(
838                     wv_package, UserHandle.USER_ALL);
839         } catch (RemoteException e) {
840         }
841     }
842
843     private void updateHdcpValues() {
844         ListPreference hdcpChecking = (ListPreference) findPreference(HDCP_CHECKING_KEY);
845         if (hdcpChecking != null) {
846             String currentValue = SystemProperties.get(HDCP_CHECKING_PROPERTY);
847             String[] values = getResources().getStringArray(R.array.hdcp_checking_values);
848             String[] summaries = getResources().getStringArray(R.array.hdcp_checking_summaries);
849             int index = 1; // Defaults to drm-only. Needs to match with R.array.hdcp_checking_values
850             for (int i = 0; i < values.length; i++) {
851                 if (currentValue.equals(values[i])) {
852                     index = i;
853                     break;
854                 }
855             }
856             hdcpChecking.setValue(values[index]);
857             hdcpChecking.setSummary(summaries[index]);
858             hdcpChecking.setOnPreferenceChangeListener(this);
859         }
860     }
861
862     private void updatePasswordSummary() {
863         try {
864             if (mBackupManager.hasBackupPassword()) {
865                 mPassword.setSummary(R.string.local_backup_password_summary_change);
866             } else {
867                 mPassword.setSummary(R.string.local_backup_password_summary_none);
868             }
869         } catch (RemoteException e) {
870             // Not much we can do here
871         }
872     }
873
874     private void writeBtHciSnoopLogOptions() {
875         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
876         adapter.configHciSnoopLog(mBtHciSnoopLog.isChecked());
877         Settings.Secure.putInt(getActivity().getContentResolver(),
878                 Settings.Secure.BLUETOOTH_HCI_LOG,
879                 mBtHciSnoopLog.isChecked() ? 1 : 0);
880     }
881
882     private boolean writeWebViewProviderOptions(Object newValue) {
883         try {
884             String updatedProvider = mWebViewUpdateService.changeProviderAndSetting(
885                     newValue == null ? "" : newValue.toString());
886             updateWebViewProviderOptions();
887             return newValue != null && newValue.equals(updatedProvider);
888         } catch (RemoteException e) {
889         }
890         return false;
891     }
892
893     private void writeDebuggerOptions() {
894         try {
895             ActivityManager.getService().setDebugApp(
896                     mDebugApp, mWaitForDebugger.isChecked(), true);
897         } catch (RemoteException ex) {
898         }
899     }
900
901     private void writeMockLocation() {
902         AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
903
904         // Disable the app op of the previous mock location app if such.
905         List<PackageOps> packageOps = appOpsManager.getPackagesForOps(MOCK_LOCATION_APP_OPS);
906         if (packageOps != null) {
907             // Should be one but in case we are in a bad state due to use of command line tools.
908             for (PackageOps packageOp : packageOps) {
909                 if (packageOp.getOps().get(0).getMode() != AppOpsManager.MODE_ERRORED) {
910                     String oldMockLocationApp = packageOp.getPackageName();
911                     try {
912                         ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(
913                                 oldMockLocationApp, PackageManager.GET_DISABLED_COMPONENTS);
914                         appOpsManager.setMode(AppOpsManager.OP_MOCK_LOCATION, ai.uid,
915                                 oldMockLocationApp, AppOpsManager.MODE_ERRORED);
916                     } catch (NameNotFoundException e) {
917                         /* ignore */
918                     }
919                 }
920             }
921         }
922
923         // Enable the app op of the new mock location app if such.
924         if (!TextUtils.isEmpty(mMockLocationApp)) {
925             try {
926                 ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(
927                         mMockLocationApp, PackageManager.GET_DISABLED_COMPONENTS);
928                 appOpsManager.setMode(AppOpsManager.OP_MOCK_LOCATION, ai.uid,
929                         mMockLocationApp, AppOpsManager.MODE_ALLOWED);
930             } catch (NameNotFoundException e) {
931                 /* ignore */
932             }
933         }
934     }
935
936     private static void resetDebuggerOptions() {
937         try {
938             ActivityManager.getService().setDebugApp(
939                     null, false, true);
940         } catch (RemoteException ex) {
941         }
942     }
943
944     private void updateDebuggerOptions() {
945         mDebugApp = Settings.Global.getString(
946                 getActivity().getContentResolver(), Settings.Global.DEBUG_APP);
947         updateSwitchPreference(mWaitForDebugger, Settings.Global.getInt(
948                 getActivity().getContentResolver(), Settings.Global.WAIT_FOR_DEBUGGER, 0) != 0);
949         if (mDebugApp != null && mDebugApp.length() > 0) {
950             String label;
951             try {
952                 ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(mDebugApp,
953                         PackageManager.GET_DISABLED_COMPONENTS);
954                 CharSequence lab = getActivity().getPackageManager().getApplicationLabel(ai);
955                 label = lab != null ? lab.toString() : mDebugApp;
956             } catch (PackageManager.NameNotFoundException e) {
957                 label = mDebugApp;
958             }
959             mDebugAppPref.setSummary(getResources().getString(R.string.debug_app_set, label));
960             mWaitForDebugger.setEnabled(true);
961             mHaveDebugSettings = true;
962         } else {
963             mDebugAppPref.setSummary(getResources().getString(R.string.debug_app_not_set));
964             mWaitForDebugger.setEnabled(false);
965         }
966     }
967
968     private void updateMockLocation() {
969         AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
970
971         List<PackageOps> packageOps = appOpsManager.getPackagesForOps(MOCK_LOCATION_APP_OPS);
972         if (packageOps != null) {
973             for (PackageOps packageOp : packageOps) {
974                 if (packageOp.getOps().get(0).getMode() == AppOpsManager.MODE_ALLOWED) {
975                     mMockLocationApp = packageOps.get(0).getPackageName();
976                     break;
977                 }
978             }
979         }
980
981         if (!TextUtils.isEmpty(mMockLocationApp)) {
982             String label = mMockLocationApp;
983             try {
984                 ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(
985                         mMockLocationApp, PackageManager.GET_DISABLED_COMPONENTS);
986                 CharSequence appLabel = getPackageManager().getApplicationLabel(ai);
987                 if (appLabel != null) {
988                     label = appLabel.toString();
989                 }
990             } catch (PackageManager.NameNotFoundException e) {
991                 /* ignore */
992             }
993
994             mMockLocationAppPref.setSummary(getString(R.string.mock_location_app_set, label));
995             mHaveDebugSettings = true;
996         } else {
997             mMockLocationAppPref.setSummary(getString(R.string.mock_location_app_not_set));
998         }
999     }
1000
1001     private void updateVerifyAppsOverUsbOptions() {
1002         updateSwitchPreference(mVerifyAppsOverUsb,
1003                 Settings.Global.getInt(getActivity().getContentResolver(),
1004                         Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, 1) != 0);
1005         mVerifyAppsOverUsb.setEnabled(enableVerifierSetting());
1006     }
1007
1008     private void writeVerifyAppsOverUsbOptions() {
1009         Settings.Global.putInt(getActivity().getContentResolver(),
1010                 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
1011                 mVerifyAppsOverUsb.isChecked() ? 1 : 0);
1012     }
1013
1014     private void updateOtaDisableAutomaticUpdateOptions() {
1015         // We use the "disabled status" in code, but show the opposite text
1016         // "Automatic system updates" on screen. So a value 0 indicates the
1017         // automatic update is enabled.
1018         updateSwitchPreference(mOtaDisableAutomaticUpdate, Settings.Global.getInt(
1019                 getActivity().getContentResolver(),
1020                 Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE, 0) != 1);
1021     }
1022
1023     private void writeOtaDisableAutomaticUpdateOptions() {
1024         // We use the "disabled status" in code, but show the opposite text
1025         // "Automatic system updates" on screen. So a value 0 indicates the
1026         // automatic update is enabled.
1027         Settings.Global.putInt(getActivity().getContentResolver(),
1028                 Settings.Global.OTA_DISABLE_AUTOMATIC_UPDATE,
1029                 mOtaDisableAutomaticUpdate.isChecked() ? 0 : 1);
1030     }
1031
1032     private boolean enableVerifierSetting() {
1033         final ContentResolver cr = getActivity().getContentResolver();
1034         if (Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0) == 0) {
1035             return false;
1036         }
1037         if (Settings.Global.getInt(cr, Settings.Global.PACKAGE_VERIFIER_ENABLE, 1) == 0) {
1038             return false;
1039         } else {
1040             final PackageManager pm = getActivity().getPackageManager();
1041             final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION);
1042             verification.setType(PACKAGE_MIME_TYPE);
1043             verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1044             final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(verification, 0);
1045             if (receivers.size() == 0) {
1046                 return false;
1047             }
1048         }
1049         return true;
1050     }
1051
1052     private boolean showVerifierSetting() {
1053         return Settings.Global.getInt(getActivity().getContentResolver(),
1054                 Settings.Global.PACKAGE_VERIFIER_SETTING_VISIBLE, 1) > 0;
1055     }
1056
1057     private static boolean showEnableOemUnlockPreference() {
1058         return !SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals("");
1059     }
1060
1061     private boolean enableOemUnlockPreference() {
1062         return !isBootloaderUnlocked() && isOemUnlockAllowed();
1063     }
1064
1065     private void updateOemUnlockOptions() {
1066         if (mEnableOemUnlock != null) {
1067             updateSwitchPreference(mEnableOemUnlock, Utils.isOemUnlockEnabled(getActivity()));
1068             updateOemUnlockSettingDescription();
1069             // Showing mEnableOemUnlock preference as device has persistent data block.
1070             mEnableOemUnlock.setDisabledByAdmin(null);
1071             mEnableOemUnlock.setEnabled(enableOemUnlockPreference());
1072             if (mEnableOemUnlock.isEnabled()) {
1073                 // Check restriction, disable mEnableOemUnlock and apply policy transparency.
1074                 mEnableOemUnlock.checkRestrictionAndSetDisabled(UserManager.DISALLOW_FACTORY_RESET);
1075             }
1076             if (mEnableOemUnlock.isEnabled()) {
1077                 // Check restriction, disable mEnableOemUnlock and apply policy transparency.
1078                 mEnableOemUnlock.checkRestrictionAndSetDisabled(UserManager.DISALLOW_OEM_UNLOCK);
1079             }
1080         }
1081     }
1082
1083     private void updateBugreportOptions() {
1084         mBugReportController.enablePreference(true);
1085         mBugReportInPowerController.updateBugreportOptions();
1086     }
1087
1088     // Returns the current state of the system property that controls
1089     // strictmode flashes.  One of:
1090     //    0: not explicitly set one way or another
1091     //    1: on
1092     //    2: off
1093     private static int currentStrictModeActiveIndex() {
1094         if (TextUtils.isEmpty(SystemProperties.get(StrictMode.VISUAL_PROPERTY))) {
1095             return 0;
1096         }
1097         boolean enabled = SystemProperties.getBoolean(StrictMode.VISUAL_PROPERTY, false);
1098         return enabled ? 1 : 2;
1099     }
1100
1101     private void writeStrictModeVisualOptions() {
1102         try {
1103             mWindowManager.setStrictModeVisualIndicatorPreference(mStrictMode.isChecked()
1104                     ? "1" : "");
1105         } catch (RemoteException e) {
1106         }
1107     }
1108
1109     private void updateStrictModeVisualOptions() {
1110         updateSwitchPreference(mStrictMode, currentStrictModeActiveIndex() == 1);
1111     }
1112
1113     private void writePointerLocationOptions() {
1114         Settings.System.putInt(getActivity().getContentResolver(),
1115                 Settings.System.POINTER_LOCATION, mPointerLocation.isChecked() ? 1 : 0);
1116     }
1117
1118     private void updatePointerLocationOptions() {
1119         updateSwitchPreference(mPointerLocation,
1120                 Settings.System.getInt(getActivity().getContentResolver(),
1121                         Settings.System.POINTER_LOCATION, 0) != 0);
1122     }
1123
1124     private void writeShowTouchesOptions() {
1125         Settings.System.putInt(getActivity().getContentResolver(),
1126                 Settings.System.SHOW_TOUCHES, mShowTouches.isChecked() ? 1 : 0);
1127     }
1128
1129     private void updateShowTouchesOptions() {
1130         updateSwitchPreference(mShowTouches,
1131                 Settings.System.getInt(getActivity().getContentResolver(),
1132                         Settings.System.SHOW_TOUCHES, 0) != 0);
1133     }
1134
1135     private void updateFlingerOptions() {
1136         // magic communication with surface flinger.
1137         try {
1138             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
1139             if (flinger != null) {
1140                 Parcel data = Parcel.obtain();
1141                 Parcel reply = Parcel.obtain();
1142                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
1143                 flinger.transact(1010, data, reply, 0);
1144                 @SuppressWarnings("unused")
1145                 int showCpu = reply.readInt();
1146                 @SuppressWarnings("unused")
1147                 int enableGL = reply.readInt();
1148                 int showUpdates = reply.readInt();
1149                 updateSwitchPreference(mShowScreenUpdates, showUpdates != 0);
1150                 @SuppressWarnings("unused")
1151                 int showBackground = reply.readInt();
1152                 int disableOverlays = reply.readInt();
1153                 updateSwitchPreference(mDisableOverlays, disableOverlays != 0);
1154                 reply.recycle();
1155                 data.recycle();
1156             }
1157         } catch (RemoteException ex) {
1158         }
1159     }
1160
1161     private void writeShowUpdatesOption() {
1162         try {
1163             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
1164             if (flinger != null) {
1165                 Parcel data = Parcel.obtain();
1166                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
1167                 final int showUpdates = mShowScreenUpdates.isChecked() ? 1 : 0;
1168                 data.writeInt(showUpdates);
1169                 flinger.transact(1002, data, null, 0);
1170                 data.recycle();
1171
1172                 updateFlingerOptions();
1173             }
1174         } catch (RemoteException ex) {
1175         }
1176     }
1177
1178     private void writeDisableOverlaysOption() {
1179         try {
1180             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
1181             if (flinger != null) {
1182                 Parcel data = Parcel.obtain();
1183                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
1184                 final int disableOverlays = mDisableOverlays.isChecked() ? 1 : 0;
1185                 data.writeInt(disableOverlays);
1186                 flinger.transact(1008, data, null, 0);
1187                 data.recycle();
1188
1189                 updateFlingerOptions();
1190             }
1191         } catch (RemoteException ex) {
1192         }
1193     }
1194
1195     private void updateHardwareUiOptions() {
1196         updateSwitchPreference(mForceHardwareUi,
1197                 SystemProperties.getBoolean(HARDWARE_UI_PROPERTY, false));
1198     }
1199
1200     private void writeHardwareUiOptions() {
1201         SystemProperties.set(HARDWARE_UI_PROPERTY, mForceHardwareUi.isChecked() ? "true" : "false");
1202         pokeSystemProperties();
1203     }
1204
1205     private void updateMsaaOptions() {
1206         updateSwitchPreference(mForceMsaa, SystemProperties.getBoolean(MSAA_PROPERTY, false));
1207     }
1208
1209     private void writeMsaaOptions() {
1210         SystemProperties.set(MSAA_PROPERTY, mForceMsaa.isChecked() ? "true" : "false");
1211         pokeSystemProperties();
1212     }
1213
1214     private void updateTrackFrameTimeOptions() {
1215         String value = SystemProperties.get(ThreadedRenderer.PROFILE_PROPERTY);
1216         if (value == null) {
1217             value = "";
1218         }
1219
1220         CharSequence[] values = mTrackFrameTime.getEntryValues();
1221         for (int i = 0; i < values.length; i++) {
1222             if (value.contentEquals(values[i])) {
1223                 mTrackFrameTime.setValueIndex(i);
1224                 mTrackFrameTime.setSummary(mTrackFrameTime.getEntries()[i]);
1225                 return;
1226             }
1227         }
1228         mTrackFrameTime.setValueIndex(0);
1229         mTrackFrameTime.setSummary(mTrackFrameTime.getEntries()[0]);
1230     }
1231
1232     private void writeTrackFrameTimeOptions(Object newValue) {
1233         SystemProperties.set(ThreadedRenderer.PROFILE_PROPERTY,
1234                 newValue == null ? "" : newValue.toString());
1235         pokeSystemProperties();
1236         updateTrackFrameTimeOptions();
1237     }
1238
1239     private void updateShowNonRectClipOptions() {
1240         String value = SystemProperties.get(
1241                 ThreadedRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY);
1242         if (value == null) {
1243             value = "hide";
1244         }
1245
1246         CharSequence[] values = mShowNonRectClip.getEntryValues();
1247         for (int i = 0; i < values.length; i++) {
1248             if (value.contentEquals(values[i])) {
1249                 mShowNonRectClip.setValueIndex(i);
1250                 mShowNonRectClip.setSummary(mShowNonRectClip.getEntries()[i]);
1251                 return;
1252             }
1253         }
1254         mShowNonRectClip.setValueIndex(0);
1255         mShowNonRectClip.setSummary(mShowNonRectClip.getEntries()[0]);
1256     }
1257
1258     private void writeShowNonRectClipOptions(Object newValue) {
1259         SystemProperties.set(ThreadedRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY,
1260                 newValue == null ? "" : newValue.toString());
1261         pokeSystemProperties();
1262         updateShowNonRectClipOptions();
1263     }
1264
1265     private void updateShowHwScreenUpdatesOptions() {
1266         updateSwitchPreference(mShowHwScreenUpdates,
1267                 SystemProperties.getBoolean(ThreadedRenderer.DEBUG_DIRTY_REGIONS_PROPERTY, false));
1268     }
1269
1270     private void writeShowHwScreenUpdatesOptions() {
1271         SystemProperties.set(ThreadedRenderer.DEBUG_DIRTY_REGIONS_PROPERTY,
1272                 mShowHwScreenUpdates.isChecked() ? "true" : null);
1273         pokeSystemProperties();
1274     }
1275
1276     private void updateShowHwLayersUpdatesOptions() {
1277         updateSwitchPreference(mShowHwLayersUpdates, SystemProperties.getBoolean(
1278                 ThreadedRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY, false));
1279     }
1280
1281     private void writeShowHwLayersUpdatesOptions() {
1282         SystemProperties.set(ThreadedRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY,
1283                 mShowHwLayersUpdates.isChecked() ? "true" : null);
1284         pokeSystemProperties();
1285     }
1286
1287     private void updateDebugHwOverdrawOptions() {
1288         String value = SystemProperties.get(ThreadedRenderer.DEBUG_OVERDRAW_PROPERTY);
1289         if (value == null) {
1290             value = "";
1291         }
1292
1293         CharSequence[] values = mDebugHwOverdraw.getEntryValues();
1294         for (int i = 0; i < values.length; i++) {
1295             if (value.contentEquals(values[i])) {
1296                 mDebugHwOverdraw.setValueIndex(i);
1297                 mDebugHwOverdraw.setSummary(mDebugHwOverdraw.getEntries()[i]);
1298                 return;
1299             }
1300         }
1301         mDebugHwOverdraw.setValueIndex(0);
1302         mDebugHwOverdraw.setSummary(mDebugHwOverdraw.getEntries()[0]);
1303     }
1304
1305     private void writeDebugHwOverdrawOptions(Object newValue) {
1306         SystemProperties.set(ThreadedRenderer.DEBUG_OVERDRAW_PROPERTY,
1307                 newValue == null ? "" : newValue.toString());
1308         pokeSystemProperties();
1309         updateDebugHwOverdrawOptions();
1310     }
1311
1312     private void updateDebugLayoutOptions() {
1313         updateSwitchPreference(mDebugLayout,
1314                 SystemProperties.getBoolean(View.DEBUG_LAYOUT_PROPERTY, false));
1315     }
1316
1317     private void writeDebugLayoutOptions() {
1318         SystemProperties.set(View.DEBUG_LAYOUT_PROPERTY,
1319                 mDebugLayout.isChecked() ? "true" : "false");
1320         pokeSystemProperties();
1321     }
1322
1323     private void updateSimulateColorSpace() {
1324         final ContentResolver cr = getContentResolver();
1325         final boolean enabled = Settings.Secure.getInt(
1326                 cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0) != 0;
1327         if (enabled) {
1328             final String mode = Integer.toString(Settings.Secure.getInt(
1329                     cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
1330                     AccessibilityManager.DALTONIZER_DISABLED));
1331             mSimulateColorSpace.setValue(mode);
1332             final int index = mSimulateColorSpace.findIndexOfValue(mode);
1333             if (index < 0) {
1334                 // We're using a mode controlled by accessibility preferences.
1335                 mSimulateColorSpace.setSummary(getString(R.string.daltonizer_type_overridden,
1336                         getString(R.string.accessibility_display_daltonizer_preference_title)));
1337             } else {
1338                 mSimulateColorSpace.setSummary("%s");
1339             }
1340         } else {
1341             mSimulateColorSpace.setValue(
1342                     Integer.toString(AccessibilityManager.DALTONIZER_DISABLED));
1343         }
1344     }
1345
1346     /**
1347      * @return <code>true</code> if the color space preference is currently
1348      * controlled by development settings
1349      */
1350     private boolean usingDevelopmentColorSpace() {
1351         final ContentResolver cr = getContentResolver();
1352         final boolean enabled = Settings.Secure.getInt(
1353                 cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0) != 0;
1354         if (enabled) {
1355             final String mode = Integer.toString(Settings.Secure.getInt(
1356                     cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
1357                     AccessibilityManager.DALTONIZER_DISABLED));
1358             final int index = mSimulateColorSpace.findIndexOfValue(mode);
1359             if (index >= 0) {
1360                 // We're using a mode controlled by developer preferences.
1361                 return true;
1362             }
1363         }
1364         return false;
1365     }
1366
1367     private void writeSimulateColorSpace(Object value) {
1368         final ContentResolver cr = getContentResolver();
1369         final int newMode = Integer.parseInt(value.toString());
1370         if (newMode < 0) {
1371             Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0);
1372         } else {
1373             Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 1);
1374             Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, newMode);
1375         }
1376     }
1377
1378     private void updateColorTemperature() {
1379         updateSwitchPreference(mColorTemperaturePreference,
1380                 SystemProperties.getBoolean(COLOR_TEMPERATURE_PROPERTY, false));
1381     }
1382
1383     private void writeColorTemperature() {
1384         SystemProperties.set(COLOR_TEMPERATURE_PROPERTY,
1385                 mColorTemperaturePreference.isChecked() ? "1" : "0");
1386         pokeSystemProperties();
1387         Toast.makeText(getActivity(), R.string.color_temperature_toast, Toast.LENGTH_LONG).show();
1388     }
1389
1390     private void updateUSBAudioOptions() {
1391         updateSwitchPreference(mUSBAudio, Settings.Secure.getInt(getContentResolver(),
1392                 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 0) != 0);
1393     }
1394
1395     private void writeUSBAudioOptions() {
1396         Settings.Secure.putInt(getContentResolver(),
1397                 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED,
1398                 mUSBAudio.isChecked() ? 1 : 0);
1399     }
1400
1401     private void updateForceResizableOptions() {
1402         updateSwitchPreference(mForceResizable, Settings.Global.getInt(getContentResolver(),
1403                 Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, 0) != 0);
1404     }
1405
1406     private void writeForceResizableOptions() {
1407         Settings.Global.putInt(getContentResolver(),
1408                 Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES,
1409                 mForceResizable.isChecked() ? 1 : 0);
1410     }
1411
1412     private void updateForceRtlOptions() {
1413         updateSwitchPreference(mForceRtlLayout,
1414                 Settings.Global.getInt(getActivity().getContentResolver(),
1415                         Settings.Global.DEVELOPMENT_FORCE_RTL, 0) != 0);
1416     }
1417
1418     private void writeForceRtlOptions() {
1419         boolean value = mForceRtlLayout.isChecked();
1420         Settings.Global.putInt(getActivity().getContentResolver(),
1421                 Settings.Global.DEVELOPMENT_FORCE_RTL, value ? 1 : 0);
1422         SystemProperties.set(Settings.Global.DEVELOPMENT_FORCE_RTL, value ? "1" : "0");
1423         LocalePicker.updateLocale(getActivity().getResources().getConfiguration().locale);
1424     }
1425
1426     private void updateWifiDisplayCertificationOptions() {
1427         updateSwitchPreference(mWifiDisplayCertification, Settings.Global.getInt(
1428                 getActivity().getContentResolver(),
1429                 Settings.Global.WIFI_DISPLAY_CERTIFICATION_ON, 0) != 0);
1430     }
1431
1432     private void writeWifiDisplayCertificationOptions() {
1433         Settings.Global.putInt(getActivity().getContentResolver(),
1434                 Settings.Global.WIFI_DISPLAY_CERTIFICATION_ON,
1435                 mWifiDisplayCertification.isChecked() ? 1 : 0);
1436     }
1437
1438     private void updateWifiVerboseLoggingOptions() {
1439         boolean enabled = mWifiManager.getVerboseLoggingLevel() > 0;
1440         updateSwitchPreference(mWifiVerboseLogging, enabled);
1441     }
1442
1443     private void writeWifiVerboseLoggingOptions() {
1444         mWifiManager.enableVerboseLogging(mWifiVerboseLogging.isChecked() ? 1 : 0);
1445     }
1446
1447     private void updateWifiAggressiveHandoverOptions() {
1448         boolean enabled = mWifiManager.getAggressiveHandover() > 0;
1449         updateSwitchPreference(mWifiAggressiveHandover, enabled);
1450     }
1451
1452     private void writeWifiAggressiveHandoverOptions() {
1453         mWifiManager.enableAggressiveHandover(mWifiAggressiveHandover.isChecked() ? 1 : 0);
1454     }
1455
1456     private void updateWifiAllowScansWithTrafficOptions() {
1457         boolean enabled = mWifiManager.getAllowScansWithTraffic() > 0;
1458         updateSwitchPreference(mWifiAllowScansWithTraffic, enabled);
1459     }
1460
1461     private void writeWifiAllowScansWithTrafficOptions() {
1462         mWifiManager.setAllowScansWithTraffic(mWifiAllowScansWithTraffic.isChecked() ? 1 : 0);
1463     }
1464
1465     private void updateBluetoothDisableAbsVolumeOptions() {
1466         updateSwitchPreference(mBluetoothDisableAbsVolume,
1467                 SystemProperties.getBoolean(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY, false));
1468     }
1469
1470     private void writeBluetoothDisableAbsVolumeOptions() {
1471         SystemProperties.set(BLUETOOTH_DISABLE_ABSOLUTE_VOLUME_PROPERTY,
1472                 mBluetoothDisableAbsVolume.isChecked() ? "true" : "false");
1473     }
1474
1475     private void updateMobileDataAlwaysOnOptions() {
1476         updateSwitchPreference(mMobileDataAlwaysOn, Settings.Global.getInt(
1477                 getActivity().getContentResolver(),
1478                 Settings.Global.MOBILE_DATA_ALWAYS_ON, 0) != 0);
1479     }
1480
1481     private void writeMobileDataAlwaysOnOptions() {
1482         Settings.Global.putInt(getActivity().getContentResolver(),
1483                 Settings.Global.MOBILE_DATA_ALWAYS_ON,
1484                 mMobileDataAlwaysOn.isChecked() ? 1 : 0);
1485     }
1486
1487     private String defaultLogdSizeValue() {
1488         String defaultValue = SystemProperties.get(SELECT_LOGD_DEFAULT_SIZE_PROPERTY);
1489         if ((defaultValue == null) || (defaultValue.length() == 0)) {
1490             if (SystemProperties.get("ro.config.low_ram").equals("true")) {
1491                 defaultValue = SELECT_LOGD_SVELTE_DEFAULT_SIZE_VALUE;
1492             } else {
1493                 defaultValue = SELECT_LOGD_DEFAULT_SIZE_VALUE;
1494             }
1495         }
1496         return defaultValue;
1497     }
1498
1499     private void updateLogdSizeValues() {
1500         if (mLogdSize != null) {
1501             String currentTag = SystemProperties.get(SELECT_LOGD_TAG_PROPERTY);
1502             String currentValue = SystemProperties.get(SELECT_LOGD_SIZE_PROPERTY);
1503             if ((currentTag != null) && currentTag.startsWith(SELECT_LOGD_TAG_SILENCE)) {
1504                 currentValue = SELECT_LOGD_OFF_SIZE_MARKER_VALUE;
1505             }
1506             if (mLogpersist != null) {
1507                 String currentLogpersistEnable
1508                         = SystemProperties.get(ACTUAL_LOGPERSIST_PROPERTY_ENABLE);
1509                 if ((currentLogpersistEnable == null)
1510                         || !currentLogpersistEnable.equals("true")
1511                         || currentValue.equals(SELECT_LOGD_OFF_SIZE_MARKER_VALUE)) {
1512                     writeLogpersistOption(null, true);
1513                     mLogpersist.setEnabled(false);
1514                 } else if (mLastEnabledState) {
1515                     mLogpersist.setEnabled(true);
1516                 }
1517             }
1518             if ((currentValue == null) || (currentValue.length() == 0)) {
1519                 currentValue = defaultLogdSizeValue();
1520             }
1521             String[] values = getResources().getStringArray(R.array.select_logd_size_values);
1522             String[] titles = getResources().getStringArray(R.array.select_logd_size_titles);
1523             int index = 2; // punt to second entry if not found
1524             if (SystemProperties.get("ro.config.low_ram").equals("true")) {
1525                 mLogdSize.setEntries(R.array.select_logd_size_lowram_titles);
1526                 titles = getResources().getStringArray(R.array.select_logd_size_lowram_titles);
1527                 index = 1;
1528             }
1529             String[] summaries = getResources().getStringArray(R.array.select_logd_size_summaries);
1530             for (int i = 0; i < titles.length; i++) {
1531                 if (currentValue.equals(values[i])
1532                         || currentValue.equals(titles[i])) {
1533                     index = i;
1534                     break;
1535                 }
1536             }
1537             mLogdSize.setValue(values[index]);
1538             mLogdSize.setSummary(summaries[index]);
1539             mLogdSize.setOnPreferenceChangeListener(this);
1540         }
1541     }
1542
1543     private void writeLogdSizeOption(Object newValue) {
1544         boolean disable = (newValue != null) &&
1545                 (newValue.toString().equals(SELECT_LOGD_OFF_SIZE_MARKER_VALUE));
1546         String currentTag = SystemProperties.get(SELECT_LOGD_TAG_PROPERTY);
1547         if (currentTag == null) {
1548             currentTag = "";
1549         }
1550         // filter clean and unstack all references to our setting
1551         String newTag = currentTag.replaceAll(
1552                 ",+" + SELECT_LOGD_TAG_SILENCE, "").replaceFirst(
1553                 "^" + SELECT_LOGD_TAG_SILENCE + ",*", "").replaceAll(
1554                 ",+", ",").replaceFirst(
1555                 ",+$", "");
1556         if (disable) {
1557             newValue = SELECT_LOGD_MINIMUM_SIZE_VALUE;
1558             // Make sure snet_event_log get through first, but do not override
1559             String snetValue = SystemProperties.get(SELECT_LOGD_SNET_TAG_PROPERTY);
1560             if ((snetValue == null) || (snetValue.length() == 0)) {
1561                 snetValue = SystemProperties.get(SELECT_LOGD_RUNTIME_SNET_TAG_PROPERTY);
1562                 if ((snetValue == null) || (snetValue.length() == 0)) {
1563                     SystemProperties.set(SELECT_LOGD_SNET_TAG_PROPERTY, "I");
1564                 }
1565             }
1566             // Silence all log sources, security logs notwithstanding
1567             if (newTag.length() != 0) {
1568                 newTag = "," + newTag;
1569             }
1570             // Stack settings, stack to help preserve original value
1571             newTag = SELECT_LOGD_TAG_SILENCE + newTag;
1572         }
1573         if (!newTag.equals(currentTag)) {
1574             SystemProperties.set(SELECT_LOGD_TAG_PROPERTY, newTag);
1575         }
1576         String defaultValue = defaultLogdSizeValue();
1577         final String size = ((newValue != null) && (newValue.toString().length() != 0)) ?
1578                 newValue.toString() : defaultValue;
1579         SystemProperties.set(SELECT_LOGD_SIZE_PROPERTY, defaultValue.equals(size) ? "" : size);
1580         SystemProperties.set("ctl.start", "logd-reinit");
1581         pokeSystemProperties();
1582         updateLogdSizeValues();
1583     }
1584
1585     private void updateLogpersistValues() {
1586         if (mLogpersist == null) {
1587             return;
1588         }
1589         String currentValue = SystemProperties.get(ACTUAL_LOGPERSIST_PROPERTY);
1590         if (currentValue == null) {
1591             currentValue = "";
1592         }
1593         String currentBuffers = SystemProperties.get(ACTUAL_LOGPERSIST_PROPERTY_BUFFER);
1594         if ((currentBuffers == null) || (currentBuffers.length() == 0)) {
1595             currentBuffers = "all";
1596         }
1597         int index = 0;
1598         if (currentValue.equals(SELECT_LOGPERSIST_PROPERTY_SERVICE)) {
1599             index = 1;
1600             if (currentBuffers.equals("kernel")) {
1601                 index = 3;
1602             } else if (!currentBuffers.equals("all") &&
1603                     !currentBuffers.contains("radio") &&
1604                     currentBuffers.contains("security") &&
1605                     currentBuffers.contains("kernel")) {
1606                 index = 2;
1607                 if (!currentBuffers.contains("default")) {
1608                     String[] contains = {"main", "events", "system", "crash"};
1609                     for (int i = 0; i < contains.length; i++) {
1610                         if (!currentBuffers.contains(contains[i])) {
1611                             index = 1;
1612                             break;
1613                         }
1614                     }
1615                 }
1616             }
1617         }
1618         mLogpersist.setValue(
1619                 getResources().getStringArray(R.array.select_logpersist_values)[index]);
1620         mLogpersist.setSummary(
1621                 getResources().getStringArray(R.array.select_logpersist_summaries)[index]);
1622         mLogpersist.setOnPreferenceChangeListener(this);
1623         if (index != 0) {
1624             mLogpersistCleared = false;
1625         } else if (!mLogpersistCleared) {
1626             // would File.delete() directly but need to switch uid/gid to access
1627             SystemProperties.set(ACTUAL_LOGPERSIST_PROPERTY, SELECT_LOGPERSIST_PROPERTY_CLEAR);
1628             pokeSystemProperties();
1629             mLogpersistCleared = true;
1630         }
1631     }
1632
1633     private void setLogpersistOff(boolean update) {
1634         SystemProperties.set(SELECT_LOGPERSIST_PROPERTY_BUFFER, "");
1635         // deal with trampoline of empty properties
1636         SystemProperties.set(ACTUAL_LOGPERSIST_PROPERTY_BUFFER, "");
1637         SystemProperties.set(SELECT_LOGPERSIST_PROPERTY, "");
1638         SystemProperties.set(ACTUAL_LOGPERSIST_PROPERTY,
1639                 update ? "" : SELECT_LOGPERSIST_PROPERTY_STOP);
1640         pokeSystemProperties();
1641         if (update) {
1642             updateLogpersistValues();
1643         } else {
1644             for (int i = 0; i < 3; i++) {
1645                 String currentValue = SystemProperties.get(ACTUAL_LOGPERSIST_PROPERTY);
1646                 if ((currentValue == null) || currentValue.equals("")) {
1647                     break;
1648                 }
1649                 try {
1650                     Thread.sleep(100);
1651                 } catch (InterruptedException e) {
1652                 }
1653             }
1654         }
1655     }
1656
1657     private void writeLogpersistOption(Object newValue, boolean skipWarning) {
1658         if (mLogpersist == null) {
1659             return;
1660         }
1661         String currentTag = SystemProperties.get(SELECT_LOGD_TAG_PROPERTY);
1662         if ((currentTag != null) && currentTag.startsWith(SELECT_LOGD_TAG_SILENCE)) {
1663             newValue = null;
1664             skipWarning = true;
1665         }
1666
1667         if ((newValue == null) || newValue.toString().equals("")) {
1668             if (skipWarning) {
1669                 mLogpersistCleared = false;
1670             } else if (!mLogpersistCleared) {
1671                 // if transitioning from on to off, pop up an are you sure?
1672                 String currentValue = SystemProperties.get(ACTUAL_LOGPERSIST_PROPERTY);
1673                 if ((currentValue != null) &&
1674                         currentValue.equals(SELECT_LOGPERSIST_PROPERTY_SERVICE)) {
1675                     if (mLogpersistClearDialog != null) dismissDialogs();
1676                     mLogpersistClearDialog = new AlertDialog.Builder(getActivity()).setMessage(
1677                             getActivity().getResources().getString(
1678                                     R.string.dev_logpersist_clear_warning_message))
1679                             .setTitle(R.string.dev_logpersist_clear_warning_title)
1680                             .setPositiveButton(android.R.string.yes, this)
1681                             .setNegativeButton(android.R.string.no, this)
1682                             .show();
1683                     mLogpersistClearDialog.setOnDismissListener(this);
1684                     return;
1685                 }
1686             }
1687             setLogpersistOff(true);
1688             return;
1689         }
1690
1691         String currentBuffer = SystemProperties.get(ACTUAL_LOGPERSIST_PROPERTY_BUFFER);
1692         if ((currentBuffer != null) && !currentBuffer.equals(newValue.toString())) {
1693             setLogpersistOff(false);
1694         }
1695         SystemProperties.set(SELECT_LOGPERSIST_PROPERTY_BUFFER, newValue.toString());
1696         SystemProperties.set(SELECT_LOGPERSIST_PROPERTY, SELECT_LOGPERSIST_PROPERTY_SERVICE);
1697         pokeSystemProperties();
1698         for (int i = 0; i < 3; i++) {
1699             String currentValue = SystemProperties.get(ACTUAL_LOGPERSIST_PROPERTY);
1700             if ((currentValue != null)
1701                     && currentValue.equals(SELECT_LOGPERSIST_PROPERTY_SERVICE)) {
1702                 break;
1703             }
1704             try {
1705                 Thread.sleep(100);
1706             } catch (InterruptedException e) {
1707             }
1708         }
1709         updateLogpersistValues();
1710     }
1711
1712     private void updateUsbConfigurationValues() {
1713         if (mUsbConfiguration != null) {
1714             UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
1715
1716             String[] values = getResources().getStringArray(R.array.usb_configuration_values);
1717             String[] titles = getResources().getStringArray(R.array.usb_configuration_titles);
1718             int index = 0;
1719             for (int i = 0; i < titles.length; i++) {
1720                 if (manager.isFunctionEnabled(values[i])) {
1721                     index = i;
1722                     break;
1723                 }
1724             }
1725             mUsbConfiguration.setValue(values[index]);
1726             mUsbConfiguration.setSummary(titles[index]);
1727             mUsbConfiguration.setOnPreferenceChangeListener(this);
1728         }
1729     }
1730
1731     private void writeUsbConfigurationOption(Object newValue) {
1732         UsbManager manager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
1733         String function = newValue.toString();
1734         if (function.equals("none")) {
1735             manager.setCurrentFunction(function, false);
1736         } else {
1737             manager.setCurrentFunction(function, true);
1738         }
1739     }
1740
1741     private void writeImmediatelyDestroyActivitiesOptions() {
1742         try {
1743             ActivityManager.getService().setAlwaysFinish(
1744                     mImmediatelyDestroyActivities.isChecked());
1745         } catch (RemoteException ex) {
1746         }
1747     }
1748
1749     private void updateImmediatelyDestroyActivitiesOptions() {
1750         updateSwitchPreference(mImmediatelyDestroyActivities, Settings.Global.getInt(
1751                 getActivity().getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0)
1752                 != 0);
1753     }
1754
1755     private void updateAnimationScaleValue(int which, ListPreference pref) {
1756         try {
1757             float scale = mWindowManager.getAnimationScale(which);
1758             if (scale != 1) {
1759                 mHaveDebugSettings = true;
1760             }
1761             CharSequence[] values = pref.getEntryValues();
1762             for (int i = 0; i < values.length; i++) {
1763                 float val = Float.parseFloat(values[i].toString());
1764                 if (scale <= val) {
1765                     pref.setValueIndex(i);
1766                     pref.setSummary(pref.getEntries()[i]);
1767                     return;
1768                 }
1769             }
1770             pref.setValueIndex(values.length - 1);
1771             pref.setSummary(pref.getEntries()[0]);
1772         } catch (RemoteException e) {
1773         }
1774     }
1775
1776     private void updateAnimationScaleOptions() {
1777         updateAnimationScaleValue(0, mWindowAnimationScale);
1778         updateAnimationScaleValue(1, mTransitionAnimationScale);
1779         updateAnimationScaleValue(2, mAnimatorDurationScale);
1780     }
1781
1782     private void writeAnimationScaleOption(int which, ListPreference pref, Object newValue) {
1783         try {
1784             float scale = newValue != null ? Float.parseFloat(newValue.toString()) : 1;
1785             mWindowManager.setAnimationScale(which, scale);
1786             updateAnimationScaleValue(which, pref);
1787         } catch (RemoteException e) {
1788         }
1789     }
1790
1791     private void updateOverlayDisplayDevicesOptions() {
1792         String value = Settings.Global.getString(getActivity().getContentResolver(),
1793                 Settings.Global.OVERLAY_DISPLAY_DEVICES);
1794         if (value == null) {
1795             value = "";
1796         }
1797
1798         CharSequence[] values = mOverlayDisplayDevices.getEntryValues();
1799         for (int i = 0; i < values.length; i++) {
1800             if (value.contentEquals(values[i])) {
1801                 mOverlayDisplayDevices.setValueIndex(i);
1802                 mOverlayDisplayDevices.setSummary(mOverlayDisplayDevices.getEntries()[i]);
1803                 return;
1804             }
1805         }
1806         mOverlayDisplayDevices.setValueIndex(0);
1807         mOverlayDisplayDevices.setSummary(mOverlayDisplayDevices.getEntries()[0]);
1808     }
1809
1810     private void writeOverlayDisplayDevicesOptions(Object newValue) {
1811         Settings.Global.putString(getActivity().getContentResolver(),
1812                 Settings.Global.OVERLAY_DISPLAY_DEVICES, (String) newValue);
1813         updateOverlayDisplayDevicesOptions();
1814     }
1815
1816     private void updateAppProcessLimitOptions() {
1817         try {
1818             int limit = ActivityManager.getService().getProcessLimit();
1819             CharSequence[] values = mAppProcessLimit.getEntryValues();
1820             for (int i = 0; i < values.length; i++) {
1821                 int val = Integer.parseInt(values[i].toString());
1822                 if (val >= limit) {
1823                     if (i != 0) {
1824                         mHaveDebugSettings = true;
1825                     }
1826                     mAppProcessLimit.setValueIndex(i);
1827                     mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[i]);
1828                     return;
1829                 }
1830             }
1831             mAppProcessLimit.setValueIndex(0);
1832             mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[0]);
1833         } catch (RemoteException e) {
1834         }
1835     }
1836
1837     private void writeAppProcessLimitOptions(Object newValue) {
1838         try {
1839             int limit = newValue != null ? Integer.parseInt(newValue.toString()) : -1;
1840             ActivityManager.getService().setProcessLimit(limit);
1841             updateAppProcessLimitOptions();
1842         } catch (RemoteException e) {
1843         }
1844     }
1845
1846     private void writeShowAllANRsOptions() {
1847         Settings.Secure.putInt(getActivity().getContentResolver(),
1848                 Settings.Secure.ANR_SHOW_BACKGROUND,
1849                 mShowAllANRs.isChecked() ? 1 : 0);
1850     }
1851
1852     private void updateShowAllANRsOptions() {
1853         updateSwitchPreference(mShowAllANRs, Settings.Secure.getInt(
1854                 getActivity().getContentResolver(), Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0);
1855     }
1856
1857     private void confirmEnableOemUnlock() {
1858         DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
1859             @Override
1860             public void onClick(DialogInterface dialog, int which) {
1861                 if (which == DialogInterface.BUTTON_POSITIVE) {
1862                     Utils.setOemUnlockEnabled(getActivity(), true);
1863                 }
1864             }
1865         };
1866
1867         DialogInterface.OnDismissListener onDismissListener =
1868                 new DialogInterface.OnDismissListener() {
1869                     @Override
1870                     public void onDismiss(DialogInterface dialog) {
1871                         if (getActivity() == null) {
1872                             return;
1873                         }
1874                         updateAllOptions();
1875                     }
1876                 };
1877
1878         new AlertDialog.Builder(getActivity())
1879                 .setTitle(R.string.confirm_enable_oem_unlock_title)
1880                 .setMessage(R.string.confirm_enable_oem_unlock_text)
1881                 .setPositiveButton(R.string.enable_text, onClickListener)
1882                 .setNegativeButton(android.R.string.cancel, null)
1883                 .setOnDismissListener(onDismissListener)
1884                 .create()
1885                 .show();
1886     }
1887
1888     @Override
1889     public void onSwitchChanged(Switch switchView, boolean isChecked) {
1890         if (switchView != mSwitchBar.getSwitch()) {
1891             return;
1892         }
1893         if (isChecked != mLastEnabledState) {
1894             if (isChecked) {
1895                 mDialogClicked = false;
1896                 if (mEnableDialog != null) dismissDialogs();
1897                 mEnableDialog = new AlertDialog.Builder(getActivity()).setMessage(
1898                         getActivity().getResources().getString(
1899                                 R.string.dev_settings_warning_message))
1900                         .setTitle(R.string.dev_settings_warning_title)
1901                         .setPositiveButton(android.R.string.yes, this)
1902                         .setNegativeButton(android.R.string.no, this)
1903                         .show();
1904                 mEnableDialog.setOnDismissListener(this);
1905             } else {
1906                 resetDangerousOptions();
1907                 Settings.Global.putInt(getActivity().getContentResolver(),
1908                         Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0);
1909                 mLastEnabledState = isChecked;
1910                 setPrefsEnabledState(mLastEnabledState);
1911             }
1912         }
1913     }
1914
1915     @Override
1916     public void onActivityResult(int requestCode, int resultCode, Intent data) {
1917         if (requestCode == RESULT_DEBUG_APP) {
1918             if (resultCode == Activity.RESULT_OK) {
1919                 mDebugApp = data.getAction();
1920                 writeDebuggerOptions();
1921                 updateDebuggerOptions();
1922             }
1923         } else if (requestCode == RESULT_MOCK_LOCATION_APP) {
1924             if (resultCode == Activity.RESULT_OK) {
1925                 mMockLocationApp = data.getAction();
1926                 writeMockLocation();
1927                 updateMockLocation();
1928             }
1929         } else if (requestCode == REQUEST_CODE_ENABLE_OEM_UNLOCK) {
1930             if (resultCode == Activity.RESULT_OK) {
1931                 if (mEnableOemUnlock.isChecked()) {
1932                     confirmEnableOemUnlock();
1933                 } else {
1934                     Utils.setOemUnlockEnabled(getActivity(), false);
1935                 }
1936             }
1937         } else {
1938             super.onActivityResult(requestCode, resultCode, data);
1939         }
1940     }
1941
1942     @Override
1943     public boolean onPreferenceTreeClick(Preference preference) {
1944         if (Utils.isMonkeyRunning()) {
1945             return false;
1946         }
1947
1948         if (mBugReportInPowerController.handlePreferenceTreeClick(preference)) {
1949             return true;
1950         }
1951
1952         if (preference == mEnableAdb) {
1953             if (mEnableAdb.isChecked()) {
1954                 mDialogClicked = false;
1955                 if (mAdbDialog != null) dismissDialogs();
1956                 mAdbDialog = new AlertDialog.Builder(getActivity()).setMessage(
1957                         getActivity().getResources().getString(R.string.adb_warning_message))
1958                         .setTitle(R.string.adb_warning_title)
1959                         .setPositiveButton(android.R.string.yes, this)
1960                         .setNegativeButton(android.R.string.no, this)
1961                         .show();
1962                 mAdbDialog.setOnDismissListener(this);
1963             } else {
1964                 Settings.Global.putInt(getActivity().getContentResolver(),
1965                         Settings.Global.ADB_ENABLED, 0);
1966                 mVerifyAppsOverUsb.setEnabled(false);
1967                 mVerifyAppsOverUsb.setChecked(false);
1968                 updateBugreportOptions();
1969             }
1970         } else if (preference == mClearAdbKeys) {
1971             if (mAdbKeysDialog != null) dismissDialogs();
1972             mAdbKeysDialog = new AlertDialog.Builder(getActivity())
1973                     .setMessage(R.string.adb_keys_warning_message)
1974                     .setPositiveButton(android.R.string.ok, this)
1975                     .setNegativeButton(android.R.string.cancel, null)
1976                     .show();
1977         } else if (preference == mEnableTerminal) {
1978             final PackageManager pm = getActivity().getPackageManager();
1979             pm.setApplicationEnabledSetting(TERMINAL_APP_PACKAGE,
1980                     mEnableTerminal.isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
1981                             : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
1982         } else if (preference == mKeepScreenOn) {
1983             Settings.Global.putInt(getActivity().getContentResolver(),
1984                     Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
1985                     mKeepScreenOn.isChecked() ?
1986                             (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB
1987                                     | BatteryManager.BATTERY_PLUGGED_WIRELESS) : 0);
1988         } else if (preference == mBtHciSnoopLog) {
1989             writeBtHciSnoopLogOptions();
1990         } else if (preference == mEnableOemUnlock && mEnableOemUnlock.isEnabled()) {
1991             if (mEnableOemUnlock.isChecked()) {
1992                 if (!showKeyguardConfirmation(getResources(), REQUEST_CODE_ENABLE_OEM_UNLOCK)) {
1993                     confirmEnableOemUnlock();
1994                 }
1995             } else {
1996                 Utils.setOemUnlockEnabled(getActivity(), false);
1997             }
1998         } else if (preference == mMockLocationAppPref) {
1999             Intent intent = new Intent(getActivity(), AppPicker.class);
2000             intent.putExtra(AppPicker.EXTRA_REQUESTIING_PERMISSION,
2001                     Manifest.permission.ACCESS_MOCK_LOCATION);
2002             startActivityForResult(intent, RESULT_MOCK_LOCATION_APP);
2003         } else if (preference == mDebugViewAttributes) {
2004             Settings.Global.putInt(getActivity().getContentResolver(),
2005                     Settings.Global.DEBUG_VIEW_ATTRIBUTES,
2006                     mDebugViewAttributes.isChecked() ? 1 : 0);
2007         } else if (preference == mForceAllowOnExternal) {
2008             Settings.Global.putInt(getActivity().getContentResolver(),
2009                     Settings.Global.FORCE_ALLOW_ON_EXTERNAL,
2010                     mForceAllowOnExternal.isChecked() ? 1 : 0);
2011         } else if (preference == mDebugAppPref) {
2012             Intent intent = new Intent(getActivity(), AppPicker.class);
2013             intent.putExtra(AppPicker.EXTRA_DEBUGGABLE, true);
2014             startActivityForResult(intent, RESULT_DEBUG_APP);
2015         } else if (preference == mWaitForDebugger) {
2016             writeDebuggerOptions();
2017         } else if (preference == mVerifyAppsOverUsb) {
2018             writeVerifyAppsOverUsbOptions();
2019         } else if (preference == mOtaDisableAutomaticUpdate) {
2020             writeOtaDisableAutomaticUpdateOptions();
2021         } else if (preference == mStrictMode) {
2022             writeStrictModeVisualOptions();
2023         } else if (preference == mPointerLocation) {
2024             writePointerLocationOptions();
2025         } else if (preference == mShowTouches) {
2026             writeShowTouchesOptions();
2027         } else if (preference == mShowScreenUpdates) {
2028             writeShowUpdatesOption();
2029         } else if (preference == mDisableOverlays) {
2030             writeDisableOverlaysOption();
2031         } else if (preference == mImmediatelyDestroyActivities) {
2032             writeImmediatelyDestroyActivitiesOptions();
2033         } else if (preference == mShowAllANRs) {
2034             writeShowAllANRsOptions();
2035         } else if (preference == mForceHardwareUi) {
2036             writeHardwareUiOptions();
2037         } else if (preference == mForceMsaa) {
2038             writeMsaaOptions();
2039         } else if (preference == mShowHwScreenUpdates) {
2040             writeShowHwScreenUpdatesOptions();
2041         } else if (preference == mShowHwLayersUpdates) {
2042             writeShowHwLayersUpdatesOptions();
2043         } else if (preference == mDebugLayout) {
2044             writeDebugLayoutOptions();
2045         } else if (preference == mForceRtlLayout) {
2046             writeForceRtlOptions();
2047         } else if (preference == mWifiDisplayCertification) {
2048             writeWifiDisplayCertificationOptions();
2049         } else if (preference == mWifiVerboseLogging) {
2050             writeWifiVerboseLoggingOptions();
2051         } else if (preference == mWifiAggressiveHandover) {
2052             writeWifiAggressiveHandoverOptions();
2053         } else if (preference == mWifiAllowScansWithTraffic) {
2054             writeWifiAllowScansWithTrafficOptions();
2055         } else if (preference == mMobileDataAlwaysOn) {
2056             writeMobileDataAlwaysOnOptions();
2057         } else if (preference == mColorTemperaturePreference) {
2058             writeColorTemperature();
2059         } else if (preference == mUSBAudio) {
2060             writeUSBAudioOptions();
2061         } else if (preference == mForceResizable) {
2062             writeForceResizableOptions();
2063         } else if (INACTIVE_APPS_KEY.equals(preference.getKey())) {
2064             startInactiveAppsFragment();
2065         } else if (BACKGROUND_CHECK_KEY.equals(preference.getKey())) {
2066             startBackgroundCheckFragment();
2067         } else if (preference == mBluetoothDisableAbsVolume) {
2068             writeBluetoothDisableAbsVolumeOptions();
2069         } else if (preference == mWebViewMultiprocess) {
2070             writeWebViewMultiprocessOptions();
2071         } else if (SHORTCUT_MANAGER_RESET_KEY.equals(preference.getKey())) {
2072             resetShortcutManagerThrottling();
2073         } else {
2074             return super.onPreferenceTreeClick(preference);
2075         }
2076
2077         return false;
2078     }
2079
2080     private void startInactiveAppsFragment() {
2081         ((SettingsActivity) getActivity()).startPreferencePanel(
2082                 InactiveApps.class.getName(),
2083                 null, R.string.inactive_apps_title, null, null, 0);
2084     }
2085
2086     private void startBackgroundCheckFragment() {
2087         ((SettingsActivity) getActivity()).startPreferencePanel(
2088                 BackgroundCheckSummary.class.getName(),
2089                 null, R.string.background_check_title, null, null, 0);
2090     }
2091
2092     private boolean showKeyguardConfirmation(Resources resources, int requestCode) {
2093         return new ChooseLockSettingsHelper(getActivity(), this).launchConfirmationActivity(
2094                 requestCode, resources.getString(R.string.oem_unlock_enable));
2095     }
2096
2097     @Override
2098     public boolean onPreferenceChange(Preference preference, Object newValue) {
2099         if (HDCP_CHECKING_KEY.equals(preference.getKey())) {
2100             SystemProperties.set(HDCP_CHECKING_PROPERTY, newValue.toString());
2101             updateHdcpValues();
2102             pokeSystemProperties();
2103             return true;
2104         } else if (preference == mWebViewProvider) {
2105             if (newValue == null) {
2106                 Log.e(TAG, "Tried to set a null WebView provider");
2107                 return false;
2108             }
2109             if (writeWebViewProviderOptions(newValue)) {
2110                 return true;
2111             } else {
2112                 // The user chose a package that became invalid since the list was last updated,
2113                 // show a Toast to explain the situation.
2114                 Toast toast = Toast.makeText(getActivity(),
2115                         R.string.select_webview_provider_toast_text, Toast.LENGTH_SHORT);
2116                 toast.show();
2117             }
2118             return false;
2119         } else if (preference == mLogdSize) {
2120             writeLogdSizeOption(newValue);
2121             return true;
2122         } else if (preference == mLogpersist) {
2123             writeLogpersistOption(newValue, false);
2124             return true;
2125         } else if (preference == mUsbConfiguration) {
2126             writeUsbConfigurationOption(newValue);
2127             return true;
2128         } else if (preference == mWindowAnimationScale) {
2129             writeAnimationScaleOption(0, mWindowAnimationScale, newValue);
2130             return true;
2131         } else if (preference == mTransitionAnimationScale) {
2132             writeAnimationScaleOption(1, mTransitionAnimationScale, newValue);
2133             return true;
2134         } else if (preference == mAnimatorDurationScale) {
2135             writeAnimationScaleOption(2, mAnimatorDurationScale, newValue);
2136             return true;
2137         } else if (preference == mOverlayDisplayDevices) {
2138             writeOverlayDisplayDevicesOptions(newValue);
2139             return true;
2140         } else if (preference == mTrackFrameTime) {
2141             writeTrackFrameTimeOptions(newValue);
2142             return true;
2143         } else if (preference == mDebugHwOverdraw) {
2144             writeDebugHwOverdrawOptions(newValue);
2145             return true;
2146         } else if (preference == mShowNonRectClip) {
2147             writeShowNonRectClipOptions(newValue);
2148             return true;
2149         } else if (preference == mAppProcessLimit) {
2150             writeAppProcessLimitOptions(newValue);
2151             return true;
2152         } else if (preference == mSimulateColorSpace) {
2153             writeSimulateColorSpace(newValue);
2154             return true;
2155         }
2156         return false;
2157     }
2158
2159     private void dismissDialogs() {
2160         if (mAdbDialog != null) {
2161             mAdbDialog.dismiss();
2162             mAdbDialog = null;
2163         }
2164         if (mAdbKeysDialog != null) {
2165             mAdbKeysDialog.dismiss();
2166             mAdbKeysDialog = null;
2167         }
2168         if (mEnableDialog != null) {
2169             mEnableDialog.dismiss();
2170             mEnableDialog = null;
2171         }
2172         if (mLogpersistClearDialog != null) {
2173             mLogpersistClearDialog.dismiss();
2174             mLogpersistClearDialog = null;
2175         }
2176     }
2177
2178     public void onClick(DialogInterface dialog, int which) {
2179         if (dialog == mAdbDialog) {
2180             if (which == DialogInterface.BUTTON_POSITIVE) {
2181                 mDialogClicked = true;
2182                 Settings.Global.putInt(getActivity().getContentResolver(),
2183                         Settings.Global.ADB_ENABLED, 1);
2184                 mVerifyAppsOverUsb.setEnabled(true);
2185                 updateVerifyAppsOverUsbOptions();
2186                 updateBugreportOptions();
2187             } else {
2188                 // Reset the toggle
2189                 mEnableAdb.setChecked(false);
2190             }
2191         } else if (dialog == mAdbKeysDialog) {
2192             if (which == DialogInterface.BUTTON_POSITIVE) {
2193                 try {
2194                     IBinder b = ServiceManager.getService(Context.USB_SERVICE);
2195                     IUsbManager service = IUsbManager.Stub.asInterface(b);
2196                     service.clearUsbDebuggingKeys();
2197                 } catch (RemoteException e) {
2198                     Log.e(TAG, "Unable to clear adb keys", e);
2199                 }
2200             }
2201         } else if (dialog == mEnableDialog) {
2202             if (which == DialogInterface.BUTTON_POSITIVE) {
2203                 mDialogClicked = true;
2204                 Settings.Global.putInt(getActivity().getContentResolver(),
2205                         Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
2206                 mLastEnabledState = true;
2207                 setPrefsEnabledState(mLastEnabledState);
2208             } else {
2209                 // Reset the toggle
2210                 mSwitchBar.setChecked(false);
2211             }
2212         } else if (dialog == mLogpersistClearDialog) {
2213             if (which == DialogInterface.BUTTON_POSITIVE) {
2214                 setLogpersistOff(true);
2215             } else {
2216                 updateLogpersistValues();
2217             }
2218         }
2219     }
2220
2221     public void onDismiss(DialogInterface dialog) {
2222         // Assuming that onClick gets called first
2223         if (dialog == mAdbDialog) {
2224             if (!mDialogClicked) {
2225                 mEnableAdb.setChecked(false);
2226             }
2227             mAdbDialog = null;
2228         } else if (dialog == mEnableDialog) {
2229             if (!mDialogClicked) {
2230                 mSwitchBar.setChecked(false);
2231             }
2232             mEnableDialog = null;
2233         } else if (dialog == mLogpersistClearDialog) {
2234             mLogpersistClearDialog = null;
2235         }
2236     }
2237
2238     @Override
2239     public void onDestroy() {
2240         dismissDialogs();
2241         super.onDestroy();
2242     }
2243
2244     void pokeSystemProperties() {
2245         if (!mDontPokeProperties) {
2246             //noinspection unchecked
2247             (new SystemPropPoker()).execute();
2248         }
2249     }
2250
2251     private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
2252         @Override
2253         public void onReceive(Context context, Intent intent) {
2254             updateUsbConfigurationValues();
2255         }
2256     };
2257
2258     public static class SystemPropPoker extends AsyncTask<Void, Void, Void> {
2259         @Override
2260         protected Void doInBackground(Void... params) {
2261             String[] services = ServiceManager.listServices();
2262             for (String service : services) {
2263                 IBinder obj = ServiceManager.checkService(service);
2264                 if (obj != null) {
2265                     Parcel data = Parcel.obtain();
2266                     try {
2267                         obj.transact(IBinder.SYSPROPS_TRANSACTION, data, null, 0);
2268                     } catch (RemoteException e) {
2269                     } catch (Exception e) {
2270                         Log.i(TAG, "Someone wrote a bad service '" + service
2271                                 + "' that doesn't like to be poked: " + e);
2272                     }
2273                     data.recycle();
2274                 }
2275             }
2276             return null;
2277         }
2278     }
2279
2280     private static boolean isPackageInstalled(Context context, String packageName) {
2281         try {
2282             return context.getPackageManager().getPackageInfo(packageName, 0) != null;
2283         } catch (NameNotFoundException e) {
2284             return false;
2285         }
2286     }
2287
2288
2289     /**
2290      * For Search.
2291      */
2292     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
2293             new BaseSearchIndexProvider() {
2294
2295                 private boolean isShowingDeveloperOptions(Context context) {
2296                     return context.getSharedPreferences(DevelopmentSettings.PREF_FILE,
2297                             Context.MODE_PRIVATE).getBoolean(
2298                             DevelopmentSettings.PREF_SHOW,
2299                             android.os.Build.TYPE.equals("eng"));
2300                 }
2301
2302                 @Override
2303                 public List<SearchIndexableResource> getXmlResourcesToIndex(
2304                         Context context, boolean enabled) {
2305
2306                     if (!isShowingDeveloperOptions(context)) {
2307                         return null;
2308                     }
2309
2310                     final SearchIndexableResource sir = new SearchIndexableResource(context);
2311                     sir.xmlResId = R.xml.development_prefs;
2312                     return Arrays.asList(sir);
2313                 }
2314
2315                 @Override
2316                 public List<String> getNonIndexableKeys(Context context) {
2317                     if (!isShowingDeveloperOptions(context)) {
2318                         return null;
2319                     }
2320
2321                     final List<String> keys = new ArrayList<String>();
2322                     if (!showEnableOemUnlockPreference()) {
2323                         keys.add(ENABLE_OEM_UNLOCK);
2324                     }
2325                     return keys;
2326                 }
2327             };
2328
2329     private void resetShortcutManagerThrottling() {
2330         final IShortcutService service = IShortcutService.Stub.asInterface(
2331                 ServiceManager.getService(Context.SHORTCUT_SERVICE));
2332         if (service != null) {
2333             try {
2334                 service.resetThrottling();
2335                 Toast.makeText(getActivity(), R.string.reset_shortcut_manager_throttling_complete,
2336                         Toast.LENGTH_SHORT).show();
2337             } catch (RemoteException e) {
2338                 Log.e(TAG, "Failed to reset rate limiting", e);
2339             }
2340         }
2341     }
2342
2343     private void updateOemUnlockSettingDescription() {
2344         if (mEnableOemUnlock != null) {
2345             int oemUnlockSummary = R.string.oem_unlock_enable_summary;
2346             if (isBootloaderUnlocked()) {
2347                 oemUnlockSummary = R.string.oem_unlock_enable_disabled_summary_bootloader_unlocked;
2348             } else if (isSimLockedDevice()) {
2349                 oemUnlockSummary = R.string.oem_unlock_enable_disabled_summary_sim_locked_device;
2350             } else if (!isOemUnlockAllowed()) {
2351                 // If the device isn't SIM-locked but OEM unlock is disabled by the system via the
2352                 // user restriction, this means either some other carrier restriction is in place or
2353                 // the device hasn't been able to confirm which restrictions (SIM-lock or otherwise)
2354                 // apply.
2355                 oemUnlockSummary =
2356                         R.string.oem_unlock_enable_disabled_summary_connectivity_or_locked;
2357             }
2358             mEnableOemUnlock.setSummary(getString(oemUnlockSummary));
2359         }
2360     }
2361
2362     /** Returns {@code true} if the device is SIM-locked. Otherwise, returns {@code false}. */
2363     private boolean isSimLockedDevice() {
2364         int phoneCount = mTelephonyManager.getPhoneCount();
2365         for (int i = 0; i < phoneCount; i++) {
2366             if (mTelephonyManager.getAllowedCarriers(i).size() > 0) {
2367                 return true;
2368             }
2369         }
2370         return false;
2371     }
2372
2373     /**
2374      * Returns {@code true} if the bootloader has been unlocked. Otherwise, returns {code false}.
2375      */
2376     private boolean isBootloaderUnlocked() {
2377         int flashLockState = PersistentDataBlockManager.FLASH_LOCK_UNKNOWN;
2378         if (mOemUnlockManager != null) {
2379             flashLockState = mOemUnlockManager.getFlashLockState();
2380         }
2381
2382         return flashLockState == PersistentDataBlockManager.FLASH_LOCK_UNLOCKED;
2383     }
2384
2385     /**
2386      * Returns {@code true} if OEM unlock is disallowed by user restriction
2387      * {@link UserManager#DISALLOW_FACTORY_RESET} or {@link UserManager#DISALLOW_OEM_UNLOCK}.
2388      * Otherwise, returns {@code false}.
2389      */
2390     private boolean isOemUnlockAllowed() {
2391         UserHandle userHandle = UserHandle.of(UserHandle.myUserId());
2392         return !(mUm.hasBaseUserRestriction(UserManager.DISALLOW_OEM_UNLOCK, userHandle)
2393                 || mUm.hasBaseUserRestriction(UserManager.DISALLOW_FACTORY_RESET, userHandle));
2394     }
2395 }