1 /* <lambda>null2 * 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.graphics.Rect 19 import android.util.ArrayMap 20 import android.view.View 21 import com.android.launcher3.util.CellAndSpan 22 23 /** Represents the solution to a reorder of items in the Workspace. */ 24 class ItemConfiguration : CellAndSpan() { 25 @JvmField val map = ArrayMap<View, CellAndSpan>() 26 private val savedMap = ArrayMap<View, CellAndSpan>() 27 28 @JvmField val sortedViews = ArrayList<View>() 29 30 @JvmField var intersectingViews: ArrayList<View> = ArrayList() 31 32 @JvmField var isSolution = false 33 fun save() { 34 // Copy current state into savedMap 35 map.forEach { (k, v) -> savedMap[k]?.copyFrom(v) } 36 } 37 38 fun restore() { 39 // Restore current state from savedMap 40 savedMap.forEach { (k, v) -> map[k]?.copyFrom(v) } 41 } 42 43 fun add(v: View, cs: CellAndSpan) { 44 map[v] = cs 45 savedMap[v] = CellAndSpan() 46 sortedViews.add(v) 47 } 48 49 fun area(): Int { 50 return spanX * spanY 51 } 52 53 fun getBoundingRectForViews(views: ArrayList<View>, outRect: Rect) { 54 views 55 .mapNotNull { v -> map[v] } 56 .forEachIndexed { i, c -> 57 if (i == 0) outRect.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY) 58 else outRect.union(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY) 59 } 60 } 61 } 62