Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 1 | package com.android.server.am; |
| 2 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 3 | import static android.app.ActivityManager.START_SUCCESS; |
| 4 | import static android.app.ActivityManager.START_TASK_TO_FRONT; |
Winson Chung | 8347163 | 2016-12-13 11:02:12 -0800 | [diff] [blame] | 5 | import static android.app.ActivityManager.StackId.ASSISTANT_STACK_ID; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 6 | import static android.app.ActivityManager.StackId.DOCKED_STACK_ID; |
| 7 | import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID; |
| 8 | import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 9 | import static android.app.ActivityManager.StackId.INVALID_STACK_ID; |
Filip Gruszczynski | caae14e | 2015-12-16 14:40:04 -0800 | [diff] [blame] | 10 | import static android.app.ActivityManager.StackId.PINNED_STACK_ID; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 11 | import static android.app.ActivityManagerInternal.APP_TRANSITION_TIMEOUT; |
| 12 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION; |
Todd Kennedy | 50d946c1 | 2017-03-17 13:55:38 -0700 | [diff] [blame] | 13 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_CALLING_PACKAGE_NAME; |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 14 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CLASS_NAME; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 15 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DELAY_MS; |
| 16 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS; |
Todd Kennedy | 50d946c1 | 2017-03-17 13:55:38 -0700 | [diff] [blame] | 17 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_IS_EPHEMERAL; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 18 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS; |
| 19 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS; |
Todd Kennedy | 50d946c1 | 2017-03-17 13:55:38 -0700 | [diff] [blame] | 20 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_INSTANT_APP_LAUNCH_TOKEN; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 21 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_COLD_LAUNCH; |
| 22 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_HOT_LAUNCH; |
| 23 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_WARM_LAUNCH; |
Jorim Jaggi | f970410 | 2016-05-05 19:14:22 -0700 | [diff] [blame] | 24 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 25 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
Filip Gruszczynski | 0e381e2 | 2016-01-14 16:31:33 -0800 | [diff] [blame] | 26 | import static com.android.server.am.ActivityStack.STACK_INVISIBLE; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 27 | |
| 28 | import android.app.ActivityManager.StackId; |
| 29 | import android.content.Context; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 30 | import android.metrics.LogMaker; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 31 | import android.os.SystemClock; |
Jorim Jaggi | f970410 | 2016-05-05 19:14:22 -0700 | [diff] [blame] | 32 | import android.util.Slog; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 33 | import android.util.SparseArray; |
| 34 | import android.util.SparseIntArray; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 35 | |
| 36 | import com.android.internal.logging.MetricsLogger; |
| 37 | |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
| 39 | |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Handles logging into Tron. |
| 42 | */ |
| 43 | class ActivityMetricsLogger { |
Jorim Jaggi | f970410 | 2016-05-05 19:14:22 -0700 | [diff] [blame] | 44 | |
| 45 | private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityMetricsLogger" : TAG_AM; |
| 46 | |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 47 | // Window modes we are interested in logging. If we ever introduce a new type, we need to add |
| 48 | // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array. |
| 49 | private static final int WINDOW_STATE_STANDARD = 0; |
| 50 | private static final int WINDOW_STATE_SIDE_BY_SIDE = 1; |
| 51 | private static final int WINDOW_STATE_FREEFORM = 2; |
Winson Chung | 8347163 | 2016-12-13 11:02:12 -0800 | [diff] [blame] | 52 | private static final int WINDOW_STATE_ASSISTANT = 3; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 53 | private static final int WINDOW_STATE_INVALID = -1; |
| 54 | |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 55 | private static final long INVALID_START_TIME = -1; |
| 56 | |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 57 | // Preallocated strings we are sending to tron, so we don't have to allocate a new one every |
| 58 | // time we log. |
| 59 | private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = { |
Winson Chung | 8347163 | 2016-12-13 11:02:12 -0800 | [diff] [blame] | 60 | "window_time_0", "window_time_1", "window_time_2", "window_time_3"}; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 61 | |
| 62 | private int mWindowState = WINDOW_STATE_STANDARD; |
| 63 | private long mLastLogTimeSecs; |
| 64 | private final ActivityStackSupervisor mSupervisor; |
| 65 | private final Context mContext; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 66 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 67 | |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 68 | private long mCurrentTransitionStartTime = INVALID_START_TIME; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 69 | |
| 70 | private int mCurrentTransitionDeviceUptime; |
| 71 | private int mCurrentTransitionDelayMs; |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 72 | private boolean mLoggedTransitionStarting; |
| 73 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 74 | private final SparseArray<StackTransitionInfo> mStackTransitionInfo = new SparseArray<>(); |
| 75 | |
| 76 | private final class StackTransitionInfo { |
| 77 | private ActivityRecord launchedActivity; |
| 78 | private int startResult; |
| 79 | private boolean currentTransitionProcessRunning; |
| 80 | private int windowsDrawnDelayMs; |
| 81 | private int startingWindowDelayMs; |
| 82 | private int reason = APP_TRANSITION_TIMEOUT; |
| 83 | private boolean loggedWindowsDrawn; |
| 84 | private boolean loggedStartingWindowDrawn; |
| 85 | } |
| 86 | |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 87 | ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context) { |
| 88 | mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000; |
| 89 | mSupervisor = supervisor; |
| 90 | mContext = context; |
| 91 | } |
| 92 | |
| 93 | void logWindowState() { |
| 94 | final long now = SystemClock.elapsedRealtime() / 1000; |
| 95 | if (mWindowState != WINDOW_STATE_INVALID) { |
| 96 | // We log even if the window state hasn't changed, because the user might remain in |
| 97 | // home/fullscreen move forever and we would like to track this kind of behavior |
| 98 | // too. |
| 99 | MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState], |
| 100 | (int) (now - mLastLogTimeSecs)); |
| 101 | } |
| 102 | mLastLogTimeSecs = now; |
| 103 | |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 104 | ActivityStack stack = mSupervisor.getStack(DOCKED_STACK_ID); |
Wale Ogunwale | cd501ec | 2017-04-07 08:53:41 -0700 | [diff] [blame] | 105 | if (stack != null && stack.shouldBeVisible(null) != STACK_INVISIBLE) { |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 106 | mWindowState = WINDOW_STATE_SIDE_BY_SIDE; |
Filip Gruszczynski | caae14e | 2015-12-16 14:40:04 -0800 | [diff] [blame] | 107 | return; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 108 | } |
Filip Gruszczynski | caae14e | 2015-12-16 14:40:04 -0800 | [diff] [blame] | 109 | mWindowState = WINDOW_STATE_INVALID; |
| 110 | stack = mSupervisor.getFocusedStack(); |
| 111 | if (stack.mStackId == PINNED_STACK_ID) { |
| 112 | stack = mSupervisor.findStackBehind(stack); |
| 113 | } |
Matthew Ng | ae1ff4f | 2016-11-10 15:49:14 -0800 | [diff] [blame] | 114 | if (StackId.isHomeOrRecentsStack(stack.mStackId) |
Filip Gruszczynski | caae14e | 2015-12-16 14:40:04 -0800 | [diff] [blame] | 115 | || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) { |
| 116 | mWindowState = WINDOW_STATE_STANDARD; |
| 117 | } else if (stack.mStackId == DOCKED_STACK_ID) { |
Jorim Jaggi | f970410 | 2016-05-05 19:14:22 -0700 | [diff] [blame] | 118 | Slog.wtf(TAG, "Docked stack shouldn't be the focused stack, because it reported not" |
| 119 | + " being visible."); |
| 120 | mWindowState = WINDOW_STATE_INVALID; |
Filip Gruszczynski | caae14e | 2015-12-16 14:40:04 -0800 | [diff] [blame] | 121 | } else if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) { |
| 122 | mWindowState = WINDOW_STATE_FREEFORM; |
Winson Chung | 8347163 | 2016-12-13 11:02:12 -0800 | [diff] [blame] | 123 | } else if (stack.mStackId == ASSISTANT_STACK_ID) { |
| 124 | mWindowState = WINDOW_STATE_ASSISTANT; |
Filip Gruszczynski | caae14e | 2015-12-16 14:40:04 -0800 | [diff] [blame] | 125 | } else if (StackId.isStaticStack(stack.mStackId)) { |
| 126 | throw new IllegalStateException("Unknown stack=" + stack); |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Notifies the tracker at the earliest possible point when we are starting to launch an |
| 132 | * activity. |
| 133 | */ |
| 134 | void notifyActivityLaunching() { |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 135 | if (!isAnyTransitionActive()) { |
Alison Cichowlas | b7f67ab | 2017-04-25 18:04:40 -0400 | [diff] [blame^] | 136 | mCurrentTransitionStartTime = SystemClock.uptimeMillis(); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 137 | } |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 141 | * Notifies the tracker that the activity is actually launching. |
| 142 | * |
| 143 | * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the |
| 144 | * launch |
| 145 | * @param launchedActivity the activity that is being launched |
| 146 | */ |
| 147 | void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity) { |
| 148 | final ProcessRecord processRecord = launchedActivity != null |
| 149 | ? mSupervisor.mService.mProcessNames.get(launchedActivity.processName, |
| 150 | launchedActivity.appInfo.uid) |
| 151 | : null; |
| 152 | final boolean processRunning = processRecord != null; |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 153 | |
| 154 | // We consider this a "process switch" if the process of the activity that gets launched |
| 155 | // didn't have an activity that was in started state. In this case, we assume that lot |
| 156 | // of caches might be purged so the time until it produces the first frame is very |
| 157 | // interesting. |
| 158 | final boolean processSwitch = processRecord == null |
| 159 | || !hasStartedActivity(processRecord, launchedActivity); |
| 160 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 161 | notifyActivityLaunched(resultCode, launchedActivity, processRunning, processSwitch); |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | private boolean hasStartedActivity(ProcessRecord record, ActivityRecord launchedActivity) { |
| 165 | final ArrayList<ActivityRecord> activities = record.activities; |
| 166 | for (int i = activities.size() - 1; i >= 0; i--) { |
| 167 | final ActivityRecord activity = activities.get(i); |
| 168 | if (launchedActivity == activity) { |
| 169 | continue; |
| 170 | } |
| 171 | if (!activity.stopped) { |
| 172 | return true; |
| 173 | } |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | /** |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 179 | * Notifies the tracker the the activity is actually launching. |
| 180 | * |
| 181 | * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the |
| 182 | * launch |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 183 | * @param launchedActivity the activity being launched |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 184 | * @param processRunning whether the process that will contains the activity is already running |
Jorim Jaggi | be67c90 | 2016-04-12 00:53:16 -0700 | [diff] [blame] | 185 | * @param processSwitch whether the process that will contain the activity didn't have any |
| 186 | * activity that was stopped, i.e. the started activity is "switching" |
| 187 | * processes |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 188 | */ |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 189 | private void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity, |
Jorim Jaggi | be67c90 | 2016-04-12 00:53:16 -0700 | [diff] [blame] | 190 | boolean processRunning, boolean processSwitch) { |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 191 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 192 | // If we are already in an existing transition, only update the activity name, but not the |
| 193 | // other attributes. |
| 194 | final int stackId = launchedActivity != null && launchedActivity.getStack() != null |
| 195 | ? launchedActivity.getStack().mStackId |
| 196 | : INVALID_STACK_ID; |
| 197 | final StackTransitionInfo info = mStackTransitionInfo.get(stackId); |
| 198 | if (launchedActivity != null && info != null) { |
| 199 | info.launchedActivity = launchedActivity; |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 203 | final boolean otherStacksLaunching = mStackTransitionInfo.size() > 0 && info == null; |
| 204 | if ((resultCode < 0 || launchedActivity == null || !processSwitch |
| 205 | || stackId == INVALID_STACK_ID) && !otherStacksLaunching) { |
Alison Cichowlas | 803054d | 2016-12-13 14:38:01 -0500 | [diff] [blame] | 206 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 207 | // Failed to launch or it was not a process switch, so we don't care about the timing. |
| 208 | reset(true /* abort */); |
| 209 | return; |
| 210 | } else if (otherStacksLaunching) { |
| 211 | // Don't log this stack but continue with the other stacks. |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | final StackTransitionInfo newInfo = new StackTransitionInfo(); |
| 216 | newInfo.launchedActivity = launchedActivity; |
| 217 | newInfo.currentTransitionProcessRunning = processRunning; |
| 218 | newInfo.startResult = resultCode; |
| 219 | mStackTransitionInfo.append(stackId, newInfo); |
| 220 | mCurrentTransitionDeviceUptime = (int) (SystemClock.uptimeMillis() / 1000); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Notifies the tracker that all windows of the app have been drawn. |
| 225 | */ |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 226 | void notifyWindowsDrawn(int stackId) { |
| 227 | final StackTransitionInfo info = mStackTransitionInfo.get(stackId); |
| 228 | if (info == null || info.loggedWindowsDrawn) { |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 229 | return; |
| 230 | } |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 231 | info.windowsDrawnDelayMs = calculateCurrentDelay(); |
| 232 | info.loggedWindowsDrawn = true; |
| 233 | if (allStacksWindowsDrawn() && mLoggedTransitionStarting) { |
| 234 | reset(false /* abort */); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Notifies the tracker that the starting window was drawn. |
| 240 | */ |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 241 | void notifyStartingWindowDrawn(int stackId) { |
| 242 | final StackTransitionInfo info = mStackTransitionInfo.get(stackId); |
| 243 | if (info == null || info.loggedStartingWindowDrawn) { |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 244 | return; |
| 245 | } |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 246 | info.loggedStartingWindowDrawn = true; |
| 247 | info.startingWindowDelayMs = calculateCurrentDelay(); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Notifies the tracker that the app transition is starting. |
| 252 | * |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 253 | * @param stackIdReasons A map from stack id to a reason integer, which must be on of |
| 254 | * ActivityManagerInternal.APP_TRANSITION_* reasons. |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 255 | */ |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 256 | void notifyTransitionStarting(SparseIntArray stackIdReasons) { |
Jorim Jaggi | d8a5777 | 2017-04-14 16:50:42 -0700 | [diff] [blame] | 257 | if (!isAnyTransitionActive() || mLoggedTransitionStarting) { |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 258 | return; |
| 259 | } |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 260 | mCurrentTransitionDelayMs = calculateCurrentDelay(); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 261 | mLoggedTransitionStarting = true; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 262 | for (int index = stackIdReasons.size() - 1; index >= 0; index--) { |
| 263 | final int stackId = stackIdReasons.keyAt(index); |
| 264 | final StackTransitionInfo info = mStackTransitionInfo.get(stackId); |
| 265 | if (info == null) { |
| 266 | continue; |
| 267 | } |
| 268 | info.reason = stackIdReasons.valueAt(index); |
| 269 | } |
| 270 | if (allStacksWindowsDrawn()) { |
| 271 | reset(false /* abort */); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 275 | private boolean allStacksWindowsDrawn() { |
| 276 | for (int index = mStackTransitionInfo.size() - 1; index >= 0; index--) { |
| 277 | if (!mStackTransitionInfo.valueAt(index).loggedWindowsDrawn) { |
| 278 | return false; |
| 279 | } |
| 280 | } |
| 281 | return true; |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 284 | private boolean isAnyTransitionActive() { |
| 285 | return mCurrentTransitionStartTime != INVALID_START_TIME |
| 286 | && mStackTransitionInfo.size() > 0; |
| 287 | } |
| 288 | |
| 289 | private void reset(boolean abort) { |
| 290 | if (!abort && isAnyTransitionActive()) { |
| 291 | logAppTransitionMultiEvents(); |
| 292 | } |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 293 | mCurrentTransitionStartTime = INVALID_START_TIME; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 294 | mCurrentTransitionDelayMs = -1; |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 295 | mLoggedTransitionStarting = false; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 296 | mStackTransitionInfo.clear(); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | private int calculateCurrentDelay() { |
| 300 | |
| 301 | // Shouldn't take more than 25 days to launch an app, so int is fine here. |
Alison Cichowlas | b7f67ab | 2017-04-25 18:04:40 -0400 | [diff] [blame^] | 302 | return (int) (SystemClock.uptimeMillis() - mCurrentTransitionStartTime); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 303 | } |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 304 | |
| 305 | private void logAppTransitionMultiEvents() { |
| 306 | for (int index = mStackTransitionInfo.size() - 1; index >= 0; index--) { |
| 307 | final StackTransitionInfo info = mStackTransitionInfo.valueAt(index); |
| 308 | final int type = getTransitionType(info); |
| 309 | if (type == -1) { |
| 310 | return; |
| 311 | } |
| 312 | final LogMaker builder = new LogMaker(APP_TRANSITION); |
| 313 | builder.setPackageName(info.launchedActivity.packageName); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 314 | builder.setType(type); |
Jason Monk | 8c09ac7 | 2017-03-16 11:53:40 -0400 | [diff] [blame] | 315 | builder.addTaggedData(FIELD_CLASS_NAME, info.launchedActivity.info.name); |
Todd Kennedy | 56279cb | 2017-04-17 14:53:49 -0700 | [diff] [blame] | 316 | final boolean isInstantApp = info.launchedActivity.info.applicationInfo.isInstantApp(); |
| 317 | if (isInstantApp && info.launchedActivity.launchedFromPackage != null) { |
Todd Kennedy | 50d946c1 | 2017-03-17 13:55:38 -0700 | [diff] [blame] | 318 | builder.addTaggedData(APP_TRANSITION_CALLING_PACKAGE_NAME, |
| 319 | info.launchedActivity.launchedFromPackage); |
| 320 | } |
| 321 | if (info.launchedActivity.info.launchToken != null) { |
| 322 | builder.addTaggedData(FIELD_INSTANT_APP_LAUNCH_TOKEN, |
| 323 | info.launchedActivity.info.launchToken); |
Todd Kennedy | b3b43130 | 2017-03-20 16:05:48 -0700 | [diff] [blame] | 324 | info.launchedActivity.info.launchToken = null; |
Todd Kennedy | 50d946c1 | 2017-03-17 13:55:38 -0700 | [diff] [blame] | 325 | } |
Todd Kennedy | 56279cb | 2017-04-17 14:53:49 -0700 | [diff] [blame] | 326 | builder.addTaggedData(APP_TRANSITION_IS_EPHEMERAL, isInstantApp ? 1 : 0); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 327 | builder.addTaggedData(APP_TRANSITION_DEVICE_UPTIME_SECONDS, |
| 328 | mCurrentTransitionDeviceUptime); |
| 329 | builder.addTaggedData(APP_TRANSITION_DELAY_MS, mCurrentTransitionDelayMs); |
| 330 | builder.setSubtype(info.reason); |
| 331 | if (info.startingWindowDelayMs != -1) { |
| 332 | builder.addTaggedData(APP_TRANSITION_STARTING_WINDOW_DELAY_MS, |
| 333 | info.startingWindowDelayMs); |
| 334 | } |
| 335 | builder.addTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS, info.windowsDrawnDelayMs); |
| 336 | mMetricsLogger.write(builder); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | private int getTransitionType(StackTransitionInfo info) { |
| 341 | if (info.currentTransitionProcessRunning) { |
| 342 | if (info.startResult == START_SUCCESS) { |
| 343 | return TYPE_TRANSITION_WARM_LAUNCH; |
| 344 | } else if (info.startResult == START_TASK_TO_FRONT) { |
| 345 | return TYPE_TRANSITION_HOT_LAUNCH; |
| 346 | } |
| 347 | } else if (info.startResult == START_SUCCESS) { |
| 348 | return TYPE_TRANSITION_COLD_LAUNCH; |
| 349 | } |
| 350 | return -1; |
| 351 | } |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 352 | } |