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