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