blob: d10a3fede4123bccd3c3895fa5a82a17ca97ad71 [file] [log] [blame]
Kevin Chynaae4a152018-01-18 11:48:09 -08001/*
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 Chynf8688a02019-08-27 17:04:05 -070014 * limitations under the License.
Kevin Chynaae4a152018-01-18 11:48:09 -080015 */
16
Kevin Chyne9275662018-07-23 16:42:06 -070017package com.android.systemui.biometrics;
Kevin Chynaae4a152018-01-18 11:48:09 -080018
Kevin Chyn050315f2019-08-08 14:22:54 -070019import android.app.ActivityManager;
20import android.app.ActivityTaskManager;
21import android.app.IActivityTaskManager;
22import android.app.TaskStackListener;
Kevin Chyn42653e82018-01-19 14:15:46 -080023import android.content.Context;
Kevin Chynaae4a152018-01-18 11:48:09 -080024import android.content.pm.PackageManager;
Gus Prevasa7df7b22018-10-30 10:29:34 -040025import android.content.res.Configuration;
Vishwath Mohanecf00ce2018-04-05 10:28:24 -070026import android.hardware.biometrics.BiometricPrompt;
Kevin Chyn23289ef2018-11-28 16:32:36 -080027import android.hardware.biometrics.IBiometricServiceReceiverInternal;
Kevin Chynaae4a152018-01-18 11:48:09 -080028import android.os.Bundle;
Kevin Chyn050315f2019-08-08 14:22:54 -070029import android.os.Handler;
30import android.os.Looper;
Kevin Chyn42653e82018-01-19 14:15:46 -080031import android.os.RemoteException;
Kevin Chynaae4a152018-01-18 11:48:09 -080032import android.util.Log;
Kevin Chyn42653e82018-01-19 14:15:46 -080033import android.view.WindowManager;
Kevin Chynaae4a152018-01-18 11:48:09 -080034
Kevin Chyn050315f2019-08-08 14:22:54 -070035import com.android.internal.annotations.VisibleForTesting;
Kevin Chyn42653e82018-01-19 14:15:46 -080036import com.android.internal.os.SomeArgs;
Kevin Chynaae4a152018-01-18 11:48:09 -080037import com.android.systemui.SystemUI;
38import com.android.systemui.statusbar.CommandQueue;
39
Kevin Chyn050315f2019-08-08 14:22:54 -070040import java.util.List;
41
Kevin Chyne9275662018-07-23 16:42:06 -070042/**
Kevin Chync53d9812019-07-30 18:10:30 -070043 * Receives messages sent from {@link com.android.server.biometrics.BiometricService} and shows the
44 * appropriate biometric UI (e.g. BiometricDialogView).
Kevin Chyne9275662018-07-23 16:42:06 -070045 */
Kevin Chynf8688a02019-08-27 17:04:05 -070046public class AuthController extends SystemUI implements CommandQueue.Callbacks,
47 AuthDialogCallback {
Kevin Chynfc468262019-08-20 17:17:11 -070048
Kevin Chynf8688a02019-08-27 17:04:05 -070049 private static final String TAG = "BiometricPrompt/AuthController";
Kevin Chyn42653e82018-01-19 14:15:46 -080050 private static final boolean DEBUG = true;
51
Kevin Chyn050315f2019-08-08 14:22:54 -070052 private final Injector mInjector;
53
Kevin Chync53d9812019-07-30 18:10:30 -070054 // TODO: These should just be saved from onSaveState
Gus Prevasa7df7b22018-10-30 10:29:34 -040055 private SomeArgs mCurrentDialogArgs;
Kevin Chyn050315f2019-08-08 14:22:54 -070056 @VisibleForTesting
Kevin Chynf8688a02019-08-27 17:04:05 -070057 AuthDialog mCurrentDialog;
Kevin Chync53d9812019-07-30 18:10:30 -070058
Kevin Chyn050315f2019-08-08 14:22:54 -070059 private Handler mHandler = new Handler(Looper.getMainLooper());
Kevin Chyn42653e82018-01-19 14:15:46 -080060 private WindowManager mWindowManager;
Kevin Chyn050315f2019-08-08 14:22:54 -070061 @VisibleForTesting
62 IActivityTaskManager mActivityTaskManager;
63 @VisibleForTesting
64 BiometricTaskStackListener mTaskStackListener;
65 @VisibleForTesting
66 IBiometricServiceReceiverInternal mReceiver;
67
68 public class BiometricTaskStackListener extends TaskStackListener {
69 @Override
70 public void onTaskStackChanged() {
71 mHandler.post(mTaskStackChangedRunnable);
72 }
73 }
74
75 private final Runnable mTaskStackChangedRunnable = () -> {
76 if (mCurrentDialog != null) {
77 try {
78 final String clientPackage = mCurrentDialog.getOpPackageName();
79 Log.w(TAG, "Task stack changed, current client: " + clientPackage);
80 final List<ActivityManager.RunningTaskInfo> runningTasks =
81 mActivityTaskManager.getTasks(1);
82 if (!runningTasks.isEmpty()) {
83 final String topPackage = runningTasks.get(0).topActivity.getPackageName();
84 if (!topPackage.contentEquals(clientPackage)) {
85 Log.w(TAG, "Evicting client due to: " + topPackage);
86 mCurrentDialog.dismissWithoutCallback(true /* animate */);
87 mCurrentDialog = null;
88 mReceiver.onDialogDismissed(BiometricPrompt.DISMISSED_REASON_USER_CANCEL);
89 mReceiver = null;
90 }
91 }
92 } catch (RemoteException e) {
93 Log.e(TAG, "Remote exception", e);
94 }
95 }
96 };
Kevin Chyn42653e82018-01-19 14:15:46 -080097
Kevin Chync53d9812019-07-30 18:10:30 -070098 @Override
99 public void onTryAgainPressed() {
100 try {
101 mReceiver.onTryAgainPressed();
102 } catch (RemoteException e) {
103 Log.e(TAG, "RemoteException when handling try again", e);
Kevin Chyn5906c172018-07-23 15:43:02 -0700104 }
105 }
106
Kevin Chync53d9812019-07-30 18:10:30 -0700107 @Override
Kevin Chyn050315f2019-08-08 14:22:54 -0700108 public void onDismissed(@DismissedReason int reason) {
Kevin Chync53d9812019-07-30 18:10:30 -0700109 switch (reason) {
Kevin Chynf8688a02019-08-27 17:04:05 -0700110 case AuthDialogCallback.DISMISSED_USER_CANCELED:
Kevin Chync53d9812019-07-30 18:10:30 -0700111 sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_USER_CANCEL);
112 break;
113
Kevin Chynf8688a02019-08-27 17:04:05 -0700114 case AuthDialogCallback.DISMISSED_BUTTON_NEGATIVE:
Kevin Chync53d9812019-07-30 18:10:30 -0700115 sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_NEGATIVE);
116 break;
117
Kevin Chynf8688a02019-08-27 17:04:05 -0700118 case AuthDialogCallback.DISMISSED_BUTTON_POSITIVE:
Kevin Chync53d9812019-07-30 18:10:30 -0700119 sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_CONFIRMED);
120 break;
121
Kevin Chynf8688a02019-08-27 17:04:05 -0700122 case AuthDialogCallback.DISMISSED_AUTHENTICATED:
Kevin Chync53d9812019-07-30 18:10:30 -0700123 sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_CONFIRM_NOT_REQUIRED);
Kevin Chync53d9812019-07-30 18:10:30 -0700124 break;
125
Kevin Chynf8688a02019-08-27 17:04:05 -0700126 case AuthDialogCallback.DISMISSED_ERROR:
Kevin Chync53d9812019-07-30 18:10:30 -0700127 sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_ERROR);
Kevin Chync53d9812019-07-30 18:10:30 -0700128 break;
Kevin Chyn050315f2019-08-08 14:22:54 -0700129
Kevin Chynf8688a02019-08-27 17:04:05 -0700130 case AuthDialogCallback.DISMISSED_BY_SYSTEM_SERVER:
Kevin Chyn050315f2019-08-08 14:22:54 -0700131 sendResultAndCleanUp(BiometricPrompt.DISMISSED_REASON_SERVER_REQUESTED);
132 break;
133
Kevin Chync53d9812019-07-30 18:10:30 -0700134 default:
135 Log.e(TAG, "Unhandled reason: " + reason);
136 break;
Kevin Chync94b7db2019-05-15 17:28:16 -0700137 }
Kevin Chync53d9812019-07-30 18:10:30 -0700138 }
139
Kevin Chyn050315f2019-08-08 14:22:54 -0700140 private void sendResultAndCleanUp(@DismissedReason int reason) {
Kevin Chync53d9812019-07-30 18:10:30 -0700141 if (mReceiver == null) {
142 Log.e(TAG, "Receiver is null");
143 return;
144 }
145 try {
Kevin Chyn050315f2019-08-08 14:22:54 -0700146 mReceiver.onDialogDismissed(reason);
Kevin Chync53d9812019-07-30 18:10:30 -0700147 } catch (RemoteException e) {
148 Log.w(TAG, "Remote exception", e);
149 }
Kevin Chyn050315f2019-08-08 14:22:54 -0700150 onDialogDismissed(reason);
151 }
152
153 public static class Injector {
154 IActivityTaskManager getActivityTaskManager() {
155 return ActivityTaskManager.getService();
156 }
157 }
158
Kevin Chynf8688a02019-08-27 17:04:05 -0700159 public AuthController() {
Kevin Chyn050315f2019-08-08 14:22:54 -0700160 this(new Injector());
161 }
162
163 @VisibleForTesting
Kevin Chynf8688a02019-08-27 17:04:05 -0700164 AuthController(Injector injector) {
Kevin Chyn050315f2019-08-08 14:22:54 -0700165 mInjector = injector;
Kevin Chync53d9812019-07-30 18:10:30 -0700166 }
Kevin Chync94b7db2019-05-15 17:28:16 -0700167
Kevin Chynaae4a152018-01-18 11:48:09 -0800168 @Override
169 public void start() {
Kevin Chyne1912712019-01-04 14:22:34 -0800170 final PackageManager pm = mContext.getPackageManager();
171 if (pm.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)
172 || pm.hasSystemFeature(PackageManager.FEATURE_FACE)
173 || pm.hasSystemFeature(PackageManager.FEATURE_IRIS)) {
Jason Monkd7c98552018-12-04 11:14:50 -0500174 getComponent(CommandQueue.class).addCallback(this);
Gus Prevasa7df7b22018-10-30 10:29:34 -0400175 mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Kevin Chyn050315f2019-08-08 14:22:54 -0700176 mActivityTaskManager = mInjector.getActivityTaskManager();
177
178 try {
179 mTaskStackListener = new BiometricTaskStackListener();
180 mActivityTaskManager.registerTaskStackListener(mTaskStackListener);
181 } catch (RemoteException e) {
182 Log.w(TAG, "Unable to register task stack listener", e);
183 }
Gus Prevasa7df7b22018-10-30 10:29:34 -0400184 }
185 }
186
Kevin Chynaae4a152018-01-18 11:48:09 -0800187 @Override
Kevin Chyn23289ef2018-11-28 16:32:36 -0800188 public void showBiometricDialog(Bundle bundle, IBiometricServiceReceiverInternal receiver,
Kevin Chyn050315f2019-08-08 14:22:54 -0700189 int type, boolean requireConfirmation, int userId, String opPackageName) {
Kevin Chyn158fefb2019-01-03 18:59:05 -0800190 if (DEBUG) {
191 Log.d(TAG, "showBiometricDialog, type: " + type
192 + ", requireConfirmation: " + requireConfirmation);
193 }
Kevin Chyn42653e82018-01-19 14:15:46 -0800194 SomeArgs args = SomeArgs.obtain();
195 args.arg1 = bundle;
196 args.arg2 = receiver;
Kevin Chyn6cf54e82018-09-18 19:13:27 -0700197 args.argi1 = type;
198 args.arg3 = requireConfirmation;
Kevin Chyn1b9f8df2018-11-12 19:04:55 -0800199 args.argi2 = userId;
Kevin Chyn050315f2019-08-08 14:22:54 -0700200 args.arg4 = opPackageName;
Kevin Chync53d9812019-07-30 18:10:30 -0700201
202 boolean skipAnimation = false;
203 if (mCurrentDialog != null) {
204 Log.w(TAG, "mCurrentDialog: " + mCurrentDialog);
205 skipAnimation = true;
206 }
Kevin Chyn050315f2019-08-08 14:22:54 -0700207 showDialog(args, skipAnimation, null /* savedState */);
Kevin Chynaae4a152018-01-18 11:48:09 -0800208 }
209
210 @Override
Kevin Chyne674e852019-04-24 12:39:40 -0700211 public void onBiometricAuthenticated(boolean authenticated, String failureReason) {
212 if (DEBUG) Log.d(TAG, "onBiometricAuthenticated: " + authenticated
213 + " reason: " + failureReason);
214
Kevin Chyne1912712019-01-04 14:22:34 -0800215 if (authenticated) {
Kevin Chync53d9812019-07-30 18:10:30 -0700216 mCurrentDialog.onAuthenticationSucceeded();
Kevin Chyn6cf54e82018-09-18 19:13:27 -0700217 } else {
Kevin Chyn3b53d6f2019-05-01 11:49:05 -0700218 mCurrentDialog.onAuthenticationFailed(failureReason);
Kevin Chyn6cf54e82018-09-18 19:13:27 -0700219 }
Kevin Chyn42653e82018-01-19 14:15:46 -0800220 }
221
Kevin Chync53d9812019-07-30 18:10:30 -0700222 @Override
223 public void onBiometricHelp(String message) {
224 if (DEBUG) Log.d(TAG, "onBiometricHelp: " + message);
225
226 mCurrentDialog.onHelp(message);
Kevin Chyn42653e82018-01-19 14:15:46 -0800227 }
228
Kevin Chync53d9812019-07-30 18:10:30 -0700229 @Override
230 public void onBiometricError(String error) {
231 if (DEBUG) Log.d(TAG, "onBiometricError: " + error);
232 mCurrentDialog.onError(error);
Kevin Chyn42653e82018-01-19 14:15:46 -0800233 }
234
Kevin Chync53d9812019-07-30 18:10:30 -0700235 @Override
236 public void hideBiometricDialog() {
237 if (DEBUG) Log.d(TAG, "hideBiometricDialog");
238
Kevin Chyn050315f2019-08-08 14:22:54 -0700239 mCurrentDialog.dismissFromSystemServer();
Kevin Chync53d9812019-07-30 18:10:30 -0700240 }
241
242 private void showDialog(SomeArgs args, boolean skipAnimation, Bundle savedState) {
243 mCurrentDialogArgs = args;
244 final int type = args.argi1;
Kevin Chyn050315f2019-08-08 14:22:54 -0700245 final Bundle biometricPromptBundle = (Bundle) args.arg1;
246 final boolean requireConfirmation = (boolean) args.arg3;
247 final int userId = args.argi2;
248 final String opPackageName = (String) args.arg4;
Kevin Chync53d9812019-07-30 18:10:30 -0700249
250 // Create a new dialog but do not replace the current one yet.
Kevin Chynf8688a02019-08-27 17:04:05 -0700251 final AuthDialog newDialog = buildDialog(
Kevin Chyn050315f2019-08-08 14:22:54 -0700252 biometricPromptBundle,
253 requireConfirmation,
254 userId,
255 type,
Kevin Chynfc468262019-08-20 17:17:11 -0700256 opPackageName,
257 skipAnimation);
Kevin Chync53d9812019-07-30 18:10:30 -0700258
259 if (newDialog == null) {
260 Log.e(TAG, "Unsupported type: " + type);
Kevin Chyn42653e82018-01-19 14:15:46 -0800261 return;
262 }
Kevin Chync53d9812019-07-30 18:10:30 -0700263
264 if (DEBUG) {
265 Log.d(TAG, "showDialog, "
266 + " savedState: " + savedState
267 + " mCurrentDialog: " + mCurrentDialog
268 + " newDialog: " + newDialog
269 + " type: " + type);
270 }
271
Kevin Chyn9cf89912019-08-30 13:33:58 -0700272 if (mCurrentDialog != null) {
Kevin Chync53d9812019-07-30 18:10:30 -0700273 // If somehow we're asked to show a dialog, the old one doesn't need to be animated
274 // away. This can happen if the app cancels and re-starts auth during configuration
275 // change. This is ugly because we also have to do things on onConfigurationChanged
276 // here.
277 mCurrentDialog.dismissWithoutCallback(false /* animate */);
278 }
279
280 mReceiver = (IBiometricServiceReceiverInternal) args.arg2;
281 mCurrentDialog = newDialog;
Kevin Chyn9cf89912019-08-30 13:33:58 -0700282 mCurrentDialog.show(mWindowManager, savedState);
Kevin Chync53d9812019-07-30 18:10:30 -0700283 }
284
Kevin Chyn050315f2019-08-08 14:22:54 -0700285 private void onDialogDismissed(@DismissedReason int reason) {
286 if (DEBUG) Log.d(TAG, "onDialogDismissed: " + reason);
Kevin Chync53d9812019-07-30 18:10:30 -0700287 if (mCurrentDialog == null) {
288 Log.w(TAG, "Dialog already dismissed");
Kevin Chyn42653e82018-01-19 14:15:46 -0800289 }
290 mReceiver = null;
Kevin Chync53d9812019-07-30 18:10:30 -0700291 mCurrentDialog = null;
Kevin Chyn23289ef2018-11-28 16:32:36 -0800292 }
293
Gus Prevasa7df7b22018-10-30 10:29:34 -0400294 @Override
295 protected void onConfigurationChanged(Configuration newConfig) {
Kevin Chyn02129b12018-11-01 16:47:12 -0700296 super.onConfigurationChanged(newConfig);
Kevin Chyne1912712019-01-04 14:22:34 -0800297
298 // Save the state of the current dialog (buttons showing, etc)
Kevin Chyne1912712019-01-04 14:22:34 -0800299 if (mCurrentDialog != null) {
Kevin Chync53d9812019-07-30 18:10:30 -0700300 final Bundle savedState = new Bundle();
Kevin Chyne1912712019-01-04 14:22:34 -0800301 mCurrentDialog.onSaveState(savedState);
Kevin Chync53d9812019-07-30 18:10:30 -0700302 mCurrentDialog.dismissWithoutCallback(false /* animate */);
303 mCurrentDialog = null;
Kevin Chyne1912712019-01-04 14:22:34 -0800304
Kevin Chyn27da7182019-09-11 12:17:55 -0700305 // Only show the dialog if necessary. If it was animating out, the dialog is supposed
306 // to send its pending callback immediately.
307 if (savedState.getInt(AuthDialog.KEY_CONTAINER_STATE)
308 != AuthContainerView.STATE_ANIMATING_OUT) {
309 showDialog(mCurrentDialogArgs, true /* skipAnimation */, savedState);
310 }
Gus Prevasa7df7b22018-10-30 10:29:34 -0400311 }
Kevin Chync53d9812019-07-30 18:10:30 -0700312 }
Kevin Chyne1912712019-01-04 14:22:34 -0800313
Kevin Chynf8688a02019-08-27 17:04:05 -0700314 protected AuthDialog buildDialog(Bundle biometricPromptBundle, boolean requireConfirmation,
Kevin Chynfc468262019-08-20 17:17:11 -0700315 int userId, int type, String opPackageName, boolean skipIntro) {
Kevin Chynd837ced2019-09-11 16:09:43 -0700316 return new AuthContainerView.Builder(mContext)
317 .setCallback(this)
318 .setBiometricPromptBundle(biometricPromptBundle)
319 .setRequireConfirmation(requireConfirmation)
320 .setUserId(userId)
321 .setOpPackageName(opPackageName)
322 .setSkipIntro(skipIntro)
323 .build(type);
Gus Prevasa7df7b22018-10-30 10:29:34 -0400324 }
Kevin Chynaae4a152018-01-18 11:48:09 -0800325}