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 
17 package android.window;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.graphics.Rect;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.Objects;
26 
27 /**
28  * The window frame container class used by client side for layout.
29  * @hide
30  */
31 public class ClientWindowFrames implements Parcelable {
32     /** The actual window bounds. */
33     public final @NonNull Rect frame = new Rect();
34 
35     /**
36      * The container frame that is usually the same as display size. It may exclude the area of
37      * insets if the window layout parameter has specified fit-insets-sides.
38      */
39     public final @NonNull Rect displayFrame = new Rect();
40 
41     /**
42      * The frame to be referenced while applying gravity and MATCH_PARENT.
43      */
44     public final @NonNull Rect parentFrame = new Rect();
45 
46     /**
47      * The frame this window attaches to. If this is not null, this is the frame of the parent
48      * window.
49      */
50     public @Nullable Rect attachedFrame;
51 
52     public boolean isParentFrameClippedByDisplayCutout;
53 
54     public float compatScale = 1f;
55 
ClientWindowFrames()56     public ClientWindowFrames() {
57     }
58 
ClientWindowFrames(@onNull ClientWindowFrames other)59     public ClientWindowFrames(@NonNull ClientWindowFrames other) {
60         setTo(other);
61     }
62 
ClientWindowFrames(@onNull Parcel in)63     private ClientWindowFrames(@NonNull Parcel in) {
64         readFromParcel(in);
65     }
66 
67     /** Updates the current frames to the given frames. */
setTo(@onNull ClientWindowFrames other)68     public void setTo(@NonNull ClientWindowFrames other) {
69         frame.set(other.frame);
70         displayFrame.set(other.displayFrame);
71         parentFrame.set(other.parentFrame);
72         if (other.attachedFrame != null) {
73             attachedFrame = new Rect(other.attachedFrame);
74         }
75         isParentFrameClippedByDisplayCutout = other.isParentFrameClippedByDisplayCutout;
76         compatScale = other.compatScale;
77     }
78 
79     /** Needed for AIDL out parameters. */
readFromParcel(Parcel in)80     public void readFromParcel(Parcel in) {
81         frame.readFromParcel(in);
82         displayFrame.readFromParcel(in);
83         parentFrame.readFromParcel(in);
84         attachedFrame = in.readTypedObject(Rect.CREATOR);
85         isParentFrameClippedByDisplayCutout = in.readBoolean();
86         compatScale = in.readFloat();
87     }
88 
89     @Override
writeToParcel(Parcel dest, int flags)90     public void writeToParcel(Parcel dest, int flags) {
91         frame.writeToParcel(dest, flags);
92         displayFrame.writeToParcel(dest, flags);
93         parentFrame.writeToParcel(dest, flags);
94         dest.writeTypedObject(attachedFrame, flags);
95         dest.writeBoolean(isParentFrameClippedByDisplayCutout);
96         dest.writeFloat(compatScale);
97     }
98 
99     @Override
toString()100     public String toString() {
101         final StringBuilder sb = new StringBuilder(32);
102         return "ClientWindowFrames{frame=" + frame.toShortString(sb)
103                 + " display=" + displayFrame.toShortString(sb)
104                 + " parentFrame=" + parentFrame.toShortString(sb)
105                 + (attachedFrame != null ? " attachedFrame=" + attachedFrame.toShortString() : "")
106                 + (isParentFrameClippedByDisplayCutout ? " parentClippedByDisplayCutout" : "")
107                 + (compatScale != 1f ? " sizeCompatScale=" + compatScale : "") +  "}";
108     }
109 
110     @Override
equals(@ullable Object o)111     public boolean equals(@Nullable Object o) {
112         if (this == o) {
113             return true;
114         }
115         if (o == null || getClass() != o.getClass()) {
116             return false;
117         }
118         final ClientWindowFrames other = (ClientWindowFrames) o;
119         return frame.equals(other.frame)
120                 && displayFrame.equals(other.displayFrame)
121                 && parentFrame.equals(other.parentFrame)
122                 && Objects.equals(attachedFrame, other.attachedFrame)
123                 && isParentFrameClippedByDisplayCutout == other.isParentFrameClippedByDisplayCutout
124                 && compatScale == other.compatScale;
125     }
126 
127     @Override
hashCode()128     public int hashCode() {
129         return Objects.hash(frame, displayFrame, parentFrame, attachedFrame,
130                 isParentFrameClippedByDisplayCutout, compatScale);
131     }
132 
133     @Override
describeContents()134     public int describeContents() {
135         return 0;
136     }
137 
138     public static final Creator<ClientWindowFrames> CREATOR = new Creator<ClientWindowFrames>() {
139         public ClientWindowFrames createFromParcel(Parcel in) {
140             return new ClientWindowFrames(in);
141         }
142 
143         public ClientWindowFrames[] newArray(int size) {
144             return new ClientWindowFrames[size];
145         }
146     };
147 }
148