Merge "Removing view layer support during state animation." into ub-launcher3-master
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 0a536f1..0c573ac 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -42,13 +42,11 @@
 import android.annotation.TargetApi;
 import android.app.ActivityOptions;
 import android.app.AlertDialog;
-import android.app.SearchManager;
 import android.appwidget.AppWidgetHostView;
 import android.appwidget.AppWidgetManager;
 import android.content.ActivityNotFoundException;
 import android.content.BroadcastReceiver;
 import android.content.ComponentCallbacks2;
-import android.content.ComponentName;
 import android.content.Context;
 import android.content.ContextWrapper;
 import android.content.DialogInterface;
@@ -70,8 +68,6 @@
 import android.os.StrictMode;
 import android.os.UserHandle;
 import android.support.annotation.Nullable;
-import android.text.Selection;
-import android.text.SpannableStringBuilder;
 import android.text.TextUtils;
 import android.text.method.TextKeyListener;
 import android.util.Log;
@@ -89,7 +85,6 @@
 import android.view.ViewGroup;
 import android.view.accessibility.AccessibilityEvent;
 import android.view.animation.OvershootInterpolator;
-import android.view.inputmethod.InputMethodManager;
 import android.widget.Toast;
 
 import com.android.launcher3.DropTarget.DragObject;
@@ -243,8 +238,6 @@
     // that results in widgets being inflated in the wrong orientation.
     private int mOrientation;
 
-    private SpannableStringBuilder mDefaultKeySsb = null;
-
     @Thunk boolean mWorkspaceLoading = true;
 
     private boolean mPaused = true;
@@ -259,14 +252,13 @@
     private ModelWriter mModelWriter;
     private IconCache mIconCache;
     private LauncherAccessibilityDelegate mAccessibilityDelegate;
-    private boolean mHasFocus = false;
 
     private ObjectAnimator mScrimAnimator;
     private boolean mShouldFadeInScrim;
 
     private PopupDataProvider mPopupDataProvider;
 
-    private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<>();
+    private int mSynchronouslyBoundPage = PagedView.INVALID_PAGE;
 
     // We only want to get the SharedPreferences once since it does an FS stat each time we get
     // it from the context.
@@ -400,8 +392,7 @@
         }
 
         // For handling default keys
-        mDefaultKeySsb = new SpannableStringBuilder();
-        Selection.setSelection(mDefaultKeySsb, 0);
+        setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
 
         // On large interfaces, or on devices that a user has specifically enabled screen rotation,
         // we want the screen to auto-rotate based on the current orientation
@@ -851,8 +842,6 @@
             mLauncherCallbacks.onResume();
         }
 
-        clearTypedText();
-
         TraceHelper.endSection("ON_RESUME");
     }
 
@@ -920,72 +909,6 @@
         }
     }
 
-    @Override
-    public Object onRetainNonConfigurationInstance() {
-        // Flag the loader to stop early before switching
-        if (mModel.isCurrentCallbacks(this)) {
-            mModel.stopLoader();
-        }
-        //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
-
-        return Boolean.TRUE;
-    }
-
-    // We can't hide the IME if it was forced open.  So don't bother
-    @Override
-    public void onWindowFocusChanged(boolean hasFocus) {
-        super.onWindowFocusChanged(hasFocus);
-        mHasFocus = hasFocus;
-
-        if (mLauncherCallbacks != null) {
-            mLauncherCallbacks.onWindowFocusChanged(hasFocus);
-        }
-    }
-
-    private boolean acceptFilter() {
-        final InputMethodManager inputManager = (InputMethodManager)
-                getSystemService(Context.INPUT_METHOD_SERVICE);
-        return !inputManager.isFullscreenMode();
-    }
-
-    @Override
-    public boolean onKeyDown(int keyCode, KeyEvent event) {
-        final int uniChar = event.getUnicodeChar();
-        final boolean handled = super.onKeyDown(keyCode, event);
-        final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
-        if (!handled && acceptFilter() && isKeyNotWhitespace) {
-            boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
-                    keyCode, event);
-            if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
-                // something usable has been typed - start a search
-                // the typed text will be retrieved and cleared by
-                // showSearchDialog()
-                // If there are multiple keystrokes before the search dialog takes focus,
-                // onSearchRequested() will be called for every keystroke,
-                // but it is idempotent, so it's fine.
-                return onSearchRequested();
-            }
-        }
-
-        // Eat the long press event so the keyboard doesn't come up.
-        if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
-            return true;
-        }
-
-        return handled;
-    }
-
-    private String getTypedText() {
-        return mDefaultKeySsb.toString();
-    }
-
-    @Override
-    public void clearTypedText() {
-        mDefaultKeySsb.clear();
-        mDefaultKeySsb.clearSpans();
-        Selection.setSelection(mDefaultKeySsb, 0);
-    }
-
     public boolean isInState(LauncherState state) {
         return mStateManager.getState() == state;
     }
@@ -1337,7 +1260,7 @@
         TraceHelper.beginSection("NEW_INTENT");
         super.onNewIntent(intent);
 
-        boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
+        boolean alreadyOnHome = hasWindowFocus() && ((intent.getFlags() &
                 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
                 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
 
@@ -1393,9 +1316,7 @@
     @Override
     public void onRestoreInstanceState(Bundle state) {
         super.onRestoreInstanceState(state);
-        for (int page: mSynchronouslyBoundPages) {
-            mWorkspace.restoreInstanceStateForChild(page);
-        }
+        mWorkspace.restoreInstanceStateForChild(mSynchronouslyBoundPage);
     }
 
     @Override
@@ -1503,11 +1424,6 @@
     @Override
     public void startSearch(String initialQuery, boolean selectInitialQuery,
             Bundle appSearchData, boolean globalSearch) {
-
-        if (initialQuery == null) {
-            // Use any text typed in the launcher as the initial query
-            initialQuery = getTypedText();
-        }
         if (appSearchData == null) {
             appSearchData = new Bundle();
             appSearchData.putString("source", "launcher-search");
@@ -1516,69 +1432,13 @@
         if (mLauncherCallbacks == null ||
                 !mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData)) {
             // Starting search from the callbacks failed. Start the default global search.
-            startGlobalSearch(initialQuery, selectInitialQuery, appSearchData, null);
+            super.startSearch(initialQuery, selectInitialQuery, appSearchData, true);
         }
 
         // We need to show the workspace after starting the search
         mStateManager.goToState(NORMAL);
     }
 
-    /**
-     * Starts the global search activity. This code is a copied from SearchManager
-     */
-    public void startGlobalSearch(String initialQuery,
-            boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
-        final SearchManager searchManager =
-            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
-        ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
-        if (globalSearchActivity == null) {
-            Log.w(TAG, "No global search activity found.");
-            return;
-        }
-        Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
-        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        intent.setComponent(globalSearchActivity);
-        // Make sure that we have a Bundle to put source in
-        if (appSearchData == null) {
-            appSearchData = new Bundle();
-        } else {
-            appSearchData = new Bundle(appSearchData);
-        }
-        // Set source to package name of app that starts global search if not set already.
-        if (!appSearchData.containsKey("source")) {
-            appSearchData.putString("source", getPackageName());
-        }
-        intent.putExtra(SearchManager.APP_DATA, appSearchData);
-        if (!TextUtils.isEmpty(initialQuery)) {
-            intent.putExtra(SearchManager.QUERY, initialQuery);
-        }
-        if (selectInitialQuery) {
-            intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
-        }
-        intent.setSourceBounds(sourceBounds);
-        try {
-            startActivity(intent);
-        } catch (ActivityNotFoundException ex) {
-            Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
-        }
-    }
-
-    @Override
-    public boolean onPrepareOptionsMenu(Menu menu) {
-        super.onPrepareOptionsMenu(menu);
-        if (mLauncherCallbacks != null) {
-            return mLauncherCallbacks.onPrepareOptionsMenu(menu);
-        }
-        return false;
-    }
-
-    @Override
-    public boolean onSearchRequested() {
-        startSearch(null, false, null, true);
-        // Use a custom animation for launching search
-        return true;
-    }
-
     public boolean isWorkspaceLocked() {
         return mWorkspaceLoading || mPendingRequestArgs != null;
     }
@@ -2766,7 +2626,7 @@
     }
 
     public void onPageBoundSynchronously(int page) {
-        mSynchronouslyBoundPages.add(page);
+        mSynchronouslyBoundPage = page;
     }
 
     @Override
@@ -2816,11 +2676,7 @@
      * Implementation of the method from LauncherModel.Callbacks.
      */
     public void finishBindingItems() {
-        Runnable r = new Runnable() {
-            public void run() {
-                finishBindingItems();
-            }
-        };
+        Runnable r = this::finishBindingItems;
         if (waitUntilResume(r)) {
             return;
         }
diff --git a/src/com/android/launcher3/LauncherCallbacks.java b/src/com/android/launcher3/LauncherCallbacks.java
index 928258f..914d9eb 100644
--- a/src/com/android/launcher3/LauncherCallbacks.java
+++ b/src/com/android/launcher3/LauncherCallbacks.java
@@ -48,10 +48,8 @@
     void onActivityResult(int requestCode, int resultCode, Intent data);
     void onRequestPermissionsResult(int requestCode, String[] permissions,
             int[] grantResults);
-    void onWindowFocusChanged(boolean hasFocus);
     void onAttachedToWindow();
     void onDetachedFromWindow();
-    boolean onPrepareOptionsMenu(Menu menu);
     void dump(String prefix, FileDescriptor fd, PrintWriter w, String[] args);
     void onHomeIntent();
     boolean handleBackPressed();
diff --git a/src/com/android/launcher3/LauncherExterns.java b/src/com/android/launcher3/LauncherExterns.java
index 887859c..272bbf6 100644
--- a/src/com/android/launcher3/LauncherExterns.java
+++ b/src/com/android/launcher3/LauncherExterns.java
@@ -24,11 +24,9 @@
  */
 public interface LauncherExterns {
 
-    public boolean setLauncherCallbacks(LauncherCallbacks callbacks);
+    boolean setLauncherCallbacks(LauncherCallbacks callbacks);
 
-    public SharedPreferences getSharedPrefs();
+    SharedPreferences getSharedPrefs();
 
-    public void setLauncherOverlay(Launcher.LauncherOverlay overlay);
-
-    void clearTypedText();
+    void setLauncherOverlay(Launcher.LauncherOverlay overlay);
 }
diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java
index 23a694c..d67fb5d 100644
--- a/src/com/android/launcher3/allapps/AllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java
@@ -552,20 +552,9 @@
             return;
         }
         mHeader.setVisibility(View.VISIBLE);
+        mHeader.setup(mAH, mComponentToAppMap, mNumPredictedAppsPerRow);
 
-        boolean usePredictionRow = mHasPredictions && !mSearchModeWhileUsingTabs;
-        int contentHeight = usePredictionRow ? mLauncher.getDeviceProfile().allAppsCellHeightPx : 0;;
-        if (usePredictionRow && !mUsingTabs) {
-            contentHeight += getResources()
-                    .getDimensionPixelSize(R.dimen.all_apps_prediction_row_divider_height);
-        }
-        AllAppsRecyclerView mainRV = mAH[AdapterHolder.MAIN].recyclerView;
-        AllAppsRecyclerView workRV = mAH[AdapterHolder.WORK].recyclerView;
-        mHeader.setup(mainRV, workRV, contentHeight);
-        mHeader.getPredictionRow().setup(mAH[AdapterHolder.MAIN].adapter,
-                mComponentToAppMap, mNumPredictedAppsPerRow);
-
-        int padding = contentHeight;
+        int padding = mHeader.getPredictionRow().getExpectedHeight();
         if (mHasPredictions && !mUsingTabs) {
             padding += mHeader.getPaddingTop() + mHeader.getPaddingBottom();
         }
diff --git a/src/com/android/launcher3/allapps/FloatingHeaderView.java b/src/com/android/launcher3/allapps/FloatingHeaderView.java
index 4f1a0d5..dc3afb5 100644
--- a/src/com/android/launcher3/allapps/FloatingHeaderView.java
+++ b/src/com/android/launcher3/allapps/FloatingHeaderView.java
@@ -30,12 +30,15 @@
 import android.view.ViewGroup;
 import android.widget.RelativeLayout;
 
+import com.android.launcher3.AppInfo;
 import com.android.launcher3.R;
+import com.android.launcher3.util.ComponentKey;
+
+import java.util.HashMap;
 
 public class FloatingHeaderView extends RelativeLayout implements
         ValueAnimator.AnimatorUpdateListener {
 
-    private static final boolean SHOW_PREDICTIONS_ONLY_ON_TOP = true;
 
     private final Rect mClip = new Rect(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
     private final ValueAnimator mAnimator = ValueAnimator.ofInt(0, 0);
@@ -43,22 +46,11 @@
     private final RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
         @Override
         public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
-            if (SHOW_PREDICTIONS_ONLY_ON_TOP) {
-                return;
-            }
-            if (!mTopOnlyMode && newState == RecyclerView.SCROLL_STATE_IDLE
-                    && mTranslationY != -mMaxTranslation && mTranslationY != 0) {
-                float scroll = Math.abs(getCurrentScroll());
-                boolean expand =  scroll > mMaxTranslation
-                        ? Math.abs(mTranslationY) < mMaxTranslation / 2 : true;
-                setExpanded(expand);
-            }
         }
 
         @Override
         public void onScrolled(RecyclerView rv, int dx, int dy) {
-            boolean isMainRV = rv == mMainRV;
-            if (isMainRV != mMainRVActive) {
+            if (rv != mCurrentRV) {
                 return;
             }
 
@@ -66,9 +58,7 @@
                 mAnimator.cancel();
             }
 
-            int current = - (isMainRV
-                    ? mMainRV.getCurrentScrollY()
-                    : mWorkRV.getCurrentScrollY());
+            int current = -mCurrentRV.getCurrentScrollY();
             moved(current);
             apply();
         }
@@ -79,15 +69,13 @@
     private View mDivider;
     private AllAppsRecyclerView mMainRV;
     private AllAppsRecyclerView mWorkRV;
+    private AllAppsRecyclerView mCurrentRV;
     private ViewGroup mParent;
-    private boolean mTopOnlyMode;
-    private boolean mHeaderHidden;
+    private boolean mTabsHidden;
+    private boolean mHeaderCollapsed;
     private int mMaxTranslation;
     private int mSnappedScrolledY;
     private int mTranslationY;
-    private int mMainScrolledY;
-    private int mWorkScrolledY;
-    private boolean mMainRVActive = true;
     private boolean mForwardToRecyclerView;
 
     public FloatingHeaderView(@NonNull Context context) {
@@ -106,15 +94,18 @@
         mPredictionRow = findViewById(R.id.header_content);
     }
 
-    public void setup(@NonNull AllAppsRecyclerView personalRV, @Nullable AllAppsRecyclerView workRV,
-                      int predictionRowHeight) {
-        mTopOnlyMode = workRV == null;
-        mTabLayout.setVisibility(mTopOnlyMode ? View.GONE : View.VISIBLE);
-        mPredictionRow.getLayoutParams().height = predictionRowHeight;
-        mMaxTranslation = predictionRowHeight;
-        mMainRV = setupRV(mMainRV, personalRV);
-        mWorkRV = setupRV(mWorkRV, workRV);
-        mParent = (ViewGroup) getRV().getParent();
+    public void setup(AllAppsContainerView.AdapterHolder[] mAH,
+            HashMap<ComponentKey, AppInfo> componentToAppMap, int numPredictedAppsPerRow) {
+        mTabsHidden = mAH[AllAppsContainerView.AdapterHolder.WORK].recyclerView == null;
+        mTabLayout.setVisibility(mTabsHidden ? View.GONE : View.VISIBLE);
+        mPredictionRow.setPadding(0, 0, 0, mTabsHidden ? getResources()
+                .getDimensionPixelSize(R.dimen.all_apps_prediction_row_divider_height) : 0);
+        mPredictionRow.setup(mAH[AllAppsContainerView.AdapterHolder.MAIN].adapter,
+                componentToAppMap, numPredictedAppsPerRow);
+        mMaxTranslation = mPredictionRow.getExpectedHeight();
+        mMainRV = setupRV(mMainRV, mAH[AllAppsContainerView.AdapterHolder.MAIN].recyclerView);
+        mWorkRV = setupRV(mWorkRV, mAH[AllAppsContainerView.AdapterHolder.WORK].recyclerView);
+        mParent = (ViewGroup) mMainRV.getParent();
         setMainActive(true);
         setupDivider();
     }
@@ -130,16 +121,16 @@
         Resources res = getResources();
         int verticalGap = res.getDimensionPixelSize(R.dimen.all_apps_divider_margin_vertical);
         int sideGap = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
-        mDivider.setPadding(sideGap, verticalGap,sideGap, mTopOnlyMode ? verticalGap : 0);
+        mDivider.setPadding(sideGap, verticalGap,sideGap, mTabsHidden ? verticalGap : 0);
         RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mDivider.getLayoutParams();
         lp.removeRule(RelativeLayout.ALIGN_BOTTOM);
-        lp.addRule(RelativeLayout.ALIGN_BOTTOM, mTopOnlyMode ? R.id.header_content : R.id.tabs);
+        lp.addRule(RelativeLayout.ALIGN_BOTTOM, mTabsHidden ? R.id.header_content : R.id.tabs);
         mDivider.setLayoutParams(lp);
     }
 
     public void setMainActive(boolean active) {
-        mMainRVActive = active;
-        mSnappedScrolledY = getCurrentScroll() - mMaxTranslation;
+        mCurrentRV = active ? mMainRV : mWorkRV;
+        mSnappedScrolledY = mCurrentRV.getCurrentScrollY() - mMaxTranslation;
         setExpanded(true);
     }
 
@@ -147,36 +138,29 @@
         return mPredictionRow;
     }
 
-    public ViewGroup getTabLayout() {
-        return mTabLayout;
-    }
-
     public View getDivider() {
         return mDivider;
     }
 
     public void reset() {
-        mMainScrolledY = 0;
-        mWorkScrolledY = 0;
         setExpanded(true);
     }
 
     private boolean canSnapAt(int currentScrollY) {
-        boolean snapOnlyOnTop = SHOW_PREDICTIONS_ONLY_ON_TOP || mTopOnlyMode;
-        return !snapOnlyOnTop || Math.abs(currentScrollY) <= mPredictionRow.getHeight();
+        return Math.abs(currentScrollY) <= mPredictionRow.getHeight();
     }
 
     private void moved(final int currentScrollY) {
-        if (mHeaderHidden) {
+        if (mHeaderCollapsed) {
             if (currentScrollY <= mSnappedScrolledY) {
                 if (canSnapAt(currentScrollY)) {
                     mSnappedScrolledY = currentScrollY;
                 }
             } else {
-                mHeaderHidden = false;
+                mHeaderCollapsed = false;
             }
             mTranslationY = currentScrollY;
-        } else if (!mHeaderHidden) {
+        } else if (!mHeaderCollapsed) {
             mTranslationY = currentScrollY - mSnappedScrolledY - mMaxTranslation;
 
             // update state vars
@@ -184,7 +168,7 @@
                 mTranslationY = 0;
                 mSnappedScrolledY = currentScrollY - mMaxTranslation;
             } else if (mTranslationY <= -mMaxTranslation) { // hide or stay hidden
-                mHeaderHidden = true;
+                mHeaderCollapsed = true;
                 mSnappedScrolledY = currentScrollY;
             }
         }
@@ -201,7 +185,7 @@
             mPredictionRow.setTranslationY(uncappedTranslationY);
         }
         mTabLayout.setTranslationY(mTranslationY);
-        mDivider.setTranslationY(mTopOnlyMode ? uncappedTranslationY : mTranslationY);
+        mDivider.setTranslationY(mTabsHidden ? uncappedTranslationY : mTranslationY);
         mClip.top = mMaxTranslation + mTranslationY;
         // clipping on a draw might cause additional redraw
         mMainRV.setClipBounds(mClip);
@@ -216,16 +200,14 @@
         mAnimator.addUpdateListener(this);
         mAnimator.setDuration(150);
         mAnimator.start();
-        mHeaderHidden = !expand;
-        mSnappedScrolledY = expand ? getCurrentScroll() - mMaxTranslation : getCurrentScroll();
+        mHeaderCollapsed = !expand;
+        mSnappedScrolledY = expand
+                ? mCurrentRV.getCurrentScrollY() - mMaxTranslation
+                : mCurrentRV.getCurrentScrollY();
     }
 
     public boolean isExpanded() {
-        return !mHeaderHidden;
-    }
-
-    private int getCurrentScroll() {
-        return mMainRVActive ? mMainScrolledY : mWorkScrolledY;
+        return !mHeaderCollapsed;
     }
 
     @Override
@@ -234,15 +216,11 @@
         apply();
     }
 
-    private AllAppsRecyclerView getRV() {
-        return mMainRVActive ? mMainRV : mWorkRV;
-    }
-
     @Override
     public boolean onInterceptTouchEvent(MotionEvent ev) {
         calcOffset(mTempOffset);
         ev.offsetLocation(mTempOffset.x, mTempOffset.y);
-        mForwardToRecyclerView = getRV().onInterceptTouchEvent(ev);
+        mForwardToRecyclerView = mCurrentRV.onInterceptTouchEvent(ev);
         ev.offsetLocation(-mTempOffset.x, -mTempOffset.y);
         return mForwardToRecyclerView || super.onInterceptTouchEvent(ev);
     }
@@ -254,7 +232,7 @@
             calcOffset(mTempOffset);
             event.offsetLocation(mTempOffset.x, mTempOffset.y);
             try {
-                return getRV().onTouchEvent(event);
+                return mCurrentRV.onTouchEvent(event);
             } finally {
                 event.offsetLocation(-mTempOffset.x, -mTempOffset.y);
             }
@@ -264,8 +242,8 @@
     }
 
     private void calcOffset(Point p) {
-        p.x = getLeft() - getRV().getLeft() - mParent.getLeft();
-        p.y = getTop() - getRV().getTop() - mParent.getTop();
+        p.x = getLeft() - mCurrentRV.getLeft() - mParent.getLeft();
+        p.y = getTop() - mCurrentRV.getTop() - mParent.getTop();
     }
 
 }
diff --git a/src/com/android/launcher3/allapps/PredictionRowView.java b/src/com/android/launcher3/allapps/PredictionRowView.java
index ea91770..30c30c8 100644
--- a/src/com/android/launcher3/allapps/PredictionRowView.java
+++ b/src/com/android/launcher3/allapps/PredictionRowView.java
@@ -26,6 +26,7 @@
 
 import com.android.launcher3.AppInfo;
 import com.android.launcher3.BubbleTextView;
+import com.android.launcher3.Launcher;
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.util.ComponentKey;
 import com.android.launcher3.util.ComponentKeyMapper;
@@ -57,11 +58,28 @@
         setOrientation(LinearLayout.HORIZONTAL);
     }
 
-    public void setup(AllAppsGridAdapter adapter,
-            HashMap<ComponentKey, AppInfo> componentToAppMap, int numPredictedAppsPerRow) {
+    public void setup(AllAppsGridAdapter adapter, HashMap<ComponentKey, AppInfo> componentToAppMap,
+                      int numPredictedAppsPerRow) {
         mAdapter = adapter;
         mComponentToAppMap = componentToAppMap;
         mNumPredictedAppsPerRow = numPredictedAppsPerRow;
+        setVisibility(mPredictedAppComponents.isEmpty() ? View.GONE : View.VISIBLE);
+    }
+
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(getExpectedHeight(),
+                MeasureSpec.EXACTLY));
+    }
+
+    public int getExpectedHeight() {
+        int height = 0;
+        if (!mPredictedAppComponents.isEmpty()) {
+            height += Launcher.getLauncher(getContext())
+                    .getDeviceProfile().allAppsCellHeightPx;
+            height += getPaddingTop() + getPaddingBottom();
+        }
+        return height;
     }
 
     /**