Moving getPagesToBindSynchronously to ModelCallbacks

Changes are part of go/launcher_SoR .

Bug: 301108526
Test: This is a no-op change, compiling should be the test
Change-Id: I5e57b73edfa23e9f12ea94c0f9c5005e2d8fccd2
diff --git a/src/com/android/launcher3/ModelCallbacks.kt b/src/com/android/launcher3/ModelCallbacks.kt
index 679f7d4..8304e96 100644
--- a/src/com/android/launcher3/ModelCallbacks.kt
+++ b/src/com/android/launcher3/ModelCallbacks.kt
@@ -8,12 +8,18 @@
 import com.android.launcher3.model.data.WorkspaceItemInfo
 import com.android.launcher3.popup.PopupContainerWithArrow
 import com.android.launcher3.util.ComponentKey
+import com.android.launcher3.util.IntArray as LIntArray
+import com.android.launcher3.util.IntSet as LIntSet
 import com.android.launcher3.util.PackageUserKey
 import com.android.launcher3.util.Preconditions
 import com.android.launcher3.widget.model.WidgetsListBaseEntry
 import java.util.function.Predicate
 
 class ModelCallbacks(private var launcher: Launcher) : BgDataModel.Callbacks {
+
+    var synchronouslyBoundPages = LIntSet()
+    var pagesToBindSynchronously = LIntSet()
+
     override fun preAddApps() {
         // If there's an undo snackbar, force it to complete to ensure empty screens are removed
         // before trying to add new items.
@@ -93,4 +99,40 @@
     override fun bindAllWidgets(allWidgets: List<WidgetsListBaseEntry?>?) {
         launcher.popupDataProvider.allWidgets = allWidgets
     }
+
+    /** Returns the ids of the workspaces to bind. */
+    override fun getPagesToBindSynchronously(orderedScreenIds: LIntArray): LIntSet {
+        // If workspace binding is still in progress, getCurrentPageScreenIds won't be
+        // accurate, and we should use mSynchronouslyBoundPages that's set during initial binding.
+        val visibleIds =
+            when {
+                !pagesToBindSynchronously.isEmpty -> pagesToBindSynchronously
+                !launcher.isWorkspaceLoading -> launcher.workspace.currentPageScreenIds
+                else -> synchronouslyBoundPages
+            }
+        // Launcher IntArray has the same name as Kotlin IntArray
+        val result = LIntSet()
+        if (visibleIds.isEmpty) {
+            return result
+        }
+        val actualIds = orderedScreenIds.clone()
+        val firstId = visibleIds.first()
+        val pairId = launcher.workspace.getScreenPair(firstId)
+        // Double check that actual screenIds contains the visibleId, as empty screens are hidden
+        // in single panel.
+        if (actualIds.contains(firstId)) {
+            result.add(firstId)
+            if (launcher.deviceProfile.isTwoPanels && actualIds.contains(pairId)) {
+                result.add(pairId)
+            }
+        } else if (
+            LauncherAppState.getIDP(launcher).supportedProfiles.any(DeviceProfile::isTwoPanels) &&
+                actualIds.contains(pairId)
+        ) {
+            // Add the right panel if left panel is hidden when switching display, due to empty
+            // pages being hidden in single panel.
+            result.add(pairId)
+        }
+        return result
+    }
 }