blob: f36c56f60965b5bd4c2aa7f5cefbb47089212c30 [file] [log] [blame]
Jorim Jaggiecc798e2014-05-26 18:14:37 +02001/*
2 * Copyright (C) 2014 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.systemui.statusbar.phone;
18
Selim Cinek6f0a62a2019-04-09 18:40:12 -070019import android.content.BroadcastReceiver;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020020import android.content.Context;
Selim Cinek6f0a62a2019-04-09 18:40:12 -070021import android.content.Intent;
22import android.content.IntentFilter;
Gus Prevasab336792018-11-14 13:52:20 -050023import android.hardware.biometrics.BiometricSourceType;
Selim Cinek6f0a62a2019-04-09 18:40:12 -070024import android.os.Build;
Nick Desaulniers1d396752016-07-25 15:05:33 -070025import android.os.Trace;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020026
27import com.android.internal.widget.LockPatternUtils;
28import com.android.keyguard.KeyguardUpdateMonitor;
29import com.android.keyguard.KeyguardUpdateMonitorCallback;
30
31import java.util.ArrayList;
32
33/**
34 * Caches whether the current unlock method is insecure, taking trust into account. This information
35 * might be a little bit out of date and should not be used for actual security decisions; it should
36 * be only used for visual indications.
37 */
38public class UnlockMethodCache {
39
40 private static UnlockMethodCache sInstance;
Selim Cinek6f0a62a2019-04-09 18:40:12 -070041 private static final boolean DEBUG_AUTH_WITH_ADB = false;
42 private static final String AUTH_BROADCAST_KEY = "debug_trigger_auth";
Jorim Jaggiecc798e2014-05-26 18:14:37 +020043
44 private final LockPatternUtils mLockPatternUtils;
45 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
46 private final ArrayList<OnUnlockMethodChangedListener> mListeners = new ArrayList<>();
Christoph Studer2231c6e2014-12-19 12:40:13 +010047 /** Whether the user configured a secure unlock method (PIN, password, etc.) */
48 private boolean mSecure;
49 /** Whether the unlock method is currently insecure (insecure method or trusted environment) */
Selim Cineke8bae622015-07-15 13:24:06 -070050 private boolean mCanSkipBouncer;
Adrian Roos7861c662014-07-25 15:37:28 +020051 private boolean mTrustManaged;
Selim Cineke8bae622015-07-15 13:24:06 -070052 private boolean mTrusted;
Selim Cinek6f0a62a2019-04-09 18:40:12 -070053 private boolean mDebugUnlocked = false;
Lucas Dupin206fe562019-05-31 14:36:42 -070054 private boolean mIsUnlockingWithFacePossible;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020055
56 private UnlockMethodCache(Context ctx) {
57 mLockPatternUtils = new LockPatternUtils(ctx);
58 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
59 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
Christoph Studer2231c6e2014-12-19 12:40:13 +010060 update(true /* updateAlways */);
Selim Cinek6f0a62a2019-04-09 18:40:12 -070061 if (Build.IS_DEBUGGABLE && DEBUG_AUTH_WITH_ADB) {
62 // Watch for interesting updates
63 final IntentFilter filter = new IntentFilter();
64 filter.addAction(AUTH_BROADCAST_KEY);
65 ctx.registerReceiver(new BroadcastReceiver() {
66 @Override
67 public void onReceive(Context context, Intent intent) {
68 if (DEBUG_AUTH_WITH_ADB && AUTH_BROADCAST_KEY.equals(intent.getAction())) {
69 mDebugUnlocked = !mDebugUnlocked;
70 update(true /* updateAlways */);
71 }
72 }
73 }, filter, null, null);
74 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +020075 }
76
77 public static UnlockMethodCache getInstance(Context context) {
78 if (sInstance == null) {
79 sInstance = new UnlockMethodCache(context);
80 }
81 return sInstance;
82 }
83
84 /**
Christoph Studer2231c6e2014-12-19 12:40:13 +010085 * @return whether the user configured a secure unlock method like PIN, password, etc.
Jorim Jaggiecc798e2014-05-26 18:14:37 +020086 */
Christoph Studer2231c6e2014-12-19 12:40:13 +010087 public boolean isMethodSecure() {
88 return mSecure;
89 }
90
Selim Cineke8bae622015-07-15 13:24:06 -070091 public boolean isTrusted() {
92 return mTrusted;
93 }
94
Christoph Studer2231c6e2014-12-19 12:40:13 +010095 /**
Selim Cineke8bae622015-07-15 13:24:06 -070096 * @return whether the lockscreen is currently insecure, and the bouncer won't be shown
Christoph Studer2231c6e2014-12-19 12:40:13 +010097 */
Selim Cineke8bae622015-07-15 13:24:06 -070098 public boolean canSkipBouncer() {
99 return mCanSkipBouncer;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200100 }
101
102 public void addListener(OnUnlockMethodChangedListener listener) {
103 mListeners.add(listener);
104 }
105
106 public void removeListener(OnUnlockMethodChangedListener listener) {
107 mListeners.remove(listener);
108 }
109
Lucas Dupin206fe562019-05-31 14:36:42 -0700110 public boolean isUnlockingWithFacePossible() {
111 return mIsUnlockingWithFacePossible;
112 }
113
Christoph Studer2231c6e2014-12-19 12:40:13 +0100114 private void update(boolean updateAlways) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700115 Trace.beginSection("UnlockMethodCache#update");
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700116 int user = KeyguardUpdateMonitor.getCurrentUser();
Adrian Roos8150d2a2015-04-16 17:11:20 -0700117 boolean secure = mLockPatternUtils.isSecure(user);
Selim Cinek6f0a62a2019-04-09 18:40:12 -0700118 boolean canSkipBouncer = !secure || mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)
119 || (Build.IS_DEBUGGABLE && DEBUG_AUTH_WITH_ADB && mDebugUnlocked);
Adrian Roos7861c662014-07-25 15:37:28 +0200120 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
Selim Cineke8bae622015-07-15 13:24:06 -0700121 boolean trusted = mKeyguardUpdateMonitor.getUserHasTrust(user);
Lucas Dupin206fe562019-05-31 14:36:42 -0700122 boolean hasEnrolledFaces = mKeyguardUpdateMonitor.isUnlockWithFacePossible(user);
Selim Cineke8bae622015-07-15 13:24:06 -0700123 boolean changed = secure != mSecure || canSkipBouncer != mCanSkipBouncer ||
Lucas Dupin206fe562019-05-31 14:36:42 -0700124 trustManaged != mTrustManaged || mIsUnlockingWithFacePossible != hasEnrolledFaces;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200125 if (changed || updateAlways) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100126 mSecure = secure;
Selim Cineke8bae622015-07-15 13:24:06 -0700127 mCanSkipBouncer = canSkipBouncer;
128 mTrusted = trusted;
Adrian Roos7861c662014-07-25 15:37:28 +0200129 mTrustManaged = trustManaged;
Lucas Dupin206fe562019-05-31 14:36:42 -0700130 mIsUnlockingWithFacePossible = hasEnrolledFaces;
Christoph Studer2231c6e2014-12-19 12:40:13 +0100131 notifyListeners();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200132 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700133 Trace.endSection();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200134 }
135
Christoph Studer2231c6e2014-12-19 12:40:13 +0100136 private void notifyListeners() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200137 for (OnUnlockMethodChangedListener listener : mListeners) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100138 listener.onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200139 }
140 }
141
142 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
143 @Override
144 public void onUserSwitchComplete(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100145 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200146 }
147
148 @Override
149 public void onTrustChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100150 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200151 }
152
153 @Override
Adrian Roos7861c662014-07-25 15:37:28 +0200154 public void onTrustManagedChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100155 update(false /* updateAlways */);
Adrian Roos7861c662014-07-25 15:37:28 +0200156 }
157
158 @Override
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700159 public void onStartedWakingUp() {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100160 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200161 }
Jim Millerf41fc962014-06-18 16:33:51 -0700162
Adrian Roos7861c662014-07-25 15:37:28 +0200163 @Override
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200164 public void onBiometricAuthenticated(int userId, BiometricSourceType biometricSourceType) {
165 Trace.beginSection("KeyguardUpdateMonitorCallback#onBiometricAuthenticated");
166 if (!mKeyguardUpdateMonitor.isUnlockingWithBiometricAllowed()) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700167 Trace.endSection();
Selim Cinek1fcafc42015-07-20 14:39:25 -0700168 return;
169 }
Christoph Studer2231c6e2014-12-19 12:40:13 +0100170 update(false /* updateAlways */);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700171 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700172 }
Adrian Roos4a410172014-08-20 17:41:44 +0200173
174 @Override
175 public void onFaceUnlockStateChanged(boolean running, int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100176 update(false /* updateAlways */);
Adrian Roos4a410172014-08-20 17:41:44 +0200177 }
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700178
179 @Override
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700180 public void onStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700181 update(false /* updateAlways */);
182 }
Kevin Chyn08ae6642017-08-30 19:02:43 -0700183
184 @Override
185 public void onScreenTurnedOff() {
186 update(false /* updateAlways */);
187 }
Kevin Chyn94f80352017-09-08 17:39:01 -0700188
189 @Override
190 public void onKeyguardVisibilityChanged(boolean showing) {
191 update(false /* updateAlways */);
192 }
Lucas Dupinad2577e2019-06-25 19:52:26 -0700193
194 @Override
195 public void onBiometricsCleared() {
196 update(false /* alwaysUpdate */);
197 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200198 };
199
Adrian Roos7861c662014-07-25 15:37:28 +0200200 public boolean isTrustManaged() {
201 return mTrustManaged;
202 }
203
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200204 public static interface OnUnlockMethodChangedListener {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100205 void onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200206 }
207}