summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/util/GridOccupancy.java
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2016-04-02 14:12:34 -0700
committerSunny Goyal <sunnygoyal@google.com>2016-05-27 09:12:26 -0700
commitff4ba2d99593ed84963b3f71c555b529dd905835 (patch)
treedf473dbc5a6ec373367de624187323a5132dc9b6 /src/com/android/launcher3/util/GridOccupancy.java
parent3c4b03dd01b89a4d4beba6a866ae3bb9093e101e (diff)
downloadandroid_packages_apps_Trebuchet-ff4ba2d99593ed84963b3f71c555b529dd905835.tar.gz
android_packages_apps_Trebuchet-ff4ba2d99593ed84963b3f71c555b529dd905835.tar.bz2
android_packages_apps_Trebuchet-ff4ba2d99593ed84963b3f71c555b529dd905835.zip
Refactoring out grid occupancy management in a separate class
Change-Id: I37a830c0f2eb0a0dd4f5fc78fa29127cb18cb3c2
Diffstat (limited to 'src/com/android/launcher3/util/GridOccupancy.java')
-rw-r--r--src/com/android/launcher3/util/GridOccupancy.java101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/com/android/launcher3/util/GridOccupancy.java b/src/com/android/launcher3/util/GridOccupancy.java
new file mode 100644
index 000000000..3f5f0b4ed
--- /dev/null
+++ b/src/com/android/launcher3/util/GridOccupancy.java
@@ -0,0 +1,101 @@
+package com.android.launcher3.util;
+
+import android.graphics.Rect;
+
+import com.android.launcher3.ItemInfo;
+
+/**
+ * Utility object to manage the occupancy in a grid.
+ */
+public class GridOccupancy {
+
+ private final int mCountX;
+ private final int mCountY;
+
+ public final boolean[][] cells;
+
+ public GridOccupancy(int countX, int countY) {
+ mCountX = countX;
+ mCountY = countY;
+ cells = new boolean[countX][countY];
+ }
+
+ /**
+ * Find the first vacant cell, if there is one.
+ *
+ * @param vacantOut Holds the x and y coordinate of the vacant cell
+ * @param spanX Horizontal cell span.
+ * @param spanY Vertical cell span.
+ *
+ * @return true if a vacant cell was found
+ */
+ public boolean findVacantCell(int[] vacantOut, int spanX, int spanY) {
+ for (int y = 0; (y + spanY) <= mCountY; y++) {
+ for (int x = 0; (x + spanX) <= mCountX; x++) {
+ boolean available = !cells[x][y];
+ out:
+ for (int i = x; i < x + spanX; i++) {
+ for (int j = y; j < y + spanY; j++) {
+ available = available && !cells[i][j];
+ if (!available) break out;
+ }
+ }
+ if (available) {
+ vacantOut[0] = x;
+ vacantOut[1] = y;
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ public void copyTo(GridOccupancy dest) {
+ for (int i = 0; i < mCountX; i++) {
+ for (int j = 0; j < mCountY; j++) {
+ dest.cells[i][j] = cells[i][j];
+ }
+ }
+ }
+
+ public boolean isRegionVacant(int x, int y, int spanX, int spanY) {
+ int x2 = x + spanX - 1;
+ int y2 = y + spanY - 1;
+ if (x < 0 || y < 0 || x2 >= mCountX || y2 >= mCountY) {
+ return false;
+ }
+ for (int i = x; i <= x2; i++) {
+ for (int j = y; j <= y2; j++) {
+ if (cells[i][j]) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ public void markCells(int cellX, int cellY, int spanX, int spanY, boolean value) {
+ if (cellX < 0 || cellY < 0) return;
+ for (int x = cellX; x < cellX + spanX && x < mCountX; x++) {
+ for (int y = cellY; y < cellY + spanY && y < mCountX; y++) {
+ cells[x][y] = value;
+ }
+ }
+ }
+
+ public void markCells(Rect r, boolean value) {
+ markCells(r.left, r.top, r.width(), r.height(), value);
+ }
+
+ public void markCells(CellAndSpan cell, boolean value) {
+ markCells(cell.cellX, cell.cellY, cell.spanX, cell.spanY, value);
+ }
+
+ public void markCells(ItemInfo item, boolean value) {
+ markCells(item.cellX, item.cellY, item.spanX, item.spanY, value);
+ }
+
+ public void clear() {
+ markCells(0, 0, mCountX, mCountY, false);
+ }
+}