Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 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 | |
| 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 */); |
| 46 | } |
| 47 | |
| 48 | public static UnlockMethodCache getInstance(Context context) { |
| 49 | if (sInstance == null) { |
| 50 | sInstance = new UnlockMethodCache(context); |
| 51 | } |
| 52 | return sInstance; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return whether the current security method is secure, i. e. the bouncer will be shown |
| 57 | */ |
| 58 | public boolean isMethodInsecure() { |
| 59 | return mMethodInsecure; |
| 60 | } |
| 61 | |
| 62 | public void addListener(OnUnlockMethodChangedListener listener) { |
| 63 | mListeners.add(listener); |
| 64 | } |
| 65 | |
| 66 | public void removeListener(OnUnlockMethodChangedListener listener) { |
| 67 | mListeners.remove(listener); |
| 68 | } |
| 69 | |
| 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); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private void notifyListeners(boolean secure) { |
| 82 | for (OnUnlockMethodChangedListener listener : mListeners) { |
| 83 | listener.onMethodSecureChanged(secure); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() { |
| 88 | @Override |
| 89 | public void onUserSwitchComplete(int userId) { |
| 90 | updateMethodSecure(false /* updateAlways */); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onTrustChanged(int userId) { |
| 95 | updateMethodSecure(false /* updateAlways */); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void onScreenTurnedOn() { |
| 100 | updateMethodSecure(false /* updateAlways */); |
| 101 | } |
Jim Miller | f41fc96 | 2014-06-18 16:33:51 -0700 | [diff] [blame^] | 102 | |
| 103 | public void onFingerprintRecognized(int userId) { |
| 104 | updateMethodSecure(false /* updateAlways */); |
| 105 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | public static interface OnUnlockMethodChangedListener { |
| 109 | void onMethodSecureChanged(boolean methodSecure); |
| 110 | } |
| 111 | } |