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