[UniversalSearchInput] Support search box dynamic height adjustment in
ActivityAllAppsContainerView

Previously, ActivityAllAppsContainerView assumed a static height for the
search box and relied only on FloatingHeaderView's height calculation.
With multi-line search bar, we need to add to the FloatingHeaderView's
mMaxTranslation to update the clipping position for the recycler views
belowe

Bug: b/341795751
Flag: com.android.launcher3.multiline_search_bar
Test: http://recall/clips/32f64e77-3522-4873-bc7b-86d844912724
Change-Id: I67aab186e96d7bdecbba454f660d73f0f4408a98
diff --git a/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java b/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java
index 56a7fef..ad660d2 100644
--- a/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java
@@ -71,6 +71,7 @@
 import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
 import com.android.launcher3.DragSource;
 import com.android.launcher3.DropTarget.DragObject;
+import com.android.launcher3.Flags;
 import com.android.launcher3.Insettable;
 import com.android.launcher3.InsettableFrameLayout;
 import com.android.launcher3.R;
@@ -764,6 +765,16 @@
         }
     }
 
+    /**
+     * Force header height update with an offset. Used by {@link UniversalSearchInputView} to
+     * request {@link FloatingHeaderView} to update its maxTranslation for multiline search bar.
+     */
+    public void forceUpdateHeaderHeight(int offset) {
+        if (Flags.multilineSearchBar()) {
+            mHeader.updateSearchBarOffset(offset);
+        }
+    }
+
     protected void updateHeaderScroll(int scrolledOffset) {
         float prog1 = Utilities.boundToRange((float) scrolledOffset / mHeaderThreshold, 0f, 1f);
         int headerColor = getHeaderColor(prog1);
diff --git a/src/com/android/launcher3/allapps/FloatingHeaderView.java b/src/com/android/launcher3/allapps/FloatingHeaderView.java
index 92c589c..a2bd5dd 100644
--- a/src/com/android/launcher3/allapps/FloatingHeaderView.java
+++ b/src/com/android/launcher3/allapps/FloatingHeaderView.java
@@ -30,6 +30,7 @@
 import androidx.annotation.Nullable;
 import androidx.recyclerview.widget.RecyclerView;
 
+import com.android.launcher3.Flags;
 import com.android.launcher3.Insettable;
 import com.android.launcher3.R;
 import com.android.launcher3.allapps.ActivityAllAppsContainerView.AdapterHolder;
@@ -104,6 +105,8 @@
     private boolean mFloatingRowsCollapsed;
     // Total height of all current floating rows. Collapsed rows == 0 height.
     private int mFloatingRowsHeight;
+    // Offset of search bar. Adds to the floating view height when multi-line is supported.
+    private int mSearchBarOffset = 0;
 
     // This is initialized once during inflation and stays constant after that. Fixed views
     // cannot be added or removed dynamically.
@@ -198,6 +201,14 @@
         }
     }
 
+    /**
+     * Offset floating rows height by search bar
+     */
+    void updateSearchBarOffset(int offset) {
+        mSearchBarOffset = offset;
+        onHeightUpdated();
+    }
+
     @Override
     public void onPluginDisconnected(AllAppsRow plugin) {
         PluginHeaderRow row = mPluginRows.get(plugin);
@@ -258,9 +269,18 @@
         mTabLayout.setVisibility(mTabsHidden ? GONE : visibility);
     }
 
+    /** Returns whether search bar has multi-line support, and is currently in multi-line state. */
+    private boolean isSearchBarMultiline() {
+        return Flags.multilineSearchBar() && mSearchBarOffset > 0;
+    }
+
     private void updateExpectedHeight() {
         updateFloatingRowsHeight();
         mMaxTranslation = 0;
+        boolean shouldAddSearchBarHeight = isSearchBarMultiline() && !Flags.floatingSearchBar();
+        if (shouldAddSearchBarHeight) {
+            mMaxTranslation += mSearchBarOffset;
+        }
         if (mFloatingRowsCollapsed) {
             return;
         }
diff --git a/tests/multivalentTests/src/com/android/launcher3/allapps/FloatingHeaderViewTests.kt b/tests/multivalentTests/src/com/android/launcher3/allapps/FloatingHeaderViewTests.kt
new file mode 100644
index 0000000..ac2c553
--- /dev/null
+++ b/tests/multivalentTests/src/com/android/launcher3/allapps/FloatingHeaderViewTests.kt
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.launcher3.allapps
+
+import android.content.Context
+import android.platform.test.annotations.DisableFlags
+import android.platform.test.annotations.EnableFlags
+import android.platform.test.flag.junit.SetFlagsRule
+import androidx.test.core.app.ApplicationProvider.getApplicationContext
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.android.launcher3.Flags
+import com.android.launcher3.util.ActivityContextWrapper
+import com.google.common.truth.Truth.assertThat
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@RunWith(AndroidJUnit4::class)
+class FloatingHeaderViewTests {
+
+    @get:Rule val mSetFlagsRule = SetFlagsRule()
+
+    private lateinit var context: Context
+    private lateinit var vut: FloatingHeaderView
+
+    @Before
+    fun setUp() {
+        context = ActivityContextWrapper(getApplicationContext())
+        // TODO(b/352161553): Inflate FloatingHeaderView or R.layout.all_apps_content with proper
+        // FloatingHeaderView#setup
+        vut = FloatingHeaderView(context)
+        vut.onFinishInflate()
+    }
+
+    @Test
+    @DisableFlags(Flags.FLAG_FLOATING_SEARCH_BAR, Flags.FLAG_MULTILINE_SEARCH_BAR)
+    fun onHeightUpdated_whenNotMultiline_thenZeroHeight() {
+        vut.setFloatingRowsCollapsed(true)
+        val beforeHeight = vut.maxTranslation
+        vut.updateSearchBarOffset(HEADER_HEIGHT_OFFSET)
+
+        vut.onHeightUpdated()
+
+        assertThat(vut.maxTranslation).isEqualTo(beforeHeight)
+    }
+
+    @Test
+    @EnableFlags(Flags.FLAG_MULTILINE_SEARCH_BAR)
+    @DisableFlags(Flags.FLAG_FLOATING_SEARCH_BAR)
+    fun onHeightUpdated_whenMultiline_thenHeightIsOffset() {
+        vut.setFloatingRowsCollapsed(true)
+        vut.updateSearchBarOffset(HEADER_HEIGHT_OFFSET)
+
+        vut.onHeightUpdated()
+
+        assertThat(vut.maxTranslation).isEqualTo(HEADER_HEIGHT_OFFSET)
+    }
+
+    @Test
+    @DisableFlags(Flags.FLAG_MULTILINE_SEARCH_BAR)
+    @EnableFlags(Flags.FLAG_FLOATING_SEARCH_BAR)
+    fun onHeightUpdated_whenFloatingRowsShownAndNotMultiline_thenAddsOnlyFloatingRow() {
+        // Collapse floating rows and expand to trigger header height calculation
+        vut.setFloatingRowsCollapsed(true)
+        vut.setFloatingRowsCollapsed(false)
+        val defaultHeight = vut.maxTranslation
+        vut.updateSearchBarOffset(HEADER_HEIGHT_OFFSET)
+
+        vut.onHeightUpdated()
+
+        assertThat(vut.maxTranslation).isEqualTo(defaultHeight)
+    }
+
+    companion object {
+        private const val HEADER_HEIGHT_OFFSET = 50
+    }
+}