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