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 | |
| 19 | import android.graphics.Insets; |
| 20 | import android.graphics.Rect; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 23 | import android.view.InsetsState.InternalInsetsType; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 24 | |
| 25 | import java.io.PrintWriter; |
| 26 | |
| 27 | /** |
| 28 | * Represents the state of a single window generating insets for clients. |
| 29 | * @hide |
| 30 | */ |
| 31 | public class InsetsSource implements Parcelable { |
| 32 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 33 | private final @InternalInsetsType int mType; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 34 | |
| 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 Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 41 | public InsetsSource(@InternalInsetsType int type) { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 42 | mType = type; |
| 43 | mFrame = new Rect(); |
Jorim Jaggi | 0dd0cf9 | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 44 | mVisible = InsetsState.getDefaultVisibility(type); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 45 | } |
| 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 Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 61 | public @InternalInsetsType int getType() { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 62 | return mType; |
| 63 | } |
| 64 | |
| 65 | public Rect getFrame() { |
| 66 | return mFrame; |
| 67 | } |
| 68 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 69 | public boolean isVisible() { |
| 70 | return mVisible; |
| 71 | } |
| 72 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 73 | /** |
| 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 Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 78 | * @return The resulting insets. The contract is that only one side will be occupied by a |
| 79 | * source. |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 80 | */ |
| 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 Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 155 | public static final @android.annotation.NonNull Creator<InsetsSource> CREATOR = new Creator<InsetsSource>() { |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 156 | |
| 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 | } |