summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/logging/UserEventLogger.java
blob: 4e5b2c1b406cb8edc4936346be50b5b6167f2ec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.android.launcher3.logging;

import android.os.Bundle;
import android.util.Log;

import com.android.launcher3.ShortcutInfo;
import com.android.launcher3.Stats;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.userevent.nano.LauncherLogProto;

import java.util.Locale;

public abstract class UserEventLogger {

    private String TAG = "UserEventLogger";
    private boolean DEBUG = false;

    private long mElapsedContainerMillis;
    private long mElapsedSessionMillis;
    private long mActionDurationMillis;


    public final void logAppLaunch(String provider, ShortcutInfo shortcut, Bundle bundle) {
        if (FeatureFlags.LAUNCHER3_LEGACY_LOGGING) return;

        LauncherLogProto.LauncherEvent event = new LauncherLogProto.LauncherEvent();
        event.action = new LauncherLogProto.Action();
        event.action.type = LauncherLogProto.Action.TOUCH;
        event.action.touch = LauncherLogProto.Action.TAP;

        event.srcTarget = new LauncherLogProto.Target();
        event.srcTarget.type = LauncherLogProto.Target.ITEM;
        event.srcTarget.itemType = LauncherLogProto.APP_ICON;
        // TODO: package hash name should be different per device.
        event.srcTarget.packageNameHash = provider.hashCode();

        event.srcTarget.parent = new LauncherLogProto.Target();
        String subContainer = bundle.getString(Stats.SOURCE_EXTRA_SUB_CONTAINER);

        if (shortcut != null) {
            event.srcTarget.parent.containerType = LoggerUtils.getContainerType(shortcut);
            event.srcTarget.pageIndex = (int) shortcut.screenId;
            event.srcTarget.gridX = shortcut.cellX;
            event.srcTarget.gridX = shortcut.cellY;
        }
        if (subContainer != null) {
            event.srcTarget.parent.type = LauncherLogProto.Target.CONTAINER;
            if (subContainer.equals(Stats.SUB_CONTAINER_FOLDER)) {
                event.srcTarget.parent.containerType = LauncherLogProto.FOLDER;
            } else if (subContainer.equals(Stats.SUB_CONTAINER_ALL_APPS_A_Z)) {
                event.srcTarget.parent.containerType = LauncherLogProto.ALLAPPS;
            } else if (subContainer.equals(Stats.CONTAINER_HOTSEAT)) {
                event.srcTarget.parent.containerType = LauncherLogProto.HOTSEAT;
            } else if (subContainer.equals(Stats.SUB_CONTAINER_ALL_APPS_PREDICTION)) {
                event.srcTarget.parent.containerType = LauncherLogProto.PREDICTION;
            }

            if (DEBUG) {
                Log.d(TAG, String.format("parent bundle: %s %s %s %s",
                        bundle.getString(Stats.SOURCE_EXTRA_CONTAINER),
                        bundle.getString(Stats.SOURCE_EXTRA_CONTAINER_PAGE),
                        bundle.getString(Stats.SOURCE_EXTRA_SUB_CONTAINER),
                        bundle.getString(Stats.SOURCE_EXTRA_SUB_CONTAINER_PAGE)));
            }
        }
        event.elapsedContainerMillis = System.currentTimeMillis() - mElapsedContainerMillis;
        event.elapsedSessionMillis = System.currentTimeMillis() - mElapsedSessionMillis;
        processEvent(event);
    }

    /**
     * Currently logs following containers: workspace, allapps, widget tray.
     */
    public final void resetElapsedContainerMillis() {
        mElapsedContainerMillis = System.currentTimeMillis();
        if(DEBUG) {
            Log.d(TAG, "resetElapsedContainerMillis " + mElapsedContainerMillis);
        }
    }

    public final void resetElapsedSessionMillis() {
        mElapsedSessionMillis = System.currentTimeMillis();
        mElapsedContainerMillis = System.currentTimeMillis();
        if(DEBUG) {
            Log.d(TAG, "resetElapsedSessionMillis " + mElapsedSessionMillis);
        }
    }

    public final void resetActionDurationMillis() {
        mActionDurationMillis = System.currentTimeMillis();
        if(DEBUG) {
            Log.d(TAG, "resetElapsedContainerMillis " + mElapsedContainerMillis);
        }
    }

    public abstract void processEvent(LauncherLogProto.LauncherEvent ev);
}