blob: b1d6ca6e5580c359aa1a6fc47baa46593ca4cbbd [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
Lucas Dupin4c507042019-07-22 16:47:34 -070031import java.io.PrintWriter;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020032import java.util.ArrayList;
33
34/**
35 * Caches whether the current unlock method is insecure, taking trust into account. This information
36 * might be a little bit out of date and should not be used for actual security decisions; it should
37 * be only used for visual indications.
38 */
39public class UnlockMethodCache {
40
41 private static UnlockMethodCache sInstance;
Selim Cinek6f0a62a2019-04-09 18:40:12 -070042 private static final boolean DEBUG_AUTH_WITH_ADB = false;
43 private static final String AUTH_BROADCAST_KEY = "debug_trigger_auth";
Jorim Jaggiecc798e2014-05-26 18:14:37 +020044
45 private final LockPatternUtils mLockPatternUtils;
46 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
47 private final ArrayList<OnUnlockMethodChangedListener> mListeners = new ArrayList<>();
Christoph Studer2231c6e2014-12-19 12:40:13 +010048 /** Whether the user configured a secure unlock method (PIN, password, etc.) */
49 private boolean mSecure;
50 /** Whether the unlock method is currently insecure (insecure method or trusted environment) */
Selim Cineke8bae622015-07-15 13:24:06 -070051 private boolean mCanSkipBouncer;
Adrian Roos7861c662014-07-25 15:37:28 +020052 private boolean mTrustManaged;
Selim Cineke8bae622015-07-15 13:24:06 -070053 private boolean mTrusted;
Selim Cinek6f0a62a2019-04-09 18:40:12 -070054 private boolean mDebugUnlocked = false;
Lucas Dupin4c507042019-07-22 16:47:34 -070055 private boolean mFaceAuthEnabled;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020056
57 private UnlockMethodCache(Context ctx) {
58 mLockPatternUtils = new LockPatternUtils(ctx);
59 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
60 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
Christoph Studer2231c6e2014-12-19 12:40:13 +010061 update(true /* updateAlways */);
Selim Cinek6f0a62a2019-04-09 18:40:12 -070062 if (Build.IS_DEBUGGABLE && DEBUG_AUTH_WITH_ADB) {
63 // Watch for interesting updates
64 final IntentFilter filter = new IntentFilter();
65 filter.addAction(AUTH_BROADCAST_KEY);
66 ctx.registerReceiver(new BroadcastReceiver() {
67 @Override
68 public void onReceive(Context context, Intent intent) {
69 if (DEBUG_AUTH_WITH_ADB && AUTH_BROADCAST_KEY.equals(intent.getAction())) {
70 mDebugUnlocked = !mDebugUnlocked;
71 update(true /* updateAlways */);
72 }
73 }
74 }, filter, null, null);
75 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +020076 }
77
78 public static UnlockMethodCache getInstance(Context context) {
79 if (sInstance == null) {
80 sInstance = new UnlockMethodCache(context);
81 }
82 return sInstance;
83 }
84
85 /**
Christoph Studer2231c6e2014-12-19 12:40:13 +010086 * @return whether the user configured a secure unlock method like PIN, password, etc.
Jorim Jaggiecc798e2014-05-26 18:14:37 +020087 */
Christoph Studer2231c6e2014-12-19 12:40:13 +010088 public boolean isMethodSecure() {
89 return mSecure;
90 }
91
Selim Cineke8bae622015-07-15 13:24:06 -070092 public boolean isTrusted() {
93 return mTrusted;
94 }
95
Christoph Studer2231c6e2014-12-19 12:40:13 +010096 /**
Selim Cineke8bae622015-07-15 13:24:06 -070097 * @return whether the lockscreen is currently insecure, and the bouncer won't be shown
Christoph Studer2231c6e2014-12-19 12:40:13 +010098 */
Selim Cineke8bae622015-07-15 13:24:06 -070099 public boolean canSkipBouncer() {
100 return mCanSkipBouncer;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200101 }
102
103 public void addListener(OnUnlockMethodChangedListener listener) {
104 mListeners.add(listener);
105 }
106
107 public void removeListener(OnUnlockMethodChangedListener listener) {
108 mListeners.remove(listener);
109 }
110
Lucas Dupin7d95f152019-07-17 16:25:54 -0700111 /**
112 * If there are faces enrolled and user enabled face auth on keyguard.
113 */
Lucas Dupin4c507042019-07-22 16:47:34 -0700114 public boolean isFaceAuthEnabled() {
115 return mFaceAuthEnabled;
Lucas Dupin206fe562019-05-31 14:36:42 -0700116 }
117
Christoph Studer2231c6e2014-12-19 12:40:13 +0100118 private void update(boolean updateAlways) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700119 Trace.beginSection("UnlockMethodCache#update");
Adrian Roosd6aa6cb2015-04-16 19:31:29 -0700120 int user = KeyguardUpdateMonitor.getCurrentUser();
Adrian Roos8150d2a2015-04-16 17:11:20 -0700121 boolean secure = mLockPatternUtils.isSecure(user);
Selim Cinek6f0a62a2019-04-09 18:40:12 -0700122 boolean canSkipBouncer = !secure || mKeyguardUpdateMonitor.getUserCanSkipBouncer(user)
123 || (Build.IS_DEBUGGABLE && DEBUG_AUTH_WITH_ADB && mDebugUnlocked);
Adrian Roos7861c662014-07-25 15:37:28 +0200124 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
Selim Cineke8bae622015-07-15 13:24:06 -0700125 boolean trusted = mKeyguardUpdateMonitor.getUserHasTrust(user);
Lucas Dupin4c507042019-07-22 16:47:34 -0700126 boolean faceAuthEnabled = mKeyguardUpdateMonitor.isFaceAuthEnabledForUser(user);
Lucas Dupin7d95f152019-07-17 16:25:54 -0700127 boolean changed = secure != mSecure || canSkipBouncer != mCanSkipBouncer
128 || trustManaged != mTrustManaged
Lucas Dupin4c507042019-07-22 16:47:34 -0700129 || mFaceAuthEnabled != faceAuthEnabled;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200130 if (changed || updateAlways) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100131 mSecure = secure;
Selim Cineke8bae622015-07-15 13:24:06 -0700132 mCanSkipBouncer = canSkipBouncer;
133 mTrusted = trusted;
Adrian Roos7861c662014-07-25 15:37:28 +0200134 mTrustManaged = trustManaged;
Lucas Dupin4c507042019-07-22 16:47:34 -0700135 mFaceAuthEnabled = faceAuthEnabled;
Christoph Studer2231c6e2014-12-19 12:40:13 +0100136 notifyListeners();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200137 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700138 Trace.endSection();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200139 }
140
Christoph Studer2231c6e2014-12-19 12:40:13 +0100141 private void notifyListeners() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200142 for (OnUnlockMethodChangedListener listener : mListeners) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100143 listener.onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200144 }
145 }
146
Lucas Dupin4c507042019-07-22 16:47:34 -0700147 public void dump(PrintWriter pw) {
148 pw.println("UnlockMethodCache");
149 pw.println(" mSecure: " + mSecure);
150 pw.println(" mCanSkipBouncer: " + mCanSkipBouncer);
151 pw.println(" mTrustManaged: " + mTrustManaged);
152 pw.println(" mTrusted: " + mTrusted);
153 pw.println(" mDebugUnlocked: " + mDebugUnlocked);
154 pw.println(" mFaceAuthEnabled: " + mFaceAuthEnabled);
155 }
156
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200157 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
158 @Override
159 public void onUserSwitchComplete(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100160 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200161 }
162
163 @Override
164 public void onTrustChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100165 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200166 }
167
168 @Override
Adrian Roos7861c662014-07-25 15:37:28 +0200169 public void onTrustManagedChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100170 update(false /* updateAlways */);
Adrian Roos7861c662014-07-25 15:37:28 +0200171 }
172
173 @Override
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700174 public void onStartedWakingUp() {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100175 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200176 }
Jim Millerf41fc962014-06-18 16:33:51 -0700177
Adrian Roos7861c662014-07-25 15:37:28 +0200178 @Override
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200179 public void onBiometricAuthenticated(int userId, BiometricSourceType biometricSourceType) {
180 Trace.beginSection("KeyguardUpdateMonitorCallback#onBiometricAuthenticated");
181 if (!mKeyguardUpdateMonitor.isUnlockingWithBiometricAllowed()) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700182 Trace.endSection();
Selim Cinek1fcafc42015-07-20 14:39:25 -0700183 return;
184 }
Christoph Studer2231c6e2014-12-19 12:40:13 +0100185 update(false /* updateAlways */);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700186 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700187 }
Adrian Roos4a410172014-08-20 17:41:44 +0200188
189 @Override
190 public void onFaceUnlockStateChanged(boolean running, int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100191 update(false /* updateAlways */);
Adrian Roos4a410172014-08-20 17:41:44 +0200192 }
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700193
194 @Override
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700195 public void onStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700196 update(false /* updateAlways */);
197 }
Kevin Chyn08ae6642017-08-30 19:02:43 -0700198
199 @Override
200 public void onScreenTurnedOff() {
201 update(false /* updateAlways */);
202 }
Kevin Chyn94f80352017-09-08 17:39:01 -0700203
204 @Override
205 public void onKeyguardVisibilityChanged(boolean showing) {
206 update(false /* updateAlways */);
207 }
Lucas Dupinad2577e2019-06-25 19:52:26 -0700208
209 @Override
210 public void onBiometricsCleared() {
211 update(false /* alwaysUpdate */);
212 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200213 };
214
Adrian Roos7861c662014-07-25 15:37:28 +0200215 public boolean isTrustManaged() {
216 return mTrustManaged;
217 }
218
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200219 public static interface OnUnlockMethodChangedListener {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100220 void onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200221 }
222}