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