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