summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/log/SessionTracker.java
blob: b98a92ff86009ce3aef0146f7493de3420bbdfd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.log;

import static android.app.StatusBarManager.ALL_SESSIONS;
import static android.app.StatusBarManager.SESSION_BIOMETRIC_PROMPT;
import static android.app.StatusBarManager.SESSION_KEYGUARD;

import android.annotation.Nullable;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.NonNull;

import com.android.internal.logging.InstanceId;
import com.android.internal.logging.InstanceIdSequence;
import com.android.internal.statusbar.IStatusBarService;
import com.android.keyguard.KeyguardUpdateMonitor;
import com.android.keyguard.KeyguardUpdateMonitorCallback;
import com.android.systemui.CoreStartable;
import com.android.systemui.biometrics.AuthController;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.statusbar.policy.KeyguardStateController;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

/**
 * Track Session InstanceIds to be used for metrics logging to correlate logs in the same
 * session. Can be used across processes via StatusBarManagerService#registerSessionListener
 */
@SysUISingleton
public class SessionTracker implements CoreStartable {
    private static final String TAG = "SessionTracker";

    // To enable logs: `adb shell setprop log.tag.SessionTracker DEBUG` & restart sysui
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    // At most 20 bits: ~1m possibilities, ~0.5% probability of collision in 100 values
    private final InstanceIdSequence mInstanceIdGenerator = new InstanceIdSequence(1 << 20);

    private final IStatusBarService mStatusBarManagerService;
    private final AuthController mAuthController;
    private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
    private final KeyguardStateController mKeyguardStateController;
    private final Map<Integer, InstanceId> mSessionToInstanceId = new HashMap<>();

    private boolean mKeyguardSessionStarted;

    @Inject
    public SessionTracker(
            IStatusBarService statusBarService,
            AuthController authController,
            KeyguardUpdateMonitor keyguardUpdateMonitor,
            KeyguardStateController keyguardStateController
    ) {
        mStatusBarManagerService = statusBarService;
        mAuthController = authController;
        mKeyguardUpdateMonitor = keyguardUpdateMonitor;
        mKeyguardStateController = keyguardStateController;
    }

    @Override
    public void start() {
        mAuthController.addCallback(mAuthControllerCallback);
        mKeyguardUpdateMonitor.registerCallback(mKeyguardUpdateMonitorCallback);
        mKeyguardStateController.addCallback(mKeyguardStateCallback);

        if (mKeyguardStateController.isShowing()) {
            mKeyguardSessionStarted = true;
            startSession(SESSION_KEYGUARD);
        }
    }

    /**
     * Get the session ID associated with the passed session type.
     */
    public @Nullable InstanceId getSessionId(int type) {
        return mSessionToInstanceId.getOrDefault(type, null);
    }

    private void startSession(int type) {
        if (mSessionToInstanceId.getOrDefault(type, null) != null) {
            Log.e(TAG, "session [" + getString(type) + "] was already started");
            return;
        }

        final InstanceId instanceId = mInstanceIdGenerator.newInstanceId();
        mSessionToInstanceId.put(type, instanceId);
        try {
            if (DEBUG) {
                Log.d(TAG, "Session start for [" + getString(type) + "] id=" + instanceId);
            }
            mStatusBarManagerService.onSessionStarted(type, instanceId);
        } catch (RemoteException e) {
            Log.e(TAG, "Unable to send onSessionStarted for session="
                    + "[" + getString(type) + "]", e);
        }
    }

    private void endSession(int type) {
        if (mSessionToInstanceId.getOrDefault(type, null) == null) {
            Log.e(TAG, "session [" + getString(type) + "] was not started");
            return;
        }

        final InstanceId instanceId = mSessionToInstanceId.get(type);
        mSessionToInstanceId.put(type, null);
        try {
            if (DEBUG) {
                Log.d(TAG, "Session end for [" + getString(type) + "] id=" + instanceId);
            }
            mStatusBarManagerService.onSessionEnded(type, instanceId);
        } catch (RemoteException e) {
            Log.e(TAG, "Unable to send onSessionEnded for session="
                    + "[" + getString(type) + "]", e);
        }
    }

    public KeyguardUpdateMonitorCallback mKeyguardUpdateMonitorCallback =
            new KeyguardUpdateMonitorCallback() {
        @Override
        public void onStartedGoingToSleep(int why) {
            if (mKeyguardSessionStarted) {
                endSession(SESSION_KEYGUARD);
            }

            // Start a new session whenever the device goes to sleep
            mKeyguardSessionStarted = true;
            startSession(SESSION_KEYGUARD);
        }
    };


    public KeyguardStateController.Callback mKeyguardStateCallback =
            new KeyguardStateController.Callback() {
        public void onKeyguardShowingChanged() {
            boolean wasSessionStarted = mKeyguardSessionStarted;
            boolean keyguardShowing = mKeyguardStateController.isShowing();
            if (keyguardShowing && !wasSessionStarted) {
                // the keyguard can start showing without the device going to sleep (ie: lockdown
                // from the power button), so we start a new keyguard session when the keyguard is
                // newly shown in addition to when the device starts going to sleep
                mKeyguardSessionStarted = true;
                startSession(SESSION_KEYGUARD);
            } else if (!keyguardShowing && wasSessionStarted) {
                mKeyguardSessionStarted = false;
                endSession(SESSION_KEYGUARD);
            }
        }
    };

    public AuthController.Callback mAuthControllerCallback = new AuthController.Callback() {
        @Override
        public void onBiometricPromptShown() {
            startSession(SESSION_BIOMETRIC_PROMPT);
        }

        @Override
        public void onBiometricPromptDismissed() {
            endSession(SESSION_BIOMETRIC_PROMPT);
        }
    };

    @Override
    public void dump(@NonNull PrintWriter pw, @NonNull String[] args) {
        for (int session : ALL_SESSIONS) {
            pw.println("  " + getString(session)
                    + " instanceId=" + mSessionToInstanceId.get(session));
        }
    }

    /**
     * @return the string representation of a SINGLE SessionFlag. Combined SessionFlags will be
     * considered unknown.
     */
    public static String getString(int sessionType) {
        if (sessionType == SESSION_KEYGUARD) {
            return "KEYGUARD";
        } else if (sessionType == SESSION_BIOMETRIC_PROMPT) {
            return "BIOMETRIC_PROMPT";
        }

        return "unknownType=" + sessionType;
    }
}