blob: c1a509ae5734f9b26615339d0f0bada3a227ed29 [file] [log] [blame]
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 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
19
20import android.app.Activity;
21import android.content.Context;
22import android.content.Intent;
23import android.content.SharedPreferences;
24import android.location.LocationManager;
25import android.os.Bundle;
26import android.preference.CheckBoxPreference;
27import android.preference.Preference;
28import android.preference.PreferenceActivity;
29import android.preference.PreferenceCategory;
30import android.preference.PreferenceScreen;
31import android.provider.Settings;
32import android.util.Config;
33import android.util.Log;
34
35import com.android.internal.widget.LockPatternUtils;
36
37/**
38 * Gesture lock pattern settings.
39 */
40public class SecuritySettings extends PreferenceActivity
41 implements SharedPreferences.OnSharedPreferenceChangeListener {
42
43 // Lock Settings
44
45 private static final String KEY_LOCK_ENABLED = "lockenabled";
46 private static final String KEY_VISIBLE_PATTERN = "visiblepattern";
47 private static final int CONFIRM_PATTERN_REQUEST_CODE = 55;
48
49 private LockPatternUtils mLockPatternUtils;
50 private CheckBoxPreference mLockEnabled;
51 private CheckBoxPreference mVisiblePattern;
52 private Preference mChoosePattern;
53
54 private CheckBoxPreference mShowPassword;
55
56 // Location Settings
57
58 private static final String LOCATION_NETWORK = "location_network";
59 private static final String LOCATION_GPS = "location_gps";
60
61 private CheckBoxPreference mNetwork;
62 private CheckBoxPreference mGps;
63 private LocationManager mLocationManager;
64
65 @Override
66 protected void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 addPreferencesFromResource(R.xml.security_settings);
69
70 mLockPatternUtils = new LockPatternUtils(getContentResolver());
71
72 createPreferenceHierarchy();
73
74 // Get the available location providers
75 mLocationManager = (LocationManager)
76 getSystemService(Context.LOCATION_SERVICE);
77
78 mNetwork = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_NETWORK);
79 mGps = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_GPS);
80 getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
81 updateToggles();
82 }
83
84 private PreferenceScreen createPreferenceHierarchy() {
85 // Root
86 PreferenceScreen root = this.getPreferenceScreen();
87
88 // Inline preferences
89 PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
90 inlinePrefCat.setTitle(R.string.lock_settings_title);
91 root.addPreference(inlinePrefCat);
92
93 // autolock toggle
94 mLockEnabled = new LockEnabledPref(this);
95 mLockEnabled.setTitle(R.string.lockpattern_settings_enable_title);
96 mLockEnabled.setSummary(R.string.lockpattern_settings_enable_summary);
97 mLockEnabled.setKey(KEY_LOCK_ENABLED);
98 inlinePrefCat.addPreference(mLockEnabled);
99
100 // visible pattern
101 mVisiblePattern = new CheckBoxPreference(this);
102 mVisiblePattern.setKey(KEY_VISIBLE_PATTERN);
103 mVisiblePattern.setTitle(R.string.lockpattern_settings_enable_visible_pattern_title);
104 inlinePrefCat.addPreference(mVisiblePattern);
105
106 // change pattern lock
107 Intent intent = new Intent();
108 intent.setClassName("com.android.settings",
109 "com.android.settings.ChooseLockPatternTutorial");
110 mChoosePattern = getPreferenceManager().createPreferenceScreen(this);
111 mChoosePattern.setIntent(intent);
112 inlinePrefCat.addPreference(mChoosePattern);
113
114 PreferenceScreen simLockPreferences = getPreferenceManager()
115 .createPreferenceScreen(this);
116 simLockPreferences.setTitle(R.string.sim_lock_settings_category);
117 // Intent to launch SIM lock settings
118 intent = new Intent();
119 intent.setClassName("com.android.settings", "com.android.settings.SimLockSettings");
120 simLockPreferences.setIntent(intent);
121
122 PreferenceCategory simLockCat = new PreferenceCategory(this);
123 simLockCat.setTitle(R.string.sim_lock_settings_title);
124 root.addPreference(simLockCat);
125 simLockCat.addPreference(simLockPreferences);
126
127 // Passwords
128 PreferenceCategory passwordsCat = new PreferenceCategory(this);
129 passwordsCat.setTitle(R.string.security_passwords_title);
130 root.addPreference(passwordsCat);
131
132 CheckBoxPreference showPassword = mShowPassword = new CheckBoxPreference(this);
133 showPassword.setKey("show_password");
134 showPassword.setTitle(R.string.show_password);
135 showPassword.setSummary(R.string.show_password_summary);
136 showPassword.setPersistent(false);
137 passwordsCat.addPreference(showPassword);
138
139 return root;
140 }
141
142 @Override
143 protected void onResume() {
144 super.onResume();
145
146 boolean patternExists = mLockPatternUtils.savedPatternExists();
147 mLockEnabled.setEnabled(patternExists);
148 mVisiblePattern.setEnabled(patternExists);
149
150 mLockEnabled.setChecked(mLockPatternUtils.isLockPatternEnabled());
151 mVisiblePattern.setChecked(mLockPatternUtils.isVisiblePatternEnabled());
152
153 int chooseStringRes = mLockPatternUtils.savedPatternExists() ?
154 R.string.lockpattern_settings_change_lock_pattern :
155 R.string.lockpattern_settings_choose_lock_pattern;
156 mChoosePattern.setTitle(chooseStringRes);
157
158 mShowPassword
159 .setChecked(Settings.System.getInt(getContentResolver(),
160 Settings.System.TEXT_SHOW_PASSWORD, 1) != 0);
161 }
162
163 @Override
164 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
165 Preference preference) {
166 final String key = preference.getKey();
167
168 if (KEY_LOCK_ENABLED.equals(key)) {
169 mLockPatternUtils.setLockPatternEnabled(isToggled(preference));
170 } else if (KEY_VISIBLE_PATTERN.equals(key)) {
171 mLockPatternUtils.setVisiblePatternEnabled(isToggled(preference));
172 } else if (preference == mShowPassword) {
173 Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
174 mShowPassword.isChecked() ? 1 : 0);
175 }
176
177 return false;
178 }
179
180 /*
181 * Creates toggles for each available location provider
182 */
183 private void updateToggles() {
184 String providers = getAllowedProviders();
185 mNetwork.setChecked(providers.contains(LocationManager.NETWORK_PROVIDER));
186 mGps.setChecked(providers.contains(LocationManager.GPS_PROVIDER));
187 }
188
189 private void updateProviders() {
190 String preferredProviders = "";
191 if (mNetwork.isChecked()) {
192 preferredProviders += LocationManager.NETWORK_PROVIDER;
193 }
194 if (mGps.isChecked()) {
195 preferredProviders += "," + LocationManager.GPS_PROVIDER;
196 }
197 setProviders(preferredProviders);
198 }
199
200 private void setProviders(String providers) {
201 // Update the system setting LOCATION_PROVIDERS_ALLOWED
202 Settings.System.putString(getContentResolver(),
203 Settings.System.LOCATION_PROVIDERS_ALLOWED, providers);
204 if (Config.LOGV) {
205 Log.v("Location Accuracy", "Setting LOCATION_PROVIDERS_ALLOWED = " + providers);
206 }
207 // Inform the location manager about the changes
208 mLocationManager.updateProviders();
209 }
210
211 /**
212 * @return string containing a list of providers that have been enabled for use
213 */
214 private String getAllowedProviders() {
215 String allowedProviders =
216 Settings.System.getString(getContentResolver(),
217 Settings.System.LOCATION_PROVIDERS_ALLOWED);
218 if (allowedProviders == null) {
219 allowedProviders = "";
220 }
221 return allowedProviders;
222 }
223
224 public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
225 if (LOCATION_NETWORK.equals(key) || LOCATION_GPS.equals(key)) {
226 updateProviders();
227 }
228 }
229
230 private boolean isToggled(Preference pref) {
231 return ((CheckBoxPreference) pref).isChecked();
232 }
233
234
235 /**
236 * For the user to disable keyguard, we first make them verify their
237 * existing pattern.
238 */
239 private class LockEnabledPref extends CheckBoxPreference {
240
241 public LockEnabledPref(Context context) {
242 super(context);
243 }
244
245 @Override
246 protected void onClick() {
247 if (isChecked() && mLockPatternUtils.savedPatternExists()) {
248 confirmPatternThenDisable();
249 } else {
250 super.onClick();
251 }
252 }
253 }
254
255 /**
256 * Launch screen to confirm the existing lock pattern.
257 * @see #onActivityResult(int, int, android.content.Intent)
258 */
259 private void confirmPatternThenDisable() {
260 final Intent intent = new Intent();
261 intent.setClassName("com.android.settings", "com.android.settings.ConfirmLockPattern");
262 startActivityForResult(intent, CONFIRM_PATTERN_REQUEST_CODE);
263 }
264
265 /**
266 * @see #confirmPatternThenDisable
267 */
268 @Override
269 protected void onActivityResult(int requestCode, int resultCode,
270 Intent data) {
271 super.onActivityResult(requestCode, resultCode, data);
272
273 if (requestCode != CONFIRM_PATTERN_REQUEST_CODE) {
274 return;
275 }
276
277 if (resultCode == Activity.RESULT_OK) {
278 mLockPatternUtils.setLockPatternEnabled(false);
279 }
280 }
281}