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
17package com.android.systemui;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.os.Build;
24import android.os.PowerManager;
25import android.os.SystemClock;
26
27import com.android.keyguard.KeyguardUpdateMonitor;
28import com.android.keyguard.LatencyTracker;
29import com.android.systemui.statusbar.phone.FingerprintUnlockController;
30import com.android.systemui.statusbar.phone.StatusBar;
31
32/**
33 * Class that only runs on debuggable builds that listens to broadcasts that simulate actions in the
34 * system that are used for testing the latency.
35 */
36public class LatencyTester extends SystemUI {
37
38    private static final String ACTION_FINGERPRINT_WAKE =
39            "com.android.systemui.latency.ACTION_FINGERPRINT_WAKE";
40    private static final String ACTION_TURN_ON_SCREEN =
41            "com.android.systemui.latency.ACTION_TURN_ON_SCREEN";
42
43    @Override
44    public void start() {
45        if (!Build.IS_DEBUGGABLE) {
46            return;
47        }
48
49        IntentFilter filter = new IntentFilter();
50        filter.addAction(ACTION_FINGERPRINT_WAKE);
51        filter.addAction(ACTION_TURN_ON_SCREEN);
52        mContext.registerReceiver(new BroadcastReceiver() {
53            @Override
54            public void onReceive(Context context, Intent intent) {
55                String action = intent.getAction();
56                if (ACTION_FINGERPRINT_WAKE.equals(action)) {
57                    fakeWakeAndUnlock();
58                } else if (ACTION_TURN_ON_SCREEN.equals(action)) {
59                    fakeTurnOnScreen();
60                }
61            }
62        }, filter);
63    }
64
65    private void fakeTurnOnScreen() {
66        PowerManager powerManager = mContext.getSystemService(PowerManager.class);
67        if (LatencyTracker.isEnabled(mContext)) {
68            LatencyTracker.getInstance(mContext).onActionStart(
69                    LatencyTracker.ACTION_TURN_ON_SCREEN);
70        }
71        powerManager.wakeUp(SystemClock.uptimeMillis(), "android.policy:LATENCY_TESTS");
72    }
73
74    private void fakeWakeAndUnlock() {
75        FingerprintUnlockController fingerprintUnlockController = getComponent(StatusBar.class)
76                .getFingerprintUnlockController();
77        fingerprintUnlockController.onFingerprintAcquired();
78        fingerprintUnlockController.onFingerprintAuthenticated(
79                KeyguardUpdateMonitor.getCurrentUser());
80    }
81}
82