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