blob: 4129e33fb07bbde4e5e7e3172c34ff96b39f63ea [file] [log] [blame]
Jim Millerdcb3d842012-08-23 19:18:12 -07001/*
2 * Copyright (C) 2012 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 */
Jim Miller5ecd8112013-01-09 18:50:26 -080016package com.android.keyguard;
Jim Millerdcb3d842012-08-23 19:18:12 -070017
18import android.app.admin.DevicePolicyManager;
19import android.content.Context;
Danielle Millettd95c6592012-10-12 14:55:44 -040020import android.telephony.TelephonyManager;
Jim Millerdcb3d842012-08-23 19:18:12 -070021
22import com.android.internal.telephony.IccCardConstants;
23import com.android.internal.widget.LockPatternUtils;
24
25public class KeyguardSecurityModel {
26 /**
27 * The different types of security available for {@link Mode#UnlockScreen}.
28 * @see com.android.internal.policy.impl.LockPatternKeyguardView#getUnlockMode()
29 */
30 enum SecurityMode {
Jim Miller63f9b812012-10-15 15:58:01 -070031 Invalid, // NULL state
Jim Millerdcb3d842012-08-23 19:18:12 -070032 None, // No security enabled
33 Pattern, // Unlock by drawing a pattern.
Daniel Sandler69bdee72012-10-23 16:45:50 -040034 Password, // Unlock by entering an alphanumeric password
35 PIN, // Strictly numeric password
Jim Millerdcb3d842012-08-23 19:18:12 -070036 Biometric, // Unlock with a biometric key (e.g. finger print or face unlock)
37 Account, // Unlock by entering an account's login and password.
38 SimPin, // Unlock by entering a sim pin.
39 SimPuk // Unlock by entering a sim puk
40 }
41
42 private Context mContext;
43 private LockPatternUtils mLockPatternUtils;
44
45 KeyguardSecurityModel(Context context) {
46 mContext = context;
47 mLockPatternUtils = new LockPatternUtils(context);
48 }
49
50 void setLockPatternUtils(LockPatternUtils utils) {
51 mLockPatternUtils = utils;
52 }
53
Jim Miller258341c2012-08-30 16:50:10 -070054 /**
Brian Colonna9ded0e12012-10-08 13:02:41 -040055 * Returns true if biometric unlock is installed and selected. If this returns false there is
56 * no need to even construct the biometric unlock.
Jim Miller258341c2012-08-30 16:50:10 -070057 */
Jim Miller63f9b812012-10-15 15:58:01 -070058 boolean isBiometricUnlockEnabled() {
Jim Miller258341c2012-08-30 16:50:10 -070059 return mLockPatternUtils.usingBiometricWeak()
Brian Colonna9ded0e12012-10-08 13:02:41 -040060 && mLockPatternUtils.isBiometricWeakInstalled();
61 }
62
63 /**
64 * Returns true if a condition is currently suppressing the biometric unlock. If this returns
65 * true there is no need to even construct the biometric unlock.
66 */
67 private boolean isBiometricUnlockSuppressed() {
68 KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
69 final boolean backupIsTimedOut = monitor.getFailedUnlockAttempts() >=
70 LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT;
Brian Colonnacc4104f2012-10-09 17:50:46 -040071 return monitor.getMaxBiometricUnlockAttemptsReached() || backupIsTimedOut
Danielle Millettd95c6592012-10-12 14:55:44 -040072 || !monitor.isAlternateUnlockEnabled()
73 || monitor.getPhoneState() != TelephonyManager.CALL_STATE_IDLE;
Jim Miller258341c2012-08-30 16:50:10 -070074 }
75
Jim Millerdcb3d842012-08-23 19:18:12 -070076 SecurityMode getSecurityMode() {
Jim Miller109f1fd2012-09-19 20:44:16 -070077 KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
78 final IccCardConstants.State simState = updateMonitor.getSimState();
Jim Miller258341c2012-08-30 16:50:10 -070079 SecurityMode mode = SecurityMode.None;
Jim Millerdcb3d842012-08-23 19:18:12 -070080 if (simState == IccCardConstants.State.PIN_REQUIRED) {
Jim Miller258341c2012-08-30 16:50:10 -070081 mode = SecurityMode.SimPin;
Jim Miller47df44a2012-09-06 17:51:12 -070082 } else if (simState == IccCardConstants.State.PUK_REQUIRED
83 && mLockPatternUtils.isPukUnlockScreenEnable()) {
Jim Miller258341c2012-08-30 16:50:10 -070084 mode = SecurityMode.SimPuk;
Jim Millerdcb3d842012-08-23 19:18:12 -070085 } else {
Jim Miller258341c2012-08-30 16:50:10 -070086 final int security = mLockPatternUtils.getKeyguardStoredPasswordQuality();
87 switch (security) {
Jim Millerdcb3d842012-08-23 19:18:12 -070088 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
Daniel Sandler69bdee72012-10-23 16:45:50 -040089 mode = mLockPatternUtils.isLockPasswordEnabled() ?
90 SecurityMode.PIN : SecurityMode.None;
91 break;
Jim Millerdcb3d842012-08-23 19:18:12 -070092 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
93 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
94 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
Jim Miller258341c2012-08-30 16:50:10 -070095 mode = mLockPatternUtils.isLockPasswordEnabled() ?
Jim Millerdcb3d842012-08-23 19:18:12 -070096 SecurityMode.Password : SecurityMode.None;
Jim Miller258341c2012-08-30 16:50:10 -070097 break;
Jim Millerdcb3d842012-08-23 19:18:12 -070098
99 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
100 case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED:
101 if (mLockPatternUtils.isLockPatternEnabled()) {
Jim Miller258341c2012-08-30 16:50:10 -0700102 mode = mLockPatternUtils.isPermanentlyLocked() ?
Jim Millerdcb3d842012-08-23 19:18:12 -0700103 SecurityMode.Account : SecurityMode.Pattern;
Jim Millerdcb3d842012-08-23 19:18:12 -0700104 }
Jim Miller258341c2012-08-30 16:50:10 -0700105 break;
106
Jim Millerdcb3d842012-08-23 19:18:12 -0700107 default:
Jim Miller258341c2012-08-30 16:50:10 -0700108 throw new IllegalStateException("Unknown unlock mode:" + mode);
Jim Millerdcb3d842012-08-23 19:18:12 -0700109 }
110 }
Jim Miller258341c2012-08-30 16:50:10 -0700111 return mode;
Jim Millerdcb3d842012-08-23 19:18:12 -0700112 }
113
Jim Miller258341c2012-08-30 16:50:10 -0700114 /**
115 * Some unlock methods can have an alternate, such as biometric unlocks (e.g. face unlock).
116 * This function decides if an alternate unlock is available and returns it. Otherwise,
117 * returns @param mode.
118 *
119 * @param mode the mode we want the alternate for
120 * @return alternate or the given mode
121 */
122 SecurityMode getAlternateFor(SecurityMode mode) {
Brian Colonna9ded0e12012-10-08 13:02:41 -0400123 if (isBiometricUnlockEnabled() && !isBiometricUnlockSuppressed()
Daniel Sandler69bdee72012-10-23 16:45:50 -0400124 && (mode == SecurityMode.Password
125 || mode == SecurityMode.PIN
126 || mode == SecurityMode.Pattern)) {
Jim Miller258341c2012-08-30 16:50:10 -0700127 return SecurityMode.Biometric;
128 }
129 return mode; // no alternate, return what was given
130 }
131
132 /**
133 * Some unlock methods can have a backup which gives the user another way to get into
134 * the device. This is currently only supported for Biometric and Pattern unlock.
135 *
Brian Colonna9ded0e12012-10-08 13:02:41 -0400136 * @return backup method or current security mode
Jim Miller258341c2012-08-30 16:50:10 -0700137 */
Jim Miller63f9b812012-10-15 15:58:01 -0700138 SecurityMode getBackupSecurityMode(SecurityMode mode) {
Jim Miller258341c2012-08-30 16:50:10 -0700139 switch(mode) {
140 case Biometric:
141 return getSecurityMode();
142 case Pattern:
143 return SecurityMode.Account;
144 }
Brian Colonna9ded0e12012-10-08 13:02:41 -0400145 return mode; // no backup, return current security mode
Jim Millerdcb3d842012-08-23 19:18:12 -0700146 }
147}