Merge "Use the number of shown hotseat icons instead of database items to store into the DB" into main
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 37b3e05..a351947 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -780,10 +780,10 @@
         if (FOLDABLE_SINGLE_PAGE.get() && mDeviceProfile.isTwoPanels) {
             mCellPosMapper = new TwoPanelCellPosMapper(mDeviceProfile.inv.numColumns);
         } else {
-            mCellPosMapper = CellPosMapper.DEFAULT;
+            mCellPosMapper = new CellPosMapper(mDeviceProfile.isVerticalBarLayout(),
+                    mDeviceProfile.numShownHotseatIcons);
         }
-        mModelWriter = mModel.getWriter(getDeviceProfile().isVerticalBarLayout(), true,
-                mCellPosMapper, this);
+        mModelWriter = mModel.getWriter(true, mCellPosMapper, this);
         return true;
     }
 
diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java
index 551735e..c81db63 100644
--- a/src/com/android/launcher3/LauncherModel.java
+++ b/src/com/android/launcher3/LauncherModel.java
@@ -180,10 +180,10 @@
     }
 
     @NonNull
-    public ModelWriter getWriter(final boolean hasVerticalHotseat, final boolean verifyChanges,
-            CellPosMapper cellPosMapper, @Nullable final Callbacks owner) {
-        return new ModelWriter(mApp.getContext(), this, mBgDataModel,
-                hasVerticalHotseat, verifyChanges, cellPosMapper, owner);
+    public ModelWriter getWriter(final boolean verifyChanges, CellPosMapper cellPosMapper,
+            @Nullable final Callbacks owner) {
+        return new ModelWriter(mApp.getContext(), this, mBgDataModel, verifyChanges, cellPosMapper,
+                owner);
     }
 
     @Override
diff --git a/src/com/android/launcher3/celllayout/CellPosMapper.java b/src/com/android/launcher3/celllayout/CellPosMapper.java
index 1891696..b133a1d 100644
--- a/src/com/android/launcher3/celllayout/CellPosMapper.java
+++ b/src/com/android/launcher3/celllayout/CellPosMapper.java
@@ -17,6 +17,7 @@
 
 import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP;
 
+import com.android.launcher3.LauncherSettings.Favorites;
 import com.android.launcher3.model.data.ItemInfo;
 
 import java.util.Objects;
@@ -26,9 +27,14 @@
  */
 public class CellPosMapper {
 
-    public static final CellPosMapper DEFAULT = new CellPosMapper();
+    public static final CellPosMapper DEFAULT = new CellPosMapper(false, -1);
+    private final boolean mHasVerticalHotseat;
+    private final int mNumOfHotseat;
 
-    private CellPosMapper() { }
+    public CellPosMapper(boolean hasVerticalHotseat, int numOfHotseat) {
+        mHasVerticalHotseat = hasVerticalHotseat;
+        mNumOfHotseat = numOfHotseat;
+    }
 
     /**
      * Maps the position in model to the position in view
@@ -42,17 +48,22 @@
      */
     public CellPos mapPresenterToModel(int presenterX, int presenterY, int presenterScreen,
             int container) {
+        if (container == Favorites.CONTAINER_HOTSEAT) {
+            presenterScreen = mHasVerticalHotseat
+                    ? mNumOfHotseat - presenterY - 1 : presenterX;
+        }
         return new CellPos(presenterX, presenterY, presenterScreen);
     }
 
     /**
      * Cell mapper which maps two panels into a single layout
      */
-    public static class TwoPanelCellPosMapper extends CellPosMapper  {
+    public static class TwoPanelCellPosMapper extends CellPosMapper {
 
         private final int mColumnCount;
 
         public TwoPanelCellPosMapper(int columnCount) {
+            super(false, -1);
             mColumnCount = columnCount;
         }
 
@@ -103,5 +114,13 @@
         public int hashCode() {
             return Objects.hash(cellX, cellY, screenId);
         }
+
+        @Override
+        public String toString() {
+            return "CellPos{"
+                    + "cellX=" + cellX
+                    + ", cellY=" + cellY
+                    + ", screenId=" + screenId + '}';
+        }
     }
 }
diff --git a/src/com/android/launcher3/folder/LauncherDelegate.java b/src/com/android/launcher3/folder/LauncherDelegate.java
index c06a0f3..66c9109 100644
--- a/src/com/android/launcher3/folder/LauncherDelegate.java
+++ b/src/com/android/launcher3/folder/LauncherDelegate.java
@@ -28,7 +28,6 @@
 import com.android.launcher3.DropTarget;
 import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherAppState;
-import com.android.launcher3.celllayout.CellPosMapper;
 import com.android.launcher3.dragndrop.DragOptions;
 import com.android.launcher3.logging.InstanceId;
 import com.android.launcher3.logging.StatsLogManager.StatsLogger;
@@ -177,8 +176,8 @@
         @Override
         ModelWriter getModelWriter() {
             if (mWriter == null) {
-                mWriter = LauncherAppState.getInstance((Context) mContext).getModel()
-                        .getWriter(false, false, CellPosMapper.DEFAULT, null);
+                mWriter = LauncherAppState.getInstance((Context) mContext).getModel().getWriter(
+                        false, mContext.getCellPosMapper(), null);
             }
             return mWriter;
         }
diff --git a/src/com/android/launcher3/model/BaseModelUpdateTask.java b/src/com/android/launcher3/model/BaseModelUpdateTask.java
index 97f540e..529c30a 100644
--- a/src/com/android/launcher3/model/BaseModelUpdateTask.java
+++ b/src/com/android/launcher3/model/BaseModelUpdateTask.java
@@ -101,8 +101,7 @@
     public ModelWriter getModelWriter() {
         // Updates from model task, do not deal with icon position in hotseat. Also no need to
         // verify changes as the ModelTasks always push the changes to callbacks
-        return mModel.getWriter(false /* hasVerticalHotseat */, false /* verifyChanges */,
-                CellPosMapper.DEFAULT, null);
+        return mModel.getWriter(false /* verifyChanges */, CellPosMapper.DEFAULT, null);
     }
 
     public void bindUpdatedWorkspaceItems(@NonNull final List<WorkspaceItemInfo> allUpdates) {
diff --git a/src/com/android/launcher3/model/ModelWriter.java b/src/com/android/launcher3/model/ModelWriter.java
index 2358a9f..55093a3 100644
--- a/src/com/android/launcher3/model/ModelWriter.java
+++ b/src/com/android/launcher3/model/ModelWriter.java
@@ -28,7 +28,6 @@
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
-import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.LauncherModel;
 import com.android.launcher3.LauncherModel.CallbackTask;
 import com.android.launcher3.LauncherSettings.Favorites;
@@ -74,7 +73,6 @@
     @Nullable
     private final Callbacks mOwner;
 
-    private final boolean mHasVerticalHotseat;
     private final boolean mVerifyChanges;
 
     // Keep track of delete operations that occur when an Undo option is present; we may not commit.
@@ -83,12 +81,10 @@
     private final CellPosMapper mCellPosMapper;
 
     public ModelWriter(Context context, LauncherModel model, BgDataModel dataModel,
-            boolean hasVerticalHotseat, boolean verifyChanges, CellPosMapper cellPosMapper,
-            @Nullable Callbacks owner) {
+            boolean verifyChanges, CellPosMapper cellPosMapper, @Nullable Callbacks owner) {
         mContext = context;
         mModel = model;
         mBgDataModel = dataModel;
-        mHasVerticalHotseat = hasVerticalHotseat;
         mVerifyChanges = verifyChanges;
         mOwner = owner;
         mCellPosMapper = cellPosMapper;
@@ -102,14 +98,8 @@
         item.container = container;
         item.cellX = modelPos.cellX;
         item.cellY = modelPos.cellY;
-        // We store hotseat items in canonical form which is this orientation invariant position
-        // in the hotseat
-        if (container == Favorites.CONTAINER_HOTSEAT) {
-            item.screenId = mHasVerticalHotseat
-                    ? LauncherAppState.getIDP(mContext).numDatabaseHotseatIcons - cellY - 1 : cellX;
-        } else {
-            item.screenId = modelPos.screenId;
-        }
+        item.screenId = modelPos.screenId;
+
     }
 
     /**
diff --git a/src/com/android/launcher3/views/ActivityContext.java b/src/com/android/launcher3/views/ActivityContext.java
index 3921e12..ca7abc2 100644
--- a/src/com/android/launcher3/views/ActivityContext.java
+++ b/src/com/android/launcher3/views/ActivityContext.java
@@ -447,7 +447,8 @@
     }
 
     default CellPosMapper getCellPosMapper() {
-        return CellPosMapper.DEFAULT;
+        DeviceProfile dp = getDeviceProfile();
+        return new CellPosMapper(dp.isVerticalBarLayout(), dp.numShownHotseatIcons);
     }
 
     /** Whether bubbles are enabled. */
diff --git a/tests/src/com/android/launcher3/celllayout/CellPosMapperTest.java b/tests/src/com/android/launcher3/celllayout/CellPosMapperTest.java
index 29efb18..b8bf362 100644
--- a/tests/src/com/android/launcher3/celllayout/CellPosMapperTest.java
+++ b/tests/src/com/android/launcher3/celllayout/CellPosMapperTest.java
@@ -39,44 +39,46 @@
 
     @Test
     public void testMapModelToPresenter_default() {
-        assertThat(CellPosMapper.DEFAULT.mapModelToPresenter(
+        CellPosMapper mapper = CellPosMapper.DEFAULT;
+        assertThat(mapper.mapModelToPresenter(
                 createInfo(0, 0, 0, CONTAINER_DESKTOP))).isEqualTo(new CellPos(0, 0, 0));
-        assertThat(CellPosMapper.DEFAULT.mapModelToPresenter(
+        assertThat(mapper.mapModelToPresenter(
                 createInfo(0, 0, 1, CONTAINER_DESKTOP))).isEqualTo(new CellPos(0, 0, 1));
-        assertThat(CellPosMapper.DEFAULT.mapModelToPresenter(
+        assertThat(mapper.mapModelToPresenter(
                 createInfo(5, 0, 1, CONTAINER_DESKTOP))).isEqualTo(new CellPos(5, 0, 1));
-        assertThat(CellPosMapper.DEFAULT.mapModelToPresenter(
+        assertThat(mapper.mapModelToPresenter(
                 createInfo(5, 0, 0, CONTAINER_DESKTOP))).isEqualTo(new CellPos(5, 0, 0));
 
-        assertThat(CellPosMapper.DEFAULT.mapModelToPresenter(
+        assertThat(mapper.mapModelToPresenter(
                 createInfo(0, 0, 0, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(0, 0, 0));
-        assertThat(CellPosMapper.DEFAULT.mapModelToPresenter(
+        assertThat(mapper.mapModelToPresenter(
                 createInfo(0, 0, 1, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(0, 0, 1));
-        assertThat(CellPosMapper.DEFAULT.mapModelToPresenter(
+        assertThat(mapper.mapModelToPresenter(
                 createInfo(5, 0, 1, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(5, 0, 1));
-        assertThat(CellPosMapper.DEFAULT.mapModelToPresenter(
+        assertThat(mapper.mapModelToPresenter(
                 createInfo(5, 0, 0, CONTAINER_HOTSEAT))).isEqualTo(new CellPos(5, 0, 0));
     }
 
     @Test
     public void testMapPresenterToModel_default() {
-        assertThat(CellPosMapper.DEFAULT.mapPresenterToModel(
+        CellPosMapper mapper = CellPosMapper.DEFAULT;
+        assertThat(mapper.mapPresenterToModel(
                 0, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 0));
-        assertThat(CellPosMapper.DEFAULT.mapPresenterToModel(
+        assertThat(mapper.mapPresenterToModel(
                 0, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 1));
-        assertThat(CellPosMapper.DEFAULT.mapPresenterToModel(
+        assertThat(mapper.mapPresenterToModel(
                 5, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(5, 0, 1));
-        assertThat(CellPosMapper.DEFAULT.mapPresenterToModel(
+        assertThat(mapper.mapPresenterToModel(
                 5, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(5, 0, 0));
 
-        assertThat(CellPosMapper.DEFAULT.mapPresenterToModel(
+        assertThat(mapper.mapPresenterToModel(
                 0, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 0));
-        assertThat(CellPosMapper.DEFAULT.mapPresenterToModel(
-                0, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 1));
-        assertThat(CellPosMapper.DEFAULT.mapPresenterToModel(
-                5, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 1));
-        assertThat(CellPosMapper.DEFAULT.mapPresenterToModel(
-                5, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 0));
+        assertThat(mapper.mapPresenterToModel(
+                0, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 0));
+        assertThat(mapper.mapPresenterToModel(
+                5, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 5));
+        assertThat(mapper.mapPresenterToModel(
+                5, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 5));
     }
 
     @Test
@@ -116,11 +118,33 @@
         assertThat(mapper.mapPresenterToModel(
                 0, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 0));
         assertThat(mapper.mapPresenterToModel(
-                0, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 1));
+                0, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 0));
         assertThat(mapper.mapPresenterToModel(
-                5, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 1));
+                5, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 5));
         assertThat(mapper.mapPresenterToModel(
-                5, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 0));
+                5, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(5, 0, 5));
+    }
+
+    @Test
+    public void testMapPresenterToModel_VerticalHotseat() {
+        CellPosMapper mapper = new CellPosMapper(true, 6);
+        assertThat(mapper.mapPresenterToModel(
+                0, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 0));
+        assertThat(mapper.mapPresenterToModel(
+                0, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(0, 0, 1));
+        assertThat(mapper.mapPresenterToModel(
+                5, 0, 1, CONTAINER_DESKTOP)).isEqualTo(new CellPos(5, 0, 1));
+        assertThat(mapper.mapPresenterToModel(
+                5, 0, 0, CONTAINER_DESKTOP)).isEqualTo(new CellPos(5, 0, 0));
+
+        assertThat(mapper.mapPresenterToModel(
+                0, 0, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 5));
+        assertThat(mapper.mapPresenterToModel(
+                0, 0, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 0, 5));
+        assertThat(mapper.mapPresenterToModel(
+                0, 5, 1, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 5, 0));
+        assertThat(mapper.mapPresenterToModel(
+                0, 5, 0, CONTAINER_HOTSEAT)).isEqualTo(new CellPos(0, 5, 0));
     }
 
     private ItemInfo createInfo(int cellX, int cellY, int screen, int container) {