blob: 324d562bfcfb7c57b03851b4947b3074b603acb6 [file] [log] [blame]
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001/*
2 * Copyright (C) 2018 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
17package android.view;
18
19import android.graphics.Insets;
20import android.graphics.Rect;
21import android.os.Parcel;
22import android.os.Parcelable;
Tiger Huang332793b2019-10-29 23:21:27 +080023import android.view.InsetsState.InternalInsetsType;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020024
25import java.io.PrintWriter;
26
27/**
28 * Represents the state of a single window generating insets for clients.
29 * @hide
30 */
31public class InsetsSource implements Parcelable {
32
Tiger Huang332793b2019-10-29 23:21:27 +080033 private final @InternalInsetsType int mType;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020034
35 /** Frame of the source in screen coordinate space */
36 private final Rect mFrame;
37 private boolean mVisible;
38
39 private final Rect mTmpFrame = new Rect();
40
Tiger Huang332793b2019-10-29 23:21:27 +080041 public InsetsSource(@InternalInsetsType int type) {
Jorim Jaggif96c90a2018-09-26 16:55:15 +020042 mType = type;
43 mFrame = new Rect();
Jorim Jaggi0dd0cf92019-12-27 15:17:44 +010044 mVisible = InsetsState.getDefaultVisibility(type);
Jorim Jaggif96c90a2018-09-26 16:55:15 +020045 }
46
47 public InsetsSource(InsetsSource other) {
48 mType = other.mType;
49 mFrame = new Rect(other.mFrame);
50 mVisible = other.mVisible;
51 }
52
53 public void setFrame(Rect frame) {
54 mFrame.set(frame);
55 }
56
57 public void setVisible(boolean visible) {
58 mVisible = visible;
59 }
60
Tiger Huang332793b2019-10-29 23:21:27 +080061 public @InternalInsetsType int getType() {
Jorim Jaggif96c90a2018-09-26 16:55:15 +020062 return mType;
63 }
64
65 public Rect getFrame() {
66 return mFrame;
67 }
68
Jorim Jaggie35c0592018-11-06 16:21:08 +010069 public boolean isVisible() {
70 return mVisible;
71 }
72
Jorim Jaggif96c90a2018-09-26 16:55:15 +020073 /**
74 * Calculates the insets this source will cause to a client window.
75 *
76 * @param relativeFrame The frame to calculate the insets relative to.
77 * @param ignoreVisibility If true, always reports back insets even if source isn't visible.
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010078 * @return The resulting insets. The contract is that only one side will be occupied by a
79 * source.
Jorim Jaggif96c90a2018-09-26 16:55:15 +020080 */
81 public Insets calculateInsets(Rect relativeFrame, boolean ignoreVisibility) {
82 if (!ignoreVisibility && !mVisible) {
83 return Insets.NONE;
84 }
85 if (!mTmpFrame.setIntersect(mFrame, relativeFrame)) {
86 return Insets.NONE;
87 }
88
89 // Intersecting at top/bottom
90 if (mTmpFrame.width() == relativeFrame.width()) {
91 if (mTmpFrame.top == relativeFrame.top) {
92 return Insets.of(0, mTmpFrame.height(), 0, 0);
93 } else {
94 return Insets.of(0, 0, 0, mTmpFrame.height());
95 }
96 }
97 // Intersecting at left/right
98 else if (mTmpFrame.height() == relativeFrame.height()) {
99 if (mTmpFrame.left == relativeFrame.left) {
100 return Insets.of(mTmpFrame.width(), 0, 0, 0);
101 } else {
102 return Insets.of(0, 0, mTmpFrame.width(), 0);
103 }
104 } else {
105 return Insets.NONE;
106 }
107 }
108
109 public void dump(String prefix, PrintWriter pw) {
110 pw.print(prefix);
111 pw.print("InsetsSource type="); pw.print(InsetsState.typeToString(mType));
112 pw.print(" frame="); pw.print(mFrame.toShortString());
113 pw.print(" visible="); pw.print(mVisible);
114 pw.println();
115 }
116
117 @Override
118 public boolean equals(Object o) {
119 if (this == o) return true;
120 if (o == null || getClass() != o.getClass()) return false;
121
122 InsetsSource that = (InsetsSource) o;
123
124 if (mType != that.mType) return false;
125 if (mVisible != that.mVisible) return false;
126 return mFrame.equals(that.mFrame);
127 }
128
129 @Override
130 public int hashCode() {
131 int result = mType;
132 result = 31 * result + mFrame.hashCode();
133 result = 31 * result + (mVisible ? 1 : 0);
134 return result;
135 }
136
137 public InsetsSource(Parcel in) {
138 mType = in.readInt();
139 mFrame = in.readParcelable(null /* loader */);
140 mVisible = in.readBoolean();
141 }
142
143 @Override
144 public int describeContents() {
145 return 0;
146 }
147
148 @Override
149 public void writeToParcel(Parcel dest, int flags) {
150 dest.writeInt(mType);
151 dest.writeParcelable(mFrame, 0 /* flags*/);
152 dest.writeBoolean(mVisible);
153 }
154
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700155 public static final @android.annotation.NonNull Creator<InsetsSource> CREATOR = new Creator<InsetsSource>() {
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200156
157 public InsetsSource createFromParcel(Parcel in) {
158 return new InsetsSource(in);
159 }
160
161 public InsetsSource[] newArray(int size) {
162 return new InsetsSource[size];
163 }
164 };
165}