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