1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.launcher3.logging;
17 
18 import android.os.Process;
19 import android.text.TextUtils;
20 
21 import com.android.launcher3.ItemInfo;
22 import com.android.launcher3.LauncherAppWidgetInfo;
23 import com.android.launcher3.LauncherSettings;
24 import com.android.launcher3.model.nano.LauncherDumpProto;
25 import com.android.launcher3.model.nano.LauncherDumpProto.ContainerType;
26 import com.android.launcher3.model.nano.LauncherDumpProto.DumpTarget;
27 import com.android.launcher3.model.nano.LauncherDumpProto.ItemType;
28 import com.android.launcher3.model.nano.LauncherDumpProto.UserType;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * This class can be used when proto definition doesn't support nesting.
35  */
36 public class DumpTargetWrapper {
37     DumpTarget node;
38     ArrayList<DumpTargetWrapper> children;
39 
DumpTargetWrapper()40     public DumpTargetWrapper() {
41         children = new ArrayList<>();
42     }
43 
DumpTargetWrapper(int containerType, int id)44     public DumpTargetWrapper(int containerType, int id) {
45         this();
46         node = newContainerTarget(containerType, id);
47     }
48 
DumpTargetWrapper(ItemInfo info)49     public DumpTargetWrapper(ItemInfo info) {
50         this();
51         node = newItemTarget(info);
52     }
53 
getDumpTarget()54     public DumpTarget getDumpTarget() {
55         return node;
56     }
57 
add(DumpTargetWrapper child)58     public void add(DumpTargetWrapper child) {
59         children.add(child);
60     }
61 
getFlattenedList()62     public List<DumpTarget> getFlattenedList() {
63         ArrayList<DumpTarget> list = new ArrayList<>();
64         list.add(node);
65         if (!children.isEmpty()) {
66             for(DumpTargetWrapper t: children) {
67                 list.addAll(t.getFlattenedList());
68             }
69             list.add(node); // add a delimiter empty object
70         }
71         return list;
72     }
newItemTarget(ItemInfo info)73     public DumpTarget newItemTarget(ItemInfo info) {
74         DumpTarget dt = new DumpTarget();
75         dt.type = DumpTarget.Type.ITEM;
76 
77         switch (info.itemType) {
78             case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
79                 dt.itemType = ItemType.APP_ICON;
80                 break;
81             case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
82                 dt.itemType = ItemType.UNKNOWN_ITEMTYPE;
83                 break;
84             case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
85                 dt.itemType = ItemType.WIDGET;
86                 break;
87             case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
88                 dt.itemType = ItemType.SHORTCUT;
89                 break;
90         }
91         return dt;
92     }
93 
newContainerTarget(int type, int id)94     public DumpTarget newContainerTarget(int type, int id) {
95         DumpTarget dt = new DumpTarget();
96         dt.type = DumpTarget.Type.CONTAINER;
97         dt.containerType = type;
98         dt.pageId = id;
99         return dt;
100     }
101 
getDumpTargetStr(DumpTarget t)102     public static String getDumpTargetStr(DumpTarget t) {
103         if (t == null){
104             return "";
105         }
106         switch (t.type) {
107             case LauncherDumpProto.DumpTarget.Type.ITEM:
108                 return getItemStr(t);
109             case LauncherDumpProto.DumpTarget.Type.CONTAINER:
110                 String str = LoggerUtils.getFieldName(t.containerType, ContainerType.class);
111                 if (t.containerType == ContainerType.WORKSPACE) {
112                     str += " id=" + t.pageId;
113                 } else if (t.containerType == ContainerType.FOLDER) {
114                     str += " grid(" + t.gridX + "," + t.gridY+ ")";
115                 }
116                 return str;
117             default:
118                 return "UNKNOWN TARGET TYPE";
119         }
120     }
121 
getItemStr(DumpTarget t)122     private static String getItemStr(DumpTarget t) {
123         String typeStr = LoggerUtils.getFieldName(t.itemType, ItemType.class);
124         if (!TextUtils.isEmpty(t.packageName)) {
125             typeStr += ", package=" + t.packageName;
126         }
127         if (!TextUtils.isEmpty(t.component)) {
128             typeStr += ", component=" + t.component;
129         }
130         return typeStr + ", grid(" + t.gridX + "," + t.gridY + "), span(" + t.spanX + "," + t.spanY
131                 + "), pageIdx=" + t.pageId + " user=" + t.userType;
132     }
133 
writeToDumpTarget(ItemInfo info)134     public DumpTarget writeToDumpTarget(ItemInfo info) {
135         node.component = info.getTargetComponent() == null? "":
136                 info.getTargetComponent().flattenToString();
137         node.packageName = info.getTargetComponent() == null? "":
138                 info.getTargetComponent().getPackageName();
139         if (info instanceof LauncherAppWidgetInfo) {
140             node.component = ((LauncherAppWidgetInfo) info).providerName.flattenToString();
141             node.packageName = ((LauncherAppWidgetInfo) info).providerName.getPackageName();
142         }
143 
144         node.gridX = info.cellX;
145         node.gridY = info.cellY;
146         node.spanX = info.spanX;
147         node.spanY = info.spanY;
148         node.userType = (info.user.equals(Process.myUserHandle()))? UserType.DEFAULT : UserType.WORK;
149         return node;
150     }
151 }
152