Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Yunfan Chen | fae0aea | 2020-02-22 20:57:57 +0900 | [diff] [blame] | 19 | import static android.view.InsetsState.ITYPE_CAPTION_BAR; |
Jorim Jaggi | 23a02a9 | 2020-01-29 15:30:36 +0100 | [diff] [blame] | 20 | import static android.view.InsetsState.ITYPE_IME; |
| 21 | |
Tiger Huang | a24aab5 | 2020-03-13 17:06:35 +0800 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 24 | import android.graphics.Insets; |
| 25 | import android.graphics.Rect; |
| 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 28 | import android.view.InsetsState.InternalInsetsType; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 29 | |
| 30 | import java.io.PrintWriter; |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 31 | import java.util.Objects; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Represents the state of a single window generating insets for clients. |
| 35 | * @hide |
| 36 | */ |
| 37 | public class InsetsSource implements Parcelable { |
| 38 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 39 | private final @InternalInsetsType int mType; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 40 | |
| 41 | /** Frame of the source in screen coordinate space */ |
| 42 | private final Rect mFrame; |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 43 | private @Nullable Rect mVisibleFrame; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 44 | private boolean mVisible; |
| 45 | |
| 46 | private final Rect mTmpFrame = new Rect(); |
| 47 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 48 | public InsetsSource(@InternalInsetsType int type) { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 49 | mType = type; |
| 50 | mFrame = new Rect(); |
Jorim Jaggi | 0dd0cf9 | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 51 | mVisible = InsetsState.getDefaultVisibility(type); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public InsetsSource(InsetsSource other) { |
| 55 | mType = other.mType; |
| 56 | mFrame = new Rect(other.mFrame); |
| 57 | mVisible = other.mVisible; |
Jorim Jaggi | 4d4fae4 | 2020-01-30 23:10:12 +0100 | [diff] [blame] | 58 | mVisibleFrame = other.mVisibleFrame != null |
| 59 | ? new Rect(other.mVisibleFrame) |
| 60 | : null; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Tiger Huang | e16645a | 2020-02-25 22:24:39 +0800 | [diff] [blame] | 63 | public void setFrame(int left, int top, int right, int bottom) { |
| 64 | mFrame.set(left, top, right, bottom); |
| 65 | } |
| 66 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 67 | public void setFrame(Rect frame) { |
| 68 | mFrame.set(frame); |
| 69 | } |
| 70 | |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 71 | public void setVisibleFrame(@Nullable Rect visibleFrame) { |
| 72 | mVisibleFrame = visibleFrame != null ? new Rect(visibleFrame) : visibleFrame; |
| 73 | } |
| 74 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 75 | public void setVisible(boolean visible) { |
| 76 | mVisible = visible; |
| 77 | } |
| 78 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 79 | public @InternalInsetsType int getType() { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 80 | return mType; |
| 81 | } |
| 82 | |
| 83 | public Rect getFrame() { |
| 84 | return mFrame; |
| 85 | } |
| 86 | |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 87 | public @Nullable Rect getVisibleFrame() { |
| 88 | return mVisibleFrame; |
| 89 | } |
| 90 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 91 | public boolean isVisible() { |
| 92 | return mVisible; |
| 93 | } |
| 94 | |
Tiger Huang | 618dbe02 | 2020-06-19 00:12:55 +0800 | [diff] [blame] | 95 | boolean isUserControllable() { |
| 96 | // If mVisibleFrame is null, it will be the same area as mFrame. |
| 97 | return mVisibleFrame == null || !mVisibleFrame.isEmpty(); |
| 98 | } |
| 99 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 100 | /** |
| 101 | * Calculates the insets this source will cause to a client window. |
| 102 | * |
| 103 | * @param relativeFrame The frame to calculate the insets relative to. |
| 104 | * @param ignoreVisibility If true, always reports back insets even if source isn't visible. |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 105 | * @return The resulting insets. The contract is that only one side will be occupied by a |
| 106 | * source. |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 107 | */ |
| 108 | public Insets calculateInsets(Rect relativeFrame, boolean ignoreVisibility) { |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 109 | return calculateInsets(relativeFrame, mFrame, ignoreVisibility); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Like {@link #calculateInsets(Rect, boolean)}, but will return visible insets. |
| 114 | */ |
| 115 | public Insets calculateVisibleInsets(Rect relativeFrame) { |
| 116 | return calculateInsets(relativeFrame, mVisibleFrame != null ? mVisibleFrame : mFrame, |
| 117 | false /* ignoreVisibility */); |
| 118 | } |
| 119 | |
| 120 | private Insets calculateInsets(Rect relativeFrame, Rect frame, boolean ignoreVisibility) { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 121 | if (!ignoreVisibility && !mVisible) { |
| 122 | return Insets.NONE; |
| 123 | } |
Yunfan Chen | fae0aea | 2020-02-22 20:57:57 +0900 | [diff] [blame] | 124 | // During drag-move and drag-resizing, the caption insets position may not get updated |
| 125 | // before the app frame get updated. To layout the app content correctly during drag events, |
| 126 | // we always return the insets with the corresponding height covering the top. |
| 127 | if (getType() == ITYPE_CAPTION_BAR) { |
| 128 | return Insets.of(0, frame.height(), 0, 0); |
| 129 | } |
Yunfan Chen | a07fb93 | 2020-04-27 18:35:14 +0900 | [diff] [blame] | 130 | if (!getIntersection(frame, relativeFrame, mTmpFrame)) { |
| 131 | return Insets.NONE; |
| 132 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 133 | |
Jorim Jaggi | 23a02a9 | 2020-01-29 15:30:36 +0100 | [diff] [blame] | 134 | // TODO: Currently, non-floating IME always intersects at bottom due to issues with cutout. |
| 135 | // However, we should let the policy decide from the server. |
| 136 | if (getType() == ITYPE_IME) { |
| 137 | return Insets.of(0, 0, 0, mTmpFrame.height()); |
| 138 | } |
| 139 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 140 | // Intersecting at top/bottom |
| 141 | if (mTmpFrame.width() == relativeFrame.width()) { |
| 142 | if (mTmpFrame.top == relativeFrame.top) { |
| 143 | return Insets.of(0, mTmpFrame.height(), 0, 0); |
Evan Rosky | e067ddf | 2020-04-24 18:28:25 -0700 | [diff] [blame] | 144 | } else if (mTmpFrame.bottom == relativeFrame.bottom) { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 145 | return Insets.of(0, 0, 0, mTmpFrame.height()); |
| 146 | } |
Evan Rosky | e067ddf | 2020-04-24 18:28:25 -0700 | [diff] [blame] | 147 | // TODO: remove when insets are shell-customizable. |
| 148 | // This is a hack that says "if this is a top-inset (eg statusbar), always apply it |
| 149 | // to the top". It is used when adjusting primary split for IME. |
| 150 | if (mTmpFrame.top == 0) { |
| 151 | return Insets.of(0, mTmpFrame.height(), 0, 0); |
| 152 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 153 | } |
| 154 | // Intersecting at left/right |
| 155 | else if (mTmpFrame.height() == relativeFrame.height()) { |
| 156 | if (mTmpFrame.left == relativeFrame.left) { |
| 157 | return Insets.of(mTmpFrame.width(), 0, 0, 0); |
Evan Rosky | e067ddf | 2020-04-24 18:28:25 -0700 | [diff] [blame] | 158 | } else if (mTmpFrame.right == relativeFrame.right) { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 159 | return Insets.of(0, 0, mTmpFrame.width(), 0); |
| 160 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 161 | } |
Evan Rosky | e067ddf | 2020-04-24 18:28:25 -0700 | [diff] [blame] | 162 | return Insets.NONE; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Tiger Huang | a24aab5 | 2020-03-13 17:06:35 +0800 | [diff] [blame] | 165 | /** |
| 166 | * Outputs the intersection of two rectangles. The shared edges will also be counted in the |
| 167 | * intersection. |
| 168 | * |
| 169 | * @param a The first rectangle being intersected with. |
| 170 | * @param b The second rectangle being intersected with. |
| 171 | * @param out The rectangle which represents the intersection. |
| 172 | * @return {@code true} if there is any intersection. |
| 173 | */ |
| 174 | private static boolean getIntersection(@NonNull Rect a, @NonNull Rect b, @NonNull Rect out) { |
| 175 | if (a.left <= b.right && b.left <= a.right && a.top <= b.bottom && b.top <= a.bottom) { |
| 176 | out.left = Math.max(a.left, b.left); |
| 177 | out.top = Math.max(a.top, b.top); |
| 178 | out.right = Math.min(a.right, b.right); |
| 179 | out.bottom = Math.min(a.bottom, b.bottom); |
| 180 | return true; |
| 181 | } |
| 182 | out.setEmpty(); |
| 183 | return false; |
| 184 | } |
| 185 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 186 | public void dump(String prefix, PrintWriter pw) { |
| 187 | pw.print(prefix); |
| 188 | pw.print("InsetsSource type="); pw.print(InsetsState.typeToString(mType)); |
| 189 | pw.print(" frame="); pw.print(mFrame.toShortString()); |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 190 | if (mVisibleFrame != null) { |
Tiger Huang | a24aab5 | 2020-03-13 17:06:35 +0800 | [diff] [blame] | 191 | pw.print(" visibleFrame="); pw.print(mVisibleFrame.toShortString()); |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 192 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 193 | pw.print(" visible="); pw.print(mVisible); |
| 194 | pw.println(); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public boolean equals(Object o) { |
Jorim Jaggi | ed31259 | 2020-05-25 16:46:56 +0200 | [diff] [blame] | 199 | return equals(o, false); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param excludeInvisibleImeFrames If {@link InsetsState#ITYPE_IME} frames should be ignored |
| 204 | * when IME is not visible. |
| 205 | */ |
| 206 | public boolean equals(Object o, boolean excludeInvisibleImeFrames) { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 207 | if (this == o) return true; |
| 208 | if (o == null || getClass() != o.getClass()) return false; |
| 209 | |
| 210 | InsetsSource that = (InsetsSource) o; |
| 211 | |
| 212 | if (mType != that.mType) return false; |
| 213 | if (mVisible != that.mVisible) return false; |
Jorim Jaggi | ed31259 | 2020-05-25 16:46:56 +0200 | [diff] [blame] | 214 | if (excludeInvisibleImeFrames && !mVisible && mType == ITYPE_IME) return true; |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 215 | if (!Objects.equals(mVisibleFrame, that.mVisibleFrame)) return false; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 216 | return mFrame.equals(that.mFrame); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public int hashCode() { |
| 221 | int result = mType; |
| 222 | result = 31 * result + mFrame.hashCode(); |
Jorim Jaggi | 4d4fae4 | 2020-01-30 23:10:12 +0100 | [diff] [blame] | 223 | result = 31 * result + (mVisibleFrame != null ? mVisibleFrame.hashCode() : 0); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 224 | result = 31 * result + (mVisible ? 1 : 0); |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | public InsetsSource(Parcel in) { |
| 229 | mType = in.readInt(); |
Jeff Sharkey | 1639e6b | 2020-04-22 17:36:53 -0600 | [diff] [blame] | 230 | if (in.readInt() != 0) { |
| 231 | mFrame = Rect.CREATOR.createFromParcel(in); |
| 232 | } else { |
| 233 | mFrame = null; |
| 234 | } |
| 235 | if (in.readInt() != 0) { |
| 236 | mVisibleFrame = Rect.CREATOR.createFromParcel(in); |
| 237 | } else { |
| 238 | mVisibleFrame = null; |
| 239 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 240 | mVisible = in.readBoolean(); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public int describeContents() { |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void writeToParcel(Parcel dest, int flags) { |
| 250 | dest.writeInt(mType); |
Jeff Sharkey | 1639e6b | 2020-04-22 17:36:53 -0600 | [diff] [blame] | 251 | if (mFrame != null) { |
| 252 | dest.writeInt(1); |
| 253 | mFrame.writeToParcel(dest, 0); |
| 254 | } else { |
| 255 | dest.writeInt(0); |
| 256 | } |
| 257 | if (mVisibleFrame != null) { |
| 258 | dest.writeInt(1); |
| 259 | mVisibleFrame.writeToParcel(dest, 0); |
| 260 | } else { |
| 261 | dest.writeInt(0); |
| 262 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 263 | dest.writeBoolean(mVisible); |
| 264 | } |
| 265 | |
Yunfan Chen | b5d2db7 | 2019-12-06 15:43:43 +0900 | [diff] [blame] | 266 | @Override |
| 267 | public String toString() { |
| 268 | return "InsetsSource: {" |
| 269 | + "mType=" + InsetsState.typeToString(mType) |
| 270 | + ", mFrame=" + mFrame.toShortString() |
Tiger Huang | a1663403 | 2020-02-05 17:10:03 +0800 | [diff] [blame] | 271 | + ", mVisible=" + mVisible |
Yunfan Chen | b5d2db7 | 2019-12-06 15:43:43 +0900 | [diff] [blame] | 272 | + "}"; |
| 273 | } |
| 274 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 275 | public static final @android.annotation.NonNull Creator<InsetsSource> CREATOR = new Creator<InsetsSource>() { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 276 | |
| 277 | public InsetsSource createFromParcel(Parcel in) { |
| 278 | return new InsetsSource(in); |
| 279 | } |
| 280 | |
| 281 | public InsetsSource[] newArray(int size) { |
| 282 | return new InsetsSource[size]; |
| 283 | } |
| 284 | }; |
| 285 | } |