• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 android.view.View;
19 
20 import com.android.launcher3.CellLayout;
21 import com.android.launcher3.MultipageCellLayout;
22 import com.android.launcher3.ShortcutAndWidgetContainer;
23 import com.android.launcher3.util.GridOccupancy;
24 
25 import java.util.Arrays;
26 import java.util.function.Supplier;
27 
28 /**
29  * Variant of ReorderAlgorithm which simulates a foldable screen and adds a seam in the middle
30  * to prevent items to be placed in the middle.
31  */
32 public class MulticellReorderAlgorithm extends ReorderAlgorithm {
33 
34     private final View mSeam;
35 
MulticellReorderAlgorithm(CellLayout cellLayout)36     public MulticellReorderAlgorithm(CellLayout cellLayout) {
37         super(cellLayout);
38         mSeam = new View(cellLayout.getContext());
39     }
40 
removeSeamFromSolution( CellLayout.ItemConfiguration solution)41     private CellLayout.ItemConfiguration removeSeamFromSolution(
42             CellLayout.ItemConfiguration solution) {
43         solution.map.forEach((view, cell) -> cell.cellX =
44                 cell.cellX > mCellLayout.getCountX() / 2 ? cell.cellX - 1 : cell.cellX);
45         solution.cellX =
46                 solution.cellX > mCellLayout.getCountX() / 2 ? solution.cellX - 1 : solution.cellX;
47         return solution;
48     }
49 
50     @Override
closestEmptySpaceReorder(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY)51     public CellLayout.ItemConfiguration closestEmptySpaceReorder(int pixelX, int pixelY,
52             int minSpanX, int minSpanY,
53             int spanX, int spanY) {
54         return removeSeamFromSolution(simulateSeam(
55                 () -> super.closestEmptySpaceReorder(pixelX, pixelY, minSpanX, minSpanY, spanX,
56                         spanY)));
57     }
58 
59     @Override
findReorderSolution(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, int[] direction, View dragView, boolean decX, CellLayout.ItemConfiguration solution)60     public CellLayout.ItemConfiguration findReorderSolution(int pixelX, int pixelY, int minSpanX,
61             int minSpanY, int spanX, int spanY, int[] direction, View dragView, boolean decX,
62             CellLayout.ItemConfiguration solution) {
63         return removeSeamFromSolution(simulateSeam(
64                 () -> super.findReorderSolution(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY,
65                         direction, dragView, decX, solution)));
66     }
67 
68     @Override
dropInPlaceSolution(int pixelX, int pixelY, int spanX, int spanY, View dragView)69     public CellLayout.ItemConfiguration dropInPlaceSolution(int pixelX, int pixelY, int spanX,
70             int spanY,
71             View dragView) {
72         return removeSeamFromSolution(simulateSeam(
73                 () -> super.dropInPlaceSolution(pixelX, pixelY, spanX, spanY, dragView)));
74     }
75 
addSeam()76     void addSeam() {
77         MultipageCellLayout mcl = (MultipageCellLayout) mCellLayout;
78         mcl.setSeamWasAdded(true);
79         CellLayoutLayoutParams lp = new CellLayoutLayoutParams(mcl.getCountX() / 2, 0, 1,
80                 mcl.getCountY());
81         lp.canReorder = false;
82         mcl.setCountX(mcl.getCountX() + 1);
83         mcl.getShortcutsAndWidgets().addViewInLayout(mSeam, lp);
84         mcl.setOccupied(createGridOccupancyWithSeam());
85         mcl.mTmpOccupied = new GridOccupancy(mcl.getCountX(), mcl.getCountY());
86     }
87 
removeSeam()88     void removeSeam() {
89         MultipageCellLayout mcl = (MultipageCellLayout) mCellLayout;
90         mcl.setCountX(mcl.getCountX() - 1);
91         mcl.getShortcutsAndWidgets().removeViewInLayout(mSeam);
92         mcl.mTmpOccupied = new GridOccupancy(mcl.getCountX(), mcl.getCountY());
93         mcl.setSeamWasAdded(false);
94     }
95 
96     /**
97      * The function supplied here will execute while the CellLayout has a simulated seam added.
98      *
99      * @param f   function to run under simulation
100      * @param <T> return value of the supplied function
101      * @return Value of supplied function
102      */
simulateSeam(Supplier<T> f)103     public <T> T simulateSeam(Supplier<T> f) {
104         MultipageCellLayout mcl = (MultipageCellLayout) mCellLayout;
105         if (mcl.isSeamWasAdded()) {
106             return f.get();
107         }
108         GridOccupancy auxGrid = mcl.getOccupied();
109         addSeam();
110         T res = f.get();
111         removeSeam();
112         mcl.setOccupied(auxGrid);
113         return res;
114     }
115 
createGridOccupancyWithSeam()116     GridOccupancy createGridOccupancyWithSeam() {
117         ShortcutAndWidgetContainer shortcutAndWidgets = mCellLayout.getShortcutsAndWidgets();
118         GridOccupancy grid = new GridOccupancy(mCellLayout.getCountX(), mCellLayout.getCountY());
119         for (int i = 0; i < shortcutAndWidgets.getChildCount(); i++) {
120             View view = shortcutAndWidgets.getChildAt(i);
121             CellLayoutLayoutParams lp = (CellLayoutLayoutParams) view.getLayoutParams();
122             int seamOffset = lp.getCellX() >= mCellLayout.getCountX() / 2 && lp.canReorder ? 1 : 0;
123             grid.markCells(lp.getCellX() + seamOffset, lp.getCellY(), lp.cellHSpan, lp.cellVSpan,
124                     true);
125         }
126         Arrays.fill(grid.cells[mCellLayout.getCountX() / 2], true);
127         return grid;
128     }
129 }
130