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