Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 14 | * limitations under the License. |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 17 | package com.android.systemui.biometrics; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 18 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 19 | import static android.hardware.biometrics.BiometricAuthenticator.TYPE_FACE; |
| 20 | import static android.hardware.biometrics.BiometricAuthenticator.TYPE_FINGERPRINT; |
Curtis Belmonte | 13eb581 | 2019-10-22 14:17:30 -0700 | [diff] [blame] | 21 | import static android.hardware.biometrics.BiometricManager.Authenticators; |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 22 | |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 24 | import android.app.ActivityManager; |
| 25 | import android.app.ActivityTaskManager; |
| 26 | import android.app.IActivityTaskManager; |
| 27 | import android.app.TaskStackListener; |
Kevin Chyn | e181b8d | 2019-11-05 15:02:52 -0800 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Kevin Chyn | e181b8d | 2019-11-05 15:02:52 -0800 | [diff] [blame] | 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
Gus Prevas | a7df7b2 | 2018-10-30 10:29:34 -0400 | [diff] [blame] | 32 | import android.content.res.Configuration; |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 33 | import android.hardware.biometrics.BiometricConstants; |
Vishwath Mohan | ecf00ce | 2018-04-05 10:28:24 -0700 | [diff] [blame] | 34 | import android.hardware.biometrics.BiometricPrompt; |
Kevin Chyn | 23289ef | 2018-11-28 16:32:36 -0800 | [diff] [blame] | 35 | import android.hardware.biometrics.IBiometricServiceReceiverInternal; |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 36 | import android.hardware.face.FaceManager; |
| 37 | import android.hardware.fingerprint.FingerprintManager; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 38 | import android.os.Bundle; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 39 | import android.os.Handler; |
| 40 | import android.os.Looper; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 41 | import android.os.RemoteException; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 42 | import android.util.Log; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 43 | import android.view.WindowManager; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 44 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 45 | import com.android.internal.R; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 46 | import com.android.internal.annotations.VisibleForTesting; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 47 | import com.android.internal.os.SomeArgs; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 48 | import com.android.systemui.SystemUI; |
| 49 | import com.android.systemui.statusbar.CommandQueue; |
| 50 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 51 | import java.util.List; |
| 52 | |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 53 | import javax.inject.Inject; |
| 54 | import javax.inject.Singleton; |
| 55 | |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 56 | /** |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 57 | * Receives messages sent from {@link com.android.server.biometrics.BiometricService} and shows the |
| 58 | * appropriate biometric UI (e.g. BiometricDialogView). |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 59 | */ |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 60 | @Singleton |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 61 | public class AuthController extends SystemUI implements CommandQueue.Callbacks, |
| 62 | AuthDialogCallback { |
Kevin Chyn | fc46826 | 2019-08-20 17:17:11 -0700 | [diff] [blame] | 63 | |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 64 | private static final String TAG = "BiometricPrompt/AuthController"; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 65 | private static final boolean DEBUG = true; |
| 66 | |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 67 | private final CommandQueue mCommandQueue; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 68 | private final Injector mInjector; |
| 69 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 70 | // TODO: These should just be saved from onSaveState |
Gus Prevas | a7df7b2 | 2018-10-30 10:29:34 -0400 | [diff] [blame] | 71 | private SomeArgs mCurrentDialogArgs; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 72 | @VisibleForTesting |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 73 | AuthDialog mCurrentDialog; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 74 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 75 | private Handler mHandler = new Handler(Looper.getMainLooper()); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 76 | private WindowManager mWindowManager; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 77 | @VisibleForTesting |
| 78 | IActivityTaskManager mActivityTaskManager; |
| 79 | @VisibleForTesting |
| 80 | BiometricTaskStackListener mTaskStackListener; |
| 81 | @VisibleForTesting |
| 82 | IBiometricServiceReceiverInternal mReceiver; |
| 83 | |
| 84 | public class BiometricTaskStackListener extends TaskStackListener { |
| 85 | @Override |
| 86 | public void onTaskStackChanged() { |
| 87 | mHandler.post(mTaskStackChangedRunnable); |
| 88 | } |
| 89 | } |
| 90 | |
Kevin Chyn | e181b8d | 2019-11-05 15:02:52 -0800 | [diff] [blame] | 91 | @VisibleForTesting |
| 92 | final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 93 | @Override |
| 94 | public void onReceive(Context context, Intent intent) { |
| 95 | if (mCurrentDialog != null |
| 96 | && Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) { |
| 97 | Log.w(TAG, "ACTION_CLOSE_SYSTEM_DIALOGS received"); |
| 98 | mCurrentDialog.dismissWithoutCallback(true /* animate */); |
| 99 | mCurrentDialog = null; |
| 100 | |
| 101 | try { |
| 102 | if (mReceiver != null) { |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 103 | mReceiver.onDialogDismissed(BiometricPrompt.DISMISSED_REASON_USER_CANCEL, |
| 104 | null /* credentialAttestation */); |
Kevin Chyn | e181b8d | 2019-11-05 15:02:52 -0800 | [diff] [blame] | 105 | mReceiver = null; |
| 106 | } |
| 107 | } catch (RemoteException e) { |
| 108 | Log.e(TAG, "Remote exception", e); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | }; |
| 113 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 114 | private final Runnable mTaskStackChangedRunnable = () -> { |
| 115 | if (mCurrentDialog != null) { |
| 116 | try { |
| 117 | final String clientPackage = mCurrentDialog.getOpPackageName(); |
| 118 | Log.w(TAG, "Task stack changed, current client: " + clientPackage); |
| 119 | final List<ActivityManager.RunningTaskInfo> runningTasks = |
| 120 | mActivityTaskManager.getTasks(1); |
| 121 | if (!runningTasks.isEmpty()) { |
| 122 | final String topPackage = runningTasks.get(0).topActivity.getPackageName(); |
| 123 | if (!topPackage.contentEquals(clientPackage)) { |
| 124 | Log.w(TAG, "Evicting client due to: " + topPackage); |
| 125 | mCurrentDialog.dismissWithoutCallback(true /* animate */); |
| 126 | mCurrentDialog = null; |
Curtis Belmonte | ffa9d87 | 2019-10-24 12:55:01 -0700 | [diff] [blame] | 127 | if (mReceiver != null) { |
| 128 | mReceiver.onDialogDismissed( |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 129 | BiometricPrompt.DISMISSED_REASON_USER_CANCEL, |
| 130 | null /* credentialAttestation */); |
Curtis Belmonte | ffa9d87 | 2019-10-24 12:55:01 -0700 | [diff] [blame] | 131 | mReceiver = null; |
| 132 | } |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } catch (RemoteException e) { |
| 136 | Log.e(TAG, "Remote exception", e); |
| 137 | } |
| 138 | } |
| 139 | }; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 140 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 141 | @Override |
| 142 | public void onTryAgainPressed() { |
Curtis Belmonte | ffa9d87 | 2019-10-24 12:55:01 -0700 | [diff] [blame] | 143 | if (mReceiver == null) { |
| 144 | Log.e(TAG, "onTryAgainPressed: Receiver is null"); |
| 145 | return; |
| 146 | } |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 147 | try { |
| 148 | mReceiver.onTryAgainPressed(); |
| 149 | } catch (RemoteException e) { |
| 150 | Log.e(TAG, "RemoteException when handling try again", e); |
Kevin Chyn | 5906c17 | 2018-07-23 15:43:02 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 154 | @Override |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 155 | public void onDeviceCredentialPressed() { |
Curtis Belmonte | ffa9d87 | 2019-10-24 12:55:01 -0700 | [diff] [blame] | 156 | if (mReceiver == null) { |
| 157 | Log.e(TAG, "onDeviceCredentialPressed: Receiver is null"); |
| 158 | return; |
| 159 | } |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 160 | try { |
| 161 | mReceiver.onDeviceCredentialPressed(); |
| 162 | } catch (RemoteException e) { |
| 163 | Log.e(TAG, "RemoteException when handling credential button", e); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | @Override |
Kevin Chyn | 0a45b66 | 2020-03-27 10:15:50 -0700 | [diff] [blame] | 168 | public void onSystemEvent(int event) { |
| 169 | if (mReceiver == null) { |
| 170 | Log.e(TAG, "onSystemEvent(" + event + "): Receiver is null"); |
| 171 | return; |
| 172 | } |
| 173 | try { |
| 174 | mReceiver.onSystemEvent(event); |
| 175 | } catch (RemoteException e) { |
| 176 | Log.e(TAG, "RemoteException when sending system event", e); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | @Override |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 181 | public void onDismissed(@DismissedReason int reason, @Nullable byte[] credentialAttestation) { |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 182 | switch (reason) { |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 183 | case AuthDialogCallback.DISMISSED_USER_CANCELED: |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 184 | sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_USER_CANCEL, |
| 185 | credentialAttestation); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 186 | break; |
| 187 | |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 188 | case AuthDialogCallback.DISMISSED_BUTTON_NEGATIVE: |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 189 | sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_NEGATIVE, |
| 190 | credentialAttestation); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 191 | break; |
| 192 | |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 193 | case AuthDialogCallback.DISMISSED_BUTTON_POSITIVE: |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 194 | sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_BIOMETRIC_CONFIRMED, |
| 195 | credentialAttestation); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 196 | break; |
| 197 | |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 198 | case AuthDialogCallback.DISMISSED_BIOMETRIC_AUTHENTICATED: |
| 199 | sendResultAndCleanUp( |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 200 | BiometricPrompt.DISMISSED_REASON_BIOMETRIC_CONFIRM_NOT_REQUIRED, |
| 201 | credentialAttestation); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 202 | break; |
| 203 | |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 204 | case AuthDialogCallback.DISMISSED_ERROR: |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 205 | sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_ERROR, |
| 206 | credentialAttestation); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 207 | break; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 208 | |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 209 | case AuthDialogCallback.DISMISSED_BY_SYSTEM_SERVER: |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 210 | sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_SERVER_REQUESTED, |
| 211 | credentialAttestation); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 212 | break; |
| 213 | |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 214 | case AuthDialogCallback.DISMISSED_CREDENTIAL_AUTHENTICATED: |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 215 | sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_CREDENTIAL_CONFIRMED, |
| 216 | credentialAttestation); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 217 | break; |
| 218 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 219 | default: |
| 220 | Log.e(TAG, "Unhandled reason: " + reason); |
| 221 | break; |
Kevin Chyn | c94b7db | 2019-05-15 17:28:16 -0700 | [diff] [blame] | 222 | } |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 225 | private void sendResultAndCleanUp(@DismissedReason int reason, |
| 226 | @Nullable byte[] credentialAttestation) { |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 227 | if (mReceiver == null) { |
Curtis Belmonte | ffa9d87 | 2019-10-24 12:55:01 -0700 | [diff] [blame] | 228 | Log.e(TAG, "sendResultAndCleanUp: Receiver is null"); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | try { |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 232 | mReceiver.onDialogDismissed(reason, credentialAttestation); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 233 | } catch (RemoteException e) { |
| 234 | Log.w(TAG, "Remote exception", e); |
| 235 | } |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 236 | onDialogDismissed(reason); |
| 237 | } |
| 238 | |
| 239 | public static class Injector { |
| 240 | IActivityTaskManager getActivityTaskManager() { |
| 241 | return ActivityTaskManager.getService(); |
| 242 | } |
| 243 | } |
| 244 | |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 245 | @Inject |
| 246 | public AuthController(Context context, CommandQueue commandQueue) { |
| 247 | this(context, commandQueue, new Injector()); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @VisibleForTesting |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 251 | AuthController(Context context, CommandQueue commandQueue, Injector injector) { |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 252 | super(context); |
Dave Mankoff | bcaca8a | 2019-10-31 18:04:08 -0400 | [diff] [blame] | 253 | mCommandQueue = commandQueue; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 254 | mInjector = injector; |
Kevin Chyn | e181b8d | 2019-11-05 15:02:52 -0800 | [diff] [blame] | 255 | |
| 256 | IntentFilter filter = new IntentFilter(); |
| 257 | filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 258 | |
| 259 | context.registerReceiver(mBroadcastReceiver, filter); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 260 | } |
Kevin Chyn | c94b7db | 2019-05-15 17:28:16 -0700 | [diff] [blame] | 261 | |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 262 | @Override |
| 263 | public void start() { |
Kevin Chyn | 4e6417d | 2020-02-20 12:10:21 -0800 | [diff] [blame] | 264 | mCommandQueue.addCallback(this); |
| 265 | mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); |
| 266 | mActivityTaskManager = mInjector.getActivityTaskManager(); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 267 | |
Kevin Chyn | 4e6417d | 2020-02-20 12:10:21 -0800 | [diff] [blame] | 268 | try { |
| 269 | mTaskStackListener = new BiometricTaskStackListener(); |
| 270 | mActivityTaskManager.registerTaskStackListener(mTaskStackListener); |
| 271 | } catch (RemoteException e) { |
| 272 | Log.w(TAG, "Unable to register task stack listener", e); |
Gus Prevas | a7df7b2 | 2018-10-30 10:29:34 -0400 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 276 | @Override |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 277 | public void showAuthenticationDialog(Bundle bundle, IBiometricServiceReceiverInternal receiver, |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 278 | int biometricModality, boolean requireConfirmation, int userId, String opPackageName, |
| 279 | long operationId) { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 280 | final int authenticators = Utils.getAuthenticators(bundle); |
| 281 | |
Kevin Chyn | 158fefb | 2019-01-03 18:59:05 -0800 | [diff] [blame] | 282 | if (DEBUG) { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 283 | Log.d(TAG, "showAuthenticationDialog, authenticators: " + authenticators |
| 284 | + ", biometricModality: " + biometricModality |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 285 | + ", requireConfirmation: " + requireConfirmation |
| 286 | + ", operationId: " + operationId); |
Kevin Chyn | 158fefb | 2019-01-03 18:59:05 -0800 | [diff] [blame] | 287 | } |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 288 | SomeArgs args = SomeArgs.obtain(); |
| 289 | args.arg1 = bundle; |
| 290 | args.arg2 = receiver; |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 291 | args.argi1 = biometricModality; |
Kevin Chyn | 6cf54e8 | 2018-09-18 19:13:27 -0700 | [diff] [blame] | 292 | args.arg3 = requireConfirmation; |
Kevin Chyn | 1b9f8df | 2018-11-12 19:04:55 -0800 | [diff] [blame] | 293 | args.argi2 = userId; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 294 | args.arg4 = opPackageName; |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 295 | args.arg5 = operationId; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 296 | |
| 297 | boolean skipAnimation = false; |
| 298 | if (mCurrentDialog != null) { |
| 299 | Log.w(TAG, "mCurrentDialog: " + mCurrentDialog); |
| 300 | skipAnimation = true; |
| 301 | } |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 302 | |
| 303 | showDialog(args, skipAnimation, null /* savedState */); |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 307 | public void onBiometricAuthenticated() { |
| 308 | mCurrentDialog.onAuthenticationSucceeded(); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 311 | @Override |
| 312 | public void onBiometricHelp(String message) { |
| 313 | if (DEBUG) Log.d(TAG, "onBiometricHelp: " + message); |
| 314 | |
| 315 | mCurrentDialog.onHelp(message); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 318 | private String getErrorString(int modality, int error, int vendorCode) { |
| 319 | switch (modality) { |
| 320 | case TYPE_FACE: |
| 321 | return FaceManager.getErrorString(mContext, error, vendorCode); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 322 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 323 | case TYPE_FINGERPRINT: |
| 324 | return FingerprintManager.getErrorString(mContext, error, vendorCode); |
| 325 | |
| 326 | default: |
| 327 | return ""; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void onBiometricError(int modality, int error, int vendorCode) { |
| 333 | if (DEBUG) { |
| 334 | Log.d(TAG, String.format("onBiometricError(%d, %d, %d)", modality, error, vendorCode)); |
| 335 | } |
| 336 | |
| 337 | final boolean isLockout = (error == BiometricConstants.BIOMETRIC_ERROR_LOCKOUT) |
| 338 | || (error == BiometricConstants.BIOMETRIC_ERROR_LOCKOUT_PERMANENT); |
| 339 | |
| 340 | // TODO(b/141025588): Create separate methods for handling hard and soft errors. |
| 341 | final boolean isSoftError = (error == BiometricConstants.BIOMETRIC_PAUSED_REJECTED |
| 342 | || error == BiometricConstants.BIOMETRIC_ERROR_TIMEOUT); |
| 343 | |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 344 | if (mCurrentDialog.isAllowDeviceCredentials() && isLockout) { |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 345 | if (DEBUG) Log.d(TAG, "onBiometricError, lockout"); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 346 | mCurrentDialog.animateToCredentialUI(); |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 347 | } else if (isSoftError) { |
| 348 | final String errorMessage = (error == BiometricConstants.BIOMETRIC_PAUSED_REJECTED) |
| 349 | ? mContext.getString(R.string.biometric_not_recognized) |
| 350 | : getErrorString(modality, error, vendorCode); |
| 351 | if (DEBUG) Log.d(TAG, "onBiometricError, soft error: " + errorMessage); |
| 352 | mCurrentDialog.onAuthenticationFailed(errorMessage); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 353 | } else { |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 354 | final String errorMessage = getErrorString(modality, error, vendorCode); |
| 355 | if (DEBUG) Log.d(TAG, "onBiometricError, hard error: " + errorMessage); |
| 356 | mCurrentDialog.onError(errorMessage); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 357 | } |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 360 | @Override |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 361 | public void hideAuthenticationDialog() { |
Kevin Chyn | a847a03 | 2020-01-17 14:17:03 -0800 | [diff] [blame] | 362 | if (DEBUG) Log.d(TAG, "hideAuthenticationDialog: " + mCurrentDialog); |
| 363 | |
| 364 | if (mCurrentDialog == null) { |
| 365 | // Could be possible if the caller canceled authentication after credential success |
| 366 | // but before the client was notified. |
| 367 | return; |
| 368 | } |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 369 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 370 | mCurrentDialog.dismissFromSystemServer(); |
Kevin Chyn | 2291072 | 2019-12-13 16:57:51 -0800 | [diff] [blame] | 371 | |
| 372 | // BiometricService will have already sent the callback to the client in this case. |
| 373 | // This avoids a round trip to SystemUI. So, just dismiss the dialog and we're done. |
| 374 | mCurrentDialog = null; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 377 | private void showDialog(SomeArgs args, boolean skipAnimation, Bundle savedState) { |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 378 | mCurrentDialogArgs = args; |
| 379 | final int type = args.argi1; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 380 | final Bundle biometricPromptBundle = (Bundle) args.arg1; |
| 381 | final boolean requireConfirmation = (boolean) args.arg3; |
| 382 | final int userId = args.argi2; |
| 383 | final String opPackageName = (String) args.arg4; |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 384 | final long operationId = (long) args.arg5; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 385 | |
| 386 | // Create a new dialog but do not replace the current one yet. |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 387 | final AuthDialog newDialog = buildDialog( |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 388 | biometricPromptBundle, |
| 389 | requireConfirmation, |
| 390 | userId, |
| 391 | type, |
Kevin Chyn | fc46826 | 2019-08-20 17:17:11 -0700 | [diff] [blame] | 392 | opPackageName, |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 393 | skipAnimation, |
| 394 | operationId); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 395 | |
| 396 | if (newDialog == null) { |
| 397 | Log.e(TAG, "Unsupported type: " + type); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 398 | return; |
| 399 | } |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 400 | |
| 401 | if (DEBUG) { |
Kevin Chyn | bc3347f | 2020-02-20 16:45:59 -0800 | [diff] [blame] | 402 | Log.d(TAG, "userId: " + userId |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 403 | + " savedState: " + savedState |
| 404 | + " mCurrentDialog: " + mCurrentDialog |
| 405 | + " newDialog: " + newDialog |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 406 | + " type: " + type); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Kevin Chyn | 9cf8991 | 2019-08-30 13:33:58 -0700 | [diff] [blame] | 409 | if (mCurrentDialog != null) { |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 410 | // If somehow we're asked to show a dialog, the old one doesn't need to be animated |
| 411 | // away. This can happen if the app cancels and re-starts auth during configuration |
| 412 | // change. This is ugly because we also have to do things on onConfigurationChanged |
| 413 | // here. |
| 414 | mCurrentDialog.dismissWithoutCallback(false /* animate */); |
| 415 | } |
| 416 | |
| 417 | mReceiver = (IBiometricServiceReceiverInternal) args.arg2; |
| 418 | mCurrentDialog = newDialog; |
Kevin Chyn | 9cf8991 | 2019-08-30 13:33:58 -0700 | [diff] [blame] | 419 | mCurrentDialog.show(mWindowManager, savedState); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 422 | private void onDialogDismissed(@DismissedReason int reason) { |
| 423 | if (DEBUG) Log.d(TAG, "onDialogDismissed: " + reason); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 424 | if (mCurrentDialog == null) { |
| 425 | Log.w(TAG, "Dialog already dismissed"); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 426 | } |
| 427 | mReceiver = null; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 428 | mCurrentDialog = null; |
Kevin Chyn | 23289ef | 2018-11-28 16:32:36 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Gus Prevas | a7df7b2 | 2018-10-30 10:29:34 -0400 | [diff] [blame] | 431 | @Override |
| 432 | protected void onConfigurationChanged(Configuration newConfig) { |
Kevin Chyn | 02129b1 | 2018-11-01 16:47:12 -0700 | [diff] [blame] | 433 | super.onConfigurationChanged(newConfig); |
Kevin Chyn | e191271 | 2019-01-04 14:22:34 -0800 | [diff] [blame] | 434 | |
| 435 | // Save the state of the current dialog (buttons showing, etc) |
Kevin Chyn | e191271 | 2019-01-04 14:22:34 -0800 | [diff] [blame] | 436 | if (mCurrentDialog != null) { |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 437 | final Bundle savedState = new Bundle(); |
Kevin Chyn | e191271 | 2019-01-04 14:22:34 -0800 | [diff] [blame] | 438 | mCurrentDialog.onSaveState(savedState); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 439 | mCurrentDialog.dismissWithoutCallback(false /* animate */); |
| 440 | mCurrentDialog = null; |
Kevin Chyn | e191271 | 2019-01-04 14:22:34 -0800 | [diff] [blame] | 441 | |
Kevin Chyn | 27da718 | 2019-09-11 12:17:55 -0700 | [diff] [blame] | 442 | // Only show the dialog if necessary. If it was animating out, the dialog is supposed |
| 443 | // to send its pending callback immediately. |
| 444 | if (savedState.getInt(AuthDialog.KEY_CONTAINER_STATE) |
| 445 | != AuthContainerView.STATE_ANIMATING_OUT) { |
Kevin Chyn | 8cbb488 | 2019-09-19 16:49:02 -0700 | [diff] [blame] | 446 | final boolean credentialShowing = |
| 447 | savedState.getBoolean(AuthDialog.KEY_CREDENTIAL_SHOWING); |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 448 | if (credentialShowing) { |
| 449 | // TODO: Clean this up |
| 450 | Bundle bundle = (Bundle) mCurrentDialogArgs.arg1; |
| 451 | bundle.putInt(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED, |
Curtis Belmonte | 13eb581 | 2019-10-22 14:17:30 -0700 | [diff] [blame] | 452 | Authenticators.DEVICE_CREDENTIAL); |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 453 | } |
Kevin Chyn | 8cbb488 | 2019-09-19 16:49:02 -0700 | [diff] [blame] | 454 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 455 | showDialog(mCurrentDialogArgs, true /* skipAnimation */, savedState); |
Kevin Chyn | 27da718 | 2019-09-11 12:17:55 -0700 | [diff] [blame] | 456 | } |
Gus Prevas | a7df7b2 | 2018-10-30 10:29:34 -0400 | [diff] [blame] | 457 | } |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 458 | } |
Kevin Chyn | e191271 | 2019-01-04 14:22:34 -0800 | [diff] [blame] | 459 | |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 460 | protected AuthDialog buildDialog(Bundle biometricPromptBundle, boolean requireConfirmation, |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 461 | int userId, int type, String opPackageName, boolean skipIntro, long operationId) { |
Kevin Chyn | d837ced | 2019-09-11 16:09:43 -0700 | [diff] [blame] | 462 | return new AuthContainerView.Builder(mContext) |
| 463 | .setCallback(this) |
| 464 | .setBiometricPromptBundle(biometricPromptBundle) |
| 465 | .setRequireConfirmation(requireConfirmation) |
| 466 | .setUserId(userId) |
| 467 | .setOpPackageName(opPackageName) |
| 468 | .setSkipIntro(skipIntro) |
Kevin Chyn | c8cb685 | 2020-03-10 18:29:15 -0700 | [diff] [blame] | 469 | .setOperationId(operationId) |
Kevin Chyn | d837ced | 2019-09-11 16:09:43 -0700 | [diff] [blame] | 470 | .build(type); |
Gus Prevas | a7df7b2 | 2018-10-30 10:29:34 -0400 | [diff] [blame] | 471 | } |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 472 | } |