Fix jank regression from AllAppsRecyclerViewPoolTest

Unit test ag/28323761 delays the preInflationCount check after creating ActivityContext on main thread, thus causing jank regression. This CL is a forward fix.

Fix: 354560500
Flag: NONE - jank fix
Test: Presubmit
Change-Id: I0e91dd765f1805b98895ce90804ec187e50285b4
diff --git a/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPool.kt b/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPool.kt
index f895b30..f231b92 100644
--- a/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPool.kt
+++ b/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPool.kt
@@ -53,6 +53,10 @@
     fun preInflateAllAppsViewHolders(context: T) {
         val appsView = context.appsView ?: return
         val activeRv: RecyclerView = appsView.activeRecyclerView ?: return
+        val preInflateCount = getPreinflateCount(context)
+        if (preInflateCount <= 0) {
+            return
+        }
 
         // Create a separate context dedicated for all apps preinflation thread. The goal is to
         // create a separate AssetManager obj internally to avoid lock contention with
@@ -81,7 +85,12 @@
                 override fun getLayoutManager(): RecyclerView.LayoutManager? = null
             }
 
-        preInflateAllAppsViewHolders(adapter, BaseAllAppsAdapter.VIEW_TYPE_ICON, activeRv) {
+        preInflateAllAppsViewHolders(
+            adapter,
+            BaseAllAppsAdapter.VIEW_TYPE_ICON,
+            activeRv,
+            preInflateCount
+        ) {
             getPreinflateCount(context)
         }
     }
@@ -91,10 +100,10 @@
         adapter: RecyclerView.Adapter<*>,
         viewType: Int,
         parent: ViewGroup,
+        preInflationCount: Int,
         preInflationCountProvider: () -> Int
     ) {
-        val preinflationCount = preInflationCountProvider.invoke()
-        if (preinflationCount <= 0) {
+        if (preInflationCount <= 0) {
             return
         }
         mCancellableTask?.cancel()
@@ -103,7 +112,7 @@
             CancellableTask(
                 {
                     val list: ArrayList<ViewHolder> = ArrayList()
-                    for (i in 0 until preinflationCount) {
+                    for (i in 0 until preInflationCount) {
                         if (task?.canceled == true) {
                             break
                         }
@@ -114,8 +123,8 @@
                 MAIN_EXECUTOR,
                 { viewHolders ->
                     // Run preInflationCountProvider again as the needed VH might have changed
-                    val newPreinflationCount = preInflationCountProvider.invoke()
-                    for (i in 0 until minOf(viewHolders.size, newPreinflationCount)) {
+                    val newPreInflationCount = preInflationCountProvider.invoke()
+                    for (i in 0 until minOf(viewHolders.size, newPreInflationCount)) {
                         putRecycledView(viewHolders[i])
                     }
                 }
diff --git a/tests/multivalentTests/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPoolTest.kt b/tests/multivalentTests/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPoolTest.kt
index 8204313..3e6aae2 100644
--- a/tests/multivalentTests/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPoolTest.kt
+++ b/tests/multivalentTests/src/com/android/launcher3/recyclerview/AllAppsRecyclerViewPoolTest.kt
@@ -65,7 +65,7 @@
 
     @Test
     fun preinflate_success() {
-        underTest.preInflateAllAppsViewHolders(adapter, VIEW_TYPE, parent) { 10 }
+        underTest.preInflateAllAppsViewHolders(adapter, VIEW_TYPE, parent, 10) { 10 }
 
         awaitTasksCompleted()
         assertThat(underTest.getRecycledViewCount(VIEW_TYPE)).isEqualTo(10)
@@ -73,7 +73,7 @@
 
     @Test
     fun preinflate_not_triggered() {
-        underTest.preInflateAllAppsViewHolders(adapter, VIEW_TYPE, parent) { 0 }
+        underTest.preInflateAllAppsViewHolders(adapter, VIEW_TYPE, parent, 0) { 0 }
 
         awaitTasksCompleted()
         assertThat(underTest.getRecycledViewCount(VIEW_TYPE)).isEqualTo(0)
@@ -81,7 +81,7 @@
 
     @Test
     fun preinflate_cancel_before_runOnMainThread() {
-        underTest.preInflateAllAppsViewHolders(adapter, VIEW_TYPE, parent) { 10 }
+        underTest.preInflateAllAppsViewHolders(adapter, VIEW_TYPE, parent, 10) { 10 }
         assertThat(underTest.mCancellableTask!!.canceled).isFalse()
 
         underTest.clear()
@@ -94,7 +94,7 @@
 
     @Test
     fun preinflate_cancel_after_run() {
-        underTest.preInflateAllAppsViewHolders(adapter, VIEW_TYPE, parent) { 10 }
+        underTest.preInflateAllAppsViewHolders(adapter, VIEW_TYPE, parent, 10) { 10 }
         assertThat(underTest.mCancellableTask!!.canceled).isFalse()
         awaitTasksCompleted()