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