Merge "Add logging for notifications." into ub-launcher3-master
diff --git a/protos/launcher_log.proto b/protos/launcher_log.proto
index 33041db..c42b142 100644
--- a/protos/launcher_log.proto
+++ b/protos/launcher_log.proto
@@ -64,6 +64,7 @@
   DEEPSHORTCUT = 5;
   SEARCHBOX = 6;
   EDITTEXT = 7;
+  NOTIFICATION = 8;
 }
 
 // Used to define what type of container a Target would represent.
diff --git a/src/com/android/launcher3/dragndrop/DragController.java b/src/com/android/launcher3/dragndrop/DragController.java
index e31c8ff..7410ae6 100644
--- a/src/com/android/launcher3/dragndrop/DragController.java
+++ b/src/com/android/launcher3/dragndrop/DragController.java
@@ -582,8 +582,8 @@
             }
         }
         final View dropTargetAsView = dropTarget instanceof View ? (View) dropTarget : null;
-        mLauncher.getUserEventDispatcher().logDragNDrop(mDragObject, dropTargetAsView);
         if (!mIsInPreDrag) {
+            mLauncher.getUserEventDispatcher().logDragNDrop(mDragObject, dropTargetAsView);
             mDragObject.dragSource.onDropCompleted(
                     dropTargetAsView, mDragObject, flingAnimation != null, accepted);
         }
diff --git a/src/com/android/launcher3/logging/UserEventDispatcher.java b/src/com/android/launcher3/logging/UserEventDispatcher.java
index 8ab33dc..b4b62a8 100644
--- a/src/com/android/launcher3/logging/UserEventDispatcher.java
+++ b/src/com/android/launcher3/logging/UserEventDispatcher.java
@@ -16,6 +16,7 @@
 
 package com.android.launcher3.logging;
 
+import android.app.PendingIntent;
 import android.content.ComponentName;
 import android.content.Intent;
 import android.os.SystemClock;
@@ -112,7 +113,7 @@
     // intentHash                       required
     // --------------------------------------------------------------
 
-    protected LauncherEvent createLauncherEvent(View v, Intent intent) {
+    protected LauncherEvent createLauncherEvent(View v, int intentHashCode, ComponentName cn) {
         LauncherEvent event = newLauncherEvent(newTouchAction(Action.Touch.TAP),
                 newItemTarget(v), newTarget(Target.Type.CONTAINER));
 
@@ -120,8 +121,7 @@
         int idx = 0;
         if (fillInLogContainerData(event, v)) {
             ItemInfo itemInfo = (ItemInfo) v.getTag();
-            event.srcTarget[idx].intentHash = intent.hashCode();
-            ComponentName cn = intent.getComponent();
+            event.srcTarget[idx].intentHash = intentHashCode;
             if (cn != null) {
                 event.srcTarget[idx].packageNameHash = cn.getPackageName().hashCode();
                 event.srcTarget[idx].componentHash = cn.hashCode();
@@ -146,13 +146,22 @@
     }
 
     public void logAppLaunch(View v, Intent intent) {
-        LauncherEvent ev = createLauncherEvent(v, intent);
+        LauncherEvent ev = createLauncherEvent(v, intent.hashCode(), intent.getComponent());
         if (ev == null) {
             return;
         }
         dispatchUserEvent(ev, intent);
     }
 
+    public void logNotificationLaunch(View v, PendingIntent intent) {
+        ComponentName dummyComponent = new ComponentName(intent.getCreatorPackage(), "--dummy--");
+        LauncherEvent ev = createLauncherEvent(v, intent.hashCode(), dummyComponent);
+        if (ev == null) {
+            return;
+        }
+        dispatchUserEvent(ev, null);
+    }
+
     public void logActionCommand(int command, int containerType) {
         logActionCommand(command, containerType, 0);
     }
@@ -199,9 +208,17 @@
         dispatchUserEvent(event, null);
     }
 
+    public void logActionOnItem(int action, int dir, int itemType) {
+        Target itemTarget = newTarget(Target.Type.ITEM);
+        itemTarget.itemType = itemType;
+        LauncherEvent event = newLauncherEvent(newTouchAction(action), itemTarget);
+        event.action.dir = dir;
+        dispatchUserEvent(event, null);
+    }
+
     public void logDeepShortcutsOpen(View icon) {
         LogContainerProvider provider = getLaunchProviderRecursive(icon);
-        if (icon == null && !(icon.getTag() instanceof ItemInfo)) {
+        if (icon == null || !(icon.getTag() instanceof ItemInfo)) {
             return;
         }
         ItemInfo info = (ItemInfo) icon.getTag();
diff --git a/src/com/android/launcher3/notification/NotificationInfo.java b/src/com/android/launcher3/notification/NotificationInfo.java
index 73cba28..77a18c7 100644
--- a/src/com/android/launcher3/notification/NotificationInfo.java
+++ b/src/com/android/launcher3/notification/NotificationInfo.java
@@ -94,6 +94,7 @@
                 view, 0, 0, view.getWidth(), view.getHeight()).toBundle();
         try {
             intent.send(null, 0, null, null, null, null, activityOptions);
+            launcher.getUserEventDispatcher().logNotificationLaunch(view, intent);
         } catch (PendingIntent.CanceledException e) {
             e.printStackTrace();
         }
diff --git a/src/com/android/launcher3/notification/NotificationItemView.java b/src/com/android/launcher3/notification/NotificationItemView.java
index 422722d..639447d 100644
--- a/src/com/android/launcher3/notification/NotificationItemView.java
+++ b/src/com/android/launcher3/notification/NotificationItemView.java
@@ -29,15 +29,16 @@
 import android.widget.FrameLayout;
 import android.widget.TextView;
 
+import com.android.launcher3.ItemInfo;
 import com.android.launcher3.LauncherAnimUtils;
 import com.android.launcher3.R;
 import com.android.launcher3.graphics.IconPalette;
+import com.android.launcher3.logging.UserEventDispatcher.LogContainerProvider;
 import com.android.launcher3.popup.PopupItemView;
+import com.android.launcher3.userevent.nano.LauncherLogProto;
 
 import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
 
 import static com.android.launcher3.LauncherAnimUtils.animateViewHeight;
 
@@ -47,7 +48,7 @@
  * The footer contains: A list of just the icons of all the notifications past the first one.
  * @see NotificationFooterLayout
  */
-public class NotificationItemView extends PopupItemView {
+public class NotificationItemView extends PopupItemView implements LogContainerProvider {
 
     private static final Rect sTempRect = new Rect();
 
@@ -57,7 +58,6 @@
     private NotificationFooterLayout mFooter;
     private SwipeHelper mSwipeHelper;
     private boolean mAnimatingNextIcon;
-    private IconPalette mIconPalette;
 
     public NotificationItemView(Context context) {
         this(context, null, 0);
@@ -114,7 +114,6 @@
     }
 
     public void applyColors(IconPalette iconPalette) {
-        mIconPalette = iconPalette;
         setBackgroundTintList(ColorStateList.valueOf(iconPalette.secondaryColor));
         mHeader.setBackgroundTintList(ColorStateList.valueOf(iconPalette.backgroundColor));
         mHeader.setTextColor(ColorStateList.valueOf(iconPalette.textColor));
@@ -174,4 +173,11 @@
         animation.playSequentially(removeMainView, removeRest);
         return animation;
     }
+
+    @Override
+    public void fillInLogContainerData(View v, ItemInfo info, LauncherLogProto.Target target,
+            LauncherLogProto.Target targetParent) {
+        target.itemType = LauncherLogProto.ItemType.NOTIFICATION;
+        targetParent.containerType = LauncherLogProto.ContainerType.DEEPSHORTCUTS;
+    }
 }
diff --git a/src/com/android/launcher3/notification/NotificationMainView.java b/src/com/android/launcher3/notification/NotificationMainView.java
index 7fed608..6677c2f 100644
--- a/src/com/android/launcher3/notification/NotificationMainView.java
+++ b/src/com/android/launcher3/notification/NotificationMainView.java
@@ -26,11 +26,13 @@
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
+import com.android.launcher3.ItemInfo;
 import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherAnimUtils;
 import com.android.launcher3.LauncherViewPropertyAnimator;
 import com.android.launcher3.R;
 import com.android.launcher3.graphics.IconPalette;
+import com.android.launcher3.userevent.nano.LauncherLogProto;
 
 /**
  * A {@link LinearLayout} that contains a single notification, e.g. icon + title + text.
@@ -88,6 +90,9 @@
                 getContext(), mIconPalette.backgroundColor));
         setOnClickListener(mNotificationInfo);
         setTranslationX(0);
+        // Add a dummy ItemInfo so that logging populates the correct container and item types
+        // instead of DEFAULT_CONTAINERTYPE and DEFAULT_ITEMTYPE, respectively.
+        setTag(new ItemInfo());
         if (animate) {
             AnimatorSet animation = LauncherAnimUtils.createAnimatorSet();
             Animator textFade = new LauncherViewPropertyAnimator(mTextView).alpha(1);
@@ -134,8 +139,13 @@
 
     @Override
     public void onChildDismissed(View v) {
-        Launcher.getLauncher(getContext()).getPopupDataProvider().cancelNotification(
+        Launcher launcher = Launcher.getLauncher(getContext());
+        launcher.getPopupDataProvider().cancelNotification(
                 mNotificationInfo.notificationKey);
+        launcher.getUserEventDispatcher().logActionOnItem(
+                LauncherLogProto.Action.Touch.SWIPE,
+                LauncherLogProto.Action.Direction.RIGHT, // Assume all swipes are right for logging.
+                LauncherLogProto.ItemType.NOTIFICATION);
     }
 
     @Override
diff --git a/src/com/android/launcher3/popup/PopupContainerWithArrow.java b/src/com/android/launcher3/popup/PopupContainerWithArrow.java
index af1bd9b..b8e2dc8 100644
--- a/src/com/android/launcher3/popup/PopupContainerWithArrow.java
+++ b/src/com/android/launcher3/popup/PopupContainerWithArrow.java
@@ -696,7 +696,7 @@
     @Override
     public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) {
         target.itemType = ItemType.DEEPSHORTCUT;
-        // TODO: add target.rank
+        target.rank = info.rank;
         targetParent.containerType = ContainerType.DEEPSHORTCUTS;
     }
 
diff --git a/src/com/android/launcher3/popup/PopupPopulator.java b/src/com/android/launcher3/popup/PopupPopulator.java
index f990fa2..d2814ee 100644
--- a/src/com/android/launcher3/popup/PopupPopulator.java
+++ b/src/com/android/launcher3/popup/PopupPopulator.java
@@ -171,6 +171,7 @@
                     // Use unbadged icon for the menu.
                     si.iconBitmap = LauncherIcons.createShortcutIcon(
                             shortcut, launcher, false /* badged */);
+                    si.rank = i;
                     uiHandler.post(new UpdateShortcutChild(container, shortcutViews.get(i),
                             si, shortcut));
                 }