blob: a61c8c1dba6844bad77fe37bb0e72c3a814bd3e5 [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
Vishnu Nair1d0fa072018-01-04 07:53:00 -080019import static android.view.DisplayCutoutProto.BOUNDS;
20import static android.view.DisplayCutoutProto.INSETS;
Adrian Roosd4970af2017-11-10 15:48:01 +010021import static android.view.Surface.ROTATION_0;
22import static android.view.Surface.ROTATION_180;
23import static android.view.Surface.ROTATION_270;
24import static android.view.Surface.ROTATION_90;
25
Adrian Roos16693f32018-01-18 17:45:52 +010026import android.content.res.Resources;
27import android.graphics.Matrix;
Adrian Roosd07bafd2017-12-11 17:30:56 +010028import android.graphics.Path;
Adrian Roosd4970af2017-11-10 15:48:01 +010029import android.graphics.Point;
30import android.graphics.Rect;
Adrian Roosd07bafd2017-12-11 17:30:56 +010031import android.graphics.RectF;
32import android.graphics.Region;
Adrian Roosd4970af2017-11-10 15:48:01 +010033import android.os.Parcel;
34import android.os.Parcelable;
Adrian Roos16693f32018-01-18 17:45:52 +010035import android.text.TextUtils;
36import android.util.Log;
37import android.util.PathParser;
Vishnu Nair1d0fa072018-01-04 07:53:00 -080038import android.util.proto.ProtoOutputStream;
Adrian Roosd4970af2017-11-10 15:48:01 +010039
Adrian Roos16693f32018-01-18 17:45:52 +010040import com.android.internal.R;
Adrian Roosd4970af2017-11-10 15:48:01 +010041import com.android.internal.annotations.VisibleForTesting;
42
Adrian Roosd4970af2017-11-10 15:48:01 +010043import java.util.List;
44
45/**
46 * Represents a part of the display that is not functional for displaying content.
47 *
48 * <p>{@code DisplayCutout} is immutable.
Adrian Roosd4970af2017-11-10 15:48:01 +010049 */
50public final class DisplayCutout {
51
Adrian Roos16693f32018-01-18 17:45:52 +010052 private static final String TAG = "DisplayCutout";
53 private static final String DP_MARKER = "@dp";
54
Adrian Roosd07bafd2017-12-11 17:30:56 +010055 private static final Rect ZERO_RECT = new Rect();
56 private static final Region EMPTY_REGION = new Region();
Adrian Roosd4970af2017-11-10 15:48:01 +010057
58 /**
Adrian Roosd07bafd2017-12-11 17:30:56 +010059 * An instance where {@link #isEmpty()} returns {@code true}.
Adrian Roosd4970af2017-11-10 15:48:01 +010060 *
61 * @hide
62 */
Adrian Roosd07bafd2017-12-11 17:30:56 +010063 public static final DisplayCutout NO_CUTOUT = new DisplayCutout(ZERO_RECT, EMPTY_REGION);
Adrian Roosd4970af2017-11-10 15:48:01 +010064
65 private final Rect mSafeInsets;
Adrian Roosd07bafd2017-12-11 17:30:56 +010066 private final Region mBounds;
Adrian Roosd4970af2017-11-10 15:48:01 +010067
68 /**
69 * Creates a DisplayCutout instance.
70 *
71 * NOTE: the Rects passed into this instance are not copied and MUST remain unchanged.
72 *
73 * @hide
74 */
75 @VisibleForTesting
Adrian Roosd07bafd2017-12-11 17:30:56 +010076 public DisplayCutout(Rect safeInsets, Region bounds) {
Adrian Roosd4970af2017-11-10 15:48:01 +010077 mSafeInsets = safeInsets != null ? safeInsets : ZERO_RECT;
Adrian Roosd07bafd2017-12-11 17:30:56 +010078 mBounds = bounds != null ? bounds : Region.obtain();
Adrian Roosd4970af2017-11-10 15:48:01 +010079 }
80
81 /**
Adrian Roosd07bafd2017-12-11 17:30:56 +010082 * Returns true if there is no cutout or it is outside of the content view.
Adrian Roosd4970af2017-11-10 15:48:01 +010083 *
Adrian Roosd07bafd2017-12-11 17:30:56 +010084 * @hide
Adrian Roosd4970af2017-11-10 15:48:01 +010085 */
Adrian Roosd07bafd2017-12-11 17:30:56 +010086 public boolean isEmpty() {
87 return mSafeInsets.equals(ZERO_RECT);
Adrian Roosd4970af2017-11-10 15:48:01 +010088 }
89
90 /** Returns the inset from the top which avoids the display cutout. */
91 public int getSafeInsetTop() {
92 return mSafeInsets.top;
93 }
94
95 /** Returns the inset from the bottom which avoids the display cutout. */
96 public int getSafeInsetBottom() {
97 return mSafeInsets.bottom;
98 }
99
100 /** Returns the inset from the left which avoids the display cutout. */
101 public int getSafeInsetLeft() {
102 return mSafeInsets.left;
103 }
104
105 /** Returns the inset from the right which avoids the display cutout. */
106 public int getSafeInsetRight() {
107 return mSafeInsets.right;
108 }
109
110 /**
Adrian Roosd07bafd2017-12-11 17:30:56 +0100111 * Returns the safe insets in a rect.
Adrian Roosd4970af2017-11-10 15:48:01 +0100112 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100113 * @return a rect which is set to the safe insets.
Adrian Roosd4970af2017-11-10 15:48:01 +0100114 * @hide
115 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100116 public Rect getSafeInsets() {
117 return new Rect(mSafeInsets);
Adrian Roosd4970af2017-11-10 15:48:01 +0100118 }
119
120 /**
Adrian Roosd07bafd2017-12-11 17:30:56 +0100121 * Returns the bounding region of the cutout.
Adrian Roosd4970af2017-11-10 15:48:01 +0100122 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100123 * @return the bounding region of the cutout. Coordinates are relative
Adrian Roosd4970af2017-11-10 15:48:01 +0100124 * to the top-left corner of the content view.
125 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100126 public Region getBounds() {
127 return Region.obtain(mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100128 }
129
130 /**
Adrian Roosd07bafd2017-12-11 17:30:56 +0100131 * Returns the bounding rect of the cutout.
Adrian Roosd4970af2017-11-10 15:48:01 +0100132 *
Adrian Roosd07bafd2017-12-11 17:30:56 +0100133 * @return the bounding rect of the cutout. Coordinates are relative
134 * to the top-left corner of the content view.
135 * @hide
Adrian Roosd4970af2017-11-10 15:48:01 +0100136 */
Adrian Roosd07bafd2017-12-11 17:30:56 +0100137 public Rect getBoundingRect() {
138 // TODO(roosa): Inline.
139 return mBounds.getBounds();
Adrian Roosd4970af2017-11-10 15:48:01 +0100140 }
141
142 @Override
143 public int hashCode() {
144 int result = mSafeInsets.hashCode();
Adrian Roosd07bafd2017-12-11 17:30:56 +0100145 result = result * 31 + mBounds.getBounds().hashCode();
Adrian Roosd4970af2017-11-10 15:48:01 +0100146 return result;
147 }
148
149 @Override
150 public boolean equals(Object o) {
151 if (o == this) {
152 return true;
153 }
154 if (o instanceof DisplayCutout) {
155 DisplayCutout c = (DisplayCutout) o;
156 return mSafeInsets.equals(c.mSafeInsets)
Adrian Roosd07bafd2017-12-11 17:30:56 +0100157 && mBounds.equals(c.mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100158 }
159 return false;
160 }
161
162 @Override
163 public String toString() {
164 return "DisplayCutout{insets=" + mSafeInsets
Adrian Roos1cf585052018-01-03 18:43:27 +0100165 + " boundingRect=" + getBoundingRect()
Adrian Roosd4970af2017-11-10 15:48:01 +0100166 + "}";
167 }
168
169 /**
Vishnu Nair1d0fa072018-01-04 07:53:00 -0800170 * @hide
171 */
172 public void writeToProto(ProtoOutputStream proto, long fieldId) {
173 final long token = proto.start(fieldId);
174 mSafeInsets.writeToProto(proto, INSETS);
175 mBounds.getBounds().writeToProto(proto, BOUNDS);
176 proto.end(token);
177 }
178
179 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100180 * Insets the reference frame of the cutout in the given directions.
181 *
182 * @return a copy of this instance which has been inset
183 * @hide
184 */
185 public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) {
Adrian Roosd07bafd2017-12-11 17:30:56 +0100186 if (mBounds.isEmpty()
Adrian Roosd4970af2017-11-10 15:48:01 +0100187 || insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0) {
188 return this;
189 }
190
191 Rect safeInsets = new Rect(mSafeInsets);
Adrian Roosd07bafd2017-12-11 17:30:56 +0100192 Region bounds = Region.obtain(mBounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100193
194 // Note: it's not really well defined what happens when the inset is negative, because we
195 // don't know if the safe inset needs to expand in general.
196 if (insetTop > 0 || safeInsets.top > 0) {
197 safeInsets.top = atLeastZero(safeInsets.top - insetTop);
198 }
199 if (insetBottom > 0 || safeInsets.bottom > 0) {
200 safeInsets.bottom = atLeastZero(safeInsets.bottom - insetBottom);
201 }
202 if (insetLeft > 0 || safeInsets.left > 0) {
203 safeInsets.left = atLeastZero(safeInsets.left - insetLeft);
204 }
205 if (insetRight > 0 || safeInsets.right > 0) {
206 safeInsets.right = atLeastZero(safeInsets.right - insetRight);
207 }
208
Adrian Roosd07bafd2017-12-11 17:30:56 +0100209 bounds.translate(-insetLeft, -insetTop);
Adrian Roosd4970af2017-11-10 15:48:01 +0100210
Adrian Roosd07bafd2017-12-11 17:30:56 +0100211 return new DisplayCutout(safeInsets, bounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100212 }
213
214 /**
215 * Calculates the safe insets relative to the given reference frame.
216 *
217 * @return a copy of this instance with the safe insets calculated
218 * @hide
219 */
220 public DisplayCutout calculateRelativeTo(Rect frame) {
Adrian Roosd07bafd2017-12-11 17:30:56 +0100221 if (mBounds.isEmpty() || !Rect.intersects(frame, mBounds.getBounds())) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100222 return NO_CUTOUT;
223 }
224
Adrian Roosd07bafd2017-12-11 17:30:56 +0100225 return DisplayCutout.calculateRelativeTo(frame, Region.obtain(mBounds));
Adrian Roosd4970af2017-11-10 15:48:01 +0100226 }
227
Adrian Roosd07bafd2017-12-11 17:30:56 +0100228 private static DisplayCutout calculateRelativeTo(Rect frame, Region bounds) {
229 Rect boundingRect = bounds.getBounds();
Adrian Roosd4970af2017-11-10 15:48:01 +0100230 Rect safeRect = new Rect();
Adrian Roosd07bafd2017-12-11 17:30:56 +0100231
Adrian Roosd4970af2017-11-10 15:48:01 +0100232 int bestArea = 0;
233 int bestVariant = 0;
234 for (int variant = ROTATION_0; variant <= ROTATION_270; variant++) {
235 int area = calculateInsetVariantArea(frame, boundingRect, variant, safeRect);
236 if (bestArea < area) {
237 bestArea = area;
238 bestVariant = variant;
239 }
240 }
241 calculateInsetVariantArea(frame, boundingRect, bestVariant, safeRect);
242 if (safeRect.isEmpty()) {
243 // The entire frame overlaps with the cutout.
244 safeRect.set(0, frame.height(), 0, 0);
245 } else {
246 // Convert safeRect to insets relative to frame. We're reusing the rect here to avoid
247 // an allocation.
248 safeRect.set(
249 Math.max(0, safeRect.left - frame.left),
250 Math.max(0, safeRect.top - frame.top),
251 Math.max(0, frame.right - safeRect.right),
252 Math.max(0, frame.bottom - safeRect.bottom));
253 }
254
Adrian Roosd07bafd2017-12-11 17:30:56 +0100255 bounds.translate(-frame.left, -frame.top);
Adrian Roosd4970af2017-11-10 15:48:01 +0100256
Adrian Roosd07bafd2017-12-11 17:30:56 +0100257 return new DisplayCutout(safeRect, bounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100258 }
259
260 private static int calculateInsetVariantArea(Rect frame, Rect boundingRect, int variant,
261 Rect outSafeRect) {
262 switch (variant) {
263 case ROTATION_0:
264 outSafeRect.set(frame.left, frame.top, frame.right, boundingRect.top);
265 break;
266 case ROTATION_90:
267 outSafeRect.set(frame.left, frame.top, boundingRect.left, frame.bottom);
268 break;
269 case ROTATION_180:
270 outSafeRect.set(frame.left, boundingRect.bottom, frame.right, frame.bottom);
271 break;
272 case ROTATION_270:
273 outSafeRect.set(boundingRect.right, frame.top, frame.right, frame.bottom);
274 break;
275 }
276
277 return outSafeRect.isEmpty() ? 0 : outSafeRect.width() * outSafeRect.height();
278 }
279
280 private static int atLeastZero(int value) {
281 return value < 0 ? 0 : value;
282 }
283
Adrian Roosd4970af2017-11-10 15:48:01 +0100284
285 /**
286 * Creates an instance from a bounding polygon.
287 *
288 * @hide
289 */
290 public static DisplayCutout fromBoundingPolygon(List<Point> points) {
Adrian Roosd07bafd2017-12-11 17:30:56 +0100291 Path path = new Path();
Adrian Roosd07bafd2017-12-11 17:30:56 +0100292 path.reset();
Adrian Roosd4970af2017-11-10 15:48:01 +0100293 for (int i = 0; i < points.size(); i++) {
294 Point point = points.get(i);
Adrian Roosd07bafd2017-12-11 17:30:56 +0100295 if (i == 0) {
296 path.moveTo(point.x, point.y);
297 } else {
298 path.lineTo(point.x, point.y);
299 }
Adrian Roosd4970af2017-11-10 15:48:01 +0100300 }
Adrian Roosd07bafd2017-12-11 17:30:56 +0100301 path.close();
Adrian Roos1cf585052018-01-03 18:43:27 +0100302 return fromBounds(path);
303 }
Adrian Roosd4970af2017-11-10 15:48:01 +0100304
Adrian Roos1cf585052018-01-03 18:43:27 +0100305 /**
306 * Creates an instance from a bounding {@link Path}.
307 *
308 * @hide
309 */
310 public static DisplayCutout fromBounds(Path path) {
Adrian Roosd07bafd2017-12-11 17:30:56 +0100311 RectF clipRect = new RectF();
312 path.computeBounds(clipRect, false /* unused */);
313 Region clipRegion = Region.obtain();
314 clipRegion.set((int) clipRect.left, (int) clipRect.top,
315 (int) clipRect.right, (int) clipRect.bottom);
316
Adrian Roos1cf585052018-01-03 18:43:27 +0100317 Region bounds = new Region();
Adrian Roosd07bafd2017-12-11 17:30:56 +0100318 bounds.setPath(path, clipRegion);
Adrian Roos1cf585052018-01-03 18:43:27 +0100319 clipRegion.recycle();
Adrian Roosd07bafd2017-12-11 17:30:56 +0100320 return new DisplayCutout(ZERO_RECT, bounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100321 }
322
323 /**
Adrian Roos16693f32018-01-18 17:45:52 +0100324 * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout.
325 *
326 * @hide
327 */
328 public static DisplayCutout fromResources(Resources res, int displayWidth) {
329 String spec = res.getString(R.string.config_mainBuiltInDisplayCutout);
330 if (TextUtils.isEmpty(spec)) {
331 return null;
332 }
333 spec = spec.trim();
334 final boolean inDp = spec.endsWith(DP_MARKER);
335 if (inDp) {
336 spec = spec.substring(0, spec.length() - DP_MARKER.length());
337 }
338
339 Path p;
340 try {
341 p = PathParser.createPathFromPathData(spec);
342 } catch (Throwable e) {
343 Log.wtf(TAG, "Could not inflate cutout: ", e);
344 return null;
345 }
346
347 final Matrix m = new Matrix();
348 if (inDp) {
349 final float dpToPx = res.getDisplayMetrics().density;
350 m.postScale(dpToPx, dpToPx);
351 }
352 m.postTranslate(displayWidth / 2f, 0);
353 p.transform(m);
354 return fromBounds(p);
355 }
356
357 /**
Adrian Roosd4970af2017-11-10 15:48:01 +0100358 * Helper class for passing {@link DisplayCutout} through binder.
359 *
360 * Needed, because {@code readFromParcel} cannot be used with immutable classes.
361 *
362 * @hide
363 */
364 public static final class ParcelableWrapper implements Parcelable {
365
366 private DisplayCutout mInner;
367
368 public ParcelableWrapper() {
369 this(NO_CUTOUT);
370 }
371
372 public ParcelableWrapper(DisplayCutout cutout) {
373 mInner = cutout;
374 }
375
376 @Override
377 public int describeContents() {
378 return 0;
379 }
380
381 @Override
382 public void writeToParcel(Parcel out, int flags) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100383 writeCutoutToParcel(mInner, out, flags);
384 }
385
386 /**
387 * Writes a DisplayCutout to a {@link Parcel}.
388 *
389 * @see #readCutoutFromParcel(Parcel)
390 */
391 public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) {
392 if (cutout == null) {
393 out.writeInt(-1);
394 } else if (cutout == NO_CUTOUT) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100395 out.writeInt(0);
396 } else {
397 out.writeInt(1);
Adrian Roos1cf585052018-01-03 18:43:27 +0100398 out.writeTypedObject(cutout.mSafeInsets, flags);
399 out.writeTypedObject(cutout.mBounds, flags);
Adrian Roosd4970af2017-11-10 15:48:01 +0100400 }
401 }
402
403 /**
404 * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing
405 * instance.
406 *
407 * Needed for AIDL out parameters.
408 */
409 public void readFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100410 mInner = readCutoutFromParcel(in);
Adrian Roosd4970af2017-11-10 15:48:01 +0100411 }
412
413 public static final Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() {
414 @Override
415 public ParcelableWrapper createFromParcel(Parcel in) {
Adrian Roos1cf585052018-01-03 18:43:27 +0100416 return new ParcelableWrapper(readCutoutFromParcel(in));
Adrian Roosd4970af2017-11-10 15:48:01 +0100417 }
418
419 @Override
420 public ParcelableWrapper[] newArray(int size) {
421 return new ParcelableWrapper[size];
422 }
423 };
424
Adrian Roos1cf585052018-01-03 18:43:27 +0100425 /**
426 * Reads a DisplayCutout from a {@link Parcel}.
427 *
428 * @see #writeCutoutToParcel(DisplayCutout, Parcel, int)
429 */
430 public static DisplayCutout readCutoutFromParcel(Parcel in) {
431 int variant = in.readInt();
432 if (variant == -1) {
433 return null;
434 }
435 if (variant == 0) {
Adrian Roosd4970af2017-11-10 15:48:01 +0100436 return NO_CUTOUT;
437 }
438
Adrian Roosd4970af2017-11-10 15:48:01 +0100439 Rect safeInsets = in.readTypedObject(Rect.CREATOR);
Adrian Roosd07bafd2017-12-11 17:30:56 +0100440 Region bounds = in.readTypedObject(Region.CREATOR);
Adrian Roosd4970af2017-11-10 15:48:01 +0100441
Adrian Roosd07bafd2017-12-11 17:30:56 +0100442 return new DisplayCutout(safeInsets, bounds);
Adrian Roosd4970af2017-11-10 15:48:01 +0100443 }
444
445 public DisplayCutout get() {
446 return mInner;
447 }
448
449 public void set(ParcelableWrapper cutout) {
450 mInner = cutout.get();
451 }
452
453 public void set(DisplayCutout cutout) {
454 mInner = cutout;
455 }
456
457 @Override
458 public int hashCode() {
459 return mInner.hashCode();
460 }
461
462 @Override
463 public boolean equals(Object o) {
464 return o instanceof ParcelableWrapper
465 && mInner.equals(((ParcelableWrapper) o).mInner);
466 }
467
468 @Override
469 public String toString() {
470 return String.valueOf(mInner);
471 }
472 }
473}