Merge "update widgets model when package is updated" into ub-launcher3-burnaby
diff --git a/src/com/android/launcher3/AppsContainerView.java b/src/com/android/launcher3/AppsContainerView.java
index b247145..f7adaf8 100644
--- a/src/com/android/launcher3/AppsContainerView.java
+++ b/src/com/android/launcher3/AppsContainerView.java
@@ -269,19 +269,8 @@
 
         // Start the drag
         mLauncher.getWorkspace().beginDragShared(v, mLastTouchPos, this, false);
-
-        // We delay entering spring-loaded mode slightly to make sure the UI
-        // thready is free of any work.
-        postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                // We don't enter spring-loaded mode if the drag has been cancelled
-                if (mLauncher.getDragController().isDragging()) {
-                    // Go into spring loaded mode (must happen before we startDrag())
-                    mLauncher.enterSpringLoadedDragMode();
-                }
-            }
-        }, 150);
+        // Enter spring loaded mode
+        mLauncher.enterSpringLoadedDragMode();
 
         return false;
     }
diff --git a/src/com/android/launcher3/AppsGridAdapter.java b/src/com/android/launcher3/AppsGridAdapter.java
index 954c59f..5bc3981 100644
--- a/src/com/android/launcher3/AppsGridAdapter.java
+++ b/src/com/android/launcher3/AppsGridAdapter.java
@@ -33,13 +33,13 @@
      */
     public static class ViewHolder extends RecyclerView.ViewHolder {
         public View mContent;
-        public boolean mIsSectionRow;
+        public boolean mIsSectionHeader;
         public boolean mIsEmptyRow;
 
-        public ViewHolder(View v, boolean isSectionRow, boolean isEmptyRow) {
+        public ViewHolder(View v, boolean isSectionHeader, boolean isEmptyRow) {
             super(v);
             mContent = v;
-            mIsSectionRow = isSectionRow;
+            mIsSectionHeader = isSectionHeader;
             mIsEmptyRow = isEmptyRow;
         }
     }
@@ -72,36 +72,26 @@
         @Override
         public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
             List<AlphabeticalAppsList.AdapterItem> items = mApps.getAdapterItems();
-            if (items.isEmpty()) {
-                return;
-            }
-
             for (int i = 0; i < parent.getChildCount(); i++) {
                 View child = parent.getChildAt(i);
                 ViewHolder holder = (ViewHolder) parent.getChildViewHolder(child);
-                if (holder != null) {
-                    GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams)
-                            child.getLayoutParams();
-                    if (!holder.mIsSectionRow && !holder.mIsEmptyRow && !lp.isItemRemoved()) {
-                        if (items.get(holder.getPosition() - 1).isSectionHeader) {
-                            // Draw at the parent
-                            AlphabeticalAppsList.AdapterItem item =
-                                    items.get(holder.getPosition());
-                            String section = item.sectionName;
-                            mSectionTextPaint.getTextBounds(section, 0, section.length(),
-                                    mTmpBounds);
-                            if (mIsRtl) {
-                                int left = parent.getWidth() - mPaddingStart - mStartMargin;
-                                c.drawText(section, left + (mStartMargin - mTmpBounds.width()) / 2,
-                                        child.getTop() + (2 * child.getPaddingTop()) +
-                                                mTmpBounds.height(), mSectionTextPaint);
-                            } else {
-                                int left = mPaddingStart;
-                                c.drawText(section, left + (mStartMargin - mTmpBounds.width()) / 2,
-                                    child.getTop() + (2 * child.getPaddingTop()) +
-                                            mTmpBounds.height(), mSectionTextPaint);
-                            }
-                        }
+                if (shouldDrawItemSection(holder, child, items)) {
+                    // Draw at the parent
+                    AlphabeticalAppsList.AdapterItem item =
+                            items.get(holder.getPosition());
+                    String section = item.sectionName;
+                    mSectionTextPaint.getTextBounds(section, 0, section.length(),
+                            mTmpBounds);
+                    if (mIsRtl) {
+                        int left = parent.getWidth() - mPaddingStart - mStartMargin;
+                        c.drawText(section, left + (mStartMargin - mTmpBounds.width()) / 2,
+                                child.getTop() + (2 * child.getPaddingTop()) +
+                                        mTmpBounds.height(), mSectionTextPaint);
+                    } else {
+                        int left = mPaddingStart;
+                        c.drawText(section, left + (mStartMargin - mTmpBounds.width()) / 2,
+                            child.getTop() + (2 * child.getPaddingTop()) +
+                                    mTmpBounds.height(), mSectionTextPaint);
                     }
                 }
             }
@@ -112,6 +102,31 @@
                 RecyclerView.State state) {
             // Do nothing
         }
+
+        private boolean shouldDrawItemSection(ViewHolder holder, View child,
+                List<AlphabeticalAppsList.AdapterItem> items) {
+            // Ensure item is not already removed
+            GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams)
+                    child.getLayoutParams();
+            if (lp.isItemRemoved()) {
+                return false;
+            }
+            // Ensure we have a valid holder
+            if (holder == null) {
+                return false;
+            }
+            // Ensure it's not an empty row
+            if (holder.mIsEmptyRow) {
+                return false;
+            }
+            // Ensure we have a holder position
+            int pos = holder.getPosition();
+            if (pos <= 0 || pos >= items.size()) {
+                return false;
+            }
+            // Only draw the first item in the section (the first one after the section header)
+            return items.get(pos - 1).isSectionHeader && !items.get(pos).isSectionHeader;
+        }
     }
 
     private LayoutInflater mLayoutInflater;
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 955eee7..4533089 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -522,13 +522,19 @@
         mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
             @Override
             public void onAllAppsBoundsChanged(Rect bounds) {
+                if (LOGD) {
+                    Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
+                }
                 mAppsView.setFixedBounds(Launcher.this, bounds);
             }
 
             @Override
             public void dismissAllApps() {
-                showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true, null,
-                        false /* notifyLauncherCallbacks */);
+                // Dismiss All Apps if we aren't already paused/invisible
+                if (!mPaused) {
+                    showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
+                            null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
+                }
             }
         });
         return true;
@@ -1012,6 +1018,13 @@
         }
         mOnResumeState = State.NONE;
 
+        // Restore the apps state if we are in all apps
+        if (mState == State.APPS) {
+            if (mLauncherCallbacks != null) {
+                mLauncherCallbacks.onAllAppsShown();
+            }
+        }
+
         // Background was set to gradient in onPause(), restore to black if in all apps.
         setWorkspaceBackground(mState == State.WORKSPACE);
 
@@ -1072,7 +1085,7 @@
                 mWorkspace.getCustomContentCallbacks().onShow(true);
             }
         }
-        mWorkspace.updateInteractionForState();
+        updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
         mWorkspace.onResume();
 
         if (!isWorkspaceLoading()) {
@@ -2096,8 +2109,6 @@
     public void startSearch(String initialQuery, boolean selectInitialQuery,
             Bundle appSearchData, boolean globalSearch) {
 
-        showWorkspace(true);
-
         if (initialQuery == null) {
             // Use any text typed in the launcher as the initial query
             initialQuery = getTypedText();
@@ -2116,6 +2127,9 @@
         if (clearTextImmediately) {
             clearTypedText();
         }
+
+        // We need to show the workspace after starting the search
+        showWorkspace(true);
     }
 
     /**
@@ -2861,6 +2875,21 @@
         }
     }
 
+    /** Updates the interaction state. */
+    public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
+        // Only update the interacting state if we are transitioning to/from a view without an
+        // overlay
+        boolean fromStateWithoutOverlay = fromState != Workspace.State.NORMAL &&
+                fromState != Workspace.State.NORMAL_HIDDEN;
+        boolean toStateWithoutOverlay = toState != Workspace.State.NORMAL &&
+                toState != Workspace.State.NORMAL_HIDDEN;
+        if (toStateWithoutOverlay) {
+            onInteractionBegin();
+        } else if (fromStateWithoutOverlay) {
+            onInteractionEnd();
+        }
+    }
+
     void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
         try {
             LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
@@ -3165,7 +3194,6 @@
 
         if (v instanceof Workspace) {
             if (!mWorkspace.isInOverviewMode()) {
-
                 if (!mWorkspace.isTouchActive()) {
                     showOverviewMode(true);
                     mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
@@ -3337,8 +3365,12 @@
             // Send an accessibility event to announce the context change
             getWindow().getDecorView()
                     .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
-
-            onWorkspaceShown(animated);
+            if (notifyLauncherCallbacks) {
+                // Dismiss all apps when the workspace is shown
+                if (mLauncherCallbacks != null) {
+                    mLauncherCallbacks.onAllAppsHidden();
+                }
+            }
         }
     }
 
@@ -3348,10 +3380,6 @@
                 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
                 null /* onCompleteRunnable */);
         mState = State.WORKSPACE;
-        onWorkspaceShown(animated);
-    }
-
-    public void onWorkspaceShown(boolean animated) {
     }
 
     /**
@@ -3411,6 +3439,18 @@
                 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
     }
 
+    /**
+     * Updates the workspace and interaction state on state change, and return the animation to this
+     * new state.
+     */
+    public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
+            boolean animated, HashMap<View, Integer> layerViews) {
+        Workspace.State fromState = mWorkspace.getState();
+        Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
+        updateInteraction(fromState, toState);
+        return anim;
+    }
+
     public void enterSpringLoadedDragMode() {
         Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
                 mState.name()));
@@ -3429,6 +3469,14 @@
             final Runnable onCompleteRunnable) {
         if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
 
+        if (successfulDrop) {
+            // We need to trigger all apps hidden to notify search to update itself before the
+            // delayed call to showWorkspace below
+            if (mLauncherCallbacks != null) {
+                mLauncherCallbacks.onAllAppsHidden();
+            }
+        }
+
         mHandler.postDelayed(new Runnable() {
             @Override
             public void run() {
@@ -3449,13 +3497,10 @@
 
     void exitSpringLoadedDragMode() {
         if (mState == State.APPS_SPRING_LOADED) {
-            mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
-            mState = State.APPS;
+            showAppsView(true, false);
         } else if (mState == State.WIDGETS_SPRING_LOADED) {
-            mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
-            mState = State.WIDGETS;
+            showWidgetsView(true, false);
         }
-        // Otherwise, we are not in spring loaded mode, so don't do anything.
     }
 
     void lockAllApps() {
diff --git a/src/com/android/launcher3/LauncherCallbacks.java b/src/com/android/launcher3/LauncherCallbacks.java
index 2fee81c..25c86c9 100644
--- a/src/com/android/launcher3/LauncherCallbacks.java
+++ b/src/com/android/launcher3/LauncherCallbacks.java
@@ -51,6 +51,7 @@
     public void finishBindingItems(final boolean upgradePath);
     public void onClickAllAppsButton(View v);
     public void onAllAppsShown();
+    public void onAllAppsHidden();
     public void bindAllApplications(ArrayList<AppInfo> apps);
     public void onClickFolderIcon(View v);
     public void onClickAppShortcut(View v);
diff --git a/src/com/android/launcher3/LauncherExtension.java b/src/com/android/launcher3/LauncherExtension.java
index e4fdbbc..8174af0 100644
--- a/src/com/android/launcher3/LauncherExtension.java
+++ b/src/com/android/launcher3/LauncherExtension.java
@@ -128,6 +128,10 @@
         }
 
         @Override
+        public void onAllAppsHidden() {
+        }
+
+        @Override
         public void bindAllApplications(ArrayList<AppInfo> apps) {
         }
 
diff --git a/src/com/android/launcher3/LauncherStateTransitionAnimation.java b/src/com/android/launcher3/LauncherStateTransitionAnimation.java
index 9e005f2..51f84bf 100644
--- a/src/com/android/launcher3/LauncherStateTransitionAnimation.java
+++ b/src/com/android/launcher3/LauncherStateTransitionAnimation.java
@@ -249,8 +249,8 @@
 
         // Create the workspace animation.
         // NOTE: this call apparently also sets the state for the workspace if !animated
-        Animator workspaceAnim = mLauncher.getWorkspace().setStateWithAnimation(
-                toWorkspaceState, -1, animated, layerViews);
+        Animator workspaceAnim = mLauncher.startWorkspaceStateChangeAnimation(toWorkspaceState, -1,
+                animated, layerViews);
 
         if (animated && initialized) {
             mStateAnimation = LauncherAnimUtils.createAnimatorSet();
@@ -546,8 +546,8 @@
 
         // Create the workspace animation.
         // NOTE: this call apparently also sets the state for the workspace if !animated
-        Animator workspaceAnim = mLauncher.getWorkspace().setStateWithAnimation(
-                toWorkspaceState, toWorkspacePage, animated, layerViews);
+        Animator workspaceAnim = mLauncher.startWorkspaceStateChangeAnimation(toWorkspaceState,
+                toWorkspacePage, animated, layerViews);
 
         if (animated && initialized) {
             mStateAnimation = LauncherAnimUtils.createAnimatorSet();
diff --git a/src/com/android/launcher3/SearchDropTargetBar.java b/src/com/android/launcher3/SearchDropTargetBar.java
index 44a76b7..4cdf1ca 100644
--- a/src/com/android/launcher3/SearchDropTargetBar.java
+++ b/src/com/android/launcher3/SearchDropTargetBar.java
@@ -180,9 +180,6 @@
         prepareStartAnimation(mDropTargetBar);
         mShowDropTargetBarAnim.start();
         hideSearchBar(true);
-        if (mQSBSearchBar != null) {
-            mQSBSearchBar.setVisibility(View.GONE);
-        }
     }
 
     /**
@@ -193,9 +190,6 @@
         prepareStartAnimation(mDropTargetBar);
         mShowDropTargetBarAnim.reverse();
         showSearchBar(true);
-        if (mQSBSearchBar != null) {
-            mQSBSearchBar.setVisibility(View.VISIBLE);
-        }
     }
 
     /*
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index 4c76092..e7a41e0 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -2047,14 +2047,6 @@
         return -offsetFromTopEdge + mInsets.top + offsetToCenterInOverview;
     }
 
-    public void updateInteractionForState() {
-        if (mState != State.NORMAL) {
-            mLauncher.onInteractionBegin();
-        } else {
-            mLauncher.onInteractionEnd();
-        }
-    }
-
     /**
      * Sets the current workspace {@link State}, returning an animation transitioning the workspace
      * to that new state.
@@ -2067,7 +2059,6 @@
 
         // Update the current state
         mState = toState;
-        updateInteractionForState();
         updateAccessibilityFlags();
 
         return workspaceAnim;