blob: a91cd5177cfd8ab746cbd5ad8940c054be15dca6 [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;
20
21import com.android.internal.widget.LockPatternUtils;
22import com.android.keyguard.KeyguardUpdateMonitor;
23import com.android.keyguard.KeyguardUpdateMonitorCallback;
24
25import java.util.ArrayList;
26
27/**
28 * Caches whether the current unlock method is insecure, taking trust into account. This information
29 * might be a little bit out of date and should not be used for actual security decisions; it should
30 * be only used for visual indications.
31 */
32public class UnlockMethodCache {
33
34 private static UnlockMethodCache sInstance;
35
36 private final LockPatternUtils mLockPatternUtils;
37 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
38 private final ArrayList<OnUnlockMethodChangedListener> mListeners = new ArrayList<>();
Christoph Studer2231c6e2014-12-19 12:40:13 +010039 /** Whether the user configured a secure unlock method (PIN, password, etc.) */
40 private boolean mSecure;
41 /** Whether the unlock method is currently insecure (insecure method or trusted environment) */
Selim Cineke8bae622015-07-15 13:24:06 -070042 private boolean mCanSkipBouncer;
Adrian Roos7861c662014-07-25 15:37:28 +020043 private boolean mTrustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +020044 private boolean mFaceUnlockRunning;
Selim Cineke8bae622015-07-15 13:24:06 -070045 private boolean mTrusted;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020046
47 private UnlockMethodCache(Context ctx) {
48 mLockPatternUtils = new LockPatternUtils(ctx);
49 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
50 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
Christoph Studer2231c6e2014-12-19 12:40:13 +010051 update(true /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +020052 }
53
54 public static UnlockMethodCache getInstance(Context context) {
55 if (sInstance == null) {
56 sInstance = new UnlockMethodCache(context);
57 }
58 return sInstance;
59 }
60
61 /**
Christoph Studer2231c6e2014-12-19 12:40:13 +010062 * @return whether the user configured a secure unlock method like PIN, password, etc.
Jorim Jaggiecc798e2014-05-26 18:14:37 +020063 */
Christoph Studer2231c6e2014-12-19 12:40:13 +010064 public boolean isMethodSecure() {
65 return mSecure;
66 }
67
Selim Cineke8bae622015-07-15 13:24:06 -070068 public boolean isTrusted() {
69 return mTrusted;
70 }
71
Christoph Studer2231c6e2014-12-19 12:40:13 +010072 /**
Selim Cineke8bae622015-07-15 13:24:06 -070073 * @return whether the lockscreen is currently insecure, and the bouncer won't be shown
Christoph Studer2231c6e2014-12-19 12:40:13 +010074 */
Selim Cineke8bae622015-07-15 13:24:06 -070075 public boolean canSkipBouncer() {
76 return mCanSkipBouncer;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020077 }
78
79 public void addListener(OnUnlockMethodChangedListener listener) {
80 mListeners.add(listener);
81 }
82
83 public void removeListener(OnUnlockMethodChangedListener listener) {
84 mListeners.remove(listener);
85 }
86
Christoph Studer2231c6e2014-12-19 12:40:13 +010087 private void update(boolean updateAlways) {
Adrian Roosd6aa6cb2015-04-16 19:31:29 -070088 int user = KeyguardUpdateMonitor.getCurrentUser();
Adrian Roos8150d2a2015-04-16 17:11:20 -070089 boolean secure = mLockPatternUtils.isSecure(user);
Selim Cineke8bae622015-07-15 13:24:06 -070090 boolean canSkipBouncer = !secure || mKeyguardUpdateMonitor.getUserCanSkipBouncer(user);
Adrian Roos7861c662014-07-25 15:37:28 +020091 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
Selim Cineke8bae622015-07-15 13:24:06 -070092 boolean trusted = mKeyguardUpdateMonitor.getUserHasTrust(user);
Adrian Roos4a410172014-08-20 17:41:44 +020093 boolean faceUnlockRunning = mKeyguardUpdateMonitor.isFaceUnlockRunning(user)
94 && trustManaged;
Selim Cineke8bae622015-07-15 13:24:06 -070095 boolean changed = secure != mSecure || canSkipBouncer != mCanSkipBouncer ||
Christoph Studer2231c6e2014-12-19 12:40:13 +010096 trustManaged != mTrustManaged || faceUnlockRunning != mFaceUnlockRunning;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020097 if (changed || updateAlways) {
Christoph Studer2231c6e2014-12-19 12:40:13 +010098 mSecure = secure;
Selim Cineke8bae622015-07-15 13:24:06 -070099 mCanSkipBouncer = canSkipBouncer;
100 mTrusted = trusted;
Adrian Roos7861c662014-07-25 15:37:28 +0200101 mTrustManaged = trustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +0200102 mFaceUnlockRunning = faceUnlockRunning;
Christoph Studer2231c6e2014-12-19 12:40:13 +0100103 notifyListeners();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200104 }
105 }
106
Christoph Studer2231c6e2014-12-19 12:40:13 +0100107 private void notifyListeners() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200108 for (OnUnlockMethodChangedListener listener : mListeners) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100109 listener.onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200110 }
111 }
112
113 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
114 @Override
115 public void onUserSwitchComplete(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100116 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200117 }
118
119 @Override
120 public void onTrustChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100121 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200122 }
123
124 @Override
Adrian Roos7861c662014-07-25 15:37:28 +0200125 public void onTrustManagedChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100126 update(false /* updateAlways */);
Adrian Roos7861c662014-07-25 15:37:28 +0200127 }
128
129 @Override
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700130 public void onStartedWakingUp() {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100131 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200132 }
Jim Millerf41fc962014-06-18 16:33:51 -0700133
Adrian Roos7861c662014-07-25 15:37:28 +0200134 @Override
Jorim Jaggi83eb6bb2015-08-17 17:38:58 -0700135 public void onFingerprintAuthenticated(int userId) {
Selim Cinek1fcafc42015-07-20 14:39:25 -0700136 if (!mKeyguardUpdateMonitor.isUnlockingWithFingerprintAllowed()) {
137 return;
138 }
Christoph Studer2231c6e2014-12-19 12:40:13 +0100139 update(false /* updateAlways */);
Jim Millerf41fc962014-06-18 16:33:51 -0700140 }
Adrian Roos4a410172014-08-20 17:41:44 +0200141
142 @Override
143 public void onFaceUnlockStateChanged(boolean running, int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100144 update(false /* updateAlways */);
Adrian Roos4a410172014-08-20 17:41:44 +0200145 }
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700146
147 @Override
Adrian Roos1de8bcb2015-08-19 16:52:52 -0700148 public void onStrongAuthStateChanged(int userId) {
Jorim Jaggi25b4d4b2015-08-11 15:54:06 -0700149 update(false /* updateAlways */);
150 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200151 };
152
Adrian Roos7861c662014-07-25 15:37:28 +0200153 public boolean isTrustManaged() {
154 return mTrustManaged;
155 }
156
Adrian Roos4a410172014-08-20 17:41:44 +0200157 public boolean isFaceUnlockRunning() {
158 return mFaceUnlockRunning;
159 }
160
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200161 public static interface OnUnlockMethodChangedListener {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100162 void onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200163 }
164}