2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.systemui.statusbar.phone;
19 import android.content.Context;
20 import android.os.Trace;
22 import com.android.internal.widget.LockPatternUtils;
23 import com.android.keyguard.KeyguardUpdateMonitor;
24 import com.android.keyguard.KeyguardUpdateMonitorCallback;
26 import java.util.ArrayList;
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.
33 public class UnlockMethodCache {
35 private static UnlockMethodCache sInstance;
37 private final LockPatternUtils mLockPatternUtils;
38 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
39 private final ArrayList<OnUnlockMethodChangedListener> mListeners = new ArrayList<>();
40 /** 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) */
43 private boolean mCanSkipBouncer;
44 private boolean mTrustManaged;
45 private boolean mFaceUnlockRunning;
46 private boolean mTrusted;
48 private UnlockMethodCache(Context ctx) {
49 mLockPatternUtils = new LockPatternUtils(ctx);
50 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
51 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
52 update(true /* updateAlways */);
55 public static UnlockMethodCache getInstance(Context context) {
56 if (sInstance == null) {
57 sInstance = new UnlockMethodCache(context);
63 * @return whether the user configured a secure unlock method like PIN, password, etc.
65 public boolean isMethodSecure() {
69 public boolean isTrusted() {
74 * @return whether the lockscreen is currently insecure, and the bouncer won't be shown
76 public boolean canSkipBouncer() {
77 return mCanSkipBouncer;
80 public void addListener(OnUnlockMethodChangedListener listener) {
81 mListeners.add(listener);
84 public void removeListener(OnUnlockMethodChangedListener listener) {
85 mListeners.remove(listener);
88 private void update(boolean updateAlways) {
89 Trace.beginSection("UnlockMethodCache#update");
90 int user = KeyguardUpdateMonitor.getCurrentUser();
91 boolean secure = mLockPatternUtils.isSecure(user);
92 boolean canSkipBouncer = !secure || mKeyguardUpdateMonitor.getUserCanSkipBouncer(user);
93 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
94 boolean trusted = mKeyguardUpdateMonitor.getUserHasTrust(user);
95 boolean faceUnlockRunning = mKeyguardUpdateMonitor.isFaceUnlockRunning(user)
97 boolean changed = secure != mSecure || canSkipBouncer != mCanSkipBouncer ||
98 trustManaged != mTrustManaged || faceUnlockRunning != mFaceUnlockRunning;
99 if (changed || updateAlways) {
101 mCanSkipBouncer = canSkipBouncer;
103 mTrustManaged = trustManaged;
104 mFaceUnlockRunning = faceUnlockRunning;
110 private void notifyListeners() {
111 for (OnUnlockMethodChangedListener listener : mListeners) {
112 listener.onUnlockMethodStateChanged();
116 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
118 public void onUserSwitchComplete(int userId) {
119 update(false /* updateAlways */);
123 public void onTrustChanged(int userId) {
124 update(false /* updateAlways */);
128 public void onTrustManagedChanged(int userId) {
129 update(false /* updateAlways */);
133 public void onStartedWakingUp() {
134 update(false /* updateAlways */);
138 public void onFingerprintAuthenticated(int userId) {
139 Trace.beginSection("KeyguardUpdateMonitorCallback#onFingerprintAuthenticated");
140 if (!mKeyguardUpdateMonitor.isUnlockingWithFingerprintAllowed()) {
144 update(false /* updateAlways */);
149 public void onFaceUnlockStateChanged(boolean running, int userId) {
150 update(false /* updateAlways */);
154 public void onStrongAuthStateChanged(int userId) {
155 update(false /* updateAlways */);
159 public boolean isTrustManaged() {
160 return mTrustManaged;
163 public boolean isFaceUnlockRunning() {
164 return mFaceUnlockRunning;
167 public static interface OnUnlockMethodChangedListener {
168 void onUnlockMethodStateChanged();