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