blob: 64f423c32f052fa4826238bcfc9fc5b3128afc26 [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;
7
8import android.app.ActivityManager.StackId;
9import android.content.Context;
10import android.os.SystemClock;
11
12import com.android.internal.logging.MetricsLogger;
13
14/**
15 * Handles logging into Tron.
16 */
17class ActivityMetricsLogger {
18 // Window modes we are interested in logging. If we ever introduce a new type, we need to add
19 // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array.
20 private static final int WINDOW_STATE_STANDARD = 0;
21 private static final int WINDOW_STATE_SIDE_BY_SIDE = 1;
22 private static final int WINDOW_STATE_FREEFORM = 2;
23 private static final int WINDOW_STATE_INVALID = -1;
24
25 // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
26 // time we log.
27 private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = {
28 "tron_varz_window_time_0", "tron_varz_window_time_1", "tron_varz_window_time_2"};
29
30 private int mWindowState = WINDOW_STATE_STANDARD;
31 private long mLastLogTimeSecs;
32 private final ActivityStackSupervisor mSupervisor;
33 private final Context mContext;
34
35 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context) {
36 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
37 mSupervisor = supervisor;
38 mContext = context;
39 }
40
41 void logWindowState() {
42 final long now = SystemClock.elapsedRealtime() / 1000;
43 if (mWindowState != WINDOW_STATE_INVALID) {
44 // We log even if the window state hasn't changed, because the user might remain in
45 // home/fullscreen move forever and we would like to track this kind of behavior
46 // too.
47 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
48 (int) (now - mLastLogTimeSecs));
49 }
50 mLastLogTimeSecs = now;
51
52 mWindowState = WINDOW_STATE_INVALID;
53 ActivityStack stack = mSupervisor.getStack(DOCKED_STACK_ID);
54 if (stack != null && stack.isStackVisibleLocked()) {
55 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
56 }
57 if (mWindowState == WINDOW_STATE_INVALID) {
58 stack = mSupervisor.getFocusedStack();
59 if (stack.mStackId == HOME_STACK_ID
60 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
61 mWindowState = WINDOW_STATE_STANDARD;
62 } else if (stack.mStackId == DOCKED_STACK_ID) {
63 throw new IllegalStateException("Docked stack shouldn't be the focused stack, "
64 + "because it reported not being visible.");
65 } else if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
66 mWindowState = WINDOW_STATE_FREEFORM;
67 } else if (StackId.isStaticStack(stack.mStackId)) {
68 throw new IllegalStateException("Unknown stack=" + stack);
69 }
70 }
71 }
72}