1 /*
2  * Copyright (C) 2020 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 
17 package com.android.server.biometrics.sensors;
18 
19 import android.content.Context;
20 import android.hardware.biometrics.IBiometricServiceLockoutResetCallback;
21 import android.os.Bundle;
22 import android.os.IBinder;
23 import android.os.IRemoteCallback;
24 import android.os.PowerManager;
25 import android.os.RemoteException;
26 import android.util.Slog;
27 
28 import java.util.ArrayList;
29 
30 /**
31  * Allows clients (such as keyguard) to register for notifications on when biometric lockout
32  * ends. This class keeps track of all client callbacks. Individual sensors should notify this
33  * when lockout for a specific sensor has been reset.
34  */
35 public class LockoutResetDispatcher implements IBinder.DeathRecipient {
36 
37     private static final String TAG = "LockoutResetTracker";
38 
39     private final Context mContext;
40     private final ArrayList<ClientCallback> mClientCallbacks;
41 
42     private static class ClientCallback {
43         private static final long WAKELOCK_TIMEOUT_MS = 2000;
44 
45         private final String mOpPackageName;
46         private final IBiometricServiceLockoutResetCallback mCallback;
47         private final PowerManager.WakeLock mWakeLock;
48 
ClientCallback(Context context, IBiometricServiceLockoutResetCallback callback, String opPackageName)49         ClientCallback(Context context, IBiometricServiceLockoutResetCallback callback,
50                 String opPackageName) {
51             final PowerManager pm = context.getSystemService(PowerManager.class);
52             mOpPackageName = opPackageName;
53             mCallback = callback;
54             mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
55                     "LockoutResetMonitor:SendLockoutReset");
56         }
57 
sendLockoutReset(int sensorId)58         void sendLockoutReset(int sensorId) {
59             if (mCallback != null) {
60                 try {
61                     mWakeLock.acquire(WAKELOCK_TIMEOUT_MS);
62                     mCallback.onLockoutReset(sensorId, new IRemoteCallback.Stub() {
63                         @Override
64                         public void sendResult(Bundle data) {
65                             releaseWakelock();
66                         }
67                     });
68                 } catch (RemoteException e) {
69                     Slog.w(TAG, "Failed to invoke onLockoutReset: ", e);
70                     releaseWakelock();
71                 }
72             }
73         }
74 
releaseWakelock()75         private void releaseWakelock() {
76             if (mWakeLock.isHeld()) {
77                 mWakeLock.release();
78             }
79         }
80     }
81 
LockoutResetDispatcher(Context context)82     public LockoutResetDispatcher(Context context) {
83         mContext = context;
84         mClientCallbacks = new ArrayList<>();
85     }
86 
addCallback(IBiometricServiceLockoutResetCallback callback, String opPackageName)87     public void addCallback(IBiometricServiceLockoutResetCallback callback, String opPackageName) {
88         if (callback == null) {
89             Slog.w(TAG, "Callback from : " + opPackageName + " is null");
90             return;
91         }
92 
93         mClientCallbacks.add(new ClientCallback(mContext, callback, opPackageName));
94         try {
95             callback.asBinder().linkToDeath(this, 0 /* flags */);
96         } catch (RemoteException e) {
97             Slog.e(TAG, "Failed to link to death", e);
98         }
99     }
100 
101     @Override
binderDied()102     public void binderDied() {
103         // Do nothing, handled below
104     }
105 
106     @Override
binderDied(IBinder who)107     public void binderDied(IBinder who) {
108         Slog.e(TAG, "Callback binder died: " + who);
109         for (ClientCallback callback : mClientCallbacks) {
110             if (callback.mCallback.asBinder().equals(who)) {
111                 Slog.e(TAG, "Removing dead callback for: " + callback.mOpPackageName);
112                 callback.releaseWakelock();
113                 mClientCallbacks.remove(callback);
114             }
115         }
116     }
117 
notifyLockoutResetCallbacks(int sensorId)118     public void notifyLockoutResetCallbacks(int sensorId) {
119         for (ClientCallback callback : mClientCallbacks) {
120             callback.sendLockoutReset(sensorId);
121         }
122     }
123 }
124