blob: 75b2c741bab18acafa9fd868da59304760b02048 [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) */
42 private boolean mCurrentlyInsecure;
Adrian Roos7861c662014-07-25 15:37:28 +020043 private boolean mTrustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +020044 private boolean mFaceUnlockRunning;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020045
46 private UnlockMethodCache(Context ctx) {
47 mLockPatternUtils = new LockPatternUtils(ctx);
48 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
49 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
Christoph Studer2231c6e2014-12-19 12:40:13 +010050 update(true /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +020051 }
52
53 public static UnlockMethodCache getInstance(Context context) {
54 if (sInstance == null) {
55 sInstance = new UnlockMethodCache(context);
56 }
57 return sInstance;
58 }
59
60 /**
Christoph Studer2231c6e2014-12-19 12:40:13 +010061 * @return whether the user configured a secure unlock method like PIN, password, etc.
Jorim Jaggiecc798e2014-05-26 18:14:37 +020062 */
Christoph Studer2231c6e2014-12-19 12:40:13 +010063 public boolean isMethodSecure() {
64 return mSecure;
65 }
66
67 /**
68 * @return whether the lockscreen is currently insecure, i. e. the bouncer won't be shown
69 */
70 public boolean isCurrentlyInsecure() {
71 return mCurrentlyInsecure;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020072 }
73
74 public void addListener(OnUnlockMethodChangedListener listener) {
75 mListeners.add(listener);
76 }
77
78 public void removeListener(OnUnlockMethodChangedListener listener) {
79 mListeners.remove(listener);
80 }
81
Christoph Studer2231c6e2014-12-19 12:40:13 +010082 private void update(boolean updateAlways) {
Jorim Jaggiecc798e2014-05-26 18:14:37 +020083 int user = mLockPatternUtils.getCurrentUser();
Adrian Roos8150d2a2015-04-16 17:11:20 -070084 boolean secure = mLockPatternUtils.isSecure(user);
Christoph Studer2231c6e2014-12-19 12:40:13 +010085 boolean currentlyInsecure = !secure || mKeyguardUpdateMonitor.getUserHasTrust(user);
Adrian Roos7861c662014-07-25 15:37:28 +020086 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
Adrian Roos4a410172014-08-20 17:41:44 +020087 boolean faceUnlockRunning = mKeyguardUpdateMonitor.isFaceUnlockRunning(user)
88 && trustManaged;
Christoph Studer2231c6e2014-12-19 12:40:13 +010089 boolean changed = secure != mSecure || currentlyInsecure != mCurrentlyInsecure ||
90 trustManaged != mTrustManaged || faceUnlockRunning != mFaceUnlockRunning;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020091 if (changed || updateAlways) {
Christoph Studer2231c6e2014-12-19 12:40:13 +010092 mSecure = secure;
93 mCurrentlyInsecure = currentlyInsecure;
Adrian Roos7861c662014-07-25 15:37:28 +020094 mTrustManaged = trustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +020095 mFaceUnlockRunning = faceUnlockRunning;
Christoph Studer2231c6e2014-12-19 12:40:13 +010096 notifyListeners();
Jorim Jaggiecc798e2014-05-26 18:14:37 +020097 }
98 }
99
Christoph Studer2231c6e2014-12-19 12:40:13 +0100100 private void notifyListeners() {
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200101 for (OnUnlockMethodChangedListener listener : mListeners) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100102 listener.onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200103 }
104 }
105
106 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
107 @Override
108 public void onUserSwitchComplete(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100109 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200110 }
111
112 @Override
113 public void onTrustChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100114 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200115 }
116
117 @Override
Adrian Roos7861c662014-07-25 15:37:28 +0200118 public void onTrustManagedChanged(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100119 update(false /* updateAlways */);
Adrian Roos7861c662014-07-25 15:37:28 +0200120 }
121
122 @Override
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200123 public void onScreenTurnedOn() {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100124 update(false /* updateAlways */);
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200125 }
Jim Millerf41fc962014-06-18 16:33:51 -0700126
Adrian Roos7861c662014-07-25 15:37:28 +0200127 @Override
Jim Miller9f0753f2015-03-23 23:59:22 -0700128 public void onFingerprintAuthenticated(int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100129 update(false /* updateAlways */);
Jim Millerf41fc962014-06-18 16:33:51 -0700130 }
Adrian Roos4a410172014-08-20 17:41:44 +0200131
132 @Override
133 public void onFaceUnlockStateChanged(boolean running, int userId) {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100134 update(false /* updateAlways */);
Adrian Roos4a410172014-08-20 17:41:44 +0200135 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200136 };
137
Adrian Roos7861c662014-07-25 15:37:28 +0200138 public boolean isTrustManaged() {
139 return mTrustManaged;
140 }
141
Adrian Roos4a410172014-08-20 17:41:44 +0200142 public boolean isFaceUnlockRunning() {
143 return mFaceUnlockRunning;
144 }
145
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200146 public static interface OnUnlockMethodChangedListener {
Christoph Studer2231c6e2014-12-19 12:40:13 +0100147 void onUnlockMethodStateChanged();
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200148 }
149}