1 package com.android.launcher3.logging;
2 
3 import static com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType.DEFAULT_CONTAINERTYPE;
4 
5 import android.view.View;
6 import android.view.ViewParent;
7 
8 import com.android.launcher3.ItemInfo;
9 import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
10 import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
11 
12 import androidx.annotation.Nullable;
13 
14 
15 public class StatsLogUtils {
16 
17     // Defined in android.stats.launcher.nano
18     // As they cannot be linked in this file, defining again.
19     public final static int LAUNCHER_STATE_BACKGROUND = 0;
20     public final static int LAUNCHER_STATE_HOME = 1;
21     public final static int LAUNCHER_STATE_OVERVIEW = 2;
22     public final static int LAUNCHER_STATE_ALLAPPS = 3;
23 
24     private final static int MAXIMUM_VIEW_HIERARCHY_LEVEL = 5;
25 
26     public interface LogStateProvider {
getCurrentState()27         int getCurrentState();
28     }
29 
30     /**
31      * Implemented by containers to provide a container source for a given child.
32      *
33      * Currently,
34      */
35     public interface LogContainerProvider {
36 
37         /**
38          * Copies data from the source to the destination proto.
39          *
40          * @param v            source of the data
41          * @param info         source of the data
42          * @param target       dest of the data
43          * @param targetParent dest of the data
44          */
fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent)45         void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent);
46     }
47 
48     /**
49      * Recursively finds the parent of the given child which implements IconLogInfoProvider
50      */
getLaunchProviderRecursive(@ullable View v)51     public static LogContainerProvider getLaunchProviderRecursive(@Nullable View v) {
52         ViewParent parent;
53         if (v != null) {
54             parent = v.getParent();
55         } else {
56             return null;
57         }
58 
59         // Optimization to only check up to 5 parents.
60         int count = MAXIMUM_VIEW_HIERARCHY_LEVEL;
61         while (parent != null && count-- > 0) {
62             if (parent instanceof LogContainerProvider) {
63                 return (LogContainerProvider) parent;
64             } else {
65                 parent = parent.getParent();
66             }
67         }
68         return null;
69     }
70 
getContainerTypeFromState(int state)71     public static int getContainerTypeFromState(int state) {
72         int containerType = DEFAULT_CONTAINERTYPE;
73         switch (state) {
74             case StatsLogUtils.LAUNCHER_STATE_ALLAPPS:
75                 containerType = ContainerType.ALLAPPS;
76                 break;
77             case StatsLogUtils.LAUNCHER_STATE_HOME:
78                 containerType = ContainerType.WORKSPACE;
79                 break;
80             case StatsLogUtils.LAUNCHER_STATE_OVERVIEW:
81                 containerType = ContainerType.OVERVIEW;
82                 break;
83         }
84         return containerType;
85     }
86 }
87