Fix All Apps header protection and spacing again

Redone, again. This time, many comments added.

Compared to I586f7332, this primarily results in some fixes for the
floating header row(s) (AiAi prediction row) if present.

Change-Id: Ib0f383fb89a6847fccbcc96d13b051983d76f0c5
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 786088e..c6daa26 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -101,7 +101,12 @@
     <dimen name="all_apps_search_bar_field_height">48dp</dimen>
     <!-- all_apps_search_bar_field_height / 2 -->
     <dimen name="all_apps_search_bar_content_overlap">24dp</dimen>
-    <dimen name="all_apps_search_bar_bottom_padding">30dp</dimen>
+    <!-- affects padding above apps lists when tabs and floating header rows are not visible -->
+    <!-- (always affects padding above search results) -->
+    <dimen name="all_apps_search_bar_bottom_padding">24dp</dimen>
+    <!-- margin adjustment for layout relative to bottom of search container -->
+    <!-- if the search container is obscuring things, try adjusting this -->
+    <dimen name="all_apps_search_bar_bottom_adjustment">-6dp</dimen>
     <dimen name="all_apps_empty_search_message_top_offset">40dp</dimen>
     <dimen name="all_apps_header_pill_height">48dp</dimen>
     <dimen name="all_apps_header_pill_corner_radius">12dp</dimen>
@@ -111,7 +116,10 @@
     <dimen name="all_apps_header_top_padding">36dp</dimen>
     <!-- Additional top padding to add when Floating Searchbar is enabled. -->
     <dimen name="all_apps_additional_top_padding_floating_search">16dp</dimen>
-    <dimen name="all_apps_header_bottom_padding">14dp</dimen>
+    <!-- influences header protection drawn height below personal/work tabs -->
+    <!-- if app icons are appearing above the protection rectangle, or if they are shifted below
+         it and cropped at the top, try adjusting this -->
+    <dimen name="all_apps_header_bottom_padding">10dp</dimen>
     <dimen name="all_apps_header_top_adjustment">6dp</dimen>
     <dimen name="all_apps_header_bottom_adjustment">4dp</dimen>
     <dimen name="all_apps_work_profile_tab_footer_top_padding">16dp</dimen>
diff --git a/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java b/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java
index 26a447f..e3623a2 100644
--- a/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java
@@ -641,13 +641,22 @@
                 tabsHidden);
 
         int padding = mHeader.getMaxTranslation();
-        mAH.forEach(adapterHolder -> {
-            adapterHolder.mPadding.top = padding;
+        for (int i = 0; i < mAH.size(); i++) {
+            final AdapterHolder adapterHolder = mAH.get(i);
+            // Search and other adapters need to be handled a bit differently; otherwise, when
+            // when leaving search, the All Apps view may be noticeably shifted downward because
+            // its padding was unnecessarily impacted, and never restored, upon entering search.
+            if (i != AdapterHolder.SEARCH && !tabsHidden && mHeader.getFloatingRowsHeight() == 0) {
+                // Only the Search adapter needs padding when there are tabs but no floating rows.
+                adapterHolder.mPadding.top = 0;
+            } else {
+                adapterHolder.mPadding.top = padding;
+            }
             adapterHolder.applyPadding();
             if (adapterHolder.mRecyclerView != null) {
                 adapterHolder.mRecyclerView.scrollToTop();
             }
-        });
+        }
 
         removeCustomRules(mHeader);
         if (!isSearchSupported()) {
@@ -711,13 +720,15 @@
         }
 
         RelativeLayout.LayoutParams layoutParams = (LayoutParams) v.getLayoutParams();
-        layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.search_container_all_apps);
+        layoutParams.addRule(RelativeLayout.BELOW, R.id.search_container_all_apps);
 
         int topMargin = getContext().getResources().getDimensionPixelSize(
-                R.dimen.all_apps_header_top_margin);
+                R.dimen.all_apps_search_bar_bottom_adjustment);
         if (includeTabsMargin) {
             topMargin += getContext().getResources().getDimensionPixelSize(
-                    R.dimen.all_apps_header_pill_height);
+                    R.dimen.all_apps_header_pill_height)
+                    + getContext().getResources().getDimensionPixelSize(
+                    R.dimen.all_apps_tabs_margin_top);
         }
         layoutParams.topMargin = topMargin;
     }
@@ -754,6 +765,7 @@
         layoutParams.removeRule(RelativeLayout.ABOVE);
         layoutParams.removeRule(RelativeLayout.ALIGN_TOP);
         layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
+        layoutParams.removeRule(RelativeLayout.BELOW);
     }
 
     protected BaseAllAppsAdapter<T> createAdapter(AlphabeticalAppsList<T> appsList) {
diff --git a/src/com/android/launcher3/allapps/FloatingHeaderView.java b/src/com/android/launcher3/allapps/FloatingHeaderView.java
index 330d13d..257f104 100644
--- a/src/com/android/launcher3/allapps/FloatingHeaderView.java
+++ b/src/com/android/launcher3/allapps/FloatingHeaderView.java
@@ -225,7 +225,6 @@
         for (FloatingHeaderRow row : mAllRows) {
             row.setup(this, mAllRows, tabsHidden);
         }
-        updateExpectedHeight();
 
         mTabsHidden = tabsHidden;
         maybeSetTabVisibility(VISIBLE);
@@ -251,6 +250,8 @@
                 : rvType == AdapterHolder.WORK ? mWorkRV : mSearchRV;
         mCurrentRV.addOnScrollListener(mOnScrollListener);
         maybeSetTabVisibility(rvType == AdapterHolder.SEARCH ? GONE : VISIBLE);
+
+        updateExpectedHeight();
     }
 
     /** Update tab visibility to the given state, only if tabs are active (work profile exists). */
@@ -265,10 +266,7 @@
             return;
         }
         mMaxTranslation += mFloatingRowsHeight;
-        if (!mTabsHidden) {
-            mMaxTranslation += mTabsAdditionalPaddingBottom
-                    + getResources().getDimensionPixelSize(R.dimen.all_apps_tabs_margin_top);
-        }
+        // No need for mMaxTranslation to be any taller now that we align below the header.
     }
 
     int getMaxTranslation() {