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 | |
Jorim Jaggi | 23a02a9 | 2020-01-29 15:30:36 +0100 | [diff] [blame] | 19 | import static android.view.InsetsState.ITYPE_IME; |
| 20 | |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 22 | import android.graphics.Insets; |
| 23 | import android.graphics.Rect; |
| 24 | import android.os.Parcel; |
| 25 | import android.os.Parcelable; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 26 | import android.view.InsetsState.InternalInsetsType; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 27 | |
| 28 | import java.io.PrintWriter; |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 29 | import java.util.Objects; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Represents the state of a single window generating insets for clients. |
| 33 | * @hide |
| 34 | */ |
| 35 | public class InsetsSource implements Parcelable { |
| 36 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 37 | private final @InternalInsetsType int mType; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 38 | |
| 39 | /** Frame of the source in screen coordinate space */ |
| 40 | private final Rect mFrame; |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 41 | private @Nullable Rect mVisibleFrame; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 42 | private boolean mVisible; |
| 43 | |
| 44 | private final Rect mTmpFrame = new Rect(); |
| 45 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 46 | public InsetsSource(@InternalInsetsType int type) { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 47 | mType = type; |
| 48 | mFrame = new Rect(); |
Jorim Jaggi | 0dd0cf9 | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 49 | mVisible = InsetsState.getDefaultVisibility(type); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | public InsetsSource(InsetsSource other) { |
| 53 | mType = other.mType; |
| 54 | mFrame = new Rect(other.mFrame); |
| 55 | mVisible = other.mVisible; |
| 56 | } |
| 57 | |
| 58 | public void setFrame(Rect frame) { |
| 59 | mFrame.set(frame); |
| 60 | } |
| 61 | |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 62 | public void setVisibleFrame(@Nullable Rect visibleFrame) { |
| 63 | mVisibleFrame = visibleFrame != null ? new Rect(visibleFrame) : visibleFrame; |
| 64 | } |
| 65 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 66 | public void setVisible(boolean visible) { |
| 67 | mVisible = visible; |
| 68 | } |
| 69 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 70 | public @InternalInsetsType int getType() { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 71 | return mType; |
| 72 | } |
| 73 | |
| 74 | public Rect getFrame() { |
| 75 | return mFrame; |
| 76 | } |
| 77 | |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 78 | public @Nullable Rect getVisibleFrame() { |
| 79 | return mVisibleFrame; |
| 80 | } |
| 81 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 82 | public boolean isVisible() { |
| 83 | return mVisible; |
| 84 | } |
| 85 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 86 | /** |
| 87 | * Calculates the insets this source will cause to a client window. |
| 88 | * |
| 89 | * @param relativeFrame The frame to calculate the insets relative to. |
| 90 | * @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] | 91 | * @return The resulting insets. The contract is that only one side will be occupied by a |
| 92 | * source. |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 93 | */ |
| 94 | public Insets calculateInsets(Rect relativeFrame, boolean ignoreVisibility) { |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 95 | return calculateInsets(relativeFrame, mFrame, ignoreVisibility); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Like {@link #calculateInsets(Rect, boolean)}, but will return visible insets. |
| 100 | */ |
| 101 | public Insets calculateVisibleInsets(Rect relativeFrame) { |
| 102 | return calculateInsets(relativeFrame, mVisibleFrame != null ? mVisibleFrame : mFrame, |
| 103 | false /* ignoreVisibility */); |
| 104 | } |
| 105 | |
| 106 | private Insets calculateInsets(Rect relativeFrame, Rect frame, boolean ignoreVisibility) { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 107 | if (!ignoreVisibility && !mVisible) { |
| 108 | return Insets.NONE; |
| 109 | } |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 110 | if (!mTmpFrame.setIntersect(frame, relativeFrame)) { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 111 | return Insets.NONE; |
| 112 | } |
| 113 | |
Jorim Jaggi | 23a02a9 | 2020-01-29 15:30:36 +0100 | [diff] [blame] | 114 | // TODO: Currently, non-floating IME always intersects at bottom due to issues with cutout. |
| 115 | // However, we should let the policy decide from the server. |
| 116 | if (getType() == ITYPE_IME) { |
| 117 | return Insets.of(0, 0, 0, mTmpFrame.height()); |
| 118 | } |
| 119 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 120 | // Intersecting at top/bottom |
| 121 | if (mTmpFrame.width() == relativeFrame.width()) { |
| 122 | if (mTmpFrame.top == relativeFrame.top) { |
| 123 | return Insets.of(0, mTmpFrame.height(), 0, 0); |
| 124 | } else { |
| 125 | return Insets.of(0, 0, 0, mTmpFrame.height()); |
| 126 | } |
| 127 | } |
| 128 | // Intersecting at left/right |
| 129 | else if (mTmpFrame.height() == relativeFrame.height()) { |
| 130 | if (mTmpFrame.left == relativeFrame.left) { |
| 131 | return Insets.of(mTmpFrame.width(), 0, 0, 0); |
| 132 | } else { |
| 133 | return Insets.of(0, 0, mTmpFrame.width(), 0); |
| 134 | } |
| 135 | } else { |
| 136 | return Insets.NONE; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | public void dump(String prefix, PrintWriter pw) { |
| 141 | pw.print(prefix); |
| 142 | pw.print("InsetsSource type="); pw.print(InsetsState.typeToString(mType)); |
| 143 | pw.print(" frame="); pw.print(mFrame.toShortString()); |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 144 | if (mVisibleFrame != null) { |
| 145 | pw.print(" visibleFrmae="); pw.print(mVisibleFrame.toShortString()); |
| 146 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 147 | pw.print(" visible="); pw.print(mVisible); |
| 148 | pw.println(); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean equals(Object o) { |
| 153 | if (this == o) return true; |
| 154 | if (o == null || getClass() != o.getClass()) return false; |
| 155 | |
| 156 | InsetsSource that = (InsetsSource) o; |
| 157 | |
| 158 | if (mType != that.mType) return false; |
| 159 | if (mVisible != that.mVisible) return false; |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 160 | if (!Objects.equals(mVisibleFrame, that.mVisibleFrame)) return false; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 161 | return mFrame.equals(that.mFrame); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public int hashCode() { |
| 166 | int result = mType; |
| 167 | result = 31 * result + mFrame.hashCode(); |
| 168 | result = 31 * result + (mVisible ? 1 : 0); |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | public InsetsSource(Parcel in) { |
| 173 | mType = in.readInt(); |
| 174 | mFrame = in.readParcelable(null /* loader */); |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 175 | mVisibleFrame = in.readParcelable(null /* loader */); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 176 | mVisible = in.readBoolean(); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public int describeContents() { |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void writeToParcel(Parcel dest, int flags) { |
| 186 | dest.writeInt(mType); |
| 187 | dest.writeParcelable(mFrame, 0 /* flags*/); |
Jorim Jaggi | 4e04eb2 | 2020-01-09 16:42:14 +0100 | [diff] [blame] | 188 | dest.writeParcelable(mVisibleFrame, 0 /* flags */); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 189 | dest.writeBoolean(mVisible); |
| 190 | } |
| 191 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 192 | public static final @android.annotation.NonNull Creator<InsetsSource> CREATOR = new Creator<InsetsSource>() { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 193 | |
| 194 | public InsetsSource createFromParcel(Parcel in) { |
| 195 | return new InsetsSource(in); |
| 196 | } |
| 197 | |
| 198 | public InsetsSource[] newArray(int size) { |
| 199 | return new InsetsSource[size]; |
| 200 | } |
| 201 | }; |
| 202 | } |