blob: 1411a544c346f7715f0b3e3a314d4cd6a9b7eae7 [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
19import android.content.Context;
Nick Desaulniers1d396752016-07-25 15:05:33 -070020import android.os.Trace;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020021
22import com.android.internal.widget.LockPatternUtils;
23import com.android.keyguard.KeyguardUpdateMonitor;
24import com.android.keyguard.KeyguardUpdateMonitorCallback;
25
26import java.util.ArrayList;
27
28/**
29 * Caches whether the current unlock method is insecure, taking trust into account. This information
30 * might be a little bit out of date and should not be used for actual security decisions; it should
31 * be only used for visual indications.
32 */
33public class UnlockMethodCache {
34
35 private static UnlockMethodCache sInstance;
36
37 private final LockPatternUtils mLockPatternUtils;
38 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
39 private final ArrayList<OnUnlockMethodChangedListener> mListeners = new ArrayList<>();
Christoph Studer2231c6e2014-12-19 12:40:13 +010040 /** Whether the user configured a secure unlock method (PIN, password, etc.) */
41 private boolean mSecure;
42 /** Whether the unlock method is currently insecure (insecure method or trusted environment) */
Selim Cineke8bae622015-07-15 13:24:06 -070043 private boolean mCanSkipBouncer;
Adrian Roos7861c662014-07-25 15:37:28 +020044 private boolean mTrustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +020045 private boolean mFaceUnlockRunning;
Selim Cineke8bae622015-07-15 13:24:06 -070046 private boolean mTrusted;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020047
48 private UnlockMethodCache(Context ctx) {
49 mLockPatternUtils = new LockPatternUtils(ctx);
50 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
51 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
Christoph Studer2231c6e2014-12-19 12:40:13 +010052 update(true /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +020053 }
54
55 public static UnlockMethodCache getInstance(Context context) {
56 if (sInstance == null) {
57 sInstance = new UnlockMethodCache(context);
58 }
59 return sInstance;
60 }
61
62 /**
Christoph Studer2231c6e2014-12-19 12:40:13 +010063 * @return whether the user configured a secure unlock method like PIN, password, etc.
Jorim Jaggiecc798e2014-05-26 18:14:37 +020064 */
Christoph Studer2231c6e2014-12-19 12:40:13 +010065 public boolean isMethodSecure() {
66 return mSecure;
67 }
68
Selim Cineke8bae622015-07-15 13:24:06 -070069 public boolean isTrusted() {
70 return mTrusted;
71 }
72
Christoph Studer2231c6e2014-12-19 12:40:13 +010073 /**
Selim Cineke8bae622015-07-15 13:24:06 -070074 * @return whether the lockscreen is currently insecure, and the bouncer won't be shown
Christoph Studer2231c6e2014-12-19 12:40:13 +010075 */
Selim Cineke8bae622015-07-15 13:24:06 -070076 public boolean canSkipBouncer() {
77 return mCanSkipBouncer;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020078 }
79
80 public void addListener(OnUnlockMethodChangedListener listener) {
81 mListeners.add(listener);
82 }
83
84 public void removeListener(OnUnlockMethodChangedListener listener) {
85 mListeners.remove(listener);
86 }
87
Christoph Studer2231c6e2014-12-19 12:40:13 +010088 private void update(boolean updateAlways) {
Nick Desaulniers1d396752016-07-25 15:05:33 -070089 Trace.beginSection("UnlockMethodCache#update");
Adrian Roosd6aa6cb2015-04-16 19:31:29 -070090 int user = KeyguardUpdateMonitor.getCurrentUser();
Adrian Roos8150d2a2015-04-16 17:11:20 -070091 boolean secure = mLockPatternUtils.isSecure(user);
Selim Cineke8bae622015-07-15 13:24:06 -070092 boolean canSkipBouncer = !secure || mKeyguardUpdateMonitor.getUserCanSkipBouncer(user);
Adrian Roos7861c662014-07-25 15:37:28 +020093 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
Selim Cineke8bae622015-07-15 13:24:06 -070094 boolean trusted = mKeyguardUpdateMonitor.getUserHasTrust(user);
Adrian Roos4a410172014-08-20 17:41:44 +020095 boolean faceUnlockRunning = mKeyguardUpdateMonitor.isFaceUnlockRunning(user)
96 && trustManaged;
Selim Cineke8bae622015-07-15 13:24:06 -070097 boolean changed = secure != mSecure || canSkipBouncer != mCanSkipBouncer ||
Christoph Studer2231c6e2014-12-19 12:40:13 +010098 trustManaged != mTrustManaged || faceUnlockRunning != mFaceUnlockRunning;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020099 if (changed || updateAlways) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100100 mSecure = secure;
Selim Cineke8bae622015-07-15 13:24:06 -0700101 mCanSkipBouncer = canSkipBouncer;
102 mTrusted = trusted;
Adrian Roos7861c662014-07-25 15:37:28 +0200103 mTrustManaged = trustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +0200104 mFaceUnlockRunning = faceUnlockRunning;
Christoph Studer2231c6e2014-12-19 12:40:13 +0100105 notifyListeners();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200106 }
Nick Desaulniers1d396752016-07-25 15:05:33 -0700107 Trace.endSection();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200108 }
109
Christoph Studer2231c6e2014-12-19 12:40:13 +0100110 private void notifyListeners() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200111 for (OnUnlockMethodChangedListener listener : mListeners) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100112 listener.onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200113 }
114 }
115
116 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
117 @Override
118 public void onUserSwitchComplete(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100119 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200120 }
121
122 @Override
123 public void onTrustChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100124 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200125 }
126
127 @Override
Adrian Roos7861c662014-07-25 15:37:28 +0200128 public void onTrustManagedChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100129 update(false /* updateAlways */);
Adrian Roos7861c662014-07-25 15:37:28 +0200130 }
131
132 @Override
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700133 public void onStartedWakingUp() {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100134 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200135 }
Jim Millerf41fc962014-06-18 16:33:51 -0700136
Adrian Roos7861c662014-07-25 15:37:28 +0200137 @Override
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700138 public void onFingerprintAuthenticated(int userId) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700139 Trace.beginSection("KeyguardUpdateMonitorCallback#onFingerprintAuthenticated");
Selim Cinek1fcafc42015-07-20 14:39:25 -0700140 if (!mKeyguardUpdateMonitor.isUnlockingWithFingerprintAllowed()) {
Nick Desaulniers1d396752016-07-25 15:05:33 -0700141 Trace.endSection();
Selim Cinek1fcafc42015-07-20 14:39:25 -0700142 return;
143 }
Christoph Studer2231c6e2014-12-19 12:40:13 +0100144 update(false /* updateAlways */);
Nick Desaulniers1d396752016-07-25 15:05:33 -0700145 Trace.endSection();
Jim Millerf41fc962014-06-18 16:33:51 -0700146 }
Adrian Roos4a410172014-08-20 17:41:44 +0200147
148 @Override
149 public void onFaceUnlockStateChanged(boolean running, int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100150 update(false /* updateAlways */);
Adrian Roos4a410172014-08-20 17:41:44 +0200151 }
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700152
153 @Override
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700154 public void onStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700155 update(false /* updateAlways */);
156 }
Kevin Chyn08ae6642017-08-30 19:02:43 -0700157
158 @Override
159 public void onScreenTurnedOff() {
160 update(false /* updateAlways */);
161 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200162 };
163
Adrian Roos7861c662014-07-25 15:37:28 +0200164 public boolean isTrustManaged() {
165 return mTrustManaged;
166 }
167
Adrian Roos4a410172014-08-20 17:41:44 +0200168 public boolean isFaceUnlockRunning() {
169 return mFaceUnlockRunning;
170 }
171
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200172 public static interface OnUnlockMethodChangedListener {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100173 void onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200174 }
175}