1 /* 2 * Copyright (C) 2026 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.celllayout; 17 18 import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_DESKTOP; 19 20 import com.android.launcher3.model.data.ItemInfo; 21 22 import java.util.Objects; 23 24 /** 25 * Class for mapping between model position and presenter position. 26 */ 27 public class CellPosMapper { 28 29 public static final CellPosMapper DEFAULT = new CellPosMapper(); 30 CellPosMapper()31 private CellPosMapper() { } 32 33 /** 34 * Maps the position in model to the position in view 35 */ mapModelToPresenter(ItemInfo info)36 public CellPos mapModelToPresenter(ItemInfo info) { 37 return new CellPos(info.cellX, info.cellY, info.screenId); 38 } 39 40 /** 41 * Maps the position in view to the position in model 42 */ mapPresenterToModel(int presenterX, int presenterY, int presenterScreen, int container)43 public CellPos mapPresenterToModel(int presenterX, int presenterY, int presenterScreen, 44 int container) { 45 return new CellPos(presenterX, presenterY, presenterScreen); 46 } 47 48 /** 49 * Cell mapper which maps two panels into a single layout 50 */ 51 public static class TwoPanelCellPosMapper extends CellPosMapper { 52 53 private final int mColumnCount; 54 TwoPanelCellPosMapper(int columnCount)55 public TwoPanelCellPosMapper(int columnCount) { 56 mColumnCount = columnCount; 57 } 58 59 /** 60 * Maps the position in model to the position in view 61 */ mapModelToPresenter(ItemInfo info)62 public CellPos mapModelToPresenter(ItemInfo info) { 63 if (info.container != CONTAINER_DESKTOP || (info.screenId % 2) == 0) { 64 return super.mapModelToPresenter(info); 65 } 66 return new CellPos(info.cellX + mColumnCount, info.cellY, info.screenId - 1); 67 } 68 69 @Override mapPresenterToModel(int presenterX, int presenterY, int presenterScreen, int container)70 public CellPos mapPresenterToModel(int presenterX, int presenterY, int presenterScreen, 71 int container) { 72 if (container == CONTAINER_DESKTOP && (presenterScreen % 2) == 0 73 && presenterX >= mColumnCount) { 74 return new CellPos(presenterX - mColumnCount, presenterY, presenterScreen + 1); 75 } 76 return super.mapPresenterToModel(presenterX, presenterY, presenterScreen, container); 77 } 78 } 79 80 /** 81 * Utility class to indicate the position of a cell 82 */ 83 public static class CellPos { 84 public final int cellX; 85 public final int cellY; 86 public final int screenId; 87 CellPos(int cellX, int cellY, int screenId)88 public CellPos(int cellX, int cellY, int screenId) { 89 this.cellX = cellX; 90 this.cellY = cellY; 91 this.screenId = screenId; 92 } 93 94 @Override equals(Object o)95 public boolean equals(Object o) { 96 if (this == o) return true; 97 if (!(o instanceof CellPos)) return false; 98 CellPos cellPos = (CellPos) o; 99 return cellX == cellPos.cellX && cellY == cellPos.cellY && screenId == cellPos.screenId; 100 } 101 102 @Override hashCode()103 public int hashCode() { 104 return Objects.hash(cellX, cellY, screenId); 105 } 106 } 107 } 108