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