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