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