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;
21 import com.android.internal.widget.LockPatternUtils;
22 import com.android.keyguard.KeyguardUpdateMonitor;
23 import com.android.keyguard.KeyguardUpdateMonitorCallback;
25 import java.util.ArrayList;
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.
32 public class UnlockMethodCache {
34 private static UnlockMethodCache sInstance;
36 private final LockPatternUtils mLockPatternUtils;
37 private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
38 private final ArrayList<OnUnlockMethodChangedListener> mListeners = new ArrayList<>();
39 private boolean mMethodInsecure;
41 private UnlockMethodCache(Context ctx) {
42 mLockPatternUtils = new LockPatternUtils(ctx);
43 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
44 KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback);
45 updateMethodSecure(true /* updateAlways */);
48 public static UnlockMethodCache getInstance(Context context) {
49 if (sInstance == null) {
50 sInstance = new UnlockMethodCache(context);
56 * @return whether the current security method is secure, i. e. the bouncer will be shown
58 public boolean isMethodInsecure() {
59 return mMethodInsecure;
62 public void addListener(OnUnlockMethodChangedListener listener) {
63 mListeners.add(listener);
66 public void removeListener(OnUnlockMethodChangedListener listener) {
67 mListeners.remove(listener);
70 private void updateMethodSecure(boolean updateAlways) {
71 int user = mLockPatternUtils.getCurrentUser();
72 boolean methodInsecure = !mLockPatternUtils.isSecure() ||
73 mKeyguardUpdateMonitor.getUserHasTrust(user);
74 boolean changed = methodInsecure != mMethodInsecure;
75 if (changed || updateAlways) {
76 mMethodInsecure = methodInsecure;
77 notifyListeners(mMethodInsecure);
81 private void notifyListeners(boolean secure) {
82 for (OnUnlockMethodChangedListener listener : mListeners) {
83 listener.onMethodSecureChanged(secure);
87 private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
89 public void onUserSwitchComplete(int userId) {
90 updateMethodSecure(false /* updateAlways */);
94 public void onTrustChanged(int userId) {
95 updateMethodSecure(false /* updateAlways */);
99 public void onScreenTurnedOn() {
100 updateMethodSecure(false /* updateAlways */);
103 public void onFingerprintRecognized(int userId) {
104 updateMethodSecure(false /* updateAlways */);
108 public static interface OnUnlockMethodChangedListener {
109 void onMethodSecureChanged(boolean methodSecure);