blob: ffb2fc4a6d027bf0e3c8ebbcc844020fc04fde57 [file] [log] [blame]
Filip Gruszczynski77d94482015-12-11 13:59:52 -08001package com.android.server.am;
2
3import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
4import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
5import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
6import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08007import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -08008import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Filip Gruszczynski77d94482015-12-11 13:59:52 -08009
10import android.app.ActivityManager.StackId;
11import android.content.Context;
12import android.os.SystemClock;
13
14import com.android.internal.logging.MetricsLogger;
15
16/**
17 * Handles logging into Tron.
18 */
19class ActivityMetricsLogger {
20 // Window modes we are interested in logging. If we ever introduce a new type, we need to add
21 // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array.
22 private static final int WINDOW_STATE_STANDARD = 0;
23 private static final int WINDOW_STATE_SIDE_BY_SIDE = 1;
24 private static final int WINDOW_STATE_FREEFORM = 2;
25 private static final int WINDOW_STATE_INVALID = -1;
26
27 // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
28 // time we log.
29 private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = {
Chris Wrenae255ee2016-01-04 16:20:17 -050030 "window_time_0", "window_time_1", "window_time_2"};
Filip Gruszczynski77d94482015-12-11 13:59:52 -080031
32 private int mWindowState = WINDOW_STATE_STANDARD;
33 private long mLastLogTimeSecs;
34 private final ActivityStackSupervisor mSupervisor;
35 private final Context mContext;
36
37 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context) {
38 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
39 mSupervisor = supervisor;
40 mContext = context;
41 }
42
43 void logWindowState() {
44 final long now = SystemClock.elapsedRealtime() / 1000;
45 if (mWindowState != WINDOW_STATE_INVALID) {
46 // We log even if the window state hasn't changed, because the user might remain in
47 // home/fullscreen move forever and we would like to track this kind of behavior
48 // too.
49 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
50 (int) (now - mLastLogTimeSecs));
51 }
52 mLastLogTimeSecs = now;
53
Filip Gruszczynski77d94482015-12-11 13:59:52 -080054 ActivityStack stack = mSupervisor.getStack(DOCKED_STACK_ID);
Filip Gruszczynski0e381e22016-01-14 16:31:33 -080055 if (stack != null && stack.getStackVisibilityLocked() != STACK_INVISIBLE) {
Filip Gruszczynski77d94482015-12-11 13:59:52 -080056 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080057 return;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080058 }
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080059 mWindowState = WINDOW_STATE_INVALID;
60 stack = mSupervisor.getFocusedStack();
61 if (stack.mStackId == PINNED_STACK_ID) {
62 stack = mSupervisor.findStackBehind(stack);
63 }
64 if (stack.mStackId == HOME_STACK_ID
65 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
66 mWindowState = WINDOW_STATE_STANDARD;
67 } else if (stack.mStackId == DOCKED_STACK_ID) {
68 throw new IllegalStateException("Docked stack shouldn't be the focused stack, "
69 + "because it reported not being visible.");
70 } else if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
71 mWindowState = WINDOW_STATE_FREEFORM;
72 } else if (StackId.isStaticStack(stack.mStackId)) {
73 throw new IllegalStateException("Unknown stack=" + stack);
Filip Gruszczynski77d94482015-12-11 13:59:52 -080074 }
75 }
76}