1 /* 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 17 package com.android.systemui.statusbar.phone; 18 19 import android.content.Context; 20 21 import com.android.internal.widget.LockPatternUtils; 22 import com.android.keyguard.KeyguardUpdateMonitor; 23 import com.android.keyguard.KeyguardUpdateMonitorCallback; 24 25 import 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 */ 32 public 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; 40 private boolean mTrustManaged; 41 private boolean mFaceUnlockRunning; 42 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); 76 boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user); 77 boolean faceUnlockRunning = mKeyguardUpdateMonitor.isFaceUnlockRunning(user) 78 && trustManaged; 79 boolean changed = methodInsecure != mMethodInsecure || trustManaged != mTrustManaged 80 || faceUnlockRunning != mFaceUnlockRunning; 81 if (changed || updateAlways) { 82 mMethodInsecure = methodInsecure; 83 mTrustManaged = trustManaged; 84 mFaceUnlockRunning = faceUnlockRunning; 85 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 107 public void onTrustManagedChanged(int userId) { 108 updateMethodSecure(false /* updateAlways */); 109 } 110 111 @Override 112 public void onScreenTurnedOn() { 113 updateMethodSecure(false /* updateAlways */); 114 } 115 116 @Override 117 public void onFingerprintRecognized(int userId) { 118 updateMethodSecure(false /* updateAlways */); 119 } 120 121 @Override 122 public void onFaceUnlockStateChanged(boolean running, int userId) { 123 updateMethodSecure(false /* updateAlways */); 124 } 125 }; 126 127 public boolean isTrustManaged() { 128 return mTrustManaged; 129 } 130 131 public boolean isFaceUnlockRunning() { 132 return mFaceUnlockRunning; 133 } 134 135 public static interface OnUnlockMethodChangedListener { 136 void onMethodSecureChanged(boolean methodSecure); 137 } 138 } 139