blob: e5eef9db7a89908a8c64c803e9f6255cf282487f [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<>();
39 private boolean mMethodInsecure;
Adrian Roos7861c662014-07-25 15:37:28 +020040 private boolean mTrustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +020041 private boolean mFaceUnlockRunning;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020042
43 private UnlockMethodCache(Context ctx) {
44 mLockPatternUtils = new LockPatternUtils(ctx);
45 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
46 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
47 updateMethodSecure(true /* updateAlways */);
48 }
49
50 public static UnlockMethodCache getInstance(Context context) {
51 if (sInstance == null) {
52 sInstance = new UnlockMethodCache(context);
53 }
54 return sInstance;
55 }
56
57 /**
58 * @return whether the current security method is secure, i. e. the bouncer will be shown
59 */
60 public boolean isMethodInsecure() {
61 return mMethodInsecure;
62 }
63
64 public void addListener(OnUnlockMethodChangedListener listener) {
65 mListeners.add(listener);
66 }
67
68 public void removeListener(OnUnlockMethodChangedListener listener) {
69 mListeners.remove(listener);
70 }
71
72 private void updateMethodSecure(boolean updateAlways) {
73 int user = mLockPatternUtils.getCurrentUser();
74 boolean methodInsecure = !mLockPatternUtils.isSecure() ||
75 mKeyguardUpdateMonitor.getUserHasTrust(user);
Adrian Roos7861c662014-07-25 15:37:28 +020076 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
Adrian Roos4a410172014-08-20 17:41:44 +020077 boolean faceUnlockRunning = mKeyguardUpdateMonitor.isFaceUnlockRunning(user)
78 && trustManaged;
79 boolean changed = methodInsecure != mMethodInsecure || trustManaged != mTrustManaged
80 || faceUnlockRunning != mFaceUnlockRunning;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020081 if (changed || updateAlways) {
82 mMethodInsecure = methodInsecure;
Adrian Roos7861c662014-07-25 15:37:28 +020083 mTrustManaged = trustManaged;
Adrian Roos4a410172014-08-20 17:41:44 +020084 mFaceUnlockRunning = faceUnlockRunning;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020085 notifyListeners(mMethodInsecure);
86 }
87 }
88
89 private void notifyListeners(boolean secure) {
90 for (OnUnlockMethodChangedListener listener : mListeners) {
91 listener.onMethodSecureChanged(secure);
92 }
93 }
94
95 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
96 @Override
97 public void onUserSwitchComplete(int userId) {
98 updateMethodSecure(false /* updateAlways */);
99 }
100
101 @Override
102 public void onTrustChanged(int userId) {
103 updateMethodSecure(false /* updateAlways */);
104 }
105
106 @Override
Adrian Roos7861c662014-07-25 15:37:28 +0200107 public void onTrustManagedChanged(int userId) {
108 updateMethodSecure(false /* updateAlways */);
109 }
110
111 @Override
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200112 public void onScreenTurnedOn() {
113 updateMethodSecure(false /* updateAlways */);
114 }
Jim Millerf41fc962014-06-18 16:33:51 -0700115
Adrian Roos7861c662014-07-25 15:37:28 +0200116 @Override
Jim Millerf41fc962014-06-18 16:33:51 -0700117 public void onFingerprintRecognized(int userId) {
118 updateMethodSecure(false /* updateAlways */);
119 }
Adrian Roos4a410172014-08-20 17:41:44 +0200120
121 @Override
122 public void onFaceUnlockStateChanged(boolean running, int userId) {
123 updateMethodSecure(false /* updateAlways */);
124 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200125 };
126
Adrian Roos7861c662014-07-25 15:37:28 +0200127 public boolean isTrustManaged() {
128 return mTrustManaged;
129 }
130
Adrian Roos4a410172014-08-20 17:41:44 +0200131 public boolean isFaceUnlockRunning() {
132 return mFaceUnlockRunning;
133 }
134
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200135 public static interface OnUnlockMethodChangedListener {
136 void onMethodSecureChanged(boolean methodSecure);
137 }
138}