Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 19 | import static android.view.DisplayCutoutProto.BOUNDS; |
| 20 | import static android.view.DisplayCutoutProto.INSETS; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 21 | import static android.view.Surface.ROTATION_0; |
| 22 | import static android.view.Surface.ROTATION_180; |
| 23 | import static android.view.Surface.ROTATION_270; |
| 24 | import static android.view.Surface.ROTATION_90; |
| 25 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame^] | 26 | import android.content.res.Resources; |
| 27 | import android.graphics.Matrix; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 28 | import android.graphics.Path; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 29 | import android.graphics.Point; |
| 30 | import android.graphics.Rect; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 31 | import android.graphics.RectF; |
| 32 | import android.graphics.Region; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 33 | import android.os.Parcel; |
| 34 | import android.os.Parcelable; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame^] | 35 | import android.text.TextUtils; |
| 36 | import android.util.Log; |
| 37 | import android.util.PathParser; |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 38 | import android.util.proto.ProtoOutputStream; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 39 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame^] | 40 | import com.android.internal.R; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 41 | import com.android.internal.annotations.VisibleForTesting; |
| 42 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 43 | import java.util.List; |
| 44 | |
| 45 | /** |
| 46 | * Represents a part of the display that is not functional for displaying content. |
| 47 | * |
| 48 | * <p>{@code DisplayCutout} is immutable. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 49 | */ |
| 50 | public final class DisplayCutout { |
| 51 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame^] | 52 | private static final String TAG = "DisplayCutout"; |
| 53 | private static final String DP_MARKER = "@dp"; |
| 54 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 55 | private static final Rect ZERO_RECT = new Rect(); |
| 56 | private static final Region EMPTY_REGION = new Region(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 57 | |
| 58 | /** |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 59 | * An instance where {@link #isEmpty()} returns {@code true}. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 60 | * |
| 61 | * @hide |
| 62 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 63 | public static final DisplayCutout NO_CUTOUT = new DisplayCutout(ZERO_RECT, EMPTY_REGION); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 64 | |
| 65 | private final Rect mSafeInsets; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 66 | private final Region mBounds; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Creates a DisplayCutout instance. |
| 70 | * |
| 71 | * NOTE: the Rects passed into this instance are not copied and MUST remain unchanged. |
| 72 | * |
| 73 | * @hide |
| 74 | */ |
| 75 | @VisibleForTesting |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 76 | public DisplayCutout(Rect safeInsets, Region bounds) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 77 | mSafeInsets = safeInsets != null ? safeInsets : ZERO_RECT; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 78 | mBounds = bounds != null ? bounds : Region.obtain(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 82 | * Returns true if there is no cutout or it is outside of the content view. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 83 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 84 | * @hide |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 85 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 86 | public boolean isEmpty() { |
| 87 | return mSafeInsets.equals(ZERO_RECT); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** Returns the inset from the top which avoids the display cutout. */ |
| 91 | public int getSafeInsetTop() { |
| 92 | return mSafeInsets.top; |
| 93 | } |
| 94 | |
| 95 | /** Returns the inset from the bottom which avoids the display cutout. */ |
| 96 | public int getSafeInsetBottom() { |
| 97 | return mSafeInsets.bottom; |
| 98 | } |
| 99 | |
| 100 | /** Returns the inset from the left which avoids the display cutout. */ |
| 101 | public int getSafeInsetLeft() { |
| 102 | return mSafeInsets.left; |
| 103 | } |
| 104 | |
| 105 | /** Returns the inset from the right which avoids the display cutout. */ |
| 106 | public int getSafeInsetRight() { |
| 107 | return mSafeInsets.right; |
| 108 | } |
| 109 | |
| 110 | /** |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 111 | * Returns the safe insets in a rect. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 112 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 113 | * @return a rect which is set to the safe insets. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 114 | * @hide |
| 115 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 116 | public Rect getSafeInsets() { |
| 117 | return new Rect(mSafeInsets); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 121 | * Returns the bounding region of the cutout. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 122 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 123 | * @return the bounding region of the cutout. Coordinates are relative |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 124 | * to the top-left corner of the content view. |
| 125 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 126 | public Region getBounds() { |
| 127 | return Region.obtain(mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 131 | * Returns the bounding rect of the cutout. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 132 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 133 | * @return the bounding rect of the cutout. Coordinates are relative |
| 134 | * to the top-left corner of the content view. |
| 135 | * @hide |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 136 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 137 | public Rect getBoundingRect() { |
| 138 | // TODO(roosa): Inline. |
| 139 | return mBounds.getBounds(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int hashCode() { |
| 144 | int result = mSafeInsets.hashCode(); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 145 | result = result * 31 + mBounds.getBounds().hashCode(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 146 | return result; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean equals(Object o) { |
| 151 | if (o == this) { |
| 152 | return true; |
| 153 | } |
| 154 | if (o instanceof DisplayCutout) { |
| 155 | DisplayCutout c = (DisplayCutout) o; |
| 156 | return mSafeInsets.equals(c.mSafeInsets) |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 157 | && mBounds.equals(c.mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public String toString() { |
| 164 | return "DisplayCutout{insets=" + mSafeInsets |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 165 | + " boundingRect=" + getBoundingRect() |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 166 | + "}"; |
| 167 | } |
| 168 | |
| 169 | /** |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 170 | * @hide |
| 171 | */ |
| 172 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 173 | final long token = proto.start(fieldId); |
| 174 | mSafeInsets.writeToProto(proto, INSETS); |
| 175 | mBounds.getBounds().writeToProto(proto, BOUNDS); |
| 176 | proto.end(token); |
| 177 | } |
| 178 | |
| 179 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 180 | * Insets the reference frame of the cutout in the given directions. |
| 181 | * |
| 182 | * @return a copy of this instance which has been inset |
| 183 | * @hide |
| 184 | */ |
| 185 | public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 186 | if (mBounds.isEmpty() |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 187 | || insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0) { |
| 188 | return this; |
| 189 | } |
| 190 | |
| 191 | Rect safeInsets = new Rect(mSafeInsets); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 192 | Region bounds = Region.obtain(mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 193 | |
| 194 | // Note: it's not really well defined what happens when the inset is negative, because we |
| 195 | // don't know if the safe inset needs to expand in general. |
| 196 | if (insetTop > 0 || safeInsets.top > 0) { |
| 197 | safeInsets.top = atLeastZero(safeInsets.top - insetTop); |
| 198 | } |
| 199 | if (insetBottom > 0 || safeInsets.bottom > 0) { |
| 200 | safeInsets.bottom = atLeastZero(safeInsets.bottom - insetBottom); |
| 201 | } |
| 202 | if (insetLeft > 0 || safeInsets.left > 0) { |
| 203 | safeInsets.left = atLeastZero(safeInsets.left - insetLeft); |
| 204 | } |
| 205 | if (insetRight > 0 || safeInsets.right > 0) { |
| 206 | safeInsets.right = atLeastZero(safeInsets.right - insetRight); |
| 207 | } |
| 208 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 209 | bounds.translate(-insetLeft, -insetTop); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 210 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 211 | return new DisplayCutout(safeInsets, bounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Calculates the safe insets relative to the given reference frame. |
| 216 | * |
| 217 | * @return a copy of this instance with the safe insets calculated |
| 218 | * @hide |
| 219 | */ |
| 220 | public DisplayCutout calculateRelativeTo(Rect frame) { |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 221 | if (mBounds.isEmpty() || !Rect.intersects(frame, mBounds.getBounds())) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 222 | return NO_CUTOUT; |
| 223 | } |
| 224 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 225 | return DisplayCutout.calculateRelativeTo(frame, Region.obtain(mBounds)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 228 | private static DisplayCutout calculateRelativeTo(Rect frame, Region bounds) { |
| 229 | Rect boundingRect = bounds.getBounds(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 230 | Rect safeRect = new Rect(); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 231 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 232 | int bestArea = 0; |
| 233 | int bestVariant = 0; |
| 234 | for (int variant = ROTATION_0; variant <= ROTATION_270; variant++) { |
| 235 | int area = calculateInsetVariantArea(frame, boundingRect, variant, safeRect); |
| 236 | if (bestArea < area) { |
| 237 | bestArea = area; |
| 238 | bestVariant = variant; |
| 239 | } |
| 240 | } |
| 241 | calculateInsetVariantArea(frame, boundingRect, bestVariant, safeRect); |
| 242 | if (safeRect.isEmpty()) { |
| 243 | // The entire frame overlaps with the cutout. |
| 244 | safeRect.set(0, frame.height(), 0, 0); |
| 245 | } else { |
| 246 | // Convert safeRect to insets relative to frame. We're reusing the rect here to avoid |
| 247 | // an allocation. |
| 248 | safeRect.set( |
| 249 | Math.max(0, safeRect.left - frame.left), |
| 250 | Math.max(0, safeRect.top - frame.top), |
| 251 | Math.max(0, frame.right - safeRect.right), |
| 252 | Math.max(0, frame.bottom - safeRect.bottom)); |
| 253 | } |
| 254 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 255 | bounds.translate(-frame.left, -frame.top); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 256 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 257 | return new DisplayCutout(safeRect, bounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | private static int calculateInsetVariantArea(Rect frame, Rect boundingRect, int variant, |
| 261 | Rect outSafeRect) { |
| 262 | switch (variant) { |
| 263 | case ROTATION_0: |
| 264 | outSafeRect.set(frame.left, frame.top, frame.right, boundingRect.top); |
| 265 | break; |
| 266 | case ROTATION_90: |
| 267 | outSafeRect.set(frame.left, frame.top, boundingRect.left, frame.bottom); |
| 268 | break; |
| 269 | case ROTATION_180: |
| 270 | outSafeRect.set(frame.left, boundingRect.bottom, frame.right, frame.bottom); |
| 271 | break; |
| 272 | case ROTATION_270: |
| 273 | outSafeRect.set(boundingRect.right, frame.top, frame.right, frame.bottom); |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | return outSafeRect.isEmpty() ? 0 : outSafeRect.width() * outSafeRect.height(); |
| 278 | } |
| 279 | |
| 280 | private static int atLeastZero(int value) { |
| 281 | return value < 0 ? 0 : value; |
| 282 | } |
| 283 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * Creates an instance from a bounding polygon. |
| 287 | * |
| 288 | * @hide |
| 289 | */ |
| 290 | public static DisplayCutout fromBoundingPolygon(List<Point> points) { |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 291 | Path path = new Path(); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 292 | path.reset(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 293 | for (int i = 0; i < points.size(); i++) { |
| 294 | Point point = points.get(i); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 295 | if (i == 0) { |
| 296 | path.moveTo(point.x, point.y); |
| 297 | } else { |
| 298 | path.lineTo(point.x, point.y); |
| 299 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 300 | } |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 301 | path.close(); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 302 | return fromBounds(path); |
| 303 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 304 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 305 | /** |
| 306 | * Creates an instance from a bounding {@link Path}. |
| 307 | * |
| 308 | * @hide |
| 309 | */ |
| 310 | public static DisplayCutout fromBounds(Path path) { |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 311 | RectF clipRect = new RectF(); |
| 312 | path.computeBounds(clipRect, false /* unused */); |
| 313 | Region clipRegion = Region.obtain(); |
| 314 | clipRegion.set((int) clipRect.left, (int) clipRect.top, |
| 315 | (int) clipRect.right, (int) clipRect.bottom); |
| 316 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 317 | Region bounds = new Region(); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 318 | bounds.setPath(path, clipRegion); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 319 | clipRegion.recycle(); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 320 | return new DisplayCutout(ZERO_RECT, bounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame^] | 324 | * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout. |
| 325 | * |
| 326 | * @hide |
| 327 | */ |
| 328 | public static DisplayCutout fromResources(Resources res, int displayWidth) { |
| 329 | String spec = res.getString(R.string.config_mainBuiltInDisplayCutout); |
| 330 | if (TextUtils.isEmpty(spec)) { |
| 331 | return null; |
| 332 | } |
| 333 | spec = spec.trim(); |
| 334 | final boolean inDp = spec.endsWith(DP_MARKER); |
| 335 | if (inDp) { |
| 336 | spec = spec.substring(0, spec.length() - DP_MARKER.length()); |
| 337 | } |
| 338 | |
| 339 | Path p; |
| 340 | try { |
| 341 | p = PathParser.createPathFromPathData(spec); |
| 342 | } catch (Throwable e) { |
| 343 | Log.wtf(TAG, "Could not inflate cutout: ", e); |
| 344 | return null; |
| 345 | } |
| 346 | |
| 347 | final Matrix m = new Matrix(); |
| 348 | if (inDp) { |
| 349 | final float dpToPx = res.getDisplayMetrics().density; |
| 350 | m.postScale(dpToPx, dpToPx); |
| 351 | } |
| 352 | m.postTranslate(displayWidth / 2f, 0); |
| 353 | p.transform(m); |
| 354 | return fromBounds(p); |
| 355 | } |
| 356 | |
| 357 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 358 | * Helper class for passing {@link DisplayCutout} through binder. |
| 359 | * |
| 360 | * Needed, because {@code readFromParcel} cannot be used with immutable classes. |
| 361 | * |
| 362 | * @hide |
| 363 | */ |
| 364 | public static final class ParcelableWrapper implements Parcelable { |
| 365 | |
| 366 | private DisplayCutout mInner; |
| 367 | |
| 368 | public ParcelableWrapper() { |
| 369 | this(NO_CUTOUT); |
| 370 | } |
| 371 | |
| 372 | public ParcelableWrapper(DisplayCutout cutout) { |
| 373 | mInner = cutout; |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public int describeContents() { |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public void writeToParcel(Parcel out, int flags) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 383 | writeCutoutToParcel(mInner, out, flags); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Writes a DisplayCutout to a {@link Parcel}. |
| 388 | * |
| 389 | * @see #readCutoutFromParcel(Parcel) |
| 390 | */ |
| 391 | public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) { |
| 392 | if (cutout == null) { |
| 393 | out.writeInt(-1); |
| 394 | } else if (cutout == NO_CUTOUT) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 395 | out.writeInt(0); |
| 396 | } else { |
| 397 | out.writeInt(1); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 398 | out.writeTypedObject(cutout.mSafeInsets, flags); |
| 399 | out.writeTypedObject(cutout.mBounds, flags); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing |
| 405 | * instance. |
| 406 | * |
| 407 | * Needed for AIDL out parameters. |
| 408 | */ |
| 409 | public void readFromParcel(Parcel in) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 410 | mInner = readCutoutFromParcel(in); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | public static final Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() { |
| 414 | @Override |
| 415 | public ParcelableWrapper createFromParcel(Parcel in) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 416 | return new ParcelableWrapper(readCutoutFromParcel(in)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public ParcelableWrapper[] newArray(int size) { |
| 421 | return new ParcelableWrapper[size]; |
| 422 | } |
| 423 | }; |
| 424 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 425 | /** |
| 426 | * Reads a DisplayCutout from a {@link Parcel}. |
| 427 | * |
| 428 | * @see #writeCutoutToParcel(DisplayCutout, Parcel, int) |
| 429 | */ |
| 430 | public static DisplayCutout readCutoutFromParcel(Parcel in) { |
| 431 | int variant = in.readInt(); |
| 432 | if (variant == -1) { |
| 433 | return null; |
| 434 | } |
| 435 | if (variant == 0) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 436 | return NO_CUTOUT; |
| 437 | } |
| 438 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 439 | Rect safeInsets = in.readTypedObject(Rect.CREATOR); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 440 | Region bounds = in.readTypedObject(Region.CREATOR); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 441 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 442 | return new DisplayCutout(safeInsets, bounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | public DisplayCutout get() { |
| 446 | return mInner; |
| 447 | } |
| 448 | |
| 449 | public void set(ParcelableWrapper cutout) { |
| 450 | mInner = cutout.get(); |
| 451 | } |
| 452 | |
| 453 | public void set(DisplayCutout cutout) { |
| 454 | mInner = cutout; |
| 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public int hashCode() { |
| 459 | return mInner.hashCode(); |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | public boolean equals(Object o) { |
| 464 | return o instanceof ParcelableWrapper |
| 465 | && mInner.equals(((ParcelableWrapper) o).mInner); |
| 466 | } |
| 467 | |
| 468 | @Override |
| 469 | public String toString() { |
| 470 | return String.valueOf(mInner); |
| 471 | } |
| 472 | } |
| 473 | } |