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