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.util
17 
18 import android.content.res.Resources
19 import androidx.core.content.res.getDimensionOrThrow
20 import androidx.core.content.res.use
21 import com.android.launcher3.R
22 import kotlin.math.max
23 
24 class IconSizeSteps(res: Resources) {
25     private val steps: List<Int>
26     val minimumIconLabelSize: Int
27 
28     init {
29         steps =
30             res.obtainTypedArray(R.array.icon_size_steps).use {
31                 (0 until it.length()).map { step -> it.getDimensionOrThrow(step).toInt() }.sorted()
32             }
33         minimumIconLabelSize = res.getDimensionPixelSize(R.dimen.minimum_icon_label_size)
34     }
35 
36     fun minimumIconSize(): Int = steps[0]
37 
38     fun getNextLowerIconSize(iconSizePx: Int): Int {
39         return steps[max(0, getIndexForIconSize(iconSizePx) - 1)]
40     }
41 
42     fun getIconSmallerThan(cellSize: Int): Int {
43         return steps.lastOrNull { it <= cellSize } ?: steps[0]
44     }
45 
46     private fun getIndexForIconSize(iconSizePx: Int): Int {
47         return max(0, steps.indexOfFirst { iconSizePx <= it })
48     }
49 
50     companion object {
51         internal const val TEXT_STEP = 1
52 
53         // This icon extra step is used for stepping down logic in extreme cases when it's
54         // necessary to reduce the icon size below minimum size available in [icon_size_steps].
55         internal const val ICON_SIZE_STEP_EXTRA = 2
56     }
57 }
58