blob: 2d492db93e0b6e2801dc148557fcea344e08a2b5 [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 {
Adrian Roos46842d92014-03-27 14:58:03 +010026
Jim Millerdcb3d842012-08-23 19:18:12 -070027 /**
28 * The different types of security available for {@link Mode#UnlockScreen}.
29 * @see com.android.internal.policy.impl.LockPatternKeyguardView#getUnlockMode()
30 */
Jorim Jaggia005f1b2014-04-16 19:06:10 +020031 public enum SecurityMode {
Jim Miller63f9b812012-10-15 15:58:01 -070032 Invalid, // NULL state
Jim Millerdcb3d842012-08-23 19:18:12 -070033 None, // No security enabled
34 Pattern, // Unlock by drawing a pattern.
Daniel Sandler69bdee72012-10-23 16:45:50 -040035 Password, // Unlock by entering an alphanumeric password
36 PIN, // Strictly numeric password
Jim Millerdcb3d842012-08-23 19:18:12 -070037 Biometric, // Unlock with a biometric key (e.g. finger print or face unlock)
38 Account, // Unlock by entering an account's login and password.
39 SimPin, // Unlock by entering a sim pin.
40 SimPuk // Unlock by entering a sim puk
41 }
42
43 private Context mContext;
44 private LockPatternUtils mLockPatternUtils;
45
46 KeyguardSecurityModel(Context context) {
47 mContext = context;
48 mLockPatternUtils = new LockPatternUtils(context);
49 }
50
51 void setLockPatternUtils(LockPatternUtils utils) {
52 mLockPatternUtils = utils;
53 }
54
Jim Miller258341c2012-08-30 16:50:10 -070055 /**
Brian Colonna9ded0e12012-10-08 13:02:41 -040056 * Returns true if biometric unlock is installed and selected. If this returns false there is
57 * no need to even construct the biometric unlock.
Jim Miller258341c2012-08-30 16:50:10 -070058 */
Jim Miller63f9b812012-10-15 15:58:01 -070059 boolean isBiometricUnlockEnabled() {
Jim Miller258341c2012-08-30 16:50:10 -070060 return mLockPatternUtils.usingBiometricWeak()
Brian Colonna9ded0e12012-10-08 13:02:41 -040061 && mLockPatternUtils.isBiometricWeakInstalled();
62 }
63
64 /**
65 * Returns true if a condition is currently suppressing the biometric unlock. If this returns
66 * true there is no need to even construct the biometric unlock.
67 */
68 private boolean isBiometricUnlockSuppressed() {
69 KeyguardUpdateMonitor monitor = KeyguardUpdateMonitor.getInstance(mContext);
70 final boolean backupIsTimedOut = monitor.getFailedUnlockAttempts() >=
71 LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT;
Brian Colonnacc4104f2012-10-09 17:50:46 -040072 return monitor.getMaxBiometricUnlockAttemptsReached() || backupIsTimedOut
Danielle Millettd95c6592012-10-12 14:55:44 -040073 || !monitor.isAlternateUnlockEnabled()
74 || monitor.getPhoneState() != TelephonyManager.CALL_STATE_IDLE;
Jim Miller258341c2012-08-30 16:50:10 -070075 }
76
Jim Millerdcb3d842012-08-23 19:18:12 -070077 SecurityMode getSecurityMode() {
Jim Miller109f1fd2012-09-19 20:44:16 -070078 KeyguardUpdateMonitor updateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
79 final IccCardConstants.State simState = updateMonitor.getSimState();
Jim Miller258341c2012-08-30 16:50:10 -070080 SecurityMode mode = SecurityMode.None;
Jim Millerdcb3d842012-08-23 19:18:12 -070081 if (simState == IccCardConstants.State.PIN_REQUIRED) {
Jim Miller258341c2012-08-30 16:50:10 -070082 mode = SecurityMode.SimPin;
Jim Miller47df44a2012-09-06 17:51:12 -070083 } else if (simState == IccCardConstants.State.PUK_REQUIRED
84 && mLockPatternUtils.isPukUnlockScreenEnable()) {
Jim Miller258341c2012-08-30 16:50:10 -070085 mode = SecurityMode.SimPuk;
Adrian Roos46842d92014-03-27 14:58:03 +010086 } else if (updateMonitor.getUserHasTrust(mLockPatternUtils.getCurrentUser())) {
87 mode = SecurityMode.None;
Jim Millerdcb3d842012-08-23 19:18:12 -070088 } else {
Jim Miller258341c2012-08-30 16:50:10 -070089 final int security = mLockPatternUtils.getKeyguardStoredPasswordQuality();
90 switch (security) {
Jim Millerdcb3d842012-08-23 19:18:12 -070091 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
Daniel Sandler69bdee72012-10-23 16:45:50 -040092 mode = mLockPatternUtils.isLockPasswordEnabled() ?
93 SecurityMode.PIN : SecurityMode.None;
94 break;
Jim Millerdcb3d842012-08-23 19:18:12 -070095 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
96 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
97 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
Jim Miller258341c2012-08-30 16:50:10 -070098 mode = mLockPatternUtils.isLockPasswordEnabled() ?
Jim Millerdcb3d842012-08-23 19:18:12 -070099 SecurityMode.Password : SecurityMode.None;
Jim Miller258341c2012-08-30 16:50:10 -0700100 break;
Jim Millerdcb3d842012-08-23 19:18:12 -0700101
102 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
103 case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED:
104 if (mLockPatternUtils.isLockPatternEnabled()) {
Jim Miller258341c2012-08-30 16:50:10 -0700105 mode = mLockPatternUtils.isPermanentlyLocked() ?
Jim Millerdcb3d842012-08-23 19:18:12 -0700106 SecurityMode.Account : SecurityMode.Pattern;
Jim Millerdcb3d842012-08-23 19:18:12 -0700107 }
Jim Miller258341c2012-08-30 16:50:10 -0700108 break;
109
Jim Millerdcb3d842012-08-23 19:18:12 -0700110 default:
Jim Miller5e612cf2014-02-03 17:57:23 -0800111 throw new IllegalStateException("Unknown security quality:" + security);
Jim Millerdcb3d842012-08-23 19:18:12 -0700112 }
113 }
Jim Miller258341c2012-08-30 16:50:10 -0700114 return mode;
Jim Millerdcb3d842012-08-23 19:18:12 -0700115 }
116
Jim Miller258341c2012-08-30 16:50:10 -0700117 /**
118 * Some unlock methods can have an alternate, such as biometric unlocks (e.g. face unlock).
119 * This function decides if an alternate unlock is available and returns it. Otherwise,
120 * returns @param mode.
121 *
122 * @param mode the mode we want the alternate for
123 * @return alternate or the given mode
124 */
125 SecurityMode getAlternateFor(SecurityMode mode) {
Brian Colonna9ded0e12012-10-08 13:02:41 -0400126 if (isBiometricUnlockEnabled() && !isBiometricUnlockSuppressed()
Daniel Sandler69bdee72012-10-23 16:45:50 -0400127 && (mode == SecurityMode.Password
128 || mode == SecurityMode.PIN
129 || mode == SecurityMode.Pattern)) {
Jim Miller258341c2012-08-30 16:50:10 -0700130 return SecurityMode.Biometric;
131 }
132 return mode; // no alternate, return what was given
133 }
134
135 /**
136 * Some unlock methods can have a backup which gives the user another way to get into
137 * the device. This is currently only supported for Biometric and Pattern unlock.
138 *
Brian Colonna9ded0e12012-10-08 13:02:41 -0400139 * @return backup method or current security mode
Jim Miller258341c2012-08-30 16:50:10 -0700140 */
Jim Miller63f9b812012-10-15 15:58:01 -0700141 SecurityMode getBackupSecurityMode(SecurityMode mode) {
Jim Miller258341c2012-08-30 16:50:10 -0700142 switch(mode) {
143 case Biometric:
144 return getSecurityMode();
145 case Pattern:
146 return SecurityMode.Account;
147 }
Brian Colonna9ded0e12012-10-08 13:02:41 -0400148 return mode; // no backup, return current security mode
Jim Millerdcb3d842012-08-23 19:18:12 -0700149 }
150}