Merge "Fixing multiwindow drag operation during with rotation." into ub-launcher3-dorval
diff --git a/protos/launcher_log.proto b/protos/launcher_log.proto
index c42b142..909a429 100644
--- a/protos/launcher_log.proto
+++ b/protos/launcher_log.proto
@@ -79,6 +79,7 @@
   PREDICTION = 7;
   SEARCHRESULT = 8;
   DEEPSHORTCUTS = 9;
+  PINITEM = 10;    // confirmation screen
 }
 
 // Used to define what type of control a Target would represent.
@@ -124,6 +125,10 @@
   enum Command {
     HOME_INTENT = 0;
     BACK = 1;
+    ENTRY = 2;    // Indicates entry to one of Launcher container type target
+                  // not using the HOME_INTENT
+    CANCEL = 3;   // Indicates that a confirmation screen was cancelled
+    CONFIRM = 4;  // Indicates thata confirmation screen was accepted
   }
   optional Type type = 1;
   optional Touch touch = 2;
@@ -149,4 +154,6 @@
   optional int64 action_duration_millis = 4;
   optional int64 elapsed_container_millis = 5;
   optional int64 elapsed_session_millis = 6;
+
+  optional bool is_in_multi_window_mode = 7;
 }
diff --git a/src/com/android/launcher3/BaseActivity.java b/src/com/android/launcher3/BaseActivity.java
index e1a3ad0..410d590 100644
--- a/src/com/android/launcher3/BaseActivity.java
+++ b/src/com/android/launcher3/BaseActivity.java
@@ -38,11 +38,16 @@
 
     public final UserEventDispatcher getUserEventDispatcher() {
         if (mUserEventDispatcher == null) {
-            mUserEventDispatcher = UserEventDispatcher.get(this);
+            mUserEventDispatcher = UserEventDispatcher.newInstance(this,
+                    isInMultiWindowModeCompat());
         }
         return mUserEventDispatcher;
     }
 
+    public boolean isInMultiWindowModeCompat() {
+        return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
+    }
+
     public static BaseActivity fromContext(Context context) {
         if (context instanceof BaseActivity) {
             return (BaseActivity) context;
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 3be6225..0a1810a 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -365,7 +365,7 @@
 
         // Load configuration-specific DeviceProfile
         mDeviceProfile = app.getInvariantDeviceProfile().getDeviceProfile(this);
-        if (Utilities.ATLEAST_NOUGAT && isInMultiWindowMode()) {
+        if (isInMultiWindowModeCompat()) {
             Display display = getWindowManager().getDefaultDisplay();
             Point mwSize = new Point();
             display.getSize(mwSize);
@@ -2267,6 +2267,9 @@
 
         if (v instanceof Workspace) {
             if (mWorkspace.isInOverviewMode()) {
+                getUserEventDispatcher().logActionOnContainer(LauncherLogProto.Action.Type.TOUCH,
+                        LauncherLogProto.Action.Direction.NONE,
+                        LauncherLogProto.ContainerType.OVERVIEW, mWorkspace.getCurrentPage());
                 showWorkspace(true);
             }
             return;
diff --git a/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java b/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java
index ebe95d6..4a55e8c 100644
--- a/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java
+++ b/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java
@@ -33,6 +33,7 @@
 import android.widget.Toast;
 
 import com.android.launcher3.IconCache;
+import com.android.launcher3.LauncherSettings;
 import com.android.launcher3.R;
 import com.android.launcher3.ShortcutInfo;
 
@@ -61,6 +62,10 @@
         return mUser;
     }
 
+    public int getItemType() {
+        return LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
+    }
+
     public abstract CharSequence getLabel();
 
     public abstract Drawable getFullResIcon(IconCache cache);
diff --git a/src/com/android/launcher3/dragndrop/AddItemActivity.java b/src/com/android/launcher3/dragndrop/AddItemActivity.java
index 629f78a..09592a8 100644
--- a/src/com/android/launcher3/dragndrop/AddItemActivity.java
+++ b/src/com/android/launcher3/dragndrop/AddItemActivity.java
@@ -16,6 +16,11 @@
 
 package com.android.launcher3.dragndrop;
 
+import static com.android.launcher3.logging.LoggerUtils.newCommandAction;
+import static com.android.launcher3.logging.LoggerUtils.newContainerTarget;
+import static com.android.launcher3.logging.LoggerUtils.newItemTarget;
+import static com.android.launcher3.logging.LoggerUtils.newLauncherEvent;
+
 import android.annotation.TargetApi;
 import android.app.ActivityOptions;
 import android.appwidget.AppWidgetHost;
@@ -32,7 +37,9 @@
 import android.os.Bundle;
 import android.view.MotionEvent;
 import android.view.View;
-import android.view.View.*;
+import android.view.View.DragShadowBuilder;
+import android.view.View.OnLongClickListener;
+import android.view.View.OnTouchListener;
 
 import com.android.launcher3.BaseActivity;
 import com.android.launcher3.InstallShortcutReceiver;
@@ -46,6 +53,9 @@
 import com.android.launcher3.compat.PinItemRequestCompat;
 import com.android.launcher3.model.WidgetItem;
 import com.android.launcher3.shortcuts.ShortcutInfoCompat;
+import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
+import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
+import com.android.launcher3.widget.PendingAddShortcutInfo;
 import com.android.launcher3.widget.PendingAddWidgetInfo;
 import com.android.launcher3.widget.WidgetHostViewLoader;
 import com.android.launcher3.widget.WidgetImageView;
@@ -104,6 +114,12 @@
 
         mWidgetCell.setOnTouchListener(this);
         mWidgetCell.setOnLongClickListener(this);
+
+        // savedInstanceState is null when the activity is created the first time (i.e., avoids
+        // duplicate logging during rotation)
+        if (savedInstanceState == null) {
+            logCommand(Action.Command.ENTRY);
+        }
     }
 
     @Override
@@ -166,7 +182,10 @@
     }
 
     private void setupShortcut() {
-        WidgetItem item = new WidgetItem(new PinShortcutRequestActivityInfo(mRequest, this));
+        PinShortcutRequestActivityInfo shortcutInfo =
+                new PinShortcutRequestActivityInfo(mRequest, this);
+        WidgetItem item = new WidgetItem(shortcutInfo);
+        mWidgetCell.getWidgetView().setTag(new PendingAddShortcutInfo(shortcutInfo));
         mWidgetCell.applyFromCellItem(item, mApp.getWidgetCache());
         mWidgetCell.ensurePreview();
     }
@@ -189,6 +208,7 @@
         mWidgetOptions = WidgetHostViewLoader.getDefaultOptionsForWidget(this, mPendingWidgetInfo);
 
         WidgetItem item = new WidgetItem(widgetInfo, getPackageManager(), mIdp);
+        mWidgetCell.getWidgetView().setTag(mPendingWidgetInfo);
         mWidgetCell.applyFromCellItem(item, mApp.getWidgetCache());
         mWidgetCell.ensurePreview();
         return true;
@@ -198,6 +218,7 @@
      * Called when the cancel button is clicked.
      */
     public void onCancelClick(View v) {
+        logCommand(Action.Command.CANCEL);
         finish();
     }
 
@@ -208,6 +229,7 @@
         if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_SHORTCUT) {
             InstallShortcutReceiver.queueShortcut(
                     new ShortcutInfoCompat(mRequest.getShortcutInfo()), this);
+            logCommand(Action.Command.CONFIRM);
             mRequest.accept();
             finish();
             return;
@@ -235,10 +257,17 @@
         InstallShortcutReceiver.queueWidget(mRequest.getAppWidgetProviderInfo(this), widgetId, this);
         mWidgetOptions.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
         mRequest.accept(mWidgetOptions);
+        logCommand(Action.Command.CONFIRM);
         finish();
     }
 
     @Override
+    public void onBackPressed() {
+        logCommand(Action.Command.BACK);
+        super.onBackPressed();
+    }
+
+    @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == REQUEST_BIND_APPWIDGET) {
             int widgetId = data != null
@@ -268,4 +297,11 @@
         mPendingBindWidgetId = savedInstanceState
                 .getInt(STATE_EXTRA_WIDGET_ID, mPendingBindWidgetId);
     }
+
+    private void logCommand(int command) {
+        getUserEventDispatcher().dispatchUserEvent(newLauncherEvent(
+                newCommandAction(command),
+                newItemTarget(mWidgetCell.getWidgetView()),
+                newContainerTarget(ContainerType.PINITEM)), null);
+    }
 }
diff --git a/src/com/android/launcher3/dragndrop/PinItemDragListener.java b/src/com/android/launcher3/dragndrop/PinItemDragListener.java
index e714778..4b402f4 100644
--- a/src/com/android/launcher3/dragndrop/PinItemDragListener.java
+++ b/src/com/android/launcher3/dragndrop/PinItemDragListener.java
@@ -43,6 +43,7 @@
 import com.android.launcher3.compat.PinItemRequestCompat;
 import com.android.launcher3.folder.Folder;
 import com.android.launcher3.userevent.nano.LauncherLogProto;
+import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
 import com.android.launcher3.widget.PendingAddShortcutInfo;
 import com.android.launcher3.widget.PendingAddWidgetInfo;
 import com.android.launcher3.widget.PendingItemDragHelper;
@@ -241,7 +242,7 @@
     @Override
     public void fillInLogContainerData(View v, ItemInfo info, LauncherLogProto.Target target,
             LauncherLogProto.Target targetParent) {
-        // TODO: We should probably log something
+        targetParent.containerType = ContainerType.PINITEM;
     }
 
     private void postCleanup() {
diff --git a/src/com/android/launcher3/dragndrop/PinShortcutRequestActivityInfo.java b/src/com/android/launcher3/dragndrop/PinShortcutRequestActivityInfo.java
index 6a8c19f..26460d7 100644
--- a/src/com/android/launcher3/dragndrop/PinShortcutRequestActivityInfo.java
+++ b/src/com/android/launcher3/dragndrop/PinShortcutRequestActivityInfo.java
@@ -27,6 +27,7 @@
 
 import com.android.launcher3.IconCache;
 import com.android.launcher3.LauncherAppState;
+import com.android.launcher3.LauncherSettings;
 import com.android.launcher3.compat.LauncherAppsCompat;
 import com.android.launcher3.compat.PinItemRequestCompat;
 import com.android.launcher3.compat.ShortcutConfigActivityInfo;
@@ -55,6 +56,11 @@
     }
 
     @Override
+    public int getItemType() {
+        return LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT;
+    }
+
+    @Override
     public CharSequence getLabel() {
         return mInfo.getShortLabel();
     }
diff --git a/src/com/android/launcher3/logging/UserEventDispatcher.java b/src/com/android/launcher3/logging/UserEventDispatcher.java
index 90e4531..07e99c6 100644
--- a/src/com/android/launcher3/logging/UserEventDispatcher.java
+++ b/src/com/android/launcher3/logging/UserEventDispatcher.java
@@ -30,7 +30,6 @@
 import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
 import com.android.launcher3.config.ProviderConfig;
-import com.android.launcher3.userevent.nano.LauncherLogProto;
 import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
 import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
 import com.android.launcher3.userevent.nano.LauncherLogProto.LauncherEvent;
@@ -63,17 +62,11 @@
     private static final boolean IS_VERBOSE =
             ProviderConfig.IS_DOGFOOD_BUILD && Utilities.isPropertyEnabled(LogConfig.USEREVENT);
 
-    private static UserEventDispatcher sInstance;
-    private static final Object LOCK = new Object();
-
-    public static UserEventDispatcher get(Context context) {
-        synchronized (LOCK) {
-            if (sInstance == null) {
-                sInstance = Utilities.getOverrideObject(UserEventDispatcher.class,
-                        context.getApplicationContext(), R.string.user_event_dispatcher_class);
-            }
-            return sInstance;
-        }
+    public static UserEventDispatcher newInstance(Context context, boolean isInMultiWindowMode) {
+        UserEventDispatcher ued = Utilities.getOverrideObject(UserEventDispatcher.class,
+                context.getApplicationContext(), R.string.user_event_dispatcher_class);
+        ued.mIsInMultiWindowMode = isInMultiWindowMode;
+        return ued;
     }
 
     /**
@@ -118,6 +111,7 @@
     private long mElapsedContainerMillis;
     private long mElapsedSessionMillis;
     private long mActionDurationMillis;
+    private boolean mIsInMultiWindowMode;
 
     // Used for filling in predictedRank on {@link Target}s.
     private List<ComponentKey> mPredictedApps;
@@ -302,6 +296,7 @@
     }
 
     public void dispatchUserEvent(LauncherEvent ev, Intent intent) {
+        ev.isInMultiWindowMode = mIsInMultiWindowMode;
         ev.elapsedContainerMillis = SystemClock.uptimeMillis() - mElapsedContainerMillis;
         ev.elapsedSessionMillis = SystemClock.uptimeMillis() - mElapsedSessionMillis;
 
@@ -320,6 +315,7 @@
                 ev.elapsedContainerMillis,
                 ev.elapsedSessionMillis,
                 ev.actionDurationMillis);
+        log += "\n isInMultiWindowMode " + ev.isInMultiWindowMode;
         Log.d(TAG, log);
     }
 
diff --git a/src/com/android/launcher3/widget/PendingAddShortcutInfo.java b/src/com/android/launcher3/widget/PendingAddShortcutInfo.java
index 7eeb8bf..e8f13a1 100644
--- a/src/com/android/launcher3/widget/PendingAddShortcutInfo.java
+++ b/src/com/android/launcher3/widget/PendingAddShortcutInfo.java
@@ -32,6 +32,6 @@
         this.activityInfo = activityInfo;
         componentName = activityInfo.getComponent();
         user = activityInfo.getUser();
-        itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
+        itemType = activityInfo.getItemType();
     }
 }