blob: e5925f07017fdfa168db35c6327f1a1c382ba1b8 [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
Gilad Brettercb51b8b2018-03-22 17:04:51 +020019import android.hardware.biometrics.BiometricSourceType;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020020import android.content.Context;
Nick Desaulniers1d396752016-07-25 15:05:33 -070021import android.os.Trace;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020022
23import com.android.internal.widget.LockPatternUtils;
24import com.android.keyguard.KeyguardUpdateMonitor;
25import com.android.keyguard.KeyguardUpdateMonitorCallback;
26
27import java.util.ArrayList;
28
29/**
30 * Caches whether the current unlock method is insecure, taking trust into account. This information
31 * might be a little bit out of date and should not be used for actual security decisions; it should
32 * be only used for visual indications.
33 */
34public class UnlockMethodCache {
35
36 private static UnlockMethodCache sInstance;
37
38 private final LockPatternUtils mLockPatternUtils;
39 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
40 private final ArrayList<OnUnlockMethodChangedListener> mListeners = new ArrayList<>();
Christoph Studer2231c6e2014-12-19 12:40:13 +010041 /** Whether the user configured a secure unlock method (PIN, password, etc.) */
42 private boolean mSecure;
43 /** Whether the unlock method is currently insecure (insecure method or trusted environment) */
Selim Cineke8bae622015-07-15 13:24:06 -070044 private boolean mCanSkipBouncer;
Adrian Roos7861c662014-07-25 15:37:28 +020045 private boolean mTrustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +020046 private boolean mFaceUnlockRunning;
Selim Cineke8bae622015-07-15 13:24:06 -070047 private boolean mTrusted;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020048
49 private UnlockMethodCache(Context ctx) {
50 mLockPatternUtils = new LockPatternUtils(ctx);
51 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
52 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
Christoph Studer2231c6e2014-12-19 12:40:13 +010053 update(true /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +020054 }
55
56 public static UnlockMethodCache getInstance(Context context) {
57 if (sInstance == null) {
58 sInstance = new UnlockMethodCache(context);
59 }
60 return sInstance;
61 }
62
63 /**
Christoph Studer2231c6e2014-12-19 12:40:13 +010064 * @return whether the user configured a secure unlock method like PIN, password, etc.
Jorim Jaggiecc798e2014-05-26 18:14:37 +020065 */
Christoph Studer2231c6e2014-12-19 12:40:13 +010066 public boolean isMethodSecure() {
67 return mSecure;
68 }
69
Selim Cineke8bae622015-07-15 13:24:06 -070070 public boolean isTrusted() {
71 return mTrusted;
72 }
73
Christoph Studer2231c6e2014-12-19 12:40:13 +010074 /**
Selim Cineke8bae622015-07-15 13:24:06 -070075 * @return whether the lockscreen is currently insecure, and the bouncer won't be shown
Christoph Studer2231c6e2014-12-19 12:40:13 +010076 */
Selim Cineke8bae622015-07-15 13:24:06 -070077 public boolean canSkipBouncer() {
78 return mCanSkipBouncer;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020079 }
80
81 public void addListener(OnUnlockMethodChangedListener listener) {
82 mListeners.add(listener);
83 }
84
85 public void removeListener(OnUnlockMethodChangedListener listener) {
86 mListeners.remove(listener);
87 }
88
Christoph Studer2231c6e2014-12-19 12:40:13 +010089 private void update(boolean updateAlways) {
Nick Desaulniers1d396752016-07-25 15:05:33 -070090 Trace.beginSection("UnlockMethodCache#update");
Adrian Roosd6aa6cb2015-04-16 19:31:29 -070091 int user = KeyguardUpdateMonitor.getCurrentUser();
Adrian Roos8150d2a2015-04-16 17:11:20 -070092 boolean secure = mLockPatternUtils.isSecure(user);
Selim Cineke8bae622015-07-15 13:24:06 -070093 boolean canSkipBouncer = !secure || mKeyguardUpdateMonitor.getUserCanSkipBouncer(user);
Adrian Roos7861c662014-07-25 15:37:28 +020094 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
Selim Cineke8bae622015-07-15 13:24:06 -070095 boolean trusted = mKeyguardUpdateMonitor.getUserHasTrust(user);
Adrian Roos4a410172014-08-20 17:41:44 +020096 boolean faceUnlockRunning = mKeyguardUpdateMonitor.isFaceUnlockRunning(user)
97 && trustManaged;
Selim Cineke8bae622015-07-15 13:24:06 -070098 boolean changed = secure != mSecure || canSkipBouncer != mCanSkipBouncer ||
Christoph Studer2231c6e2014-12-19 12:40:13 +010099 trustManaged != mTrustManaged || faceUnlockRunning != mFaceUnlockRunning;
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200100 if (changed || updateAlways) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100101 mSecure = secure;
Selim Cineke8bae622015-07-15 13:24:06 -0700102 mCanSkipBouncer = canSkipBouncer;
103 mTrusted = trusted;
Adrian Roos7861c662014-07-25 15:37:28 +0200104 mTrustManaged = trustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +0200105 mFaceUnlockRunning = faceUnlockRunning;
Christoph Studer2231c6e2014-12-19 12:40:13 +0100106 notifyListeners();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200107 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700108 Trace.endSection();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200109 }
110
Christoph Studer2231c6e2014-12-19 12:40:13 +0100111 private void notifyListeners() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200112 for (OnUnlockMethodChangedListener listener : mListeners) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100113 listener.onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200114 }
115 }
116
117 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
118 @Override
119 public void onUserSwitchComplete(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100120 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200121 }
122
123 @Override
124 public void onTrustChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100125 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200126 }
127
128 @Override
Adrian Roos7861c662014-07-25 15:37:28 +0200129 public void onTrustManagedChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100130 update(false /* updateAlways */);
Adrian Roos7861c662014-07-25 15:37:28 +0200131 }
132
133 @Override
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700134 public void onStartedWakingUp() {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100135 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200136 }
Jim Millerf41fc962014-06-18 16:33:51 -0700137
Adrian Roos7861c662014-07-25 15:37:28 +0200138 @Override
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200139 public void onBiometricAuthenticated(int userId, BiometricSourceType biometricSourceType) {
140 Trace.beginSection("KeyguardUpdateMonitorCallback#onBiometricAuthenticated");
141 if (!mKeyguardUpdateMonitor.isUnlockingWithBiometricAllowed()) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700142 Trace.endSection();
Selim Cinek1fcafc42015-07-20 14:39:25 -0700143 return;
144 }
Christoph Studer2231c6e2014-12-19 12:40:13 +0100145 update(false /* updateAlways */);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700146 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700147 }
Adrian Roos4a410172014-08-20 17:41:44 +0200148
149 @Override
150 public void onFaceUnlockStateChanged(boolean running, int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100151 update(false /* updateAlways */);
Adrian Roos4a410172014-08-20 17:41:44 +0200152 }
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700153
154 @Override
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700155 public void onStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700156 update(false /* updateAlways */);
157 }
Kevin Chyn08ae6642017-08-30 19:02:43 -0700158
159 @Override
160 public void onScreenTurnedOff() {
161 update(false /* updateAlways */);
162 }
Kevin Chyn94f80352017-09-08 17:39:01 -0700163
164 @Override
165 public void onKeyguardVisibilityChanged(boolean showing) {
166 update(false /* updateAlways */);
167 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200168 };
169
Adrian Roos7861c662014-07-25 15:37:28 +0200170 public boolean isTrustManaged() {
171 return mTrustManaged;
172 }
173
Adrian Roos4a410172014-08-20 17:41:44 +0200174 public boolean isFaceUnlockRunning() {
175 return mFaceUnlockRunning;
176 }
177
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200178 public static interface OnUnlockMethodChangedListener {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100179 void onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200180 }
181}