blob: 797c128e514c463c7afe79d8d2814436db18ea0e [file] [log] [blame]
Adrian Roosd4970af2017-11-10 15:48:01 +01001/*
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
17package android.view;
18
Adrian Roos535c4202018-04-16 16:12:40 +020019import static android.util.DisplayMetrics.DENSITY_DEFAULT;
20import static android.util.DisplayMetrics.DENSITY_DEVICE_STABLE;
Issei Suzuki43190bd2018-08-20 17:28:41 +020021import static android.view.DisplayCutoutProto.BOUND_BOTTOM;
22import static android.view.DisplayCutoutProto.BOUND_LEFT;
23import static android.view.DisplayCutoutProto.BOUND_RIGHT;
24import static android.view.DisplayCutoutProto.BOUND_TOP;
Vishnu Nair1d0fa072018-01-04 07:53:00 -080025import static android.view.DisplayCutoutProto.INSETS;
Adrian Roosd4970af2017-11-10 15:48:01 +010026
Adrian Roos8d13bf12018-02-21 15:17:07 +010027import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
28
Issei Suzuki43190bd2018-08-20 17:28:41 +020029import android.annotation.IntDef;
30import android.annotation.NonNull;
31import android.annotation.Nullable;
Adrian Roos16693f32018-01-18 17:45:52 +010032import android.content.res.Resources;
Issei Suzuki43190bd2018-08-20 17:28:41 +020033import android.graphics.Insets;
Adrian Roos16693f32018-01-18 17:45:52 +010034import android.graphics.Matrix;
Adrian Roosd07bafd2017-12-11 17:30:56 +010035import android.graphics.Path;
Adrian Roosd4970af2017-11-10 15:48:01 +010036import android.graphics.Rect;
Adrian Roosd07bafd2017-12-11 17:30:56 +010037import android.graphics.RectF;
38import android.graphics.Region;
Jorim Jaggi60640512018-06-29 01:14:31 +020039import android.graphics.Region.Op;
Adrian Roosd4970af2017-11-10 15:48:01 +010040import android.os.Parcel;
41import android.os.Parcelable;
Adrian Roos16693f32018-01-18 17:45:52 +010042import android.text.TextUtils;
43import android.util.Log;
Adrian Roos51072a82018-04-10 15:17:08 -070044import android.util.Pair;
Adrian Roos16693f32018-01-18 17:45:52 +010045import android.util.PathParser;
Vishnu Nair1d0fa072018-01-04 07:53:00 -080046import android.util.proto.ProtoOutputStream;
Adrian Roosd4970af2017-11-10 15:48:01 +010047
Adrian Roos16693f32018-01-18 17:45:52 +010048import com.android.internal.R;
Adrian Roos8d13bf12018-02-21 15:17:07 +010049import com.android.internal.annotations.GuardedBy;
Adrian Roosd4970af2017-11-10 15:48:01 +010050import com.android.internal.annotations.VisibleForTesting;
51
Issei Suzuki43190bd2018-08-20 17:28:41 +020052import java.lang.annotation.Retention;
53import java.lang.annotation.RetentionPolicy;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010054import java.util.ArrayList;
Issei Suzuki43190bd2018-08-20 17:28:41 +020055import java.util.Arrays;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010056import java.util.List;
Adrian Roosd4970af2017-11-10 15:48:01 +010057
58/**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010059 * Represents the area of the display that is not functional for displaying content.
Adrian Roosd4970af2017-11-10 15:48:01 +010060 *
61 * <p>{@code DisplayCutout} is immutable.
Adrian Roosd4970af2017-11-10 15:48:01 +010062 */
63public final class DisplayCutout {
64
Adrian Roos16693f32018-01-18 17:45:52 +010065 private static final String TAG = "DisplayCutout";
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010066 private static final String BOTTOM_MARKER = "@bottom";
Adrian Roos16693f32018-01-18 17:45:52 +010067 private static final String DP_MARKER = "@dp";
Adrian Roosb8b10f82018-03-15 20:09:42 +010068 private static final String RIGHT_MARKER = "@right";
Adrian Roos16693f32018-01-18 17:45:52 +010069
Adrian Roosc84df772018-01-19 21:20:22 +010070 /**
71 * Category for overlays that allow emulating a display cutout on devices that don't have
72 * one.
73 *
74 * @see android.content.om.IOverlayManager
75 * @hide
76 */
77 public static final String EMULATION_OVERLAY_CATEGORY =
78 "com.android.internal.display_cutout_emulation";
79
Adrian Roosd07bafd2017-12-11 17:30:56 +010080 private static final Rect ZERO_RECT = new Rect();
Adrian Roosd4970af2017-11-10 15:48:01 +010081
82 /**
Adrian Roosd07bafd2017-12-11 17:30:56 +010083 * An instance where {@link #isEmpty()} returns {@code true}.
Adrian Roosd4970af2017-11-10 15:48:01 +010084 *
85 * @hide
86 */
Issei Suzuki43190bd2018-08-20 17:28:41 +020087 public static final DisplayCutout NO_CUTOUT = new DisplayCutout(
88 ZERO_RECT, ZERO_RECT, ZERO_RECT, ZERO_RECT, ZERO_RECT,
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010089 false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +010090
Adrian Roos8d13bf12018-02-21 15:17:07 +010091
Adrian Roos51072a82018-04-10 15:17:08 -070092 private static final Pair<Path, DisplayCutout> NULL_PAIR = new Pair<>(null, null);
Adrian Roos8d13bf12018-02-21 15:17:07 +010093 private static final Object CACHE_LOCK = new Object();
Adrian Roos51072a82018-04-10 15:17:08 -070094
Adrian Roos8d13bf12018-02-21 15:17:07 +010095 @GuardedBy("CACHE_LOCK")
96 private static String sCachedSpec;
97 @GuardedBy("CACHE_LOCK")
98 private static int sCachedDisplayWidth;
99 @GuardedBy("CACHE_LOCK")
Adrian Roos51072a82018-04-10 15:17:08 -0700100 private static int sCachedDisplayHeight;
101 @GuardedBy("CACHE_LOCK")
Adrian Roos8d13bf12018-02-21 15:17:07 +0100102 private static float sCachedDensity;
103 @GuardedBy("CACHE_LOCK")
Adrian Roos51072a82018-04-10 15:17:08 -0700104 private static Pair<Path, DisplayCutout> sCachedCutout = NULL_PAIR;
Adrian Roos8d13bf12018-02-21 15:17:07 +0100105
Adrian Roosd4970af2017-11-10 15:48:01 +0100106 private final Rect mSafeInsets;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200107
108
109 /**
110 * The bound is at the left of the screen.
111 * @hide
112 */
113 public static final int BOUNDS_POSITION_LEFT = 0;
114
115 /**
116 * The bound is at the top of the screen.
117 * @hide
118 */
119 public static final int BOUNDS_POSITION_TOP = 1;
120
121 /**
122 * The bound is at the right of the screen.
123 * @hide
124 */
125 public static final int BOUNDS_POSITION_RIGHT = 2;
126
127 /**
128 * The bound is at the bottom of the screen.
129 * @hide
130 */
131 public static final int BOUNDS_POSITION_BOTTOM = 3;
132
133 /**
134 * The number of possible positions at which bounds can be located.
135 * @hide
136 */
137 public static final int BOUNDS_POSITION_LENGTH = 4;
138
139 /** @hide */
140 @IntDef(prefix = { "BOUNDS_POSITION_" }, value = {
141 BOUNDS_POSITION_LEFT,
142 BOUNDS_POSITION_TOP,
143 BOUNDS_POSITION_RIGHT,
144 BOUNDS_POSITION_BOTTOM
145 })
146 @Retention(RetentionPolicy.SOURCE)
147 public @interface BoundsPosition {}
148
149 private static class Bounds {
150 private final Rect[] mRects;
151
152 private Bounds(Rect left, Rect top, Rect right, Rect bottom, boolean copyArguments) {
153 mRects = new Rect[BOUNDS_POSITION_LENGTH];
154 mRects[BOUNDS_POSITION_LEFT] = getCopyOrRef(left, copyArguments);
155 mRects[BOUNDS_POSITION_TOP] = getCopyOrRef(top, copyArguments);
156 mRects[BOUNDS_POSITION_RIGHT] = getCopyOrRef(right, copyArguments);
157 mRects[BOUNDS_POSITION_BOTTOM] = getCopyOrRef(bottom, copyArguments);
158
159 }
160
161 private Bounds(Rect[] rects, boolean copyArguments) {
162 if (rects.length != BOUNDS_POSITION_LENGTH) {
163 throw new IllegalArgumentException(
164 "rects must have exactly 4 elements: rects=" + Arrays.toString(rects));
165 }
166 if (copyArguments) {
167 mRects = new Rect[BOUNDS_POSITION_LENGTH];
168 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) {
169 mRects[i] = new Rect(rects[i]);
170 }
171 } else {
172 for (Rect rect : rects) {
173 if (rect == null) {
174 throw new IllegalArgumentException(
175 "rects must have non-null elements: rects="
176 + Arrays.toString(rects));
177 }
178 }
179 mRects = rects;
180 }
181 }
182
183 private boolean isEmpty() {
184 for (Rect rect : mRects) {
185 if (!rect.isEmpty()) {
186 return false;
187 }
188 }
189 return true;
190 }
191
192 private Rect getRect(@BoundsPosition int pos) {
193 return new Rect(mRects[pos]);
194 }
195
196 private Rect[] getRects() {
197 Rect[] rects = new Rect[BOUNDS_POSITION_LENGTH];
198 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) {
199 rects[i] = new Rect(mRects[i]);
200 }
201 return rects;
202 }
203
204 @Override
205 public int hashCode() {
206 int result = 0;
207 for (Rect rect : mRects) {
208 result = result * 48271 + rect.hashCode();
209 }
210 return result;
211 }
212 @Override
213 public boolean equals(Object o) {
214 if (o == this) {
215 return true;
216 }
217 if (o instanceof Bounds) {
218 Bounds b = (Bounds) o;
219 return Arrays.deepEquals(mRects, b.mRects);
220 }
221 return false;
222 }
223
224 @Override
225 public String toString() {
226 return "Bounds=" + Arrays.toString(mRects);
227 }
228
229 }
230
231 private final Bounds mBounds;
232
233 /**
234 * Creates a DisplayCutout instance.
235 *
Issei Suzuki8b0e6172019-01-14 17:04:33 +0100236 * <p>Note that this is only useful for tests. For production code, developers should always
237 * use a {@link DisplayCutout} obtained from the system.</p>
238 *
Issei Suzuki43190bd2018-08-20 17:28:41 +0200239 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
240 * {@link #getSafeInsetTop()} etc.
241 * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed,
242 * it's treated as an empty rectangle (0,0)-(0,0).
243 * @param boundTop the top bounding rect of the display cutout in pixels. If null is passed,
244 * it's treated as an empty rectangle (0,0)-(0,0).
245 * @param boundRight the right bounding rect of the display cutout in pixels. If null is
246 * passed, it's treated as an empty rectangle (0,0)-(0,0).
247 * @param boundBottom the bottom bounding rect of the display cutout in pixels. If null is
248 * passed, it's treated as an empty rectangle (0,0)-(0,0).
249 */
250 // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE)
Adrian Roosde363d02018-10-12 13:03:56 +0200251 public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft,
252 @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200253 this(safeInsets.toRect(), boundLeft, boundTop, boundRight, boundBottom, true);
254 }
Adrian Roos9bbd9662018-03-02 14:31:37 +0100255
256 /**
257 * Creates a DisplayCutout instance.
258 *
Issei Suzuki8b0e6172019-01-14 17:04:33 +0100259 * <p>Note that this is only useful for tests. For production code, developers should always
260 * use a {@link DisplayCutout} obtained from the system.</p>
261 *
Adrian Roos9bbd9662018-03-02 14:31:37 +0100262 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
263 * {@link #getSafeInsetTop()} etc.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100264 * @param boundingRects the bounding rects of the display cutouts as returned by
265 * {@link #getBoundingRects()} ()}.
Issei Suzuki43190bd2018-08-20 17:28:41 +0200266 * @deprecated Use {@link DisplayCutout#DisplayCutout(Insets, Rect, Rect, Rect, Rect)} instead.
Adrian Roos9bbd9662018-03-02 14:31:37 +0100267 */
268 // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE)
Issei Suzuki43190bd2018-08-20 17:28:41 +0200269 @Deprecated
Adrian Roosde363d02018-10-12 13:03:56 +0200270 public DisplayCutout(@Nullable Rect safeInsets, @Nullable List<Rect> boundingRects) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200271 this(safeInsets, extractBoundsFromList(safeInsets, boundingRects),
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100272 true /* copyArguments */);
Adrian Roos9bbd9662018-03-02 14:31:37 +0100273 }
Adrian Roosd4970af2017-11-10 15:48:01 +0100274
275 /**
276 * Creates a DisplayCutout instance.
277 *
Issei Suzuki43190bd2018-08-20 17:28:41 +0200278 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
279 * {@link #getSafeInsetTop()} etc.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100280 * @param copyArguments if true, create a copy of the arguments. If false, the passed arguments
281 * are not copied and MUST remain unchanged forever.
Adrian Roosd4970af2017-11-10 15:48:01 +0100282 */
Issei Suzuki43190bd2018-08-20 17:28:41 +0200283 private DisplayCutout(Rect safeInsets, Rect boundLeft, Rect boundTop, Rect boundRight,
284 Rect boundBottom, boolean copyArguments) {
285 mSafeInsets = getCopyOrRef(safeInsets, copyArguments);
286 mBounds = new Bounds(boundLeft, boundTop, boundRight, boundBottom, copyArguments);
287 }
288
289 private DisplayCutout(Rect safeInsets, Rect[] bounds, boolean copyArguments) {
290 mSafeInsets = getCopyOrRef(safeInsets, copyArguments);
291 mBounds = new Bounds(bounds, copyArguments);
292 }
293
294 private DisplayCutout(Rect safeInsets, Bounds bounds) {
295 mSafeInsets = safeInsets;
296 mBounds = bounds;
297
298 }
299
300 private static Rect getCopyOrRef(Rect r, boolean copyArguments) {
301 if (r == null) {
302 return ZERO_RECT;
303 } else if (copyArguments) {
304 return new Rect(r);
305 } else {
306 return r;
307 }
308 }
309
310 /**
311 * Find the position of the bounding rect, and create an array of Rect whose index represents
312 * the position (= BoundsPosition).
313 *
314 * @hide
315 */
316 public static Rect[] extractBoundsFromList(Rect safeInsets, List<Rect> boundingRects) {
317 Rect[] sortedBounds = new Rect[BOUNDS_POSITION_LENGTH];
318 for (int i = 0; i < sortedBounds.length; ++i) {
319 sortedBounds[i] = ZERO_RECT;
320 }
Adrian Roosde363d02018-10-12 13:03:56 +0200321 if (safeInsets != null && boundingRects != null) {
Issei Suzuki6b5f5e92019-07-15 15:44:01 +0200322 // There is at most one non-functional area per short edge of the device, but none
323 // on the long edges, so either a) safeInsets.top and safeInsets.bottom is 0, or
324 // b) safeInsets.left and safeInset.right is 0.
325 final boolean topBottomInset = safeInsets.top > 0 || safeInsets.bottom > 0;
Adrian Roosde363d02018-10-12 13:03:56 +0200326 for (Rect bound : boundingRects) {
Issei Suzuki6b5f5e92019-07-15 15:44:01 +0200327 if (topBottomInset) {
328 if (bound.top == 0) {
329 sortedBounds[BOUNDS_POSITION_TOP] = bound;
330 } else {
331 sortedBounds[BOUNDS_POSITION_BOTTOM] = bound;
332 }
333 } else {
334 if (bound.left == 0) {
335 sortedBounds[BOUNDS_POSITION_LEFT] = bound;
336 } else {
337 sortedBounds[BOUNDS_POSITION_RIGHT] = bound;
338 }
Adrian Roosde363d02018-10-12 13:03:56 +0200339 }
Issei Suzuki43190bd2018-08-20 17:28:41 +0200340 }
341 }
342 return sortedBounds;
343 }
344
345 /**
346 * Returns true if there is no cutout, i.e. the bounds are empty.
347 *
348 * @hide
349 */
350 public boolean isBoundsEmpty() {
351 return mBounds.isEmpty();
Adrian Roosd4970af2017-11-10 15:48:01 +0100352 }
353
354 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100355 * Returns true if the safe insets are empty (and therefore the current view does not
356 * overlap with the cutout or cutout area).
Adrian Roosd4970af2017-11-10 15:48:01 +0100357 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100358 * @hide
Adrian Roosd4970af2017-11-10 15:48:01 +0100359 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100360 public boolean isEmpty() {
361 return mSafeInsets.equals(ZERO_RECT);
Adrian Roosd4970af2017-11-10 15:48:01 +0100362 }
363
Adrian Roos9bbd9662018-03-02 14:31:37 +0100364 /** Returns the inset from the top which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100365 public int getSafeInsetTop() {
366 return mSafeInsets.top;
367 }
368
Adrian Roos9bbd9662018-03-02 14:31:37 +0100369 /** Returns the inset from the bottom which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100370 public int getSafeInsetBottom() {
371 return mSafeInsets.bottom;
372 }
373
Adrian Roos9bbd9662018-03-02 14:31:37 +0100374 /** Returns the inset from the left which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100375 public int getSafeInsetLeft() {
376 return mSafeInsets.left;
377 }
378
Adrian Roos9bbd9662018-03-02 14:31:37 +0100379 /** Returns the inset from the right which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100380 public int getSafeInsetRight() {
381 return mSafeInsets.right;
382 }
383
384 /**
Adrian Roos9bbd9662018-03-02 14:31:37 +0100385 * Returns the safe insets in a rect in pixel units.
Adrian Roosd4970af2017-11-10 15:48:01 +0100386 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100387 * @return a rect which is set to the safe insets.
Adrian Roosd4970af2017-11-10 15:48:01 +0100388 * @hide
389 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100390 public Rect getSafeInsets() {
391 return new Rect(mSafeInsets);
Adrian Roosd4970af2017-11-10 15:48:01 +0100392 }
393
394 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100395 * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional
396 * area on the display.
Adrian Roosd4970af2017-11-10 15:48:01 +0100397 *
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100398 * There will be at most one non-functional area per short edge of the device, and none on
399 * the long edges.
400 *
Issei Suzuki43190bd2018-08-20 17:28:41 +0200401 * @return a list of bounding {@code Rect}s, one for each display cutout area. No empty Rect is
402 * returned.
Adrian Roosd4970af2017-11-10 15:48:01 +0100403 */
Adrian Roosde363d02018-10-12 13:03:56 +0200404 @NonNull
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100405 public List<Rect> getBoundingRects() {
406 List<Rect> result = new ArrayList<>();
Issei Suzuki43190bd2018-08-20 17:28:41 +0200407 for (Rect bound : getBoundingRectsAll()) {
408 if (!bound.isEmpty()) {
409 result.add(new Rect(bound));
410 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100411 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100412 return result;
Adrian Roosd4970af2017-11-10 15:48:01 +0100413 }
414
Issei Suzuki43190bd2018-08-20 17:28:41 +0200415 /**
416 * Returns an array of {@code Rect}s, each of which is the bounding rectangle for a non-
417 * functional area on the display. Ordinal value of BoundPosition is used as an index of
418 * the array.
419 *
420 * There will be at most one non-functional area per short edge of the device, and none on
421 * the long edges.
422 *
423 * @return an array of bounding {@code Rect}s, one for each display cutout area. This might
424 * contain ZERO_RECT, which means there is no cutout area at the position.
425 *
426 * @hide
427 */
428 public Rect[] getBoundingRectsAll() {
429 return mBounds.getRects();
430 }
431
432 /**
433 * Returns a bounding rectangle for a non-functional area on the display which is located on
434 * the left of the screen.
435 *
436 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
437 * is returned.
438 */
439 public @NonNull Rect getBoundingRectLeft() {
440 return mBounds.getRect(BOUNDS_POSITION_LEFT);
441 }
442
443 /**
444 * Returns a bounding rectangle for a non-functional area on the display which is located on
445 * the top of the screen.
446 *
447 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
448 * is returned.
449 */
450 public @NonNull Rect getBoundingRectTop() {
451 return mBounds.getRect(BOUNDS_POSITION_TOP);
452 }
453
454 /**
455 * Returns a bounding rectangle for a non-functional area on the display which is located on
456 * the right of the screen.
457 *
458 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
459 * is returned.
460 */
461 public @NonNull Rect getBoundingRectRight() {
462 return mBounds.getRect(BOUNDS_POSITION_RIGHT);
463 }
464
465 /**
466 * Returns a bounding rectangle for a non-functional area on the display which is located on
467 * the bottom of the screen.
468 *
469 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
470 * is returned.
471 */
472 public @NonNull Rect getBoundingRectBottom() {
473 return mBounds.getRect(BOUNDS_POSITION_BOTTOM);
474 }
475
Adrian Roosd4970af2017-11-10 15:48:01 +0100476 @Override
477 public int hashCode() {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200478 return mSafeInsets.hashCode() * 48271 + mBounds.hashCode();
Adrian Roosd4970af2017-11-10 15:48:01 +0100479 }
480
481 @Override
482 public boolean equals(Object o) {
483 if (o == this) {
484 return true;
485 }
486 if (o instanceof DisplayCutout) {
487 DisplayCutout c = (DisplayCutout) o;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200488 return mSafeInsets.equals(c.mSafeInsets) && mBounds.equals(c.mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100489 }
490 return false;
491 }
492
493 @Override
494 public String toString() {
495 return "DisplayCutout{insets=" + mSafeInsets
Issei Suzuki43190bd2018-08-20 17:28:41 +0200496 + " boundingRect={" + mBounds + "}"
Adrian Roosd4970af2017-11-10 15:48:01 +0100497 + "}";
498 }
499
500 /**
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800501 * @hide
502 */
503 public void writeToProto(ProtoOutputStream proto, long fieldId) {
504 final long token = proto.start(fieldId);
505 mSafeInsets.writeToProto(proto, INSETS);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200506 mBounds.getRect(BOUNDS_POSITION_LEFT).writeToProto(proto, BOUND_LEFT);
507 mBounds.getRect(BOUNDS_POSITION_TOP).writeToProto(proto, BOUND_TOP);
508 mBounds.getRect(BOUNDS_POSITION_RIGHT).writeToProto(proto, BOUND_RIGHT);
509 mBounds.getRect(BOUNDS_POSITION_BOTTOM).writeToProto(proto, BOUND_BOTTOM);
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800510 proto.end(token);
511 }
512
513 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100514 * Insets the reference frame of the cutout in the given directions.
515 *
516 * @return a copy of this instance which has been inset
517 * @hide
518 */
519 public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) {
Jorim Jaggi4981f152019-03-26 18:58:45 +0100520 if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0
521 || isBoundsEmpty()) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100522 return this;
523 }
524
525 Rect safeInsets = new Rect(mSafeInsets);
Adrian Roosd4970af2017-11-10 15:48:01 +0100526
527 // Note: it's not really well defined what happens when the inset is negative, because we
528 // don't know if the safe inset needs to expand in general.
529 if (insetTop > 0 || safeInsets.top > 0) {
530 safeInsets.top = atLeastZero(safeInsets.top - insetTop);
531 }
532 if (insetBottom > 0 || safeInsets.bottom > 0) {
533 safeInsets.bottom = atLeastZero(safeInsets.bottom - insetBottom);
534 }
535 if (insetLeft > 0 || safeInsets.left > 0) {
536 safeInsets.left = atLeastZero(safeInsets.left - insetLeft);
537 }
538 if (insetRight > 0 || safeInsets.right > 0) {
539 safeInsets.right = atLeastZero(safeInsets.right - insetRight);
540 }
541
Jorim Jaggi4981f152019-03-26 18:58:45 +0100542 // If we are not cutting off part of the cutout by insetting it on bottom/right, and we also
543 // don't move it around, we can avoid the allocation and copy of the instance.
544 if (insetLeft == 0 && insetTop == 0 && mSafeInsets.equals(safeInsets)) {
545 return this;
546 }
547
Issei Suzuki43190bd2018-08-20 17:28:41 +0200548 Rect[] bounds = mBounds.getRects();
549 for (int i = 0; i < bounds.length; ++i) {
550 if (!bounds[i].equals(ZERO_RECT)) {
551 bounds[i].offset(-insetLeft, -insetTop);
552 }
553 }
554
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100555 return new DisplayCutout(safeInsets, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100556 }
557
558 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100559 * Returns a copy of this instance with the safe insets replaced with the parameter.
Adrian Roos24264212018-02-19 16:26:15 +0100560 *
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100561 * @param safeInsets the new safe insets in pixels
562 * @return a copy of this instance with the safe insets replaced with the argument.
Adrian Roos24264212018-02-19 16:26:15 +0100563 *
Adrian Roos24264212018-02-19 16:26:15 +0100564 * @hide
565 */
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100566 public DisplayCutout replaceSafeInsets(Rect safeInsets) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200567 return new DisplayCutout(new Rect(safeInsets), mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100568 }
569
570 private static int atLeastZero(int value) {
571 return value < 0 ? 0 : value;
572 }
573
Adrian Roosd4970af2017-11-10 15:48:01 +0100574
575 /**
Adrian Roos24264212018-02-19 16:26:15 +0100576 * Creates an instance from a bounding rect.
Adrian Roosd4970af2017-11-10 15:48:01 +0100577 *
578 * @hide
579 */
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200580 @VisibleForTesting
Issei Suzuki43190bd2018-08-20 17:28:41 +0200581 public static DisplayCutout fromBoundingRect(
582 int left, int top, int right, int bottom, @BoundsPosition int pos) {
583 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH];
584 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) {
585 bounds[i] = (pos == i) ? new Rect(left, top, right, bottom) : new Rect();
586 }
587 return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */);
Adrian Roos1cf585052018-01-03 18:43:27 +0100588 }
Adrian Roosd4970af2017-11-10 15:48:01 +0100589
Adrian Roos1cf585052018-01-03 18:43:27 +0100590 /**
591 * Creates an instance from a bounding {@link Path}.
592 *
593 * @hide
594 */
Issei Suzuki43190bd2018-08-20 17:28:41 +0200595 public static DisplayCutout fromBounds(Rect[] bounds) {
596 return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100597 }
598
599 /**
Jorim Jaggi60640512018-06-29 01:14:31 +0200600 * Creates the display cutout according to
601 * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest
602 * rectangle-base approximation of the cutout.
Adrian Roos16693f32018-01-18 17:45:52 +0100603 *
604 * @hide
605 */
Jorim Jaggi60640512018-06-29 01:14:31 +0200606 public static DisplayCutout fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight) {
607 return fromSpec(res.getString(R.string.config_mainBuiltInDisplayCutoutRectApproximation),
Adrian Roos535c4202018-04-16 16:12:40 +0200608 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100609 }
610
611 /**
Adrian Roos51072a82018-04-10 15:17:08 -0700612 * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout.
613 *
614 * @hide
615 */
616 public static Path pathFromResources(Resources res, int displayWidth, int displayHeight) {
Jorim Jaggi60640512018-06-29 01:14:31 +0200617 return pathAndDisplayCutoutFromSpec(
618 res.getString(R.string.config_mainBuiltInDisplayCutout),
Adrian Roos535c4202018-04-16 16:12:40 +0200619 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT).first;
Adrian Roos51072a82018-04-10 15:17:08 -0700620 }
621
622 /**
Adrian Roos8d13bf12018-02-21 15:17:07 +0100623 * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec.
624 *
625 * @hide
626 */
627 @VisibleForTesting(visibility = PRIVATE)
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100628 public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight,
629 float density) {
Adrian Roos51072a82018-04-10 15:17:08 -0700630 return pathAndDisplayCutoutFromSpec(spec, displayWidth, displayHeight, density).second;
631 }
632
633 private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(String spec,
634 int displayWidth, int displayHeight, float density) {
Adrian Roos16693f32018-01-18 17:45:52 +0100635 if (TextUtils.isEmpty(spec)) {
Adrian Roos51072a82018-04-10 15:17:08 -0700636 return NULL_PAIR;
Adrian Roos16693f32018-01-18 17:45:52 +0100637 }
Adrian Roos8d13bf12018-02-21 15:17:07 +0100638 synchronized (CACHE_LOCK) {
639 if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth
Adrian Roos51072a82018-04-10 15:17:08 -0700640 && sCachedDisplayHeight == displayHeight
Adrian Roos8d13bf12018-02-21 15:17:07 +0100641 && sCachedDensity == density) {
642 return sCachedCutout;
643 }
644 }
Adrian Roos16693f32018-01-18 17:45:52 +0100645 spec = spec.trim();
Adrian Roosb8b10f82018-03-15 20:09:42 +0100646 final float offsetX;
647 if (spec.endsWith(RIGHT_MARKER)) {
648 offsetX = displayWidth;
649 spec = spec.substring(0, spec.length() - RIGHT_MARKER.length()).trim();
650 } else {
651 offsetX = displayWidth / 2f;
652 }
Adrian Roos16693f32018-01-18 17:45:52 +0100653 final boolean inDp = spec.endsWith(DP_MARKER);
654 if (inDp) {
655 spec = spec.substring(0, spec.length() - DP_MARKER.length());
656 }
657
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100658 String bottomSpec = null;
659 if (spec.contains(BOTTOM_MARKER)) {
660 String[] splits = spec.split(BOTTOM_MARKER, 2);
661 spec = splits[0].trim();
662 bottomSpec = splits[1].trim();
663 }
664
665 final Path p;
Jorim Jaggi60640512018-06-29 01:14:31 +0200666 final Region r = Region.obtain();
Adrian Roos16693f32018-01-18 17:45:52 +0100667 try {
668 p = PathParser.createPathFromPathData(spec);
669 } catch (Throwable e) {
670 Log.wtf(TAG, "Could not inflate cutout: ", e);
Adrian Roos51072a82018-04-10 15:17:08 -0700671 return NULL_PAIR;
Adrian Roos16693f32018-01-18 17:45:52 +0100672 }
673
674 final Matrix m = new Matrix();
675 if (inDp) {
Adrian Roos8d13bf12018-02-21 15:17:07 +0100676 m.postScale(density, density);
Adrian Roos16693f32018-01-18 17:45:52 +0100677 }
Adrian Roosb8b10f82018-03-15 20:09:42 +0100678 m.postTranslate(offsetX, 0);
Adrian Roos16693f32018-01-18 17:45:52 +0100679 p.transform(m);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100680
Issei Suzuki43190bd2018-08-20 17:28:41 +0200681 Rect boundTop = new Rect();
682 toRectAndAddToRegion(p, r, boundTop);
683 final int topInset = boundTop.bottom;
Jorim Jaggi60640512018-06-29 01:14:31 +0200684
Issei Suzuki43190bd2018-08-20 17:28:41 +0200685 Rect boundBottom = null;
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200686 final int bottomInset;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100687 if (bottomSpec != null) {
688 final Path bottomPath;
689 try {
690 bottomPath = PathParser.createPathFromPathData(bottomSpec);
691 } catch (Throwable e) {
692 Log.wtf(TAG, "Could not inflate bottom cutout: ", e);
Adrian Roos51072a82018-04-10 15:17:08 -0700693 return NULL_PAIR;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100694 }
695 // Keep top transform
696 m.postTranslate(0, displayHeight);
697 bottomPath.transform(m);
698 p.addPath(bottomPath);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200699 boundBottom = new Rect();
700 toRectAndAddToRegion(bottomPath, r, boundBottom);
701 bottomInset = displayHeight - boundBottom.top;
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200702 } else {
703 bottomInset = 0;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100704 }
705
Issei Suzuki43190bd2018-08-20 17:28:41 +0200706 Rect safeInset = new Rect(0, topInset, 0, bottomInset);
707 final DisplayCutout cutout = new DisplayCutout(
708 safeInset, null /* boundLeft */, boundTop, null /* boundRight */, boundBottom,
709 false /* copyArguments */);
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200710
711 final Pair<Path, DisplayCutout> result = new Pair<>(p, cutout);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100712 synchronized (CACHE_LOCK) {
713 sCachedSpec = spec;
714 sCachedDisplayWidth = displayWidth;
Adrian Roos51072a82018-04-10 15:17:08 -0700715 sCachedDisplayHeight = displayHeight;
Adrian Roos8d13bf12018-02-21 15:17:07 +0100716 sCachedDensity = density;
717 sCachedCutout = result;
718 }
719 return result;
Adrian Roos16693f32018-01-18 17:45:52 +0100720 }
721
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200722 private static void toRectAndAddToRegion(Path p, Region inoutRegion, Rect inoutRect) {
Jorim Jaggi60640512018-06-29 01:14:31 +0200723 final RectF rectF = new RectF();
Jorim Jaggi60640512018-06-29 01:14:31 +0200724 p.computeBounds(rectF, false /* unused */);
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200725 rectF.round(inoutRect);
726 inoutRegion.op(inoutRect, Op.UNION);
Jorim Jaggi60640512018-06-29 01:14:31 +0200727 }
728
Adrian Roos16693f32018-01-18 17:45:52 +0100729 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100730 * Helper class for passing {@link DisplayCutout} through binder.
731 *
732 * Needed, because {@code readFromParcel} cannot be used with immutable classes.
733 *
734 * @hide
735 */
736 public static final class ParcelableWrapper implements Parcelable {
737
738 private DisplayCutout mInner;
739
740 public ParcelableWrapper() {
741 this(NO_CUTOUT);
742 }
743
744 public ParcelableWrapper(DisplayCutout cutout) {
745 mInner = cutout;
746 }
747
748 @Override
749 public int describeContents() {
750 return 0;
751 }
752
753 @Override
754 public void writeToParcel(Parcel out, int flags) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100755 writeCutoutToParcel(mInner, out, flags);
756 }
757
758 /**
759 * Writes a DisplayCutout to a {@link Parcel}.
760 *
761 * @see #readCutoutFromParcel(Parcel)
762 */
763 public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) {
764 if (cutout == null) {
765 out.writeInt(-1);
766 } else if (cutout == NO_CUTOUT) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100767 out.writeInt(0);
768 } else {
769 out.writeInt(1);
Adrian Roos1cf585052018-01-03 18:43:27 +0100770 out.writeTypedObject(cutout.mSafeInsets, flags);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200771 out.writeTypedArray(cutout.mBounds.getRects(), flags);
Adrian Roosd4970af2017-11-10 15:48:01 +0100772 }
773 }
774
775 /**
776 * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing
777 * instance.
778 *
779 * Needed for AIDL out parameters.
780 */
781 public void readFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100782 mInner = readCutoutFromParcel(in);
Adrian Roosd4970af2017-11-10 15:48:01 +0100783 }
784
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700785 public static final @android.annotation.NonNull Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() {
Adrian Roosd4970af2017-11-10 15:48:01 +0100786 @Override
787 public ParcelableWrapper createFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100788 return new ParcelableWrapper(readCutoutFromParcel(in));
Adrian Roosd4970af2017-11-10 15:48:01 +0100789 }
790
791 @Override
792 public ParcelableWrapper[] newArray(int size) {
793 return new ParcelableWrapper[size];
794 }
795 };
796
Adrian Roos1cf585052018-01-03 18:43:27 +0100797 /**
798 * Reads a DisplayCutout from a {@link Parcel}.
799 *
800 * @see #writeCutoutToParcel(DisplayCutout, Parcel, int)
801 */
802 public static DisplayCutout readCutoutFromParcel(Parcel in) {
803 int variant = in.readInt();
804 if (variant == -1) {
805 return null;
806 }
807 if (variant == 0) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100808 return NO_CUTOUT;
809 }
810
Adrian Roosd4970af2017-11-10 15:48:01 +0100811 Rect safeInsets = in.readTypedObject(Rect.CREATOR);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200812 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH];
813 in.readTypedArray(bounds, Rect.CREATOR);
Adrian Roosd4970af2017-11-10 15:48:01 +0100814
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100815 return new DisplayCutout(safeInsets, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100816 }
817
818 public DisplayCutout get() {
819 return mInner;
820 }
821
822 public void set(ParcelableWrapper cutout) {
823 mInner = cutout.get();
824 }
825
826 public void set(DisplayCutout cutout) {
827 mInner = cutout;
828 }
829
830 @Override
831 public int hashCode() {
832 return mInner.hashCode();
833 }
834
835 @Override
836 public boolean equals(Object o) {
837 return o instanceof ParcelableWrapper
838 && mInner.equals(((ParcelableWrapper) o).mInner);
839 }
840
841 @Override
842 public String toString() {
843 return String.valueOf(mInner);
844 }
845 }
846}