blob: 679c912e846f2a531026c96148f89f035833a31a [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 Roosd07bafd2017-12-11 17:30:56 +010034import android.graphics.Path;
Adrian Roosd4970af2017-11-10 15:48:01 +010035import android.graphics.Rect;
36import android.os.Parcel;
37import android.os.Parcelable;
Adrian Roos16693f32018-01-18 17:45:52 +010038import android.text.TextUtils;
Adrian Roos51072a82018-04-10 15:17:08 -070039import android.util.Pair;
Vishnu Nair1d0fa072018-01-04 07:53:00 -080040import android.util.proto.ProtoOutputStream;
Adrian Roosd4970af2017-11-10 15:48:01 +010041
Adrian Roos16693f32018-01-18 17:45:52 +010042import com.android.internal.R;
Adrian Roos8d13bf12018-02-21 15:17:07 +010043import com.android.internal.annotations.GuardedBy;
Adrian Roosd4970af2017-11-10 15:48:01 +010044import com.android.internal.annotations.VisibleForTesting;
45
Issei Suzuki43190bd2018-08-20 17:28:41 +020046import java.lang.annotation.Retention;
47import java.lang.annotation.RetentionPolicy;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010048import java.util.ArrayList;
Issei Suzuki43190bd2018-08-20 17:28:41 +020049import java.util.Arrays;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010050import java.util.List;
Adrian Roosd4970af2017-11-10 15:48:01 +010051
52/**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010053 * Represents the area of the display that is not functional for displaying content.
Adrian Roosd4970af2017-11-10 15:48:01 +010054 *
55 * <p>{@code DisplayCutout} is immutable.
Adrian Roosd4970af2017-11-10 15:48:01 +010056 */
57public final class DisplayCutout {
58
Adrian Roos16693f32018-01-18 17:45:52 +010059 private static final String TAG = "DisplayCutout";
Adrian Roos16693f32018-01-18 17:45:52 +010060
Adrian Roosc84df772018-01-19 21:20:22 +010061 /**
62 * Category for overlays that allow emulating a display cutout on devices that don't have
63 * one.
64 *
65 * @see android.content.om.IOverlayManager
66 * @hide
67 */
68 public static final String EMULATION_OVERLAY_CATEGORY =
69 "com.android.internal.display_cutout_emulation";
70
Adrian Roosd07bafd2017-12-11 17:30:56 +010071 private static final Rect ZERO_RECT = new Rect();
Adrian Roosd4970af2017-11-10 15:48:01 +010072
73 /**
Adrian Roosd07bafd2017-12-11 17:30:56 +010074 * An instance where {@link #isEmpty()} returns {@code true}.
Adrian Roosd4970af2017-11-10 15:48:01 +010075 *
76 * @hide
77 */
Issei Suzuki43190bd2018-08-20 17:28:41 +020078 public static final DisplayCutout NO_CUTOUT = new DisplayCutout(
shawnlin7dabcab2020-01-13 22:24:43 +080079 ZERO_RECT, Insets.NONE, ZERO_RECT, ZERO_RECT, ZERO_RECT, ZERO_RECT,
Adrian Roos6a4fa0e2018-03-05 19:50:16 +010080 false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +010081
Adrian Roos8d13bf12018-02-21 15:17:07 +010082
Adrian Roos51072a82018-04-10 15:17:08 -070083 private static final Pair<Path, DisplayCutout> NULL_PAIR = new Pair<>(null, null);
Adrian Roos8d13bf12018-02-21 15:17:07 +010084 private static final Object CACHE_LOCK = new Object();
Adrian Roos51072a82018-04-10 15:17:08 -070085
Adrian Roos8d13bf12018-02-21 15:17:07 +010086 @GuardedBy("CACHE_LOCK")
87 private static String sCachedSpec;
88 @GuardedBy("CACHE_LOCK")
89 private static int sCachedDisplayWidth;
90 @GuardedBy("CACHE_LOCK")
Adrian Roos51072a82018-04-10 15:17:08 -070091 private static int sCachedDisplayHeight;
92 @GuardedBy("CACHE_LOCK")
Adrian Roos8d13bf12018-02-21 15:17:07 +010093 private static float sCachedDensity;
94 @GuardedBy("CACHE_LOCK")
Adrian Roos51072a82018-04-10 15:17:08 -070095 private static Pair<Path, DisplayCutout> sCachedCutout = NULL_PAIR;
shawnlin7dabcab2020-01-13 22:24:43 +080096 @GuardedBy("CACHE_LOCK")
97 private static Insets sCachedWaterfallInsets;
Adrian Roos8d13bf12018-02-21 15:17:07 +010098
Adrian Roosd4970af2017-11-10 15:48:01 +010099 private final Rect mSafeInsets;
shawnlin7dabcab2020-01-13 22:24:43 +0800100 @NonNull
101 private final Insets mWaterfallInsets;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200102
103
104 /**
105 * The bound is at the left of the screen.
106 * @hide
107 */
108 public static final int BOUNDS_POSITION_LEFT = 0;
109
110 /**
111 * The bound is at the top of the screen.
112 * @hide
113 */
114 public static final int BOUNDS_POSITION_TOP = 1;
115
116 /**
117 * The bound is at the right of the screen.
118 * @hide
119 */
120 public static final int BOUNDS_POSITION_RIGHT = 2;
121
122 /**
123 * The bound is at the bottom of the screen.
124 * @hide
125 */
126 public static final int BOUNDS_POSITION_BOTTOM = 3;
127
128 /**
129 * The number of possible positions at which bounds can be located.
130 * @hide
131 */
132 public static final int BOUNDS_POSITION_LENGTH = 4;
133
134 /** @hide */
135 @IntDef(prefix = { "BOUNDS_POSITION_" }, value = {
136 BOUNDS_POSITION_LEFT,
137 BOUNDS_POSITION_TOP,
138 BOUNDS_POSITION_RIGHT,
139 BOUNDS_POSITION_BOTTOM
140 })
141 @Retention(RetentionPolicy.SOURCE)
142 public @interface BoundsPosition {}
143
144 private static class Bounds {
145 private final Rect[] mRects;
146
147 private Bounds(Rect left, Rect top, Rect right, Rect bottom, boolean copyArguments) {
148 mRects = new Rect[BOUNDS_POSITION_LENGTH];
149 mRects[BOUNDS_POSITION_LEFT] = getCopyOrRef(left, copyArguments);
150 mRects[BOUNDS_POSITION_TOP] = getCopyOrRef(top, copyArguments);
151 mRects[BOUNDS_POSITION_RIGHT] = getCopyOrRef(right, copyArguments);
152 mRects[BOUNDS_POSITION_BOTTOM] = getCopyOrRef(bottom, copyArguments);
153
154 }
155
156 private Bounds(Rect[] rects, boolean copyArguments) {
157 if (rects.length != BOUNDS_POSITION_LENGTH) {
158 throw new IllegalArgumentException(
159 "rects must have exactly 4 elements: rects=" + Arrays.toString(rects));
160 }
161 if (copyArguments) {
162 mRects = new Rect[BOUNDS_POSITION_LENGTH];
163 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) {
164 mRects[i] = new Rect(rects[i]);
165 }
166 } else {
167 for (Rect rect : rects) {
168 if (rect == null) {
169 throw new IllegalArgumentException(
170 "rects must have non-null elements: rects="
171 + Arrays.toString(rects));
172 }
173 }
174 mRects = rects;
175 }
176 }
177
178 private boolean isEmpty() {
179 for (Rect rect : mRects) {
180 if (!rect.isEmpty()) {
181 return false;
182 }
183 }
184 return true;
185 }
186
187 private Rect getRect(@BoundsPosition int pos) {
188 return new Rect(mRects[pos]);
189 }
190
191 private Rect[] getRects() {
192 Rect[] rects = new Rect[BOUNDS_POSITION_LENGTH];
193 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) {
194 rects[i] = new Rect(mRects[i]);
195 }
196 return rects;
197 }
198
199 @Override
200 public int hashCode() {
201 int result = 0;
202 for (Rect rect : mRects) {
203 result = result * 48271 + rect.hashCode();
204 }
205 return result;
206 }
207 @Override
208 public boolean equals(Object o) {
209 if (o == this) {
210 return true;
211 }
212 if (o instanceof Bounds) {
213 Bounds b = (Bounds) o;
214 return Arrays.deepEquals(mRects, b.mRects);
215 }
216 return false;
217 }
218
219 @Override
220 public String toString() {
221 return "Bounds=" + Arrays.toString(mRects);
222 }
223
224 }
225
226 private final Bounds mBounds;
227
228 /**
229 * Creates a DisplayCutout instance.
230 *
Issei Suzuki8b0e6172019-01-14 17:04:33 +0100231 * <p>Note that this is only useful for tests. For production code, developers should always
232 * use a {@link DisplayCutout} obtained from the system.</p>
233 *
Issei Suzuki43190bd2018-08-20 17:28:41 +0200234 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
235 * {@link #getSafeInsetTop()} etc.
236 * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed,
237 * it's treated as an empty rectangle (0,0)-(0,0).
238 * @param boundTop the top 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 boundRight the right bounding rect of the display cutout in pixels. If null is
241 * passed, it's treated as an empty rectangle (0,0)-(0,0).
242 * @param boundBottom the bottom 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 */
245 // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE)
Adrian Roosde363d02018-10-12 13:03:56 +0200246 public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft,
247 @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom) {
shawnlin7dabcab2020-01-13 22:24:43 +0800248 this(safeInsets.toRect(), Insets.NONE, boundLeft, boundTop, boundRight, boundBottom, true);
249 }
250
251 /**
252 * Creates a DisplayCutout instance.
253 *
254 * <p>Note that this is only useful for tests. For production code, developers should always
255 * use a {@link DisplayCutout} obtained from the system.</p>
256 *
257 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
258 * {@link #getSafeInsetTop()} etc.
259 * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed,
260 * it's treated as an empty rectangle (0,0)-(0,0).
261 * @param boundTop the top bounding rect of the display cutout in pixels. If null is passed,
262 * it's treated as an empty rectangle (0,0)-(0,0).
263 * @param boundRight the right bounding rect of the display cutout in pixels. If null is
264 * passed, it's treated as an empty rectangle (0,0)-(0,0).
265 * @param boundBottom the bottom bounding rect of the display cutout in pixels. If null is
266 * passed, it's treated as an empty rectangle (0,0)-(0,0).
267 * @param waterfallInsets the insets for the curved areas in waterfall display.
268 */
269 public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft,
270 @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom,
271 @NonNull Insets waterfallInsets) {
272 this(safeInsets.toRect(), waterfallInsets, boundLeft, boundTop, boundRight, boundBottom,
273 true);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200274 }
Adrian Roos9bbd9662018-03-02 14:31:37 +0100275
276 /**
277 * Creates a DisplayCutout instance.
278 *
Issei Suzuki8b0e6172019-01-14 17:04:33 +0100279 * <p>Note that this is only useful for tests. For production code, developers should always
280 * use a {@link DisplayCutout} obtained from the system.</p>
281 *
Adrian Roos9bbd9662018-03-02 14:31:37 +0100282 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
283 * {@link #getSafeInsetTop()} etc.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100284 * @param boundingRects the bounding rects of the display cutouts as returned by
285 * {@link #getBoundingRects()} ()}.
Issei Suzuki43190bd2018-08-20 17:28:41 +0200286 * @deprecated Use {@link DisplayCutout#DisplayCutout(Insets, Rect, Rect, Rect, Rect)} instead.
Adrian Roos9bbd9662018-03-02 14:31:37 +0100287 */
288 // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE)
Issei Suzuki43190bd2018-08-20 17:28:41 +0200289 @Deprecated
Adrian Roosde363d02018-10-12 13:03:56 +0200290 public DisplayCutout(@Nullable Rect safeInsets, @Nullable List<Rect> boundingRects) {
shawnlin7dabcab2020-01-13 22:24:43 +0800291 this(safeInsets, Insets.NONE, extractBoundsFromList(safeInsets, boundingRects),
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100292 true /* copyArguments */);
Adrian Roos9bbd9662018-03-02 14:31:37 +0100293 }
Adrian Roosd4970af2017-11-10 15:48:01 +0100294
295 /**
296 * Creates a DisplayCutout instance.
297 *
Issei Suzuki43190bd2018-08-20 17:28:41 +0200298 * @param safeInsets the insets from each edge which avoid the display cutout as returned by
299 * {@link #getSafeInsetTop()} etc.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100300 * @param copyArguments if true, create a copy of the arguments. If false, the passed arguments
301 * are not copied and MUST remain unchanged forever.
Adrian Roosd4970af2017-11-10 15:48:01 +0100302 */
shawnlin7dabcab2020-01-13 22:24:43 +0800303 private DisplayCutout(Rect safeInsets, Insets waterfallInsets, Rect boundLeft,
304 Rect boundTop, Rect boundRight, Rect boundBottom, boolean copyArguments) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200305 mSafeInsets = getCopyOrRef(safeInsets, copyArguments);
shawnlin7dabcab2020-01-13 22:24:43 +0800306 mWaterfallInsets = waterfallInsets == null ? Insets.NONE : waterfallInsets;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200307 mBounds = new Bounds(boundLeft, boundTop, boundRight, boundBottom, copyArguments);
308 }
309
shawnlin7dabcab2020-01-13 22:24:43 +0800310 private DisplayCutout(Rect safeInsets, Insets waterfallInsets, Rect[] bounds,
311 boolean copyArguments) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200312 mSafeInsets = getCopyOrRef(safeInsets, copyArguments);
shawnlin7dabcab2020-01-13 22:24:43 +0800313 mWaterfallInsets = waterfallInsets == null ? Insets.NONE : waterfallInsets;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200314 mBounds = new Bounds(bounds, copyArguments);
315 }
316
shawnlin7dabcab2020-01-13 22:24:43 +0800317 private DisplayCutout(Rect safeInsets, Insets waterfallInsets, Bounds bounds) {
Issei Suzuki43190bd2018-08-20 17:28:41 +0200318 mSafeInsets = safeInsets;
shawnlin7dabcab2020-01-13 22:24:43 +0800319 mWaterfallInsets = waterfallInsets == null ? Insets.NONE : waterfallInsets;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200320 mBounds = bounds;
321
322 }
323
324 private static Rect getCopyOrRef(Rect r, boolean copyArguments) {
325 if (r == null) {
326 return ZERO_RECT;
327 } else if (copyArguments) {
328 return new Rect(r);
329 } else {
330 return r;
331 }
332 }
333
334 /**
shawnlin7dabcab2020-01-13 22:24:43 +0800335 * Return the waterfall insets.
336 */
337 public @NonNull Insets getWaterfallInsets() {
338 return mWaterfallInsets;
339 }
340
341
342 /**
Issei Suzuki43190bd2018-08-20 17:28:41 +0200343 * Find the position of the bounding rect, and create an array of Rect whose index represents
344 * the position (= BoundsPosition).
345 *
346 * @hide
347 */
348 public static Rect[] extractBoundsFromList(Rect safeInsets, List<Rect> boundingRects) {
349 Rect[] sortedBounds = new Rect[BOUNDS_POSITION_LENGTH];
350 for (int i = 0; i < sortedBounds.length; ++i) {
351 sortedBounds[i] = ZERO_RECT;
352 }
Adrian Roosde363d02018-10-12 13:03:56 +0200353 if (safeInsets != null && boundingRects != null) {
Issei Suzuki6b5f5e92019-07-15 15:44:01 +0200354 // There is at most one non-functional area per short edge of the device, but none
355 // on the long edges, so either a) safeInsets.top and safeInsets.bottom is 0, or
356 // b) safeInsets.left and safeInset.right is 0.
357 final boolean topBottomInset = safeInsets.top > 0 || safeInsets.bottom > 0;
Adrian Roosde363d02018-10-12 13:03:56 +0200358 for (Rect bound : boundingRects) {
Issei Suzuki6b5f5e92019-07-15 15:44:01 +0200359 if (topBottomInset) {
360 if (bound.top == 0) {
361 sortedBounds[BOUNDS_POSITION_TOP] = bound;
362 } else {
363 sortedBounds[BOUNDS_POSITION_BOTTOM] = bound;
364 }
365 } else {
366 if (bound.left == 0) {
367 sortedBounds[BOUNDS_POSITION_LEFT] = bound;
368 } else {
369 sortedBounds[BOUNDS_POSITION_RIGHT] = bound;
370 }
Adrian Roosde363d02018-10-12 13:03:56 +0200371 }
Issei Suzuki43190bd2018-08-20 17:28:41 +0200372 }
373 }
374 return sortedBounds;
375 }
376
377 /**
378 * Returns true if there is no cutout, i.e. the bounds are empty.
379 *
380 * @hide
381 */
382 public boolean isBoundsEmpty() {
383 return mBounds.isEmpty();
Adrian Roosd4970af2017-11-10 15:48:01 +0100384 }
385
386 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100387 * Returns true if the safe insets are empty (and therefore the current view does not
388 * overlap with the cutout or cutout area).
Adrian Roosd4970af2017-11-10 15:48:01 +0100389 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100390 * @hide
Adrian Roosd4970af2017-11-10 15:48:01 +0100391 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100392 public boolean isEmpty() {
393 return mSafeInsets.equals(ZERO_RECT);
Adrian Roosd4970af2017-11-10 15:48:01 +0100394 }
395
Tiger Huange06d0712020-02-26 20:36:13 +0800396 /**
397 * Returns the inset from the top which avoids the display cutout in pixels.
398 *
399 * @see WindowInsets.Type#displayCutout()
400 */
Adrian Roosd4970af2017-11-10 15:48:01 +0100401 public int getSafeInsetTop() {
402 return mSafeInsets.top;
403 }
404
Tiger Huange06d0712020-02-26 20:36:13 +0800405 /**
406 * Returns the inset from the bottom which avoids the display cutout in pixels.
407 *
408 * @see WindowInsets.Type#displayCutout()
409 */
Adrian Roosd4970af2017-11-10 15:48:01 +0100410 public int getSafeInsetBottom() {
411 return mSafeInsets.bottom;
412 }
413
Tiger Huange06d0712020-02-26 20:36:13 +0800414 /**
415 * Returns the inset from the left which avoids the display cutout in pixels.
416 *
417 * @see WindowInsets.Type#displayCutout()
418 */
Adrian Roosd4970af2017-11-10 15:48:01 +0100419 public int getSafeInsetLeft() {
420 return mSafeInsets.left;
421 }
422
Tiger Huange06d0712020-02-26 20:36:13 +0800423 /**
424 * Returns the inset from the right which avoids the display cutout in pixels.
425 *
426 * @see WindowInsets.Type#displayCutout()
427 */
Adrian Roosd4970af2017-11-10 15:48:01 +0100428 public int getSafeInsetRight() {
429 return mSafeInsets.right;
430 }
431
432 /**
Adrian Roos9bbd9662018-03-02 14:31:37 +0100433 * Returns the safe insets in a rect in pixel units.
Adrian Roosd4970af2017-11-10 15:48:01 +0100434 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100435 * @return a rect which is set to the safe insets.
Adrian Roosd4970af2017-11-10 15:48:01 +0100436 * @hide
437 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100438 public Rect getSafeInsets() {
439 return new Rect(mSafeInsets);
Adrian Roosd4970af2017-11-10 15:48:01 +0100440 }
441
442 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100443 * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional
444 * area on the display.
Adrian Roosd4970af2017-11-10 15:48:01 +0100445 *
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100446 * There will be at most one non-functional area per short edge of the device, and none on
447 * the long edges.
448 *
Issei Suzuki43190bd2018-08-20 17:28:41 +0200449 * @return a list of bounding {@code Rect}s, one for each display cutout area. No empty Rect is
450 * returned.
Adrian Roosd4970af2017-11-10 15:48:01 +0100451 */
Adrian Roosde363d02018-10-12 13:03:56 +0200452 @NonNull
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100453 public List<Rect> getBoundingRects() {
454 List<Rect> result = new ArrayList<>();
Issei Suzuki43190bd2018-08-20 17:28:41 +0200455 for (Rect bound : getBoundingRectsAll()) {
456 if (!bound.isEmpty()) {
457 result.add(new Rect(bound));
458 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100459 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100460 return result;
Adrian Roosd4970af2017-11-10 15:48:01 +0100461 }
462
Issei Suzuki43190bd2018-08-20 17:28:41 +0200463 /**
464 * Returns an array of {@code Rect}s, each of which is the bounding rectangle for a non-
465 * functional area on the display. Ordinal value of BoundPosition is used as an index of
466 * the array.
467 *
468 * There will be at most one non-functional area per short edge of the device, and none on
469 * the long edges.
470 *
471 * @return an array of bounding {@code Rect}s, one for each display cutout area. This might
472 * contain ZERO_RECT, which means there is no cutout area at the position.
473 *
474 * @hide
475 */
476 public Rect[] getBoundingRectsAll() {
477 return mBounds.getRects();
478 }
479
480 /**
481 * Returns a bounding rectangle for a non-functional area on the display which is located on
482 * the left of the screen.
483 *
484 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
485 * is returned.
486 */
487 public @NonNull Rect getBoundingRectLeft() {
488 return mBounds.getRect(BOUNDS_POSITION_LEFT);
489 }
490
491 /**
492 * Returns a bounding rectangle for a non-functional area on the display which is located on
493 * the top of the screen.
494 *
495 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
496 * is returned.
497 */
498 public @NonNull Rect getBoundingRectTop() {
499 return mBounds.getRect(BOUNDS_POSITION_TOP);
500 }
501
502 /**
503 * Returns a bounding rectangle for a non-functional area on the display which is located on
504 * the right of the screen.
505 *
506 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
507 * is returned.
508 */
509 public @NonNull Rect getBoundingRectRight() {
510 return mBounds.getRect(BOUNDS_POSITION_RIGHT);
511 }
512
513 /**
514 * Returns a bounding rectangle for a non-functional area on the display which is located on
515 * the bottom of the screen.
516 *
517 * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle
518 * is returned.
519 */
520 public @NonNull Rect getBoundingRectBottom() {
521 return mBounds.getRect(BOUNDS_POSITION_BOTTOM);
522 }
523
Adrian Roosd4970af2017-11-10 15:48:01 +0100524 @Override
525 public int hashCode() {
shawnlin7dabcab2020-01-13 22:24:43 +0800526 return (mSafeInsets.hashCode() * 48271 + mBounds.hashCode()) * 48271
527 + mWaterfallInsets.hashCode();
Adrian Roosd4970af2017-11-10 15:48:01 +0100528 }
529
530 @Override
531 public boolean equals(Object o) {
532 if (o == this) {
533 return true;
534 }
535 if (o instanceof DisplayCutout) {
536 DisplayCutout c = (DisplayCutout) o;
shawnlin7dabcab2020-01-13 22:24:43 +0800537 return mSafeInsets.equals(c.mSafeInsets) && mBounds.equals(c.mBounds)
538 && mWaterfallInsets.equals(c.mWaterfallInsets);
Adrian Roosd4970af2017-11-10 15:48:01 +0100539 }
540 return false;
541 }
542
543 @Override
544 public String toString() {
545 return "DisplayCutout{insets=" + mSafeInsets
shawnlin7dabcab2020-01-13 22:24:43 +0800546 + " waterfall=" + mWaterfallInsets
Issei Suzuki43190bd2018-08-20 17:28:41 +0200547 + " boundingRect={" + mBounds + "}"
Adrian Roosd4970af2017-11-10 15:48:01 +0100548 + "}";
549 }
550
551 /**
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800552 * @hide
553 */
Jeffrey Huangcb782852019-12-05 11:28:11 -0800554 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800555 final long token = proto.start(fieldId);
Jeffrey Huangcb782852019-12-05 11:28:11 -0800556 mSafeInsets.dumpDebug(proto, INSETS);
557 mBounds.getRect(BOUNDS_POSITION_LEFT).dumpDebug(proto, BOUND_LEFT);
558 mBounds.getRect(BOUNDS_POSITION_TOP).dumpDebug(proto, BOUND_TOP);
559 mBounds.getRect(BOUNDS_POSITION_RIGHT).dumpDebug(proto, BOUND_RIGHT);
560 mBounds.getRect(BOUNDS_POSITION_BOTTOM).dumpDebug(proto, BOUND_BOTTOM);
shawnlin7dabcab2020-01-13 22:24:43 +0800561 mWaterfallInsets.toRect().dumpDebug(proto, INSETS);
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800562 proto.end(token);
563 }
564
565 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100566 * Insets the reference frame of the cutout in the given directions.
567 *
568 * @return a copy of this instance which has been inset
569 * @hide
570 */
571 public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) {
Jorim Jaggi4981f152019-03-26 18:58:45 +0100572 if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0
shawnlin16ba0682020-02-21 19:35:19 +0800573 || (isBoundsEmpty() && mWaterfallInsets.equals(Insets.NONE))) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100574 return this;
575 }
576
shawnlin16ba0682020-02-21 19:35:19 +0800577 Rect safeInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom,
578 new Rect(mSafeInsets));
Adrian Roosd4970af2017-11-10 15:48:01 +0100579
Jorim Jaggi4981f152019-03-26 18:58:45 +0100580 // If we are not cutting off part of the cutout by insetting it on bottom/right, and we also
581 // don't move it around, we can avoid the allocation and copy of the instance.
582 if (insetLeft == 0 && insetTop == 0 && mSafeInsets.equals(safeInsets)) {
583 return this;
584 }
585
shawnlin16ba0682020-02-21 19:35:19 +0800586 Rect waterfallInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom,
587 mWaterfallInsets.toRect());
588
Issei Suzuki43190bd2018-08-20 17:28:41 +0200589 Rect[] bounds = mBounds.getRects();
590 for (int i = 0; i < bounds.length; ++i) {
591 if (!bounds[i].equals(ZERO_RECT)) {
592 bounds[i].offset(-insetLeft, -insetTop);
593 }
594 }
595
shawnlin16ba0682020-02-21 19:35:19 +0800596 return new DisplayCutout(safeInsets, Insets.of(waterfallInsets), bounds,
597 false /* copyArguments */);
598 }
599
600 private Rect insetInsets(int insetLeft, int insetTop, int insetRight, int insetBottom,
601 Rect insets) {
602 // Note: it's not really well defined what happens when the inset is negative, because we
603 // don't know if the safe inset needs to expand in general.
604 if (insetTop > 0 || insets.top > 0) {
605 insets.top = atLeastZero(insets.top - insetTop);
606 }
607 if (insetBottom > 0 || insets.bottom > 0) {
608 insets.bottom = atLeastZero(insets.bottom - insetBottom);
609 }
610 if (insetLeft > 0 || insets.left > 0) {
611 insets.left = atLeastZero(insets.left - insetLeft);
612 }
613 if (insetRight > 0 || insets.right > 0) {
614 insets.right = atLeastZero(insets.right - insetRight);
615 }
616 return insets;
Adrian Roosd4970af2017-11-10 15:48:01 +0100617 }
618
619 /**
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100620 * Returns a copy of this instance with the safe insets replaced with the parameter.
Adrian Roos24264212018-02-19 16:26:15 +0100621 *
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100622 * @param safeInsets the new safe insets in pixels
623 * @return a copy of this instance with the safe insets replaced with the argument.
Adrian Roos24264212018-02-19 16:26:15 +0100624 *
Adrian Roos24264212018-02-19 16:26:15 +0100625 * @hide
626 */
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100627 public DisplayCutout replaceSafeInsets(Rect safeInsets) {
shawnlin7dabcab2020-01-13 22:24:43 +0800628 return new DisplayCutout(new Rect(safeInsets), mWaterfallInsets, mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100629 }
630
631 private static int atLeastZero(int value) {
632 return value < 0 ? 0 : value;
633 }
634
Adrian Roosd4970af2017-11-10 15:48:01 +0100635
636 /**
Adrian Roos24264212018-02-19 16:26:15 +0100637 * Creates an instance from a bounding rect.
Adrian Roosd4970af2017-11-10 15:48:01 +0100638 *
639 * @hide
640 */
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200641 @VisibleForTesting
Issei Suzuki43190bd2018-08-20 17:28:41 +0200642 public static DisplayCutout fromBoundingRect(
643 int left, int top, int right, int bottom, @BoundsPosition int pos) {
644 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH];
645 for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) {
646 bounds[i] = (pos == i) ? new Rect(left, top, right, bottom) : new Rect();
647 }
shawnlin7dabcab2020-01-13 22:24:43 +0800648 return new DisplayCutout(ZERO_RECT, Insets.NONE, bounds, false /* copyArguments */);
649 }
650
651 /**
652 * Creates an instance from a bounding and waterfall insets.
653 *
654 * @hide
655 */
656 public static DisplayCutout fromBoundsAndWaterfall(Rect[] bounds, Insets waterfallInsets) {
657 return new DisplayCutout(ZERO_RECT, waterfallInsets, bounds, false /* copyArguments */);
Adrian Roos1cf585052018-01-03 18:43:27 +0100658 }
Adrian Roosd4970af2017-11-10 15:48:01 +0100659
Adrian Roos1cf585052018-01-03 18:43:27 +0100660 /**
661 * Creates an instance from a bounding {@link Path}.
662 *
663 * @hide
664 */
Issei Suzuki43190bd2018-08-20 17:28:41 +0200665 public static DisplayCutout fromBounds(Rect[] bounds) {
shawnlin7dabcab2020-01-13 22:24:43 +0800666 return new DisplayCutout(ZERO_RECT, Insets.NONE, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100667 }
668
669 /**
Jorim Jaggi60640512018-06-29 01:14:31 +0200670 * Creates the display cutout according to
671 * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest
672 * rectangle-base approximation of the cutout.
Adrian Roos16693f32018-01-18 17:45:52 +0100673 *
674 * @hide
675 */
Jorim Jaggi60640512018-06-29 01:14:31 +0200676 public static DisplayCutout fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight) {
677 return fromSpec(res.getString(R.string.config_mainBuiltInDisplayCutoutRectApproximation),
shawnlin7dabcab2020-01-13 22:24:43 +0800678 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT,
679 loadWaterfallInset(res));
Adrian Roos8d13bf12018-02-21 15:17:07 +0100680 }
681
682 /**
Adrian Roos51072a82018-04-10 15:17:08 -0700683 * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout.
684 *
685 * @hide
686 */
687 public static Path pathFromResources(Resources res, int displayWidth, int displayHeight) {
Jorim Jaggi60640512018-06-29 01:14:31 +0200688 return pathAndDisplayCutoutFromSpec(
689 res.getString(R.string.config_mainBuiltInDisplayCutout),
shawnlin7dabcab2020-01-13 22:24:43 +0800690 displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT,
691 loadWaterfallInset(res)).first;
Adrian Roos51072a82018-04-10 15:17:08 -0700692 }
693
694 /**
Adrian Roos8d13bf12018-02-21 15:17:07 +0100695 * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec.
696 *
697 * @hide
698 */
699 @VisibleForTesting(visibility = PRIVATE)
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100700 public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight,
shawnlin7dabcab2020-01-13 22:24:43 +0800701 float density, Insets waterfallInsets) {
702 return pathAndDisplayCutoutFromSpec(
703 spec, displayWidth, displayHeight, density, waterfallInsets).second;
Adrian Roos51072a82018-04-10 15:17:08 -0700704 }
705
706 private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(String spec,
shawnlin7dabcab2020-01-13 22:24:43 +0800707 int displayWidth, int displayHeight, float density, Insets waterfallInsets) {
708 if (TextUtils.isEmpty(spec) && waterfallInsets.equals(Insets.NONE)) {
Adrian Roos51072a82018-04-10 15:17:08 -0700709 return NULL_PAIR;
Adrian Roos16693f32018-01-18 17:45:52 +0100710 }
shawnlin7dabcab2020-01-13 22:24:43 +0800711
Adrian Roos8d13bf12018-02-21 15:17:07 +0100712 synchronized (CACHE_LOCK) {
713 if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth
Adrian Roos51072a82018-04-10 15:17:08 -0700714 && sCachedDisplayHeight == displayHeight
shawnlin7dabcab2020-01-13 22:24:43 +0800715 && sCachedDensity == density
716 && waterfallInsets.equals(sCachedWaterfallInsets)) {
Adrian Roos8d13bf12018-02-21 15:17:07 +0100717 return sCachedCutout;
718 }
719 }
Adrian Roos16693f32018-01-18 17:45:52 +0100720
Felka Chang489cf262019-12-26 13:36:23 +0800721 spec = spec.trim();
shawnlin7dabcab2020-01-13 22:24:43 +0800722
Felka Chang489cf262019-12-26 13:36:23 +0800723 CutoutSpecification cutoutSpec = new CutoutSpecification.Parser(density,
724 displayWidth, displayHeight).parse(spec);
725 Rect safeInset = cutoutSpec.getSafeInset();
726 final Rect boundLeft = cutoutSpec.getLeftBound();
727 final Rect boundTop = cutoutSpec.getTopBound();
728 final Rect boundRight = cutoutSpec.getRightBound();
729 final Rect boundBottom = cutoutSpec.getBottomBound();
shawnlin7dabcab2020-01-13 22:24:43 +0800730
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100731
shawnlin7dabcab2020-01-13 22:24:43 +0800732 if (!waterfallInsets.equals(Insets.NONE)) {
733 safeInset.set(
734 Math.max(waterfallInsets.left, safeInset.left),
735 Math.max(waterfallInsets.top, safeInset.top),
736 Math.max(waterfallInsets.right, safeInset.right),
737 Math.max(waterfallInsets.bottom, safeInset.bottom));
738 }
Adrian Roos8c28c7c2018-08-20 13:43:38 +0200739
shawnlin7dabcab2020-01-13 22:24:43 +0800740 final DisplayCutout cutout = new DisplayCutout(
Felka Chang489cf262019-12-26 13:36:23 +0800741 safeInset, waterfallInsets, boundLeft, boundTop,
742 boundRight, boundBottom, false /* copyArguments */);
743 final Pair<Path, DisplayCutout> result = new Pair<>(cutoutSpec.getPath(), cutout);
Adrian Roos8d13bf12018-02-21 15:17:07 +0100744 synchronized (CACHE_LOCK) {
745 sCachedSpec = spec;
746 sCachedDisplayWidth = displayWidth;
Adrian Roos51072a82018-04-10 15:17:08 -0700747 sCachedDisplayHeight = displayHeight;
Adrian Roos8d13bf12018-02-21 15:17:07 +0100748 sCachedDensity = density;
749 sCachedCutout = result;
shawnlin7dabcab2020-01-13 22:24:43 +0800750 sCachedWaterfallInsets = waterfallInsets;
Adrian Roos8d13bf12018-02-21 15:17:07 +0100751 }
752 return result;
Adrian Roos16693f32018-01-18 17:45:52 +0100753 }
754
shawnlin7dabcab2020-01-13 22:24:43 +0800755 private static Insets loadWaterfallInset(Resources res) {
756 return Insets.of(
757 res.getDimensionPixelSize(R.dimen.waterfall_display_left_edge_size),
758 res.getDimensionPixelSize(R.dimen.waterfall_display_top_edge_size),
759 res.getDimensionPixelSize(R.dimen.waterfall_display_right_edge_size),
760 res.getDimensionPixelSize(R.dimen.waterfall_display_bottom_edge_size));
761 }
762
Adrian Roos16693f32018-01-18 17:45:52 +0100763 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100764 * Helper class for passing {@link DisplayCutout} through binder.
765 *
766 * Needed, because {@code readFromParcel} cannot be used with immutable classes.
767 *
768 * @hide
769 */
770 public static final class ParcelableWrapper implements Parcelable {
771
772 private DisplayCutout mInner;
773
774 public ParcelableWrapper() {
775 this(NO_CUTOUT);
776 }
777
778 public ParcelableWrapper(DisplayCutout cutout) {
779 mInner = cutout;
780 }
781
782 @Override
783 public int describeContents() {
784 return 0;
785 }
786
787 @Override
788 public void writeToParcel(Parcel out, int flags) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100789 writeCutoutToParcel(mInner, out, flags);
790 }
791
792 /**
793 * Writes a DisplayCutout to a {@link Parcel}.
794 *
795 * @see #readCutoutFromParcel(Parcel)
796 */
797 public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) {
798 if (cutout == null) {
799 out.writeInt(-1);
800 } else if (cutout == NO_CUTOUT) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100801 out.writeInt(0);
802 } else {
803 out.writeInt(1);
Adrian Roos1cf585052018-01-03 18:43:27 +0100804 out.writeTypedObject(cutout.mSafeInsets, flags);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200805 out.writeTypedArray(cutout.mBounds.getRects(), flags);
shawnlin7dabcab2020-01-13 22:24:43 +0800806 out.writeTypedObject(cutout.mWaterfallInsets, flags);
Adrian Roosd4970af2017-11-10 15:48:01 +0100807 }
808 }
809
810 /**
811 * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing
812 * instance.
813 *
814 * Needed for AIDL out parameters.
815 */
816 public void readFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100817 mInner = readCutoutFromParcel(in);
Adrian Roosd4970af2017-11-10 15:48:01 +0100818 }
819
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700820 public static final @android.annotation.NonNull Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() {
Adrian Roosd4970af2017-11-10 15:48:01 +0100821 @Override
822 public ParcelableWrapper createFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100823 return new ParcelableWrapper(readCutoutFromParcel(in));
Adrian Roosd4970af2017-11-10 15:48:01 +0100824 }
825
826 @Override
827 public ParcelableWrapper[] newArray(int size) {
828 return new ParcelableWrapper[size];
829 }
830 };
831
Adrian Roos1cf585052018-01-03 18:43:27 +0100832 /**
833 * Reads a DisplayCutout from a {@link Parcel}.
834 *
835 * @see #writeCutoutToParcel(DisplayCutout, Parcel, int)
836 */
837 public static DisplayCutout readCutoutFromParcel(Parcel in) {
838 int variant = in.readInt();
839 if (variant == -1) {
840 return null;
841 }
842 if (variant == 0) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100843 return NO_CUTOUT;
844 }
845
Adrian Roosd4970af2017-11-10 15:48:01 +0100846 Rect safeInsets = in.readTypedObject(Rect.CREATOR);
Issei Suzuki43190bd2018-08-20 17:28:41 +0200847 Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH];
848 in.readTypedArray(bounds, Rect.CREATOR);
shawnlin7dabcab2020-01-13 22:24:43 +0800849 Insets waterfallInsets = in.readTypedObject(Insets.CREATOR);
Adrian Roosd4970af2017-11-10 15:48:01 +0100850
shawnlin7dabcab2020-01-13 22:24:43 +0800851 return new DisplayCutout(
852 safeInsets, waterfallInsets, bounds, false /* copyArguments */);
Adrian Roosd4970af2017-11-10 15:48:01 +0100853 }
854
855 public DisplayCutout get() {
856 return mInner;
857 }
858
859 public void set(ParcelableWrapper cutout) {
860 mInner = cutout.get();
861 }
862
863 public void set(DisplayCutout cutout) {
864 mInner = cutout;
865 }
866
867 @Override
868 public int hashCode() {
869 return mInner.hashCode();
870 }
871
872 @Override
873 public boolean equals(Object o) {
874 return o instanceof ParcelableWrapper
875 && mInner.equals(((ParcelableWrapper) o).mInner);
876 }
877
878 @Override
879 public String toString() {
880 return String.valueOf(mInner);
881 }
882 }
883}