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