blob: 75fef24138069f3ba6ed37dea276bc5acd2872e7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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.internal.widget;
18
Amith Yamasani4b4b9542012-09-14 13:36:29 -070019import android.app.ActivityManagerNative;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080020import android.app.admin.DevicePolicyManager;
Jim Miller51117262012-11-04 17:58:09 -080021import android.appwidget.AppWidgetManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ContentResolver;
Jim Miller31f90b62010-01-20 13:35:20 -080023import android.content.Context;
Steven Ross329979c2011-09-28 11:42:56 -040024import android.content.Intent;
Danielle Millett58396982011-09-30 13:55:07 -040025import android.content.pm.PackageManager;
Amith Yamasani52c489c2012-03-28 11:42:42 -070026import android.os.Binder;
Adam Cohenf7522022012-10-03 20:03:18 -070027import android.os.Bundle;
Jason parksf7b3cd42011-01-27 09:28:25 -060028import android.os.IBinder;
Jim Miller69ac9882010-02-24 15:35:05 -080029import android.os.RemoteException;
30import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070032import android.os.UserHandle;
Jason parksf7b3cd42011-01-27 09:28:25 -060033import android.os.storage.IMountService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.provider.Settings;
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -070035import android.security.KeyStore;
Jim Miller69ac9882010-02-24 15:35:05 -080036import android.telephony.TelephonyManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.text.TextUtils;
38import android.util.Log;
Jim Miller51117262012-11-04 17:58:09 -080039import android.view.IWindowManager;
John Wang0f7b3f82011-05-31 11:20:55 -070040import android.view.View;
Jim Miller69ac9882010-02-24 15:35:05 -080041import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Michael Jurka1254f2f2012-10-25 11:44:31 -070043import com.android.internal.R;
44import com.android.internal.telephony.ITelephony;
45import com.google.android.collect.Lists;
Michael Jurkaaa2859a2012-10-24 12:46:49 -070046
Brian Carlstrom929a1c22011-02-01 21:54:09 -080047import java.security.MessageDigest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.security.NoSuchAlgorithmException;
Jim Miller11b019d2010-01-20 16:34:45 -080049import java.security.SecureRandom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import java.util.List;
51
52/**
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -070053 * Utilities for the lock pattern and its settings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 */
55public class LockPatternUtils {
56
Jim Millercb3521e2011-10-03 20:42:26 -070057 private static final String OPTION_ENABLE_FACELOCK = "enable_facelock";
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 private static final String TAG = "LockPatternUtils";
Jim Miller69aa4a92009-12-22 19:03:28 -080060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 /**
62 * The maximum number of incorrect attempts before the user is prevented
63 * from trying again for {@link #FAILED_ATTEMPT_TIMEOUT_MS}.
64 */
65 public static final int FAILED_ATTEMPTS_BEFORE_TIMEOUT = 5;
66
67 /**
68 * The number of incorrect attempts before which we fall back on an alternative
69 * method of verifying the user, and resetting their lock pattern.
70 */
71 public static final int FAILED_ATTEMPTS_BEFORE_RESET = 20;
72
73 /**
74 * How long the user is prevented from trying again after entering the
75 * wrong pattern too many times.
76 */
77 public static final long FAILED_ATTEMPT_TIMEOUT_MS = 30000L;
78
79 /**
80 * The interval of the countdown for showing progress of the lockout.
81 */
82 public static final long FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS = 1000L;
83
Jim Miller4f369952011-08-19 18:29:22 -070084
85 /**
86 * This dictates when we start telling the user that continued failed attempts will wipe
87 * their device.
88 */
89 public static final int FAILED_ATTEMPTS_BEFORE_WIPE_GRACE = 5;
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 /**
92 * The minimum number of dots in a valid pattern.
93 */
94 public static final int MIN_LOCK_PATTERN_SIZE = 4;
95
96 /**
97 * The minimum number of dots the user must include in a wrong pattern
98 * attempt for it to be counted against the counts that affect
99 * {@link #FAILED_ATTEMPTS_BEFORE_TIMEOUT} and {@link #FAILED_ATTEMPTS_BEFORE_RESET}
100 */
Jim Miller4f369952011-08-19 18:29:22 -0700101 public static final int MIN_PATTERN_REGISTER_FAIL = MIN_LOCK_PATTERN_SIZE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Danielle Millett925a7d82012-03-19 18:02:20 -0400103 /**
Adam Cohenf7522022012-10-03 20:03:18 -0700104 * Tells the keyguard to show the user switcher when the keyguard is created.
105 */
106 public static final String KEYGUARD_SHOW_USER_SWITCHER = "showuserswitcher";
107
108 /**
109 * Tells the keyguard to show the security challenge when the keyguard is created.
110 */
111 public static final String KEYGUARD_SHOW_SECURITY_CHALLENGE = "showsecuritychallenge";
112
113 /**
Michael Jurka76017ca2012-11-06 16:21:09 -0800114 * Tells the keyguard to show the widget with the specified id when the keyguard is created.
115 */
116 public static final String KEYGUARD_SHOW_APPWIDGET = "showappwidget";
117
118 /**
Adam Cohenf7522022012-10-03 20:03:18 -0700119 * Options used to lock the device upon user switch.
120 */
121 public static final Bundle USER_SWITCH_LOCK_OPTIONS = new Bundle();
122
123 static {
124 USER_SWITCH_LOCK_OPTIONS.putBoolean(KEYGUARD_SHOW_USER_SWITCHER, true);
125 USER_SWITCH_LOCK_OPTIONS.putBoolean(KEYGUARD_SHOW_SECURITY_CHALLENGE, true);
126 }
127
128 /**
Danielle Millett925a7d82012-03-19 18:02:20 -0400129 * The bit in LOCK_BIOMETRIC_WEAK_FLAGS to be used to indicate whether liveliness should
130 * be used
131 */
132 public static final int FLAG_BIOMETRIC_WEAK_LIVELINESS = 0x1;
133
Michael Jurka1254f2f2012-10-25 11:44:31 -0700134 /**
135 * Pseudo-appwidget id we use to represent the default clock status widget
136 */
137 public static final int ID_DEFAULT_STATUS_WIDGET = -2;
138
Amith Yamasani52c489c2012-03-28 11:42:42 -0700139 protected final static String LOCKOUT_PERMANENT_KEY = "lockscreen.lockedoutpermanently";
140 protected final static String LOCKOUT_ATTEMPT_DEADLINE = "lockscreen.lockoutattemptdeadline";
141 protected final static String PATTERN_EVER_CHOSEN_KEY = "lockscreen.patterneverchosen";
Jim Miller69aa4a92009-12-22 19:03:28 -0800142 public final static String PASSWORD_TYPE_KEY = "lockscreen.password_type";
Jim Miller6edf2632011-09-05 16:03:14 -0700143 public static final String PASSWORD_TYPE_ALTERNATE_KEY = "lockscreen.password_type_alternate";
Amith Yamasani52c489c2012-03-28 11:42:42 -0700144 protected final static String LOCK_PASSWORD_SALT_KEY = "lockscreen.password_salt";
145 protected final static String DISABLE_LOCKSCREEN_KEY = "lockscreen.disabled";
146 protected final static String LOCKSCREEN_OPTIONS = "lockscreen.options";
Jim Miller6edf2632011-09-05 16:03:14 -0700147 public final static String LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK
148 = "lockscreen.biometric_weak_fallback";
Danielle Millett7a072192011-10-03 17:36:01 -0400149 public final static String BIOMETRIC_WEAK_EVER_CHOSEN_KEY
150 = "lockscreen.biometricweakeverchosen";
Jim Millera4edd152012-01-06 18:24:04 -0800151 public final static String LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS
152 = "lockscreen.power_button_instantly_locks";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
Amith Yamasani52c489c2012-03-28 11:42:42 -0700154 protected final static String PASSWORD_HISTORY_KEY = "lockscreen.passwordhistory";
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700155
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800156 private final Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 private final ContentResolver mContentResolver;
Jim Miller31f90b62010-01-20 13:35:20 -0800158 private DevicePolicyManager mDevicePolicyManager;
Amith Yamasani52c489c2012-03-28 11:42:42 -0700159 private ILockSettings mLockSettingsService;
Jim Milleree82f8f2012-10-01 16:26:18 -0700160
161 // The current user is set by KeyguardViewMediator and shared by all LockPatternUtils.
162 private static volatile int sCurrentUserId = UserHandle.USER_NULL;
Dianne Hackbornde4c26f2011-07-17 13:42:47 -0700163
Jim Millercd709882010-03-25 18:24:02 -0700164 public DevicePolicyManager getDevicePolicyManager() {
Jim Miller5b0fb3a2010-02-23 13:46:35 -0800165 if (mDevicePolicyManager == null) {
166 mDevicePolicyManager =
167 (DevicePolicyManager)mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
168 if (mDevicePolicyManager == null) {
169 Log.e(TAG, "Can't get DevicePolicyManagerService: is it running?",
170 new IllegalStateException("Stack trace:"));
171 }
172 }
173 return mDevicePolicyManager;
174 }
Amith Yamasani52c489c2012-03-28 11:42:42 -0700175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 /**
177 * @param contentResolver Used to look up and save settings.
178 */
Jim Miller31f90b62010-01-20 13:35:20 -0800179 public LockPatternUtils(Context context) {
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800180 mContext = context;
Jim Miller31f90b62010-01-20 13:35:20 -0800181 mContentResolver = context.getContentResolver();
Amith Yamasani52c489c2012-03-28 11:42:42 -0700182 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800183
Amith Yamasani52c489c2012-03-28 11:42:42 -0700184 private ILockSettings getLockSettings() {
185 if (mLockSettingsService == null) {
186 mLockSettingsService = ILockSettings.Stub.asInterface(
187 (IBinder) ServiceManager.getService("lock_settings"));
Brad Fitzpatrick90881002010-08-23 18:30:08 -0700188 }
Amith Yamasani52c489c2012-03-28 11:42:42 -0700189 return mLockSettingsService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
191
Jim Miller31f90b62010-01-20 13:35:20 -0800192 public int getRequestedMinimumPasswordLength() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700193 return getDevicePolicyManager().getPasswordMinimumLength(null, getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800194 }
195
Jim Miller31f90b62010-01-20 13:35:20 -0800196 /**
197 * Gets the device policy password mode. If the mode is non-specific, returns
198 * MODE_PATTERN which allows the user to choose anything.
Jim Miller31f90b62010-01-20 13:35:20 -0800199 */
Jim Millercd709882010-03-25 18:24:02 -0700200 public int getRequestedPasswordQuality() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700201 return getDevicePolicyManager().getPasswordQuality(null, getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800202 }
203
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700204 public int getRequestedPasswordHistoryLength() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700205 return getDevicePolicyManager().getPasswordHistoryLength(null, getCurrentOrCallingUserId());
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700206 }
207
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700208 public int getRequestedPasswordMinimumLetters() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700209 return getDevicePolicyManager().getPasswordMinimumLetters(null,
210 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700211 }
212
213 public int getRequestedPasswordMinimumUpperCase() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700214 return getDevicePolicyManager().getPasswordMinimumUpperCase(null,
215 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700216 }
217
218 public int getRequestedPasswordMinimumLowerCase() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700219 return getDevicePolicyManager().getPasswordMinimumLowerCase(null,
220 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700221 }
222
223 public int getRequestedPasswordMinimumNumeric() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700224 return getDevicePolicyManager().getPasswordMinimumNumeric(null,
225 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700226 }
227
228 public int getRequestedPasswordMinimumSymbols() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700229 return getDevicePolicyManager().getPasswordMinimumSymbols(null,
230 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700231 }
232
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700233 public int getRequestedPasswordMinimumNonLetter() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700234 return getDevicePolicyManager().getPasswordMinimumNonLetter(null,
235 getCurrentOrCallingUserId());
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700236 }
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700237
Jim Miller31f90b62010-01-20 13:35:20 -0800238 /**
239 * Returns the actual password mode, as set by keyguard after updating the password.
240 *
241 * @return
242 */
Jim Miller31f90b62010-01-20 13:35:20 -0800243 public void reportFailedPasswordAttempt() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700244 getDevicePolicyManager().reportFailedPasswordAttempt(getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800245 }
246
247 public void reportSuccessfulPasswordAttempt() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700248 getDevicePolicyManager().reportSuccessfulPasswordAttempt(getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800249 }
250
Amith Yamasani52c489c2012-03-28 11:42:42 -0700251 public void setCurrentUser(int userId) {
Jim Milleree82f8f2012-10-01 16:26:18 -0700252 sCurrentUserId = userId;
Amith Yamasani52c489c2012-03-28 11:42:42 -0700253 }
254
Amith Yamasanif882f1a2012-04-10 15:13:39 -0700255 public int getCurrentUser() {
Jim Milleree82f8f2012-10-01 16:26:18 -0700256 if (sCurrentUserId != UserHandle.USER_NULL) {
Jim Miller25645d82012-09-21 14:47:54 -0700257 // Someone is regularly updating using setCurrentUser() use that value.
Jim Milleree82f8f2012-10-01 16:26:18 -0700258 return sCurrentUserId;
Jim Miller25645d82012-09-21 14:47:54 -0700259 }
260 try {
261 return ActivityManagerNative.getDefault().getCurrentUser().id;
262 } catch (RemoteException re) {
263 return UserHandle.USER_OWNER;
Amith Yamasanif882f1a2012-04-10 15:13:39 -0700264 }
265 }
266
Amith Yamasani52c489c2012-03-28 11:42:42 -0700267 public void removeUser(int userId) {
Jim Miller25645d82012-09-21 14:47:54 -0700268 try {
269 getLockSettings().removeUser(userId);
270 } catch (RemoteException re) {
271 Log.e(TAG, "Couldn't remove lock settings for user " + userId);
Amith Yamasani52c489c2012-03-28 11:42:42 -0700272 }
273 }
274
275 private int getCurrentOrCallingUserId() {
276 int callingUid = Binder.getCallingUid();
277 if (callingUid == android.os.Process.SYSTEM_UID) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700278 // TODO: This is a little inefficient. See if all users of this are able to
279 // handle USER_CURRENT and pass that instead.
280 return getCurrentUser();
Amith Yamasani52c489c2012-03-28 11:42:42 -0700281 } else {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700282 return UserHandle.getUserId(callingUid);
Amith Yamasani52c489c2012-03-28 11:42:42 -0700283 }
284 }
285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 /**
287 * Check to see if a pattern matches the saved pattern. If no pattern exists,
288 * always returns true.
289 * @param pattern The pattern to check.
Jim Miller69aa4a92009-12-22 19:03:28 -0800290 * @return Whether the pattern matches the stored one.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 */
292 public boolean checkPattern(List<LockPatternView.Cell> pattern) {
Kenny Rootdf8bfe02012-09-21 15:00:25 -0700293 final int userId = getCurrentOrCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 try {
Kenny Rootdf8bfe02012-09-21 15:00:25 -0700295 final boolean matched = getLockSettings().checkPattern(patternToHash(pattern), userId);
296 if (matched && (userId == UserHandle.USER_OWNER)) {
297 KeyStore.getInstance().password(patternToString(pattern));
298 }
299 return matched;
Amith Yamasani52c489c2012-03-28 11:42:42 -0700300 } catch (RemoteException re) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 return true;
302 }
303 }
304
305 /**
Jim Miller69aa4a92009-12-22 19:03:28 -0800306 * Check to see if a password matches the saved password. If no password exists,
307 * always returns true.
308 * @param password The password to check.
309 * @return Whether the password matches the stored one.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 */
Jim Miller69aa4a92009-12-22 19:03:28 -0800311 public boolean checkPassword(String password) {
Kenny Rootdf8bfe02012-09-21 15:00:25 -0700312 final int userId = getCurrentOrCallingUserId();
Jim Miller69aa4a92009-12-22 19:03:28 -0800313 try {
Kenny Rootdf8bfe02012-09-21 15:00:25 -0700314 final boolean matched = getLockSettings().checkPassword(passwordToHash(password),
315 userId);
316 if (matched && (userId == UserHandle.USER_OWNER)) {
317 KeyStore.getInstance().password(password);
318 }
319 return matched;
Amith Yamasani52c489c2012-03-28 11:42:42 -0700320 } catch (RemoteException re) {
Jim Miller69aa4a92009-12-22 19:03:28 -0800321 return true;
322 }
323 }
324
325 /**
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700326 * Check to see if a password matches any of the passwords stored in the
327 * password history.
328 *
329 * @param password The password to check.
330 * @return Whether the password matches any in the history.
331 */
332 public boolean checkPasswordHistory(String password) {
333 String passwordHashString = new String(passwordToHash(password));
334 String passwordHistory = getString(PASSWORD_HISTORY_KEY);
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700335 if (passwordHistory == null) {
336 return false;
337 }
338 // Password History may be too long...
339 int passwordHashLength = passwordHashString.length();
340 int passwordHistoryLength = getRequestedPasswordHistoryLength();
341 if(passwordHistoryLength == 0) {
342 return false;
343 }
344 int neededPasswordHistoryLength = passwordHashLength * passwordHistoryLength
345 + passwordHistoryLength - 1;
346 if (passwordHistory.length() > neededPasswordHistoryLength) {
347 passwordHistory = passwordHistory.substring(0, neededPasswordHistoryLength);
348 }
349 return passwordHistory.contains(passwordHashString);
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700350 }
351
352 /**
Jim Miller69aa4a92009-12-22 19:03:28 -0800353 * Check to see if the user has stored a lock pattern.
354 * @return Whether a saved pattern exists.
355 */
356 public boolean savedPatternExists() {
Amith Yamasani52c489c2012-03-28 11:42:42 -0700357 try {
358 return getLockSettings().havePattern(getCurrentOrCallingUserId());
359 } catch (RemoteException re) {
360 return false;
361 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800362 }
363
364 /**
365 * Check to see if the user has stored a lock pattern.
366 * @return Whether a saved pattern exists.
367 */
368 public boolean savedPasswordExists() {
Amith Yamasani52c489c2012-03-28 11:42:42 -0700369 try {
370 return getLockSettings().havePassword(getCurrentOrCallingUserId());
371 } catch (RemoteException re) {
372 return false;
373 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800374 }
375
376 /**
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700377 * Return true if the user has ever chosen a pattern. This is true even if the pattern is
378 * currently cleared.
379 *
380 * @return True if the user has ever chosen a pattern.
381 */
382 public boolean isPatternEverChosen() {
Jim Millera4edd152012-01-06 18:24:04 -0800383 return getBoolean(PATTERN_EVER_CHOSEN_KEY, false);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700384 }
385
386 /**
Danielle Millett7a072192011-10-03 17:36:01 -0400387 * Return true if the user has ever chosen biometric weak. This is true even if biometric
388 * weak is not current set.
389 *
390 * @return True if the user has ever chosen biometric weak.
391 */
392 public boolean isBiometricWeakEverChosen() {
Jim Millera4edd152012-01-06 18:24:04 -0800393 return getBoolean(BIOMETRIC_WEAK_EVER_CHOSEN_KEY, false);
Danielle Millett7a072192011-10-03 17:36:01 -0400394 }
395
396 /**
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700397 * Used by device policy manager to validate the current password
398 * information it has.
399 */
400 public int getActivePasswordQuality() {
Jim Millercd709882010-03-25 18:24:02 -0700401 int activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
Danielle Millettc8fb5322011-10-04 12:18:51 -0400402 // Note we don't want to use getKeyguardStoredPasswordQuality() because we want this to
403 // return biometric_weak if that is being used instead of the backup
404 int quality =
405 (int) getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
406 switch (quality) {
Jim Millercd709882010-03-25 18:24:02 -0700407 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700408 if (isLockPatternEnabled()) {
Jim Millercd709882010-03-25 18:24:02 -0700409 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700410 }
Jim Millercd709882010-03-25 18:24:02 -0700411 break;
Danielle Millettc8fb5322011-10-04 12:18:51 -0400412 case DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK:
413 if (isBiometricWeakInstalled()) {
414 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK;
415 }
416 break;
Jim Millercd709882010-03-25 18:24:02 -0700417 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700418 if (isLockPasswordEnabled()) {
Jim Millercd709882010-03-25 18:24:02 -0700419 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700420 }
Jim Millercd709882010-03-25 18:24:02 -0700421 break;
422 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700423 if (isLockPasswordEnabled()) {
Jim Millercd709882010-03-25 18:24:02 -0700424 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700425 }
Jim Millercd709882010-03-25 18:24:02 -0700426 break;
427 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
428 if (isLockPasswordEnabled()) {
429 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC;
430 }
431 break;
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700432 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
433 if (isLockPasswordEnabled()) {
434 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
435 }
436 break;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700437 }
Danielle Millettc8fb5322011-10-04 12:18:51 -0400438
Jim Millercd709882010-03-25 18:24:02 -0700439 return activePasswordQuality;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700440 }
Jim Millercd709882010-03-25 18:24:02 -0700441
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700442 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800443 * Clear any lock pattern or password.
444 */
Steven Ross329979c2011-09-28 11:42:56 -0400445 public void clearLock(boolean isFallback) {
446 if(!isFallback) deleteGallery();
Jim Millercd709882010-03-25 18:24:02 -0700447 saveLockPassword(null, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800448 setLockPatternEnabled(false);
449 saveLockPattern(null);
Jim Miller93708af12012-01-25 18:26:12 -0800450 setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
Jim Miller6edf2632011-09-05 16:03:14 -0700451 setLong(PASSWORD_TYPE_ALTERNATE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800452 }
Jim Miller5b0fb3a2010-02-23 13:46:35 -0800453
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800454 /**
Jim Miller2a98a4c2010-11-19 18:49:26 -0800455 * Disable showing lock screen at all when the DevicePolicyManager allows it.
456 * This is only meaningful if pattern, pin or password are not set.
457 *
458 * @param disable Disables lock screen when true
459 */
460 public void setLockScreenDisabled(boolean disable) {
461 setLong(DISABLE_LOCKSCREEN_KEY, disable ? 1 : 0);
462 }
463
464 /**
465 * Determine if LockScreen can be disabled. This is used, for example, to tell if we should
466 * show LockScreen or go straight to the home screen.
467 *
468 * @return true if lock screen is can be disabled
469 */
470 public boolean isLockScreenDisabled() {
471 return !isSecure() && getLong(DISABLE_LOCKSCREEN_KEY, 0) != 0;
472 }
473
474 /**
Steven Ross3553c292011-09-30 15:48:40 -0400475 * Calls back SetupFaceLock to delete the temporary gallery file
Steven Ross329979c2011-09-28 11:42:56 -0400476 */
477 public void deleteTempGallery() {
Danielle Millett574e49e2012-03-27 16:17:10 -0400478 Intent intent = new Intent().setAction("com.android.facelock.DELETE_GALLERY");
Steven Ross3553c292011-09-30 15:48:40 -0400479 intent.putExtra("deleteTempGallery", true);
Danielle Millett574e49e2012-03-27 16:17:10 -0400480 mContext.sendBroadcast(intent);
Steven Ross329979c2011-09-28 11:42:56 -0400481 }
482
483 /**
484 * Calls back SetupFaceLock to delete the gallery file when the lock type is changed
485 */
486 void deleteGallery() {
Danielle Millett58396982011-09-30 13:55:07 -0400487 if(usingBiometricWeak()) {
Danielle Millett574e49e2012-03-27 16:17:10 -0400488 Intent intent = new Intent().setAction("com.android.facelock.DELETE_GALLERY");
Steven Ross329979c2011-09-28 11:42:56 -0400489 intent.putExtra("deleteGallery", true);
Danielle Millett574e49e2012-03-27 16:17:10 -0400490 mContext.sendBroadcast(intent);
Steven Ross329979c2011-09-28 11:42:56 -0400491 }
492 }
493
494 /**
Jim Miller6edf2632011-09-05 16:03:14 -0700495 * Save a lock pattern.
496 * @param pattern The new pattern to save.
Danielle Millett2364a222011-12-21 17:02:32 -0500497 */
498 public void saveLockPattern(List<LockPatternView.Cell> pattern) {
499 this.saveLockPattern(pattern, false);
500 }
501
502 /**
503 * Save a lock pattern.
504 * @param pattern The new pattern to save.
Jim Miller6edf2632011-09-05 16:03:14 -0700505 * @param isFallback Specifies if this is a fallback to biometric weak
506 */
507 public void saveLockPattern(List<LockPatternView.Cell> pattern, boolean isFallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 // Compute the hash
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700509 final byte[] hash = LockPatternUtils.patternToHash(pattern);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 try {
Amith Yamasani52c489c2012-03-28 11:42:42 -0700511 getLockSettings().setLockPattern(hash, getCurrentOrCallingUserId());
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800512 DevicePolicyManager dpm = getDevicePolicyManager();
Brian Carlstrome2afc242011-06-02 16:21:55 -0700513 KeyStore keyStore = KeyStore.getInstance();
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800514 if (pattern != null) {
Brian Carlstrome2afc242011-06-02 16:21:55 -0700515 keyStore.password(patternToString(pattern));
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800516 setBoolean(PATTERN_EVER_CHOSEN_KEY, true);
Jim Miller6edf2632011-09-05 16:03:14 -0700517 if (!isFallback) {
Steven Ross329979c2011-09-28 11:42:56 -0400518 deleteGallery();
Jim Miller6edf2632011-09-05 16:03:14 -0700519 setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
Danielle Millett2364a222011-12-21 17:02:32 -0500520 dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700521 pattern.size(), 0, 0, 0, 0, 0, 0, getCurrentOrCallingUserId());
Jim Miller6edf2632011-09-05 16:03:14 -0700522 } else {
523 setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK);
524 setLong(PASSWORD_TYPE_ALTERNATE_KEY,
525 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
Danielle Millett044a0a72011-11-07 15:42:12 -0500526 finishBiometricWeak();
Danielle Millett2364a222011-12-21 17:02:32 -0500527 dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700528 0, 0, 0, 0, 0, 0, 0, getCurrentOrCallingUserId());
Jim Miller6edf2632011-09-05 16:03:14 -0700529 }
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800530 } else {
Brian Carlstrome2afc242011-06-02 16:21:55 -0700531 if (keyStore.isEmpty()) {
532 keyStore.reset();
533 }
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700534 dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, 0, 0,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700535 0, 0, 0, 0, 0, getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800536 }
Amith Yamasani52c489c2012-03-28 11:42:42 -0700537 } catch (RemoteException re) {
538 Log.e(TAG, "Couldn't save lock pattern " + re);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540 }
541
542 /**
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700543 * Compute the password quality from the given password string.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800544 */
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700545 static public int computePasswordQuality(String password) {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800546 boolean hasDigit = false;
547 boolean hasNonDigit = false;
548 final int len = password.length();
549 for (int i = 0; i < len; i++) {
550 if (Character.isDigit(password.charAt(i))) {
551 hasDigit = true;
552 } else {
553 hasNonDigit = true;
554 }
555 }
Jim Miller5b0fb3a2010-02-23 13:46:35 -0800556
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700557 if (hasNonDigit && hasDigit) {
558 return DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC;
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800559 }
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800560 if (hasNonDigit) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700561 return DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800562 }
563 if (hasDigit) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700564 return DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800565 }
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700566 return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800567 }
Jim Miller5b0fb3a2010-02-23 13:46:35 -0800568
Jason parksf7b3cd42011-01-27 09:28:25 -0600569 /** Update the encryption password if it is enabled **/
570 private void updateEncryptionPassword(String password) {
571 DevicePolicyManager dpm = getDevicePolicyManager();
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700572 if (dpm.getStorageEncryptionStatus(getCurrentOrCallingUserId())
573 != DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE) {
Jason parksf7b3cd42011-01-27 09:28:25 -0600574 return;
575 }
576
577 IBinder service = ServiceManager.getService("mount");
578 if (service == null) {
579 Log.e(TAG, "Could not find the mount service to update the encryption password");
580 return;
581 }
582
583 IMountService mountService = IMountService.Stub.asInterface(service);
584 try {
585 mountService.changeEncryptionPassword(password);
586 } catch (RemoteException e) {
587 Log.e(TAG, "Error changing encryption password", e);
588 }
589 }
590
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800591 /**
Jim Millercd709882010-03-25 18:24:02 -0700592 * Save a lock password. Does not ensure that the password is as good
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800593 * as the requested mode, but will adjust the mode to be as good as the
594 * pattern.
Jim Miller69aa4a92009-12-22 19:03:28 -0800595 * @param password The password to save
Jim Millercd709882010-03-25 18:24:02 -0700596 * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
Jim Miller69aa4a92009-12-22 19:03:28 -0800597 */
Jim Millercd709882010-03-25 18:24:02 -0700598 public void saveLockPassword(String password, int quality) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700599 this.saveLockPassword(password, quality, false, getCurrentOrCallingUserId());
Jim Miller6edf2632011-09-05 16:03:14 -0700600 }
601
602 /**
603 * Save a lock password. Does not ensure that the password is as good
604 * as the requested mode, but will adjust the mode to be as good as the
605 * pattern.
606 * @param password The password to save
607 * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
608 * @param isFallback Specifies if this is a fallback to biometric weak
609 */
610 public void saveLockPassword(String password, int quality, boolean isFallback) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700611 saveLockPassword(password, quality, isFallback, getCurrentOrCallingUserId());
612 }
613
614 /**
615 * Save a lock password. Does not ensure that the password is as good
616 * as the requested mode, but will adjust the mode to be as good as the
617 * pattern.
618 * @param password The password to save
619 * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
620 * @param isFallback Specifies if this is a fallback to biometric weak
621 * @param userHandle The userId of the user to change the password for
622 */
623 public void saveLockPassword(String password, int quality, boolean isFallback, int userHandle) {
Jim Miller69aa4a92009-12-22 19:03:28 -0800624 // Compute the hash
Jim Miller11b019d2010-01-20 16:34:45 -0800625 final byte[] hash = passwordToHash(password);
Jim Miller69aa4a92009-12-22 19:03:28 -0800626 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700627 getLockSettings().setLockPassword(hash, userHandle);
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800628 DevicePolicyManager dpm = getDevicePolicyManager();
Brian Carlstrome2afc242011-06-02 16:21:55 -0700629 KeyStore keyStore = KeyStore.getInstance();
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800630 if (password != null) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700631 if (userHandle == UserHandle.USER_OWNER) {
632 // Update the encryption password.
633 updateEncryptionPassword(password);
Jason parksf7b3cd42011-01-27 09:28:25 -0600634
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700635 // Update the keystore password
636 keyStore.password(password);
637 }
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700638
Jim Millercd709882010-03-25 18:24:02 -0700639 int computedQuality = computePasswordQuality(password);
Jim Miller6edf2632011-09-05 16:03:14 -0700640 if (!isFallback) {
Steven Ross329979c2011-09-28 11:42:56 -0400641 deleteGallery();
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700642 setLong(PASSWORD_TYPE_KEY, Math.max(quality, computedQuality), userHandle);
Danielle Millett2364a222011-12-21 17:02:32 -0500643 if (computedQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
644 int letters = 0;
645 int uppercase = 0;
646 int lowercase = 0;
647 int numbers = 0;
648 int symbols = 0;
649 int nonletter = 0;
650 for (int i = 0; i < password.length(); i++) {
651 char c = password.charAt(i);
652 if (c >= 'A' && c <= 'Z') {
653 letters++;
654 uppercase++;
655 } else if (c >= 'a' && c <= 'z') {
656 letters++;
657 lowercase++;
658 } else if (c >= '0' && c <= '9') {
659 numbers++;
660 nonletter++;
661 } else {
662 symbols++;
663 nonletter++;
664 }
665 }
666 dpm.setActivePasswordState(Math.max(quality, computedQuality),
667 password.length(), letters, uppercase, lowercase,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700668 numbers, symbols, nonletter, userHandle);
Danielle Millett2364a222011-12-21 17:02:32 -0500669 } else {
670 // The password is not anything.
671 dpm.setActivePasswordState(
672 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700673 0, 0, 0, 0, 0, 0, 0, userHandle);
Danielle Millett2364a222011-12-21 17:02:32 -0500674 }
Jim Miller6edf2632011-09-05 16:03:14 -0700675 } else {
Danielle Millett2364a222011-12-21 17:02:32 -0500676 // Case where it's a fallback for biometric weak
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700677 setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK,
678 userHandle);
679 setLong(PASSWORD_TYPE_ALTERNATE_KEY, Math.max(quality, computedQuality),
680 userHandle);
Danielle Millett044a0a72011-11-07 15:42:12 -0500681 finishBiometricWeak();
Danielle Millett2364a222011-12-21 17:02:32 -0500682 dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700683 0, 0, 0, 0, 0, 0, 0, userHandle);
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700684 }
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700685 // Add the password to the password history. We assume all
686 // password
687 // hashes have the same length for simplicity of implementation.
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700688 String passwordHistory = getString(PASSWORD_HISTORY_KEY, userHandle);
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700689 if (passwordHistory == null) {
690 passwordHistory = new String();
691 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700692 int passwordHistoryLength = getRequestedPasswordHistoryLength();
693 if (passwordHistoryLength == 0) {
694 passwordHistory = "";
695 } else {
696 passwordHistory = new String(hash) + "," + passwordHistory;
697 // Cut it to contain passwordHistoryLength hashes
698 // and passwordHistoryLength -1 commas.
699 passwordHistory = passwordHistory.substring(0, Math.min(hash.length
700 * passwordHistoryLength + passwordHistoryLength - 1, passwordHistory
701 .length()));
702 }
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700703 setString(PASSWORD_HISTORY_KEY, passwordHistory, userHandle);
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800704 } else {
Brian Carlstrome2afc242011-06-02 16:21:55 -0700705 // Conditionally reset the keystore if empty. If
706 // non-empty, we are just switching key guard type
707 if (keyStore.isEmpty()) {
708 keyStore.reset();
709 }
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800710 dpm.setActivePasswordState(
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700711 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, 0, 0, 0, 0, 0, 0, 0,
712 userHandle);
Jim Miller31f90b62010-01-20 13:35:20 -0800713 }
Amith Yamasani52c489c2012-03-28 11:42:42 -0700714 } catch (RemoteException re) {
Jim Miller69aa4a92009-12-22 19:03:28 -0800715 // Cant do much
Amith Yamasani52c489c2012-03-28 11:42:42 -0700716 Log.e(TAG, "Unable to save lock password " + re);
Jim Miller69aa4a92009-12-22 19:03:28 -0800717 }
718 }
719
Jim Millercd709882010-03-25 18:24:02 -0700720 /**
721 * Retrieves the quality mode we're in.
722 * {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
723 *
724 * @return stored password quality
725 */
726 public int getKeyguardStoredPasswordQuality() {
Jim Miller6edf2632011-09-05 16:03:14 -0700727 int quality =
728 (int) getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
729 // If the user has chosen to use weak biometric sensor, then return the backup locking
730 // method and treat biometric as a special case.
731 if (quality == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) {
732 quality =
733 (int) getLong(PASSWORD_TYPE_ALTERNATE_KEY,
734 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
735 }
736 return quality;
737 }
738
Danielle Millett58396982011-09-30 13:55:07 -0400739 /**
740 * @return true if the lockscreen method is set to biometric weak
741 */
Jim Miller6edf2632011-09-05 16:03:14 -0700742 public boolean usingBiometricWeak() {
743 int quality =
744 (int) getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
745 return quality == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK;
Jim Miller69aa4a92009-12-22 19:03:28 -0800746 }
747
748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 * Deserialize a pattern.
750 * @param string The pattern serialized with {@link #patternToString}
751 * @return The pattern.
752 */
753 public static List<LockPatternView.Cell> stringToPattern(String string) {
754 List<LockPatternView.Cell> result = Lists.newArrayList();
755
756 final byte[] bytes = string.getBytes();
757 for (int i = 0; i < bytes.length; i++) {
758 byte b = bytes[i];
759 result.add(LockPatternView.Cell.of(b / 3, b % 3));
760 }
761 return result;
762 }
763
764 /**
765 * Serialize a pattern.
766 * @param pattern The pattern.
767 * @return The pattern in string form.
768 */
769 public static String patternToString(List<LockPatternView.Cell> pattern) {
770 if (pattern == null) {
771 return "";
772 }
773 final int patternSize = pattern.size();
774
775 byte[] res = new byte[patternSize];
776 for (int i = 0; i < patternSize; i++) {
777 LockPatternView.Cell cell = pattern.get(i);
778 res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
779 }
780 return new String(res);
781 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 /*
784 * Generate an SHA-1 hash for the pattern. Not the most secure, but it is
785 * at least a second level of protection. First level is that the file
786 * is in a location only readable by the system process.
787 * @param pattern the gesture pattern.
788 * @return the hash of the pattern in a byte array.
789 */
Jim Miller69aa4a92009-12-22 19:03:28 -0800790 private static byte[] patternToHash(List<LockPatternView.Cell> pattern) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 if (pattern == null) {
792 return null;
793 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 final int patternSize = pattern.size();
796 byte[] res = new byte[patternSize];
797 for (int i = 0; i < patternSize; i++) {
798 LockPatternView.Cell cell = pattern.get(i);
799 res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
800 }
801 try {
802 MessageDigest md = MessageDigest.getInstance("SHA-1");
803 byte[] hash = md.digest(res);
804 return hash;
805 } catch (NoSuchAlgorithmException nsa) {
806 return res;
807 }
808 }
809
Jim Miller11b019d2010-01-20 16:34:45 -0800810 private String getSalt() {
811 long salt = getLong(LOCK_PASSWORD_SALT_KEY, 0);
812 if (salt == 0) {
813 try {
814 salt = SecureRandom.getInstance("SHA1PRNG").nextLong();
815 setLong(LOCK_PASSWORD_SALT_KEY, salt);
816 Log.v(TAG, "Initialized lock password salt");
817 } catch (NoSuchAlgorithmException e) {
818 // Throw an exception rather than storing a password we'll never be able to recover
819 throw new IllegalStateException("Couldn't get SecureRandom number", e);
820 }
821 }
822 return Long.toHexString(salt);
823 }
824
Jim Miller69aa4a92009-12-22 19:03:28 -0800825 /*
826 * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash.
827 * Not the most secure, but it is at least a second level of protection. First level is that
828 * the file is in a location only readable by the system process.
829 * @param password the gesture pattern.
830 * @return the hash of the pattern in a byte array.
831 */
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700832 public byte[] passwordToHash(String password) {
Jim Miller69aa4a92009-12-22 19:03:28 -0800833 if (password == null) {
834 return null;
835 }
836 String algo = null;
837 byte[] hashed = null;
838 try {
Jim Miller11b019d2010-01-20 16:34:45 -0800839 byte[] saltedPassword = (password + getSalt()).getBytes();
Jim Miller69aa4a92009-12-22 19:03:28 -0800840 byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword);
841 byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword);
842 hashed = (toHex(sha1) + toHex(md5)).getBytes();
843 } catch (NoSuchAlgorithmException e) {
844 Log.w(TAG, "Failed to encode string because of missing algorithm: " + algo);
845 }
846 return hashed;
847 }
848
849 private static String toHex(byte[] ary) {
850 final String hex = "0123456789ABCDEF";
851 String ret = "";
852 for (int i = 0; i < ary.length; i++) {
853 ret += hex.charAt((ary[i] >> 4) & 0xf);
854 ret += hex.charAt(ary[i] & 0xf);
855 }
856 return ret;
857 }
858
859 /**
Danielle Millett73da5fe2011-09-13 16:20:05 -0400860 * @return Whether the lock password is enabled, or if it is set as a backup for biometric weak
Jim Miller69aa4a92009-12-22 19:03:28 -0800861 */
862 public boolean isLockPasswordEnabled() {
863 long mode = getLong(PASSWORD_TYPE_KEY, 0);
Danielle Millett73da5fe2011-09-13 16:20:05 -0400864 long backupMode = getLong(PASSWORD_TYPE_ALTERNATE_KEY, 0);
865 final boolean passwordEnabled = mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC
866 || mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC
867 || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC
868 || mode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
869 final boolean backupEnabled = backupMode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC
870 || backupMode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC
871 || backupMode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC
872 || backupMode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
873
874 return savedPasswordExists() && (passwordEnabled ||
Danielle Millett58396982011-09-30 13:55:07 -0400875 (usingBiometricWeak() && backupEnabled));
Jim Miller69aa4a92009-12-22 19:03:28 -0800876 }
877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 /**
Danielle Millett73da5fe2011-09-13 16:20:05 -0400879 * @return Whether the lock pattern is enabled, or if it is set as a backup for biometric weak
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 */
881 public boolean isLockPatternEnabled() {
Danielle Millett73da5fe2011-09-13 16:20:05 -0400882 final boolean backupEnabled =
883 getLong(PASSWORD_TYPE_ALTERNATE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING)
884 == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
885
Jim Millera4edd152012-01-06 18:24:04 -0800886 return getBoolean(Settings.Secure.LOCK_PATTERN_ENABLED, false)
Danielle Millett73da5fe2011-09-13 16:20:05 -0400887 && (getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING)
888 == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING ||
Danielle Millett58396982011-09-30 13:55:07 -0400889 (usingBiometricWeak() && backupEnabled));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 }
891
892 /**
Danielle Millett58396982011-09-30 13:55:07 -0400893 * @return Whether biometric weak lock is installed and that the front facing camera exists
Jim Miller6edf2632011-09-05 16:03:14 -0700894 */
Danielle Millett58396982011-09-30 13:55:07 -0400895 public boolean isBiometricWeakInstalled() {
Danielle Millett58396982011-09-30 13:55:07 -0400896 // Check that it's installed
897 PackageManager pm = mContext.getPackageManager();
898 try {
899 pm.getPackageInfo("com.android.facelock", PackageManager.GET_ACTIVITIES);
900 } catch (PackageManager.NameNotFoundException e) {
901 return false;
902 }
903
904 // Check that the camera is enabled
905 if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
906 return false;
907 }
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700908 if (getDevicePolicyManager().getCameraDisabled(null, getCurrentOrCallingUserId())) {
Danielle Millett58396982011-09-30 13:55:07 -0400909 return false;
910 }
911
912
913 return true;
Jim Miller6edf2632011-09-05 16:03:14 -0700914 }
915
916 /**
Danielle Millett925a7d82012-03-19 18:02:20 -0400917 * Set whether biometric weak liveliness is enabled.
918 */
919 public void setBiometricWeakLivelinessEnabled(boolean enabled) {
920 long currentFlag = getLong(Settings.Secure.LOCK_BIOMETRIC_WEAK_FLAGS, 0L);
921 long newFlag;
922 if (enabled) {
923 newFlag = currentFlag | FLAG_BIOMETRIC_WEAK_LIVELINESS;
924 } else {
925 newFlag = currentFlag & ~FLAG_BIOMETRIC_WEAK_LIVELINESS;
926 }
927 setLong(Settings.Secure.LOCK_BIOMETRIC_WEAK_FLAGS, newFlag);
928 }
929
930 /**
931 * @return Whether the biometric weak liveliness is enabled.
932 */
933 public boolean isBiometricWeakLivelinessEnabled() {
934 long currentFlag = getLong(Settings.Secure.LOCK_BIOMETRIC_WEAK_FLAGS, 0L);
935 return ((currentFlag & FLAG_BIOMETRIC_WEAK_LIVELINESS) != 0);
936 }
937
938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 * Set whether the lock pattern is enabled.
940 */
941 public void setLockPatternEnabled(boolean enabled) {
Amith Yamasani156c4352010-03-05 17:10:03 -0800942 setBoolean(Settings.Secure.LOCK_PATTERN_ENABLED, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 }
944
945 /**
946 * @return Whether the visible pattern is enabled.
947 */
948 public boolean isVisiblePatternEnabled() {
Jim Millera4edd152012-01-06 18:24:04 -0800949 return getBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
951
952 /**
953 * Set whether the visible pattern is enabled.
954 */
955 public void setVisiblePatternEnabled(boolean enabled) {
Amith Yamasani156c4352010-03-05 17:10:03 -0800956 setBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
958
959 /**
960 * @return Whether tactile feedback for the pattern is enabled.
961 */
962 public boolean isTactileFeedbackEnabled() {
Jeff Sharkey5ed9d682012-10-10 14:28:27 -0700963 return Settings.System.getIntForUser(mContentResolver,
964 Settings.System.HAPTIC_FEEDBACK_ENABLED, 1, UserHandle.USER_CURRENT) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 }
966
967 /**
968 * Set and store the lockout deadline, meaning the user can't attempt his/her unlock
969 * pattern until the deadline has passed.
970 * @return the chosen deadline.
971 */
972 public long setLockoutAttemptDeadline() {
973 final long deadline = SystemClock.elapsedRealtime() + FAILED_ATTEMPT_TIMEOUT_MS;
974 setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline);
975 return deadline;
976 }
977
978 /**
979 * @return The elapsed time in millis in the future when the user is allowed to
980 * attempt to enter his/her lock pattern, or 0 if the user is welcome to
981 * enter a pattern.
982 */
983 public long getLockoutAttemptDeadline() {
984 final long deadline = getLong(LOCKOUT_ATTEMPT_DEADLINE, 0L);
985 final long now = SystemClock.elapsedRealtime();
986 if (deadline < now || deadline > (now + FAILED_ATTEMPT_TIMEOUT_MS)) {
987 return 0L;
988 }
989 return deadline;
990 }
991
992 /**
993 * @return Whether the user is permanently locked out until they verify their
994 * credentials. Occurs after {@link #FAILED_ATTEMPTS_BEFORE_RESET} failed
995 * attempts.
996 */
997 public boolean isPermanentlyLocked() {
Jim Millera4edd152012-01-06 18:24:04 -0800998 return getBoolean(LOCKOUT_PERMANENT_KEY, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
1000
1001 /**
1002 * Set the state of whether the device is permanently locked, meaning the user
Karl Rosaen678771b2009-08-21 14:00:26 -07001003 * must authenticate via other means.
1004 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 * @param locked Whether the user is permanently locked out until they verify their
1006 * credentials. Occurs after {@link #FAILED_ATTEMPTS_BEFORE_RESET} failed
1007 * attempts.
1008 */
1009 public void setPermanentlyLocked(boolean locked) {
1010 setBoolean(LOCKOUT_PERMANENT_KEY, locked);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 }
1012
John Wang0f7b3f82011-05-31 11:20:55 -07001013 public boolean isEmergencyCallCapable() {
1014 return mContext.getResources().getBoolean(
1015 com.android.internal.R.bool.config_voice_capable);
1016 }
1017
1018 public boolean isPukUnlockScreenEnable() {
1019 return mContext.getResources().getBoolean(
1020 com.android.internal.R.bool.config_enable_puk_unlock_screen);
1021 }
1022
Jim Miller1f56edc2011-11-07 19:00:48 -08001023 public boolean isEmergencyCallEnabledWhileSimLocked() {
1024 return mContext.getResources().getBoolean(
1025 com.android.internal.R.bool.config_enable_emergency_call_while_sim_locked);
1026 }
1027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 /**
1029 * @return A formatted string of the next alarm (for showing on the lock screen),
1030 * or null if there is no next alarm.
1031 */
1032 public String getNextAlarm() {
Amith Yamasani8fd96ec2012-09-21 17:48:49 -07001033 String nextAlarm = Settings.System.getStringForUser(mContentResolver,
1034 Settings.System.NEXT_ALARM_FORMATTED, UserHandle.USER_CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 if (nextAlarm == null || TextUtils.isEmpty(nextAlarm)) {
1036 return null;
1037 }
1038 return nextAlarm;
1039 }
1040
Jim Millera4edd152012-01-06 18:24:04 -08001041 private boolean getBoolean(String secureSettingKey, boolean defaultValue) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001042 try {
1043 return getLockSettings().getBoolean(secureSettingKey, defaultValue,
1044 getCurrentOrCallingUserId());
1045 } catch (RemoteException re) {
1046 return defaultValue;
1047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049
Amith Yamasani156c4352010-03-05 17:10:03 -08001050 private void setBoolean(String secureSettingKey, boolean enabled) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001051 try {
1052 getLockSettings().setBoolean(secureSettingKey, enabled, getCurrentOrCallingUserId());
1053 } catch (RemoteException re) {
1054 // What can we do?
1055 Log.e(TAG, "Couldn't write boolean " + secureSettingKey + re);
1056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001059 public int[] getAppWidgets() {
Amith Yamasani8fd96ec2012-09-21 17:48:49 -07001060 String appWidgetIdString = Settings.Secure.getStringForUser(
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001061 mContentResolver, Settings.Secure.LOCK_SCREEN_APPWIDGET_IDS,
Amith Yamasani8fd96ec2012-09-21 17:48:49 -07001062 UserHandle.USER_CURRENT);
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001063 String delims = ",";
Michael Jurka1254f2f2012-10-25 11:44:31 -07001064 if (appWidgetIdString != null && appWidgetIdString.length() > 0) {
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001065 String[] appWidgetStringIds = appWidgetIdString.split(delims);
1066 int[] appWidgetIds = new int[appWidgetStringIds.length];
1067 for (int i = 0; i < appWidgetStringIds.length; i++) {
1068 String appWidget = appWidgetStringIds[i];
1069 try {
1070 appWidgetIds[i] = Integer.decode(appWidget);
1071 } catch (NumberFormatException e) {
Michael Jurka1254f2f2012-10-25 11:44:31 -07001072 Log.d(TAG, "Error when parsing widget id " + appWidget);
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001073 return null;
1074 }
1075 }
1076 return appWidgetIds;
Jim Millerf229e4d2012-09-12 20:32:50 -07001077 }
Michael Jurka67a871d2012-11-01 18:26:01 -07001078 return new int[0];
Jim Millerf229e4d2012-09-12 20:32:50 -07001079 }
1080
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001081 private static String combineStrings(int[] list, String separator) {
1082 int listLength = list.length;
1083
1084 switch (listLength) {
1085 case 0: {
1086 return "";
1087 }
1088 case 1: {
1089 return Integer.toString(list[0]);
1090 }
Michael Jurka20c41d52012-09-20 19:01:06 -07001091 }
1092
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001093 int strLength = 0;
1094 int separatorLength = separator.length();
1095
1096 String[] stringList = new String[list.length];
1097 for (int i = 0; i < listLength; i++) {
1098 stringList[i] = Integer.toString(list[i]);
1099 strLength += stringList[i].length();
1100 if (i < listLength - 1) {
1101 strLength += separatorLength;
1102 }
1103 }
1104
1105 StringBuilder sb = new StringBuilder(strLength);
1106
1107 for (int i = 0; i < listLength; i++) {
1108 sb.append(list[i]);
1109 if (i < listLength - 1) {
1110 sb.append(separator);
1111 }
1112 }
1113
1114 return sb.toString();
1115 }
1116
Jim Miller51117262012-11-04 17:58:09 -08001117 // appwidget used when appwidgets are disabled (we make an exception for
1118 // default clock widget)
1119 public void writeFallbackAppWidgetId(int appWidgetId) {
1120 Settings.Secure.putIntForUser(mContentResolver,
1121 Settings.Secure.LOCK_SCREEN_FALLBACK_APPWIDGET_ID,
1122 appWidgetId,
1123 UserHandle.USER_CURRENT);
1124 }
1125
1126 // appwidget used when appwidgets are disabled (we make an exception for
1127 // default clock widget)
1128 public int getFallbackAppWidgetId() {
1129 return Settings.Secure.getIntForUser(
1130 mContentResolver,
1131 Settings.Secure.LOCK_SCREEN_FALLBACK_APPWIDGET_ID,
1132 AppWidgetManager.INVALID_APPWIDGET_ID,
1133 UserHandle.USER_CURRENT);
1134 }
1135
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001136 private void writeAppWidgets(int[] appWidgetIds) {
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001137 Settings.Secure.putStringForUser(mContentResolver,
1138 Settings.Secure.LOCK_SCREEN_APPWIDGET_IDS,
1139 combineStrings(appWidgetIds, ","),
1140 UserHandle.USER_CURRENT);
1141 }
1142
Michael Jurka1254f2f2012-10-25 11:44:31 -07001143 // TODO: log an error if this returns false
1144 public boolean addAppWidget(int widgetId, int index) {
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001145 int[] widgets = getAppWidgets();
Michael Jurka1254f2f2012-10-25 11:44:31 -07001146 if (widgets == null) {
1147 return false;
1148 }
Michael Jurka229dd8b2012-10-26 16:06:12 -07001149 if (index < 0 || index > widgets.length) {
Michael Jurka1254f2f2012-10-25 11:44:31 -07001150 return false;
1151 }
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001152 int[] newWidgets = new int[widgets.length + 1];
1153 for (int i = 0, j = 0; i < newWidgets.length; i++) {
1154 if (index == i) {
1155 newWidgets[i] = widgetId;
1156 i++;
1157 }
1158 if (i < newWidgets.length) {
1159 newWidgets[i] = widgets[j];
1160 j++;
1161 }
1162 }
1163 writeAppWidgets(newWidgets);
Michael Jurka1254f2f2012-10-25 11:44:31 -07001164 return true;
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001165 }
1166
Michael Jurka1254f2f2012-10-25 11:44:31 -07001167 public boolean removeAppWidget(int widgetId) {
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001168 int[] widgets = getAppWidgets();
Michael Jurka1254f2f2012-10-25 11:44:31 -07001169
Jim Millera9768602012-11-06 22:17:25 -08001170 if (widgets.length == 0) {
1171 return false;
1172 }
1173
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001174 int[] newWidgets = new int[widgets.length - 1];
1175 for (int i = 0, j = 0; i < widgets.length; i++) {
Michael Jurka1254f2f2012-10-25 11:44:31 -07001176 if (widgets[i] == widgetId) {
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001177 // continue...
Michael Jurka1254f2f2012-10-25 11:44:31 -07001178 } else if (j >= newWidgets.length) {
1179 // we couldn't find the widget
1180 return false;
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001181 } else {
1182 newWidgets[j] = widgets[i];
1183 j++;
1184 }
1185 }
1186 writeAppWidgets(newWidgets);
1187 return true;
Michael Jurka20c41d52012-09-20 19:01:06 -07001188 }
1189
Amith Yamasani52c489c2012-03-28 11:42:42 -07001190 private long getLong(String secureSettingKey, long defaultValue) {
1191 try {
1192 return getLockSettings().getLong(secureSettingKey, defaultValue,
1193 getCurrentOrCallingUserId());
1194 } catch (RemoteException re) {
1195 return defaultValue;
1196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
1198
Amith Yamasani156c4352010-03-05 17:10:03 -08001199 private void setLong(String secureSettingKey, long value) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001200 setLong(secureSettingKey, value, getCurrentOrCallingUserId());
1201 }
1202
1203 private void setLong(String secureSettingKey, long value, int userHandle) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001204 try {
1205 getLockSettings().setLong(secureSettingKey, value, getCurrentOrCallingUserId());
1206 } catch (RemoteException re) {
1207 // What can we do?
1208 Log.e(TAG, "Couldn't write long " + secureSettingKey + re);
1209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -07001212 private String getString(String secureSettingKey) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001213 return getString(secureSettingKey, getCurrentOrCallingUserId());
1214 }
1215
1216 private String getString(String secureSettingKey, int userHandle) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001217 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001218 return getLockSettings().getString(secureSettingKey, null, userHandle);
Amith Yamasani52c489c2012-03-28 11:42:42 -07001219 } catch (RemoteException re) {
1220 return null;
1221 }
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -07001222 }
1223
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001224 private void setString(String secureSettingKey, String value, int userHandle) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001225 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001226 getLockSettings().setString(secureSettingKey, value, userHandle);
Amith Yamasani52c489c2012-03-28 11:42:42 -07001227 } catch (RemoteException re) {
1228 // What can we do?
1229 Log.e(TAG, "Couldn't write string " + secureSettingKey + re);
1230 }
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -07001231 }
1232
Jim Miller69aa4a92009-12-22 19:03:28 -08001233 public boolean isSecure() {
Jim Millercd709882010-03-25 18:24:02 -07001234 long mode = getKeyguardStoredPasswordQuality();
1235 final boolean isPattern = mode == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
1236 final boolean isPassword = mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC
1237 || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001238 || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC
1239 || mode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
Jim Millercd709882010-03-25 18:24:02 -07001240 final boolean secure = isPattern && isLockPatternEnabled() && savedPatternExists()
Danielle Millett73da5fe2011-09-13 16:20:05 -04001241 || isPassword && savedPasswordExists();
Jim Miller69aa4a92009-12-22 19:03:28 -08001242 return secure;
1243 }
Jim Miller69ac9882010-02-24 15:35:05 -08001244
1245 /**
John Wang0f7b3f82011-05-31 11:20:55 -07001246 * Sets the emergency button visibility based on isEmergencyCallCapable().
1247 *
1248 * If the emergency button is visible, sets the text on the emergency button
1249 * to indicate what action will be taken.
1250 *
Jim Miller69ac9882010-02-24 15:35:05 -08001251 * If there's currently a call in progress, the button will take them to the call
1252 * @param button the button to update
Jim Miller3f5f83b2011-09-26 15:17:05 -07001253 * @param the phone state:
1254 * {@link TelephonyManager#CALL_STATE_IDLE}
1255 * {@link TelephonyManager#CALL_STATE_RINGING}
1256 * {@link TelephonyManager#CALL_STATE_OFFHOOK}
Jim Miller1f56edc2011-11-07 19:00:48 -08001257 * @param shown indicates whether the given screen wants the emergency button to show at all
Jim Miller109f1fd2012-09-19 20:44:16 -07001258 * @param button
1259 * @param phoneState
1260 * @param shown shown if true; hidden if false
1261 * @param upperCase if true, converts button label string to upper case
Jim Miller69ac9882010-02-24 15:35:05 -08001262 */
Jim Miller109f1fd2012-09-19 20:44:16 -07001263 public void updateEmergencyCallButtonState(Button button, int phoneState, boolean shown,
1264 boolean upperCase, boolean showIcon) {
Jim Miller1f56edc2011-11-07 19:00:48 -08001265 if (isEmergencyCallCapable() && shown) {
John Wang0f7b3f82011-05-31 11:20:55 -07001266 button.setVisibility(View.VISIBLE);
1267 } else {
1268 button.setVisibility(View.GONE);
1269 return;
1270 }
1271
Jim Miller69ac9882010-02-24 15:35:05 -08001272 int textId;
Jim Miller3f5f83b2011-09-26 15:17:05 -07001273 if (phoneState == TelephonyManager.CALL_STATE_OFFHOOK) {
Jim Miller69ac9882010-02-24 15:35:05 -08001274 // show "return to call" text and show phone icon
1275 textId = R.string.lockscreen_return_to_call;
Jim Miller109f1fd2012-09-19 20:44:16 -07001276 int phoneCallIcon = showIcon ? R.drawable.stat_sys_phone_call : 0;
Jim Miller69ac9882010-02-24 15:35:05 -08001277 button.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
1278 } else {
1279 textId = R.string.lockscreen_emergency_call;
Jim Miller109f1fd2012-09-19 20:44:16 -07001280 int emergencyIcon = showIcon ? R.drawable.ic_emergency : 0;
Jim Miller69ac9882010-02-24 15:35:05 -08001281 button.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
1282 }
Jim Miller109f1fd2012-09-19 20:44:16 -07001283 if (upperCase) {
1284 CharSequence original = mContext.getResources().getText(textId);
1285 String upper = original != null ? original.toString().toUpperCase() : null;
1286 button.setText(upper);
1287 } else {
1288 button.setText(textId);
1289 }
1290 }
1291
1292 /**
1293 * @deprecated
1294 * @param button
1295 * @param phoneState
1296 * @param shown
1297 */
1298 public void updateEmergencyCallButtonState(Button button, int phoneState, boolean shown) {
1299 updateEmergencyCallButtonState(button, phoneState, shown, false, true);
Jim Miller69ac9882010-02-24 15:35:05 -08001300 }
1301
1302 /**
1303 * Resumes a call in progress. Typically launched from the EmergencyCall button
1304 * on various lockscreens.
1305 *
1306 * @return true if we were able to tell InCallScreen to show.
1307 */
1308 public boolean resumeCall() {
1309 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1310 try {
1311 if (phone != null && phone.showCallScreen()) {
1312 return true;
1313 }
1314 } catch (RemoteException e) {
1315 // What can we do?
1316 }
1317 return false;
1318 }
Danielle Millett044a0a72011-11-07 15:42:12 -05001319
1320 private void finishBiometricWeak() {
1321 setBoolean(BIOMETRIC_WEAK_EVER_CHOSEN_KEY, true);
1322
1323 // Launch intent to show final screen, this also
1324 // moves the temporary gallery to the actual gallery
1325 Intent intent = new Intent();
1326 intent.setClassName("com.android.facelock",
1327 "com.android.facelock.SetupEndScreen");
1328 mContext.startActivity(intent);
1329 }
1330
Jim Millera4edd152012-01-06 18:24:04 -08001331 public void setPowerButtonInstantlyLocks(boolean enabled) {
1332 setBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, enabled);
1333 }
1334
1335 public boolean getPowerButtonInstantlyLocks() {
1336 return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true);
1337 }
Jim Millera9768602012-11-06 22:17:25 -08001338
Jim Miller51117262012-11-04 17:58:09 -08001339 public static boolean isSafeModeEnabled() {
1340 try {
1341 return IWindowManager.Stub.asInterface(
1342 ServiceManager.getService("window")).isSafeModeEnabled();
1343 } catch (RemoteException e) {
1344 // Shouldn't happen!
1345 }
1346 return false;
1347 }
Jim Millera4edd152012-01-06 18:24:04 -08001348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349}