blob: 58196f73e66e3e6d4cfe0c773186ad1e9156187b [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;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020041
42 private UnlockMethodCache(Context ctx) {
43 mLockPatternUtils = new LockPatternUtils(ctx);
44 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
45 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
46 updateMethodSecure(true /* updateAlways */);
47 }
48
49 public static UnlockMethodCache getInstance(Context context) {
50 if (sInstance == null) {
51 sInstance = new UnlockMethodCache(context);
52 }
53 return sInstance;
54 }
55
56 /**
57 * @return whether the current security method is secure, i. e. the bouncer will be shown
58 */
59 public boolean isMethodInsecure() {
60 return mMethodInsecure;
61 }
62
63 public void addListener(OnUnlockMethodChangedListener listener) {
64 mListeners.add(listener);
65 }
66
67 public void removeListener(OnUnlockMethodChangedListener listener) {
68 mListeners.remove(listener);
69 }
70
71 private void updateMethodSecure(boolean updateAlways) {
72 int user = mLockPatternUtils.getCurrentUser();
73 boolean methodInsecure = !mLockPatternUtils.isSecure() ||
74 mKeyguardUpdateMonitor.getUserHasTrust(user);
Adrian Roos7861c662014-07-25 15:37:28 +020075 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user);
76 boolean changed = methodInsecure != mMethodInsecure || trustManaged != mTrustManaged;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020077 if (changed || updateAlways) {
78 mMethodInsecure = methodInsecure;
Adrian Roos7861c662014-07-25 15:37:28 +020079 mTrustManaged = trustManaged;
Jorim Jaggiecc798e2014-05-26 18:14:37 +020080 notifyListeners(mMethodInsecure);
81 }
82 }
83
84 private void notifyListeners(boolean secure) {
85 for (OnUnlockMethodChangedListener listener : mListeners) {
86 listener.onMethodSecureChanged(secure);
87 }
88 }
89
90 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
91 @Override
92 public void onUserSwitchComplete(int userId) {
93 updateMethodSecure(false /* updateAlways */);
94 }
95
96 @Override
97 public void onTrustChanged(int userId) {
98 updateMethodSecure(false /* updateAlways */);
99 }
100
101 @Override
Adrian Roos7861c662014-07-25 15:37:28 +0200102 public void onTrustManagedChanged(int userId) {
103 updateMethodSecure(false /* updateAlways */);
104 }
105
106 @Override
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200107 public void onScreenTurnedOn() {
108 updateMethodSecure(false /* updateAlways */);
109 }
Jim Millerf41fc962014-06-18 16:33:51 -0700110
Adrian Roos7861c662014-07-25 15:37:28 +0200111 @Override
Jim Millerf41fc962014-06-18 16:33:51 -0700112 public void onFingerprintRecognized(int userId) {
113 updateMethodSecure(false /* updateAlways */);
114 }
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200115 };
116
Adrian Roos7861c662014-07-25 15:37:28 +0200117 public boolean isTrustManaged() {
118 return mTrustManaged;
119 }
120
Jorim Jaggiecc798e2014-05-26 18:14:37 +0200121 public static interface OnUnlockMethodChangedListener {
122 void onMethodSecureChanged(boolean methodSecure);
123 }
124}