summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/logging/StatsLogUtils.java
blob: b02a0504ffc629ed8471b0350ab36092bdd2f677 (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
package com.android.launcher3.logging;

import static com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType.DEFAULT_CONTAINERTYPE;

import android.view.View;
import android.view.ViewParent;

import com.android.launcher3.ItemInfo;
import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
import com.android.launcher3.userevent.nano.LauncherLogProto.Target;

import androidx.annotation.Nullable;


public class StatsLogUtils {

    // Defined in android.stats.launcher.nano
    // As they cannot be linked in this file, defining again.
    public final static int LAUNCHER_STATE_BACKGROUND = 0;
    public final static int LAUNCHER_STATE_HOME = 1;
    public final static int LAUNCHER_STATE_OVERVIEW = 2;
    public final static int LAUNCHER_STATE_ALLAPPS = 3;

    private final static int MAXIMUM_VIEW_HIERARCHY_LEVEL = 5;

    public interface LogStateProvider {
        int getCurrentState();
    }

    /**
     * Implemented by containers to provide a container source for a given child.
     *
     * Currently,
     */
    public interface LogContainerProvider {

        /**
         * Copies data from the source to the destination proto.
         *
         * @param v            source of the data
         * @param info         source of the data
         * @param target       dest of the data
         * @param targetParent dest of the data
         */
        void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent);
    }

    /**
     * Recursively finds the parent of the given child which implements IconLogInfoProvider
     */
    public static LogContainerProvider getLaunchProviderRecursive(@Nullable View v) {
        ViewParent parent;
        if (v != null) {
            parent = v.getParent();
        } else {
            return null;
        }

        // Optimization to only check up to 5 parents.
        int count = MAXIMUM_VIEW_HIERARCHY_LEVEL;
        while (parent != null && count-- > 0) {
            if (parent instanceof LogContainerProvider) {
                return (LogContainerProvider) parent;
            } else {
                parent = parent.getParent();
            }
        }
        return null;
    }

    public static int getContainerTypeFromState(int state) {
        int containerType = DEFAULT_CONTAINERTYPE;
        switch (state) {
            case StatsLogUtils.LAUNCHER_STATE_ALLAPPS:
                containerType = ContainerType.ALLAPPS;
                break;
            case StatsLogUtils.LAUNCHER_STATE_HOME:
                containerType = ContainerType.WORKSPACE;
                break;
            case StatsLogUtils.LAUNCHER_STATE_OVERVIEW:
                containerType = ContainerType.OVERVIEW;
                break;
        }
        return containerType;
    }
}