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 |
| 14 | * limitations under the License |
| 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 | |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 20 | import android.content.pm.PackageManager; |
Vishwath Mohan | ecf00ce | 2018-04-05 10:28:24 -0700 | [diff] [blame] | 21 | import android.hardware.biometrics.BiometricPrompt; |
| 22 | import android.hardware.biometrics.IBiometricPromptReceiver; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 23 | import android.os.Bundle; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
| 26 | import android.os.RemoteException; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 27 | import android.util.Log; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 28 | import android.view.WindowManager; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 29 | |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 30 | import com.android.internal.os.SomeArgs; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 31 | import com.android.systemui.SystemUI; |
| 32 | import com.android.systemui.statusbar.CommandQueue; |
| 33 | |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Receives messages sent from AuthenticationClient and shows the appropriate biometric UI (e.g. |
| 36 | * FingerprintDialogView). |
| 37 | */ |
| 38 | public class BiometricDialogImpl extends SystemUI implements CommandQueue.Callbacks { |
Kevin Chyn | 5a2ff5d | 2018-08-29 19:07:30 -0700 | [diff] [blame] | 39 | private static final String TAG = "BiometricDialogImpl"; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 40 | private static final boolean DEBUG = true; |
| 41 | |
Kevin Chyn | 5906c17 | 2018-07-23 15:43:02 -0700 | [diff] [blame] | 42 | private static final int MSG_SHOW_DIALOG = 1; |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 43 | private static final int MSG_BIOMETRIC_AUTHENTICATED = 2; |
| 44 | private static final int MSG_BIOMETRIC_HELP = 3; |
| 45 | private static final int MSG_BIOMETRIC_ERROR = 4; |
| 46 | private static final int MSG_HIDE_DIALOG = 5; |
Kevin Chyn | 5906c17 | 2018-07-23 15:43:02 -0700 | [diff] [blame] | 47 | private static final int MSG_BUTTON_NEGATIVE = 6; |
| 48 | private static final int MSG_USER_CANCELED = 7; |
| 49 | private static final int MSG_BUTTON_POSITIVE = 8; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 50 | |
| 51 | private FingerprintDialogView mDialogView; |
| 52 | private WindowManager mWindowManager; |
Vishwath Mohan | ecf00ce | 2018-04-05 10:28:24 -0700 | [diff] [blame] | 53 | private IBiometricPromptReceiver mReceiver; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 54 | private boolean mDialogShowing; |
Kevin Chyn | 5906c17 | 2018-07-23 15:43:02 -0700 | [diff] [blame] | 55 | private Callback mCallback = new Callback(); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 56 | |
| 57 | private Handler mHandler = new Handler() { |
| 58 | @Override |
| 59 | public void handleMessage(Message msg) { |
| 60 | switch(msg.what) { |
| 61 | case MSG_SHOW_DIALOG: |
| 62 | handleShowDialog((SomeArgs) msg.obj); |
| 63 | break; |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 64 | case MSG_BIOMETRIC_AUTHENTICATED: |
| 65 | handleBiometricAuthenticated(); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 66 | break; |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 67 | case MSG_BIOMETRIC_HELP: |
| 68 | handleBiometricHelp((String) msg.obj); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 69 | break; |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 70 | case MSG_BIOMETRIC_ERROR: |
| 71 | handleBiometricError((String) msg.obj); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 72 | break; |
| 73 | case MSG_HIDE_DIALOG: |
| 74 | handleHideDialog((Boolean) msg.obj); |
| 75 | break; |
| 76 | case MSG_BUTTON_NEGATIVE: |
| 77 | handleButtonNegative(); |
| 78 | break; |
| 79 | case MSG_USER_CANCELED: |
| 80 | handleUserCanceled(); |
| 81 | break; |
| 82 | case MSG_BUTTON_POSITIVE: |
| 83 | handleButtonPositive(); |
| 84 | break; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | }; |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 88 | |
Kevin Chyn | 5906c17 | 2018-07-23 15:43:02 -0700 | [diff] [blame] | 89 | private class Callback implements DialogViewCallback { |
| 90 | @Override |
| 91 | public void onUserCanceled() { |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 92 | mHandler.obtainMessage(BiometricDialogImpl.MSG_USER_CANCELED).sendToTarget(); |
Kevin Chyn | 5906c17 | 2018-07-23 15:43:02 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onErrorShown() { |
| 97 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_HIDE_DIALOG, |
| 98 | false /* userCanceled */), BiometricPrompt.HIDE_DIALOG_DELAY); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void onNegativePressed() { |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 103 | mHandler.obtainMessage(BiometricDialogImpl.MSG_BUTTON_NEGATIVE).sendToTarget(); |
Kevin Chyn | 5906c17 | 2018-07-23 15:43:02 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onPositivePressed() { |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 108 | mHandler.obtainMessage(BiometricDialogImpl.MSG_BUTTON_POSITIVE).sendToTarget(); |
Kevin Chyn | 5906c17 | 2018-07-23 15:43:02 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 112 | @Override |
| 113 | public void start() { |
| 114 | if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { |
| 115 | return; |
| 116 | } |
| 117 | getComponent(CommandQueue.class).addCallbacks(this); |
Kevin Chyn | e8f3e1b | 2018-01-23 17:33:58 -0800 | [diff] [blame] | 118 | mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); |
Kevin Chyn | 5906c17 | 2018-07-23 15:43:02 -0700 | [diff] [blame] | 119 | mDialogView = new FingerprintDialogView(mContext, mCallback); |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Kevin Chyn | 5a2ff5d | 2018-08-29 19:07:30 -0700 | [diff] [blame] | 123 | public void showBiometricDialog(Bundle bundle, IBiometricPromptReceiver receiver, int type) { |
| 124 | if (DEBUG) Log.d(TAG, "showBiometricDialog, type: " + type); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 125 | // Remove these messages as they are part of the previous client |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 126 | mHandler.removeMessages(MSG_BIOMETRIC_ERROR); |
| 127 | mHandler.removeMessages(MSG_BIOMETRIC_HELP); |
| 128 | mHandler.removeMessages(MSG_BIOMETRIC_AUTHENTICATED); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 129 | SomeArgs args = SomeArgs.obtain(); |
| 130 | args.arg1 = bundle; |
| 131 | args.arg2 = receiver; |
| 132 | mHandler.obtainMessage(MSG_SHOW_DIALOG, args).sendToTarget(); |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 136 | public void onBiometricAuthenticated() { |
| 137 | if (DEBUG) Log.d(TAG, "onBiometricAuthenticated"); |
| 138 | mHandler.obtainMessage(MSG_BIOMETRIC_AUTHENTICATED).sendToTarget(); |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 142 | public void onBiometricHelp(String message) { |
| 143 | if (DEBUG) Log.d(TAG, "onBiometricHelp: " + message); |
| 144 | mHandler.obtainMessage(MSG_BIOMETRIC_HELP, message).sendToTarget(); |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 148 | public void onBiometricError(String error) { |
| 149 | if (DEBUG) Log.d(TAG, "onBiometricError: " + error); |
| 150 | mHandler.obtainMessage(MSG_BIOMETRIC_ERROR, error).sendToTarget(); |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 154 | public void hideBiometricDialog() { |
| 155 | if (DEBUG) Log.d(TAG, "hideBiometricDialog"); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 156 | mHandler.obtainMessage(MSG_HIDE_DIALOG, false /* userCanceled */).sendToTarget(); |
| 157 | } |
| 158 | |
| 159 | private void handleShowDialog(SomeArgs args) { |
Kevin Chyn | 87df068 | 2018-04-10 19:29:23 -0700 | [diff] [blame] | 160 | if (DEBUG) Log.d(TAG, "handleShowDialog, isAnimatingAway: " |
| 161 | + mDialogView.isAnimatingAway()); |
| 162 | if (mDialogView.isAnimatingAway()) { |
| 163 | mDialogView.forceRemove(); |
| 164 | } else if (mDialogShowing) { |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 165 | Log.w(TAG, "Dialog already showing"); |
| 166 | return; |
| 167 | } |
Vishwath Mohan | ecf00ce | 2018-04-05 10:28:24 -0700 | [diff] [blame] | 168 | mReceiver = (IBiometricPromptReceiver) args.arg2; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 169 | mDialogView.setBundle((Bundle)args.arg1); |
| 170 | mWindowManager.addView(mDialogView, mDialogView.getLayoutParams()); |
| 171 | mDialogShowing = true; |
| 172 | } |
| 173 | |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 174 | private void handleBiometricAuthenticated() { |
| 175 | if (DEBUG) Log.d(TAG, "handleBiometricAuthenticated"); |
| 176 | |
| 177 | // TODO: announce correct string depending on modality |
Kevin Chyn | 932e1df | 2018-03-26 11:53:47 -0700 | [diff] [blame] | 178 | mDialogView.announceForAccessibility( |
| 179 | mContext.getResources().getText( |
| 180 | com.android.internal.R.string.fingerprint_authenticated)); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 181 | handleHideDialog(false /* userCanceled */); |
| 182 | } |
| 183 | |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 184 | private void handleBiometricHelp(String message) { |
| 185 | if (DEBUG) Log.d(TAG, "handleBiometricHelp: " + message); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 186 | mDialogView.showHelpMessage(message); |
| 187 | } |
| 188 | |
Kevin Chyn | e927566 | 2018-07-23 16:42:06 -0700 | [diff] [blame] | 189 | private void handleBiometricError(String error) { |
| 190 | if (DEBUG) Log.d(TAG, "handleBiometricError: " + error); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 191 | if (!mDialogShowing) { |
| 192 | if (DEBUG) Log.d(TAG, "Dialog already dismissed"); |
| 193 | return; |
| 194 | } |
| 195 | mDialogView.showErrorMessage(error); |
| 196 | } |
| 197 | |
| 198 | private void handleHideDialog(boolean userCanceled) { |
Kevin Chyn | 87df068 | 2018-04-10 19:29:23 -0700 | [diff] [blame] | 199 | if (DEBUG) Log.d(TAG, "handleHideDialog, userCanceled: " + userCanceled); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 200 | if (!mDialogShowing) { |
| 201 | // This can happen if there's a race and we get called from both |
| 202 | // onAuthenticated and onError, etc. |
| 203 | Log.w(TAG, "Dialog already dismissed, userCanceled: " + userCanceled); |
| 204 | return; |
| 205 | } |
| 206 | if (userCanceled) { |
| 207 | try { |
Vishwath Mohan | ecf00ce | 2018-04-05 10:28:24 -0700 | [diff] [blame] | 208 | mReceiver.onDialogDismissed(BiometricPrompt.DISMISSED_REASON_USER_CANCEL); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 209 | } catch (RemoteException e) { |
| 210 | Log.e(TAG, "RemoteException when hiding dialog", e); |
| 211 | } |
| 212 | } |
| 213 | mReceiver = null; |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 214 | mDialogShowing = false; |
Kevin Chyn | 27e1f26 | 2018-03-08 16:38:32 -0800 | [diff] [blame] | 215 | mDialogView.startDismiss(); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private void handleButtonNegative() { |
| 219 | if (mReceiver == null) { |
| 220 | Log.e(TAG, "Receiver is null"); |
| 221 | return; |
| 222 | } |
| 223 | try { |
Vishwath Mohan | ecf00ce | 2018-04-05 10:28:24 -0700 | [diff] [blame] | 224 | mReceiver.onDialogDismissed(BiometricPrompt.DISMISSED_REASON_NEGATIVE); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 225 | } catch (RemoteException e) { |
| 226 | Log.e(TAG, "Remote exception when handling negative button", e); |
| 227 | } |
| 228 | handleHideDialog(false /* userCanceled */); |
| 229 | } |
| 230 | |
| 231 | private void handleButtonPositive() { |
| 232 | if (mReceiver == null) { |
| 233 | Log.e(TAG, "Receiver is null"); |
| 234 | return; |
| 235 | } |
| 236 | try { |
Vishwath Mohan | ecf00ce | 2018-04-05 10:28:24 -0700 | [diff] [blame] | 237 | mReceiver.onDialogDismissed(BiometricPrompt.DISMISSED_REASON_POSITIVE); |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 238 | } catch (RemoteException e) { |
| 239 | Log.e(TAG, "Remote exception when handling positive button", e); |
| 240 | } |
| 241 | handleHideDialog(false /* userCanceled */); |
| 242 | } |
| 243 | |
Kevin Chyn | 42653e8 | 2018-01-19 14:15:46 -0800 | [diff] [blame] | 244 | private void handleUserCanceled() { |
| 245 | handleHideDialog(true /* userCanceled */); |
Kevin Chyn | aae4a15 | 2018-01-18 11:48:09 -0800 | [diff] [blame] | 246 | } |
| 247 | } |