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