• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.launcher3.util;
2 
3 import android.graphics.Rect;
4 
5 import com.android.launcher3.model.data.ItemInfo;
6 
7 /**
8  * Utility object to manage the occupancy in a grid.
9  */
10 public class GridOccupancy extends AbsGridOccupancy {
11 
12     private final int mCountX;
13     private final int mCountY;
14 
15     public final boolean[][] cells;
16 
GridOccupancy(int countX, int countY)17     public GridOccupancy(int countX, int countY) {
18         mCountX = countX;
19         mCountY = countY;
20         cells = new boolean[countX][countY];
21     }
22 
23     /**
24      * Find the first vacant cell, if there is one.
25      *
26      * @param vacantOut Holds the x and y coordinate of the vacant cell
27      * @param spanX Horizontal cell span.
28      * @param spanY Vertical cell span.
29      *
30      * @return true if a vacant cell was found
31      */
findVacantCell(int[] vacantOut, int spanX, int spanY)32     public boolean findVacantCell(int[] vacantOut, int spanX, int spanY) {
33         return super.findVacantCell(vacantOut, cells, mCountX, mCountY, spanX, spanY);
34     }
35 
copyTo(GridOccupancy dest)36     public void copyTo(GridOccupancy dest) {
37         for (int i = 0; i < mCountX; i++) {
38             for (int j = 0; j < mCountY; j++) {
39                 dest.cells[i][j] = cells[i][j];
40             }
41         }
42     }
43 
isRegionVacant(int x, int y, int spanX, int spanY)44     public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
45         int x2 = x + spanX - 1;
46         int y2 = y + spanY - 1;
47         if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
48             return false;
49         }
50         for (int i = x; i <= x2; i++) {
51             for (int j = y; j <= y2; j++) {
52                 if (cells[i][j]) {
53                     return false;
54                 }
55             }
56         }
57         return true;
58     }
59 
markCells(int cellX, int cellY, int spanX, int spanY, boolean value)60     public void markCells(int cellX, int cellY, int spanX, int spanY, boolean value) {
61         if (cellX < 0 || cellY < 0) return;
62         for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
63             for (int y = cellY; y < cellY + spanY && y < mCountY; y++) {
64                 cells[x][y] = value;
65             }
66         }
67     }
68 
markCells(Rect r, boolean value)69     public void markCells(Rect r, boolean value) {
70         markCells(r.left, r.top, r.width(), r.height(), value);
71     }
72 
markCells(CellAndSpan cell, boolean value)73     public void markCells(CellAndSpan cell, boolean value) {
74         markCells(cell.cellX, cell.cellY, cell.spanX, cell.spanY, value);
75     }
76 
markCells(ItemInfo item, boolean value)77     public void markCells(ItemInfo item, boolean value) {
78         markCells(item.cellX, item.cellY, item.spanX, item.spanY, value);
79     }
80 
clear()81     public void clear() {
82         markCells(0, 0, mCountX, mCountY, false);
83     }
84 
85     @Override
toString()86     public String toString() {
87         StringBuilder s = new StringBuilder("Grid: \n");
88         for (int y = 0; y < mCountY; y++) {
89             for (int x = 0; x < mCountX; x++) {
90                 s.append(cells[x][y] ? 1 : 0).append(" ");
91             }
92             s.append("\n");
93         }
94         return s.toString();
95     }
96 }
97