1 /*
2  * Copyright 2021 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 androidx.window.layout
17 
18 import android.graphics.Rect
19 import android.util.DisplayMetrics
20 import androidx.annotation.RestrictTo
21 import androidx.window.core.Bounds
22 
23 /**
24  * Metrics about a [android.view.Window], consisting of its bounds.
25  *
26  * This is obtained from [WindowMetricsCalculator.computeCurrentWindowMetrics] or
27  * [WindowMetricsCalculator.computeMaximumWindowMetrics].
28  *
29  * @see WindowMetricsCalculator
30  */
31 class WindowMetrics
32 internal constructor(
33     private val _bounds: Bounds,
34     /**
35      * Returns the logical density of the display this window is in.
36      *
37      * @see [DisplayMetrics.density]
38      */
39     val density: Float
40 ) {
41 
42     /** An internal constructor for [WindowMetrics] */
43     @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
44     constructor(bounds: Rect, density: Float) : this(Bounds(bounds), density)
45 
46     /**
47      * Returns a new [Rect] describing the bounds of the area the window occupies.
48      *
49      * **Note that the size of the reported bounds can have different size than
50      * [android.view.Display.getSize].** This method reports the window size including insets from
51      * all system decorations, while [android.view.Display.getSize] reports the area excluding
52      * navigation bars and display cutout areas.
53      *
54      * @return window bounds in pixels.
55      */
56     val bounds: Rect
57         get() = _bounds.toRect()
58 
59     /** Returns the width of the [Rect] in DP units including insets from all system decorations. */
60     val widthDp: Float
61         get() = bounds.width() / density
62 
63     /**
64      * Returns the height of the [Rect] in DP units including insets from all system decorations.
65      */
66     val heightDp: Float
67         get() = bounds.height() / density
68 
equalsnull69     override fun equals(other: Any?): Boolean {
70         if (this === other) return true
71         if (javaClass != other?.javaClass) return false
72 
73         other as WindowMetrics
74 
75         if (_bounds != other._bounds) return false
76         if (density != other.density) return false
77 
78         return true
79     }
80 
hashCodenull81     override fun hashCode(): Int {
82         var result = _bounds.hashCode()
83         result = 31 * result + density.hashCode()
84         return result
85     }
86 
toStringnull87     override fun toString(): String {
88         return "WindowMetrics(_bounds=$_bounds, density=$density)"
89     }
90 }
91