blob: 0331470a7f8328fb9b964f0e6256e9f42ba90f7f [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
Jorim Jaggi275561a2016-02-23 10:11:02 -050010import android.annotation.Nullable;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080011import android.app.ActivityManager.StackId;
12import android.content.Context;
13import android.os.SystemClock;
14
15import com.android.internal.logging.MetricsLogger;
Jorim Jaggi275561a2016-02-23 10:11:02 -050016import com.android.internal.logging.MetricsProto.MetricsEvent;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080017
18/**
19 * Handles logging into Tron.
20 */
21class ActivityMetricsLogger {
22 // Window modes we are interested in logging. If we ever introduce a new type, we need to add
23 // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array.
24 private static final int WINDOW_STATE_STANDARD = 0;
25 private static final int WINDOW_STATE_SIDE_BY_SIDE = 1;
26 private static final int WINDOW_STATE_FREEFORM = 2;
27 private static final int WINDOW_STATE_INVALID = -1;
28
Jorim Jaggi275561a2016-02-23 10:11:02 -050029 private static final long INVALID_START_TIME = -1;
30
Filip Gruszczynski77d94482015-12-11 13:59:52 -080031 // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
32 // time we log.
33 private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = {
Chris Wrenae255ee2016-01-04 16:20:17 -050034 "window_time_0", "window_time_1", "window_time_2"};
Filip Gruszczynski77d94482015-12-11 13:59:52 -080035
36 private int mWindowState = WINDOW_STATE_STANDARD;
37 private long mLastLogTimeSecs;
38 private final ActivityStackSupervisor mSupervisor;
39 private final Context mContext;
40
Jorim Jaggi275561a2016-02-23 10:11:02 -050041 private long mCurrentTransitionStartTime = INVALID_START_TIME;
42 private boolean mLoggedWindowsDrawn;
43 private boolean mLoggedStartingWindowDrawn;
44 private boolean mLoggedTransitionStarting;
45
Filip Gruszczynski77d94482015-12-11 13:59:52 -080046 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context) {
47 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
48 mSupervisor = supervisor;
49 mContext = context;
50 }
51
52 void logWindowState() {
53 final long now = SystemClock.elapsedRealtime() / 1000;
54 if (mWindowState != WINDOW_STATE_INVALID) {
55 // We log even if the window state hasn't changed, because the user might remain in
56 // home/fullscreen move forever and we would like to track this kind of behavior
57 // too.
58 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
59 (int) (now - mLastLogTimeSecs));
60 }
61 mLastLogTimeSecs = now;
62
Filip Gruszczynski77d94482015-12-11 13:59:52 -080063 ActivityStack stack = mSupervisor.getStack(DOCKED_STACK_ID);
Wale Ogunwale8051c5c2016-03-04 10:27:32 -080064 if (stack != null && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Filip Gruszczynski77d94482015-12-11 13:59:52 -080065 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080066 return;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080067 }
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080068 mWindowState = WINDOW_STATE_INVALID;
69 stack = mSupervisor.getFocusedStack();
70 if (stack.mStackId == PINNED_STACK_ID) {
71 stack = mSupervisor.findStackBehind(stack);
72 }
73 if (stack.mStackId == HOME_STACK_ID
74 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
75 mWindowState = WINDOW_STATE_STANDARD;
76 } else if (stack.mStackId == DOCKED_STACK_ID) {
77 throw new IllegalStateException("Docked stack shouldn't be the focused stack, "
78 + "because it reported not being visible.");
79 } else if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
80 mWindowState = WINDOW_STATE_FREEFORM;
81 } else if (StackId.isStaticStack(stack.mStackId)) {
82 throw new IllegalStateException("Unknown stack=" + stack);
Filip Gruszczynski77d94482015-12-11 13:59:52 -080083 }
84 }
Jorim Jaggi275561a2016-02-23 10:11:02 -050085
86 /**
87 * Notifies the tracker at the earliest possible point when we are starting to launch an
88 * activity.
89 */
90 void notifyActivityLaunching() {
91 mCurrentTransitionStartTime = System.currentTimeMillis();
92 }
93
94 /**
95 * Notifies the tracker the the activity is actually launching.
96 *
97 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
98 * launch
99 * @param componentName the component name of the activity being launched
100 * @param processRunning whether the process that will contains the activity is already running
Jorim Jaggibe67c902016-04-12 00:53:16 -0700101 * @param processSwitch whether the process that will contain the activity didn't have any
102 * activity that was stopped, i.e. the started activity is "switching"
103 * processes
Jorim Jaggi275561a2016-02-23 10:11:02 -0500104 */
105 void notifyActivityLaunched(int resultCode, @Nullable String componentName,
Jorim Jaggibe67c902016-04-12 00:53:16 -0700106 boolean processRunning, boolean processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500107
Jorim Jaggibe67c902016-04-12 00:53:16 -0700108 if (resultCode < 0 || componentName == null || !processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500109
Jorim Jaggibe67c902016-04-12 00:53:16 -0700110 // Failed to launch or it was not a process switch, so we don't care about the timing.
Jorim Jaggi275561a2016-02-23 10:11:02 -0500111 reset();
112 return;
113 }
114
115 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_COMPONENT_NAME,
116 componentName);
117 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_PROCESS_RUNNING,
118 processRunning);
119 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS,
120 (int) (SystemClock.uptimeMillis() / 1000));
121 }
122
123 /**
124 * Notifies the tracker that all windows of the app have been drawn.
125 */
126 void notifyWindowsDrawn() {
127 if (!isTransitionActive() || mLoggedWindowsDrawn) {
128 return;
129 }
130 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS,
131 calculateCurrentDelay());
132 mLoggedWindowsDrawn = true;
133 if (mLoggedTransitionStarting) {
134 reset();
135 }
136 }
137
138 /**
139 * Notifies the tracker that the starting window was drawn.
140 */
141 void notifyStartingWindowDrawn() {
142 if (!isTransitionActive() || mLoggedStartingWindowDrawn) {
143 return;
144 }
145 mLoggedStartingWindowDrawn = true;
146 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS,
147 calculateCurrentDelay());
148 }
149
150 /**
151 * Notifies the tracker that the app transition is starting.
152 *
153 * @param reason The reason why we started it. Must be on of
154 * ActivityManagerInternal.APP_TRANSITION_* reasons.
155 */
156 void notifyTransitionStarting(int reason) {
157 if (!isTransitionActive() || mLoggedTransitionStarting) {
158 return;
159 }
160 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_REASON, reason);
161 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_DELAY_MS,
162 calculateCurrentDelay());
163 mLoggedTransitionStarting = true;
164 if (mLoggedWindowsDrawn) {
165 reset();
166 }
167 }
168
169 private boolean isTransitionActive() {
170 return mCurrentTransitionStartTime != INVALID_START_TIME;
171 }
172
173 private void reset() {
174 mCurrentTransitionStartTime = INVALID_START_TIME;
175 mLoggedWindowsDrawn = false;
176 mLoggedTransitionStarting = false;
177 mLoggedStartingWindowDrawn = false;
178 }
179
180 private int calculateCurrentDelay() {
181
182 // Shouldn't take more than 25 days to launch an app, so int is fine here.
183 return (int) (System.currentTimeMillis() - mCurrentTransitionStartTime);
184 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800185}