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