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