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