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