• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.launcher3.responsive
18 
19 import android.content.res.TypedArray
20 import android.util.Log
21 import com.android.launcher3.R
22 import com.android.launcher3.util.ResourceHelper
23 
24 class HotseatSpecs(val specs: List<HotseatSpec>) {
25 
getCalculatedHeightSpecnull26     fun getCalculatedHeightSpec(availableHeight: Int): CalculatedHotseatSpec {
27         val spec = specs.firstOrNull { availableHeight <= it.maxAvailableSize }
28         check(spec != null) { "No available height spec found within $availableHeight." }
29         return CalculatedHotseatSpec(availableHeight, spec)
30     }
31 
32     companion object {
33         private const val XML_HOTSEAT_SPEC = "hotseatSpec"
34 
35         @JvmStatic
createnull36         fun create(resourceHelper: ResourceHelper): HotseatSpecs {
37             val parser = ResponsiveSpecsParser(resourceHelper)
38             val specs = parser.parseXML(XML_HOTSEAT_SPEC, ::HotseatSpec)
39             return HotseatSpecs(specs.filter { it.specType == ResponsiveSpec.SpecType.HEIGHT })
40         }
41     }
42 }
43 
44 data class HotseatSpec(
45     val maxAvailableSize: Int,
46     val specType: ResponsiveSpec.SpecType,
47     val hotseatQsbSpace: SizeSpec
48 ) {
49 
50     init {
<lambda>null51         check(isValid()) { "Invalid HotseatSpec found." }
52     }
53 
54     constructor(
55         attrs: TypedArray,
56         specs: Map<String, SizeSpec>
57     ) : this(
58         maxAvailableSize =
59             attrs.getDimensionPixelSize(R.styleable.ResponsiveSpec_maxAvailableSize, 0),
60         specType =
61             ResponsiveSpec.SpecType.values()[
62                     attrs.getInt(
63                         R.styleable.ResponsiveSpec_specType,
64                         ResponsiveSpec.SpecType.HEIGHT.ordinal
65                     )],
66         hotseatQsbSpace = specs.getOrError(SizeSpec.XmlTags.HOTSEAT_QSB_SPACE)
67     )
68 
isValidnull69     fun isValid(): Boolean {
70         if (maxAvailableSize <= 0) {
71             Log.e(LOG_TAG, "${this::class.simpleName}#isValid - maxAvailableSize <= 0")
72             return false
73         }
74 
75         // All specs need to be individually valid
76         if (!allSpecsAreValid()) {
77             Log.e(LOG_TAG, "${this::class.simpleName}#isValid - !allSpecsAreValid()")
78             return false
79         }
80 
81         return true
82     }
83 
allSpecsAreValidnull84     private fun allSpecsAreValid(): Boolean {
85         return hotseatQsbSpace.isValid() && hotseatQsbSpace.onlyFixedSize()
86     }
87 
88     companion object {
89         private const val LOG_TAG = "HotseatSpec"
90     }
91 }
92 
93 class CalculatedHotseatSpec(val availableSpace: Int, val spec: HotseatSpec) {
94 
95     var hotseatQsbSpace: Int = 0
96         private set
97 
98     init {
99         hotseatQsbSpace = spec.hotseatQsbSpace.getCalculatedValue(availableSpace)
100     }
101 
hashCodenull102     override fun hashCode(): Int {
103         var result = availableSpace.hashCode()
104         result = 31 * result + hotseatQsbSpace.hashCode()
105         result = 31 * result + spec.hashCode()
106         return result
107     }
108 
equalsnull109     override fun equals(other: Any?): Boolean {
110         return other is CalculatedHotseatSpec &&
111             availableSpace == other.availableSpace &&
112             hotseatQsbSpace == other.hotseatQsbSpace &&
113             spec == other.spec
114     }
115 
toStringnull116     override fun toString(): String {
117         return "${this::class.simpleName}(" +
118             "availableSpace=$availableSpace, hotseatQsbSpace=$hotseatQsbSpace, " +
119             "${spec::class.simpleName}.maxAvailableSize=${spec.maxAvailableSize}" +
120             ")"
121     }
122 }
123