Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Adrian Roos | 535c420 | 2018-04-16 16:12:40 +0200 | [diff] [blame] | 19 | import static android.util.DisplayMetrics.DENSITY_DEFAULT; |
| 20 | import static android.util.DisplayMetrics.DENSITY_DEVICE_STABLE; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 21 | import static android.view.DisplayCutoutProto.BOUND_BOTTOM; |
| 22 | import static android.view.DisplayCutoutProto.BOUND_LEFT; |
| 23 | import static android.view.DisplayCutoutProto.BOUND_RIGHT; |
| 24 | import static android.view.DisplayCutoutProto.BOUND_TOP; |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 25 | import static android.view.DisplayCutoutProto.INSETS; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 26 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 27 | import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; |
| 28 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 29 | import android.annotation.IntDef; |
| 30 | import android.annotation.NonNull; |
| 31 | import android.annotation.Nullable; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 32 | import android.content.res.Resources; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 33 | import android.graphics.Insets; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 34 | import android.graphics.Matrix; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 35 | import android.graphics.Path; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 36 | import android.graphics.Rect; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 37 | import android.graphics.RectF; |
| 38 | import android.graphics.Region; |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 39 | import android.graphics.Region.Op; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 40 | import android.os.Parcel; |
| 41 | import android.os.Parcelable; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 42 | import android.text.TextUtils; |
| 43 | import android.util.Log; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 44 | import android.util.Pair; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 45 | import android.util.PathParser; |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 46 | import android.util.proto.ProtoOutputStream; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 47 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 48 | import com.android.internal.R; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 49 | import com.android.internal.annotations.GuardedBy; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 50 | import com.android.internal.annotations.VisibleForTesting; |
| 51 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 52 | import java.lang.annotation.Retention; |
| 53 | import java.lang.annotation.RetentionPolicy; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 54 | import java.util.ArrayList; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 55 | import java.util.Arrays; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 56 | import java.util.List; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 57 | |
| 58 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 59 | * Represents the area of the display that is not functional for displaying content. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 60 | * |
| 61 | * <p>{@code DisplayCutout} is immutable. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 62 | */ |
| 63 | public final class DisplayCutout { |
| 64 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 65 | private static final String TAG = "DisplayCutout"; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 66 | private static final String BOTTOM_MARKER = "@bottom"; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 67 | private static final String DP_MARKER = "@dp"; |
Adrian Roos | b8b10f8 | 2018-03-15 20:09:42 +0100 | [diff] [blame] | 68 | private static final String RIGHT_MARKER = "@right"; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 69 | |
Adrian Roos | c84df77 | 2018-01-19 21:20:22 +0100 | [diff] [blame] | 70 | /** |
| 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 Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 80 | private static final Rect ZERO_RECT = new Rect(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 81 | |
| 82 | /** |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 83 | * An instance where {@link #isEmpty()} returns {@code true}. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 84 | * |
| 85 | * @hide |
| 86 | */ |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 87 | public static final DisplayCutout NO_CUTOUT = new DisplayCutout( |
| 88 | ZERO_RECT, ZERO_RECT, ZERO_RECT, ZERO_RECT, ZERO_RECT, |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 89 | false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 90 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 91 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 92 | private static final Pair<Path, DisplayCutout> NULL_PAIR = new Pair<>(null, null); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 93 | private static final Object CACHE_LOCK = new Object(); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 94 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 95 | @GuardedBy("CACHE_LOCK") |
| 96 | private static String sCachedSpec; |
| 97 | @GuardedBy("CACHE_LOCK") |
| 98 | private static int sCachedDisplayWidth; |
| 99 | @GuardedBy("CACHE_LOCK") |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 100 | private static int sCachedDisplayHeight; |
| 101 | @GuardedBy("CACHE_LOCK") |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 102 | private static float sCachedDensity; |
| 103 | @GuardedBy("CACHE_LOCK") |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 104 | private static Pair<Path, DisplayCutout> sCachedCutout = NULL_PAIR; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 105 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 106 | private final Rect mSafeInsets; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 107 | |
| 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 Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 248 | public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft, |
| 249 | @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 250 | this(safeInsets.toRect(), boundLeft, boundTop, boundRight, boundBottom, true); |
| 251 | } |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 252 | |
| 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 Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 258 | * @param boundingRects the bounding rects of the display cutouts as returned by |
| 259 | * {@link #getBoundingRects()} ()}. |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 260 | * @deprecated Use {@link DisplayCutout#DisplayCutout(Insets, Rect, Rect, Rect, Rect)} instead. |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 261 | */ |
| 262 | // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 263 | @Deprecated |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 264 | public DisplayCutout(@Nullable Rect safeInsets, @Nullable List<Rect> boundingRects) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 265 | this(safeInsets, extractBoundsFromList(safeInsets, boundingRects), |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 266 | true /* copyArguments */); |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 267 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * Creates a DisplayCutout instance. |
| 271 | * |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 272 | * @param safeInsets the insets from each edge which avoid the display cutout as returned by |
| 273 | * {@link #getSafeInsetTop()} etc. |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 274 | * @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 Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 276 | */ |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 277 | 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 Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 315 | 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 Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 329 | } |
| 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 Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 344 | * Returns true if the safe insets are empty (and therefore the current view does not |
| 345 | * overlap with the cutout or cutout area). |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 346 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 347 | * @hide |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 348 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 349 | public boolean isEmpty() { |
| 350 | return mSafeInsets.equals(ZERO_RECT); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 353 | /** Returns the inset from the top which avoids the display cutout in pixels. */ |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 354 | public int getSafeInsetTop() { |
| 355 | return mSafeInsets.top; |
| 356 | } |
| 357 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 358 | /** Returns the inset from the bottom which avoids the display cutout in pixels. */ |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 359 | public int getSafeInsetBottom() { |
| 360 | return mSafeInsets.bottom; |
| 361 | } |
| 362 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 363 | /** Returns the inset from the left which avoids the display cutout in pixels. */ |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 364 | public int getSafeInsetLeft() { |
| 365 | return mSafeInsets.left; |
| 366 | } |
| 367 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 368 | /** Returns the inset from the right which avoids the display cutout in pixels. */ |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 369 | public int getSafeInsetRight() { |
| 370 | return mSafeInsets.right; |
| 371 | } |
| 372 | |
| 373 | /** |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 374 | * Returns the safe insets in a rect in pixel units. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 375 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 376 | * @return a rect which is set to the safe insets. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 377 | * @hide |
| 378 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 379 | public Rect getSafeInsets() { |
| 380 | return new Rect(mSafeInsets); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 384 | * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional |
| 385 | * area on the display. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 386 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 387 | * There will be at most one non-functional area per short edge of the device, and none on |
| 388 | * the long edges. |
| 389 | * |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 390 | * @return a list of bounding {@code Rect}s, one for each display cutout area. No empty Rect is |
| 391 | * returned. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 392 | */ |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 393 | @NonNull |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 394 | public List<Rect> getBoundingRects() { |
| 395 | List<Rect> result = new ArrayList<>(); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 396 | for (Rect bound : getBoundingRectsAll()) { |
| 397 | if (!bound.isEmpty()) { |
| 398 | result.add(new Rect(bound)); |
| 399 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 400 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 401 | return result; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 404 | /** |
| 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 Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 465 | @Override |
| 466 | public int hashCode() { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 467 | return mSafeInsets.hashCode() * 48271 + mBounds.hashCode(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 468 | } |
| 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 Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 477 | return mSafeInsets.equals(c.mSafeInsets) && mBounds.equals(c.mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 478 | } |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public String toString() { |
| 484 | return "DisplayCutout{insets=" + mSafeInsets |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 485 | + " boundingRect={" + mBounds + "}" |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 486 | + "}"; |
| 487 | } |
| 488 | |
| 489 | /** |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 490 | * @hide |
| 491 | */ |
| 492 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 493 | final long token = proto.start(fieldId); |
| 494 | mSafeInsets.writeToProto(proto, INSETS); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 495 | 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 Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 499 | proto.end(token); |
| 500 | } |
| 501 | |
| 502 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 503 | * 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 Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 509 | if (isBoundsEmpty() |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 510 | || insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0) { |
| 511 | return this; |
| 512 | } |
| 513 | |
| 514 | Rect safeInsets = new Rect(mSafeInsets); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 515 | |
| 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 Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 531 | 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 Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 538 | return new DisplayCutout(safeInsets, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 542 | * Returns a copy of this instance with the safe insets replaced with the parameter. |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 543 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 544 | * @param safeInsets the new safe insets in pixels |
| 545 | * @return a copy of this instance with the safe insets replaced with the argument. |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 546 | * |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 547 | * @hide |
| 548 | */ |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 549 | public DisplayCutout replaceSafeInsets(Rect safeInsets) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 550 | return new DisplayCutout(new Rect(safeInsets), mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | private static int atLeastZero(int value) { |
| 554 | return value < 0 ? 0 : value; |
| 555 | } |
| 556 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 557 | |
| 558 | /** |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 559 | * Creates an instance from a bounding rect. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 560 | * |
| 561 | * @hide |
| 562 | */ |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 563 | @VisibleForTesting |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 564 | 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 Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 571 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 572 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 573 | /** |
| 574 | * Creates an instance from a bounding {@link Path}. |
| 575 | * |
| 576 | * @hide |
| 577 | */ |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 578 | public static DisplayCutout fromBounds(Rect[] bounds) { |
| 579 | return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 583 | * Creates the display cutout according to |
| 584 | * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest |
| 585 | * rectangle-base approximation of the cutout. |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 586 | * |
| 587 | * @hide |
| 588 | */ |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 589 | public static DisplayCutout fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight) { |
| 590 | return fromSpec(res.getString(R.string.config_mainBuiltInDisplayCutoutRectApproximation), |
Adrian Roos | 535c420 | 2018-04-16 16:12:40 +0200 | [diff] [blame] | 591 | displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /** |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 595 | * 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 Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 600 | return pathAndDisplayCutoutFromSpec( |
| 601 | res.getString(R.string.config_mainBuiltInDisplayCutout), |
Adrian Roos | 535c420 | 2018-04-16 16:12:40 +0200 | [diff] [blame] | 602 | displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT).first; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /** |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 606 | * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec. |
| 607 | * |
| 608 | * @hide |
| 609 | */ |
| 610 | @VisibleForTesting(visibility = PRIVATE) |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 611 | public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight, |
| 612 | float density) { |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 613 | 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 Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 618 | if (TextUtils.isEmpty(spec)) { |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 619 | return NULL_PAIR; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 620 | } |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 621 | synchronized (CACHE_LOCK) { |
| 622 | if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 623 | && sCachedDisplayHeight == displayHeight |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 624 | && sCachedDensity == density) { |
| 625 | return sCachedCutout; |
| 626 | } |
| 627 | } |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 628 | spec = spec.trim(); |
Adrian Roos | b8b10f8 | 2018-03-15 20:09:42 +0100 | [diff] [blame] | 629 | 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 Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 636 | final boolean inDp = spec.endsWith(DP_MARKER); |
| 637 | if (inDp) { |
| 638 | spec = spec.substring(0, spec.length() - DP_MARKER.length()); |
| 639 | } |
| 640 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 641 | 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 Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 649 | final Region r = Region.obtain(); |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 650 | try { |
| 651 | p = PathParser.createPathFromPathData(spec); |
| 652 | } catch (Throwable e) { |
| 653 | Log.wtf(TAG, "Could not inflate cutout: ", e); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 654 | return NULL_PAIR; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | final Matrix m = new Matrix(); |
| 658 | if (inDp) { |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 659 | m.postScale(density, density); |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 660 | } |
Adrian Roos | b8b10f8 | 2018-03-15 20:09:42 +0100 | [diff] [blame] | 661 | m.postTranslate(offsetX, 0); |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 662 | p.transform(m); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 663 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 664 | Rect boundTop = new Rect(); |
| 665 | toRectAndAddToRegion(p, r, boundTop); |
| 666 | final int topInset = boundTop.bottom; |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 667 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 668 | Rect boundBottom = null; |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 669 | final int bottomInset; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 670 | 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 Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 676 | return NULL_PAIR; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 677 | } |
| 678 | // Keep top transform |
| 679 | m.postTranslate(0, displayHeight); |
| 680 | bottomPath.transform(m); |
| 681 | p.addPath(bottomPath); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 682 | boundBottom = new Rect(); |
| 683 | toRectAndAddToRegion(bottomPath, r, boundBottom); |
| 684 | bottomInset = displayHeight - boundBottom.top; |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 685 | } else { |
| 686 | bottomInset = 0; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 687 | } |
| 688 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 689 | 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 Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 693 | |
| 694 | final Pair<Path, DisplayCutout> result = new Pair<>(p, cutout); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 695 | synchronized (CACHE_LOCK) { |
| 696 | sCachedSpec = spec; |
| 697 | sCachedDisplayWidth = displayWidth; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 698 | sCachedDisplayHeight = displayHeight; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 699 | sCachedDensity = density; |
| 700 | sCachedCutout = result; |
| 701 | } |
| 702 | return result; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 705 | private static void toRectAndAddToRegion(Path p, Region inoutRegion, Rect inoutRect) { |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 706 | final RectF rectF = new RectF(); |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 707 | p.computeBounds(rectF, false /* unused */); |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 708 | rectF.round(inoutRect); |
| 709 | inoutRegion.op(inoutRect, Op.UNION); |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 712 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 713 | * 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 Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 738 | 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 Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 750 | out.writeInt(0); |
| 751 | } else { |
| 752 | out.writeInt(1); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 753 | out.writeTypedObject(cutout.mSafeInsets, flags); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 754 | out.writeTypedArray(cutout.mBounds.getRects(), flags); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 755 | } |
| 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 Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 765 | mInner = readCutoutFromParcel(in); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 768 | public static final @android.annotation.NonNull Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 769 | @Override |
| 770 | public ParcelableWrapper createFromParcel(Parcel in) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 771 | return new ParcelableWrapper(readCutoutFromParcel(in)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | @Override |
| 775 | public ParcelableWrapper[] newArray(int size) { |
| 776 | return new ParcelableWrapper[size]; |
| 777 | } |
| 778 | }; |
| 779 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 780 | /** |
| 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 Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 791 | return NO_CUTOUT; |
| 792 | } |
| 793 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 794 | Rect safeInsets = in.readTypedObject(Rect.CREATOR); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 795 | Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH]; |
| 796 | in.readTypedArray(bounds, Rect.CREATOR); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 797 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 798 | return new DisplayCutout(safeInsets, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 799 | } |
| 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 | } |