blob: 610f7edef451d1ac2eb09840de2fef47ee507c26 [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) {
322 for (Rect bound : boundingRects) {
323 // There is at most one non-functional area per short edge of the device, but none
324 // on the long edges, so either safeInsets.right or safeInsets.bottom must be 0.
325 // TODO(b/117199965): Refine the logic to handle edge cases.
326 if (bound.left == 0) {
327 sortedBounds[BOUNDS_POSITION_LEFT] = bound;
328 } else if (bound.top == 0) {
329 sortedBounds[BOUNDS_POSITION_TOP] = bound;
330 } else if (safeInsets.right > 0) {
331 sortedBounds[BOUNDS_POSITION_RIGHT] = bound;
332 } else if (safeInsets.bottom > 0) {
333 sortedBounds[BOUNDS_POSITION_BOTTOM] = bound;
334 }
Issei Suzuki43190bd2018-08-20 17:28:41 +0200335 }
336 }
337 return sortedBounds;
338 }
339
340 /**
341 * Returns true if there is no cutout, i.e. the bounds are empty.
342 *
343 * @hide
344 */
345 public boolean isBoundsEmpty() {
346 return mBounds.isEmpty();
Adrian Roosd4970af2017-11-10 15:48:01 +0100347 }
348
349 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100350 * Returns true if the safe insets are empty (and therefore the current view does not
351 * overlap with the cutout or cutout area).
Adrian Roosd4970af2017-11-10 15:48:01 +0100352 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100353 * @hide
Adrian Roosd4970af2017-11-10 15:48:01 +0100354 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100355 public boolean isEmpty() {
356 return mSafeInsets.equals(ZERO_RECT);
Adrian Roosd4970af2017-11-10 15:48:01 +0100357 }
358
Adrian Roos9bbd9662018-03-02 14:31:37 +0100359 /** Returns the inset from the top which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100360 public int getSafeInsetTop() {
361 return mSafeInsets.top;
362 }
363
Adrian Roos9bbd9662018-03-02 14:31:37 +0100364 /** Returns the inset from the bottom which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100365 public int getSafeInsetBottom() {
366 return mSafeInsets.bottom;
367 }
368
Adrian Roos9bbd9662018-03-02 14:31:37 +0100369 /** Returns the inset from the left which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100370 public int getSafeInsetLeft() {
371 return mSafeInsets.left;
372 }
373
Adrian Roos9bbd9662018-03-02 14:31:37 +0100374 /** Returns the inset from the right which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100375 public int getSafeInsetRight() {
376 return mSafeInsets.right;
377 }
378
379 /**
Adrian Roos9bbd9662018-03-02 14:31:37 +0100380 * Returns the safe insets in a rect in pixel units.
Adrian Roosd4970af2017-11-10 15:48:01 +0100381 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100382 * @return a rect which is set to the safe insets.
Adrian Roosd4970af2017-11-10 15:48:01 +0100383 * @hide
384 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100385 public Rect getSafeInsets() {
386 return new Rect(mSafeInsets);
Adrian Roosd4970af2017-11-10 15:48:01 +0100387 }
388
389 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100390 * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional
391 * area on the display.
Adrian Roosd4970af2017-11-10 15:48:01 +0100392 *
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100393 * There will be at most one non-functional area per short edge of the device, and none on
394 * the long edges.
395 *
Issei Suzuki43190bd2018-08-20 17:28:41 +0200396 * @return a list of bounding {@code Rect}s, one for each display cutout area. No empty Rect is
397 * returned.
Adrian Roosd4970af2017-11-10 15:48:01 +0100398 */
Adrian Roosde363d02018-10-12 13:03:56 +0200399 @NonNull
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100400 public List<Rect> getBoundingRects() {
401 List<Rect> result = new ArrayList<>();
Issei Suzuki43190bd2018-08-20 17:28:41 +0200402 for (Rect bound : getBoundingRectsAll()) {
403 if (!bound.isEmpty()) {
404 result.add(new Rect(bound));
405 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100406 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100407 return result;
Adrian Roosd4970af2017-11-10 15:48:01 +0100408 }
409
Issei Suzuki43190bd2018-08-20 17:28:41 +0200410 /**
411 * Returns an array of {@code Rect}s, each of which is the bounding rectangle for a non-
412 * functional area on the display. Ordinal value of BoundPosition is used as an index of
413 * the array.
414 *
415 * There will be at most one non-functional area per short edge of the device, and none on
416 * the long edges.
417 *
418 * @return an array of bounding {@code Rect}s, one for each display cutout area. This might
419 * contain ZERO_RECT, which means there is no cutout area at the position.
420 *
421 * @hide
422 */
423 public Rect[] getBoundingRectsAll() {
424 return mBounds.getRects();
425 }
426
427 /**
428 * Returns a bounding rectangle for a non-functional area on the display which is located on
429 * the left of the screen.
430 *
431 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
432 * is returned.
433 */
434 public @NonNull Rect getBoundingRectLeft() {
435 return mBounds.getRect(BOUNDS_POSITION_LEFT);
436 }
437
438 /**
439 * Returns a bounding rectangle for a non-functional area on the display which is located on
440 * the top of the screen.
441 *
442 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
443 * is returned.
444 */
445 public @NonNull Rect getBoundingRectTop() {
446 return mBounds.getRect(BOUNDS_POSITION_TOP);
447 }
448
449 /**
450 * Returns a bounding rectangle for a non-functional area on the display which is located on
451 * the right of the screen.
452 *
453 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
454 * is returned.
455 */
456 public @NonNull Rect getBoundingRectRight() {
457 return mBounds.getRect(BOUNDS_POSITION_RIGHT);
458 }
459
460 /**
461 * Returns a bounding rectangle for a non-functional area on the display which is located on
462 * the bottom of the screen.
463 *
464 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
465 * is returned.
466 */
467 public @NonNull Rect getBoundingRectBottom() {
468 return mBounds.getRect(BOUNDS_POSITION_BOTTOM);
469 }
470
Adrian Roosd4970af2017-11-10 15:48:01 +0100471 @Override
472 public int hashCode() {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200473 return mSafeInsets.hashCode() * 48271 + mBounds.hashCode();
Adrian Roosd4970af2017-11-10 15:48:01 +0100474 }
475
476 @Override
477 public boolean equals(Object o) {
478 if (o == this) {
479 return true;
480 }
481 if (o instanceof DisplayCutout) {
482 DisplayCutout c = (DisplayCutout) o;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200483 return mSafeInsets.equals(c.mSafeInsets) && mBounds.equals(c.mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100484 }
485 return false;
486 }
487
488 @Override
489 public String toString() {
490 return "DisplayCutout{insets=" + mSafeInsets
Issei Suzuki43190bd2018-08-20 17:28:41 +0200491 + " boundingRect={" + mBounds + "}"
Adrian Roosd4970af2017-11-10 15:48:01 +0100492 + "}";
493 }
494
495 /**
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800496 * @hide
497 */
498 public void writeToProto(ProtoOutputStream proto, long fieldId) {
499 final long token = proto.start(fieldId);
500 mSafeInsets.writeToProto(proto, INSETS);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200501 mBounds.getRect(BOUNDS_POSITION_LEFT).writeToProto(proto, BOUND_LEFT);
502 mBounds.getRect(BOUNDS_POSITION_TOP).writeToProto(proto, BOUND_TOP);
503 mBounds.getRect(BOUNDS_POSITION_RIGHT).writeToProto(proto, BOUND_RIGHT);
504 mBounds.getRect(BOUNDS_POSITION_BOTTOM).writeToProto(proto, BOUND_BOTTOM);
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800505 proto.end(token);
506 }
507
508 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100509 * Insets the reference frame of the cutout in the given directions.
510 *
511 * @return a copy of this instance which has been inset
512 * @hide
513 */
514 public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200515 if (isBoundsEmpty()
Adrian Roosd4970af2017-11-10 15:48:01 +0100516 || insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0) {
517 return this;
518 }
519
520 Rect safeInsets = new Rect(mSafeInsets);
Adrian Roosd4970af2017-11-10 15:48:01 +0100521
522 // Note: it's not really well defined what happens when the inset is negative, because we
523 // don't know if the safe inset needs to expand in general.
524 if (insetTop > 0 || safeInsets.top > 0) {
525 safeInsets.top = atLeastZero(safeInsets.top - insetTop);
526 }
527 if (insetBottom > 0 || safeInsets.bottom > 0) {
528 safeInsets.bottom = atLeastZero(safeInsets.bottom - insetBottom);
529 }
530 if (insetLeft > 0 || safeInsets.left > 0) {
531 safeInsets.left = atLeastZero(safeInsets.left - insetLeft);
532 }
533 if (insetRight > 0 || safeInsets.right > 0) {
534 safeInsets.right = atLeastZero(safeInsets.right - insetRight);
535 }
536
Issei Suzuki43190bd2018-08-20 17:28:41 +0200537 Rect[] bounds = mBounds.getRects();
538 for (int i = 0; i < bounds.length; ++i) {
539 if (!bounds[i].equals(ZERO_RECT)) {
540 bounds[i].offset(-insetLeft, -insetTop);
541 }
542 }
543
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100544 return new DisplayCutout(safeInsets, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100545 }
546
547 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100548 * Returns a copy of this instance with the safe insets replaced with the parameter.
Adrian Roos24264212018-02-19 16:26:15 +0100549 *
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100550 * @param safeInsets the new safe insets in pixels
551 * @return a copy of this instance with the safe insets replaced with the argument.
Adrian Roos24264212018-02-19 16:26:15 +0100552 *
Adrian Roos24264212018-02-19 16:26:15 +0100553 * @hide
554 */
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100555 public DisplayCutout replaceSafeInsets(Rect safeInsets) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200556 return new DisplayCutout(new Rect(safeInsets), mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100557 }
558
559 private static int atLeastZero(int value) {
560 return value < 0 ? 0 : value;
561 }
562
Adrian Roosd4970af2017-11-10 15:48:01 +0100563
564 /**
Adrian Roos24264212018-02-19 16:26:15 +0100565 * Creates an instance from a bounding rect.
Adrian Roosd4970af2017-11-10 15:48:01 +0100566 *
567 * @hide
568 */
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200569 @VisibleForTesting
Issei Suzuki43190bd2018-08-20 17:28:41 +0200570 public static DisplayCutout fromBoundingRect(
571 int left, int top, int right, int bottom, @BoundsPosition int pos) {
572 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH];
573 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) {
574 bounds[i] = (pos == i) ? new Rect(left, top, right, bottom) : new Rect();
575 }
576 return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */);
Adrian Roos1cf585052018-01-03 18:43:27 +0100577 }
Adrian Roosd4970af2017-11-10 15:48:01 +0100578
Adrian Roos1cf585052018-01-03 18:43:27 +0100579 /**
580 * Creates an instance from a bounding {@link Path}.
581 *
582 * @hide
583 */
Issei Suzuki43190bd2018-08-20 17:28:41 +0200584 public static DisplayCutout fromBounds(Rect[] bounds) {
585 return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100586 }
587
588 /**
Jorim Jaggi60640512018-06-29 01:14:31 +0200589 * Creates the display cutout according to
590 * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest
591 * rectangle-base approximation of the cutout.
Adrian Roos16693f32018-01-18 17:45:52 +0100592 *
593 * @hide
594 */
Jorim Jaggi60640512018-06-29 01:14:31 +0200595 public static DisplayCutout fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight) {
596 return fromSpec(res.getString(R.string.config_mainBuiltInDisplayCutoutRectApproximation),
Adrian Roos535c4202018-04-16 16:12:40 +0200597 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100598 }
599
600 /**
Adrian Roos51072a82018-04-10 15:17:08 -0700601 * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout.
602 *
603 * @hide
604 */
605 public static Path pathFromResources(Resources res, int displayWidth, int displayHeight) {
Jorim Jaggi60640512018-06-29 01:14:31 +0200606 return pathAndDisplayCutoutFromSpec(
607 res.getString(R.string.config_mainBuiltInDisplayCutout),
Adrian Roos535c4202018-04-16 16:12:40 +0200608 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT).first;
Adrian Roos51072a82018-04-10 15:17:08 -0700609 }
610
611 /**
Adrian Roos8d13bf12018-02-21 15:17:07 +0100612 * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec.
613 *
614 * @hide
615 */
616 @VisibleForTesting(visibility = PRIVATE)
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100617 public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight,
618 float density) {
Adrian Roos51072a82018-04-10 15:17:08 -0700619 return pathAndDisplayCutoutFromSpec(spec, displayWidth, displayHeight, density).second;
620 }
621
622 private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(String spec,
623 int displayWidth, int displayHeight, float density) {
Adrian Roos16693f32018-01-18 17:45:52 +0100624 if (TextUtils.isEmpty(spec)) {
Adrian Roos51072a82018-04-10 15:17:08 -0700625 return NULL_PAIR;
Adrian Roos16693f32018-01-18 17:45:52 +0100626 }
Adrian Roos8d13bf12018-02-21 15:17:07 +0100627 synchronized (CACHE_LOCK) {
628 if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth
Adrian Roos51072a82018-04-10 15:17:08 -0700629 && sCachedDisplayHeight == displayHeight
Adrian Roos8d13bf12018-02-21 15:17:07 +0100630 && sCachedDensity == density) {
631 return sCachedCutout;
632 }
633 }
Adrian Roos16693f32018-01-18 17:45:52 +0100634 spec = spec.trim();
Adrian Roosb8b10f82018-03-15 20:09:42 +0100635 final float offsetX;
636 if (spec.endsWith(RIGHT_MARKER)) {
637 offsetX = displayWidth;
638 spec = spec.substring(0, spec.length() - RIGHT_MARKER.length()).trim();
639 } else {
640 offsetX = displayWidth / 2f;
641 }
Adrian Roos16693f32018-01-18 17:45:52 +0100642 final boolean inDp = spec.endsWith(DP_MARKER);
643 if (inDp) {
644 spec = spec.substring(0, spec.length() - DP_MARKER.length());
645 }
646
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100647 String bottomSpec = null;
648 if (spec.contains(BOTTOM_MARKER)) {
649 String[] splits = spec.split(BOTTOM_MARKER, 2);
650 spec = splits[0].trim();
651 bottomSpec = splits[1].trim();
652 }
653
654 final Path p;
Jorim Jaggi60640512018-06-29 01:14:31 +0200655 final Region r = Region.obtain();
Adrian Roos16693f32018-01-18 17:45:52 +0100656 try {
657 p = PathParser.createPathFromPathData(spec);
658 } catch (Throwable e) {
659 Log.wtf(TAG, "Could not inflate cutout: ", e);
Adrian Roos51072a82018-04-10 15:17:08 -0700660 return NULL_PAIR;
Adrian Roos16693f32018-01-18 17:45:52 +0100661 }
662
663 final Matrix m = new Matrix();
664 if (inDp) {
Adrian Roos8d13bf12018-02-21 15:17:07 +0100665 m.postScale(density, density);
Adrian Roos16693f32018-01-18 17:45:52 +0100666 }
Adrian Roosb8b10f82018-03-15 20:09:42 +0100667 m.postTranslate(offsetX, 0);
Adrian Roos16693f32018-01-18 17:45:52 +0100668 p.transform(m);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100669
Issei Suzuki43190bd2018-08-20 17:28:41 +0200670 Rect boundTop = new Rect();
671 toRectAndAddToRegion(p, r, boundTop);
672 final int topInset = boundTop.bottom;
Jorim Jaggi60640512018-06-29 01:14:31 +0200673
Issei Suzuki43190bd2018-08-20 17:28:41 +0200674 Rect boundBottom = null;
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200675 final int bottomInset;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100676 if (bottomSpec != null) {
677 final Path bottomPath;
678 try {
679 bottomPath = PathParser.createPathFromPathData(bottomSpec);
680 } catch (Throwable e) {
681 Log.wtf(TAG, "Could not inflate bottom cutout: ", e);
Adrian Roos51072a82018-04-10 15:17:08 -0700682 return NULL_PAIR;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100683 }
684 // Keep top transform
685 m.postTranslate(0, displayHeight);
686 bottomPath.transform(m);
687 p.addPath(bottomPath);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200688 boundBottom = new Rect();
689 toRectAndAddToRegion(bottomPath, r, boundBottom);
690 bottomInset = displayHeight - boundBottom.top;
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200691 } else {
692 bottomInset = 0;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100693 }
694
Issei Suzuki43190bd2018-08-20 17:28:41 +0200695 Rect safeInset = new Rect(0, topInset, 0, bottomInset);
696 final DisplayCutout cutout = new DisplayCutout(
697 safeInset, null /* boundLeft */, boundTop, null /* boundRight */, boundBottom,
698 false /* copyArguments */);
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200699
700 final Pair<Path, DisplayCutout> result = new Pair<>(p, cutout);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100701 synchronized (CACHE_LOCK) {
702 sCachedSpec = spec;
703 sCachedDisplayWidth = displayWidth;
Adrian Roos51072a82018-04-10 15:17:08 -0700704 sCachedDisplayHeight = displayHeight;
Adrian Roos8d13bf12018-02-21 15:17:07 +0100705 sCachedDensity = density;
706 sCachedCutout = result;
707 }
708 return result;
Adrian Roos16693f32018-01-18 17:45:52 +0100709 }
710
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200711 private static void toRectAndAddToRegion(Path p, Region inoutRegion, Rect inoutRect) {
Jorim Jaggi60640512018-06-29 01:14:31 +0200712 final RectF rectF = new RectF();
Jorim Jaggi60640512018-06-29 01:14:31 +0200713 p.computeBounds(rectF, false /* unused */);
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200714 rectF.round(inoutRect);
715 inoutRegion.op(inoutRect, Op.UNION);
Jorim Jaggi60640512018-06-29 01:14:31 +0200716 }
717
Adrian Roos16693f32018-01-18 17:45:52 +0100718 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100719 * Helper class for passing {@link DisplayCutout} through binder.
720 *
721 * Needed, because {@code readFromParcel} cannot be used with immutable classes.
722 *
723 * @hide
724 */
725 public static final class ParcelableWrapper implements Parcelable {
726
727 private DisplayCutout mInner;
728
729 public ParcelableWrapper() {
730 this(NO_CUTOUT);
731 }
732
733 public ParcelableWrapper(DisplayCutout cutout) {
734 mInner = cutout;
735 }
736
737 @Override
738 public int describeContents() {
739 return 0;
740 }
741
742 @Override
743 public void writeToParcel(Parcel out, int flags) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100744 writeCutoutToParcel(mInner, out, flags);
745 }
746
747 /**
748 * Writes a DisplayCutout to a {@link Parcel}.
749 *
750 * @see #readCutoutFromParcel(Parcel)
751 */
752 public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) {
753 if (cutout == null) {
754 out.writeInt(-1);
755 } else if (cutout == NO_CUTOUT) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100756 out.writeInt(0);
757 } else {
758 out.writeInt(1);
Adrian Roos1cf585052018-01-03 18:43:27 +0100759 out.writeTypedObject(cutout.mSafeInsets, flags);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200760 out.writeTypedArray(cutout.mBounds.getRects(), flags);
Adrian Roosd4970af2017-11-10 15:48:01 +0100761 }
762 }
763
764 /**
765 * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing
766 * instance.
767 *
768 * Needed for AIDL out parameters.
769 */
770 public void readFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100771 mInner = readCutoutFromParcel(in);
Adrian Roosd4970af2017-11-10 15:48:01 +0100772 }
773
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700774 public static final @android.annotation.NonNull Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() {
Adrian Roosd4970af2017-11-10 15:48:01 +0100775 @Override
776 public ParcelableWrapper createFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100777 return new ParcelableWrapper(readCutoutFromParcel(in));
Adrian Roosd4970af2017-11-10 15:48:01 +0100778 }
779
780 @Override
781 public ParcelableWrapper[] newArray(int size) {
782 return new ParcelableWrapper[size];
783 }
784 };
785
Adrian Roos1cf585052018-01-03 18:43:27 +0100786 /**
787 * Reads a DisplayCutout from a {@link Parcel}.
788 *
789 * @see #writeCutoutToParcel(DisplayCutout, Parcel, int)
790 */
791 public static DisplayCutout readCutoutFromParcel(Parcel in) {
792 int variant = in.readInt();
793 if (variant == -1) {
794 return null;
795 }
796 if (variant == 0) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100797 return NO_CUTOUT;
798 }
799
Adrian Roosd4970af2017-11-10 15:48:01 +0100800 Rect safeInsets = in.readTypedObject(Rect.CREATOR);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200801 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH];
802 in.readTypedArray(bounds, Rect.CREATOR);
Adrian Roosd4970af2017-11-10 15:48:01 +0100803
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100804 return new DisplayCutout(safeInsets, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100805 }
806
807 public DisplayCutout get() {
808 return mInner;
809 }
810
811 public void set(ParcelableWrapper cutout) {
812 mInner = cutout.get();
813 }
814
815 public void set(DisplayCutout cutout) {
816 mInner = cutout;
817 }
818
819 @Override
820 public int hashCode() {
821 return mInner.hashCode();
822 }
823
824 @Override
825 public boolean equals(Object o) {
826 return o instanceof ParcelableWrapper
827 && mInner.equals(((ParcelableWrapper) o).mInner);
828 }
829
830 @Override
831 public String toString() {
832 return String.valueOf(mInner);
833 }
834 }
835}