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 | * |
Issei Suzuki | 8b0e617 | 2019-01-14 17:04:33 +0100 | [diff] [blame] | 236 | * <p>Note that this is only useful for tests. For production code, developers should always |
| 237 | * use a {@link DisplayCutout} obtained from the system.</p> |
| 238 | * |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 239 | * @param safeInsets the insets from each edge which avoid the display cutout as returned by |
| 240 | * {@link #getSafeInsetTop()} etc. |
| 241 | * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed, |
| 242 | * it's treated as an empty rectangle (0,0)-(0,0). |
| 243 | * @param boundTop the top bounding rect of the display cutout in pixels. If null is passed, |
| 244 | * it's treated as an empty rectangle (0,0)-(0,0). |
| 245 | * @param boundRight the right bounding rect of the display cutout in pixels. If null is |
| 246 | * passed, it's treated as an empty rectangle (0,0)-(0,0). |
| 247 | * @param boundBottom the bottom bounding rect of the display cutout in pixels. If null is |
| 248 | * passed, it's treated as an empty rectangle (0,0)-(0,0). |
| 249 | */ |
| 250 | // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 251 | public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft, |
| 252 | @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 253 | this(safeInsets.toRect(), boundLeft, boundTop, boundRight, boundBottom, true); |
| 254 | } |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * Creates a DisplayCutout instance. |
| 258 | * |
Issei Suzuki | 8b0e617 | 2019-01-14 17:04:33 +0100 | [diff] [blame] | 259 | * <p>Note that this is only useful for tests. For production code, developers should always |
| 260 | * use a {@link DisplayCutout} obtained from the system.</p> |
| 261 | * |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 262 | * @param safeInsets the insets from each edge which avoid the display cutout as returned by |
| 263 | * {@link #getSafeInsetTop()} etc. |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 264 | * @param boundingRects the bounding rects of the display cutouts as returned by |
| 265 | * {@link #getBoundingRects()} ()}. |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 266 | * @deprecated Use {@link DisplayCutout#DisplayCutout(Insets, Rect, Rect, Rect, Rect)} instead. |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 267 | */ |
| 268 | // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 269 | @Deprecated |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 270 | public DisplayCutout(@Nullable Rect safeInsets, @Nullable List<Rect> boundingRects) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 271 | this(safeInsets, extractBoundsFromList(safeInsets, boundingRects), |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 272 | true /* copyArguments */); |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 273 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * Creates a DisplayCutout instance. |
| 277 | * |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 278 | * @param safeInsets the insets from each edge which avoid the display cutout as returned by |
| 279 | * {@link #getSafeInsetTop()} etc. |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 280 | * @param copyArguments if true, create a copy of the arguments. If false, the passed arguments |
| 281 | * are not copied and MUST remain unchanged forever. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 282 | */ |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 283 | private DisplayCutout(Rect safeInsets, Rect boundLeft, Rect boundTop, Rect boundRight, |
| 284 | Rect boundBottom, boolean copyArguments) { |
| 285 | mSafeInsets = getCopyOrRef(safeInsets, copyArguments); |
| 286 | mBounds = new Bounds(boundLeft, boundTop, boundRight, boundBottom, copyArguments); |
| 287 | } |
| 288 | |
| 289 | private DisplayCutout(Rect safeInsets, Rect[] bounds, boolean copyArguments) { |
| 290 | mSafeInsets = getCopyOrRef(safeInsets, copyArguments); |
| 291 | mBounds = new Bounds(bounds, copyArguments); |
| 292 | } |
| 293 | |
| 294 | private DisplayCutout(Rect safeInsets, Bounds bounds) { |
| 295 | mSafeInsets = safeInsets; |
| 296 | mBounds = bounds; |
| 297 | |
| 298 | } |
| 299 | |
| 300 | private static Rect getCopyOrRef(Rect r, boolean copyArguments) { |
| 301 | if (r == null) { |
| 302 | return ZERO_RECT; |
| 303 | } else if (copyArguments) { |
| 304 | return new Rect(r); |
| 305 | } else { |
| 306 | return r; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Find the position of the bounding rect, and create an array of Rect whose index represents |
| 312 | * the position (= BoundsPosition). |
| 313 | * |
| 314 | * @hide |
| 315 | */ |
| 316 | public static Rect[] extractBoundsFromList(Rect safeInsets, List<Rect> boundingRects) { |
| 317 | Rect[] sortedBounds = new Rect[BOUNDS_POSITION_LENGTH]; |
| 318 | for (int i = 0; i < sortedBounds.length; ++i) { |
| 319 | sortedBounds[i] = ZERO_RECT; |
| 320 | } |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 321 | if (safeInsets != null && boundingRects != null) { |
Issei Suzuki | 6b5f5e9 | 2019-07-15 15:44:01 +0200 | [diff] [blame] | 322 | // There is at most one non-functional area per short edge of the device, but none |
| 323 | // on the long edges, so either a) safeInsets.top and safeInsets.bottom is 0, or |
| 324 | // b) safeInsets.left and safeInset.right is 0. |
| 325 | final boolean topBottomInset = safeInsets.top > 0 || safeInsets.bottom > 0; |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 326 | for (Rect bound : boundingRects) { |
Issei Suzuki | 6b5f5e9 | 2019-07-15 15:44:01 +0200 | [diff] [blame] | 327 | if (topBottomInset) { |
| 328 | if (bound.top == 0) { |
| 329 | sortedBounds[BOUNDS_POSITION_TOP] = bound; |
| 330 | } else { |
| 331 | sortedBounds[BOUNDS_POSITION_BOTTOM] = bound; |
| 332 | } |
| 333 | } else { |
| 334 | if (bound.left == 0) { |
| 335 | sortedBounds[BOUNDS_POSITION_LEFT] = bound; |
| 336 | } else { |
| 337 | sortedBounds[BOUNDS_POSITION_RIGHT] = bound; |
| 338 | } |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 339 | } |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | return sortedBounds; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Returns true if there is no cutout, i.e. the bounds are empty. |
| 347 | * |
| 348 | * @hide |
| 349 | */ |
| 350 | public boolean isBoundsEmpty() { |
| 351 | return mBounds.isEmpty(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 355 | * Returns true if the safe insets are empty (and therefore the current view does not |
| 356 | * overlap with the cutout or cutout area). |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 357 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 358 | * @hide |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 359 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 360 | public boolean isEmpty() { |
| 361 | return mSafeInsets.equals(ZERO_RECT); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 364 | /** 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] | 365 | public int getSafeInsetTop() { |
| 366 | return mSafeInsets.top; |
| 367 | } |
| 368 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 369 | /** 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] | 370 | public int getSafeInsetBottom() { |
| 371 | return mSafeInsets.bottom; |
| 372 | } |
| 373 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 374 | /** 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] | 375 | public int getSafeInsetLeft() { |
| 376 | return mSafeInsets.left; |
| 377 | } |
| 378 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 379 | /** 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] | 380 | public int getSafeInsetRight() { |
| 381 | return mSafeInsets.right; |
| 382 | } |
| 383 | |
| 384 | /** |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 385 | * Returns the safe insets in a rect in pixel units. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 386 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 387 | * @return a rect which is set to the safe insets. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 388 | * @hide |
| 389 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 390 | public Rect getSafeInsets() { |
| 391 | return new Rect(mSafeInsets); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 395 | * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional |
| 396 | * area on the display. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 397 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 398 | * There will be at most one non-functional area per short edge of the device, and none on |
| 399 | * the long edges. |
| 400 | * |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 401 | * @return a list of bounding {@code Rect}s, one for each display cutout area. No empty Rect is |
| 402 | * returned. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 403 | */ |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 404 | @NonNull |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 405 | public List<Rect> getBoundingRects() { |
| 406 | List<Rect> result = new ArrayList<>(); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 407 | for (Rect bound : getBoundingRectsAll()) { |
| 408 | if (!bound.isEmpty()) { |
| 409 | result.add(new Rect(bound)); |
| 410 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 411 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 412 | return result; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 415 | /** |
| 416 | * Returns an array of {@code Rect}s, each of which is the bounding rectangle for a non- |
| 417 | * functional area on the display. Ordinal value of BoundPosition is used as an index of |
| 418 | * the array. |
| 419 | * |
| 420 | * There will be at most one non-functional area per short edge of the device, and none on |
| 421 | * the long edges. |
| 422 | * |
| 423 | * @return an array of bounding {@code Rect}s, one for each display cutout area. This might |
| 424 | * contain ZERO_RECT, which means there is no cutout area at the position. |
| 425 | * |
| 426 | * @hide |
| 427 | */ |
| 428 | public Rect[] getBoundingRectsAll() { |
| 429 | return mBounds.getRects(); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Returns a bounding rectangle for a non-functional area on the display which is located on |
| 434 | * the left 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 getBoundingRectLeft() { |
| 440 | return mBounds.getRect(BOUNDS_POSITION_LEFT); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Returns a bounding rectangle for a non-functional area on the display which is located on |
| 445 | * the top 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 getBoundingRectTop() { |
| 451 | return mBounds.getRect(BOUNDS_POSITION_TOP); |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Returns a bounding rectangle for a non-functional area on the display which is located on |
| 456 | * the right 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 getBoundingRectRight() { |
| 462 | return mBounds.getRect(BOUNDS_POSITION_RIGHT); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Returns a bounding rectangle for a non-functional area on the display which is located on |
| 467 | * the bottom of the screen. |
| 468 | * |
| 469 | * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle |
| 470 | * is returned. |
| 471 | */ |
| 472 | public @NonNull Rect getBoundingRectBottom() { |
| 473 | return mBounds.getRect(BOUNDS_POSITION_BOTTOM); |
| 474 | } |
| 475 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 476 | @Override |
| 477 | public int hashCode() { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 478 | return mSafeInsets.hashCode() * 48271 + mBounds.hashCode(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public boolean equals(Object o) { |
| 483 | if (o == this) { |
| 484 | return true; |
| 485 | } |
| 486 | if (o instanceof DisplayCutout) { |
| 487 | DisplayCutout c = (DisplayCutout) o; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 488 | return mSafeInsets.equals(c.mSafeInsets) && mBounds.equals(c.mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 489 | } |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public String toString() { |
| 495 | return "DisplayCutout{insets=" + mSafeInsets |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 496 | + " boundingRect={" + mBounds + "}" |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 497 | + "}"; |
| 498 | } |
| 499 | |
| 500 | /** |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 501 | * @hide |
| 502 | */ |
| 503 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 504 | final long token = proto.start(fieldId); |
| 505 | mSafeInsets.writeToProto(proto, INSETS); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 506 | mBounds.getRect(BOUNDS_POSITION_LEFT).writeToProto(proto, BOUND_LEFT); |
| 507 | mBounds.getRect(BOUNDS_POSITION_TOP).writeToProto(proto, BOUND_TOP); |
| 508 | mBounds.getRect(BOUNDS_POSITION_RIGHT).writeToProto(proto, BOUND_RIGHT); |
| 509 | mBounds.getRect(BOUNDS_POSITION_BOTTOM).writeToProto(proto, BOUND_BOTTOM); |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 510 | proto.end(token); |
| 511 | } |
| 512 | |
| 513 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 514 | * Insets the reference frame of the cutout in the given directions. |
| 515 | * |
| 516 | * @return a copy of this instance which has been inset |
| 517 | * @hide |
| 518 | */ |
| 519 | public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { |
Jorim Jaggi | 4981f15 | 2019-03-26 18:58:45 +0100 | [diff] [blame] | 520 | if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0 |
| 521 | || isBoundsEmpty()) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 522 | return this; |
| 523 | } |
| 524 | |
| 525 | Rect safeInsets = new Rect(mSafeInsets); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 526 | |
| 527 | // Note: it's not really well defined what happens when the inset is negative, because we |
| 528 | // don't know if the safe inset needs to expand in general. |
| 529 | if (insetTop > 0 || safeInsets.top > 0) { |
| 530 | safeInsets.top = atLeastZero(safeInsets.top - insetTop); |
| 531 | } |
| 532 | if (insetBottom > 0 || safeInsets.bottom > 0) { |
| 533 | safeInsets.bottom = atLeastZero(safeInsets.bottom - insetBottom); |
| 534 | } |
| 535 | if (insetLeft > 0 || safeInsets.left > 0) { |
| 536 | safeInsets.left = atLeastZero(safeInsets.left - insetLeft); |
| 537 | } |
| 538 | if (insetRight > 0 || safeInsets.right > 0) { |
| 539 | safeInsets.right = atLeastZero(safeInsets.right - insetRight); |
| 540 | } |
| 541 | |
Jorim Jaggi | 4981f15 | 2019-03-26 18:58:45 +0100 | [diff] [blame] | 542 | // If we are not cutting off part of the cutout by insetting it on bottom/right, and we also |
| 543 | // don't move it around, we can avoid the allocation and copy of the instance. |
| 544 | if (insetLeft == 0 && insetTop == 0 && mSafeInsets.equals(safeInsets)) { |
| 545 | return this; |
| 546 | } |
| 547 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 548 | Rect[] bounds = mBounds.getRects(); |
| 549 | for (int i = 0; i < bounds.length; ++i) { |
| 550 | if (!bounds[i].equals(ZERO_RECT)) { |
| 551 | bounds[i].offset(-insetLeft, -insetTop); |
| 552 | } |
| 553 | } |
| 554 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 555 | return new DisplayCutout(safeInsets, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 559 | * 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] | 560 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 561 | * @param safeInsets the new safe insets in pixels |
| 562 | * @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] | 563 | * |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 564 | * @hide |
| 565 | */ |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 566 | public DisplayCutout replaceSafeInsets(Rect safeInsets) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 567 | return new DisplayCutout(new Rect(safeInsets), mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | private static int atLeastZero(int value) { |
| 571 | return value < 0 ? 0 : value; |
| 572 | } |
| 573 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 574 | |
| 575 | /** |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 576 | * Creates an instance from a bounding rect. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 577 | * |
| 578 | * @hide |
| 579 | */ |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 580 | @VisibleForTesting |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 581 | public static DisplayCutout fromBoundingRect( |
| 582 | int left, int top, int right, int bottom, @BoundsPosition int pos) { |
| 583 | Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH]; |
| 584 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { |
| 585 | bounds[i] = (pos == i) ? new Rect(left, top, right, bottom) : new Rect(); |
| 586 | } |
| 587 | return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 588 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 589 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 590 | /** |
| 591 | * Creates an instance from a bounding {@link Path}. |
| 592 | * |
| 593 | * @hide |
| 594 | */ |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 595 | public static DisplayCutout fromBounds(Rect[] bounds) { |
| 596 | return new DisplayCutout(ZERO_RECT, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /** |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 600 | * Creates the display cutout according to |
| 601 | * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest |
| 602 | * rectangle-base approximation of the cutout. |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 603 | * |
| 604 | * @hide |
| 605 | */ |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 606 | public static DisplayCutout fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight) { |
| 607 | return fromSpec(res.getString(R.string.config_mainBuiltInDisplayCutoutRectApproximation), |
Adrian Roos | 535c420 | 2018-04-16 16:12:40 +0200 | [diff] [blame] | 608 | displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /** |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 612 | * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout. |
| 613 | * |
| 614 | * @hide |
| 615 | */ |
| 616 | public static Path pathFromResources(Resources res, int displayWidth, int displayHeight) { |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 617 | return pathAndDisplayCutoutFromSpec( |
| 618 | res.getString(R.string.config_mainBuiltInDisplayCutout), |
Adrian Roos | 535c420 | 2018-04-16 16:12:40 +0200 | [diff] [blame] | 619 | displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT).first; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /** |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 623 | * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec. |
| 624 | * |
| 625 | * @hide |
| 626 | */ |
| 627 | @VisibleForTesting(visibility = PRIVATE) |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 628 | public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight, |
| 629 | float density) { |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 630 | return pathAndDisplayCutoutFromSpec(spec, displayWidth, displayHeight, density).second; |
| 631 | } |
| 632 | |
| 633 | private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(String spec, |
| 634 | int displayWidth, int displayHeight, float density) { |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 635 | if (TextUtils.isEmpty(spec)) { |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 636 | return NULL_PAIR; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 637 | } |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 638 | synchronized (CACHE_LOCK) { |
| 639 | if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 640 | && sCachedDisplayHeight == displayHeight |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 641 | && sCachedDensity == density) { |
| 642 | return sCachedCutout; |
| 643 | } |
| 644 | } |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 645 | spec = spec.trim(); |
Adrian Roos | b8b10f8 | 2018-03-15 20:09:42 +0100 | [diff] [blame] | 646 | final float offsetX; |
| 647 | if (spec.endsWith(RIGHT_MARKER)) { |
| 648 | offsetX = displayWidth; |
| 649 | spec = spec.substring(0, spec.length() - RIGHT_MARKER.length()).trim(); |
| 650 | } else { |
| 651 | offsetX = displayWidth / 2f; |
| 652 | } |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 653 | final boolean inDp = spec.endsWith(DP_MARKER); |
| 654 | if (inDp) { |
| 655 | spec = spec.substring(0, spec.length() - DP_MARKER.length()); |
| 656 | } |
| 657 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 658 | String bottomSpec = null; |
| 659 | if (spec.contains(BOTTOM_MARKER)) { |
| 660 | String[] splits = spec.split(BOTTOM_MARKER, 2); |
| 661 | spec = splits[0].trim(); |
| 662 | bottomSpec = splits[1].trim(); |
| 663 | } |
| 664 | |
| 665 | final Path p; |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 666 | final Region r = Region.obtain(); |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 667 | try { |
| 668 | p = PathParser.createPathFromPathData(spec); |
| 669 | } catch (Throwable e) { |
| 670 | Log.wtf(TAG, "Could not inflate cutout: ", e); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 671 | return NULL_PAIR; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | final Matrix m = new Matrix(); |
| 675 | if (inDp) { |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 676 | m.postScale(density, density); |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 677 | } |
Adrian Roos | b8b10f8 | 2018-03-15 20:09:42 +0100 | [diff] [blame] | 678 | m.postTranslate(offsetX, 0); |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 679 | p.transform(m); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 680 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 681 | Rect boundTop = new Rect(); |
| 682 | toRectAndAddToRegion(p, r, boundTop); |
| 683 | final int topInset = boundTop.bottom; |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 684 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 685 | Rect boundBottom = null; |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 686 | final int bottomInset; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 687 | if (bottomSpec != null) { |
| 688 | final Path bottomPath; |
| 689 | try { |
| 690 | bottomPath = PathParser.createPathFromPathData(bottomSpec); |
| 691 | } catch (Throwable e) { |
| 692 | Log.wtf(TAG, "Could not inflate bottom cutout: ", e); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 693 | return NULL_PAIR; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 694 | } |
| 695 | // Keep top transform |
| 696 | m.postTranslate(0, displayHeight); |
| 697 | bottomPath.transform(m); |
| 698 | p.addPath(bottomPath); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 699 | boundBottom = new Rect(); |
| 700 | toRectAndAddToRegion(bottomPath, r, boundBottom); |
| 701 | bottomInset = displayHeight - boundBottom.top; |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 702 | } else { |
| 703 | bottomInset = 0; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 706 | Rect safeInset = new Rect(0, topInset, 0, bottomInset); |
| 707 | final DisplayCutout cutout = new DisplayCutout( |
| 708 | safeInset, null /* boundLeft */, boundTop, null /* boundRight */, boundBottom, |
| 709 | false /* copyArguments */); |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 710 | |
| 711 | final Pair<Path, DisplayCutout> result = new Pair<>(p, cutout); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 712 | synchronized (CACHE_LOCK) { |
| 713 | sCachedSpec = spec; |
| 714 | sCachedDisplayWidth = displayWidth; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 715 | sCachedDisplayHeight = displayHeight; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 716 | sCachedDensity = density; |
| 717 | sCachedCutout = result; |
| 718 | } |
| 719 | return result; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 720 | } |
| 721 | |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 722 | private static void toRectAndAddToRegion(Path p, Region inoutRegion, Rect inoutRect) { |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 723 | final RectF rectF = new RectF(); |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 724 | p.computeBounds(rectF, false /* unused */); |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 725 | rectF.round(inoutRect); |
| 726 | inoutRegion.op(inoutRect, Op.UNION); |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 729 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 730 | * Helper class for passing {@link DisplayCutout} through binder. |
| 731 | * |
| 732 | * Needed, because {@code readFromParcel} cannot be used with immutable classes. |
| 733 | * |
| 734 | * @hide |
| 735 | */ |
| 736 | public static final class ParcelableWrapper implements Parcelable { |
| 737 | |
| 738 | private DisplayCutout mInner; |
| 739 | |
| 740 | public ParcelableWrapper() { |
| 741 | this(NO_CUTOUT); |
| 742 | } |
| 743 | |
| 744 | public ParcelableWrapper(DisplayCutout cutout) { |
| 745 | mInner = cutout; |
| 746 | } |
| 747 | |
| 748 | @Override |
| 749 | public int describeContents() { |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | @Override |
| 754 | public void writeToParcel(Parcel out, int flags) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 755 | writeCutoutToParcel(mInner, out, flags); |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Writes a DisplayCutout to a {@link Parcel}. |
| 760 | * |
| 761 | * @see #readCutoutFromParcel(Parcel) |
| 762 | */ |
| 763 | public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) { |
| 764 | if (cutout == null) { |
| 765 | out.writeInt(-1); |
| 766 | } else if (cutout == NO_CUTOUT) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 767 | out.writeInt(0); |
| 768 | } else { |
| 769 | out.writeInt(1); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 770 | out.writeTypedObject(cutout.mSafeInsets, flags); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 771 | out.writeTypedArray(cutout.mBounds.getRects(), flags); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing |
| 777 | * instance. |
| 778 | * |
| 779 | * Needed for AIDL out parameters. |
| 780 | */ |
| 781 | public void readFromParcel(Parcel in) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 782 | mInner = readCutoutFromParcel(in); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 785 | public static final @android.annotation.NonNull Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 786 | @Override |
| 787 | public ParcelableWrapper createFromParcel(Parcel in) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 788 | return new ParcelableWrapper(readCutoutFromParcel(in)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | @Override |
| 792 | public ParcelableWrapper[] newArray(int size) { |
| 793 | return new ParcelableWrapper[size]; |
| 794 | } |
| 795 | }; |
| 796 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 797 | /** |
| 798 | * Reads a DisplayCutout from a {@link Parcel}. |
| 799 | * |
| 800 | * @see #writeCutoutToParcel(DisplayCutout, Parcel, int) |
| 801 | */ |
| 802 | public static DisplayCutout readCutoutFromParcel(Parcel in) { |
| 803 | int variant = in.readInt(); |
| 804 | if (variant == -1) { |
| 805 | return null; |
| 806 | } |
| 807 | if (variant == 0) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 808 | return NO_CUTOUT; |
| 809 | } |
| 810 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 811 | Rect safeInsets = in.readTypedObject(Rect.CREATOR); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 812 | Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH]; |
| 813 | in.readTypedArray(bounds, Rect.CREATOR); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 814 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 815 | return new DisplayCutout(safeInsets, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | public DisplayCutout get() { |
| 819 | return mInner; |
| 820 | } |
| 821 | |
| 822 | public void set(ParcelableWrapper cutout) { |
| 823 | mInner = cutout.get(); |
| 824 | } |
| 825 | |
| 826 | public void set(DisplayCutout cutout) { |
| 827 | mInner = cutout; |
| 828 | } |
| 829 | |
| 830 | @Override |
| 831 | public int hashCode() { |
| 832 | return mInner.hashCode(); |
| 833 | } |
| 834 | |
| 835 | @Override |
| 836 | public boolean equals(Object o) { |
| 837 | return o instanceof ParcelableWrapper |
| 838 | && mInner.equals(((ParcelableWrapper) o).mInner); |
| 839 | } |
| 840 | |
| 841 | @Override |
| 842 | public String toString() { |
| 843 | return String.valueOf(mInner); |
| 844 | } |
| 845 | } |
| 846 | } |