blob: a75b1cada9c8f0e7d7fd7e51edac6708211cc75c [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
Jason parksf7b3cd42011-01-27 09:28:25 -060019import com.android.internal.R;
20import com.android.internal.telephony.ITelephony;
21import com.google.android.collect.Lists;
22
Amith Yamasani4b4b9542012-09-14 13:36:29 -070023import android.app.ActivityManagerNative;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080024import android.app.admin.DevicePolicyManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.ContentResolver;
Jim Miller31f90b62010-01-20 13:35:20 -080026import android.content.Context;
Steven Ross329979c2011-09-28 11:42:56 -040027import android.content.Intent;
Danielle Millett58396982011-09-30 13:55:07 -040028import android.content.pm.PackageManager;
Amith Yamasani52c489c2012-03-28 11:42:42 -070029import android.os.Binder;
Adam Cohenf7522022012-10-03 20:03:18 -070030import android.os.Bundle;
Jason parksf7b3cd42011-01-27 09:28:25 -060031import android.os.IBinder;
Jim Miller69ac9882010-02-24 15:35:05 -080032import android.os.RemoteException;
33import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070035import android.os.UserHandle;
Jason parksf7b3cd42011-01-27 09:28:25 -060036import android.os.storage.IMountService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.provider.Settings;
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -070038import android.security.KeyStore;
Jim Miller69ac9882010-02-24 15:35:05 -080039import android.telephony.TelephonyManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.text.TextUtils;
41import android.util.Log;
John Wang0f7b3f82011-05-31 11:20:55 -070042import android.view.View;
Jim Miller69ac9882010-02-24 15:35:05 -080043import android.widget.Button;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Michael Jurkaaa2859a2012-10-24 12:46:49 -070045import org.apache.harmony.kernel.vm.StringUtils;
46
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;
Michael Jurkaaa2859a2012-10-24 12:46:49 -070050import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import java.util.List;
52
53/**
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -070054 * Utilities for the lock pattern and its settings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 */
56public class LockPatternUtils {
57
Jim Millercb3521e2011-10-03 20:42:26 -070058 private static final String OPTION_ENABLE_FACELOCK = "enable_facelock";
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 private static final String TAG = "LockPatternUtils";
Jim Miller69aa4a92009-12-22 19:03:28 -080061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 /**
63 * The maximum number of incorrect attempts before the user is prevented
64 * from trying again for {@link #FAILED_ATTEMPT_TIMEOUT_MS}.
65 */
66 public static final int FAILED_ATTEMPTS_BEFORE_TIMEOUT = 5;
67
68 /**
69 * The number of incorrect attempts before which we fall back on an alternative
70 * method of verifying the user, and resetting their lock pattern.
71 */
72 public static final int FAILED_ATTEMPTS_BEFORE_RESET = 20;
73
74 /**
75 * How long the user is prevented from trying again after entering the
76 * wrong pattern too many times.
77 */
78 public static final long FAILED_ATTEMPT_TIMEOUT_MS = 30000L;
79
80 /**
81 * The interval of the countdown for showing progress of the lockout.
82 */
83 public static final long FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS = 1000L;
84
Jim Miller4f369952011-08-19 18:29:22 -070085
86 /**
87 * This dictates when we start telling the user that continued failed attempts will wipe
88 * their device.
89 */
90 public static final int FAILED_ATTEMPTS_BEFORE_WIPE_GRACE = 5;
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
93 * The minimum number of dots in a valid pattern.
94 */
95 public static final int MIN_LOCK_PATTERN_SIZE = 4;
96
97 /**
98 * The minimum number of dots the user must include in a wrong pattern
99 * attempt for it to be counted against the counts that affect
100 * {@link #FAILED_ATTEMPTS_BEFORE_TIMEOUT} and {@link #FAILED_ATTEMPTS_BEFORE_RESET}
101 */
Jim Miller4f369952011-08-19 18:29:22 -0700102 public static final int MIN_PATTERN_REGISTER_FAIL = MIN_LOCK_PATTERN_SIZE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
Danielle Millett925a7d82012-03-19 18:02:20 -0400104 /**
Adam Cohenf7522022012-10-03 20:03:18 -0700105 * Tells the keyguard to show the user switcher when the keyguard is created.
106 */
107 public static final String KEYGUARD_SHOW_USER_SWITCHER = "showuserswitcher";
108
109 /**
110 * Tells the keyguard to show the security challenge when the keyguard is created.
111 */
112 public static final String KEYGUARD_SHOW_SECURITY_CHALLENGE = "showsecuritychallenge";
113
114 /**
115 * Options used to lock the device upon user switch.
116 */
117 public static final Bundle USER_SWITCH_LOCK_OPTIONS = new Bundle();
118
119 static {
120 USER_SWITCH_LOCK_OPTIONS.putBoolean(KEYGUARD_SHOW_USER_SWITCHER, true);
121 USER_SWITCH_LOCK_OPTIONS.putBoolean(KEYGUARD_SHOW_SECURITY_CHALLENGE, true);
122 }
123
124 /**
Danielle Millett925a7d82012-03-19 18:02:20 -0400125 * The bit in LOCK_BIOMETRIC_WEAK_FLAGS to be used to indicate whether liveliness should
126 * be used
127 */
128 public static final int FLAG_BIOMETRIC_WEAK_LIVELINESS = 0x1;
129
Amith Yamasani52c489c2012-03-28 11:42:42 -0700130 protected final static String LOCKOUT_PERMANENT_KEY = "lockscreen.lockedoutpermanently";
131 protected final static String LOCKOUT_ATTEMPT_DEADLINE = "lockscreen.lockoutattemptdeadline";
132 protected final static String PATTERN_EVER_CHOSEN_KEY = "lockscreen.patterneverchosen";
Jim Miller69aa4a92009-12-22 19:03:28 -0800133 public final static String PASSWORD_TYPE_KEY = "lockscreen.password_type";
Jim Miller6edf2632011-09-05 16:03:14 -0700134 public static final String PASSWORD_TYPE_ALTERNATE_KEY = "lockscreen.password_type_alternate";
Amith Yamasani52c489c2012-03-28 11:42:42 -0700135 protected final static String LOCK_PASSWORD_SALT_KEY = "lockscreen.password_salt";
136 protected final static String DISABLE_LOCKSCREEN_KEY = "lockscreen.disabled";
137 protected final static String LOCKSCREEN_OPTIONS = "lockscreen.options";
Jim Miller6edf2632011-09-05 16:03:14 -0700138 public final static String LOCKSCREEN_BIOMETRIC_WEAK_FALLBACK
139 = "lockscreen.biometric_weak_fallback";
Danielle Millett7a072192011-10-03 17:36:01 -0400140 public final static String BIOMETRIC_WEAK_EVER_CHOSEN_KEY
141 = "lockscreen.biometricweakeverchosen";
Jim Millera4edd152012-01-06 18:24:04 -0800142 public final static String LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS
143 = "lockscreen.power_button_instantly_locks";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Amith Yamasani52c489c2012-03-28 11:42:42 -0700145 protected final static String PASSWORD_HISTORY_KEY = "lockscreen.passwordhistory";
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700146
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800147 private final Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 private final ContentResolver mContentResolver;
Jim Miller31f90b62010-01-20 13:35:20 -0800149 private DevicePolicyManager mDevicePolicyManager;
Amith Yamasani52c489c2012-03-28 11:42:42 -0700150 private ILockSettings mLockSettingsService;
Jim Milleree82f8f2012-10-01 16:26:18 -0700151
152 // The current user is set by KeyguardViewMediator and shared by all LockPatternUtils.
153 private static volatile int sCurrentUserId = UserHandle.USER_NULL;
Dianne Hackbornde4c26f2011-07-17 13:42:47 -0700154
Jim Millercd709882010-03-25 18:24:02 -0700155 public DevicePolicyManager getDevicePolicyManager() {
Jim Miller5b0fb3a2010-02-23 13:46:35 -0800156 if (mDevicePolicyManager == null) {
157 mDevicePolicyManager =
158 (DevicePolicyManager)mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
159 if (mDevicePolicyManager == null) {
160 Log.e(TAG, "Can't get DevicePolicyManagerService: is it running?",
161 new IllegalStateException("Stack trace:"));
162 }
163 }
164 return mDevicePolicyManager;
165 }
Amith Yamasani52c489c2012-03-28 11:42:42 -0700166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 /**
168 * @param contentResolver Used to look up and save settings.
169 */
Jim Miller31f90b62010-01-20 13:35:20 -0800170 public LockPatternUtils(Context context) {
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800171 mContext = context;
Jim Miller31f90b62010-01-20 13:35:20 -0800172 mContentResolver = context.getContentResolver();
Amith Yamasani52c489c2012-03-28 11:42:42 -0700173 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800174
Amith Yamasani52c489c2012-03-28 11:42:42 -0700175 private ILockSettings getLockSettings() {
176 if (mLockSettingsService == null) {
177 mLockSettingsService = ILockSettings.Stub.asInterface(
178 (IBinder) ServiceManager.getService("lock_settings"));
Brad Fitzpatrick90881002010-08-23 18:30:08 -0700179 }
Amith Yamasani52c489c2012-03-28 11:42:42 -0700180 return mLockSettingsService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 }
182
Jim Miller31f90b62010-01-20 13:35:20 -0800183 public int getRequestedMinimumPasswordLength() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700184 return getDevicePolicyManager().getPasswordMinimumLength(null, getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800185 }
186
Jim Miller31f90b62010-01-20 13:35:20 -0800187 /**
188 * Gets the device policy password mode. If the mode is non-specific, returns
189 * MODE_PATTERN which allows the user to choose anything.
Jim Miller31f90b62010-01-20 13:35:20 -0800190 */
Jim Millercd709882010-03-25 18:24:02 -0700191 public int getRequestedPasswordQuality() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700192 return getDevicePolicyManager().getPasswordQuality(null, getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800193 }
194
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700195 public int getRequestedPasswordHistoryLength() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700196 return getDevicePolicyManager().getPasswordHistoryLength(null, getCurrentOrCallingUserId());
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700197 }
198
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700199 public int getRequestedPasswordMinimumLetters() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700200 return getDevicePolicyManager().getPasswordMinimumLetters(null,
201 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700202 }
203
204 public int getRequestedPasswordMinimumUpperCase() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700205 return getDevicePolicyManager().getPasswordMinimumUpperCase(null,
206 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700207 }
208
209 public int getRequestedPasswordMinimumLowerCase() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700210 return getDevicePolicyManager().getPasswordMinimumLowerCase(null,
211 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700212 }
213
214 public int getRequestedPasswordMinimumNumeric() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700215 return getDevicePolicyManager().getPasswordMinimumNumeric(null,
216 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700217 }
218
219 public int getRequestedPasswordMinimumSymbols() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700220 return getDevicePolicyManager().getPasswordMinimumSymbols(null,
221 getCurrentOrCallingUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700222 }
223
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700224 public int getRequestedPasswordMinimumNonLetter() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700225 return getDevicePolicyManager().getPasswordMinimumNonLetter(null,
226 getCurrentOrCallingUserId());
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700227 }
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700228
Jim Miller31f90b62010-01-20 13:35:20 -0800229 /**
230 * Returns the actual password mode, as set by keyguard after updating the password.
231 *
232 * @return
233 */
Jim Miller31f90b62010-01-20 13:35:20 -0800234 public void reportFailedPasswordAttempt() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700235 getDevicePolicyManager().reportFailedPasswordAttempt(getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800236 }
237
238 public void reportSuccessfulPasswordAttempt() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700239 getDevicePolicyManager().reportSuccessfulPasswordAttempt(getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800240 }
241
Amith Yamasani52c489c2012-03-28 11:42:42 -0700242 public void setCurrentUser(int userId) {
Jim Milleree82f8f2012-10-01 16:26:18 -0700243 sCurrentUserId = userId;
Amith Yamasani52c489c2012-03-28 11:42:42 -0700244 }
245
Amith Yamasanif882f1a2012-04-10 15:13:39 -0700246 public int getCurrentUser() {
Jim Milleree82f8f2012-10-01 16:26:18 -0700247 if (sCurrentUserId != UserHandle.USER_NULL) {
Jim Miller25645d82012-09-21 14:47:54 -0700248 // Someone is regularly updating using setCurrentUser() use that value.
Jim Milleree82f8f2012-10-01 16:26:18 -0700249 return sCurrentUserId;
Jim Miller25645d82012-09-21 14:47:54 -0700250 }
251 try {
252 return ActivityManagerNative.getDefault().getCurrentUser().id;
253 } catch (RemoteException re) {
254 return UserHandle.USER_OWNER;
Amith Yamasanif882f1a2012-04-10 15:13:39 -0700255 }
256 }
257
Amith Yamasani52c489c2012-03-28 11:42:42 -0700258 public void removeUser(int userId) {
Jim Miller25645d82012-09-21 14:47:54 -0700259 try {
260 getLockSettings().removeUser(userId);
261 } catch (RemoteException re) {
262 Log.e(TAG, "Couldn't remove lock settings for user " + userId);
Amith Yamasani52c489c2012-03-28 11:42:42 -0700263 }
264 }
265
266 private int getCurrentOrCallingUserId() {
267 int callingUid = Binder.getCallingUid();
268 if (callingUid == android.os.Process.SYSTEM_UID) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700269 // TODO: This is a little inefficient. See if all users of this are able to
270 // handle USER_CURRENT and pass that instead.
271 return getCurrentUser();
Amith Yamasani52c489c2012-03-28 11:42:42 -0700272 } else {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700273 return UserHandle.getUserId(callingUid);
Amith Yamasani52c489c2012-03-28 11:42:42 -0700274 }
275 }
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * Check to see if a pattern matches the saved pattern. If no pattern exists,
279 * always returns true.
280 * @param pattern The pattern to check.
Jim Miller69aa4a92009-12-22 19:03:28 -0800281 * @return Whether the pattern matches the stored one.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 */
283 public boolean checkPattern(List<LockPatternView.Cell> pattern) {
Kenny Rootdf8bfe02012-09-21 15:00:25 -0700284 final int userId = getCurrentOrCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 try {
Kenny Rootdf8bfe02012-09-21 15:00:25 -0700286 final boolean matched = getLockSettings().checkPattern(patternToHash(pattern), userId);
287 if (matched && (userId == UserHandle.USER_OWNER)) {
288 KeyStore.getInstance().password(patternToString(pattern));
289 }
290 return matched;
Amith Yamasani52c489c2012-03-28 11:42:42 -0700291 } catch (RemoteException re) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 return true;
293 }
294 }
295
296 /**
Jim Miller69aa4a92009-12-22 19:03:28 -0800297 * Check to see if a password matches the saved password. If no password exists,
298 * always returns true.
299 * @param password The password to check.
300 * @return Whether the password matches the stored one.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 */
Jim Miller69aa4a92009-12-22 19:03:28 -0800302 public boolean checkPassword(String password) {
Kenny Rootdf8bfe02012-09-21 15:00:25 -0700303 final int userId = getCurrentOrCallingUserId();
Jim Miller69aa4a92009-12-22 19:03:28 -0800304 try {
Kenny Rootdf8bfe02012-09-21 15:00:25 -0700305 final boolean matched = getLockSettings().checkPassword(passwordToHash(password),
306 userId);
307 if (matched && (userId == UserHandle.USER_OWNER)) {
308 KeyStore.getInstance().password(password);
309 }
310 return matched;
Amith Yamasani52c489c2012-03-28 11:42:42 -0700311 } catch (RemoteException re) {
Jim Miller69aa4a92009-12-22 19:03:28 -0800312 return true;
313 }
314 }
315
316 /**
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700317 * Check to see if a password matches any of the passwords stored in the
318 * password history.
319 *
320 * @param password The password to check.
321 * @return Whether the password matches any in the history.
322 */
323 public boolean checkPasswordHistory(String password) {
324 String passwordHashString = new String(passwordToHash(password));
325 String passwordHistory = getString(PASSWORD_HISTORY_KEY);
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700326 if (passwordHistory == null) {
327 return false;
328 }
329 // Password History may be too long...
330 int passwordHashLength = passwordHashString.length();
331 int passwordHistoryLength = getRequestedPasswordHistoryLength();
332 if(passwordHistoryLength == 0) {
333 return false;
334 }
335 int neededPasswordHistoryLength = passwordHashLength * passwordHistoryLength
336 + passwordHistoryLength - 1;
337 if (passwordHistory.length() > neededPasswordHistoryLength) {
338 passwordHistory = passwordHistory.substring(0, neededPasswordHistoryLength);
339 }
340 return passwordHistory.contains(passwordHashString);
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700341 }
342
343 /**
Jim Miller69aa4a92009-12-22 19:03:28 -0800344 * Check to see if the user has stored a lock pattern.
345 * @return Whether a saved pattern exists.
346 */
347 public boolean savedPatternExists() {
Amith Yamasani52c489c2012-03-28 11:42:42 -0700348 try {
349 return getLockSettings().havePattern(getCurrentOrCallingUserId());
350 } catch (RemoteException re) {
351 return false;
352 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800353 }
354
355 /**
356 * Check to see if the user has stored a lock pattern.
357 * @return Whether a saved pattern exists.
358 */
359 public boolean savedPasswordExists() {
Amith Yamasani52c489c2012-03-28 11:42:42 -0700360 try {
361 return getLockSettings().havePassword(getCurrentOrCallingUserId());
362 } catch (RemoteException re) {
363 return false;
364 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800365 }
366
367 /**
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700368 * Return true if the user has ever chosen a pattern. This is true even if the pattern is
369 * currently cleared.
370 *
371 * @return True if the user has ever chosen a pattern.
372 */
373 public boolean isPatternEverChosen() {
Jim Millera4edd152012-01-06 18:24:04 -0800374 return getBoolean(PATTERN_EVER_CHOSEN_KEY, false);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700375 }
376
377 /**
Danielle Millett7a072192011-10-03 17:36:01 -0400378 * Return true if the user has ever chosen biometric weak. This is true even if biometric
379 * weak is not current set.
380 *
381 * @return True if the user has ever chosen biometric weak.
382 */
383 public boolean isBiometricWeakEverChosen() {
Jim Millera4edd152012-01-06 18:24:04 -0800384 return getBoolean(BIOMETRIC_WEAK_EVER_CHOSEN_KEY, false);
Danielle Millett7a072192011-10-03 17:36:01 -0400385 }
386
387 /**
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700388 * Used by device policy manager to validate the current password
389 * information it has.
390 */
391 public int getActivePasswordQuality() {
Jim Millercd709882010-03-25 18:24:02 -0700392 int activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
Danielle Millettc8fb5322011-10-04 12:18:51 -0400393 // Note we don't want to use getKeyguardStoredPasswordQuality() because we want this to
394 // return biometric_weak if that is being used instead of the backup
395 int quality =
396 (int) getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
397 switch (quality) {
Jim Millercd709882010-03-25 18:24:02 -0700398 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700399 if (isLockPatternEnabled()) {
Jim Millercd709882010-03-25 18:24:02 -0700400 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700401 }
Jim Millercd709882010-03-25 18:24:02 -0700402 break;
Danielle Millettc8fb5322011-10-04 12:18:51 -0400403 case DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK:
404 if (isBiometricWeakInstalled()) {
405 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK;
406 }
407 break;
Jim Millercd709882010-03-25 18:24:02 -0700408 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700409 if (isLockPasswordEnabled()) {
Jim Millercd709882010-03-25 18:24:02 -0700410 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700411 }
Jim Millercd709882010-03-25 18:24:02 -0700412 break;
413 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700414 if (isLockPasswordEnabled()) {
Jim Millercd709882010-03-25 18:24:02 -0700415 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700416 }
Jim Millercd709882010-03-25 18:24:02 -0700417 break;
418 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
419 if (isLockPasswordEnabled()) {
420 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC;
421 }
422 break;
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700423 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
424 if (isLockPasswordEnabled()) {
425 activePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
426 }
427 break;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700428 }
Danielle Millettc8fb5322011-10-04 12:18:51 -0400429
Jim Millercd709882010-03-25 18:24:02 -0700430 return activePasswordQuality;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700431 }
Jim Millercd709882010-03-25 18:24:02 -0700432
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700433 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800434 * Clear any lock pattern or password.
435 */
Steven Ross329979c2011-09-28 11:42:56 -0400436 public void clearLock(boolean isFallback) {
437 if(!isFallback) deleteGallery();
Jim Millercd709882010-03-25 18:24:02 -0700438 saveLockPassword(null, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800439 setLockPatternEnabled(false);
440 saveLockPattern(null);
Jim Miller93708af12012-01-25 18:26:12 -0800441 setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
Jim Miller6edf2632011-09-05 16:03:14 -0700442 setLong(PASSWORD_TYPE_ALTERNATE_KEY, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800443 }
Jim Miller5b0fb3a2010-02-23 13:46:35 -0800444
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800445 /**
Jim Miller2a98a4c2010-11-19 18:49:26 -0800446 * Disable showing lock screen at all when the DevicePolicyManager allows it.
447 * This is only meaningful if pattern, pin or password are not set.
448 *
449 * @param disable Disables lock screen when true
450 */
451 public void setLockScreenDisabled(boolean disable) {
452 setLong(DISABLE_LOCKSCREEN_KEY, disable ? 1 : 0);
453 }
454
455 /**
456 * Determine if LockScreen can be disabled. This is used, for example, to tell if we should
457 * show LockScreen or go straight to the home screen.
458 *
459 * @return true if lock screen is can be disabled
460 */
461 public boolean isLockScreenDisabled() {
462 return !isSecure() && getLong(DISABLE_LOCKSCREEN_KEY, 0) != 0;
463 }
464
465 /**
Steven Ross3553c292011-09-30 15:48:40 -0400466 * Calls back SetupFaceLock to delete the temporary gallery file
Steven Ross329979c2011-09-28 11:42:56 -0400467 */
468 public void deleteTempGallery() {
Danielle Millett574e49e2012-03-27 16:17:10 -0400469 Intent intent = new Intent().setAction("com.android.facelock.DELETE_GALLERY");
Steven Ross3553c292011-09-30 15:48:40 -0400470 intent.putExtra("deleteTempGallery", true);
Danielle Millett574e49e2012-03-27 16:17:10 -0400471 mContext.sendBroadcast(intent);
Steven Ross329979c2011-09-28 11:42:56 -0400472 }
473
474 /**
475 * Calls back SetupFaceLock to delete the gallery file when the lock type is changed
476 */
477 void deleteGallery() {
Danielle Millett58396982011-09-30 13:55:07 -0400478 if(usingBiometricWeak()) {
Danielle Millett574e49e2012-03-27 16:17:10 -0400479 Intent intent = new Intent().setAction("com.android.facelock.DELETE_GALLERY");
Steven Ross329979c2011-09-28 11:42:56 -0400480 intent.putExtra("deleteGallery", true);
Danielle Millett574e49e2012-03-27 16:17:10 -0400481 mContext.sendBroadcast(intent);
Steven Ross329979c2011-09-28 11:42:56 -0400482 }
483 }
484
485 /**
Jim Miller6edf2632011-09-05 16:03:14 -0700486 * Save a lock pattern.
487 * @param pattern The new pattern to save.
Danielle Millett2364a222011-12-21 17:02:32 -0500488 */
489 public void saveLockPattern(List<LockPatternView.Cell> pattern) {
490 this.saveLockPattern(pattern, false);
491 }
492
493 /**
494 * Save a lock pattern.
495 * @param pattern The new pattern to save.
Jim Miller6edf2632011-09-05 16:03:14 -0700496 * @param isFallback Specifies if this is a fallback to biometric weak
497 */
498 public void saveLockPattern(List<LockPatternView.Cell> pattern, boolean isFallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 // Compute the hash
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700500 final byte[] hash = LockPatternUtils.patternToHash(pattern);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 try {
Amith Yamasani52c489c2012-03-28 11:42:42 -0700502 getLockSettings().setLockPattern(hash, getCurrentOrCallingUserId());
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800503 DevicePolicyManager dpm = getDevicePolicyManager();
Brian Carlstrome2afc242011-06-02 16:21:55 -0700504 KeyStore keyStore = KeyStore.getInstance();
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800505 if (pattern != null) {
Brian Carlstrome2afc242011-06-02 16:21:55 -0700506 keyStore.password(patternToString(pattern));
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800507 setBoolean(PATTERN_EVER_CHOSEN_KEY, true);
Jim Miller6edf2632011-09-05 16:03:14 -0700508 if (!isFallback) {
Steven Ross329979c2011-09-28 11:42:56 -0400509 deleteGallery();
Jim Miller6edf2632011-09-05 16:03:14 -0700510 setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
Danielle Millett2364a222011-12-21 17:02:32 -0500511 dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700512 pattern.size(), 0, 0, 0, 0, 0, 0, getCurrentOrCallingUserId());
Jim Miller6edf2632011-09-05 16:03:14 -0700513 } else {
514 setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK);
515 setLong(PASSWORD_TYPE_ALTERNATE_KEY,
516 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
Danielle Millett044a0a72011-11-07 15:42:12 -0500517 finishBiometricWeak();
Danielle Millett2364a222011-12-21 17:02:32 -0500518 dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700519 0, 0, 0, 0, 0, 0, 0, getCurrentOrCallingUserId());
Jim Miller6edf2632011-09-05 16:03:14 -0700520 }
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800521 } else {
Brian Carlstrome2afc242011-06-02 16:21:55 -0700522 if (keyStore.isEmpty()) {
523 keyStore.reset();
524 }
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700525 dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, 0, 0,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700526 0, 0, 0, 0, 0, getCurrentOrCallingUserId());
Jim Miller31f90b62010-01-20 13:35:20 -0800527 }
Amith Yamasani52c489c2012-03-28 11:42:42 -0700528 } catch (RemoteException re) {
529 Log.e(TAG, "Couldn't save lock pattern " + re);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
531 }
532
533 /**
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700534 * Compute the password quality from the given password string.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800535 */
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700536 static public int computePasswordQuality(String password) {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800537 boolean hasDigit = false;
538 boolean hasNonDigit = false;
539 final int len = password.length();
540 for (int i = 0; i < len; i++) {
541 if (Character.isDigit(password.charAt(i))) {
542 hasDigit = true;
543 } else {
544 hasNonDigit = true;
545 }
546 }
Jim Miller5b0fb3a2010-02-23 13:46:35 -0800547
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700548 if (hasNonDigit && hasDigit) {
549 return DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC;
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800550 }
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800551 if (hasNonDigit) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700552 return DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800553 }
554 if (hasDigit) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700555 return DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800556 }
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700557 return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800558 }
Jim Miller5b0fb3a2010-02-23 13:46:35 -0800559
Jason parksf7b3cd42011-01-27 09:28:25 -0600560 /** Update the encryption password if it is enabled **/
561 private void updateEncryptionPassword(String password) {
562 DevicePolicyManager dpm = getDevicePolicyManager();
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700563 if (dpm.getStorageEncryptionStatus(getCurrentOrCallingUserId())
564 != DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE) {
Jason parksf7b3cd42011-01-27 09:28:25 -0600565 return;
566 }
567
568 IBinder service = ServiceManager.getService("mount");
569 if (service == null) {
570 Log.e(TAG, "Could not find the mount service to update the encryption password");
571 return;
572 }
573
574 IMountService mountService = IMountService.Stub.asInterface(service);
575 try {
576 mountService.changeEncryptionPassword(password);
577 } catch (RemoteException e) {
578 Log.e(TAG, "Error changing encryption password", e);
579 }
580 }
581
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800582 /**
Jim Millercd709882010-03-25 18:24:02 -0700583 * Save a lock password. Does not ensure that the password is as good
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800584 * as the requested mode, but will adjust the mode to be as good as the
585 * pattern.
Jim Miller69aa4a92009-12-22 19:03:28 -0800586 * @param password The password to save
Jim Millercd709882010-03-25 18:24:02 -0700587 * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
Jim Miller69aa4a92009-12-22 19:03:28 -0800588 */
Jim Millercd709882010-03-25 18:24:02 -0700589 public void saveLockPassword(String password, int quality) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700590 this.saveLockPassword(password, quality, false, getCurrentOrCallingUserId());
Jim Miller6edf2632011-09-05 16:03:14 -0700591 }
592
593 /**
594 * Save a lock password. Does not ensure that the password is as good
595 * as the requested mode, but will adjust the mode to be as good as the
596 * pattern.
597 * @param password The password to save
598 * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
599 * @param isFallback Specifies if this is a fallback to biometric weak
600 */
601 public void saveLockPassword(String password, int quality, boolean isFallback) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700602 saveLockPassword(password, quality, isFallback, getCurrentOrCallingUserId());
603 }
604
605 /**
606 * Save a lock password. Does not ensure that the password is as good
607 * as the requested mode, but will adjust the mode to be as good as the
608 * pattern.
609 * @param password The password to save
610 * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
611 * @param isFallback Specifies if this is a fallback to biometric weak
612 * @param userHandle The userId of the user to change the password for
613 */
614 public void saveLockPassword(String password, int quality, boolean isFallback, int userHandle) {
Jim Miller69aa4a92009-12-22 19:03:28 -0800615 // Compute the hash
Jim Miller11b019d2010-01-20 16:34:45 -0800616 final byte[] hash = passwordToHash(password);
Jim Miller69aa4a92009-12-22 19:03:28 -0800617 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700618 getLockSettings().setLockPassword(hash, userHandle);
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800619 DevicePolicyManager dpm = getDevicePolicyManager();
Brian Carlstrome2afc242011-06-02 16:21:55 -0700620 KeyStore keyStore = KeyStore.getInstance();
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800621 if (password != null) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700622 if (userHandle == UserHandle.USER_OWNER) {
623 // Update the encryption password.
624 updateEncryptionPassword(password);
Jason parksf7b3cd42011-01-27 09:28:25 -0600625
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700626 // Update the keystore password
627 keyStore.password(password);
628 }
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700629
Jim Millercd709882010-03-25 18:24:02 -0700630 int computedQuality = computePasswordQuality(password);
Jim Miller6edf2632011-09-05 16:03:14 -0700631 if (!isFallback) {
Steven Ross329979c2011-09-28 11:42:56 -0400632 deleteGallery();
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700633 setLong(PASSWORD_TYPE_KEY, Math.max(quality, computedQuality), userHandle);
Danielle Millett2364a222011-12-21 17:02:32 -0500634 if (computedQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
635 int letters = 0;
636 int uppercase = 0;
637 int lowercase = 0;
638 int numbers = 0;
639 int symbols = 0;
640 int nonletter = 0;
641 for (int i = 0; i < password.length(); i++) {
642 char c = password.charAt(i);
643 if (c >= 'A' && c <= 'Z') {
644 letters++;
645 uppercase++;
646 } else if (c >= 'a' && c <= 'z') {
647 letters++;
648 lowercase++;
649 } else if (c >= '0' && c <= '9') {
650 numbers++;
651 nonletter++;
652 } else {
653 symbols++;
654 nonletter++;
655 }
656 }
657 dpm.setActivePasswordState(Math.max(quality, computedQuality),
658 password.length(), letters, uppercase, lowercase,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700659 numbers, symbols, nonletter, userHandle);
Danielle Millett2364a222011-12-21 17:02:32 -0500660 } else {
661 // The password is not anything.
662 dpm.setActivePasswordState(
663 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700664 0, 0, 0, 0, 0, 0, 0, userHandle);
Danielle Millett2364a222011-12-21 17:02:32 -0500665 }
Jim Miller6edf2632011-09-05 16:03:14 -0700666 } else {
Danielle Millett2364a222011-12-21 17:02:32 -0500667 // Case where it's a fallback for biometric weak
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700668 setLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK,
669 userHandle);
670 setLong(PASSWORD_TYPE_ALTERNATE_KEY, Math.max(quality, computedQuality),
671 userHandle);
Danielle Millett044a0a72011-11-07 15:42:12 -0500672 finishBiometricWeak();
Danielle Millett2364a222011-12-21 17:02:32 -0500673 dpm.setActivePasswordState(DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK,
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700674 0, 0, 0, 0, 0, 0, 0, userHandle);
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700675 }
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700676 // Add the password to the password history. We assume all
677 // password
678 // hashes have the same length for simplicity of implementation.
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700679 String passwordHistory = getString(PASSWORD_HISTORY_KEY, userHandle);
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -0700680 if (passwordHistory == null) {
681 passwordHistory = new String();
682 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700683 int passwordHistoryLength = getRequestedPasswordHistoryLength();
684 if (passwordHistoryLength == 0) {
685 passwordHistory = "";
686 } else {
687 passwordHistory = new String(hash) + "," + passwordHistory;
688 // Cut it to contain passwordHistoryLength hashes
689 // and passwordHistoryLength -1 commas.
690 passwordHistory = passwordHistory.substring(0, Math.min(hash.length
691 * passwordHistoryLength + passwordHistoryLength - 1, passwordHistory
692 .length()));
693 }
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700694 setString(PASSWORD_HISTORY_KEY, passwordHistory, userHandle);
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800695 } else {
Brian Carlstrome2afc242011-06-02 16:21:55 -0700696 // Conditionally reset the keystore if empty. If
697 // non-empty, we are just switching key guard type
698 if (keyStore.isEmpty()) {
699 keyStore.reset();
700 }
Dianne Hackborn2509d3c2010-03-08 12:54:25 -0800701 dpm.setActivePasswordState(
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700702 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED, 0, 0, 0, 0, 0, 0, 0,
703 userHandle);
Jim Miller31f90b62010-01-20 13:35:20 -0800704 }
Amith Yamasani52c489c2012-03-28 11:42:42 -0700705 } catch (RemoteException re) {
Jim Miller69aa4a92009-12-22 19:03:28 -0800706 // Cant do much
Amith Yamasani52c489c2012-03-28 11:42:42 -0700707 Log.e(TAG, "Unable to save lock password " + re);
Jim Miller69aa4a92009-12-22 19:03:28 -0800708 }
709 }
710
Jim Millercd709882010-03-25 18:24:02 -0700711 /**
712 * Retrieves the quality mode we're in.
713 * {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
714 *
715 * @return stored password quality
716 */
717 public int getKeyguardStoredPasswordQuality() {
Jim Miller6edf2632011-09-05 16:03:14 -0700718 int quality =
719 (int) getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
720 // If the user has chosen to use weak biometric sensor, then return the backup locking
721 // method and treat biometric as a special case.
722 if (quality == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK) {
723 quality =
724 (int) getLong(PASSWORD_TYPE_ALTERNATE_KEY,
725 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
726 }
727 return quality;
728 }
729
Danielle Millett58396982011-09-30 13:55:07 -0400730 /**
731 * @return true if the lockscreen method is set to biometric weak
732 */
Jim Miller6edf2632011-09-05 16:03:14 -0700733 public boolean usingBiometricWeak() {
734 int quality =
735 (int) getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
736 return quality == DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK;
Jim Miller69aa4a92009-12-22 19:03:28 -0800737 }
738
739 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 * Deserialize a pattern.
741 * @param string The pattern serialized with {@link #patternToString}
742 * @return The pattern.
743 */
744 public static List<LockPatternView.Cell> stringToPattern(String string) {
745 List<LockPatternView.Cell> result = Lists.newArrayList();
746
747 final byte[] bytes = string.getBytes();
748 for (int i = 0; i < bytes.length; i++) {
749 byte b = bytes[i];
750 result.add(LockPatternView.Cell.of(b / 3, b % 3));
751 }
752 return result;
753 }
754
755 /**
756 * Serialize a pattern.
757 * @param pattern The pattern.
758 * @return The pattern in string form.
759 */
760 public static String patternToString(List<LockPatternView.Cell> pattern) {
761 if (pattern == null) {
762 return "";
763 }
764 final int patternSize = pattern.size();
765
766 byte[] res = new byte[patternSize];
767 for (int i = 0; i < patternSize; i++) {
768 LockPatternView.Cell cell = pattern.get(i);
769 res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
770 }
771 return new String(res);
772 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 /*
775 * Generate an SHA-1 hash for the pattern. Not the most secure, but it is
776 * at least a second level of protection. First level is that the file
777 * is in a location only readable by the system process.
778 * @param pattern the gesture pattern.
779 * @return the hash of the pattern in a byte array.
780 */
Jim Miller69aa4a92009-12-22 19:03:28 -0800781 private static byte[] patternToHash(List<LockPatternView.Cell> pattern) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 if (pattern == null) {
783 return null;
784 }
Jim Miller69aa4a92009-12-22 19:03:28 -0800785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 final int patternSize = pattern.size();
787 byte[] res = new byte[patternSize];
788 for (int i = 0; i < patternSize; i++) {
789 LockPatternView.Cell cell = pattern.get(i);
790 res[i] = (byte) (cell.getRow() * 3 + cell.getColumn());
791 }
792 try {
793 MessageDigest md = MessageDigest.getInstance("SHA-1");
794 byte[] hash = md.digest(res);
795 return hash;
796 } catch (NoSuchAlgorithmException nsa) {
797 return res;
798 }
799 }
800
Jim Miller11b019d2010-01-20 16:34:45 -0800801 private String getSalt() {
802 long salt = getLong(LOCK_PASSWORD_SALT_KEY, 0);
803 if (salt == 0) {
804 try {
805 salt = SecureRandom.getInstance("SHA1PRNG").nextLong();
806 setLong(LOCK_PASSWORD_SALT_KEY, salt);
807 Log.v(TAG, "Initialized lock password salt");
808 } catch (NoSuchAlgorithmException e) {
809 // Throw an exception rather than storing a password we'll never be able to recover
810 throw new IllegalStateException("Couldn't get SecureRandom number", e);
811 }
812 }
813 return Long.toHexString(salt);
814 }
815
Jim Miller69aa4a92009-12-22 19:03:28 -0800816 /*
817 * Generate a hash for the given password. To avoid brute force attacks, we use a salted hash.
818 * Not the most secure, but it is at least a second level of protection. First level is that
819 * the file is in a location only readable by the system process.
820 * @param password the gesture pattern.
821 * @return the hash of the pattern in a byte array.
822 */
Brian Carlstrom5cfee3f2011-05-31 01:00:15 -0700823 public byte[] passwordToHash(String password) {
Jim Miller69aa4a92009-12-22 19:03:28 -0800824 if (password == null) {
825 return null;
826 }
827 String algo = null;
828 byte[] hashed = null;
829 try {
Jim Miller11b019d2010-01-20 16:34:45 -0800830 byte[] saltedPassword = (password + getSalt()).getBytes();
Jim Miller69aa4a92009-12-22 19:03:28 -0800831 byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword);
832 byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword);
833 hashed = (toHex(sha1) + toHex(md5)).getBytes();
834 } catch (NoSuchAlgorithmException e) {
835 Log.w(TAG, "Failed to encode string because of missing algorithm: " + algo);
836 }
837 return hashed;
838 }
839
840 private static String toHex(byte[] ary) {
841 final String hex = "0123456789ABCDEF";
842 String ret = "";
843 for (int i = 0; i < ary.length; i++) {
844 ret += hex.charAt((ary[i] >> 4) & 0xf);
845 ret += hex.charAt(ary[i] & 0xf);
846 }
847 return ret;
848 }
849
850 /**
Danielle Millett73da5fe2011-09-13 16:20:05 -0400851 * @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 -0800852 */
853 public boolean isLockPasswordEnabled() {
854 long mode = getLong(PASSWORD_TYPE_KEY, 0);
Danielle Millett73da5fe2011-09-13 16:20:05 -0400855 long backupMode = getLong(PASSWORD_TYPE_ALTERNATE_KEY, 0);
856 final boolean passwordEnabled = mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC
857 || mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC
858 || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC
859 || mode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
860 final boolean backupEnabled = backupMode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC
861 || backupMode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC
862 || backupMode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC
863 || backupMode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
864
865 return savedPasswordExists() && (passwordEnabled ||
Danielle Millett58396982011-09-30 13:55:07 -0400866 (usingBiometricWeak() && backupEnabled));
Jim Miller69aa4a92009-12-22 19:03:28 -0800867 }
868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 /**
Danielle Millett73da5fe2011-09-13 16:20:05 -0400870 * @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 -0800871 */
872 public boolean isLockPatternEnabled() {
Danielle Millett73da5fe2011-09-13 16:20:05 -0400873 final boolean backupEnabled =
874 getLong(PASSWORD_TYPE_ALTERNATE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING)
875 == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
876
Jim Millera4edd152012-01-06 18:24:04 -0800877 return getBoolean(Settings.Secure.LOCK_PATTERN_ENABLED, false)
Danielle Millett73da5fe2011-09-13 16:20:05 -0400878 && (getLong(PASSWORD_TYPE_KEY, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING)
879 == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING ||
Danielle Millett58396982011-09-30 13:55:07 -0400880 (usingBiometricWeak() && backupEnabled));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 }
882
883 /**
Danielle Millett58396982011-09-30 13:55:07 -0400884 * @return Whether biometric weak lock is installed and that the front facing camera exists
Jim Miller6edf2632011-09-05 16:03:14 -0700885 */
Danielle Millett58396982011-09-30 13:55:07 -0400886 public boolean isBiometricWeakInstalled() {
Danielle Millett58396982011-09-30 13:55:07 -0400887 // Check that it's installed
888 PackageManager pm = mContext.getPackageManager();
889 try {
890 pm.getPackageInfo("com.android.facelock", PackageManager.GET_ACTIVITIES);
891 } catch (PackageManager.NameNotFoundException e) {
892 return false;
893 }
894
895 // Check that the camera is enabled
896 if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
897 return false;
898 }
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700899 if (getDevicePolicyManager().getCameraDisabled(null, getCurrentOrCallingUserId())) {
Danielle Millett58396982011-09-30 13:55:07 -0400900 return false;
901 }
902
903
904 return true;
Jim Miller6edf2632011-09-05 16:03:14 -0700905 }
906
907 /**
Danielle Millett925a7d82012-03-19 18:02:20 -0400908 * Set whether biometric weak liveliness is enabled.
909 */
910 public void setBiometricWeakLivelinessEnabled(boolean enabled) {
911 long currentFlag = getLong(Settings.Secure.LOCK_BIOMETRIC_WEAK_FLAGS, 0L);
912 long newFlag;
913 if (enabled) {
914 newFlag = currentFlag | FLAG_BIOMETRIC_WEAK_LIVELINESS;
915 } else {
916 newFlag = currentFlag & ~FLAG_BIOMETRIC_WEAK_LIVELINESS;
917 }
918 setLong(Settings.Secure.LOCK_BIOMETRIC_WEAK_FLAGS, newFlag);
919 }
920
921 /**
922 * @return Whether the biometric weak liveliness is enabled.
923 */
924 public boolean isBiometricWeakLivelinessEnabled() {
925 long currentFlag = getLong(Settings.Secure.LOCK_BIOMETRIC_WEAK_FLAGS, 0L);
926 return ((currentFlag & FLAG_BIOMETRIC_WEAK_LIVELINESS) != 0);
927 }
928
929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 * Set whether the lock pattern is enabled.
931 */
932 public void setLockPatternEnabled(boolean enabled) {
Amith Yamasani156c4352010-03-05 17:10:03 -0800933 setBoolean(Settings.Secure.LOCK_PATTERN_ENABLED, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 }
935
936 /**
937 * @return Whether the visible pattern is enabled.
938 */
939 public boolean isVisiblePatternEnabled() {
Jim Millera4edd152012-01-06 18:24:04 -0800940 return getBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
942
943 /**
944 * Set whether the visible pattern is enabled.
945 */
946 public void setVisiblePatternEnabled(boolean enabled) {
Amith Yamasani156c4352010-03-05 17:10:03 -0800947 setBoolean(Settings.Secure.LOCK_PATTERN_VISIBLE, enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949
950 /**
951 * @return Whether tactile feedback for the pattern is enabled.
952 */
953 public boolean isTactileFeedbackEnabled() {
Jeff Sharkey5ed9d682012-10-10 14:28:27 -0700954 return Settings.System.getIntForUser(mContentResolver,
955 Settings.System.HAPTIC_FEEDBACK_ENABLED, 1, UserHandle.USER_CURRENT) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
957
958 /**
959 * Set and store the lockout deadline, meaning the user can't attempt his/her unlock
960 * pattern until the deadline has passed.
961 * @return the chosen deadline.
962 */
963 public long setLockoutAttemptDeadline() {
964 final long deadline = SystemClock.elapsedRealtime() + FAILED_ATTEMPT_TIMEOUT_MS;
965 setLong(LOCKOUT_ATTEMPT_DEADLINE, deadline);
966 return deadline;
967 }
968
969 /**
970 * @return The elapsed time in millis in the future when the user is allowed to
971 * attempt to enter his/her lock pattern, or 0 if the user is welcome to
972 * enter a pattern.
973 */
974 public long getLockoutAttemptDeadline() {
975 final long deadline = getLong(LOCKOUT_ATTEMPT_DEADLINE, 0L);
976 final long now = SystemClock.elapsedRealtime();
977 if (deadline < now || deadline > (now + FAILED_ATTEMPT_TIMEOUT_MS)) {
978 return 0L;
979 }
980 return deadline;
981 }
982
983 /**
984 * @return Whether the user is permanently locked out until they verify their
985 * credentials. Occurs after {@link #FAILED_ATTEMPTS_BEFORE_RESET} failed
986 * attempts.
987 */
988 public boolean isPermanentlyLocked() {
Jim Millera4edd152012-01-06 18:24:04 -0800989 return getBoolean(LOCKOUT_PERMANENT_KEY, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991
992 /**
993 * Set the state of whether the device is permanently locked, meaning the user
Karl Rosaen678771b2009-08-21 14:00:26 -0700994 * must authenticate via other means.
995 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 * @param locked Whether the user is permanently locked out until they verify their
997 * credentials. Occurs after {@link #FAILED_ATTEMPTS_BEFORE_RESET} failed
998 * attempts.
999 */
1000 public void setPermanentlyLocked(boolean locked) {
1001 setBoolean(LOCKOUT_PERMANENT_KEY, locked);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003
John Wang0f7b3f82011-05-31 11:20:55 -07001004 public boolean isEmergencyCallCapable() {
1005 return mContext.getResources().getBoolean(
1006 com.android.internal.R.bool.config_voice_capable);
1007 }
1008
1009 public boolean isPukUnlockScreenEnable() {
1010 return mContext.getResources().getBoolean(
1011 com.android.internal.R.bool.config_enable_puk_unlock_screen);
1012 }
1013
Jim Miller1f56edc2011-11-07 19:00:48 -08001014 public boolean isEmergencyCallEnabledWhileSimLocked() {
1015 return mContext.getResources().getBoolean(
1016 com.android.internal.R.bool.config_enable_emergency_call_while_sim_locked);
1017 }
1018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 /**
1020 * @return A formatted string of the next alarm (for showing on the lock screen),
1021 * or null if there is no next alarm.
1022 */
1023 public String getNextAlarm() {
Amith Yamasani8fd96ec2012-09-21 17:48:49 -07001024 String nextAlarm = Settings.System.getStringForUser(mContentResolver,
1025 Settings.System.NEXT_ALARM_FORMATTED, UserHandle.USER_CURRENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 if (nextAlarm == null || TextUtils.isEmpty(nextAlarm)) {
1027 return null;
1028 }
1029 return nextAlarm;
1030 }
1031
Jim Millera4edd152012-01-06 18:24:04 -08001032 private boolean getBoolean(String secureSettingKey, boolean defaultValue) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001033 try {
1034 return getLockSettings().getBoolean(secureSettingKey, defaultValue,
1035 getCurrentOrCallingUserId());
1036 } catch (RemoteException re) {
1037 return defaultValue;
1038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040
Amith Yamasani156c4352010-03-05 17:10:03 -08001041 private void setBoolean(String secureSettingKey, boolean enabled) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001042 try {
1043 getLockSettings().setBoolean(secureSettingKey, enabled, getCurrentOrCallingUserId());
1044 } catch (RemoteException re) {
1045 // What can we do?
1046 Log.e(TAG, "Couldn't write boolean " + secureSettingKey + re);
1047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001050 public int[] getAppWidgets() {
Amith Yamasani8fd96ec2012-09-21 17:48:49 -07001051 String appWidgetIdString = Settings.Secure.getStringForUser(
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001052 mContentResolver, Settings.Secure.LOCK_SCREEN_APPWIDGET_IDS,
Amith Yamasani8fd96ec2012-09-21 17:48:49 -07001053 UserHandle.USER_CURRENT);
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001054 String delims = ",";
Jim Millerf229e4d2012-09-12 20:32:50 -07001055 if (appWidgetIdString != null) {
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001056 String[] appWidgetStringIds = appWidgetIdString.split(delims);
1057 int[] appWidgetIds = new int[appWidgetStringIds.length];
1058 for (int i = 0; i < appWidgetStringIds.length; i++) {
1059 String appWidget = appWidgetStringIds[i];
1060 try {
1061 appWidgetIds[i] = Integer.decode(appWidget);
1062 } catch (NumberFormatException e) {
1063 return null;
1064 }
1065 }
1066 return appWidgetIds;
Jim Millerf229e4d2012-09-12 20:32:50 -07001067 }
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001068 return new int[0];
Jim Millerf229e4d2012-09-12 20:32:50 -07001069 }
1070
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001071 private static String combineStrings(int[] list, String separator) {
1072 int listLength = list.length;
1073
1074 switch (listLength) {
1075 case 0: {
1076 return "";
1077 }
1078 case 1: {
1079 return Integer.toString(list[0]);
1080 }
Michael Jurka20c41d52012-09-20 19:01:06 -07001081 }
1082
Michael Jurkaaa2859a2012-10-24 12:46:49 -07001083 int strLength = 0;
1084 int separatorLength = separator.length();
1085
1086 String[] stringList = new String[list.length];
1087 for (int i = 0; i < listLength; i++) {
1088 stringList[i] = Integer.toString(list[i]);
1089 strLength += stringList[i].length();
1090 if (i < listLength - 1) {
1091 strLength += separatorLength;
1092 }
1093 }
1094
1095 StringBuilder sb = new StringBuilder(strLength);
1096
1097 for (int i = 0; i < listLength; i++) {
1098 sb.append(list[i]);
1099 if (i < listLength - 1) {
1100 sb.append(separator);
1101 }
1102 }
1103
1104 return sb.toString();
1105 }
1106
1107 private void writeAppWidgets(int[] appWidgetIds) {
1108 Settings.Secure.putString(mContentResolver,
1109 Settings.Secure.putStringForUser(mContentResolver,
1110 Settings.Secure.LOCK_SCREEN_APPWIDGET_IDS,
1111 combineStrings(appWidgetIds, ","),
1112 UserHandle.USER_CURRENT);
1113 }
1114
1115 public void addAppWidget(int widgetId, int index) {
1116 int[] widgets = getAppWidgets();
1117 int[] newWidgets = new int[widgets.length + 1];
1118 for (int i = 0, j = 0; i < newWidgets.length; i++) {
1119 if (index == i) {
1120 newWidgets[i] = widgetId;
1121 i++;
1122 }
1123 if (i < newWidgets.length) {
1124 newWidgets[i] = widgets[j];
1125 j++;
1126 }
1127 }
1128 writeAppWidgets(newWidgets);
1129 }
1130
1131 public boolean removeAppWidget(int widgetId, int index) {
1132 int[] widgets = getAppWidgets();
1133 int[] newWidgets = new int[widgets.length - 1];
1134 for (int i = 0, j = 0; i < widgets.length; i++) {
1135 if (index == i) {
1136 if (widgets[i] != widgetId) {
1137 return false;
1138 }
1139 // continue...
1140 } else {
1141 newWidgets[j] = widgets[i];
1142 j++;
1143 }
1144 }
1145 writeAppWidgets(newWidgets);
1146 return true;
Michael Jurka20c41d52012-09-20 19:01:06 -07001147 }
1148
Amith Yamasani52c489c2012-03-28 11:42:42 -07001149 private long getLong(String secureSettingKey, long defaultValue) {
1150 try {
1151 return getLockSettings().getLong(secureSettingKey, defaultValue,
1152 getCurrentOrCallingUserId());
1153 } catch (RemoteException re) {
1154 return defaultValue;
1155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157
Amith Yamasani156c4352010-03-05 17:10:03 -08001158 private void setLong(String secureSettingKey, long value) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001159 setLong(secureSettingKey, value, getCurrentOrCallingUserId());
1160 }
1161
1162 private void setLong(String secureSettingKey, long value, int userHandle) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001163 try {
1164 getLockSettings().setLong(secureSettingKey, value, getCurrentOrCallingUserId());
1165 } catch (RemoteException re) {
1166 // What can we do?
1167 Log.e(TAG, "Couldn't write long " + secureSettingKey + re);
1168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -07001171 private String getString(String secureSettingKey) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001172 return getString(secureSettingKey, getCurrentOrCallingUserId());
1173 }
1174
1175 private String getString(String secureSettingKey, int userHandle) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001176 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001177 return getLockSettings().getString(secureSettingKey, null, userHandle);
Amith Yamasani52c489c2012-03-28 11:42:42 -07001178 } catch (RemoteException re) {
1179 return null;
1180 }
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -07001181 }
1182
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001183 private void setString(String secureSettingKey, String value, int userHandle) {
Amith Yamasani52c489c2012-03-28 11:42:42 -07001184 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001185 getLockSettings().setString(secureSettingKey, value, userHandle);
Amith Yamasani52c489c2012-03-28 11:42:42 -07001186 } catch (RemoteException re) {
1187 // What can we do?
1188 Log.e(TAG, "Couldn't write string " + secureSettingKey + re);
1189 }
Konstantin Lopyrev863f22d2010-05-12 17:16:58 -07001190 }
1191
Jim Miller69aa4a92009-12-22 19:03:28 -08001192 public boolean isSecure() {
Jim Millercd709882010-03-25 18:24:02 -07001193 long mode = getKeyguardStoredPasswordQuality();
1194 final boolean isPattern = mode == DevicePolicyManager.PASSWORD_QUALITY_SOMETHING;
1195 final boolean isPassword = mode == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC
1196 || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001197 || mode == DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC
1198 || mode == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
Jim Millercd709882010-03-25 18:24:02 -07001199 final boolean secure = isPattern && isLockPatternEnabled() && savedPatternExists()
Danielle Millett73da5fe2011-09-13 16:20:05 -04001200 || isPassword && savedPasswordExists();
Jim Miller69aa4a92009-12-22 19:03:28 -08001201 return secure;
1202 }
Jim Miller69ac9882010-02-24 15:35:05 -08001203
1204 /**
John Wang0f7b3f82011-05-31 11:20:55 -07001205 * Sets the emergency button visibility based on isEmergencyCallCapable().
1206 *
1207 * If the emergency button is visible, sets the text on the emergency button
1208 * to indicate what action will be taken.
1209 *
Jim Miller69ac9882010-02-24 15:35:05 -08001210 * If there's currently a call in progress, the button will take them to the call
1211 * @param button the button to update
Jim Miller3f5f83b2011-09-26 15:17:05 -07001212 * @param the phone state:
1213 * {@link TelephonyManager#CALL_STATE_IDLE}
1214 * {@link TelephonyManager#CALL_STATE_RINGING}
1215 * {@link TelephonyManager#CALL_STATE_OFFHOOK}
Jim Miller1f56edc2011-11-07 19:00:48 -08001216 * @param shown indicates whether the given screen wants the emergency button to show at all
Jim Miller109f1fd2012-09-19 20:44:16 -07001217 * @param button
1218 * @param phoneState
1219 * @param shown shown if true; hidden if false
1220 * @param upperCase if true, converts button label string to upper case
Jim Miller69ac9882010-02-24 15:35:05 -08001221 */
Jim Miller109f1fd2012-09-19 20:44:16 -07001222 public void updateEmergencyCallButtonState(Button button, int phoneState, boolean shown,
1223 boolean upperCase, boolean showIcon) {
Jim Miller1f56edc2011-11-07 19:00:48 -08001224 if (isEmergencyCallCapable() && shown) {
John Wang0f7b3f82011-05-31 11:20:55 -07001225 button.setVisibility(View.VISIBLE);
1226 } else {
1227 button.setVisibility(View.GONE);
1228 return;
1229 }
1230
Jim Miller69ac9882010-02-24 15:35:05 -08001231 int textId;
Jim Miller3f5f83b2011-09-26 15:17:05 -07001232 if (phoneState == TelephonyManager.CALL_STATE_OFFHOOK) {
Jim Miller69ac9882010-02-24 15:35:05 -08001233 // show "return to call" text and show phone icon
1234 textId = R.string.lockscreen_return_to_call;
Jim Miller109f1fd2012-09-19 20:44:16 -07001235 int phoneCallIcon = showIcon ? R.drawable.stat_sys_phone_call : 0;
Jim Miller69ac9882010-02-24 15:35:05 -08001236 button.setCompoundDrawablesWithIntrinsicBounds(phoneCallIcon, 0, 0, 0);
1237 } else {
1238 textId = R.string.lockscreen_emergency_call;
Jim Miller109f1fd2012-09-19 20:44:16 -07001239 int emergencyIcon = showIcon ? R.drawable.ic_emergency : 0;
Jim Miller69ac9882010-02-24 15:35:05 -08001240 button.setCompoundDrawablesWithIntrinsicBounds(emergencyIcon, 0, 0, 0);
1241 }
Jim Miller109f1fd2012-09-19 20:44:16 -07001242 if (upperCase) {
1243 CharSequence original = mContext.getResources().getText(textId);
1244 String upper = original != null ? original.toString().toUpperCase() : null;
1245 button.setText(upper);
1246 } else {
1247 button.setText(textId);
1248 }
1249 }
1250
1251 /**
1252 * @deprecated
1253 * @param button
1254 * @param phoneState
1255 * @param shown
1256 */
1257 public void updateEmergencyCallButtonState(Button button, int phoneState, boolean shown) {
1258 updateEmergencyCallButtonState(button, phoneState, shown, false, true);
Jim Miller69ac9882010-02-24 15:35:05 -08001259 }
1260
1261 /**
1262 * Resumes a call in progress. Typically launched from the EmergencyCall button
1263 * on various lockscreens.
1264 *
1265 * @return true if we were able to tell InCallScreen to show.
1266 */
1267 public boolean resumeCall() {
1268 ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
1269 try {
1270 if (phone != null && phone.showCallScreen()) {
1271 return true;
1272 }
1273 } catch (RemoteException e) {
1274 // What can we do?
1275 }
1276 return false;
1277 }
Danielle Millett044a0a72011-11-07 15:42:12 -05001278
1279 private void finishBiometricWeak() {
1280 setBoolean(BIOMETRIC_WEAK_EVER_CHOSEN_KEY, true);
1281
1282 // Launch intent to show final screen, this also
1283 // moves the temporary gallery to the actual gallery
1284 Intent intent = new Intent();
1285 intent.setClassName("com.android.facelock",
1286 "com.android.facelock.SetupEndScreen");
1287 mContext.startActivity(intent);
1288 }
1289
Jim Millera4edd152012-01-06 18:24:04 -08001290 public void setPowerButtonInstantlyLocks(boolean enabled) {
1291 setBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, enabled);
1292 }
1293
1294 public boolean getPowerButtonInstantlyLocks() {
1295 return getBoolean(LOCKSCREEN_POWER_BUTTON_INSTANTLY_LOCKS, true);
1296 }
1297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298}