1 /*
2  * Copyright (C) 2016 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.systemui;
18 
19 import static android.os.PowerManager.WAKE_REASON_UNKNOWN;
20 
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.hardware.biometrics.BiometricSourceType;
26 import android.os.Build;
27 import android.os.PowerManager;
28 import android.os.SystemClock;
29 
30 import com.android.internal.util.LatencyTracker;
31 import com.android.keyguard.KeyguardUpdateMonitor;
32 import com.android.systemui.broadcast.BroadcastDispatcher;
33 import com.android.systemui.statusbar.phone.BiometricUnlockController;
34 
35 import javax.inject.Inject;
36 import javax.inject.Singleton;
37 
38 /**
39  * Class that only runs on debuggable builds that listens to broadcasts that simulate actions in the
40  * system that are used for testing the latency.
41  */
42 @Singleton
43 public class LatencyTester extends SystemUI {
44 
45     private static final String
46             ACTION_FINGERPRINT_WAKE =
47             "com.android.systemui.latency.ACTION_FINGERPRINT_WAKE";
48     private static final String
49             ACTION_TURN_ON_SCREEN =
50             "com.android.systemui.latency.ACTION_TURN_ON_SCREEN";
51     private final BiometricUnlockController mBiometricUnlockController;
52     private final PowerManager mPowerManager;
53     private final BroadcastDispatcher mBroadcastDispatcher;
54 
55     @Inject
LatencyTester(Context context, BiometricUnlockController biometricUnlockController, PowerManager powerManager, BroadcastDispatcher broadcastDispatcher)56     public LatencyTester(Context context, BiometricUnlockController biometricUnlockController,
57             PowerManager powerManager, BroadcastDispatcher broadcastDispatcher) {
58         super(context);
59 
60         mBiometricUnlockController = biometricUnlockController;
61         mPowerManager = powerManager;
62         mBroadcastDispatcher = broadcastDispatcher;
63     }
64 
65     @Override
start()66     public void start() {
67         if (!Build.IS_DEBUGGABLE) {
68             return;
69         }
70 
71         IntentFilter filter = new IntentFilter();
72         filter.addAction(ACTION_FINGERPRINT_WAKE);
73         filter.addAction(ACTION_TURN_ON_SCREEN);
74         mBroadcastDispatcher.registerReceiver(new BroadcastReceiver() {
75             @Override
76             public void onReceive(Context context, Intent intent) {
77                 String action = intent.getAction();
78                 if (ACTION_FINGERPRINT_WAKE.equals(action)) {
79                     fakeWakeAndUnlock();
80                 } else if (ACTION_TURN_ON_SCREEN.equals(action)) {
81                     fakeTurnOnScreen();
82                 }
83             }
84         }, filter);
85     }
86 
fakeTurnOnScreen()87     private void fakeTurnOnScreen() {
88         if (LatencyTracker.isEnabled(mContext)) {
89             LatencyTracker.getInstance(mContext).onActionStart(
90                     LatencyTracker.ACTION_TURN_ON_SCREEN);
91         }
92         mPowerManager.wakeUp(
93                 SystemClock.uptimeMillis(), WAKE_REASON_UNKNOWN, "android.policy:LATENCY_TESTS");
94     }
95 
fakeWakeAndUnlock()96     private void fakeWakeAndUnlock() {
97         mBiometricUnlockController.onBiometricAcquired(BiometricSourceType.FINGERPRINT);
98         mBiometricUnlockController.onBiometricAuthenticated(
99                 KeyguardUpdateMonitor.getCurrentUser(), BiometricSourceType.FINGERPRINT,
100                 true /* isStrongBiometric */);
101     }
102 }
103