1 /*
2  * Copyright (C) 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 package com.android.launcher3.util.window;
17 
18 import static com.android.launcher3.util.RotationUtils.deltaRotation;
19 import static com.android.launcher3.util.RotationUtils.rotateSize;
20 
21 import android.graphics.Insets;
22 import android.graphics.Point;
23 import android.view.DisplayCutout;
24 import android.view.Surface;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 
29 import java.util.Objects;
30 
31 /**
32  * Properties on a display
33  */
34 public class CachedDisplayInfo {
35 
36     private static final DisplayCutout NO_CUTOUT =
37             new DisplayCutout(Insets.NONE, null, null, null, null);
38 
39     public final Point size;
40     public final int rotation;
41     @NonNull
42     public final DisplayCutout cutout;
43 
CachedDisplayInfo()44     public CachedDisplayInfo() {
45         this(new Point(0, 0), 0);
46     }
47 
CachedDisplayInfo(Point size, int rotation)48     public CachedDisplayInfo(Point size, int rotation) {
49         this(size, rotation, NO_CUTOUT);
50     }
51 
CachedDisplayInfo(Point size, int rotation, @Nullable DisplayCutout cutout)52     public CachedDisplayInfo(Point size, int rotation, @Nullable DisplayCutout cutout) {
53         this.size = size;
54         this.rotation = rotation;
55         this.cutout = cutout == null ? NO_CUTOUT : cutout;
56     }
57 
58     /**
59      * Returns a CachedDisplayInfo where the properties are normalized to {@link Surface#ROTATION_0}
60      */
normalize(WindowManagerProxy windowManagerProxy)61     public CachedDisplayInfo normalize(WindowManagerProxy windowManagerProxy) {
62         if (rotation == Surface.ROTATION_0) {
63             return this;
64         }
65         Point newSize = new Point(size);
66         rotateSize(newSize, deltaRotation(rotation, Surface.ROTATION_0));
67 
68         DisplayCutout newCutout = windowManagerProxy.rotateCutout(
69                 cutout, size.x, size.y, rotation, Surface.ROTATION_0);
70         return new CachedDisplayInfo(newSize, Surface.ROTATION_0, newCutout);
71     }
72 
73     @Override
toString()74     public String toString() {
75         return "CachedDisplayInfo{"
76                 + "size=" + size
77                 + ", cutout=" + cutout
78                 + ", rotation=" + rotation
79                 + '}';
80     }
81 
82     @Override
equals(Object o)83     public boolean equals(Object o) {
84         if (this == o) return true;
85         if (!(o instanceof CachedDisplayInfo)) return false;
86         CachedDisplayInfo that = (CachedDisplayInfo) o;
87         return rotation == that.rotation
88                 && Objects.equals(size, that.size)
89                 && cutout.getSafeInsetLeft() == that.cutout.getSafeInsetLeft()
90                 && cutout.getSafeInsetTop() == that.cutout.getSafeInsetTop()
91                 && cutout.getSafeInsetRight() == that.cutout.getSafeInsetRight()
92                 && cutout.getSafeInsetBottom() == that.cutout.getSafeInsetBottom();
93     }
94 
95     @Override
hashCode()96     public int hashCode() {
97         return Objects.hash(size, rotation,
98                 cutout.getSafeInsetLeft(), cutout.getSafeInsetTop(),
99                 cutout.getSafeInsetRight(), cutout.getSafeInsetBottom());
100     }
101 }
102