blob: 25526b8693023e398cd39c7c9d69cec058035f65 [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 *
236 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
237 * {@link #getSafeInsetTop()} etc.
238 * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed,
239 * it's treated as an empty rectangle (0,0)-(0,0).
240 * @param boundTop the top bounding rect of the display cutout in pixels. If null is passed,
241 * it's treated as an empty rectangle (0,0)-(0,0).
242 * @param boundRight the right bounding rect of the display cutout in pixels. If null is
243 * passed, it's treated as an empty rectangle (0,0)-(0,0).
244 * @param boundBottom the bottom bounding rect of the display cutout in pixels. If null is
245 * passed, it's treated as an empty rectangle (0,0)-(0,0).
246 */
247 // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE)
Adrian Roosde363d02018-10-12 13:03:56 +0200248 public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft,
249 @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200250 this(safeInsets.toRect(), boundLeft, boundTop, boundRight, boundBottom, true);
251 }
Adrian Roos9bbd9662018-03-02 14:31:37 +0100252
253 /**
254 * Creates a DisplayCutout instance.
255 *
256 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
257 * {@link #getSafeInsetTop()} etc.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100258 * @param boundingRects the bounding rects of the display cutouts as returned by
259 * {@link #getBoundingRects()} ()}.
Issei Suzuki43190bd2018-08-20 17:28:41 +0200260 * @deprecated Use {@link DisplayCutout#DisplayCutout(Insets, Rect, Rect, Rect, Rect)} instead.
Adrian Roos9bbd9662018-03-02 14:31:37 +0100261 */
262 // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE)
Issei Suzuki43190bd2018-08-20 17:28:41 +0200263 @Deprecated
Adrian Roosde363d02018-10-12 13:03:56 +0200264 public DisplayCutout(@Nullable Rect safeInsets, @Nullable List<Rect> boundingRects) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200265 this(safeInsets, extractBoundsFromList(safeInsets, boundingRects),
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100266 true /* copyArguments */);
Adrian Roos9bbd9662018-03-02 14:31:37 +0100267 }
Adrian Roosd4970af2017-11-10 15:48:01 +0100268
269 /**
270 * Creates a DisplayCutout instance.
271 *
Issei Suzuki43190bd2018-08-20 17:28:41 +0200272 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
273 * {@link #getSafeInsetTop()} etc.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100274 * @param copyArguments if true, create a copy of the arguments. If false, the passed arguments
275 * are not copied and MUST remain unchanged forever.
Adrian Roosd4970af2017-11-10 15:48:01 +0100276 */
Issei Suzuki43190bd2018-08-20 17:28:41 +0200277 private DisplayCutout(Rect safeInsets, Rect boundLeft, Rect boundTop, Rect boundRight,
278 Rect boundBottom, boolean copyArguments) {
279 mSafeInsets = getCopyOrRef(safeInsets, copyArguments);
280 mBounds = new Bounds(boundLeft, boundTop, boundRight, boundBottom, copyArguments);
281 }
282
283 private DisplayCutout(Rect safeInsets, Rect[] bounds, boolean copyArguments) {
284 mSafeInsets = getCopyOrRef(safeInsets, copyArguments);
285 mBounds = new Bounds(bounds, copyArguments);
286 }
287
288 private DisplayCutout(Rect safeInsets, Bounds bounds) {
289 mSafeInsets = safeInsets;
290 mBounds = bounds;
291
292 }
293
294 private static Rect getCopyOrRef(Rect r, boolean copyArguments) {
295 if (r == null) {
296 return ZERO_RECT;
297 } else if (copyArguments) {
298 return new Rect(r);
299 } else {
300 return r;
301 }
302 }
303
304 /**
305 * Find the position of the bounding rect, and create an array of Rect whose index represents
306 * the position (= BoundsPosition).
307 *
308 * @hide
309 */
310 public static Rect[] extractBoundsFromList(Rect safeInsets, List<Rect> boundingRects) {
311 Rect[] sortedBounds = new Rect[BOUNDS_POSITION_LENGTH];
312 for (int i = 0; i < sortedBounds.length; ++i) {
313 sortedBounds[i] = ZERO_RECT;
314 }
Adrian Roosde363d02018-10-12 13:03:56 +0200315 if (safeInsets != null && boundingRects != null) {
316 for (Rect bound : boundingRects) {
317 // There is at most one non-functional area per short edge of the device, but none
318 // on the long edges, so either safeInsets.right or safeInsets.bottom must be 0.
319 // TODO(b/117199965): Refine the logic to handle edge cases.
320 if (bound.left == 0) {
321 sortedBounds[BOUNDS_POSITION_LEFT] = bound;
322 } else if (bound.top == 0) {
323 sortedBounds[BOUNDS_POSITION_TOP] = bound;
324 } else if (safeInsets.right > 0) {
325 sortedBounds[BOUNDS_POSITION_RIGHT] = bound;
326 } else if (safeInsets.bottom > 0) {
327 sortedBounds[BOUNDS_POSITION_BOTTOM] = bound;
328 }
Issei Suzuki43190bd2018-08-20 17:28:41 +0200329 }
330 }
331 return sortedBounds;
332 }
333
334 /**
335 * Returns true if there is no cutout, i.e. the bounds are empty.
336 *
337 * @hide
338 */
339 public boolean isBoundsEmpty() {
340 return mBounds.isEmpty();
Adrian Roosd4970af2017-11-10 15:48:01 +0100341 }
342
343 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100344 * Returns true if the safe insets are empty (and therefore the current view does not
345 * overlap with the cutout or cutout area).
Adrian Roosd4970af2017-11-10 15:48:01 +0100346 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100347 * @hide
Adrian Roosd4970af2017-11-10 15:48:01 +0100348 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100349 public boolean isEmpty() {
350 return mSafeInsets.equals(ZERO_RECT);
Adrian Roosd4970af2017-11-10 15:48:01 +0100351 }
352
Adrian Roos9bbd9662018-03-02 14:31:37 +0100353 /** Returns the inset from the top which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100354 public int getSafeInsetTop() {
355 return mSafeInsets.top;
356 }
357
Adrian Roos9bbd9662018-03-02 14:31:37 +0100358 /** Returns the inset from the bottom which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100359 public int getSafeInsetBottom() {
360 return mSafeInsets.bottom;
361 }
362
Adrian Roos9bbd9662018-03-02 14:31:37 +0100363 /** Returns the inset from the left which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100364 public int getSafeInsetLeft() {
365 return mSafeInsets.left;
366 }
367
Adrian Roos9bbd9662018-03-02 14:31:37 +0100368 /** Returns the inset from the right which avoids the display cutout in pixels. */
Adrian Roosd4970af2017-11-10 15:48:01 +0100369 public int getSafeInsetRight() {
370 return mSafeInsets.right;
371 }
372
373 /**
Adrian Roos9bbd9662018-03-02 14:31:37 +0100374 * Returns the safe insets in a rect in pixel units.
Adrian Roosd4970af2017-11-10 15:48:01 +0100375 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100376 * @return a rect which is set to the safe insets.
Adrian Roosd4970af2017-11-10 15:48:01 +0100377 * @hide
378 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100379 public Rect getSafeInsets() {
380 return new Rect(mSafeInsets);
Adrian Roosd4970af2017-11-10 15:48:01 +0100381 }
382
383 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100384 * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional
385 * area on the display.
Adrian Roosd4970af2017-11-10 15:48:01 +0100386 *
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100387 * There will be at most one non-functional area per short edge of the device, and none on
388 * the long edges.
389 *
Issei Suzuki43190bd2018-08-20 17:28:41 +0200390 * @return a list of bounding {@code Rect}s, one for each display cutout area. No empty Rect is
391 * returned.
Adrian Roosd4970af2017-11-10 15:48:01 +0100392 */
Adrian Roosde363d02018-10-12 13:03:56 +0200393 @NonNull
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100394 public List<Rect> getBoundingRects() {
395 List<Rect> result = new ArrayList<>();
Issei Suzuki43190bd2018-08-20 17:28:41 +0200396 for (Rect bound : getBoundingRectsAll()) {
397 if (!bound.isEmpty()) {
398 result.add(new Rect(bound));
399 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100400 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100401 return result;
Adrian Roosd4970af2017-11-10 15:48:01 +0100402 }
403
Issei Suzuki43190bd2018-08-20 17:28:41 +0200404 /**
405 * Returns an array of {@code Rect}s, each of which is the bounding rectangle for a non-
406 * functional area on the display. Ordinal value of BoundPosition is used as an index of
407 * the array.
408 *
409 * There will be at most one non-functional area per short edge of the device, and none on
410 * the long edges.
411 *
412 * @return an array of bounding {@code Rect}s, one for each display cutout area. This might
413 * contain ZERO_RECT, which means there is no cutout area at the position.
414 *
415 * @hide
416 */
417 public Rect[] getBoundingRectsAll() {
418 return mBounds.getRects();
419 }
420
421 /**
422 * Returns a bounding rectangle for a non-functional area on the display which is located on
423 * the left of the screen.
424 *
425 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
426 * is returned.
427 */
428 public @NonNull Rect getBoundingRectLeft() {
429 return mBounds.getRect(BOUNDS_POSITION_LEFT);
430 }
431
432 /**
433 * Returns a bounding rectangle for a non-functional area on the display which is located on
434 * the top 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 getBoundingRectTop() {
440 return mBounds.getRect(BOUNDS_POSITION_TOP);
441 }
442
443 /**
444 * Returns a bounding rectangle for a non-functional area on the display which is located on
445 * the right 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 getBoundingRectRight() {
451 return mBounds.getRect(BOUNDS_POSITION_RIGHT);
452 }
453
454 /**
455 * Returns a bounding rectangle for a non-functional area on the display which is located on
456 * the bottom 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 getBoundingRectBottom() {
462 return mBounds.getRect(BOUNDS_POSITION_BOTTOM);
463 }
464
Adrian Roosd4970af2017-11-10 15:48:01 +0100465 @Override
466 public int hashCode() {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200467 return mSafeInsets.hashCode() * 48271 + mBounds.hashCode();
Adrian Roosd4970af2017-11-10 15:48:01 +0100468 }
469
470 @Override
471 public boolean equals(Object o) {
472 if (o == this) {
473 return true;
474 }
475 if (o instanceof DisplayCutout) {
476 DisplayCutout c = (DisplayCutout) o;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200477 return mSafeInsets.equals(c.mSafeInsets) && mBounds.equals(c.mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100478 }
479 return false;
480 }
481
482 @Override
483 public String toString() {
484 return "DisplayCutout{insets=" + mSafeInsets
Issei Suzuki43190bd2018-08-20 17:28:41 +0200485 + " boundingRect={" + mBounds + "}"
Adrian Roosd4970af2017-11-10 15:48:01 +0100486 + "}";
487 }
488
489 /**
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800490 * @hide
491 */
492 public void writeToProto(ProtoOutputStream proto, long fieldId) {
493 final long token = proto.start(fieldId);
494 mSafeInsets.writeToProto(proto, INSETS);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200495 mBounds.getRect(BOUNDS_POSITION_LEFT).writeToProto(proto, BOUND_LEFT);
496 mBounds.getRect(BOUNDS_POSITION_TOP).writeToProto(proto, BOUND_TOP);
497 mBounds.getRect(BOUNDS_POSITION_RIGHT).writeToProto(proto, BOUND_RIGHT);
498 mBounds.getRect(BOUNDS_POSITION_BOTTOM).writeToProto(proto, BOUND_BOTTOM);
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800499 proto.end(token);
500 }
501
502 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100503 * Insets the reference frame of the cutout in the given directions.
504 *
505 * @return a copy of this instance which has been inset
506 * @hide
507 */
508 public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200509 if (isBoundsEmpty()
Adrian Roosd4970af2017-11-10 15:48:01 +0100510 || insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0) {
511 return this;
512 }
513
514 Rect safeInsets = new Rect(mSafeInsets);
Adrian Roosd4970af2017-11-10 15:48:01 +0100515
516 // Note: it's not really well defined what happens when the inset is negative, because we
517 // don't know if the safe inset needs to expand in general.
518 if (insetTop > 0 || safeInsets.top > 0) {
519 safeInsets.top = atLeastZero(safeInsets.top - insetTop);
520 }
521 if (insetBottom > 0 || safeInsets.bottom > 0) {
522 safeInsets.bottom = atLeastZero(safeInsets.bottom - insetBottom);
523 }
524 if (insetLeft > 0 || safeInsets.left > 0) {
525 safeInsets.left = atLeastZero(safeInsets.left - insetLeft);
526 }
527 if (insetRight > 0 || safeInsets.right > 0) {
528 safeInsets.right = atLeastZero(safeInsets.right - insetRight);
529 }
530
Issei Suzuki43190bd2018-08-20 17:28:41 +0200531 Rect[] bounds = mBounds.getRects();
532 for (int i = 0; i < bounds.length; ++i) {
533 if (!bounds[i].equals(ZERO_RECT)) {
534 bounds[i].offset(-insetLeft, -insetTop);
535 }
536 }
537
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100538 return new DisplayCutout(safeInsets, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100539 }
540
541 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100542 * Returns a copy of this instance with the safe insets replaced with the parameter.
Adrian Roos24264212018-02-19 16:26:15 +0100543 *
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100544 * @param safeInsets the new safe insets in pixels
545 * @return a copy of this instance with the safe insets replaced with the argument.
Adrian Roos24264212018-02-19 16:26:15 +0100546 *
Adrian Roos24264212018-02-19 16:26:15 +0100547 * @hide
548 */
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100549 public DisplayCutout replaceSafeInsets(Rect safeInsets) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200550 return new DisplayCutout(new Rect(safeInsets), mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100551 }
552
553 private static int atLeastZero(int value) {
554 return value < 0 ? 0 : value;
555 }
556
Adrian Roosd4970af2017-11-10 15:48:01 +0100557
558 /**
Adrian Roos24264212018-02-19 16:26:15 +0100559 * Creates an instance from a bounding rect.
Adrian Roosd4970af2017-11-10 15:48:01 +0100560 *
561 * @hide
562 */
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200563 @VisibleForTesting
Issei Suzuki43190bd2018-08-20 17:28:41 +0200564 public static DisplayCutout fromBoundingRect(
565 int left, int top, int right, int bottom, @BoundsPosition int pos) {
566 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH];
567 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) {
568 bounds[i] = (pos == i) ? new Rect(left, top, right, bottom) : new Rect();
569 }
570 return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */);
Adrian Roos1cf585052018-01-03 18:43:27 +0100571 }
Adrian Roosd4970af2017-11-10 15:48:01 +0100572
Adrian Roos1cf585052018-01-03 18:43:27 +0100573 /**
574 * Creates an instance from a bounding {@link Path}.
575 *
576 * @hide
577 */
Issei Suzuki43190bd2018-08-20 17:28:41 +0200578 public static DisplayCutout fromBounds(Rect[] bounds) {
579 return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100580 }
581
582 /**
Jorim Jaggi60640512018-06-29 01:14:31 +0200583 * Creates the display cutout according to
584 * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest
585 * rectangle-base approximation of the cutout.
Adrian Roos16693f32018-01-18 17:45:52 +0100586 *
587 * @hide
588 */
Jorim Jaggi60640512018-06-29 01:14:31 +0200589 public static DisplayCutout fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight) {
590 return fromSpec(res.getString(R.string.config_mainBuiltInDisplayCutoutRectApproximation),
Adrian Roos535c4202018-04-16 16:12:40 +0200591 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100592 }
593
594 /**
Adrian Roos51072a82018-04-10 15:17:08 -0700595 * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout.
596 *
597 * @hide
598 */
599 public static Path pathFromResources(Resources res, int displayWidth, int displayHeight) {
Jorim Jaggi60640512018-06-29 01:14:31 +0200600 return pathAndDisplayCutoutFromSpec(
601 res.getString(R.string.config_mainBuiltInDisplayCutout),
Adrian Roos535c4202018-04-16 16:12:40 +0200602 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT).first;
Adrian Roos51072a82018-04-10 15:17:08 -0700603 }
604
605 /**
Adrian Roos8d13bf12018-02-21 15:17:07 +0100606 * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec.
607 *
608 * @hide
609 */
610 @VisibleForTesting(visibility = PRIVATE)
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100611 public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight,
612 float density) {
Adrian Roos51072a82018-04-10 15:17:08 -0700613 return pathAndDisplayCutoutFromSpec(spec, displayWidth, displayHeight, density).second;
614 }
615
616 private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(String spec,
617 int displayWidth, int displayHeight, float density) {
Adrian Roos16693f32018-01-18 17:45:52 +0100618 if (TextUtils.isEmpty(spec)) {
Adrian Roos51072a82018-04-10 15:17:08 -0700619 return NULL_PAIR;
Adrian Roos16693f32018-01-18 17:45:52 +0100620 }
Adrian Roos8d13bf12018-02-21 15:17:07 +0100621 synchronized (CACHE_LOCK) {
622 if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth
Adrian Roos51072a82018-04-10 15:17:08 -0700623 && sCachedDisplayHeight == displayHeight
Adrian Roos8d13bf12018-02-21 15:17:07 +0100624 && sCachedDensity == density) {
625 return sCachedCutout;
626 }
627 }
Adrian Roos16693f32018-01-18 17:45:52 +0100628 spec = spec.trim();
Adrian Roosb8b10f82018-03-15 20:09:42 +0100629 final float offsetX;
630 if (spec.endsWith(RIGHT_MARKER)) {
631 offsetX = displayWidth;
632 spec = spec.substring(0, spec.length() - RIGHT_MARKER.length()).trim();
633 } else {
634 offsetX = displayWidth / 2f;
635 }
Adrian Roos16693f32018-01-18 17:45:52 +0100636 final boolean inDp = spec.endsWith(DP_MARKER);
637 if (inDp) {
638 spec = spec.substring(0, spec.length() - DP_MARKER.length());
639 }
640
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100641 String bottomSpec = null;
642 if (spec.contains(BOTTOM_MARKER)) {
643 String[] splits = spec.split(BOTTOM_MARKER, 2);
644 spec = splits[0].trim();
645 bottomSpec = splits[1].trim();
646 }
647
648 final Path p;
Jorim Jaggi60640512018-06-29 01:14:31 +0200649 final Region r = Region.obtain();
Adrian Roos16693f32018-01-18 17:45:52 +0100650 try {
651 p = PathParser.createPathFromPathData(spec);
652 } catch (Throwable e) {
653 Log.wtf(TAG, "Could not inflate cutout: ", e);
Adrian Roos51072a82018-04-10 15:17:08 -0700654 return NULL_PAIR;
Adrian Roos16693f32018-01-18 17:45:52 +0100655 }
656
657 final Matrix m = new Matrix();
658 if (inDp) {
Adrian Roos8d13bf12018-02-21 15:17:07 +0100659 m.postScale(density, density);
Adrian Roos16693f32018-01-18 17:45:52 +0100660 }
Adrian Roosb8b10f82018-03-15 20:09:42 +0100661 m.postTranslate(offsetX, 0);
Adrian Roos16693f32018-01-18 17:45:52 +0100662 p.transform(m);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100663
Issei Suzuki43190bd2018-08-20 17:28:41 +0200664 Rect boundTop = new Rect();
665 toRectAndAddToRegion(p, r, boundTop);
666 final int topInset = boundTop.bottom;
Jorim Jaggi60640512018-06-29 01:14:31 +0200667
Issei Suzuki43190bd2018-08-20 17:28:41 +0200668 Rect boundBottom = null;
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200669 final int bottomInset;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100670 if (bottomSpec != null) {
671 final Path bottomPath;
672 try {
673 bottomPath = PathParser.createPathFromPathData(bottomSpec);
674 } catch (Throwable e) {
675 Log.wtf(TAG, "Could not inflate bottom cutout: ", e);
Adrian Roos51072a82018-04-10 15:17:08 -0700676 return NULL_PAIR;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100677 }
678 // Keep top transform
679 m.postTranslate(0, displayHeight);
680 bottomPath.transform(m);
681 p.addPath(bottomPath);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200682 boundBottom = new Rect();
683 toRectAndAddToRegion(bottomPath, r, boundBottom);
684 bottomInset = displayHeight - boundBottom.top;
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200685 } else {
686 bottomInset = 0;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100687 }
688
Issei Suzuki43190bd2018-08-20 17:28:41 +0200689 Rect safeInset = new Rect(0, topInset, 0, bottomInset);
690 final DisplayCutout cutout = new DisplayCutout(
691 safeInset, null /* boundLeft */, boundTop, null /* boundRight */, boundBottom,
692 false /* copyArguments */);
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200693
694 final Pair<Path, DisplayCutout> result = new Pair<>(p, cutout);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100695 synchronized (CACHE_LOCK) {
696 sCachedSpec = spec;
697 sCachedDisplayWidth = displayWidth;
Adrian Roos51072a82018-04-10 15:17:08 -0700698 sCachedDisplayHeight = displayHeight;
Adrian Roos8d13bf12018-02-21 15:17:07 +0100699 sCachedDensity = density;
700 sCachedCutout = result;
701 }
702 return result;
Adrian Roos16693f32018-01-18 17:45:52 +0100703 }
704
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200705 private static void toRectAndAddToRegion(Path p, Region inoutRegion, Rect inoutRect) {
Jorim Jaggi60640512018-06-29 01:14:31 +0200706 final RectF rectF = new RectF();
Jorim Jaggi60640512018-06-29 01:14:31 +0200707 p.computeBounds(rectF, false /* unused */);
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200708 rectF.round(inoutRect);
709 inoutRegion.op(inoutRect, Op.UNION);
Jorim Jaggi60640512018-06-29 01:14:31 +0200710 }
711
Adrian Roos16693f32018-01-18 17:45:52 +0100712 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100713 * Helper class for passing {@link DisplayCutout} through binder.
714 *
715 * Needed, because {@code readFromParcel} cannot be used with immutable classes.
716 *
717 * @hide
718 */
719 public static final class ParcelableWrapper implements Parcelable {
720
721 private DisplayCutout mInner;
722
723 public ParcelableWrapper() {
724 this(NO_CUTOUT);
725 }
726
727 public ParcelableWrapper(DisplayCutout cutout) {
728 mInner = cutout;
729 }
730
731 @Override
732 public int describeContents() {
733 return 0;
734 }
735
736 @Override
737 public void writeToParcel(Parcel out, int flags) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100738 writeCutoutToParcel(mInner, out, flags);
739 }
740
741 /**
742 * Writes a DisplayCutout to a {@link Parcel}.
743 *
744 * @see #readCutoutFromParcel(Parcel)
745 */
746 public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) {
747 if (cutout == null) {
748 out.writeInt(-1);
749 } else if (cutout == NO_CUTOUT) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100750 out.writeInt(0);
751 } else {
752 out.writeInt(1);
Adrian Roos1cf585052018-01-03 18:43:27 +0100753 out.writeTypedObject(cutout.mSafeInsets, flags);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200754 out.writeTypedArray(cutout.mBounds.getRects(), flags);
Adrian Roosd4970af2017-11-10 15:48:01 +0100755 }
756 }
757
758 /**
759 * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing
760 * instance.
761 *
762 * Needed for AIDL out parameters.
763 */
764 public void readFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100765 mInner = readCutoutFromParcel(in);
Adrian Roosd4970af2017-11-10 15:48:01 +0100766 }
767
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700768 public static final @android.annotation.NonNull Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() {
Adrian Roosd4970af2017-11-10 15:48:01 +0100769 @Override
770 public ParcelableWrapper createFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100771 return new ParcelableWrapper(readCutoutFromParcel(in));
Adrian Roosd4970af2017-11-10 15:48:01 +0100772 }
773
774 @Override
775 public ParcelableWrapper[] newArray(int size) {
776 return new ParcelableWrapper[size];
777 }
778 };
779
Adrian Roos1cf585052018-01-03 18:43:27 +0100780 /**
781 * Reads a DisplayCutout from a {@link Parcel}.
782 *
783 * @see #writeCutoutToParcel(DisplayCutout, Parcel, int)
784 */
785 public static DisplayCutout readCutoutFromParcel(Parcel in) {
786 int variant = in.readInt();
787 if (variant == -1) {
788 return null;
789 }
790 if (variant == 0) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100791 return NO_CUTOUT;
792 }
793
Adrian Roosd4970af2017-11-10 15:48:01 +0100794 Rect safeInsets = in.readTypedObject(Rect.CREATOR);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200795 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH];
796 in.readTypedArray(bounds, Rect.CREATOR);
Adrian Roosd4970af2017-11-10 15:48:01 +0100797
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100798 return new DisplayCutout(safeInsets, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100799 }
800
801 public DisplayCutout get() {
802 return mInner;
803 }
804
805 public void set(ParcelableWrapper cutout) {
806 mInner = cutout.get();
807 }
808
809 public void set(DisplayCutout cutout) {
810 mInner = cutout;
811 }
812
813 @Override
814 public int hashCode() {
815 return mInner.hashCode();
816 }
817
818 @Override
819 public boolean equals(Object o) {
820 return o instanceof ParcelableWrapper
821 && mInner.equals(((ParcelableWrapper) o).mInner);
822 }
823
824 @Override
825 public String toString() {
826 return String.valueOf(mInner);
827 }
828 }
829}