blob: fe085d7054ff2fc0863ccefb6387d98adf060ad5 [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;
Jorim Jaggif9704102016-05-05 19:14:22 -07008import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
9import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -080010import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080011
Jorim Jaggi275561a2016-02-23 10:11:02 -050012import android.annotation.Nullable;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080013import android.app.ActivityManager.StackId;
14import android.content.Context;
15import android.os.SystemClock;
Jorim Jaggif9704102016-05-05 19:14:22 -070016import android.util.Slog;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080017
18import com.android.internal.logging.MetricsLogger;
Jorim Jaggi275561a2016-02-23 10:11:02 -050019import com.android.internal.logging.MetricsProto.MetricsEvent;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080020
21/**
22 * Handles logging into Tron.
23 */
24class ActivityMetricsLogger {
Jorim Jaggif9704102016-05-05 19:14:22 -070025
26 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityMetricsLogger" : TAG_AM;
27
Filip Gruszczynski77d94482015-12-11 13:59:52 -080028 // Window modes we are interested in logging. If we ever introduce a new type, we need to add
29 // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array.
30 private static final int WINDOW_STATE_STANDARD = 0;
31 private static final int WINDOW_STATE_SIDE_BY_SIDE = 1;
32 private static final int WINDOW_STATE_FREEFORM = 2;
33 private static final int WINDOW_STATE_INVALID = -1;
34
Jorim Jaggi275561a2016-02-23 10:11:02 -050035 private static final long INVALID_START_TIME = -1;
36
Filip Gruszczynski77d94482015-12-11 13:59:52 -080037 // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
38 // time we log.
39 private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = {
Chris Wrenae255ee2016-01-04 16:20:17 -050040 "window_time_0", "window_time_1", "window_time_2"};
Filip Gruszczynski77d94482015-12-11 13:59:52 -080041
42 private int mWindowState = WINDOW_STATE_STANDARD;
43 private long mLastLogTimeSecs;
44 private final ActivityStackSupervisor mSupervisor;
45 private final Context mContext;
46
Jorim Jaggi275561a2016-02-23 10:11:02 -050047 private long mCurrentTransitionStartTime = INVALID_START_TIME;
48 private boolean mLoggedWindowsDrawn;
49 private boolean mLoggedStartingWindowDrawn;
50 private boolean mLoggedTransitionStarting;
51
Filip Gruszczynski77d94482015-12-11 13:59:52 -080052 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context) {
53 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
54 mSupervisor = supervisor;
55 mContext = context;
56 }
57
58 void logWindowState() {
59 final long now = SystemClock.elapsedRealtime() / 1000;
60 if (mWindowState != WINDOW_STATE_INVALID) {
61 // We log even if the window state hasn't changed, because the user might remain in
62 // home/fullscreen move forever and we would like to track this kind of behavior
63 // too.
64 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
65 (int) (now - mLastLogTimeSecs));
66 }
67 mLastLogTimeSecs = now;
68
Filip Gruszczynski77d94482015-12-11 13:59:52 -080069 ActivityStack stack = mSupervisor.getStack(DOCKED_STACK_ID);
Wale Ogunwale8051c5c2016-03-04 10:27:32 -080070 if (stack != null && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Filip Gruszczynski77d94482015-12-11 13:59:52 -080071 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080072 return;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080073 }
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080074 mWindowState = WINDOW_STATE_INVALID;
75 stack = mSupervisor.getFocusedStack();
76 if (stack.mStackId == PINNED_STACK_ID) {
77 stack = mSupervisor.findStackBehind(stack);
78 }
79 if (stack.mStackId == HOME_STACK_ID
80 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
81 mWindowState = WINDOW_STATE_STANDARD;
82 } else if (stack.mStackId == DOCKED_STACK_ID) {
Jorim Jaggif9704102016-05-05 19:14:22 -070083 Slog.wtf(TAG, "Docked stack shouldn't be the focused stack, because it reported not"
84 + " being visible.");
85 mWindowState = WINDOW_STATE_INVALID;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080086 } else if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
87 mWindowState = WINDOW_STATE_FREEFORM;
88 } else if (StackId.isStaticStack(stack.mStackId)) {
89 throw new IllegalStateException("Unknown stack=" + stack);
Filip Gruszczynski77d94482015-12-11 13:59:52 -080090 }
91 }
Jorim Jaggi275561a2016-02-23 10:11:02 -050092
93 /**
94 * Notifies the tracker at the earliest possible point when we are starting to launch an
95 * activity.
96 */
97 void notifyActivityLaunching() {
98 mCurrentTransitionStartTime = System.currentTimeMillis();
99 }
100
101 /**
102 * Notifies the tracker the the activity is actually launching.
103 *
104 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
105 * launch
106 * @param componentName the component name of the activity being launched
107 * @param processRunning whether the process that will contains the activity is already running
Jorim Jaggibe67c902016-04-12 00:53:16 -0700108 * @param processSwitch whether the process that will contain the activity didn't have any
109 * activity that was stopped, i.e. the started activity is "switching"
110 * processes
Jorim Jaggi275561a2016-02-23 10:11:02 -0500111 */
112 void notifyActivityLaunched(int resultCode, @Nullable String componentName,
Jorim Jaggibe67c902016-04-12 00:53:16 -0700113 boolean processRunning, boolean processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500114
Jorim Jaggibe67c902016-04-12 00:53:16 -0700115 if (resultCode < 0 || componentName == null || !processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500116
Jorim Jaggibe67c902016-04-12 00:53:16 -0700117 // 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 -0500118 reset();
119 return;
120 }
121
122 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_COMPONENT_NAME,
123 componentName);
124 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_PROCESS_RUNNING,
125 processRunning);
126 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS,
127 (int) (SystemClock.uptimeMillis() / 1000));
128 }
129
130 /**
131 * Notifies the tracker that all windows of the app have been drawn.
132 */
133 void notifyWindowsDrawn() {
134 if (!isTransitionActive() || mLoggedWindowsDrawn) {
135 return;
136 }
137 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS,
138 calculateCurrentDelay());
139 mLoggedWindowsDrawn = true;
140 if (mLoggedTransitionStarting) {
141 reset();
142 }
143 }
144
145 /**
146 * Notifies the tracker that the starting window was drawn.
147 */
148 void notifyStartingWindowDrawn() {
149 if (!isTransitionActive() || mLoggedStartingWindowDrawn) {
150 return;
151 }
152 mLoggedStartingWindowDrawn = true;
153 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS,
154 calculateCurrentDelay());
155 }
156
157 /**
158 * Notifies the tracker that the app transition is starting.
159 *
160 * @param reason The reason why we started it. Must be on of
161 * ActivityManagerInternal.APP_TRANSITION_* reasons.
162 */
163 void notifyTransitionStarting(int reason) {
164 if (!isTransitionActive() || mLoggedTransitionStarting) {
165 return;
166 }
167 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_REASON, reason);
168 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_DELAY_MS,
169 calculateCurrentDelay());
170 mLoggedTransitionStarting = true;
171 if (mLoggedWindowsDrawn) {
172 reset();
173 }
174 }
175
176 private boolean isTransitionActive() {
177 return mCurrentTransitionStartTime != INVALID_START_TIME;
178 }
179
180 private void reset() {
181 mCurrentTransitionStartTime = INVALID_START_TIME;
182 mLoggedWindowsDrawn = false;
183 mLoggedTransitionStarting = false;
184 mLoggedStartingWindowDrawn = false;
185 }
186
187 private int calculateCurrentDelay() {
188
189 // Shouldn't take more than 25 days to launch an app, so int is fine here.
190 return (int) (System.currentTimeMillis() - mCurrentTransitionStartTime);
191 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800192}