1 /*
2  * Copyright 2022 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 androidx.window.core.layout
18 
19 import kotlin.jvm.JvmField
20 
21 /**
22  * A class to represent the height size buckets for a viewport. The possible values are [COMPACT],
23  * [MEDIUM], and [EXPANDED]. [WindowHeightSizeClass] should not be used as a proxy for the device
24  * type. It is possible to have resizeable windows in different device types. The viewport might
25  * change from a [COMPACT] all the way to an [EXPANDED] size class.
26  */
27 @Suppress("DEPRECATION")
28 @Deprecated("WindowHeightSizeClass will not be developed further, use WindowSizeClass instead.")
29 class WindowHeightSizeClass private constructor(private val rawValue: Int) {
30 
toStringnull31     override fun toString(): String {
32         val name =
33             when (this) {
34                 COMPACT -> "COMPACT"
35                 MEDIUM -> "MEDIUM"
36                 EXPANDED -> "EXPANDED"
37                 else -> "UNKNOWN"
38             }
39         return "WindowHeightSizeClass: $name"
40     }
41 
equalsnull42     override fun equals(other: Any?): Boolean {
43         if (this === other) return true
44         if (other == null) return false
45         if (this::class != other::class) return false
46 
47         val that = other as WindowHeightSizeClass
48 
49         return rawValue == that.rawValue
50     }
51 
hashCodenull52     override fun hashCode(): Int {
53         return rawValue
54     }
55 
56     companion object {
57         /** A bucket to represent a compact height, typical for a phone that is in landscape. */
58         @Deprecated("WindowHeightSizeClass not be developed further.")
59         @JvmField
60         val COMPACT: WindowHeightSizeClass = WindowHeightSizeClass(0)
61 
62         /** A bucket to represent a medium height, typical for a phone in portrait or a tablet. */
63         @Deprecated("WindowHeightSizeClass not be developed further.")
64         @JvmField
65         val MEDIUM: WindowHeightSizeClass = WindowHeightSizeClass(1)
66 
67         /**
68          * A bucket to represent an expanded height window, typical for a large tablet or a desktop
69          * form-factor.
70          */
71         @Deprecated("WindowHeightSizeClass not be developed further.")
72         @JvmField
73         val EXPANDED: WindowHeightSizeClass = WindowHeightSizeClass(2)
74 
75         /**
76          * Returns a recommended [WindowHeightSizeClass] for the height of a window given the height
77          * in DP.
78          *
79          * @param dpHeight the height of the window in DP
80          * @return A recommended size class for the height
81          * @throws IllegalArgumentException if the height is negative
82          */
83         @Deprecated("WindowHeightSizeClass not be developed further.")
computenull84         internal fun compute(dpHeight: Float): WindowHeightSizeClass {
85             require(dpHeight >= 0) { "Height must be positive, received $dpHeight" }
86             return when {
87                 dpHeight < 480 -> COMPACT
88                 dpHeight < 900 -> MEDIUM
89                 else -> EXPANDED
90             }
91         }
92     }
93 }
94