DevelopmentSettings.java revision 4f4ddaa823bfa7bd2fdff97338ff1839739925dd
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;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.content.ContentResolver;
22import android.content.DialogInterface;
23import android.os.BatteryManager;
24import android.os.Build;
25import android.os.Bundle;
26import android.os.SystemProperties;
27import android.preference.CheckBoxPreference;
28import android.preference.ListPreference;
29import android.preference.Preference;
30import android.preference.PreferenceFragment;
31import android.preference.PreferenceScreen;
32import android.preference.Preference.OnPreferenceChangeListener;
33import android.provider.Settings;
34
35/*
36 * Displays preferences for application developers.
37 */
38public class DevelopmentSettings extends PreferenceFragment
39        implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
40                OnPreferenceChangeListener {
41
42    private static final String ENABLE_ADB = "enable_adb";
43    private static final String KEEP_SCREEN_ON = "keep_screen_on";
44    private static final String ALLOW_MOCK_LOCATION = "allow_mock_location";
45    private static final String HDCP_CHECKING_KEY = "hdcp_checking";
46    private static final String HDCP_CHECKING_PROPERTY = "persist.sys.hdcp_checking";
47
48    private CheckBoxPreference mEnableAdb;
49    private CheckBoxPreference mKeepScreenOn;
50    private CheckBoxPreference mAllowMockLocation;
51
52    // To track whether Yes was clicked in the adb warning dialog
53    private boolean mOkClicked;
54
55    private Dialog mOkDialog;
56
57    @Override
58    public void onCreate(Bundle icicle) {
59        super.onCreate(icicle);
60
61        addPreferencesFromResource(R.xml.development_prefs);
62
63        mEnableAdb = (CheckBoxPreference) findPreference(ENABLE_ADB);
64        mKeepScreenOn = (CheckBoxPreference) findPreference(KEEP_SCREEN_ON);
65        mAllowMockLocation = (CheckBoxPreference) findPreference(ALLOW_MOCK_LOCATION);
66
67        removeHdcpOptionsForProduction();
68    }
69
70    private void removeHdcpOptionsForProduction() {
71        if ("user".equals(Build.TYPE)) {
72            Preference hdcpChecking = findPreference(HDCP_CHECKING_KEY);
73            if (hdcpChecking != null) {
74                // Remove the preference
75                getPreferenceScreen().removePreference(hdcpChecking);
76            }
77        }
78    }
79
80    @Override
81    public void onResume() {
82        super.onResume();
83
84        final ContentResolver cr = getActivity().getContentResolver();
85        mEnableAdb.setChecked(Settings.Secure.getInt(cr,
86                Settings.Secure.ADB_ENABLED, 0) != 0);
87        mKeepScreenOn.setChecked(Settings.System.getInt(cr,
88                Settings.System.STAY_ON_WHILE_PLUGGED_IN, 0) != 0);
89        mAllowMockLocation.setChecked(Settings.Secure.getInt(cr,
90                Settings.Secure.ALLOW_MOCK_LOCATION, 0) != 0);
91        updateHdcpValues();
92    }
93
94    private void updateHdcpValues() {
95        int index = 1; // Defaults to drm-only. Needs to match with R.array.hdcp_checking_values
96        ListPreference hdcpChecking = (ListPreference) findPreference(HDCP_CHECKING_KEY);
97        if (hdcpChecking != null) {
98            String currentValue = SystemProperties.get(HDCP_CHECKING_PROPERTY);
99            String[] values = getResources().getStringArray(R.array.hdcp_checking_values);
100            String[] summaries = getResources().getStringArray(R.array.hdcp_checking_summaries);
101            for (int i = 0; i < values.length; i++) {
102                if (currentValue.equals(values[i])) {
103                    index = i;
104                    break;
105                }
106            }
107            hdcpChecking.setValue(values[index]);
108            hdcpChecking.setSummary(summaries[index]);
109            hdcpChecking.setOnPreferenceChangeListener(this);
110        }
111    }
112
113    @Override
114    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
115
116        if (Utils.isMonkeyRunning()) {
117            return false;
118        }
119
120        if (preference == mEnableAdb) {
121            if (mEnableAdb.isChecked()) {
122                mOkClicked = false;
123                if (mOkDialog != null) dismissDialog();
124                mOkDialog = new AlertDialog.Builder(getActivity()).setMessage(
125                        getActivity().getResources().getString(R.string.adb_warning_message))
126                        .setTitle(R.string.adb_warning_title)
127                        .setIcon(android.R.drawable.ic_dialog_alert)
128                        .setPositiveButton(android.R.string.yes, this)
129                        .setNegativeButton(android.R.string.no, this)
130                        .show();
131                mOkDialog.setOnDismissListener(this);
132            } else {
133                Settings.Secure.putInt(getActivity().getContentResolver(),
134                        Settings.Secure.ADB_ENABLED, 0);
135            }
136        } else if (preference == mKeepScreenOn) {
137            Settings.System.putInt(getActivity().getContentResolver(),
138                    Settings.System.STAY_ON_WHILE_PLUGGED_IN,
139                    mKeepScreenOn.isChecked() ?
140                    (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);
141        } else if (preference == mAllowMockLocation) {
142            Settings.Secure.putInt(getActivity().getContentResolver(),
143                    Settings.Secure.ALLOW_MOCK_LOCATION,
144                    mAllowMockLocation.isChecked() ? 1 : 0);
145        }
146
147        return false;
148    }
149
150    private void dismissDialog() {
151        if (mOkDialog == null) return;
152        mOkDialog.dismiss();
153        mOkDialog = null;
154    }
155
156    public void onClick(DialogInterface dialog, int which) {
157        if (which == DialogInterface.BUTTON_POSITIVE) {
158            mOkClicked = true;
159            Settings.Secure.putInt(getActivity().getContentResolver(),
160                    Settings.Secure.ADB_ENABLED, 1);
161        } else {
162            // Reset the toggle
163            mEnableAdb.setChecked(false);
164        }
165    }
166
167    public void onDismiss(DialogInterface dialog) {
168        // Assuming that onClick gets called first
169        if (!mOkClicked) {
170            mEnableAdb.setChecked(false);
171        }
172    }
173
174    @Override
175    public void onDestroy() {
176        dismissDialog();
177        super.onDestroy();
178    }
179
180    @Override
181    public boolean onPreferenceChange(Preference preference, Object newValue) {
182        if (HDCP_CHECKING_KEY.equals(preference.getKey())) {
183            SystemProperties.set(HDCP_CHECKING_PROPERTY, newValue.toString());
184            updateHdcpValues();
185            return true;
186        }
187        return false;
188    }
189}
190