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 | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 34 | import android.graphics.Path; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 35 | import android.graphics.Rect; |
| 36 | import android.os.Parcel; |
| 37 | import android.os.Parcelable; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 38 | import android.text.TextUtils; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 39 | import android.util.Pair; |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 40 | import android.util.proto.ProtoOutputStream; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 41 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 42 | import com.android.internal.R; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 43 | import com.android.internal.annotations.GuardedBy; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 44 | import com.android.internal.annotations.VisibleForTesting; |
| 45 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 46 | import java.lang.annotation.Retention; |
| 47 | import java.lang.annotation.RetentionPolicy; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 48 | import java.util.ArrayList; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 49 | import java.util.Arrays; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 50 | import java.util.List; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 51 | |
| 52 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 53 | * 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] | 54 | * |
| 55 | * <p>{@code DisplayCutout} is immutable. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 56 | */ |
| 57 | public final class DisplayCutout { |
| 58 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 59 | private static final String TAG = "DisplayCutout"; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 60 | |
Adrian Roos | c84df77 | 2018-01-19 21:20:22 +0100 | [diff] [blame] | 61 | /** |
| 62 | * Category for overlays that allow emulating a display cutout on devices that don't have |
| 63 | * one. |
| 64 | * |
| 65 | * @see android.content.om.IOverlayManager |
| 66 | * @hide |
| 67 | */ |
| 68 | public static final String EMULATION_OVERLAY_CATEGORY = |
| 69 | "com.android.internal.display_cutout_emulation"; |
| 70 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 71 | private static final Rect ZERO_RECT = new Rect(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 72 | |
| 73 | /** |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 74 | * An instance where {@link #isEmpty()} returns {@code true}. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 75 | * |
| 76 | * @hide |
| 77 | */ |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 78 | public static final DisplayCutout NO_CUTOUT = new DisplayCutout( |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 79 | ZERO_RECT, Insets.NONE, ZERO_RECT, ZERO_RECT, ZERO_RECT, ZERO_RECT, |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 80 | false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 81 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 82 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 83 | private static final Pair<Path, DisplayCutout> NULL_PAIR = new Pair<>(null, null); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 84 | private static final Object CACHE_LOCK = new Object(); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 85 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 86 | @GuardedBy("CACHE_LOCK") |
| 87 | private static String sCachedSpec; |
| 88 | @GuardedBy("CACHE_LOCK") |
| 89 | private static int sCachedDisplayWidth; |
| 90 | @GuardedBy("CACHE_LOCK") |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 91 | private static int sCachedDisplayHeight; |
| 92 | @GuardedBy("CACHE_LOCK") |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 93 | private static float sCachedDensity; |
| 94 | @GuardedBy("CACHE_LOCK") |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 95 | private static Pair<Path, DisplayCutout> sCachedCutout = NULL_PAIR; |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 96 | @GuardedBy("CACHE_LOCK") |
| 97 | private static Insets sCachedWaterfallInsets; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 98 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 99 | private final Rect mSafeInsets; |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 100 | @NonNull |
| 101 | private final Insets mWaterfallInsets; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 102 | |
| 103 | |
| 104 | /** |
| 105 | * The bound is at the left of the screen. |
| 106 | * @hide |
| 107 | */ |
| 108 | public static final int BOUNDS_POSITION_LEFT = 0; |
| 109 | |
| 110 | /** |
| 111 | * The bound is at the top of the screen. |
| 112 | * @hide |
| 113 | */ |
| 114 | public static final int BOUNDS_POSITION_TOP = 1; |
| 115 | |
| 116 | /** |
| 117 | * The bound is at the right of the screen. |
| 118 | * @hide |
| 119 | */ |
| 120 | public static final int BOUNDS_POSITION_RIGHT = 2; |
| 121 | |
| 122 | /** |
| 123 | * The bound is at the bottom of the screen. |
| 124 | * @hide |
| 125 | */ |
| 126 | public static final int BOUNDS_POSITION_BOTTOM = 3; |
| 127 | |
| 128 | /** |
| 129 | * The number of possible positions at which bounds can be located. |
| 130 | * @hide |
| 131 | */ |
| 132 | public static final int BOUNDS_POSITION_LENGTH = 4; |
| 133 | |
| 134 | /** @hide */ |
| 135 | @IntDef(prefix = { "BOUNDS_POSITION_" }, value = { |
| 136 | BOUNDS_POSITION_LEFT, |
| 137 | BOUNDS_POSITION_TOP, |
| 138 | BOUNDS_POSITION_RIGHT, |
| 139 | BOUNDS_POSITION_BOTTOM |
| 140 | }) |
| 141 | @Retention(RetentionPolicy.SOURCE) |
| 142 | public @interface BoundsPosition {} |
| 143 | |
| 144 | private static class Bounds { |
| 145 | private final Rect[] mRects; |
| 146 | |
| 147 | private Bounds(Rect left, Rect top, Rect right, Rect bottom, boolean copyArguments) { |
| 148 | mRects = new Rect[BOUNDS_POSITION_LENGTH]; |
| 149 | mRects[BOUNDS_POSITION_LEFT] = getCopyOrRef(left, copyArguments); |
| 150 | mRects[BOUNDS_POSITION_TOP] = getCopyOrRef(top, copyArguments); |
| 151 | mRects[BOUNDS_POSITION_RIGHT] = getCopyOrRef(right, copyArguments); |
| 152 | mRects[BOUNDS_POSITION_BOTTOM] = getCopyOrRef(bottom, copyArguments); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | private Bounds(Rect[] rects, boolean copyArguments) { |
| 157 | if (rects.length != BOUNDS_POSITION_LENGTH) { |
| 158 | throw new IllegalArgumentException( |
| 159 | "rects must have exactly 4 elements: rects=" + Arrays.toString(rects)); |
| 160 | } |
| 161 | if (copyArguments) { |
| 162 | mRects = new Rect[BOUNDS_POSITION_LENGTH]; |
| 163 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { |
| 164 | mRects[i] = new Rect(rects[i]); |
| 165 | } |
| 166 | } else { |
| 167 | for (Rect rect : rects) { |
| 168 | if (rect == null) { |
| 169 | throw new IllegalArgumentException( |
| 170 | "rects must have non-null elements: rects=" |
| 171 | + Arrays.toString(rects)); |
| 172 | } |
| 173 | } |
| 174 | mRects = rects; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private boolean isEmpty() { |
| 179 | for (Rect rect : mRects) { |
| 180 | if (!rect.isEmpty()) { |
| 181 | return false; |
| 182 | } |
| 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | private Rect getRect(@BoundsPosition int pos) { |
| 188 | return new Rect(mRects[pos]); |
| 189 | } |
| 190 | |
| 191 | private Rect[] getRects() { |
| 192 | Rect[] rects = new Rect[BOUNDS_POSITION_LENGTH]; |
| 193 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { |
| 194 | rects[i] = new Rect(mRects[i]); |
| 195 | } |
| 196 | return rects; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public int hashCode() { |
| 201 | int result = 0; |
| 202 | for (Rect rect : mRects) { |
| 203 | result = result * 48271 + rect.hashCode(); |
| 204 | } |
| 205 | return result; |
| 206 | } |
| 207 | @Override |
| 208 | public boolean equals(Object o) { |
| 209 | if (o == this) { |
| 210 | return true; |
| 211 | } |
| 212 | if (o instanceof Bounds) { |
| 213 | Bounds b = (Bounds) o; |
| 214 | return Arrays.deepEquals(mRects, b.mRects); |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public String toString() { |
| 221 | return "Bounds=" + Arrays.toString(mRects); |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
| 226 | private final Bounds mBounds; |
| 227 | |
| 228 | /** |
| 229 | * Creates a DisplayCutout instance. |
| 230 | * |
Issei Suzuki | 8b0e617 | 2019-01-14 17:04:33 +0100 | [diff] [blame] | 231 | * <p>Note that this is only useful for tests. For production code, developers should always |
| 232 | * use a {@link DisplayCutout} obtained from the system.</p> |
| 233 | * |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 234 | * @param safeInsets the insets from each edge which avoid the display cutout as returned by |
| 235 | * {@link #getSafeInsetTop()} etc. |
| 236 | * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed, |
| 237 | * it's treated as an empty rectangle (0,0)-(0,0). |
| 238 | * @param boundTop the top bounding rect of the display cutout in pixels. If null is passed, |
| 239 | * it's treated as an empty rectangle (0,0)-(0,0). |
| 240 | * @param boundRight the right bounding rect of the display cutout in pixels. If null is |
| 241 | * passed, it's treated as an empty rectangle (0,0)-(0,0). |
| 242 | * @param boundBottom the bottom bounding rect of the display cutout in pixels. If null is |
| 243 | * passed, it's treated as an empty rectangle (0,0)-(0,0). |
| 244 | */ |
| 245 | // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 246 | public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft, |
| 247 | @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom) { |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 248 | this(safeInsets.toRect(), Insets.NONE, boundLeft, boundTop, boundRight, boundBottom, true); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Creates a DisplayCutout instance. |
| 253 | * |
| 254 | * <p>Note that this is only useful for tests. For production code, developers should always |
| 255 | * use a {@link DisplayCutout} obtained from the system.</p> |
| 256 | * |
| 257 | * @param safeInsets the insets from each edge which avoid the display cutout as returned by |
| 258 | * {@link #getSafeInsetTop()} etc. |
| 259 | * @param boundLeft the left bounding rect of the display cutout in pixels. If null is passed, |
| 260 | * it's treated as an empty rectangle (0,0)-(0,0). |
| 261 | * @param boundTop the top bounding rect of the display cutout in pixels. If null is passed, |
| 262 | * it's treated as an empty rectangle (0,0)-(0,0). |
| 263 | * @param boundRight the right bounding rect of the display cutout in pixels. If null is |
| 264 | * passed, it's treated as an empty rectangle (0,0)-(0,0). |
| 265 | * @param boundBottom the bottom bounding rect of the display cutout in pixels. If null is |
| 266 | * passed, it's treated as an empty rectangle (0,0)-(0,0). |
| 267 | * @param waterfallInsets the insets for the curved areas in waterfall display. |
| 268 | */ |
| 269 | public DisplayCutout(@NonNull Insets safeInsets, @Nullable Rect boundLeft, |
| 270 | @Nullable Rect boundTop, @Nullable Rect boundRight, @Nullable Rect boundBottom, |
| 271 | @NonNull Insets waterfallInsets) { |
| 272 | this(safeInsets.toRect(), waterfallInsets, boundLeft, boundTop, boundRight, boundBottom, |
| 273 | true); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 274 | } |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * Creates a DisplayCutout instance. |
| 278 | * |
Issei Suzuki | 8b0e617 | 2019-01-14 17:04:33 +0100 | [diff] [blame] | 279 | * <p>Note that this is only useful for tests. For production code, developers should always |
| 280 | * use a {@link DisplayCutout} obtained from the system.</p> |
| 281 | * |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 282 | * @param safeInsets the insets from each edge which avoid the display cutout as returned by |
| 283 | * {@link #getSafeInsetTop()} etc. |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 284 | * @param boundingRects the bounding rects of the display cutouts as returned by |
| 285 | * {@link #getBoundingRects()} ()}. |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 286 | * @deprecated Use {@link DisplayCutout#DisplayCutout(Insets, Rect, Rect, Rect, Rect)} instead. |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 287 | */ |
| 288 | // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 289 | @Deprecated |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 290 | public DisplayCutout(@Nullable Rect safeInsets, @Nullable List<Rect> boundingRects) { |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 291 | this(safeInsets, Insets.NONE, extractBoundsFromList(safeInsets, boundingRects), |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 292 | true /* copyArguments */); |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 293 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Creates a DisplayCutout instance. |
| 297 | * |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 298 | * @param safeInsets the insets from each edge which avoid the display cutout as returned by |
| 299 | * {@link #getSafeInsetTop()} etc. |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 300 | * @param copyArguments if true, create a copy of the arguments. If false, the passed arguments |
| 301 | * are not copied and MUST remain unchanged forever. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 302 | */ |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 303 | private DisplayCutout(Rect safeInsets, Insets waterfallInsets, Rect boundLeft, |
| 304 | Rect boundTop, Rect boundRight, Rect boundBottom, boolean copyArguments) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 305 | mSafeInsets = getCopyOrRef(safeInsets, copyArguments); |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 306 | mWaterfallInsets = waterfallInsets == null ? Insets.NONE : waterfallInsets; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 307 | mBounds = new Bounds(boundLeft, boundTop, boundRight, boundBottom, copyArguments); |
| 308 | } |
| 309 | |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 310 | private DisplayCutout(Rect safeInsets, Insets waterfallInsets, Rect[] bounds, |
| 311 | boolean copyArguments) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 312 | mSafeInsets = getCopyOrRef(safeInsets, copyArguments); |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 313 | mWaterfallInsets = waterfallInsets == null ? Insets.NONE : waterfallInsets; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 314 | mBounds = new Bounds(bounds, copyArguments); |
| 315 | } |
| 316 | |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 317 | private DisplayCutout(Rect safeInsets, Insets waterfallInsets, Bounds bounds) { |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 318 | mSafeInsets = safeInsets; |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 319 | mWaterfallInsets = waterfallInsets == null ? Insets.NONE : waterfallInsets; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 320 | mBounds = bounds; |
| 321 | |
| 322 | } |
| 323 | |
| 324 | private static Rect getCopyOrRef(Rect r, boolean copyArguments) { |
| 325 | if (r == null) { |
| 326 | return ZERO_RECT; |
| 327 | } else if (copyArguments) { |
| 328 | return new Rect(r); |
| 329 | } else { |
| 330 | return r; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 335 | * Return the waterfall insets. |
| 336 | */ |
| 337 | public @NonNull Insets getWaterfallInsets() { |
| 338 | return mWaterfallInsets; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /** |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 343 | * Find the position of the bounding rect, and create an array of Rect whose index represents |
| 344 | * the position (= BoundsPosition). |
| 345 | * |
| 346 | * @hide |
| 347 | */ |
| 348 | public static Rect[] extractBoundsFromList(Rect safeInsets, List<Rect> boundingRects) { |
| 349 | Rect[] sortedBounds = new Rect[BOUNDS_POSITION_LENGTH]; |
| 350 | for (int i = 0; i < sortedBounds.length; ++i) { |
| 351 | sortedBounds[i] = ZERO_RECT; |
| 352 | } |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 353 | if (safeInsets != null && boundingRects != null) { |
Issei Suzuki | 6b5f5e9 | 2019-07-15 15:44:01 +0200 | [diff] [blame] | 354 | // There is at most one non-functional area per short edge of the device, but none |
| 355 | // on the long edges, so either a) safeInsets.top and safeInsets.bottom is 0, or |
| 356 | // b) safeInsets.left and safeInset.right is 0. |
| 357 | final boolean topBottomInset = safeInsets.top > 0 || safeInsets.bottom > 0; |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 358 | for (Rect bound : boundingRects) { |
Issei Suzuki | 6b5f5e9 | 2019-07-15 15:44:01 +0200 | [diff] [blame] | 359 | if (topBottomInset) { |
| 360 | if (bound.top == 0) { |
| 361 | sortedBounds[BOUNDS_POSITION_TOP] = bound; |
| 362 | } else { |
| 363 | sortedBounds[BOUNDS_POSITION_BOTTOM] = bound; |
| 364 | } |
| 365 | } else { |
| 366 | if (bound.left == 0) { |
| 367 | sortedBounds[BOUNDS_POSITION_LEFT] = bound; |
| 368 | } else { |
| 369 | sortedBounds[BOUNDS_POSITION_RIGHT] = bound; |
| 370 | } |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 371 | } |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | return sortedBounds; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Returns true if there is no cutout, i.e. the bounds are empty. |
| 379 | * |
| 380 | * @hide |
| 381 | */ |
| 382 | public boolean isBoundsEmpty() { |
| 383 | return mBounds.isEmpty(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 387 | * Returns true if the safe insets are empty (and therefore the current view does not |
| 388 | * overlap with the cutout or cutout area). |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 389 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 390 | * @hide |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 391 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 392 | public boolean isEmpty() { |
| 393 | return mSafeInsets.equals(ZERO_RECT); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Tiger Huang | e06d071 | 2020-02-26 20:36:13 +0800 | [diff] [blame] | 396 | /** |
| 397 | * Returns the inset from the top which avoids the display cutout in pixels. |
| 398 | * |
| 399 | * @see WindowInsets.Type#displayCutout() |
| 400 | */ |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 401 | public int getSafeInsetTop() { |
| 402 | return mSafeInsets.top; |
| 403 | } |
| 404 | |
Tiger Huang | e06d071 | 2020-02-26 20:36:13 +0800 | [diff] [blame] | 405 | /** |
| 406 | * Returns the inset from the bottom which avoids the display cutout in pixels. |
| 407 | * |
| 408 | * @see WindowInsets.Type#displayCutout() |
| 409 | */ |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 410 | public int getSafeInsetBottom() { |
| 411 | return mSafeInsets.bottom; |
| 412 | } |
| 413 | |
Tiger Huang | e06d071 | 2020-02-26 20:36:13 +0800 | [diff] [blame] | 414 | /** |
| 415 | * Returns the inset from the left which avoids the display cutout in pixels. |
| 416 | * |
| 417 | * @see WindowInsets.Type#displayCutout() |
| 418 | */ |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 419 | public int getSafeInsetLeft() { |
| 420 | return mSafeInsets.left; |
| 421 | } |
| 422 | |
Tiger Huang | e06d071 | 2020-02-26 20:36:13 +0800 | [diff] [blame] | 423 | /** |
| 424 | * Returns the inset from the right which avoids the display cutout in pixels. |
| 425 | * |
| 426 | * @see WindowInsets.Type#displayCutout() |
| 427 | */ |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 428 | public int getSafeInsetRight() { |
| 429 | return mSafeInsets.right; |
| 430 | } |
| 431 | |
| 432 | /** |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 433 | * Returns the safe insets in a rect in pixel units. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 434 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 435 | * @return a rect which is set to the safe insets. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 436 | * @hide |
| 437 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 438 | public Rect getSafeInsets() { |
| 439 | return new Rect(mSafeInsets); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 443 | * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional |
| 444 | * area on the display. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 445 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 446 | * There will be at most one non-functional area per short edge of the device, and none on |
| 447 | * the long edges. |
| 448 | * |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 449 | * @return a list of bounding {@code Rect}s, one for each display cutout area. No empty Rect is |
| 450 | * returned. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 451 | */ |
Adrian Roos | de363d0 | 2018-10-12 13:03:56 +0200 | [diff] [blame] | 452 | @NonNull |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 453 | public List<Rect> getBoundingRects() { |
| 454 | List<Rect> result = new ArrayList<>(); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 455 | for (Rect bound : getBoundingRectsAll()) { |
| 456 | if (!bound.isEmpty()) { |
| 457 | result.add(new Rect(bound)); |
| 458 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 459 | } |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 460 | return result; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 463 | /** |
| 464 | * Returns an array of {@code Rect}s, each of which is the bounding rectangle for a non- |
| 465 | * functional area on the display. Ordinal value of BoundPosition is used as an index of |
| 466 | * the array. |
| 467 | * |
| 468 | * There will be at most one non-functional area per short edge of the device, and none on |
| 469 | * the long edges. |
| 470 | * |
| 471 | * @return an array of bounding {@code Rect}s, one for each display cutout area. This might |
| 472 | * contain ZERO_RECT, which means there is no cutout area at the position. |
| 473 | * |
| 474 | * @hide |
| 475 | */ |
| 476 | public Rect[] getBoundingRectsAll() { |
| 477 | return mBounds.getRects(); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Returns a bounding rectangle for a non-functional area on the display which is located on |
| 482 | * the left of the screen. |
| 483 | * |
| 484 | * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle |
| 485 | * is returned. |
| 486 | */ |
| 487 | public @NonNull Rect getBoundingRectLeft() { |
| 488 | return mBounds.getRect(BOUNDS_POSITION_LEFT); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Returns a bounding rectangle for a non-functional area on the display which is located on |
| 493 | * the top of the screen. |
| 494 | * |
| 495 | * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle |
| 496 | * is returned. |
| 497 | */ |
| 498 | public @NonNull Rect getBoundingRectTop() { |
| 499 | return mBounds.getRect(BOUNDS_POSITION_TOP); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Returns a bounding rectangle for a non-functional area on the display which is located on |
| 504 | * the right of the screen. |
| 505 | * |
| 506 | * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle |
| 507 | * is returned. |
| 508 | */ |
| 509 | public @NonNull Rect getBoundingRectRight() { |
| 510 | return mBounds.getRect(BOUNDS_POSITION_RIGHT); |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Returns a bounding rectangle for a non-functional area on the display which is located on |
| 515 | * the bottom of the screen. |
| 516 | * |
| 517 | * @return bounding rectangle in pixels. In case of no bounding rectangle, an empty rectangle |
| 518 | * is returned. |
| 519 | */ |
| 520 | public @NonNull Rect getBoundingRectBottom() { |
| 521 | return mBounds.getRect(BOUNDS_POSITION_BOTTOM); |
| 522 | } |
| 523 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 524 | @Override |
| 525 | public int hashCode() { |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 526 | return (mSafeInsets.hashCode() * 48271 + mBounds.hashCode()) * 48271 |
| 527 | + mWaterfallInsets.hashCode(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public boolean equals(Object o) { |
| 532 | if (o == this) { |
| 533 | return true; |
| 534 | } |
| 535 | if (o instanceof DisplayCutout) { |
| 536 | DisplayCutout c = (DisplayCutout) o; |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 537 | return mSafeInsets.equals(c.mSafeInsets) && mBounds.equals(c.mBounds) |
| 538 | && mWaterfallInsets.equals(c.mWaterfallInsets); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 539 | } |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public String toString() { |
| 545 | return "DisplayCutout{insets=" + mSafeInsets |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 546 | + " waterfall=" + mWaterfallInsets |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 547 | + " boundingRect={" + mBounds + "}" |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 548 | + "}"; |
| 549 | } |
| 550 | |
| 551 | /** |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 552 | * @hide |
| 553 | */ |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 554 | public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 555 | final long token = proto.start(fieldId); |
Jeffrey Huang | cb78285 | 2019-12-05 11:28:11 -0800 | [diff] [blame] | 556 | mSafeInsets.dumpDebug(proto, INSETS); |
| 557 | mBounds.getRect(BOUNDS_POSITION_LEFT).dumpDebug(proto, BOUND_LEFT); |
| 558 | mBounds.getRect(BOUNDS_POSITION_TOP).dumpDebug(proto, BOUND_TOP); |
| 559 | mBounds.getRect(BOUNDS_POSITION_RIGHT).dumpDebug(proto, BOUND_RIGHT); |
| 560 | mBounds.getRect(BOUNDS_POSITION_BOTTOM).dumpDebug(proto, BOUND_BOTTOM); |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 561 | mWaterfallInsets.toRect().dumpDebug(proto, INSETS); |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 562 | proto.end(token); |
| 563 | } |
| 564 | |
| 565 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 566 | * Insets the reference frame of the cutout in the given directions. |
| 567 | * |
| 568 | * @return a copy of this instance which has been inset |
| 569 | * @hide |
| 570 | */ |
| 571 | public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { |
Jorim Jaggi | 4981f15 | 2019-03-26 18:58:45 +0100 | [diff] [blame] | 572 | if (insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0 |
shawnlin | 16ba068 | 2020-02-21 19:35:19 +0800 | [diff] [blame] | 573 | || (isBoundsEmpty() && mWaterfallInsets.equals(Insets.NONE))) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 574 | return this; |
| 575 | } |
| 576 | |
shawnlin | 16ba068 | 2020-02-21 19:35:19 +0800 | [diff] [blame] | 577 | Rect safeInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, |
| 578 | new Rect(mSafeInsets)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 579 | |
Jorim Jaggi | 4981f15 | 2019-03-26 18:58:45 +0100 | [diff] [blame] | 580 | // If we are not cutting off part of the cutout by insetting it on bottom/right, and we also |
| 581 | // don't move it around, we can avoid the allocation and copy of the instance. |
| 582 | if (insetLeft == 0 && insetTop == 0 && mSafeInsets.equals(safeInsets)) { |
| 583 | return this; |
| 584 | } |
| 585 | |
shawnlin | 16ba068 | 2020-02-21 19:35:19 +0800 | [diff] [blame] | 586 | Rect waterfallInsets = insetInsets(insetLeft, insetTop, insetRight, insetBottom, |
| 587 | mWaterfallInsets.toRect()); |
| 588 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 589 | Rect[] bounds = mBounds.getRects(); |
| 590 | for (int i = 0; i < bounds.length; ++i) { |
| 591 | if (!bounds[i].equals(ZERO_RECT)) { |
| 592 | bounds[i].offset(-insetLeft, -insetTop); |
| 593 | } |
| 594 | } |
| 595 | |
shawnlin | 16ba068 | 2020-02-21 19:35:19 +0800 | [diff] [blame] | 596 | return new DisplayCutout(safeInsets, Insets.of(waterfallInsets), bounds, |
| 597 | false /* copyArguments */); |
| 598 | } |
| 599 | |
| 600 | private Rect insetInsets(int insetLeft, int insetTop, int insetRight, int insetBottom, |
| 601 | Rect insets) { |
| 602 | // Note: it's not really well defined what happens when the inset is negative, because we |
| 603 | // don't know if the safe inset needs to expand in general. |
| 604 | if (insetTop > 0 || insets.top > 0) { |
| 605 | insets.top = atLeastZero(insets.top - insetTop); |
| 606 | } |
| 607 | if (insetBottom > 0 || insets.bottom > 0) { |
| 608 | insets.bottom = atLeastZero(insets.bottom - insetBottom); |
| 609 | } |
| 610 | if (insetLeft > 0 || insets.left > 0) { |
| 611 | insets.left = atLeastZero(insets.left - insetLeft); |
| 612 | } |
| 613 | if (insetRight > 0 || insets.right > 0) { |
| 614 | insets.right = atLeastZero(insets.right - insetRight); |
| 615 | } |
| 616 | return insets; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 620 | * 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] | 621 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 622 | * @param safeInsets the new safe insets in pixels |
| 623 | * @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] | 624 | * |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 625 | * @hide |
| 626 | */ |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 627 | public DisplayCutout replaceSafeInsets(Rect safeInsets) { |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 628 | return new DisplayCutout(new Rect(safeInsets), mWaterfallInsets, mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | private static int atLeastZero(int value) { |
| 632 | return value < 0 ? 0 : value; |
| 633 | } |
| 634 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 635 | |
| 636 | /** |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 637 | * Creates an instance from a bounding rect. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 638 | * |
| 639 | * @hide |
| 640 | */ |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 641 | @VisibleForTesting |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 642 | public static DisplayCutout fromBoundingRect( |
| 643 | int left, int top, int right, int bottom, @BoundsPosition int pos) { |
| 644 | Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH]; |
| 645 | for (int i = 0; i < BOUNDS_POSITION_LENGTH; ++i) { |
| 646 | bounds[i] = (pos == i) ? new Rect(left, top, right, bottom) : new Rect(); |
| 647 | } |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 648 | return new DisplayCutout(ZERO_RECT, Insets.NONE, bounds, false /* copyArguments */); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Creates an instance from a bounding and waterfall insets. |
| 653 | * |
| 654 | * @hide |
| 655 | */ |
| 656 | public static DisplayCutout fromBoundsAndWaterfall(Rect[] bounds, Insets waterfallInsets) { |
| 657 | return new DisplayCutout(ZERO_RECT, waterfallInsets, bounds, false /* copyArguments */); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 658 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 659 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 660 | /** |
| 661 | * Creates an instance from a bounding {@link Path}. |
| 662 | * |
| 663 | * @hide |
| 664 | */ |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 665 | public static DisplayCutout fromBounds(Rect[] bounds) { |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 666 | return new DisplayCutout(ZERO_RECT, Insets.NONE, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /** |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 670 | * Creates the display cutout according to |
| 671 | * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest |
| 672 | * rectangle-base approximation of the cutout. |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 673 | * |
| 674 | * @hide |
| 675 | */ |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 676 | public static DisplayCutout fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight) { |
| 677 | return fromSpec(res.getString(R.string.config_mainBuiltInDisplayCutoutRectApproximation), |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 678 | displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT, |
| 679 | loadWaterfallInset(res)); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 683 | * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout. |
| 684 | * |
| 685 | * @hide |
| 686 | */ |
| 687 | public static Path pathFromResources(Resources res, int displayWidth, int displayHeight) { |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 688 | return pathAndDisplayCutoutFromSpec( |
| 689 | res.getString(R.string.config_mainBuiltInDisplayCutout), |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 690 | displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT, |
| 691 | loadWaterfallInset(res)).first; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /** |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 695 | * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec. |
| 696 | * |
| 697 | * @hide |
| 698 | */ |
| 699 | @VisibleForTesting(visibility = PRIVATE) |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 700 | public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight, |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 701 | float density, Insets waterfallInsets) { |
| 702 | return pathAndDisplayCutoutFromSpec( |
| 703 | spec, displayWidth, displayHeight, density, waterfallInsets).second; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(String spec, |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 707 | int displayWidth, int displayHeight, float density, Insets waterfallInsets) { |
| 708 | if (TextUtils.isEmpty(spec) && waterfallInsets.equals(Insets.NONE)) { |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 709 | return NULL_PAIR; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 710 | } |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 711 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 712 | synchronized (CACHE_LOCK) { |
| 713 | if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 714 | && sCachedDisplayHeight == displayHeight |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 715 | && sCachedDensity == density |
| 716 | && waterfallInsets.equals(sCachedWaterfallInsets)) { |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 717 | return sCachedCutout; |
| 718 | } |
| 719 | } |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 720 | |
Felka Chang | 489cf26 | 2019-12-26 13:36:23 +0800 | [diff] [blame] | 721 | spec = spec.trim(); |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 722 | |
Felka Chang | 489cf26 | 2019-12-26 13:36:23 +0800 | [diff] [blame] | 723 | CutoutSpecification cutoutSpec = new CutoutSpecification.Parser(density, |
| 724 | displayWidth, displayHeight).parse(spec); |
| 725 | Rect safeInset = cutoutSpec.getSafeInset(); |
| 726 | final Rect boundLeft = cutoutSpec.getLeftBound(); |
| 727 | final Rect boundTop = cutoutSpec.getTopBound(); |
| 728 | final Rect boundRight = cutoutSpec.getRightBound(); |
| 729 | final Rect boundBottom = cutoutSpec.getBottomBound(); |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 730 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 731 | |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 732 | if (!waterfallInsets.equals(Insets.NONE)) { |
| 733 | safeInset.set( |
| 734 | Math.max(waterfallInsets.left, safeInset.left), |
| 735 | Math.max(waterfallInsets.top, safeInset.top), |
| 736 | Math.max(waterfallInsets.right, safeInset.right), |
| 737 | Math.max(waterfallInsets.bottom, safeInset.bottom)); |
| 738 | } |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 739 | |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 740 | final DisplayCutout cutout = new DisplayCutout( |
Felka Chang | 489cf26 | 2019-12-26 13:36:23 +0800 | [diff] [blame] | 741 | safeInset, waterfallInsets, boundLeft, boundTop, |
| 742 | boundRight, boundBottom, false /* copyArguments */); |
| 743 | final Pair<Path, DisplayCutout> result = new Pair<>(cutoutSpec.getPath(), cutout); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 744 | synchronized (CACHE_LOCK) { |
| 745 | sCachedSpec = spec; |
| 746 | sCachedDisplayWidth = displayWidth; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 747 | sCachedDisplayHeight = displayHeight; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 748 | sCachedDensity = density; |
| 749 | sCachedCutout = result; |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 750 | sCachedWaterfallInsets = waterfallInsets; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 751 | } |
| 752 | return result; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 753 | } |
| 754 | |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 755 | private static Insets loadWaterfallInset(Resources res) { |
| 756 | return Insets.of( |
| 757 | res.getDimensionPixelSize(R.dimen.waterfall_display_left_edge_size), |
| 758 | res.getDimensionPixelSize(R.dimen.waterfall_display_top_edge_size), |
| 759 | res.getDimensionPixelSize(R.dimen.waterfall_display_right_edge_size), |
| 760 | res.getDimensionPixelSize(R.dimen.waterfall_display_bottom_edge_size)); |
| 761 | } |
| 762 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 763 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 764 | * Helper class for passing {@link DisplayCutout} through binder. |
| 765 | * |
| 766 | * Needed, because {@code readFromParcel} cannot be used with immutable classes. |
| 767 | * |
| 768 | * @hide |
| 769 | */ |
| 770 | public static final class ParcelableWrapper implements Parcelable { |
| 771 | |
| 772 | private DisplayCutout mInner; |
| 773 | |
| 774 | public ParcelableWrapper() { |
| 775 | this(NO_CUTOUT); |
| 776 | } |
| 777 | |
| 778 | public ParcelableWrapper(DisplayCutout cutout) { |
| 779 | mInner = cutout; |
| 780 | } |
| 781 | |
| 782 | @Override |
| 783 | public int describeContents() { |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | @Override |
| 788 | public void writeToParcel(Parcel out, int flags) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 789 | writeCutoutToParcel(mInner, out, flags); |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * Writes a DisplayCutout to a {@link Parcel}. |
| 794 | * |
| 795 | * @see #readCutoutFromParcel(Parcel) |
| 796 | */ |
| 797 | public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) { |
| 798 | if (cutout == null) { |
| 799 | out.writeInt(-1); |
| 800 | } else if (cutout == NO_CUTOUT) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 801 | out.writeInt(0); |
| 802 | } else { |
| 803 | out.writeInt(1); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 804 | out.writeTypedObject(cutout.mSafeInsets, flags); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 805 | out.writeTypedArray(cutout.mBounds.getRects(), flags); |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 806 | out.writeTypedObject(cutout.mWaterfallInsets, flags); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing |
| 812 | * instance. |
| 813 | * |
| 814 | * Needed for AIDL out parameters. |
| 815 | */ |
| 816 | public void readFromParcel(Parcel in) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 817 | mInner = readCutoutFromParcel(in); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 820 | public static final @android.annotation.NonNull Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 821 | @Override |
| 822 | public ParcelableWrapper createFromParcel(Parcel in) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 823 | return new ParcelableWrapper(readCutoutFromParcel(in)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | @Override |
| 827 | public ParcelableWrapper[] newArray(int size) { |
| 828 | return new ParcelableWrapper[size]; |
| 829 | } |
| 830 | }; |
| 831 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 832 | /** |
| 833 | * Reads a DisplayCutout from a {@link Parcel}. |
| 834 | * |
| 835 | * @see #writeCutoutToParcel(DisplayCutout, Parcel, int) |
| 836 | */ |
| 837 | public static DisplayCutout readCutoutFromParcel(Parcel in) { |
| 838 | int variant = in.readInt(); |
| 839 | if (variant == -1) { |
| 840 | return null; |
| 841 | } |
| 842 | if (variant == 0) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 843 | return NO_CUTOUT; |
| 844 | } |
| 845 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 846 | Rect safeInsets = in.readTypedObject(Rect.CREATOR); |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 847 | Rect[] bounds = new Rect[BOUNDS_POSITION_LENGTH]; |
| 848 | in.readTypedArray(bounds, Rect.CREATOR); |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 849 | Insets waterfallInsets = in.readTypedObject(Insets.CREATOR); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 850 | |
shawnlin | 7dabcab | 2020-01-13 22:24:43 +0800 | [diff] [blame] | 851 | return new DisplayCutout( |
| 852 | safeInsets, waterfallInsets, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | public DisplayCutout get() { |
| 856 | return mInner; |
| 857 | } |
| 858 | |
| 859 | public void set(ParcelableWrapper cutout) { |
| 860 | mInner = cutout.get(); |
| 861 | } |
| 862 | |
| 863 | public void set(DisplayCutout cutout) { |
| 864 | mInner = cutout; |
| 865 | } |
| 866 | |
| 867 | @Override |
| 868 | public int hashCode() { |
| 869 | return mInner.hashCode(); |
| 870 | } |
| 871 | |
| 872 | @Override |
| 873 | public boolean equals(Object o) { |
| 874 | return o instanceof ParcelableWrapper |
| 875 | && mInner.equals(((ParcelableWrapper) o).mInner); |
| 876 | } |
| 877 | |
| 878 | @Override |
| 879 | public String toString() { |
| 880 | return String.valueOf(mInner); |
| 881 | } |
| 882 | } |
| 883 | } |