• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27     init {
28         steps =
29             res.obtainTypedArray(R.array.icon_size_steps).use {
30                 (0 until it.length()).map { step -> it.getDimensionOrThrow(step).toInt() }.sorted()
31             }
32     }
33 
34     fun minimumIconSize(): Int = steps[0]
35 
36     fun getNextLowerIconSize(iconSizePx: Int): Int {
37         return steps[max(0, getIndexForIconSize(iconSizePx) - 1)]
38     }
39 
40     fun getIconSmallerThan(cellWidth: Int): Int {
41         return steps.lastOrNull { it <= cellWidth } ?: steps[0]
42     }
43 
44     private fun getIndexForIconSize(iconSizePx: Int): Int {
45         return max(0, steps.indexOfFirst { iconSizePx <= it })
46     }
47 }
48