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; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame^] | 40 | private boolean mTrustManaged; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 41 | |
| 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 Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame^] | 75 | boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user); |
| 76 | boolean changed = methodInsecure != mMethodInsecure || trustManaged != mTrustManaged; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 77 | if (changed || updateAlways) { |
| 78 | mMethodInsecure = methodInsecure; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame^] | 79 | mTrustManaged = trustManaged; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 80 | 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 Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame^] | 102 | public void onTrustManagedChanged(int userId) { |
| 103 | updateMethodSecure(false /* updateAlways */); |
| 104 | } |
| 105 | |
| 106 | @Override |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 107 | public void onScreenTurnedOn() { |
| 108 | updateMethodSecure(false /* updateAlways */); |
| 109 | } |
Jim Miller | f41fc96 | 2014-06-18 16:33:51 -0700 | [diff] [blame] | 110 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame^] | 111 | @Override |
Jim Miller | f41fc96 | 2014-06-18 16:33:51 -0700 | [diff] [blame] | 112 | public void onFingerprintRecognized(int userId) { |
| 113 | updateMethodSecure(false /* updateAlways */); |
| 114 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame^] | 117 | public boolean isTrustManaged() { |
| 118 | return mTrustManaged; |
| 119 | } |
| 120 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 121 | public static interface OnUnlockMethodChangedListener { |
| 122 | void onMethodSecureChanged(boolean methodSecure); |
| 123 | } |
| 124 | } |