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.board 17 18 import android.graphics.Point 19 20 /** 21 * Compares two [CellLayoutBoard] and returns 0 if they contain the same widgets and icons even if 22 * they are in different positions i.e. in a different permutation. 23 */ 24 class PermutedBoardComparator : Comparator<CellLayoutBoard> { 25 26 /** 27 * The key for the set is the span since the widgets could change location but shouldn't change 28 * size 29 */ boardToSpanCountMapnull30 private fun boardToSpanCountMap(widgets: List<WidgetRect>) = 31 widgets.groupingBy { Point(it.spanX, it.spanY) }.eachCount() comparenull32 override fun compare( 33 cellLayoutBoard: CellLayoutBoard, 34 otherCellLayoutBoard: CellLayoutBoard 35 ): Int { 36 return if ( 37 boardToSpanCountMap(cellLayoutBoard.widgets) != 38 boardToSpanCountMap(otherCellLayoutBoard.widgets) 39 ) { 40 1 41 } else cellLayoutBoard.icons.size.compareTo(otherCellLayoutBoard.icons.size) 42 } 43 } 44