Moving bindAppsAdded and setIsFirstPagePinnedItemEnabled to ModelCallbacks

Changes are part of go/launcher_SoR .

Flag: NA
Bug: 301108526
Test: This is a no-op change, compiling should be the test
Change-Id: I79a651c9d57e443037e330728540616b0d01434a
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 22870bc..da86336 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -27,7 +27,6 @@
 import static com.android.launcher3.AbstractFloatingView.TYPE_ICON_SURFACE;
 import static com.android.launcher3.AbstractFloatingView.TYPE_REBIND_SAFE;
 import static com.android.launcher3.AbstractFloatingView.getTopOpenViewWithType;
-import static com.android.launcher3.BuildConfig.QSB_ON_FIRST_SCREEN;
 import static com.android.launcher3.LauncherAnimUtils.HOTSEAT_SCALE_PROPERTY_FACTORY;
 import static com.android.launcher3.LauncherAnimUtils.SCALE_INDEX_WIDGET_TRANSITION;
 import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
@@ -65,11 +64,9 @@
 import static com.android.launcher3.LauncherState.NO_SCALE;
 import static com.android.launcher3.LauncherState.SPRING_LOADED;
 import static com.android.launcher3.Utilities.postAsyncCallback;
-import static com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID;
 import static com.android.launcher3.config.FeatureFlags.ENABLE_SMARTSPACE_REMOVAL;
 import static com.android.launcher3.config.FeatureFlags.FOLDABLE_SINGLE_PAGE;
 import static com.android.launcher3.config.FeatureFlags.MULTI_SELECT_EDIT_MODE;
-import static com.android.launcher3.config.FeatureFlags.shouldShowFirstPageWidget;
 import static com.android.launcher3.logging.StatsLogManager.EventEnum;
 import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_BACKGROUND;
 import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_HOME;
@@ -403,12 +400,8 @@
     // session on the server side.
     protected InstanceId mAllAppsSessionLogId;
     private LauncherState mPrevLauncherState;
-
-    private StringCache mStringCache;
     private StartupLatencyLogger mStartupLatencyLogger;
     private CellPosMapper mCellPosMapper = CellPosMapper.DEFAULT;
-    private boolean mIsFirstPagePinnedItemEnabled = QSB_ON_FIRST_SCREEN
-            && !ENABLE_SMARTSPACE_REMOVAL.get();
 
     private final CannedAnimationCoordinator mAnimationCoordinator =
             new CannedAnimationCoordinator(this);
@@ -2126,32 +2119,12 @@
 
     @Override
     public void setIsFirstPagePinnedItemEnabled(boolean isFirstPagePinnedItemEnabled) {
-        mIsFirstPagePinnedItemEnabled = isFirstPagePinnedItemEnabled;
-        mWorkspace.bindAndInitFirstWorkspaceScreen();
+        mModelCallbacks.setIsFirstPagePinnedItemEnabled(isFirstPagePinnedItemEnabled);
     }
 
     @Override
     public void bindScreens(IntArray orderedScreenIds) {
-        mWorkspace.mPageIndicator.setAreScreensBinding(true, mDeviceProfile.isTwoPanels);
-        int firstScreenPosition = 0;
-        if ((FeatureFlags.QSB_ON_FIRST_SCREEN
-                && mIsFirstPagePinnedItemEnabled
-                && !shouldShowFirstPageWidget())
-                && orderedScreenIds.indexOf(FIRST_SCREEN_ID) != firstScreenPosition) {
-            orderedScreenIds.removeValue(FIRST_SCREEN_ID);
-            orderedScreenIds.add(firstScreenPosition, FIRST_SCREEN_ID);
-        } else if (((!FeatureFlags.QSB_ON_FIRST_SCREEN && !mIsFirstPagePinnedItemEnabled)
-                || shouldShowFirstPageWidget())
-                && orderedScreenIds.isEmpty()) {
-            // If there are no screens, we need to have an empty screen
-            mWorkspace.addExtraEmptyScreens();
-        }
-        bindAddScreens(orderedScreenIds);
-
-        // After we have added all the screens, if the wallpaper was locked to the default state,
-        // then notify to indicate that it can be released and a proper wallpaper offset can be
-        // computed before the next layout
-        mWorkspace.unlockWallpaperFromDefaultPageOnNextLayout();
+        mModelCallbacks.bindScreens(orderedScreenIds);
     }
 
     /**
@@ -2172,35 +2145,6 @@
         return screenIds.getArray();
     }
 
-    private void bindAddScreens(IntArray orderedScreenIds) {
-
-        if (mDeviceProfile.isTwoPanels) {
-            if (FOLDABLE_SINGLE_PAGE.get()) {
-                orderedScreenIds = filterTwoPanelScreenIds(orderedScreenIds);
-            } else {
-                // Some empty pages might have been removed while the phone was in a single panel
-                // mode, so we want to add those empty pages back.
-                IntSet screenIds = IntSet.wrap(orderedScreenIds);
-                orderedScreenIds.forEach(
-                        screenId -> screenIds.add(mWorkspace.getScreenPair(screenId)));
-                orderedScreenIds = screenIds.getArray();
-            }
-        }
-
-        int count = orderedScreenIds.size();
-        for (int i = 0; i < count; i++) {
-            int screenId = orderedScreenIds.get(i);
-            if (FeatureFlags.QSB_ON_FIRST_SCREEN
-                    && mIsFirstPagePinnedItemEnabled
-                    && !shouldShowFirstPageWidget()
-                    && screenId == FIRST_SCREEN_ID) {
-                // No need to bind the first screen, as its always bound.
-                continue;
-            }
-            mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId);
-        }
-    }
-
     @Override
     public void preAddApps() {
         mModelCallbacks.preAddApps();
@@ -2209,25 +2153,7 @@
     @Override
     public void bindAppsAdded(IntArray newScreens, ArrayList<ItemInfo> addNotAnimated,
             ArrayList<ItemInfo> addAnimated) {
-        // Add the new screens
-        if (newScreens != null) {
-            // newScreens can contain an empty right panel that is already bound, but not known
-            // by BgDataModel.
-            newScreens.removeAllValues(mWorkspace.mScreenOrder);
-            bindAddScreens(newScreens);
-        }
-
-        // We add the items without animation on non-visible pages, and with
-        // animations on the new page (which we will try and snap to).
-        if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
-            bindItems(addNotAnimated, false);
-        }
-        if (addAnimated != null && !addAnimated.isEmpty()) {
-            bindItems(addAnimated, true);
-        }
-
-        // Remove the extra empty screen
-        mWorkspace.removeExtraEmptyScreen(false);
+        mModelCallbacks.bindAppsAdded(newScreens, addNotAnimated, addAnimated);
     }
 
     /**
@@ -2877,8 +2803,7 @@
 
     @Override
     public void bindStringCache(StringCache cache) {
-        mStringCache = cache;
-        mAppsView.updateWorkUI();
+        mModelCallbacks.bindStringCache(cache);
     }
 
     /**
@@ -3314,7 +3239,7 @@
 
     @Override
     public StringCache getStringCache() {
-        return mStringCache;
+        return mModelCallbacks.getStringCache();
     }
 
     /**
@@ -3349,7 +3274,7 @@
     }
 
     public boolean getIsFirstPagePinnedItemEnabled() {
-        return mIsFirstPagePinnedItemEnabled;
+        return mModelCallbacks.getIsFirstPagePinnedItemEnabled();
     }
 
     /**
diff --git a/src/com/android/launcher3/ModelCallbacks.kt b/src/com/android/launcher3/ModelCallbacks.kt
index 2c3e74d..bcd30d3 100644
--- a/src/com/android/launcher3/ModelCallbacks.kt
+++ b/src/com/android/launcher3/ModelCallbacks.kt
@@ -1,7 +1,11 @@
 package com.android.launcher3
 
 import androidx.annotation.UiThread
+import com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID
+import com.android.launcher3.config.FeatureFlags
+import com.android.launcher3.config.FeatureFlags.shouldShowFirstPageWidget
 import com.android.launcher3.model.BgDataModel
+import com.android.launcher3.model.StringCache
 import com.android.launcher3.model.data.AppInfo
 import com.android.launcher3.model.data.ItemInfo
 import com.android.launcher3.model.data.LauncherAppWidgetInfo
@@ -9,7 +13,9 @@
 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.IntArray
 import com.android.launcher3.util.IntSet as LIntSet
+import com.android.launcher3.util.IntSet
 import com.android.launcher3.util.PackageUserKey
 import com.android.launcher3.util.Preconditions
 import com.android.launcher3.widget.PendingAddWidgetInfo
@@ -21,6 +27,11 @@
     var synchronouslyBoundPages = LIntSet()
     var pagesToBindSynchronously = LIntSet()
 
+    var isFirstPagePinnedItemEnabled =
+        (BuildConfig.QSB_ON_FIRST_SCREEN && !FeatureFlags.ENABLE_SMARTSPACE_REMOVAL.get())
+
+    var stringCache: StringCache? = null
+
     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.
@@ -166,4 +177,116 @@
             info.spanY
         )
     }
+
+    override fun bindScreens(orderedScreenIds: IntArray) {
+        launcher.workspace.pageIndicator.setAreScreensBinding(
+            true,
+            launcher.deviceProfile.isTwoPanels
+        )
+        val firstScreenPosition = 0
+        if (
+            (FeatureFlags.QSB_ON_FIRST_SCREEN &&
+                isFirstPagePinnedItemEnabled &&
+                !shouldShowFirstPageWidget()) &&
+                orderedScreenIds.indexOf(FIRST_SCREEN_ID) != firstScreenPosition
+        ) {
+            orderedScreenIds.removeValue(FIRST_SCREEN_ID)
+            orderedScreenIds.add(firstScreenPosition, FIRST_SCREEN_ID)
+        } else if (
+            (!FeatureFlags.QSB_ON_FIRST_SCREEN && !isFirstPagePinnedItemEnabled ||
+                shouldShowFirstPageWidget()) && orderedScreenIds.isEmpty
+        ) {
+            // If there are no screens, we need to have an empty screen
+            launcher.workspace.addExtraEmptyScreens()
+        }
+        bindAddScreens(orderedScreenIds)
+
+        // After we have added all the screens, if the wallpaper was locked to the default state,
+        // then notify to indicate that it can be released and a proper wallpaper offset can be
+        // computed before the next layout
+        launcher.workspace.unlockWallpaperFromDefaultPageOnNextLayout()
+    }
+
+    override fun bindAppsAdded(
+        newScreens: IntArray?,
+        addNotAnimated: java.util.ArrayList<ItemInfo?>?,
+        addAnimated: java.util.ArrayList<ItemInfo?>?
+    ) {
+        // Add the new screens
+        if (newScreens != null) {
+            // newScreens can contain an empty right panel that is already bound, but not known
+            // by BgDataModel.
+            newScreens.removeAllValues(launcher.workspace.mScreenOrder)
+            bindAddScreens(newScreens)
+        }
+
+        // We add the items without animation on non-visible pages, and with
+        // animations on the new page (which we will try and snap to).
+        if (!addNotAnimated.isNullOrEmpty()) {
+            launcher.bindItems(addNotAnimated, false)
+        }
+        if (!addAnimated.isNullOrEmpty()) {
+            launcher.bindItems(addAnimated, true)
+        }
+
+        // Remove the extra empty screen
+        launcher.workspace.removeExtraEmptyScreen(false)
+    }
+
+    private fun bindAddScreens(orderedScreenIdsArg: IntArray) {
+        var orderedScreenIds = orderedScreenIdsArg
+        if (launcher.deviceProfile.isTwoPanels) {
+            if (FeatureFlags.FOLDABLE_SINGLE_PAGE.get()) {
+                orderedScreenIds = filterTwoPanelScreenIds(orderedScreenIds)
+            } else {
+                // Some empty pages might have been removed while the phone was in a single panel
+                // mode, so we want to add those empty pages back.
+                val screenIds = IntSet.wrap(orderedScreenIds)
+                orderedScreenIds.forEach { screenId: Int ->
+                    screenIds.add(launcher.workspace.getScreenPair(screenId))
+                }
+                orderedScreenIds = screenIds.array
+            }
+        }
+        orderedScreenIds
+            .filterNot { screenId ->
+                FeatureFlags.QSB_ON_FIRST_SCREEN &&
+                    isFirstPagePinnedItemEnabled &&
+                    !FeatureFlags.shouldShowFirstPageWidget() &&
+                    screenId == WorkspaceLayoutManager.FIRST_SCREEN_ID
+            }
+            .forEach { screenId ->
+                launcher.workspace.insertNewWorkspaceScreenBeforeEmptyScreen(screenId)
+            }
+    }
+
+    /**
+     * Remove odd number because they are already included when isTwoPanels and add the pair screen
+     * if not present.
+     */
+    private fun filterTwoPanelScreenIds(orderedScreenIds: IntArray): IntArray {
+        val screenIds = IntSet.wrap(orderedScreenIds)
+        orderedScreenIds
+            .filter { screenId -> screenId % 2 == 1 }
+            .forEach { screenId ->
+                screenIds.remove(screenId)
+                // In case the pair is not added, add it
+                if (!launcher.workspace.containsScreenId(screenId - 1)) {
+                    screenIds.add(screenId - 1)
+                }
+            }
+        return screenIds.array
+    }
+
+    override fun setIsFirstPagePinnedItemEnabled(isFirstPagePinnedItemEnabled: Boolean) {
+        this.isFirstPagePinnedItemEnabled = isFirstPagePinnedItemEnabled
+        launcher.workspace.bindAndInitFirstWorkspaceScreen()
+    }
+
+    override fun bindStringCache(cache: StringCache) {
+        stringCache = cache
+        launcher.appsView.updateWorkUI()
+    }
+
+    fun getIsFirstPagePinnedItemEnabled(): Boolean = isFirstPagePinnedItemEnabled
 }