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<>(); |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 39 | /** Whether the user configured a secure unlock method (PIN, password, etc.) */ |
| 40 | private boolean mSecure; |
| 41 | /** Whether the unlock method is currently insecure (insecure method or trusted environment) */ |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 42 | private boolean mCanSkipBouncer; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 43 | private boolean mTrustManaged; |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 44 | private boolean mFaceUnlockRunning; |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 45 | private boolean mTrusted; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 46 | |
| 47 | private UnlockMethodCache(Context ctx) { |
| 48 | mLockPatternUtils = new LockPatternUtils(ctx); |
| 49 | mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx); |
| 50 | KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback); |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 51 | update(true /* updateAlways */); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public static UnlockMethodCache getInstance(Context context) { |
| 55 | if (sInstance == null) { |
| 56 | sInstance = new UnlockMethodCache(context); |
| 57 | } |
| 58 | return sInstance; |
| 59 | } |
| 60 | |
| 61 | /** |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 62 | * @return whether the user configured a secure unlock method like PIN, password, etc. |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 63 | */ |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 64 | public boolean isMethodSecure() { |
| 65 | return mSecure; |
| 66 | } |
| 67 | |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 68 | public boolean isTrusted() { |
| 69 | return mTrusted; |
| 70 | } |
| 71 | |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 72 | /** |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 73 | * @return whether the lockscreen is currently insecure, and the bouncer won't be shown |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 74 | */ |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 75 | public boolean canSkipBouncer() { |
| 76 | return mCanSkipBouncer; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | public void addListener(OnUnlockMethodChangedListener listener) { |
| 80 | mListeners.add(listener); |
| 81 | } |
| 82 | |
| 83 | public void removeListener(OnUnlockMethodChangedListener listener) { |
| 84 | mListeners.remove(listener); |
| 85 | } |
| 86 | |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 87 | private void update(boolean updateAlways) { |
Adrian Roos | d6aa6cb | 2015-04-16 19:31:29 -0700 | [diff] [blame] | 88 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 89 | boolean secure = mLockPatternUtils.isSecure(user); |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 90 | boolean canSkipBouncer = !secure || mKeyguardUpdateMonitor.getUserCanSkipBouncer(user); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 91 | boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user); |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 92 | boolean trusted = mKeyguardUpdateMonitor.getUserHasTrust(user); |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 93 | boolean faceUnlockRunning = mKeyguardUpdateMonitor.isFaceUnlockRunning(user) |
| 94 | && trustManaged; |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 95 | boolean changed = secure != mSecure || canSkipBouncer != mCanSkipBouncer || |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 96 | trustManaged != mTrustManaged || faceUnlockRunning != mFaceUnlockRunning; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 97 | if (changed || updateAlways) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 98 | mSecure = secure; |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 99 | mCanSkipBouncer = canSkipBouncer; |
| 100 | mTrusted = trusted; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 101 | mTrustManaged = trustManaged; |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 102 | mFaceUnlockRunning = faceUnlockRunning; |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 103 | notifyListeners(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 107 | private void notifyListeners() { |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 108 | for (OnUnlockMethodChangedListener listener : mListeners) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 109 | listener.onUnlockMethodStateChanged(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() { |
| 114 | @Override |
| 115 | public void onUserSwitchComplete(int userId) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 116 | update(false /* updateAlways */); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onTrustChanged(int userId) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 121 | update(false /* updateAlways */); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 125 | public void onTrustManagedChanged(int userId) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 126 | update(false /* updateAlways */); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame^] | 130 | public void onStartedWakingUp() { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 131 | update(false /* updateAlways */); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 132 | } |
Jim Miller | f41fc96 | 2014-06-18 16:33:51 -0700 | [diff] [blame] | 133 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 134 | @Override |
Jorim Jaggi | 740452e | 2015-07-09 12:13:59 -0700 | [diff] [blame] | 135 | public void onFingerprintAuthenticated(int userId, boolean wakeAndUnlocking) { |
Selim Cinek | 1fcafc4 | 2015-07-20 14:39:25 -0700 | [diff] [blame] | 136 | if (!mKeyguardUpdateMonitor.isUnlockingWithFingerprintAllowed()) { |
| 137 | return; |
| 138 | } |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 139 | update(false /* updateAlways */); |
Jim Miller | f41fc96 | 2014-06-18 16:33:51 -0700 | [diff] [blame] | 140 | } |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 141 | |
| 142 | @Override |
| 143 | public void onFaceUnlockStateChanged(boolean running, int userId) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 144 | update(false /* updateAlways */); |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 145 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 148 | public boolean isTrustManaged() { |
| 149 | return mTrustManaged; |
| 150 | } |
| 151 | |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 152 | public boolean isFaceUnlockRunning() { |
| 153 | return mFaceUnlockRunning; |
| 154 | } |
| 155 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 156 | public static interface OnUnlockMethodChangedListener { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 157 | void onUnlockMethodStateChanged(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 158 | } |
| 159 | } |