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