2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
20 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
22 import android.app.AlertDialog;
23 import android.app.admin.DevicePolicyManager;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.os.Vibrator;
29 import android.preference.CheckBoxPreference;
30 import android.preference.ListPreference;
31 import android.preference.Preference;
32 import android.preference.Preference.OnPreferenceChangeListener;
33 import android.preference.PreferenceGroup;
34 import android.preference.PreferenceScreen;
35 import android.provider.Settings;
36 import android.security.KeyStore;
37 import android.telephony.TelephonyManager;
38 import android.util.Log;
40 import com.android.internal.telephony.Phone;
41 import com.android.internal.widget.LockPatternUtils;
43 import java.util.ArrayList;
46 * Gesture lock pattern settings.
48 public class SecuritySettings extends SettingsPreferenceFragment
49 implements OnPreferenceChangeListener, DialogInterface.OnClickListener {
52 private static final String KEY_UNLOCK_SET_OR_CHANGE = "unlock_set_or_change";
53 private static final String KEY_LOCK_ENABLED = "lockenabled";
54 private static final String KEY_VISIBLE_PATTERN = "visiblepattern";
55 private static final String KEY_TACTILE_FEEDBACK_ENABLED = "unlock_tactile_feedback";
56 private static final String KEY_SECURITY_CATEGORY = "security_category";
57 private static final String KEY_LOCK_AFTER_TIMEOUT = "lock_after_timeout";
58 private static final int SET_OR_CHANGE_LOCK_METHOD_REQUEST = 123;
61 private static final String KEY_SIM_LOCK = "sim_lock";
62 private static final String KEY_SHOW_PASSWORD = "show_password";
63 private static final String KEY_RESET_CREDENTIALS = "reset_credentials";
64 private static final String KEY_TOGGLE_INSTALL_APPLICATIONS = "toggle_install_applications";
66 DevicePolicyManager mDPM;
68 private ChooseLockSettingsHelper mChooseLockSettingsHelper;
69 private LockPatternUtils mLockPatternUtils;
70 private ListPreference mLockAfter;
72 private CheckBoxPreference mVisiblePattern;
73 private CheckBoxPreference mTactileFeedback;
75 private CheckBoxPreference mShowPassword;
77 private Preference mResetCredentials;
79 private CheckBoxPreference mToggleAppInstallation;
80 private DialogInterface mWarnInstallApps;
83 public void onCreate(Bundle savedInstanceState) {
84 super.onCreate(savedInstanceState);
86 mLockPatternUtils = new LockPatternUtils(getActivity());
88 mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
90 mChooseLockSettingsHelper = new ChooseLockSettingsHelper(getActivity());
93 private PreferenceScreen createPreferenceHierarchy() {
94 PreferenceScreen root = getPreferenceScreen();
98 addPreferencesFromResource(R.xml.security_settings);
99 root = getPreferenceScreen();
101 // Add options for lock/unlock screen
103 if (!mLockPatternUtils.isSecure()) {
104 if (mLockPatternUtils.isLockScreenDisabled()) {
105 resid = R.xml.security_settings_lockscreen;
107 resid = R.xml.security_settings_chooser;
109 } else if (mLockPatternUtils.usingBiometricWeak() &&
110 mLockPatternUtils.isBiometricWeakInstalled()) {
111 resid = R.xml.security_settings_biometric_weak;
113 switch (mLockPatternUtils.getKeyguardStoredPasswordQuality()) {
114 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
115 resid = R.xml.security_settings_pattern;
117 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
118 resid = R.xml.security_settings_pin;
120 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
121 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
122 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
123 resid = R.xml.security_settings_password;
126 // TODO: enable facepass options
128 addPreferencesFromResource(resid);
131 // Add options for device encryption
132 DevicePolicyManager dpm =
133 (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
135 switch (dpm.getStorageEncryptionStatus()) {
136 case DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE:
137 // The device is currently encrypted.
138 addPreferencesFromResource(R.xml.security_settings_encrypted);
140 case DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE:
141 // This device supports encryption but isn't encrypted.
142 addPreferencesFromResource(R.xml.security_settings_unencrypted);
146 // lock after preference
147 mLockAfter = (ListPreference) root.findPreference(KEY_LOCK_AFTER_TIMEOUT);
148 if (mLockAfter != null) {
149 setupLockAfterPreference();
150 updateLockAfterPreferenceSummary();
154 mVisiblePattern = (CheckBoxPreference) root.findPreference(KEY_VISIBLE_PATTERN);
156 // tactile feedback. Should be common to all unlock preference screens.
157 mTactileFeedback = (CheckBoxPreference) root.findPreference(KEY_TACTILE_FEEDBACK_ENABLED);
158 if (!((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).hasVibrator()) {
159 PreferenceGroup securityCategory = (PreferenceGroup)
160 root.findPreference(KEY_SECURITY_CATEGORY);
161 if (securityCategory != null && mTactileFeedback != null) {
162 securityCategory.removePreference(mTactileFeedback);
166 // Append the rest of the settings
167 addPreferencesFromResource(R.xml.security_settings_misc);
169 // Do not display SIM lock for CDMA phone
170 TelephonyManager tm = TelephonyManager.getDefault();
171 if ((TelephonyManager.PHONE_TYPE_CDMA == tm.getCurrentPhoneType()) &&
172 (tm.getLteOnCdmaMode() != Phone.LTE_ON_CDMA_TRUE)) {
173 root.removePreference(root.findPreference(KEY_SIM_LOCK));
177 mShowPassword = (CheckBoxPreference) root.findPreference(KEY_SHOW_PASSWORD);
179 // Credential storage
180 mResetCredentials = root.findPreference(KEY_RESET_CREDENTIALS);
182 mToggleAppInstallation = (CheckBoxPreference) findPreference(
183 KEY_TOGGLE_INSTALL_APPLICATIONS);
184 mToggleAppInstallation.setChecked(isNonMarketAppsAllowed());
189 private boolean isNonMarketAppsAllowed() {
190 return Settings.Secure.getInt(getContentResolver(),
191 Settings.Secure.INSTALL_NON_MARKET_APPS, 0) > 0;
194 private void setNonMarketAppsAllowed(boolean enabled) {
195 // Change the system setting
196 Settings.Secure.putInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS,
200 private void warnAppInstallation() {
201 // TODO: DialogFragment?
202 mWarnInstallApps = new AlertDialog.Builder(getActivity()).setTitle(
203 getResources().getString(R.string.error_title))
204 .setIcon(com.android.internal.R.drawable.ic_dialog_alert)
205 .setMessage(getResources().getString(R.string.install_all_warning))
206 .setPositiveButton(android.R.string.yes, this)
207 .setNegativeButton(android.R.string.no, null)
211 public void onClick(DialogInterface dialog, int which) {
212 if (dialog == mWarnInstallApps && which == DialogInterface.BUTTON_POSITIVE) {
213 setNonMarketAppsAllowed(true);
214 mToggleAppInstallation.setChecked(true);
219 public void onDestroy() {
221 if (mWarnInstallApps != null) {
222 mWarnInstallApps.dismiss();
226 private void setupLockAfterPreference() {
227 // Compatible with pre-Froyo
228 long currentTimeout = Settings.Secure.getLong(getContentResolver(),
229 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
230 mLockAfter.setValue(String.valueOf(currentTimeout));
231 mLockAfter.setOnPreferenceChangeListener(this);
232 final long adminTimeout = (mDPM != null ? mDPM.getMaximumTimeToLock(null) : 0);
233 final long displayTimeout = Math.max(0,
234 Settings.System.getInt(getContentResolver(), SCREEN_OFF_TIMEOUT, 0));
235 if (adminTimeout > 0) {
236 // This setting is a slave to display timeout when a device policy is enforced.
237 // As such, maxLockTimeout = adminTimeout - displayTimeout.
238 // If there isn't enough time, shows "immediately" setting.
239 disableUnusableTimeouts(Math.max(0, adminTimeout - displayTimeout));
243 private void updateLockAfterPreferenceSummary() {
244 // Update summary message with current value
245 long currentTimeout = Settings.Secure.getLong(getContentResolver(),
246 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, 5000);
247 final CharSequence[] entries = mLockAfter.getEntries();
248 final CharSequence[] values = mLockAfter.getEntryValues();
250 for (int i = 0; i < values.length; i++) {
251 long timeout = Long.valueOf(values[i].toString());
252 if (currentTimeout >= timeout) {
256 mLockAfter.setSummary(getString(R.string.lock_after_timeout_summary, entries[best]));
259 private void disableUnusableTimeouts(long maxTimeout) {
260 final CharSequence[] entries = mLockAfter.getEntries();
261 final CharSequence[] values = mLockAfter.getEntryValues();
262 ArrayList<CharSequence> revisedEntries = new ArrayList<CharSequence>();
263 ArrayList<CharSequence> revisedValues = new ArrayList<CharSequence>();
264 for (int i = 0; i < values.length; i++) {
265 long timeout = Long.valueOf(values[i].toString());
266 if (timeout <= maxTimeout) {
267 revisedEntries.add(entries[i]);
268 revisedValues.add(values[i]);
271 if (revisedEntries.size() != entries.length || revisedValues.size() != values.length) {
272 mLockAfter.setEntries(
273 revisedEntries.toArray(new CharSequence[revisedEntries.size()]));
274 mLockAfter.setEntryValues(
275 revisedValues.toArray(new CharSequence[revisedValues.size()]));
276 final int userPreference = Integer.valueOf(mLockAfter.getValue());
277 if (userPreference <= maxTimeout) {
278 mLockAfter.setValue(String.valueOf(userPreference));
280 // There will be no highlighted selection since nothing in the list matches
281 // maxTimeout. The user can still select anything less than maxTimeout.
282 // TODO: maybe append maxTimeout to the list and mark selected.
285 mLockAfter.setEnabled(revisedEntries.size() > 0);
289 public void onResume() {
292 // Make sure we reload the preference hierarchy since some of these settings
293 // depend on others...
294 createPreferenceHierarchy();
296 final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
297 if (mVisiblePattern != null) {
298 mVisiblePattern.setChecked(lockPatternUtils.isVisiblePatternEnabled());
300 if (mTactileFeedback != null) {
301 mTactileFeedback.setChecked(lockPatternUtils.isTactileFeedbackEnabled());
304 mShowPassword.setChecked(Settings.System.getInt(getContentResolver(),
305 Settings.System.TEXT_SHOW_PASSWORD, 1) != 0);
307 KeyStore.State state = KeyStore.getInstance().state();
308 mResetCredentials.setEnabled(state != KeyStore.State.UNINITIALIZED);
312 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
313 final String key = preference.getKey();
315 final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
316 if (KEY_UNLOCK_SET_OR_CHANGE.equals(key)) {
317 startFragment(this, "com.android.settings.ChooseLockGeneric$ChooseLockGenericFragment",
318 SET_OR_CHANGE_LOCK_METHOD_REQUEST, null);
319 } else if (KEY_LOCK_ENABLED.equals(key)) {
320 lockPatternUtils.setLockPatternEnabled(isToggled(preference));
321 } else if (KEY_VISIBLE_PATTERN.equals(key)) {
322 lockPatternUtils.setVisiblePatternEnabled(isToggled(preference));
323 } else if (KEY_TACTILE_FEEDBACK_ENABLED.equals(key)) {
324 lockPatternUtils.setTactileFeedbackEnabled(isToggled(preference));
325 } else if (preference == mShowPassword) {
326 Settings.System.putInt(getContentResolver(), Settings.System.TEXT_SHOW_PASSWORD,
327 mShowPassword.isChecked() ? 1 : 0);
328 } else if (preference == mToggleAppInstallation) {
329 if (mToggleAppInstallation.isChecked()) {
330 mToggleAppInstallation.setChecked(false);
331 warnAppInstallation();
333 setNonMarketAppsAllowed(false);
336 // If we didn't handle it, let preferences handle it.
337 return super.onPreferenceTreeClick(preferenceScreen, preference);
343 private boolean isToggled(Preference pref) {
344 return ((CheckBoxPreference) pref).isChecked();
348 * see confirmPatternThenDisableAndClear
351 public void onActivityResult(int requestCode, int resultCode, Intent data) {
352 super.onActivityResult(requestCode, resultCode, data);
353 createPreferenceHierarchy();
356 public boolean onPreferenceChange(Preference preference, Object value) {
357 if (preference == mLockAfter) {
358 int timeout = Integer.parseInt((String) value);
360 Settings.Secure.putInt(getContentResolver(),
361 Settings.Secure.LOCK_SCREEN_LOCK_AFTER_TIMEOUT, timeout);
362 } catch (NumberFormatException e) {
363 Log.e("SecuritySettings", "could not persist lockAfter timeout setting", e);
365 updateLockAfterPreferenceSummary();