• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.tv.settings.system.development;
18 
19 import android.Manifest;
20 import android.app.Activity;
21 import android.app.ActivityManager;
22 import android.app.AppOpsManager;
23 import android.app.admin.DevicePolicyManager;
24 import android.app.backup.IBackupManager;
25 import android.bluetooth.BluetoothAdapter;
26 import android.content.BroadcastReceiver;
27 import android.content.ComponentName;
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.IntentFilter;
32 import android.content.pm.ApplicationInfo;
33 import android.content.pm.PackageManager;
34 import android.content.pm.ResolveInfo;
35 import android.hardware.usb.UsbManager;
36 import android.net.wifi.WifiManager;
37 import android.os.BatteryManager;
38 import android.os.Build;
39 import android.os.Bundle;
40 import android.os.IBinder;
41 import android.os.Parcel;
42 import android.os.RemoteException;
43 import android.os.ServiceManager;
44 import android.os.StrictMode;
45 import android.os.SystemProperties;
46 import android.os.UserManager;
47 import android.provider.Settings;
48 import android.service.persistentdata.PersistentDataBlockManager;
49 import android.sysprop.AdbProperties;
50 import android.sysprop.DisplayProperties;
51 import android.text.TextUtils;
52 import android.util.Log;
53 import android.view.IWindowManager;
54 import android.view.LayoutInflater;
55 import android.view.ThreadedRenderer;
56 import android.view.View;
57 import android.view.ViewGroup;
58 import android.view.accessibility.AccessibilityManager;
59 import android.widget.Toast;
60 
61 import androidx.preference.ListPreference;
62 import androidx.preference.Preference;
63 import androidx.preference.PreferenceGroup;
64 import androidx.preference.PreferenceScreen;
65 import androidx.preference.SwitchPreference;
66 
67 import com.android.internal.app.LocalePicker;
68 import com.android.internal.logging.nano.MetricsProto;
69 import com.android.settingslib.core.ConfirmationDialogController;
70 import com.android.settingslib.development.DevelopmentSettingsEnabler;
71 import com.android.settingslib.development.SystemPropPoker;
72 import com.android.tv.settings.R;
73 import com.android.tv.settings.SettingsPreferenceFragment;
74 
75 import java.util.ArrayList;
76 import java.util.HashSet;
77 import java.util.List;
78 
79 /**
80  * Displays preferences for application developers.
81  */
82 public class DevelopmentFragment extends SettingsPreferenceFragment
83         implements Preference.OnPreferenceChangeListener,
84         EnableDevelopmentDialog.Callback, OemUnlockDialog.Callback, AdbDialog.Callback {
85     private static final String TAG = "DevelopmentSettings";
86 
87     private static final String ENABLE_DEVELOPER = "development_settings_enable";
88     private static final String ENABLE_ADB = "enable_adb";
89     private static final String CLEAR_ADB_KEYS = "clear_adb_keys";
90     private static final String ENABLE_TERMINAL = "enable_terminal";
91     private static final String KEEP_SCREEN_ON = "keep_screen_on";
92     private static final String BT_HCI_SNOOP_LOG = "bt_hci_snoop_log";
93     private static final String BTSNOOP_ENABLE_PROPERTY = "persist.bluetooth.btsnoopenable";
94     private static final String ENABLE_OEM_UNLOCK = "oem_unlock_enable";
95     private static final String HDCP_CHECKING_KEY = "hdcp_checking";
96     private static final String HDCP_CHECKING_PROPERTY = "persist.sys.hdcp_checking";
97     private static final String LOCAL_BACKUP_PASSWORD = "local_backup_password";
98     private static final String BUGREPORT = "bugreport";
99     private static final String BUGREPORT_IN_POWER_KEY = "bugreport_in_power";
100     private static final String RUNNING_APPS = "running_apps";
101 
102     private static final String DEBUG_APP_KEY = "debug_app";
103     private static final String WAIT_FOR_DEBUGGER_KEY = "wait_for_debugger";
104     private static final String MOCK_LOCATION_APP_KEY = "mock_location_app";
105     private static final String VERIFY_APPS_OVER_USB_KEY = "verify_apps_over_usb";
106     private static final String DEBUG_VIEW_ATTRIBUTES =  "debug_view_attributes";
107     private static final String FORCE_ALLOW_ON_EXTERNAL_KEY = "force_allow_on_external";
108     private static final String STRICT_MODE_KEY = "strict_mode";
109     private static final String POINTER_LOCATION_KEY = "pointer_location";
110     private static final String SHOW_TOUCHES_KEY = "show_touches";
111     private static final String SHOW_SCREEN_UPDATES_KEY = "show_screen_updates";
112     private static final String DISABLE_OVERLAYS_KEY = "disable_overlays";
113     private static final String SIMULATE_COLOR_SPACE = "simulate_color_space";
114     private static final String USB_AUDIO_KEY = "usb_audio";
115     private static final String FORCE_MSAA_KEY = "force_msaa";
116     private static final String TRACK_FRAME_TIME_KEY = "track_frame_time";
117     private static final String SHOW_NON_RECTANGULAR_CLIP_KEY = "show_non_rect_clip";
118     private static final String SHOW_HW_SCREEN_UPDATES_KEY = "show_hw_screen_udpates";
119     private static final String SHOW_HW_LAYERS_UPDATES_KEY = "show_hw_layers_udpates";
120     private static final String DEBUG_HW_OVERDRAW_KEY = "debug_hw_overdraw";
121     private static final String DEBUG_LAYOUT_KEY = "debug_layout";
122     private static final String FORCE_RTL_LAYOUT_KEY = "force_rtl_layout_all_locales";
123     private static final String WINDOW_ANIMATION_SCALE_KEY = "window_animation_scale";
124     private static final String TRANSITION_ANIMATION_SCALE_KEY = "transition_animation_scale";
125     private static final String ANIMATOR_DURATION_SCALE_KEY = "animator_duration_scale";
126     private static final String OVERLAY_DISPLAY_DEVICES_KEY = "overlay_display_devices";
127     private static final String DEBUG_DEBUGGING_CATEGORY_KEY = "debug_debugging_category";
128 
129     private static final String WIFI_DISPLAY_CERTIFICATION_KEY = "wifi_display_certification";
130     private static final String WIFI_VERBOSE_LOGGING_KEY = "wifi_verbose_logging";
131     private static final String USB_CONFIGURATION_KEY = "select_usb_configuration";
132     private static final String MOBILE_DATA_ALWAYS_ON = "mobile_data_always_on";
133     private static final String KEY_COLOR_MODE = "color_mode";
134     private static final String FORCE_RESIZABLE_KEY = "force_resizable_activities";
135 
136     private static final String INACTIVE_APPS_KEY = "inactive_apps";
137 
138     private static final String OPENGL_TRACES_KEY = "enable_opengl_traces";
139 
140     private static final String IMMEDIATELY_DESTROY_ACTIVITIES_KEY
141             = "immediately_destroy_activities";
142     private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit";
143 
144     private static final String SHOW_ALL_ANRS_KEY = "show_all_anrs";
145 
146     private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
147 
148     private static final String TERMINAL_APP_PACKAGE = "com.android.terminal";
149 
150     private static final String KEY_CONVERT_FBE = "convert_to_file_encryption";
151 
152     private static final int RESULT_DEBUG_APP = 1000;
153     private static final int RESULT_MOCK_LOCATION_APP = 1001;
154 
155     private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst";
156 
157     private static String DEFAULT_LOG_RING_BUFFER_SIZE_IN_BYTES = "262144"; // 256K
158 
159     private static final int[] MOCK_LOCATION_APP_OPS = new int[] {AppOpsManager.OP_MOCK_LOCATION};
160 
161     private static final String STATE_SHOWING_DIALOG_KEY = "showing_dialog_key";
162 
163     private String mPendingDialogKey;
164 
165     private IWindowManager mWindowManager;
166     private IBackupManager mBackupManager;
167     private DevicePolicyManager mDpm;
168     private UserManager mUm;
169     private WifiManager mWifiManager;
170     private ContentResolver mContentResolver;
171 
172     private boolean mLastEnabledState;
173     private boolean mHaveDebugSettings;
174 
175     private SwitchPreference mEnableDeveloper;
176     private SwitchPreference mEnableAdb;
177     private Preference mClearAdbKeys;
178     private SwitchPreference mEnableTerminal;
179     private Preference mBugreport;
180     private SwitchPreference mKeepScreenOn;
181     private SwitchPreference mBtHciSnoopLog;
182     private SwitchPreference mEnableOemUnlock;
183     private SwitchPreference mDebugViewAttributes;
184     private SwitchPreference mForceAllowOnExternal;
185 
186     private PreferenceScreen mPassword;
187     private String mDebugApp;
188     private Preference mDebugAppPref;
189 
190     private String mMockLocationApp;
191     private Preference mMockLocationAppPref;
192 
193     private SwitchPreference mWaitForDebugger;
194     private SwitchPreference mVerifyAppsOverUsb;
195     private SwitchPreference mWifiDisplayCertification;
196     private SwitchPreference mWifiVerboseLogging;
197     private SwitchPreference mMobileDataAlwaysOn;
198 
199     private SwitchPreference mStrictMode;
200     private SwitchPreference mPointerLocation;
201     private SwitchPreference mShowTouches;
202     private SwitchPreference mShowScreenUpdates;
203     private SwitchPreference mDisableOverlays;
204     private SwitchPreference mForceMsaa;
205     private SwitchPreference mShowHwScreenUpdates;
206     private SwitchPreference mShowHwLayersUpdates;
207     private SwitchPreference mDebugLayout;
208     private SwitchPreference mForceRtlLayout;
209     private ListPreference mDebugHwOverdraw;
210     private LogdSizePreferenceController mLogdSizeController;
211     private LogpersistPreferenceController mLogpersistController;
212     private ListPreference mUsbConfiguration;
213     private ListPreference mTrackFrameTime;
214     private ListPreference mShowNonRectClip;
215     private ListPreference mWindowAnimationScale;
216     private ListPreference mTransitionAnimationScale;
217     private ListPreference mAnimatorDurationScale;
218     private ListPreference mOverlayDisplayDevices;
219     private ListPreference mOpenGLTraces;
220 
221     private ListPreference mSimulateColorSpace;
222 
223     private SwitchPreference mUSBAudio;
224     private SwitchPreference mImmediatelyDestroyActivities;
225 
226     private ListPreference mAppProcessLimit;
227 
228     private SwitchPreference mShowAllANRs;
229 
230     private ColorModePreference mColorModePreference;
231 
232     private SwitchPreference mForceResizable;
233 
234     private final ArrayList<Preference> mAllPrefs = new ArrayList<>();
235 
236     private final ArrayList<SwitchPreference> mResetSwitchPrefs
237             = new ArrayList<>();
238 
239     private final HashSet<Preference> mDisabledPrefs = new HashSet<>();
240 
241     private boolean mUnavailable;
242 
newInstance()243     public static DevelopmentFragment newInstance() {
244         return new DevelopmentFragment();
245     }
246 
247     @Override
getMetricsCategory()248     public int getMetricsCategory() {
249         return MetricsProto.MetricsEvent.DEVELOPMENT;
250     }
251 
252     @Override
onCreate(Bundle icicle)253     public void onCreate(Bundle icicle) {
254 
255         if (icicle != null) {
256             // Don't show this in onCreate since we might be on the back stack
257             mPendingDialogKey = icicle.getString(STATE_SHOWING_DIALOG_KEY);
258         }
259 
260         mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
261         mBackupManager = IBackupManager.Stub.asInterface(
262                 ServiceManager.getService(Context.BACKUP_SERVICE));
263         mDpm = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
264         mUm = (UserManager) getActivity().getSystemService(Context.USER_SERVICE);
265 
266         mWifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
267 
268         mContentResolver = getActivity().getContentResolver();
269 
270         super.onCreate(icicle);
271     }
272 
273     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)274     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
275         mLogdSizeController = new LogdSizePreferenceController(getActivity());
276         mLogpersistController = new LogpersistPreferenceController(getActivity(), getLifecycle());
277 
278         if (!mUm.isAdminUser()
279                 || mUm.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)
280                 || Settings.Global.getInt(mContentResolver,
281                 Settings.Global.DEVICE_PROVISIONED, 0) == 0) {
282             // Block access to developer options if the user is not the owner, if user policy
283             // restricts it, or if the device has not been provisioned
284             mUnavailable = true;
285             addPreferencesFromResource(R.xml.development_prefs_not_available);
286             return;
287         }
288 
289         addPreferencesFromResource(R.xml.development_prefs);
290         final PreferenceScreen preferenceScreen = getPreferenceScreen();
291 
292         // Don't add to prefs lists or it'll disable itself when switched off
293         mEnableDeveloper = (SwitchPreference) findPreference(ENABLE_DEVELOPER);
294 
295         final PreferenceGroup debugDebuggingCategory = (PreferenceGroup)
296                 findPreference(DEBUG_DEBUGGING_CATEGORY_KEY);
297         mEnableAdb = findAndInitSwitchPref(ENABLE_ADB);
298         mClearAdbKeys = findPreference(CLEAR_ADB_KEYS);
299         if (!AdbProperties.secure().orElse(false)) {
300             if (debugDebuggingCategory != null) {
301                 debugDebuggingCategory.removePreference(mClearAdbKeys);
302             }
303         }
304         mAllPrefs.add(mClearAdbKeys);
305         mEnableTerminal = findAndInitSwitchPref(ENABLE_TERMINAL);
306         if (!isPackageInstalled(getActivity(), TERMINAL_APP_PACKAGE)) {
307             if (debugDebuggingCategory != null) {
308                 debugDebuggingCategory.removePreference(mEnableTerminal);
309             }
310             mEnableTerminal = null;
311         }
312 
313         mBugreport = findPreference(BUGREPORT);
314         mLogdSizeController.displayPreference(preferenceScreen);
315         mLogpersistController.displayPreference(preferenceScreen);
316 
317         mKeepScreenOn = findAndInitSwitchPref(KEEP_SCREEN_ON);
318         mBtHciSnoopLog = findAndInitSwitchPref(BT_HCI_SNOOP_LOG);
319         mEnableOemUnlock = findAndInitSwitchPref(ENABLE_OEM_UNLOCK);
320         if (!showEnableOemUnlockPreference()) {
321             removePreference(mEnableOemUnlock);
322             mEnableOemUnlock = null;
323         }
324 
325         // TODO: implement UI for TV
326         removePreference(RUNNING_APPS);
327 
328         mDebugViewAttributes = findAndInitSwitchPref(DEBUG_VIEW_ATTRIBUTES);
329         mForceAllowOnExternal = findAndInitSwitchPref(FORCE_ALLOW_ON_EXTERNAL_KEY);
330         mPassword = (PreferenceScreen) findPreference(LOCAL_BACKUP_PASSWORD);
331         // We don't have a backup password activity on TV
332         mPassword.setVisible(false);
333         mAllPrefs.add(mPassword);
334 
335         if (!mUm.isAdminUser()) {
336             disableForUser(mEnableAdb);
337             disableForUser(mClearAdbKeys);
338             disableForUser(mEnableTerminal);
339             disableForUser(mPassword);
340         }
341 
342         mDebugAppPref = findPreference(DEBUG_APP_KEY);
343         mAllPrefs.add(mDebugAppPref);
344         mWaitForDebugger = findAndInitSwitchPref(WAIT_FOR_DEBUGGER_KEY);
345 
346         mMockLocationAppPref = findPreference(MOCK_LOCATION_APP_KEY);
347         mAllPrefs.add(mMockLocationAppPref);
348 
349         mVerifyAppsOverUsb = findAndInitSwitchPref(VERIFY_APPS_OVER_USB_KEY);
350         if (!showVerifierSetting()) {
351             if (debugDebuggingCategory != null) {
352                 debugDebuggingCategory.removePreference(mVerifyAppsOverUsb);
353             } else {
354                 mVerifyAppsOverUsb.setEnabled(false);
355             }
356         }
357         mStrictMode = findAndInitSwitchPref(STRICT_MODE_KEY);
358         mPointerLocation = findAndInitSwitchPref(POINTER_LOCATION_KEY);
359         mShowTouches = findAndInitSwitchPref(SHOW_TOUCHES_KEY);
360         mShowScreenUpdates = findAndInitSwitchPref(SHOW_SCREEN_UPDATES_KEY);
361         mDisableOverlays = findAndInitSwitchPref(DISABLE_OVERLAYS_KEY);
362         mForceMsaa = findAndInitSwitchPref(FORCE_MSAA_KEY);
363         mTrackFrameTime = addListPreference(TRACK_FRAME_TIME_KEY);
364         mShowNonRectClip = addListPreference(SHOW_NON_RECTANGULAR_CLIP_KEY);
365         mShowHwScreenUpdates = findAndInitSwitchPref(SHOW_HW_SCREEN_UPDATES_KEY);
366         mShowHwLayersUpdates = findAndInitSwitchPref(SHOW_HW_LAYERS_UPDATES_KEY);
367         mDebugLayout = findAndInitSwitchPref(DEBUG_LAYOUT_KEY);
368         mForceRtlLayout = findAndInitSwitchPref(FORCE_RTL_LAYOUT_KEY);
369         mDebugHwOverdraw = addListPreference(DEBUG_HW_OVERDRAW_KEY);
370         mWifiDisplayCertification = findAndInitSwitchPref(WIFI_DISPLAY_CERTIFICATION_KEY);
371         mWifiVerboseLogging = findAndInitSwitchPref(WIFI_VERBOSE_LOGGING_KEY);
372         mMobileDataAlwaysOn = findAndInitSwitchPref(MOBILE_DATA_ALWAYS_ON);
373         mUsbConfiguration = addListPreference(USB_CONFIGURATION_KEY);
374 
375         mWindowAnimationScale = addListPreference(WINDOW_ANIMATION_SCALE_KEY);
376         mTransitionAnimationScale = addListPreference(TRANSITION_ANIMATION_SCALE_KEY);
377         mAnimatorDurationScale = addListPreference(ANIMATOR_DURATION_SCALE_KEY);
378         mOverlayDisplayDevices = addListPreference(OVERLAY_DISPLAY_DEVICES_KEY);
379         mOpenGLTraces = addListPreference(OPENGL_TRACES_KEY);
380         mSimulateColorSpace = addListPreference(SIMULATE_COLOR_SPACE);
381         mUSBAudio = findAndInitSwitchPref(USB_AUDIO_KEY);
382         mForceResizable = findAndInitSwitchPref(FORCE_RESIZABLE_KEY);
383 
384         mImmediatelyDestroyActivities = (SwitchPreference) findPreference(
385                 IMMEDIATELY_DESTROY_ACTIVITIES_KEY);
386         mAllPrefs.add(mImmediatelyDestroyActivities);
387         mResetSwitchPrefs.add(mImmediatelyDestroyActivities);
388 
389         mAppProcessLimit = addListPreference(APP_PROCESS_LIMIT_KEY);
390 
391         mShowAllANRs = (SwitchPreference) findPreference(
392                 SHOW_ALL_ANRS_KEY);
393         mAllPrefs.add(mShowAllANRs);
394         mResetSwitchPrefs.add(mShowAllANRs);
395 
396         Preference hdcpChecking = findPreference(HDCP_CHECKING_KEY);
397         if (hdcpChecking != null) {
398             mAllPrefs.add(hdcpChecking);
399             removePreferenceForProduction(hdcpChecking);
400         }
401 
402         // TODO: implement UI for TV
403         removePreference(KEY_CONVERT_FBE);
404 /*
405         // Please import android.sysprop.CryptoProperties when you uncomment this block.
406         PreferenceScreen convertFbePreference =
407                 (PreferenceScreen) findPreference(KEY_CONVERT_FBE);
408 
409         try {
410             IBinder service = ServiceManager.getService("mount");
411             IMountService mountService = IMountService.Stub.asInterface(service);
412             if (!mountService.isConvertibleToFBE()) {
413                 removePreference(KEY_CONVERT_FBE);
414             } else if (CryptoProperties.type().orElse(CryptoProperties.type_values.NONE) ==
415                        CryptoProperties.type_values.FILE) {
416                 convertFbePreference.setEnabled(false);
417                 convertFbePreference.setSummary(getResources()
418                         .getString(R.string.convert_to_file_encryption_done));
419             }
420         } catch(RemoteException e) {
421             removePreference(KEY_CONVERT_FBE);
422         }
423 */
424 
425         mColorModePreference = (ColorModePreference) findPreference(KEY_COLOR_MODE);
426         mColorModePreference.updateCurrentAndSupported();
427         if (mColorModePreference.getColorModeCount() < 2) {
428             removePreference(KEY_COLOR_MODE);
429             mColorModePreference = null;
430         }
431     }
432 
removePreference(String key)433     private void removePreference(String key) {
434         final Preference preference = findPreference(key);
435         if (preference != null) {
436             getPreferenceScreen().removePreference(preference);
437         }
438     }
439 
addListPreference(String prefKey)440     private ListPreference addListPreference(String prefKey) {
441         ListPreference pref = (ListPreference) findPreference(prefKey);
442         mAllPrefs.add(pref);
443         pref.setOnPreferenceChangeListener(this);
444         return pref;
445     }
446 
disableForUser(Preference pref)447     private void disableForUser(Preference pref) {
448         if (pref != null) {
449             pref.setEnabled(false);
450             mDisabledPrefs.add(pref);
451         }
452     }
453 
findAndInitSwitchPref(String key)454     private SwitchPreference findAndInitSwitchPref(String key) {
455         SwitchPreference pref = (SwitchPreference) findPreference(key);
456         if (pref == null) {
457             throw new IllegalArgumentException("Cannot find preference with key = " + key);
458         }
459         mAllPrefs.add(pref);
460         mResetSwitchPrefs.add(pref);
461         return pref;
462     }
463 
464     @Override
onActivityCreated(Bundle savedInstanceState)465     public void onActivityCreated(Bundle savedInstanceState) {
466         super.onActivityCreated(savedInstanceState);
467 
468         if (mUnavailable) {
469             if (mEnableDeveloper != null) {
470                 mEnableDeveloper.setEnabled(false);
471             }
472         }
473     }
474 
removePreferenceForProduction(Preference preference)475     private boolean removePreferenceForProduction(Preference preference) {
476         if ("user".equals(Build.TYPE)) {
477             removePreference(preference);
478             return true;
479         }
480         return false;
481     }
482 
removePreference(Preference preference)483     private void removePreference(Preference preference) {
484         getPreferenceScreen().removePreference(preference);
485         mAllPrefs.remove(preference);
486         mResetSwitchPrefs.remove(preference);
487     }
488 
setPrefsEnabledState(boolean enabled)489     private void setPrefsEnabledState(boolean enabled) {
490         for (final Preference pref : mAllPrefs) {
491             pref.setEnabled(enabled && !mDisabledPrefs.contains(pref));
492         }
493         mLogdSizeController.enablePreference(enabled);
494         mLogpersistController.enablePreference(enabled);
495         updateAllOptions();
496     }
497 
498     @Override
onResume()499     public void onResume() {
500         super.onResume();
501 
502         if (mUnavailable) {
503             return;
504         }
505 
506         if (mDpm.getMaximumTimeToLock(null) > 0) {
507             // A DeviceAdmin has specified a maximum time until the device
508             // will lock...  in this case we can't allow the user to turn
509             // on "stay awake when plugged in" because that would defeat the
510             // restriction.
511             mDisabledPrefs.add(mKeepScreenOn);
512         } else {
513             mDisabledPrefs.remove(mKeepScreenOn);
514         }
515 
516         mLastEnabledState = DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(getContext());
517         mEnableDeveloper.setChecked(mLastEnabledState);
518         setPrefsEnabledState(mLastEnabledState);
519 
520         if (mHaveDebugSettings && !mLastEnabledState) {
521             // Overall debugging is disabled, but there are some debug
522             // settings that are enabled.  This is an invalid state.  Switch
523             // to debug settings being enabled, so the user knows there is
524             // stuff enabled and can turn it all off if they want.
525             DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(getContext(), true);
526             mLastEnabledState = true;
527             mEnableDeveloper.setChecked(mLastEnabledState);
528             setPrefsEnabledState(mLastEnabledState);
529         }
530 
531         if (mColorModePreference != null) {
532             mColorModePreference.startListening();
533             mColorModePreference.updateCurrentAndSupported();
534         }
535 
536         if (mPendingDialogKey != null) {
537             recreateDialogForKey(mPendingDialogKey);
538             mPendingDialogKey = null;
539         }
540     }
541 
542     @Override
onPause()543     public void onPause() {
544         super.onPause();
545         if (mColorModePreference != null) {
546             mColorModePreference.stopListening();
547         }
548     }
549 
550     @Override
onSaveInstanceState(Bundle outState)551     public void onSaveInstanceState(Bundle outState) {
552         super.onSaveInstanceState(outState);
553         outState.putString(STATE_SHOWING_DIALOG_KEY, getKeyForShowingDialog());
554     }
555 
556     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)557     public View onCreateView(LayoutInflater inflater, ViewGroup container,
558             Bundle savedInstanceState) {
559         IntentFilter filter = new IntentFilter();
560         filter.addAction(UsbManager.ACTION_USB_STATE);
561         if (getActivity().registerReceiver(mUsbReceiver, filter) == null) {
562             updateUsbConfigurationValues();
563         }
564         return super.onCreateView(inflater, container, savedInstanceState);
565     }
566 
567     @Override
onDestroyView()568     public void onDestroyView() {
569         super.onDestroyView();
570 
571         getActivity().unregisterReceiver(mUsbReceiver);
572     }
573 
574     @Override
onDestroy()575     public void onDestroy() {
576         super.onDestroy();
577         dismissDialogs();
578     }
579 
updateSwitchPreference(SwitchPreference switchPreference, boolean value)580     void updateSwitchPreference(SwitchPreference switchPreference, boolean value) {
581         switchPreference.setChecked(value);
582         mHaveDebugSettings |= value;
583     }
584 
updateAllOptions()585     private void updateAllOptions() {
586         final Context context = getActivity();
587         final ContentResolver cr = context.getContentResolver();
588         mHaveDebugSettings = false;
589         updateSwitchPreference(mEnableAdb, Settings.Global.getInt(cr,
590                 Settings.Global.ADB_ENABLED, 0) != 0);
591         if (mEnableTerminal != null) {
592             updateSwitchPreference(mEnableTerminal,
593                     context.getPackageManager().getApplicationEnabledSetting(TERMINAL_APP_PACKAGE)
594                             == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
595         }
596         updateSwitchPreference(mKeepScreenOn, Settings.Global.getInt(cr,
597                 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0) != 0);
598         updateSwitchPreference(mBtHciSnoopLog,
599                 SystemProperties.getBoolean(BTSNOOP_ENABLE_PROPERTY, false));
600         if (mEnableOemUnlock != null) {
601             updateSwitchPreference(mEnableOemUnlock, isOemUnlockEnabled(getActivity()));
602             mEnableOemUnlock.setEnabled(isOemUnlockAllowed());
603         }
604         updateSwitchPreference(mDebugViewAttributes, Settings.Global.getInt(cr,
605                 Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0);
606         updateSwitchPreference(mForceAllowOnExternal, Settings.Global.getInt(cr,
607                 Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0);
608         updateHdcpValues();
609         updatePasswordSummary();
610         updateDebuggerOptions();
611         updateMockLocation();
612         updateStrictModeVisualOptions();
613         updatePointerLocationOptions();
614         updateShowTouchesOptions();
615         updateFlingerOptions();
616         updateMsaaOptions();
617         updateTrackFrameTimeOptions();
618         updateShowNonRectClipOptions();
619         updateShowHwScreenUpdatesOptions();
620         updateShowHwLayersUpdatesOptions();
621         updateDebugHwOverdrawOptions();
622         updateDebugLayoutOptions();
623         updateAnimationScaleOptions();
624         updateOverlayDisplayDevicesOptions();
625         updateOpenGLTracesOptions();
626         updateImmediatelyDestroyActivitiesOptions();
627         updateAppProcessLimitOptions();
628         updateShowAllANRsOptions();
629         updateVerifyAppsOverUsbOptions();
630         updateBugreportOptions();
631         updateForceRtlOptions();
632         mLogdSizeController.updateLogdSizeValues();
633         mLogpersistController.updateLogpersistValues();
634         updateWifiDisplayCertificationOptions();
635         updateWifiVerboseLoggingOptions();
636         updateMobileDataAlwaysOnOptions();
637         updateSimulateColorSpace();
638         updateUSBAudioOptions();
639         updateForceResizableOptions();
640     }
641 
resetDangerousOptions()642     private void resetDangerousOptions() {
643         SystemPropPoker.getInstance().blockPokes();
644         for (final SwitchPreference cb : mResetSwitchPrefs) {
645             if (cb.isChecked()) {
646                 cb.setChecked(false);
647                 onPreferenceTreeClick(cb);
648             }
649         }
650         resetDebuggerOptions();
651         mLogpersistController.writeLogpersistOption(null, true);
652         mLogdSizeController.writeLogdSizeOption(null);
653         writeAnimationScaleOption(0, mWindowAnimationScale, null);
654         writeAnimationScaleOption(1, mTransitionAnimationScale, null);
655         writeAnimationScaleOption(2, mAnimatorDurationScale, null);
656         // Only poke the color space setting if we control it.
657         if (usingDevelopmentColorSpace()) {
658             writeSimulateColorSpace(-1);
659         }
660         writeOverlayDisplayDevicesOptions(null);
661         writeAppProcessLimitOptions(null);
662         mHaveDebugSettings = false;
663         updateAllOptions();
664         SystemPropPoker.getInstance().unblockPokes();
665         SystemPropPoker.getInstance().poke();
666     }
667 
updateHdcpValues()668     private void updateHdcpValues() {
669         ListPreference hdcpChecking = (ListPreference) findPreference(HDCP_CHECKING_KEY);
670         if (hdcpChecking != null) {
671             String currentValue = SystemProperties.get(HDCP_CHECKING_PROPERTY);
672             String[] values = getResources().getStringArray(R.array.hdcp_checking_values);
673             String[] summaries = getResources().getStringArray(R.array.hdcp_checking_summaries);
674             int index = 1; // Defaults to drm-only. Needs to match with R.array.hdcp_checking_values
675             for (int i = 0; i < values.length; i++) {
676                 if (currentValue.equals(values[i])) {
677                     index = i;
678                     break;
679                 }
680             }
681             hdcpChecking.setValue(values[index]);
682             hdcpChecking.setSummary(summaries[index]);
683             hdcpChecking.setOnPreferenceChangeListener(this);
684         }
685     }
686 
updatePasswordSummary()687     private void updatePasswordSummary() {
688         try {
689             if (mBackupManager.hasBackupPassword()) {
690                 mPassword.setSummary(R.string.local_backup_password_summary_change);
691             } else {
692                 mPassword.setSummary(R.string.local_backup_password_summary_none);
693             }
694         } catch (RemoteException e) {
695             // ignore
696         }
697     }
698 
writeBtHciSnoopLogOptions()699     private void writeBtHciSnoopLogOptions() {
700         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
701         SystemProperties.set(BTSNOOP_ENABLE_PROPERTY,
702                 Boolean.toString(mBtHciSnoopLog.isChecked()));
703     }
704 
writeDebuggerOptions()705     private void writeDebuggerOptions() {
706         try {
707             ActivityManager.getService().setDebugApp(
708                     mDebugApp, mWaitForDebugger.isChecked(), true);
709         } catch (RemoteException ex) {
710             // ignore
711         }
712     }
713 
writeMockLocation()714     private void writeMockLocation() {
715         AppOpsManager appOpsManager =
716                 (AppOpsManager) getActivity().getSystemService(Context.APP_OPS_SERVICE);
717 
718         // Disable the app op of the previous mock location app if such.
719         List<AppOpsManager.PackageOps> packageOps =
720                 appOpsManager.getPackagesForOps(MOCK_LOCATION_APP_OPS);
721         if (packageOps != null) {
722             // Should be one but in case we are in a bad state due to use of command line tools.
723             for (AppOpsManager.PackageOps packageOp : packageOps) {
724                 if (packageOp.getOps().get(0).getMode() != AppOpsManager.MODE_ERRORED) {
725                     String oldMockLocationApp = packageOp.getPackageName();
726                     try {
727                         ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(
728                                 oldMockLocationApp, PackageManager.GET_DISABLED_COMPONENTS);
729                         appOpsManager.setMode(AppOpsManager.OP_MOCK_LOCATION, ai.uid,
730                                 oldMockLocationApp, AppOpsManager.MODE_ERRORED);
731                     } catch (PackageManager.NameNotFoundException e) {
732                         /* ignore */
733                     }
734                 }
735             }
736         }
737 
738         // Enable the app op of the new mock location app if such.
739         if (!TextUtils.isEmpty(mMockLocationApp)) {
740             try {
741                 ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(
742                         mMockLocationApp, PackageManager.GET_DISABLED_COMPONENTS);
743                 appOpsManager.setMode(AppOpsManager.OP_MOCK_LOCATION, ai.uid,
744                         mMockLocationApp, AppOpsManager.MODE_ALLOWED);
745             } catch (PackageManager.NameNotFoundException e) {
746                 /* ignore */
747             }
748         }
749     }
750 
resetDebuggerOptions()751     private static void resetDebuggerOptions() {
752         try {
753             ActivityManager.getService().setDebugApp(
754                     null, false, true);
755         } catch (RemoteException ex) {
756             // ignore
757         }
758     }
759 
updateDebuggerOptions()760     private void updateDebuggerOptions() {
761         mDebugApp = Settings.Global.getString(mContentResolver, Settings.Global.DEBUG_APP);
762         updateSwitchPreference(mWaitForDebugger, Settings.Global.getInt(mContentResolver,
763                 Settings.Global.WAIT_FOR_DEBUGGER, 0) != 0);
764         if (mDebugApp != null && mDebugApp.length() > 0) {
765             String label;
766             try {
767                 ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(mDebugApp,
768                         PackageManager.GET_DISABLED_COMPONENTS);
769                 CharSequence lab = getActivity().getPackageManager().getApplicationLabel(ai);
770                 label = lab != null ? lab.toString() : mDebugApp;
771             } catch (PackageManager.NameNotFoundException e) {
772                 label = mDebugApp;
773             }
774             mDebugAppPref.setSummary(getResources().getString(R.string.debug_app_set, label));
775             mWaitForDebugger.setEnabled(true);
776             mHaveDebugSettings = true;
777         } else {
778             mDebugAppPref.setSummary(getResources().getString(R.string.debug_app_not_set));
779             mWaitForDebugger.setEnabled(false);
780         }
781     }
782 
updateMockLocation()783     private void updateMockLocation() {
784         AppOpsManager appOpsManager =
785                 (AppOpsManager) getActivity().getSystemService(Context.APP_OPS_SERVICE);
786 
787         List<AppOpsManager.PackageOps> packageOps =
788                 appOpsManager.getPackagesForOps(MOCK_LOCATION_APP_OPS);
789         if (packageOps != null) {
790             for (AppOpsManager.PackageOps packageOp : packageOps) {
791                 if (packageOp.getOps().get(0).getMode() == AppOpsManager.MODE_ALLOWED) {
792                     mMockLocationApp = packageOps.get(0).getPackageName();
793                     break;
794                 }
795             }
796         }
797 
798         if (!TextUtils.isEmpty(mMockLocationApp)) {
799             String label = mMockLocationApp;
800             try {
801                 ApplicationInfo ai = getActivity().getPackageManager().getApplicationInfo(
802                         mMockLocationApp, PackageManager.GET_DISABLED_COMPONENTS);
803                 CharSequence appLabel = getActivity().getPackageManager().getApplicationLabel(ai);
804                 if (appLabel != null) {
805                     label = appLabel.toString();
806                 }
807             } catch (PackageManager.NameNotFoundException e) {
808                 /* ignore */
809             }
810 
811             mMockLocationAppPref.setSummary(getString(R.string.mock_location_app_set, label));
812             mHaveDebugSettings = true;
813         } else {
814             mMockLocationAppPref.setSummary(getString(R.string.mock_location_app_not_set));
815         }
816     }
817 
updateVerifyAppsOverUsbOptions()818     private void updateVerifyAppsOverUsbOptions() {
819         updateSwitchPreference(mVerifyAppsOverUsb,
820                 Settings.Global.getInt(mContentResolver,
821                 Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, 1) != 0);
822         mVerifyAppsOverUsb.setEnabled(enableVerifierSetting());
823     }
824 
writeVerifyAppsOverUsbOptions()825     private void writeVerifyAppsOverUsbOptions() {
826         Settings.Global.putInt(mContentResolver, Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB,
827                 mVerifyAppsOverUsb.isChecked() ? 1 : 0);
828     }
829 
enableVerifierSetting()830     private boolean enableVerifierSetting() {
831         if (Settings.Global.getInt(mContentResolver, Settings.Global.ADB_ENABLED, 0) == 0) {
832             return false;
833         }
834         if (Settings.Global.getInt(mContentResolver,
835                 Settings.Global.PACKAGE_VERIFIER_ENABLE, 1) == 0) {
836             return false;
837         } else {
838             final PackageManager pm = getActivity().getPackageManager();
839             final Intent verification = new Intent(Intent.ACTION_PACKAGE_NEEDS_VERIFICATION);
840             verification.setType(PACKAGE_MIME_TYPE);
841             verification.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
842             final List<ResolveInfo> receivers = pm.queryBroadcastReceivers(verification, 0);
843             if (receivers.size() == 0) {
844                 return false;
845             }
846         }
847         return true;
848     }
849 
showVerifierSetting()850     private boolean showVerifierSetting() {
851         return Settings.Global.getInt(mContentResolver,
852                 Settings.Global.PACKAGE_VERIFIER_SETTING_VISIBLE, 1) > 0;
853     }
854 
showEnableOemUnlockPreference()855     private static boolean showEnableOemUnlockPreference() {
856         return !SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals("");
857     }
858 
isOemUnlockAllowed()859     private boolean isOemUnlockAllowed() {
860         return !mUm.hasUserRestriction(UserManager.DISALLOW_OEM_UNLOCK);
861     }
862 
updateBugreportOptions()863     private void updateBugreportOptions() {
864         boolean enabled = "1".equals(SystemProperties.get("ro.debuggable"))
865                 || mEnableDeveloper.isChecked();
866         mBugreport.setEnabled(enabled);
867         final ComponentName componentName = new ComponentName("com.android.shell",
868                 "com.android.shell.BugreportStorageProvider");
869         getActivity().getPackageManager().setComponentEnabledSetting(componentName,
870                 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
871                         : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
872                 0);
873     }
874 
captureBugReport()875     private void captureBugReport() {
876         Toast.makeText(getActivity(), R.string.capturing_bugreport, Toast.LENGTH_SHORT).show();
877         try {
878             ActivityManager.getService()
879                     .requestBugReport(ActivityManager.BUGREPORT_OPTION_FULL);
880         } catch (RemoteException e) {
881             Log.e(TAG, "Error taking bugreport", e);
882         }
883     }
884 
885     // Returns the current state of the system property that controls
886     // strictmode flashes.  One of:
887     //    0: not explicitly set one way or another
888     //    1: on
889     //    2: off
currentStrictModeActiveIndex()890     private static int currentStrictModeActiveIndex() {
891         if (TextUtils.isEmpty(SystemProperties.get(StrictMode.VISUAL_PROPERTY))) {
892             return 0;
893         }
894         boolean enabled = SystemProperties.getBoolean(StrictMode.VISUAL_PROPERTY, false);
895         return enabled ? 1 : 2;
896     }
897 
writeStrictModeVisualOptions()898     private void writeStrictModeVisualOptions() {
899         try {
900             mWindowManager.setStrictModeVisualIndicatorPreference(mStrictMode.isChecked()
901                     ? "1" : "");
902         } catch (RemoteException e) {
903             // ignore
904         }
905     }
906 
updateStrictModeVisualOptions()907     private void updateStrictModeVisualOptions() {
908         updateSwitchPreference(mStrictMode, currentStrictModeActiveIndex() == 1);
909     }
910 
writePointerLocationOptions()911     private void writePointerLocationOptions() {
912         Settings.System.putInt(mContentResolver,
913                 Settings.System.POINTER_LOCATION, mPointerLocation.isChecked() ? 1 : 0);
914     }
915 
updatePointerLocationOptions()916     private void updatePointerLocationOptions() {
917         updateSwitchPreference(mPointerLocation,
918                 Settings.System.getInt(mContentResolver, Settings.System.POINTER_LOCATION, 0) != 0);
919     }
920 
writeShowTouchesOptions()921     private void writeShowTouchesOptions() {
922         Settings.System.putInt(mContentResolver,
923                 Settings.System.SHOW_TOUCHES, mShowTouches.isChecked() ? 1 : 0);
924     }
925 
updateShowTouchesOptions()926     private void updateShowTouchesOptions() {
927         updateSwitchPreference(mShowTouches,
928                 Settings.System.getInt(mContentResolver, Settings.System.SHOW_TOUCHES, 0) != 0);
929     }
930 
updateFlingerOptions()931     private void updateFlingerOptions() {
932         // magic communication with surface flinger.
933         try {
934             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
935             if (flinger != null) {
936                 Parcel data = Parcel.obtain();
937                 Parcel reply = Parcel.obtain();
938                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
939                 flinger.transact(1010, data, reply, 0);
940                 @SuppressWarnings("unused")
941                 int showCpu = reply.readInt();
942                 @SuppressWarnings("unused")
943                 int enableGL = reply.readInt();
944                 int showUpdates = reply.readInt();
945                 updateSwitchPreference(mShowScreenUpdates, showUpdates != 0);
946                 @SuppressWarnings("unused")
947                 int showBackground = reply.readInt();
948                 int disableOverlays = reply.readInt();
949                 updateSwitchPreference(mDisableOverlays, disableOverlays != 0);
950                 reply.recycle();
951                 data.recycle();
952             }
953         } catch (RemoteException ex) {
954             // ignore
955         }
956     }
957 
writeShowUpdatesOption()958     private void writeShowUpdatesOption() {
959         try {
960             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
961             if (flinger != null) {
962                 Parcel data = Parcel.obtain();
963                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
964                 final int showUpdates = mShowScreenUpdates.isChecked() ? 1 : 0;
965                 data.writeInt(showUpdates);
966                 flinger.transact(1002, data, null, 0);
967                 data.recycle();
968 
969                 updateFlingerOptions();
970             }
971         } catch (RemoteException ex) {
972             // ignore
973         }
974     }
975 
writeDisableOverlaysOption()976     private void writeDisableOverlaysOption() {
977         try {
978             IBinder flinger = ServiceManager.getService("SurfaceFlinger");
979             if (flinger != null) {
980                 Parcel data = Parcel.obtain();
981                 data.writeInterfaceToken("android.ui.ISurfaceComposer");
982                 final int disableOverlays = mDisableOverlays.isChecked() ? 1 : 0;
983                 data.writeInt(disableOverlays);
984                 flinger.transact(1008, data, null, 0);
985                 data.recycle();
986 
987                 updateFlingerOptions();
988             }
989         } catch (RemoteException ex) {
990             // ignore
991         }
992     }
993 
updateMsaaOptions()994     private void updateMsaaOptions() {
995         updateSwitchPreference(mForceMsaa, DisplayProperties.debug_force_msaa().orElse(false));
996     }
997 
writeMsaaOptions()998     private void writeMsaaOptions() {
999         DisplayProperties.debug_force_msaa(mForceMsaa.isChecked());
1000         SystemPropPoker.getInstance().poke();
1001     }
1002 
updateTrackFrameTimeOptions()1003     private void updateTrackFrameTimeOptions() {
1004         String value = SystemProperties.get(ThreadedRenderer.PROFILE_PROPERTY);
1005         if (value == null) {
1006             value = "";
1007         }
1008 
1009         CharSequence[] values = mTrackFrameTime.getEntryValues();
1010         for (int i = 0; i < values.length; i++) {
1011             if (value.contentEquals(values[i])) {
1012                 mTrackFrameTime.setValueIndex(i);
1013                 mTrackFrameTime.setSummary(mTrackFrameTime.getEntries()[i]);
1014                 return;
1015             }
1016         }
1017         mTrackFrameTime.setValueIndex(0);
1018         mTrackFrameTime.setSummary(mTrackFrameTime.getEntries()[0]);
1019     }
1020 
writeTrackFrameTimeOptions(Object newValue)1021     private void writeTrackFrameTimeOptions(Object newValue) {
1022         SystemProperties.set(ThreadedRenderer.PROFILE_PROPERTY,
1023                 newValue == null ? "" : newValue.toString());
1024         SystemPropPoker.getInstance().poke();
1025         updateTrackFrameTimeOptions();
1026     }
1027 
updateShowNonRectClipOptions()1028     private void updateShowNonRectClipOptions() {
1029         String value = SystemProperties.get(
1030                 ThreadedRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY);
1031         if (value == null) {
1032             value = "hide";
1033         }
1034 
1035         CharSequence[] values = mShowNonRectClip.getEntryValues();
1036         for (int i = 0; i < values.length; i++) {
1037             if (value.contentEquals(values[i])) {
1038                 mShowNonRectClip.setValueIndex(i);
1039                 mShowNonRectClip.setSummary(mShowNonRectClip.getEntries()[i]);
1040                 return;
1041             }
1042         }
1043         mShowNonRectClip.setValueIndex(0);
1044         mShowNonRectClip.setSummary(mShowNonRectClip.getEntries()[0]);
1045     }
1046 
writeShowNonRectClipOptions(Object newValue)1047     private void writeShowNonRectClipOptions(Object newValue) {
1048         SystemProperties.set(ThreadedRenderer.DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY,
1049                 newValue == null ? "" : newValue.toString());
1050         SystemPropPoker.getInstance().poke();
1051         updateShowNonRectClipOptions();
1052     }
1053 
updateShowHwScreenUpdatesOptions()1054     private void updateShowHwScreenUpdatesOptions() {
1055         updateSwitchPreference(mShowHwScreenUpdates,
1056                 SystemProperties.getBoolean(ThreadedRenderer.DEBUG_DIRTY_REGIONS_PROPERTY, false));
1057     }
1058 
writeShowHwScreenUpdatesOptions()1059     private void writeShowHwScreenUpdatesOptions() {
1060         SystemProperties.set(ThreadedRenderer.DEBUG_DIRTY_REGIONS_PROPERTY,
1061                 mShowHwScreenUpdates.isChecked() ? "true" : null);
1062         SystemPropPoker.getInstance().poke();
1063     }
1064 
updateShowHwLayersUpdatesOptions()1065     private void updateShowHwLayersUpdatesOptions() {
1066         updateSwitchPreference(mShowHwLayersUpdates, SystemProperties.getBoolean(
1067                 ThreadedRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY, false));
1068     }
1069 
writeShowHwLayersUpdatesOptions()1070     private void writeShowHwLayersUpdatesOptions() {
1071         SystemProperties.set(ThreadedRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY,
1072                 mShowHwLayersUpdates.isChecked() ? "true" : null);
1073         SystemPropPoker.getInstance().poke();
1074     }
1075 
updateDebugHwOverdrawOptions()1076     private void updateDebugHwOverdrawOptions() {
1077         String value = SystemProperties.get(ThreadedRenderer.DEBUG_OVERDRAW_PROPERTY);
1078         if (value == null) {
1079             value = "";
1080         }
1081 
1082         CharSequence[] values = mDebugHwOverdraw.getEntryValues();
1083         for (int i = 0; i < values.length; i++) {
1084             if (value.contentEquals(values[i])) {
1085                 mDebugHwOverdraw.setValueIndex(i);
1086                 mDebugHwOverdraw.setSummary(mDebugHwOverdraw.getEntries()[i]);
1087                 return;
1088             }
1089         }
1090         mDebugHwOverdraw.setValueIndex(0);
1091         mDebugHwOverdraw.setSummary(mDebugHwOverdraw.getEntries()[0]);
1092     }
1093 
writeDebugHwOverdrawOptions(Object newValue)1094     private void writeDebugHwOverdrawOptions(Object newValue) {
1095         SystemProperties.set(ThreadedRenderer.DEBUG_OVERDRAW_PROPERTY,
1096                 newValue == null ? "" : newValue.toString());
1097         SystemPropPoker.getInstance().poke();
1098         updateDebugHwOverdrawOptions();
1099     }
1100 
updateDebugLayoutOptions()1101     private void updateDebugLayoutOptions() {
1102         updateSwitchPreference(mDebugLayout,
1103                 DisplayProperties.debug_layout().orElse(false));
1104     }
1105 
writeDebugLayoutOptions()1106     private void writeDebugLayoutOptions() {
1107         DisplayProperties.debug_layout(mDebugLayout.isChecked());
1108         SystemPropPoker.getInstance().poke();
1109     }
1110 
updateSimulateColorSpace()1111     private void updateSimulateColorSpace() {
1112         final boolean enabled = Settings.Secure.getInt(
1113                 mContentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0) != 0;
1114         if (enabled) {
1115             final String mode = Integer.toString(Settings.Secure.getInt(
1116                     mContentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
1117                     AccessibilityManager.DALTONIZER_DISABLED));
1118             mSimulateColorSpace.setValue(mode);
1119             final int index = mSimulateColorSpace.findIndexOfValue(mode);
1120             if (index < 0) {
1121                 // We're using a mode controlled by accessibility preferences.
1122                 mSimulateColorSpace.setSummary(getString(R.string.daltonizer_type_overridden,
1123                         getString(R.string.accessibility_display_daltonizer_preference_title)));
1124             } else {
1125                 mSimulateColorSpace.setSummary("%s");
1126             }
1127         } else {
1128             mSimulateColorSpace.setValue(
1129                     Integer.toString(AccessibilityManager.DALTONIZER_DISABLED));
1130         }
1131     }
1132 
1133     /**
1134      * @return <code>true</code> if the color space preference is currently
1135      *         controlled by development settings
1136      */
usingDevelopmentColorSpace()1137     private boolean usingDevelopmentColorSpace() {
1138         final boolean enabled = Settings.Secure.getInt(
1139                 mContentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0) != 0;
1140         if (enabled) {
1141             final String mode = Integer.toString(Settings.Secure.getInt(
1142                     mContentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
1143                     AccessibilityManager.DALTONIZER_DISABLED));
1144             final int index = mSimulateColorSpace.findIndexOfValue(mode);
1145             if (index >= 0) {
1146                 // We're using a mode controlled by developer preferences.
1147                 return true;
1148             }
1149         }
1150         return false;
1151     }
1152 
writeSimulateColorSpace(Object value)1153     private void writeSimulateColorSpace(Object value) {
1154         final int newMode = Integer.parseInt(value.toString());
1155         if (newMode < 0) {
1156             Settings.Secure.putInt(mContentResolver,
1157                     Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0);
1158         } else {
1159             Settings.Secure.putInt(mContentResolver,
1160                     Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 1);
1161             Settings.Secure.putInt(mContentResolver,
1162                     Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, newMode);
1163         }
1164     }
1165 
updateUSBAudioOptions()1166     private void updateUSBAudioOptions() {
1167         updateSwitchPreference(mUSBAudio, Settings.Secure.getInt(mContentResolver,
1168                 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, 0) != 0);
1169     }
1170 
writeUSBAudioOptions()1171     private void writeUSBAudioOptions() {
1172         Settings.Secure.putInt(mContentResolver,
1173                 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED,
1174                 mUSBAudio.isChecked() ? 1 : 0);
1175     }
1176 
updateForceResizableOptions()1177     private void updateForceResizableOptions() {
1178         updateSwitchPreference(mForceResizable,
1179                 Settings.Global.getInt(mContentResolver,
1180                 Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, 0) != 0);
1181     }
1182 
writeForceResizableOptions()1183     private void writeForceResizableOptions() {
1184         Settings.Global.putInt(mContentResolver,
1185                 Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES,
1186                 mForceResizable.isChecked() ? 1 : 0);
1187     }
1188 
updateForceRtlOptions()1189     private void updateForceRtlOptions() {
1190         updateSwitchPreference(mForceRtlLayout,
1191                 Settings.Global.getInt(mContentResolver,
1192                         Settings.Global.DEVELOPMENT_FORCE_RTL, 0) != 0);
1193     }
1194 
writeForceRtlOptions()1195     private void writeForceRtlOptions() {
1196         boolean value = mForceRtlLayout.isChecked();
1197         Settings.Global.putInt(mContentResolver,
1198                 Settings.Global.DEVELOPMENT_FORCE_RTL, value ? 1 : 0);
1199         DisplayProperties.debug_force_rtl(value);
1200         LocalePicker.updateLocale(
1201                 getActivity().getResources().getConfiguration().getLocales().get(0));
1202     }
1203 
updateWifiDisplayCertificationOptions()1204     private void updateWifiDisplayCertificationOptions() {
1205         updateSwitchPreference(mWifiDisplayCertification, Settings.Global.getInt(
1206                 mContentResolver, Settings.Global.WIFI_DISPLAY_CERTIFICATION_ON, 0) != 0);
1207     }
1208 
writeWifiDisplayCertificationOptions()1209     private void writeWifiDisplayCertificationOptions() {
1210         Settings.Global.putInt(mContentResolver,
1211                 Settings.Global.WIFI_DISPLAY_CERTIFICATION_ON,
1212                 mWifiDisplayCertification.isChecked() ? 1 : 0);
1213     }
1214 
updateWifiVerboseLoggingOptions()1215     private void updateWifiVerboseLoggingOptions() {
1216         boolean enabled = mWifiManager.getVerboseLoggingLevel() > 0;
1217         updateSwitchPreference(mWifiVerboseLogging, enabled);
1218     }
1219 
writeWifiVerboseLoggingOptions()1220     private void writeWifiVerboseLoggingOptions() {
1221         mWifiManager.enableVerboseLogging(mWifiVerboseLogging.isChecked() ? 1 : 0);
1222     }
1223 
updateMobileDataAlwaysOnOptions()1224     private void updateMobileDataAlwaysOnOptions() {
1225         updateSwitchPreference(mMobileDataAlwaysOn, Settings.Global.getInt(mContentResolver,
1226                 Settings.Global.MOBILE_DATA_ALWAYS_ON, 0) != 0);
1227     }
1228 
writeMobileDataAlwaysOnOptions()1229     private void writeMobileDataAlwaysOnOptions() {
1230         Settings.Global.putInt(mContentResolver, Settings.Global.MOBILE_DATA_ALWAYS_ON,
1231                 mMobileDataAlwaysOn.isChecked() ? 1 : 0);
1232     }
1233 
updateUsbConfigurationValues()1234     private void updateUsbConfigurationValues() {
1235         if (mUsbConfiguration != null) {
1236             UsbManager manager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
1237 
1238             String[] values = getResources().getStringArray(R.array.usb_configuration_values);
1239             String[] titles = getResources().getStringArray(R.array.usb_configuration_titles);
1240             int index = 0;
1241             long functions = manager.getCurrentFunctions();
1242             for (int i = 0; i < titles.length; i++) {
1243                 if ((functions | UsbManager.usbFunctionsFromString(values[i])) != 0) {
1244                     index = i;
1245                     break;
1246                 }
1247             }
1248             mUsbConfiguration.setValue(values[index]);
1249             mUsbConfiguration.setSummary(titles[index]);
1250             mUsbConfiguration.setOnPreferenceChangeListener(this);
1251         }
1252     }
1253 
writeUsbConfigurationOption(Object newValue)1254     private void writeUsbConfigurationOption(Object newValue) {
1255         UsbManager manager = (UsbManager)getActivity().getSystemService(Context.USB_SERVICE);
1256         String function = newValue.toString();
1257         manager.setCurrentFunctions(UsbManager.usbFunctionsFromString(function));
1258     }
1259 
writeImmediatelyDestroyActivitiesOptions()1260     private void writeImmediatelyDestroyActivitiesOptions() {
1261         try {
1262             ActivityManager.getService().setAlwaysFinish(
1263                     mImmediatelyDestroyActivities.isChecked());
1264         } catch (RemoteException ex) {
1265             // ignore
1266         }
1267     }
1268 
updateImmediatelyDestroyActivitiesOptions()1269     private void updateImmediatelyDestroyActivitiesOptions() {
1270         updateSwitchPreference(mImmediatelyDestroyActivities, Settings.Global.getInt(
1271                 mContentResolver, Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) != 0);
1272     }
1273 
updateAnimationScaleValue(int which, ListPreference pref)1274     private void updateAnimationScaleValue(int which, ListPreference pref) {
1275         try {
1276             float scale = mWindowManager.getAnimationScale(which);
1277             if (scale != 1) {
1278                 mHaveDebugSettings = true;
1279             }
1280             CharSequence[] values = pref.getEntryValues();
1281             for (int i=0; i<values.length; i++) {
1282                 float val = Float.parseFloat(values[i].toString());
1283                 if (scale <= val) {
1284                     pref.setValueIndex(i);
1285                     pref.setSummary(pref.getEntries()[i]);
1286                     return;
1287                 }
1288             }
1289             pref.setValueIndex(values.length-1);
1290             pref.setSummary(pref.getEntries()[0]);
1291         } catch (RemoteException e) {
1292             // ignore
1293         }
1294     }
1295 
updateAnimationScaleOptions()1296     private void updateAnimationScaleOptions() {
1297         updateAnimationScaleValue(0, mWindowAnimationScale);
1298         updateAnimationScaleValue(1, mTransitionAnimationScale);
1299         updateAnimationScaleValue(2, mAnimatorDurationScale);
1300     }
1301 
writeAnimationScaleOption(int which, ListPreference pref, Object newValue)1302     private void writeAnimationScaleOption(int which, ListPreference pref, Object newValue) {
1303         try {
1304             float scale = newValue != null ? Float.parseFloat(newValue.toString()) : 1;
1305             mWindowManager.setAnimationScale(which, scale);
1306             updateAnimationScaleValue(which, pref);
1307         } catch (RemoteException e) {
1308             // ignore
1309         }
1310     }
1311 
updateOverlayDisplayDevicesOptions()1312     private void updateOverlayDisplayDevicesOptions() {
1313         String value = Settings.Global.getString(mContentResolver,
1314                 Settings.Global.OVERLAY_DISPLAY_DEVICES);
1315         if (value == null) {
1316             value = "";
1317         }
1318 
1319         CharSequence[] values = mOverlayDisplayDevices.getEntryValues();
1320         for (int i = 0; i < values.length; i++) {
1321             if (value.contentEquals(values[i])) {
1322                 mOverlayDisplayDevices.setValueIndex(i);
1323                 mOverlayDisplayDevices.setSummary(mOverlayDisplayDevices.getEntries()[i]);
1324                 return;
1325             }
1326         }
1327         mOverlayDisplayDevices.setValueIndex(0);
1328         mOverlayDisplayDevices.setSummary(mOverlayDisplayDevices.getEntries()[0]);
1329     }
1330 
writeOverlayDisplayDevicesOptions(Object newValue)1331     private void writeOverlayDisplayDevicesOptions(Object newValue) {
1332         Settings.Global.putString(mContentResolver, Settings.Global.OVERLAY_DISPLAY_DEVICES,
1333                 (String)newValue);
1334         updateOverlayDisplayDevicesOptions();
1335     }
1336 
updateOpenGLTracesOptions()1337     private void updateOpenGLTracesOptions() {
1338         String value = DisplayProperties.debug_opengl_trace().orElse("");
1339 
1340         CharSequence[] values = mOpenGLTraces.getEntryValues();
1341         for (int i = 0; i < values.length; i++) {
1342             if (value.contentEquals(values[i])) {
1343                 mOpenGLTraces.setValueIndex(i);
1344                 mOpenGLTraces.setSummary(mOpenGLTraces.getEntries()[i]);
1345                 return;
1346             }
1347         }
1348         mOpenGLTraces.setValueIndex(0);
1349         mOpenGLTraces.setSummary(mOpenGLTraces.getEntries()[0]);
1350     }
1351 
writeOpenGLTracesOptions(Object newValue)1352     private void writeOpenGLTracesOptions(Object newValue) {
1353         DisplayProperties.debug_opengl_trace(newValue == null ? "" : newValue.toString());
1354         SystemPropPoker.getInstance().poke();
1355         updateOpenGLTracesOptions();
1356     }
1357 
updateAppProcessLimitOptions()1358     private void updateAppProcessLimitOptions() {
1359         try {
1360             int limit = ActivityManager.getService().getProcessLimit();
1361             CharSequence[] values = mAppProcessLimit.getEntryValues();
1362             for (int i=0; i<values.length; i++) {
1363                 int val = Integer.parseInt(values[i].toString());
1364                 if (val >= limit) {
1365                     if (i != 0) {
1366                         mHaveDebugSettings = true;
1367                     }
1368                     mAppProcessLimit.setValueIndex(i);
1369                     mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[i]);
1370                     return;
1371                 }
1372             }
1373             mAppProcessLimit.setValueIndex(0);
1374             mAppProcessLimit.setSummary(mAppProcessLimit.getEntries()[0]);
1375         } catch (RemoteException e) {
1376             // ignore
1377         }
1378     }
1379 
writeAppProcessLimitOptions(Object newValue)1380     private void writeAppProcessLimitOptions(Object newValue) {
1381         try {
1382             int limit = newValue != null ? Integer.parseInt(newValue.toString()) : -1;
1383             ActivityManager.getService().setProcessLimit(limit);
1384             updateAppProcessLimitOptions();
1385         } catch (RemoteException e) {
1386             // ignore
1387         }
1388     }
1389 
writeShowAllANRsOptions()1390     private void writeShowAllANRsOptions() {
1391         Settings.Secure.putInt(mContentResolver, Settings.Secure.ANR_SHOW_BACKGROUND,
1392                 mShowAllANRs.isChecked() ? 1 : 0);
1393     }
1394 
updateShowAllANRsOptions()1395     private void updateShowAllANRsOptions() {
1396         updateSwitchPreference(mShowAllANRs, Settings.Secure.getInt(
1397                 mContentResolver, Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0);
1398     }
1399 
1400     @Override
onOemUnlockConfirm()1401     public void onOemUnlockConfirm() {
1402         mEnableOemUnlock.setChecked(true);
1403         setOemUnlockEnabled(getActivity(), true);
1404         updateAllOptions();
1405     }
1406 
1407     @Override
onEnableDevelopmentConfirm()1408     public void onEnableDevelopmentConfirm() {
1409         mEnableDeveloper.setChecked(true);
1410         DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(getContext(), true);
1411         mLastEnabledState = true;
1412         setPrefsEnabledState(true);
1413     }
1414 
1415     @Override
onEnableAdbConfirm()1416     public void onEnableAdbConfirm() {
1417         Settings.Global.putInt(mContentResolver, Settings.Global.ADB_ENABLED, 1);
1418         mEnableAdb.setChecked(true);
1419         updateVerifyAppsOverUsbOptions();
1420     }
1421 
1422     @Override
onActivityResult(int requestCode, int resultCode, Intent data)1423     public void onActivityResult(int requestCode, int resultCode, Intent data) {
1424         if (requestCode == RESULT_DEBUG_APP) {
1425             if (resultCode == Activity.RESULT_OK) {
1426                 mDebugApp = data.getAction();
1427                 writeDebuggerOptions();
1428                 updateDebuggerOptions();
1429             }
1430         } else if (requestCode == RESULT_MOCK_LOCATION_APP) {
1431             if (resultCode == Activity.RESULT_OK) {
1432                 mMockLocationApp = data.getAction();
1433                 writeMockLocation();
1434                 updateMockLocation();
1435             }
1436         } else {
1437             super.onActivityResult(requestCode, resultCode, data);
1438         }
1439     }
1440 
1441     @Override
onPreferenceTreeClick(Preference preference)1442     public boolean onPreferenceTreeClick(Preference preference) {
1443         if (ActivityManager.isUserAMonkey()) {
1444             return false;
1445         }
1446 
1447         if (preference == mEnableDeveloper) {
1448             if (mEnableDeveloper.isChecked()) {
1449                 // Pass to super to launch the dialog, then uncheck until the dialog
1450                 // result comes back
1451                 super.onPreferenceTreeClick(preference);
1452                 mEnableDeveloper.setChecked(false);
1453             } else {
1454                 resetDangerousOptions();
1455                 DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(getContext(), false);
1456                 mLastEnabledState = false;
1457                 setPrefsEnabledState(false);
1458             }
1459         } else if (preference == mBugreport) {
1460             captureBugReport();
1461         } else if (preference == mEnableAdb) {
1462             if (mEnableAdb.isChecked()) {
1463                 // Pass to super to launch the dialog, then uncheck until the dialog
1464                 // result comes back
1465                 super.onPreferenceTreeClick(preference);
1466                 mEnableAdb.setChecked(false);
1467             } else {
1468                 Settings.Global.putInt(mContentResolver, Settings.Global.ADB_ENABLED, 0);
1469                 mVerifyAppsOverUsb.setEnabled(false);
1470                 mVerifyAppsOverUsb.setChecked(false);
1471             }
1472         } else if (preference == mEnableTerminal) {
1473             final PackageManager pm = getActivity().getPackageManager();
1474             pm.setApplicationEnabledSetting(TERMINAL_APP_PACKAGE,
1475                     mEnableTerminal.isChecked() ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
1476                             : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
1477         } else if (preference == mKeepScreenOn) {
1478             Settings.Global.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
1479                     mKeepScreenOn.isChecked() ?
1480                             (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB)
1481                             : 0);
1482         } else if (preference == mBtHciSnoopLog) {
1483             writeBtHciSnoopLogOptions();
1484         } else if (preference == mEnableOemUnlock) {
1485             if (mEnableOemUnlock.isChecked()) {
1486                 // Pass to super to launch the dialog, then uncheck until the dialog
1487                 // result comes back
1488                 super.onPreferenceTreeClick(preference);
1489                 mEnableOemUnlock.setChecked(false);
1490             } else {
1491                 setOemUnlockEnabled(getActivity(), false);
1492             }
1493         } else if (preference == mMockLocationAppPref) {
1494             Intent intent = new Intent(getActivity(), AppPicker.class);
1495             intent.putExtra(AppPicker.EXTRA_REQUESTIING_PERMISSION,
1496                     Manifest.permission.ACCESS_MOCK_LOCATION);
1497             startActivityForResult(intent, RESULT_MOCK_LOCATION_APP);
1498         } else if (preference == mDebugViewAttributes) {
1499             Settings.Global.putInt(mContentResolver, Settings.Global.DEBUG_VIEW_ATTRIBUTES,
1500                     mDebugViewAttributes.isChecked() ? 1 : 0);
1501         } else if (preference == mForceAllowOnExternal) {
1502             Settings.Global.putInt(mContentResolver, Settings.Global.FORCE_ALLOW_ON_EXTERNAL,
1503                     mForceAllowOnExternal.isChecked() ? 1 : 0);
1504         } else if (preference == mDebugAppPref) {
1505             Intent intent = new Intent(getActivity(), AppPicker.class);
1506             intent.putExtra(AppPicker.EXTRA_DEBUGGABLE, true);
1507             startActivityForResult(intent, RESULT_DEBUG_APP);
1508         } else if (preference == mWaitForDebugger) {
1509             writeDebuggerOptions();
1510         } else if (preference == mVerifyAppsOverUsb) {
1511             writeVerifyAppsOverUsbOptions();
1512         } else if (preference == mStrictMode) {
1513             writeStrictModeVisualOptions();
1514         } else if (preference == mPointerLocation) {
1515             writePointerLocationOptions();
1516         } else if (preference == mShowTouches) {
1517             writeShowTouchesOptions();
1518         } else if (preference == mShowScreenUpdates) {
1519             writeShowUpdatesOption();
1520         } else if (preference == mDisableOverlays) {
1521             writeDisableOverlaysOption();
1522         } else if (preference == mImmediatelyDestroyActivities) {
1523             writeImmediatelyDestroyActivitiesOptions();
1524         } else if (preference == mShowAllANRs) {
1525             writeShowAllANRsOptions();
1526         } else if (preference == mForceMsaa) {
1527             writeMsaaOptions();
1528         } else if (preference == mShowHwScreenUpdates) {
1529             writeShowHwScreenUpdatesOptions();
1530         } else if (preference == mShowHwLayersUpdates) {
1531             writeShowHwLayersUpdatesOptions();
1532         } else if (preference == mDebugLayout) {
1533             writeDebugLayoutOptions();
1534         } else if (preference == mForceRtlLayout) {
1535             writeForceRtlOptions();
1536         } else if (preference == mWifiDisplayCertification) {
1537             writeWifiDisplayCertificationOptions();
1538         } else if (preference == mWifiVerboseLogging) {
1539             writeWifiVerboseLoggingOptions();
1540         } else if (preference == mMobileDataAlwaysOn) {
1541             writeMobileDataAlwaysOnOptions();
1542         } else if (preference == mUSBAudio) {
1543             writeUSBAudioOptions();
1544         } else if (preference == mForceResizable) {
1545             writeForceResizableOptions();
1546         } else {
1547             return super.onPreferenceTreeClick(preference);
1548         }
1549 
1550         return false;
1551     }
1552 
1553     @Override
onPreferenceChange(Preference preference, Object newValue)1554     public boolean onPreferenceChange(Preference preference, Object newValue) {
1555         if (HDCP_CHECKING_KEY.equals(preference.getKey())) {
1556             SystemProperties.set(HDCP_CHECKING_PROPERTY, newValue.toString());
1557             updateHdcpValues();
1558             SystemPropPoker.getInstance().poke();
1559             return true;
1560         } else if (preference == mUsbConfiguration) {
1561             writeUsbConfigurationOption(newValue);
1562             return true;
1563         } else if (preference == mWindowAnimationScale) {
1564             writeAnimationScaleOption(0, mWindowAnimationScale, newValue);
1565             return true;
1566         } else if (preference == mTransitionAnimationScale) {
1567             writeAnimationScaleOption(1, mTransitionAnimationScale, newValue);
1568             return true;
1569         } else if (preference == mAnimatorDurationScale) {
1570             writeAnimationScaleOption(2, mAnimatorDurationScale, newValue);
1571             return true;
1572         } else if (preference == mOverlayDisplayDevices) {
1573             writeOverlayDisplayDevicesOptions(newValue);
1574             return true;
1575         } else if (preference == mOpenGLTraces) {
1576             writeOpenGLTracesOptions(newValue);
1577             return true;
1578         } else if (preference == mTrackFrameTime) {
1579             writeTrackFrameTimeOptions(newValue);
1580             return true;
1581         } else if (preference == mDebugHwOverdraw) {
1582             writeDebugHwOverdrawOptions(newValue);
1583             return true;
1584         } else if (preference == mShowNonRectClip) {
1585             writeShowNonRectClipOptions(newValue);
1586             return true;
1587         } else if (preference == mAppProcessLimit) {
1588             writeAppProcessLimitOptions(newValue);
1589             return true;
1590         } else if (preference == mSimulateColorSpace) {
1591             writeSimulateColorSpace(newValue);
1592             return true;
1593         }
1594         return false;
1595     }
1596 
1597     /**
1598      * Iterates through preference controllers that show confirmation dialogs and returns the
1599      * preference key for the first currently showing dialog. Ideally there should only ever be one.
1600      * @return Preference key, or null if no dialog is showing
1601      */
getKeyForShowingDialog()1602     private String getKeyForShowingDialog() {
1603         // TODO: iterate through a fragment-wide list of PreferenceControllers and just pick out the
1604         // ConfirmationDialogController objects
1605         final List<ConfirmationDialogController> dialogControllers = new ArrayList<>(2);
1606         dialogControllers.add(mLogpersistController);
1607         for (ConfirmationDialogController dialogController : dialogControllers) {
1608             if (dialogController.isConfirmationDialogShowing()) {
1609                 return dialogController.getPreferenceKey();
1610             }
1611         }
1612         return null;
1613     }
1614 
1615     /**
1616      * Re-show the dialog we lost previously
1617      * @param preferenceKey Key for the preference the dialog is for
1618      */
recreateDialogForKey(String preferenceKey)1619     private void recreateDialogForKey(String preferenceKey) {
1620         // TODO: iterate through a fragment-wide list of PreferenceControllers and just pick out the
1621         // ConfirmationDialogController objects
1622         final List<ConfirmationDialogController> dialogControllers = new ArrayList<>(2);
1623         dialogControllers.add(mLogpersistController);
1624         for (ConfirmationDialogController dialogController : dialogControllers) {
1625             if (TextUtils.equals(preferenceKey, dialogController.getPreferenceKey())) {
1626                 dialogController.showConfirmationDialog(findPreference(preferenceKey));
1627             }
1628         }
1629     }
1630 
dismissDialogs()1631     private void dismissDialogs() {
1632         mLogpersistController.dismissConfirmationDialog();
1633     }
1634 
1635     private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
1636         @Override
1637         public void onReceive(Context context, Intent intent) {
1638             updateUsbConfigurationValues();
1639         }
1640     };
1641 
isPackageInstalled(Context context, String packageName)1642     private static boolean isPackageInstalled(Context context, String packageName) {
1643         try {
1644             return context.getPackageManager().getPackageInfo(packageName, 0) != null;
1645         } catch (PackageManager.NameNotFoundException e) {
1646             return false;
1647         }
1648     }
1649 
1650     /**
1651      * Returns whether or not this device is able to be OEM unlocked.
1652      */
isOemUnlockEnabled(Context context)1653     static boolean isOemUnlockEnabled(Context context) {
1654         PersistentDataBlockManager manager =(PersistentDataBlockManager)
1655                 context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
1656         return manager.getOemUnlockEnabled();
1657     }
1658 
1659     /**
1660      * Allows enabling or disabling OEM unlock on this device. OEM unlocked
1661      * devices allow users to flash other OSes to them.
1662      */
setOemUnlockEnabled(Context context, boolean enabled)1663     static void setOemUnlockEnabled(Context context, boolean enabled) {
1664         try {
1665             PersistentDataBlockManager manager = (PersistentDataBlockManager)
1666                     context.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
1667             manager.setOemUnlockEnabled(enabled);
1668         } catch (SecurityException e) {
1669             Log.e(TAG, "Fail to set oem unlock.", e);
1670         }
1671     }
1672 }
1673