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