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