Merge "Fix hotseat flicker b/29648104" into ub-launcher3-calgary
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index 3e66654..3e98c13 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -236,7 +236,7 @@
 
     /** Runs the specified runnable immediately if called from the main thread, otherwise it is
      * posted on the main thread handler. */
-    @Thunk void runOnMainThread(Runnable r) {
+    private void runOnMainThread(Runnable r) {
         if (sWorkerThread.getThreadId() == Process.myTid()) {
             // If we are on the worker thread, post onto the main handler
             mHandler.post(r);
@@ -247,7 +247,7 @@
 
     /** Runs the specified runnable immediately if called from the worker thread, otherwise it is
      * posted on the worker thread handler. */
-    @Thunk static void runOnWorkerThread(Runnable r) {
+    private static void runOnWorkerThread(Runnable r) {
         if (sWorkerThread.getThreadId() == Process.myTid()) {
             r.run();
         } else {
@@ -1298,8 +1298,8 @@
                 // If there is already one running, tell it to stop.
                 stopLoaderLocked();
                 mLoaderTask = new LoaderTask(mApp.getContext(), synchronousBindPage);
-                if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE
-                        && mAllAppsLoaded && mWorkspaceLoaded && !mIsLoaderTaskRunning) {
+                if (synchronousBindPage != PagedView.INVALID_RESTORE_PAGE && mAllAppsLoaded
+                        && mWorkspaceLoaded && mDeepShortcutsLoaded && !mIsLoaderTaskRunning) {
                     mLoaderTask.runBindSynchronousPage(synchronousBindPage);
                 } else {
                     sWorkerThread.setPriority(Thread.NORM_PRIORITY);
@@ -1441,6 +1441,8 @@
             // XXX: For now, continue posting the binding of AllApps as there are other issues that
             //      arise from that.
             onlyBindAllApps();
+
+            bindDeepShortcuts();
         }
 
         public void run() {
@@ -2660,12 +2662,7 @@
                     }
                 }
             };
-            boolean isRunningOnMainThread = !(sWorkerThread.getThreadId() == Process.myTid());
-            if (isRunningOnMainThread) {
-                r.run();
-            } else {
-                mHandler.post(r);
-            }
+            runOnMainThread(r);
         }
 
         private void loadAllApps() {
@@ -2777,7 +2774,7 @@
                     mDeepShortcutsLoaded = true;
                 }
             }
-            bindDeepShortcutMapOnMainThread();
+            bindDeepShortcuts();
         }
 
         public void dumpState() {
@@ -2810,10 +2807,10 @@
         }
     }
 
-    private void bindDeepShortcutMapOnMainThread() {
+    public void bindDeepShortcuts() {
         final MultiHashMap<ComponentKey, String> shortcutMapCopy = new MultiHashMap<>();
         shortcutMapCopy.putAll(mBgDeepShortcutMap);
-        mHandler.post(new Runnable() {
+        Runnable r = new Runnable() {
             @Override
             public void run() {
                 Callbacks callbacks = getCallback();
@@ -2821,7 +2818,8 @@
                     callbacks.bindDeepShortcutMap(shortcutMapCopy);
                 }
             }
-        });
+        };
+        runOnMainThread(r);
     }
 
     /**
@@ -3322,7 +3320,7 @@
 
             // Update the deep shortcut map, in case the list of ids has changed for an activity.
             updateDeepShortcutMap(mPackageName, mShortcuts);
-            bindDeepShortcutMapOnMainThread();
+            bindDeepShortcuts();
         }
     }
 
diff --git a/src/com/android/launcher3/allapps/AllAppsTransitionController.java b/src/com/android/launcher3/allapps/AllAppsTransitionController.java
index 304e898..8a14a66 100644
--- a/src/com/android/launcher3/allapps/AllAppsTransitionController.java
+++ b/src/com/android/launcher3/allapps/AllAppsTransitionController.java
@@ -32,7 +32,8 @@
  * If release velocity < THRES1, snap according to either top or bottom depending on whether it's
  *     closer to top or closer to the page indicator.
  */
-public class AllAppsTransitionController implements TouchController, VerticalPullDetector.Listener {
+public class AllAppsTransitionController implements TouchController, VerticalPullDetector.Listener,
+        View.OnLayoutChangeListener {
 
     private static final String TAG = "AllAppsTrans";
     private static final boolean DBG = false;
@@ -426,18 +427,19 @@
         mAppsView = appsView;
         mHotseat = hotseat;
         mWorkspace = workspace;
-        mHotseat.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){
-            public void onLayoutChange(View v, int left, int top, int right, int bottom,
-                    int oldLeft, int oldTop, int oldRight, int oldBottom) {
-                if (!mLauncher.getDeviceProfile().isVerticalBarLayout()) {
-                    mShiftRange = top;
-                } else {
-                    mShiftRange = bottom;
-                }
-                if (!mLauncher.isAllAppsVisible()) {
-                    setProgress(mShiftRange);
-                }
-            }
-        });
+        mHotseat.addOnLayoutChangeListener(this);
+    }
+
+    @Override
+    public void onLayoutChange(View v, int left, int top, int right, int bottom,
+            int oldLeft, int oldTop, int oldRight, int oldBottom) {
+        if (!mLauncher.getDeviceProfile().isVerticalBarLayout()) {
+            mShiftRange = top;
+        } else {
+            mShiftRange = bottom;
+        }
+        if (!mLauncher.isAllAppsVisible()) {
+            setProgress(mShiftRange);
+        }
     }
 }
diff --git a/src/com/android/launcher3/dragndrop/DragLayer.java b/src/com/android/launcher3/dragndrop/DragLayer.java
index aebb1fd..ce97536 100644
--- a/src/com/android/launcher3/dragndrop/DragLayer.java
+++ b/src/com/android/launcher3/dragndrop/DragLayer.java
@@ -230,6 +230,28 @@
             }
         }
 
+        // Remove the shortcuts container when touching outside of it.
+        DeepShortcutsContainer deepShortcutsContainer = (DeepShortcutsContainer)
+                findViewById(R.id.deep_shortcuts_container);
+        if (deepShortcutsContainer != null) {
+            if (isEventOverView(deepShortcutsContainer, ev)) {
+                // Let the container handle the event.
+                return false;
+            } else {
+                if (isInAccessibleDrag()) {
+                    // Do not close the container if in drag and drop.
+                    if (!isEventOverDropTargetBar(ev)) {
+                        return true;
+                    }
+                } else {
+                    removeView(deepShortcutsContainer);
+                    // We let touches on the original icon go through so that users can launch
+                    // the app with one tap if they don't find a shortcut they want.
+                    return !isEventOverView(deepShortcutsContainer.getDeferredDragIcon(), ev);
+                }
+            }
+        }
+
         Folder currentFolder = mLauncher.getWorkspace().getOpenFolder();
         if (currentFolder != null && intercept) {
             if (currentFolder.isEditingName()) {
@@ -251,25 +273,6 @@
                 }
             }
         }
-
-        // Remove the shortcuts container when touching outside of it.
-        DeepShortcutsContainer deepShortcutsContainer = (DeepShortcutsContainer)
-                findViewById(R.id.deep_shortcuts_container);
-        if (deepShortcutsContainer != null) {
-            if (!isEventOverView(deepShortcutsContainer, ev)) {
-                if (isInAccessibleDrag()) {
-                    // Do not close the container if in drag and drop.
-                    if (!isEventOverDropTargetBar(ev)) {
-                        return true;
-                    }
-                } else {
-                    removeView(deepShortcutsContainer);
-                    // We let touches on the original icon go through so that users can launch
-                    // the app with one tap if they don't find a shortcut they want.
-                    return !isEventOverView(deepShortcutsContainer.getDeferredDragIcon(), ev);
-                }
-            }
-        }
         return false;
     }
 
diff --git a/src/com/android/launcher3/shortcuts/DeepShortcutsContainer.java b/src/com/android/launcher3/shortcuts/DeepShortcutsContainer.java
index 008b265..6f3875c 100644
--- a/src/com/android/launcher3/shortcuts/DeepShortcutsContainer.java
+++ b/src/com/android/launcher3/shortcuts/DeepShortcutsContainer.java
@@ -359,6 +359,8 @@
         mLauncher.getDragLayer().setController(mLauncher.getDragController());
         mLauncher.getWorkspace().beginDragShared(v, mIconLastTouchPos, this, false);
         ((DragLayer) getParent()).removeView(this);
+        // TODO: support dragging from within folder without having to close it
+        mLauncher.closeFolder();
         return false;
     }