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; |
Nick Desaulniers | 1d39675 | 2016-07-25 15:05:33 -0700 | [diff] [blame] | 20 | import android.os.Trace; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 21 | |
| 22 | import com.android.internal.widget.LockPatternUtils; |
| 23 | import com.android.keyguard.KeyguardUpdateMonitor; |
| 24 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | |
| 28 | /** |
| 29 | * Caches whether the current unlock method is insecure, taking trust into account. This information |
| 30 | * might be a little bit out of date and should not be used for actual security decisions; it should |
| 31 | * be only used for visual indications. |
| 32 | */ |
| 33 | public class UnlockMethodCache { |
| 34 | |
| 35 | private static UnlockMethodCache sInstance; |
| 36 | |
| 37 | private final LockPatternUtils mLockPatternUtils; |
| 38 | private final KeyguardUpdateMonitor mKeyguardUpdateMonitor; |
| 39 | private final ArrayList<OnUnlockMethodChangedListener> mListeners = new ArrayList<>(); |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 40 | /** Whether the user configured a secure unlock method (PIN, password, etc.) */ |
| 41 | private boolean mSecure; |
| 42 | /** Whether the unlock method is currently insecure (insecure method or trusted environment) */ |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 43 | private boolean mCanSkipBouncer; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 44 | private boolean mTrustManaged; |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 45 | private boolean mFaceUnlockRunning; |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 46 | private boolean mTrusted; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 47 | |
| 48 | private UnlockMethodCache(Context ctx) { |
| 49 | mLockPatternUtils = new LockPatternUtils(ctx); |
| 50 | mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx); |
| 51 | KeyguardUpdateMonitor.getInstance(ctx).registerCallback(mCallback); |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 52 | update(true /* updateAlways */); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public static UnlockMethodCache getInstance(Context context) { |
| 56 | if (sInstance == null) { |
| 57 | sInstance = new UnlockMethodCache(context); |
| 58 | } |
| 59 | return sInstance; |
| 60 | } |
| 61 | |
| 62 | /** |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 63 | * @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] | 64 | */ |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 65 | public boolean isMethodSecure() { |
| 66 | return mSecure; |
| 67 | } |
| 68 | |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 69 | public boolean isTrusted() { |
| 70 | return mTrusted; |
| 71 | } |
| 72 | |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 73 | /** |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 74 | * @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] | 75 | */ |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 76 | public boolean canSkipBouncer() { |
| 77 | return mCanSkipBouncer; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | public void addListener(OnUnlockMethodChangedListener listener) { |
| 81 | mListeners.add(listener); |
| 82 | } |
| 83 | |
| 84 | public void removeListener(OnUnlockMethodChangedListener listener) { |
| 85 | mListeners.remove(listener); |
| 86 | } |
| 87 | |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 88 | private void update(boolean updateAlways) { |
Nick Desaulniers | 1d39675 | 2016-07-25 15:05:33 -0700 | [diff] [blame] | 89 | Trace.beginSection("UnlockMethodCache#update"); |
Adrian Roos | d6aa6cb | 2015-04-16 19:31:29 -0700 | [diff] [blame] | 90 | int user = KeyguardUpdateMonitor.getCurrentUser(); |
Adrian Roos | 8150d2a | 2015-04-16 17:11:20 -0700 | [diff] [blame] | 91 | boolean secure = mLockPatternUtils.isSecure(user); |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 92 | boolean canSkipBouncer = !secure || mKeyguardUpdateMonitor.getUserCanSkipBouncer(user); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 93 | boolean trustManaged = mKeyguardUpdateMonitor.getUserTrustIsManaged(user); |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 94 | boolean trusted = mKeyguardUpdateMonitor.getUserHasTrust(user); |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 95 | boolean faceUnlockRunning = mKeyguardUpdateMonitor.isFaceUnlockRunning(user) |
| 96 | && trustManaged; |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 97 | boolean changed = secure != mSecure || canSkipBouncer != mCanSkipBouncer || |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 98 | trustManaged != mTrustManaged || faceUnlockRunning != mFaceUnlockRunning; |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 99 | if (changed || updateAlways) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 100 | mSecure = secure; |
Selim Cinek | e8bae62 | 2015-07-15 13:24:06 -0700 | [diff] [blame] | 101 | mCanSkipBouncer = canSkipBouncer; |
| 102 | mTrusted = trusted; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 103 | mTrustManaged = trustManaged; |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 104 | mFaceUnlockRunning = faceUnlockRunning; |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 105 | notifyListeners(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 106 | } |
Nick Desaulniers | 1d39675 | 2016-07-25 15:05:33 -0700 | [diff] [blame] | 107 | Trace.endSection(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 110 | private void notifyListeners() { |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 111 | for (OnUnlockMethodChangedListener listener : mListeners) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 112 | listener.onUnlockMethodStateChanged(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | private final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() { |
| 117 | @Override |
| 118 | public void onUserSwitchComplete(int userId) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 119 | update(false /* updateAlways */); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void onTrustChanged(int userId) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 124 | update(false /* updateAlways */); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 128 | public void onTrustManagedChanged(int userId) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 129 | update(false /* updateAlways */); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 133 | public void onStartedWakingUp() { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 134 | update(false /* updateAlways */); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 135 | } |
Jim Miller | f41fc96 | 2014-06-18 16:33:51 -0700 | [diff] [blame] | 136 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 137 | @Override |
Jorim Jaggi | 83eb6bb | 2015-08-17 17:38:58 -0700 | [diff] [blame] | 138 | public void onFingerprintAuthenticated(int userId) { |
Nick Desaulniers | 1d39675 | 2016-07-25 15:05:33 -0700 | [diff] [blame] | 139 | Trace.beginSection("KeyguardUpdateMonitorCallback#onFingerprintAuthenticated"); |
Selim Cinek | 1fcafc4 | 2015-07-20 14:39:25 -0700 | [diff] [blame] | 140 | if (!mKeyguardUpdateMonitor.isUnlockingWithFingerprintAllowed()) { |
Nick Desaulniers | 1d39675 | 2016-07-25 15:05:33 -0700 | [diff] [blame] | 141 | Trace.endSection(); |
Selim Cinek | 1fcafc4 | 2015-07-20 14:39:25 -0700 | [diff] [blame] | 142 | return; |
| 143 | } |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 144 | update(false /* updateAlways */); |
Nick Desaulniers | 1d39675 | 2016-07-25 15:05:33 -0700 | [diff] [blame] | 145 | Trace.endSection(); |
Jim Miller | f41fc96 | 2014-06-18 16:33:51 -0700 | [diff] [blame] | 146 | } |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 147 | |
| 148 | @Override |
| 149 | public void onFaceUnlockStateChanged(boolean running, int userId) { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 150 | update(false /* updateAlways */); |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 151 | } |
Jorim Jaggi | 25b4d4b | 2015-08-11 15:54:06 -0700 | [diff] [blame] | 152 | |
| 153 | @Override |
Adrian Roos | 1de8bcb | 2015-08-19 16:52:52 -0700 | [diff] [blame] | 154 | public void onStrongAuthStateChanged(int userId) { |
Jorim Jaggi | 25b4d4b | 2015-08-11 15:54:06 -0700 | [diff] [blame] | 155 | update(false /* updateAlways */); |
| 156 | } |
Kevin Chyn | 08ae664 | 2017-08-30 19:02:43 -0700 | [diff] [blame] | 157 | |
| 158 | @Override |
| 159 | public void onScreenTurnedOff() { |
| 160 | update(false /* updateAlways */); |
| 161 | } |
Kevin Chyn | 94f8035 | 2017-09-08 17:39:01 -0700 | [diff] [blame^] | 162 | |
| 163 | @Override |
| 164 | public void onKeyguardVisibilityChanged(boolean showing) { |
| 165 | update(false /* updateAlways */); |
| 166 | } |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 167 | }; |
| 168 | |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 169 | public boolean isTrustManaged() { |
| 170 | return mTrustManaged; |
| 171 | } |
| 172 | |
Adrian Roos | 4a41017 | 2014-08-20 17:41:44 +0200 | [diff] [blame] | 173 | public boolean isFaceUnlockRunning() { |
| 174 | return mFaceUnlockRunning; |
| 175 | } |
| 176 | |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 177 | public static interface OnUnlockMethodChangedListener { |
Christoph Studer | 2231c6e | 2014-12-19 12:40:13 +0100 | [diff] [blame] | 178 | void onUnlockMethodStateChanged(); |
Jorim Jaggi | ecc798e | 2014-05-26 18:14:37 +0200 | [diff] [blame] | 179 | } |
| 180 | } |