blob: 499fdc7d381e45ebb1e46823333d23bb2e0425d1 [file] [log] [blame]
Hyunyoung Song6aa37292017-02-06 10:46:24 -08001/*
2 * Copyright (C) 2016 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 */
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080016package com.android.launcher3.logging;
17
Hyunyoung Song6aa37292017-02-06 10:46:24 -080018import android.text.TextUtils;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053019import android.util.ArrayMap;
20import android.util.SparseArray;
Hyunyoung Song8ce60632016-08-15 16:22:20 -070021import android.view.View;
22
Hyunyoung Song59a23802016-09-01 12:47:12 -070023import com.android.launcher3.ButtonDropTarget;
24import com.android.launcher3.DeleteDropTarget;
25import com.android.launcher3.InfoDropTarget;
Hyunyoung Song8ce60632016-08-15 16:22:20 -070026import com.android.launcher3.ItemInfo;
27import com.android.launcher3.LauncherSettings;
Hyunyoung Song59a23802016-09-01 12:47:12 -070028import com.android.launcher3.UninstallDropTarget;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080029import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053030import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
31import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType;
32import com.android.launcher3.userevent.nano.LauncherLogProto.ItemType;
Sunny Goyalfa94c792016-11-22 23:49:52 +053033import com.android.launcher3.userevent.nano.LauncherLogProto.LauncherEvent;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080034import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
35
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053036import java.lang.reflect.Field;
37import java.lang.reflect.Modifier;
38
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080039/**
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053040 * Helper methods for logging.
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080041 */
42public class LoggerUtils {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053043 private static final ArrayMap<Class, SparseArray<String>> sNameCache = new ArrayMap<>();
44 private static final String UNKNOWN = "UNKNOWN";
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080045
Hyunyoung Song6aa37292017-02-06 10:46:24 -080046 public static String getFieldName(int value, Class c) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053047 SparseArray<String> cache;
48 synchronized (sNameCache) {
49 cache = sNameCache.get(c);
50 if (cache == null) {
51 cache = new SparseArray<>();
52 for (Field f : c.getDeclaredFields()) {
53 if (f.getType() == int.class && Modifier.isStatic(f.getModifiers())) {
54 try {
55 f.setAccessible(true);
56 cache.put(f.getInt(null), f.getName());
57 } catch (IllegalAccessException e) {
58 // Ignore
59 }
60 }
61 }
62 sNameCache.put(c, cache);
63 }
Jon Mirandafeba90f2016-10-06 10:53:29 -070064 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053065 String result = cache.get(value);
66 return result != null ? result : UNKNOWN;
Jon Mirandafeba90f2016-10-06 10:53:29 -070067 }
68
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053069 public static String getActionStr(Action action) {
Jon Mirandafeba90f2016-10-06 10:53:29 -070070 switch (action.type) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053071 case Action.Type.TOUCH: return getFieldName(action.touch, Action.Touch.class);
72 case Action.Type.COMMAND: return getFieldName(action.command, Action.Command.class);
73 default: return UNKNOWN;
Jon Mirandafeba90f2016-10-06 10:53:29 -070074 }
75 }
76
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080077 public static String getTargetStr(Target t) {
Hyunyoung Song5aa27142016-07-21 11:48:37 -070078 if (t == null){
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053079 return "";
Hyunyoung Song5aa27142016-07-21 11:48:37 -070080 }
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080081 switch (t.type) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053082 case Target.Type.ITEM:
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080083 return getItemStr(t);
Sunny Goyal6c46a6d2016-11-23 02:24:32 +053084 case Target.Type.CONTROL:
85 return getFieldName(t.controlType, ControlType.class);
86 case Target.Type.CONTAINER:
Hyunyoung Song6aa37292017-02-06 10:46:24 -080087 String str = getFieldName(t.containerType, ContainerType.class);
88 if (t.containerType == ContainerType.WORKSPACE) {
89 str += " id=" + t.pageIndex;
90 } else if (t.containerType == ContainerType.FOLDER) {
91 str += " grid(" + t.gridX + "," + t.gridY+ ")";
92 }
93 return str;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -080094 default:
95 return "UNKNOWN TARGET TYPE";
96 }
97 }
98
99 private static String getItemStr(Target t) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530100 String typeStr = getFieldName(t.itemType, ItemType.class);
Hyunyoung Song8ce60632016-08-15 16:22:20 -0700101 if (t.packageNameHash != 0) {
102 typeStr += ", packageHash=" + t.packageNameHash;
103 }
104 if (t.componentHash != 0) {
105 typeStr += ", componentHash=" + t.componentHash;
106 }
107 if (t.intentHash != 0) {
108 typeStr += ", intentHash=" + t.intentHash;
109 }
Hyunyoung Song6aa37292017-02-06 10:46:24 -0800110 return typeStr + ", grid(" + t.gridX + "," + t.gridY + "), span(" + t.spanX + "," + t.spanY
111 + "), pageIdx=" + t.pageIndex;
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800112 }
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700113
Sunny Goyalfa94c792016-11-22 23:49:52 +0530114 public static Target newItemTarget(View v) {
115 return (v.getTag() instanceof ItemInfo)
116 ? newItemTarget((ItemInfo) v.getTag())
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530117 : newTarget(Target.Type.ITEM);
Hyunyoung Songddec1c72016-04-12 18:32:04 -0700118 }
Hyunyoung Song5aa27142016-07-21 11:48:37 -0700119
Sunny Goyalfa94c792016-11-22 23:49:52 +0530120 public static Target newItemTarget(ItemInfo info) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530121 Target t = newTarget(Target.Type.ITEM);
Hyunyoung Song59a23802016-09-01 12:47:12 -0700122 switch (info.itemType) {
Hyunyoung Song8ce60632016-08-15 16:22:20 -0700123 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530124 t.itemType = ItemType.APP_ICON;
Hyunyoung Song8ce60632016-08-15 16:22:20 -0700125 break;
126 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530127 t.itemType = ItemType.SHORTCUT;
Hyunyoung Song8ce60632016-08-15 16:22:20 -0700128 break;
129 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530130 t.itemType = ItemType.FOLDER_ICON;
Hyunyoung Song8ce60632016-08-15 16:22:20 -0700131 break;
132 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530133 t.itemType = ItemType.WIDGET;
Hyunyoung Song8ce60632016-08-15 16:22:20 -0700134 break;
135 case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT:
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530136 t.itemType = ItemType.DEEPSHORTCUT;
Hyunyoung Song8ce60632016-08-15 16:22:20 -0700137 break;
138 }
139 return t;
140 }
Hyunyoung Song59a23802016-09-01 12:47:12 -0700141
Sunny Goyalfa94c792016-11-22 23:49:52 +0530142 public static Target newDropTarget(View v) {
143 if (!(v instanceof ButtonDropTarget)) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530144 return newTarget(Target.Type.CONTAINER);
Hyunyoung Song59a23802016-09-01 12:47:12 -0700145 }
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530146 Target t = newTarget(Target.Type.CONTROL);
Hyunyoung Song59a23802016-09-01 12:47:12 -0700147 if (v instanceof InfoDropTarget) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530148 t.controlType = ControlType.APPINFO_TARGET;
Hyunyoung Song59a23802016-09-01 12:47:12 -0700149 } else if (v instanceof UninstallDropTarget) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530150 t.controlType = ControlType.UNINSTALL_TARGET;
Hyunyoung Song59a23802016-09-01 12:47:12 -0700151 } else if (v instanceof DeleteDropTarget) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530152 t.controlType = ControlType.REMOVE_TARGET;
Hyunyoung Song59a23802016-09-01 12:47:12 -0700153 }
154 return t;
155 }
156
Sunny Goyalfa94c792016-11-22 23:49:52 +0530157 public static Target newTarget(int targetType) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530158 Target t = new Target();
Sunny Goyalfa94c792016-11-22 23:49:52 +0530159 t.type = targetType;
160 return t;
161 }
162 public static Target newContainerTarget(int containerType) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530163 Target t = newTarget(Target.Type.CONTAINER);
Sunny Goyalfa94c792016-11-22 23:49:52 +0530164 t.containerType = containerType;
165 return t;
166 }
167
168 public static Action newAction(int type) {
169 Action a = new Action();
170 a.type = type;
171 return a;
172 }
173 public static Action newCommandAction(int command) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530174 Action a = newAction(Action.Type.COMMAND);
Sunny Goyalfa94c792016-11-22 23:49:52 +0530175 a.command = command;
176 return a;
177 }
178 public static Action newTouchAction(int touch) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530179 Action a = newAction(Action.Type.TOUCH);
Sunny Goyalfa94c792016-11-22 23:49:52 +0530180 a.touch = touch;
181 return a;
182 }
183
184 public static LauncherEvent newLauncherEvent(Action action, Target... srcTargets) {
Sunny Goyal6c46a6d2016-11-23 02:24:32 +0530185 LauncherEvent event = new LauncherEvent();
Sunny Goyalfa94c792016-11-22 23:49:52 +0530186 event.srcTarget = srcTargets;
187 event.action = action;
188 return event;
Hyunyoung Song59a23802016-09-01 12:47:12 -0700189 }
Hyunyoung Song8fd5e932016-03-08 16:55:47 -0800190}