• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.graphics.Insets;
19 import android.graphics.Point;
20 import android.graphics.Rect;
21 import android.view.WindowInsets.Type;
22 import android.view.WindowMetrics;
23 
24 import androidx.annotation.Nullable;
25 
26 import java.util.Objects;
27 
28 /**
29  * Utility class to hold information about window position and layout
30  */
31 public class WindowBounds {
32 
33     public final Rect bounds;
34     public final Rect insets;
35     public final Point availableSize;
36 
WindowBounds(Rect bounds, Rect insets)37     public WindowBounds(Rect bounds, Rect insets) {
38         this.bounds = bounds;
39         this.insets = insets;
40         availableSize = new Point(bounds.width() - insets.left - insets.right,
41                 bounds.height() - insets.top - insets.bottom);
42     }
43 
WindowBounds(int width, int height, int availableWidth, int availableHeight)44     public WindowBounds(int width, int height, int availableWidth, int availableHeight) {
45         this.bounds = new Rect(0, 0, width, height);
46         this.availableSize = new Point(availableWidth, availableHeight);
47         // We don't care about insets in this case
48         this.insets = new Rect(0, 0, width - availableWidth, height - availableHeight);
49     }
50 
51     @Override
hashCode()52     public int hashCode() {
53         return Objects.hash(bounds, insets);
54     }
55 
56     @Override
equals(@ullable Object obj)57     public boolean equals(@Nullable Object obj) {
58         if (!(obj instanceof WindowBounds)) {
59             return false;
60         }
61         WindowBounds other = (WindowBounds) obj;
62         return other.bounds.equals(bounds) && other.insets.equals(insets);
63     }
64 
65     @Override
toString()66     public String toString() {
67         return "WindowBounds{"
68                 + "bounds=" + bounds
69                 + ", insets=" + insets
70                 + ", availableSize=" + availableSize
71                 + '}';
72     }
73 
74     /**
75      * Returns true if the device is in landscape orientation
76      */
isLandscape()77     public final boolean isLandscape() {
78         return availableSize.x > availableSize.y;
79     }
80 
81     /**
82      * Returns the bounds corresponding to the provided WindowMetrics
83      */
84     @SuppressWarnings("NewApi")
fromWindowMetrics(WindowMetrics wm)85     public static WindowBounds fromWindowMetrics(WindowMetrics wm) {
86         Insets insets = wm.getWindowInsets().getInsets(Type.systemBars());
87         return new WindowBounds(wm.getBounds(),
88                 new Rect(insets.left, insets.top, insets.right, insets.bottom));
89     }
90 }
91