Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame^] | 1 | /* |
| 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 Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 16 | package com.android.launcher3.logging; |
| 17 | |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame^] | 18 | import android.text.TextUtils; |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 19 | import android.util.ArrayMap; |
| 20 | import android.util.SparseArray; |
Hyunyoung Song | 8ce6063 | 2016-08-15 16:22:20 -0700 | [diff] [blame] | 21 | import android.view.View; |
| 22 | |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 23 | import com.android.launcher3.ButtonDropTarget; |
| 24 | import com.android.launcher3.DeleteDropTarget; |
| 25 | import com.android.launcher3.InfoDropTarget; |
Hyunyoung Song | 8ce6063 | 2016-08-15 16:22:20 -0700 | [diff] [blame] | 26 | import com.android.launcher3.ItemInfo; |
| 27 | import com.android.launcher3.LauncherSettings; |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 28 | import com.android.launcher3.UninstallDropTarget; |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 29 | import com.android.launcher3.userevent.nano.LauncherLogProto.Action; |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 30 | import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType; |
| 31 | import com.android.launcher3.userevent.nano.LauncherLogProto.ControlType; |
| 32 | import com.android.launcher3.userevent.nano.LauncherLogProto.ItemType; |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 33 | import com.android.launcher3.userevent.nano.LauncherLogProto.LauncherEvent; |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 34 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
| 35 | |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 36 | import java.lang.reflect.Field; |
| 37 | import java.lang.reflect.Modifier; |
| 38 | |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 39 | /** |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 40 | * Helper methods for logging. |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 41 | */ |
| 42 | public class LoggerUtils { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 43 | private static final ArrayMap<Class, SparseArray<String>> sNameCache = new ArrayMap<>(); |
| 44 | private static final String UNKNOWN = "UNKNOWN"; |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 45 | |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame^] | 46 | public static String getFieldName(int value, Class c) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 47 | 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 Miranda | feba90f | 2016-10-06 10:53:29 -0700 | [diff] [blame] | 64 | } |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 65 | String result = cache.get(value); |
| 66 | return result != null ? result : UNKNOWN; |
Jon Miranda | feba90f | 2016-10-06 10:53:29 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 69 | public static String getActionStr(Action action) { |
Jon Miranda | feba90f | 2016-10-06 10:53:29 -0700 | [diff] [blame] | 70 | switch (action.type) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 71 | 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 Miranda | feba90f | 2016-10-06 10:53:29 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 77 | public static String getTargetStr(Target t) { |
Hyunyoung Song | 5aa2714 | 2016-07-21 11:48:37 -0700 | [diff] [blame] | 78 | if (t == null){ |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 79 | return ""; |
Hyunyoung Song | 5aa2714 | 2016-07-21 11:48:37 -0700 | [diff] [blame] | 80 | } |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 81 | switch (t.type) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 82 | case Target.Type.ITEM: |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 83 | return getItemStr(t); |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 84 | case Target.Type.CONTROL: |
| 85 | return getFieldName(t.controlType, ControlType.class); |
| 86 | case Target.Type.CONTAINER: |
Hyunyoung Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame^] | 87 | 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 Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 94 | default: |
| 95 | return "UNKNOWN TARGET TYPE"; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private static String getItemStr(Target t) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 100 | String typeStr = getFieldName(t.itemType, ItemType.class); |
Hyunyoung Song | 8ce6063 | 2016-08-15 16:22:20 -0700 | [diff] [blame] | 101 | 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 Song | 6aa3729 | 2017-02-06 10:46:24 -0800 | [diff] [blame^] | 110 | return typeStr + ", grid(" + t.gridX + "," + t.gridY + "), span(" + t.spanX + "," + t.spanY |
| 111 | + "), pageIdx=" + t.pageIndex; |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 112 | } |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 113 | |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 114 | public static Target newItemTarget(View v) { |
| 115 | return (v.getTag() instanceof ItemInfo) |
| 116 | ? newItemTarget((ItemInfo) v.getTag()) |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 117 | : newTarget(Target.Type.ITEM); |
Hyunyoung Song | ddec1c7 | 2016-04-12 18:32:04 -0700 | [diff] [blame] | 118 | } |
Hyunyoung Song | 5aa2714 | 2016-07-21 11:48:37 -0700 | [diff] [blame] | 119 | |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 120 | public static Target newItemTarget(ItemInfo info) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 121 | Target t = newTarget(Target.Type.ITEM); |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 122 | switch (info.itemType) { |
Hyunyoung Song | 8ce6063 | 2016-08-15 16:22:20 -0700 | [diff] [blame] | 123 | case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 124 | t.itemType = ItemType.APP_ICON; |
Hyunyoung Song | 8ce6063 | 2016-08-15 16:22:20 -0700 | [diff] [blame] | 125 | break; |
| 126 | case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 127 | t.itemType = ItemType.SHORTCUT; |
Hyunyoung Song | 8ce6063 | 2016-08-15 16:22:20 -0700 | [diff] [blame] | 128 | break; |
| 129 | case LauncherSettings.Favorites.ITEM_TYPE_FOLDER: |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 130 | t.itemType = ItemType.FOLDER_ICON; |
Hyunyoung Song | 8ce6063 | 2016-08-15 16:22:20 -0700 | [diff] [blame] | 131 | break; |
| 132 | case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 133 | t.itemType = ItemType.WIDGET; |
Hyunyoung Song | 8ce6063 | 2016-08-15 16:22:20 -0700 | [diff] [blame] | 134 | break; |
| 135 | case LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT: |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 136 | t.itemType = ItemType.DEEPSHORTCUT; |
Hyunyoung Song | 8ce6063 | 2016-08-15 16:22:20 -0700 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | return t; |
| 140 | } |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 141 | |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 142 | public static Target newDropTarget(View v) { |
| 143 | if (!(v instanceof ButtonDropTarget)) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 144 | return newTarget(Target.Type.CONTAINER); |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 145 | } |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 146 | Target t = newTarget(Target.Type.CONTROL); |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 147 | if (v instanceof InfoDropTarget) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 148 | t.controlType = ControlType.APPINFO_TARGET; |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 149 | } else if (v instanceof UninstallDropTarget) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 150 | t.controlType = ControlType.UNINSTALL_TARGET; |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 151 | } else if (v instanceof DeleteDropTarget) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 152 | t.controlType = ControlType.REMOVE_TARGET; |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 153 | } |
| 154 | return t; |
| 155 | } |
| 156 | |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 157 | public static Target newTarget(int targetType) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 158 | Target t = new Target(); |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 159 | t.type = targetType; |
| 160 | return t; |
| 161 | } |
| 162 | public static Target newContainerTarget(int containerType) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 163 | Target t = newTarget(Target.Type.CONTAINER); |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 164 | 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 Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 174 | Action a = newAction(Action.Type.COMMAND); |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 175 | a.command = command; |
| 176 | return a; |
| 177 | } |
| 178 | public static Action newTouchAction(int touch) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 179 | Action a = newAction(Action.Type.TOUCH); |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 180 | a.touch = touch; |
| 181 | return a; |
| 182 | } |
| 183 | |
| 184 | public static LauncherEvent newLauncherEvent(Action action, Target... srcTargets) { |
Sunny Goyal | 6c46a6d | 2016-11-23 02:24:32 +0530 | [diff] [blame] | 185 | LauncherEvent event = new LauncherEvent(); |
Sunny Goyal | fa94c79 | 2016-11-22 23:49:52 +0530 | [diff] [blame] | 186 | event.srcTarget = srcTargets; |
| 187 | event.action = action; |
| 188 | return event; |
Hyunyoung Song | 59a2380 | 2016-09-01 12:47:12 -0700 | [diff] [blame] | 189 | } |
Hyunyoung Song | 8fd5e93 | 2016-03-08 16:55:47 -0800 | [diff] [blame] | 190 | } |