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; |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 21 | import static android.view.DisplayCutoutProto.BOUNDS; |
| 22 | import static android.view.DisplayCutoutProto.INSETS; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 23 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 24 | import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; |
| 25 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 26 | import android.content.res.Resources; |
| 27 | import android.graphics.Matrix; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 28 | import android.graphics.Path; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 29 | import android.graphics.Rect; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 30 | import android.graphics.RectF; |
| 31 | import android.graphics.Region; |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 32 | import android.graphics.Region.Op; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 33 | import android.os.Parcel; |
| 34 | import android.os.Parcelable; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 35 | import android.text.TextUtils; |
| 36 | import android.util.Log; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 37 | import android.util.Pair; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 38 | import android.util.PathParser; |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 39 | import android.util.proto.ProtoOutputStream; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 40 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 41 | import com.android.internal.R; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 42 | import com.android.internal.annotations.GuardedBy; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 43 | import com.android.internal.annotations.VisibleForTesting; |
| 44 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.List; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 47 | |
| 48 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 49 | * 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] | 50 | * |
| 51 | * <p>{@code DisplayCutout} is immutable. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 52 | */ |
| 53 | public final class DisplayCutout { |
| 54 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 55 | private static final String TAG = "DisplayCutout"; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 56 | private static final String BOTTOM_MARKER = "@bottom"; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 57 | private static final String DP_MARKER = "@dp"; |
Adrian Roos | b8b10f8 | 2018-03-15 20:09:42 +0100 | [diff] [blame] | 58 | private static final String RIGHT_MARKER = "@right"; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 59 | |
Adrian Roos | c84df77 | 2018-01-19 21:20:22 +0100 | [diff] [blame] | 60 | /** |
| 61 | * Category for overlays that allow emulating a display cutout on devices that don't have |
| 62 | * one. |
| 63 | * |
| 64 | * @see android.content.om.IOverlayManager |
| 65 | * @hide |
| 66 | */ |
| 67 | public static final String EMULATION_OVERLAY_CATEGORY = |
| 68 | "com.android.internal.display_cutout_emulation"; |
| 69 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 70 | private static final Rect ZERO_RECT = new Rect(); |
| 71 | private static final Region EMPTY_REGION = new Region(); |
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 | */ |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 78 | public static final DisplayCutout NO_CUTOUT = new DisplayCutout(ZERO_RECT, EMPTY_REGION, |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 79 | false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 80 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 81 | |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 82 | private static final Pair<Path, DisplayCutout> NULL_PAIR = new Pair<>(null, null); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 83 | private static final Object CACHE_LOCK = new Object(); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 84 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 85 | @GuardedBy("CACHE_LOCK") |
| 86 | private static String sCachedSpec; |
| 87 | @GuardedBy("CACHE_LOCK") |
| 88 | private static int sCachedDisplayWidth; |
| 89 | @GuardedBy("CACHE_LOCK") |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 90 | private static int sCachedDisplayHeight; |
| 91 | @GuardedBy("CACHE_LOCK") |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 92 | private static float sCachedDensity; |
| 93 | @GuardedBy("CACHE_LOCK") |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 94 | private static Pair<Path, DisplayCutout> sCachedCutout = NULL_PAIR; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 95 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 96 | private final Rect mSafeInsets; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 97 | private final Region mBounds; |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Creates a DisplayCutout instance. |
| 101 | * |
| 102 | * @param safeInsets the insets from each edge which avoid the display cutout as returned by |
| 103 | * {@link #getSafeInsetTop()} etc. |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 104 | * @param boundingRects the bounding rects of the display cutouts as returned by |
| 105 | * {@link #getBoundingRects()} ()}. |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 106 | */ |
| 107 | // TODO(b/73953958): @VisibleForTesting(visibility = PRIVATE) |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 108 | public DisplayCutout(Rect safeInsets, List<Rect> boundingRects) { |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 109 | this(safeInsets != null ? new Rect(safeInsets) : ZERO_RECT, |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 110 | boundingRectsToRegion(boundingRects), |
| 111 | true /* copyArguments */); |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 112 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Creates a DisplayCutout instance. |
| 116 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 117 | * @param copyArguments if true, create a copy of the arguments. If false, the passed arguments |
| 118 | * are not copied and MUST remain unchanged forever. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 119 | */ |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 120 | private DisplayCutout(Rect safeInsets, Region bounds, boolean copyArguments) { |
| 121 | mSafeInsets = safeInsets == null ? ZERO_RECT : |
| 122 | (copyArguments ? new Rect(safeInsets) : safeInsets); |
| 123 | mBounds = bounds == null ? Region.obtain() : |
| 124 | (copyArguments ? Region.obtain(bounds) : bounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 128 | * Returns true if the safe insets are empty (and therefore the current view does not |
| 129 | * overlap with the cutout or cutout area). |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 130 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 131 | * @hide |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 132 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 133 | public boolean isEmpty() { |
| 134 | return mSafeInsets.equals(ZERO_RECT); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 137 | /** |
| 138 | * Returns true if there is no cutout, i.e. the bounds are empty. |
| 139 | * |
| 140 | * @hide |
| 141 | */ |
| 142 | public boolean isBoundsEmpty() { |
| 143 | return mBounds.isEmpty(); |
| 144 | } |
| 145 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 146 | /** 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] | 147 | public int getSafeInsetTop() { |
| 148 | return mSafeInsets.top; |
| 149 | } |
| 150 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 151 | /** 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] | 152 | public int getSafeInsetBottom() { |
| 153 | return mSafeInsets.bottom; |
| 154 | } |
| 155 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 156 | /** 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] | 157 | public int getSafeInsetLeft() { |
| 158 | return mSafeInsets.left; |
| 159 | } |
| 160 | |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 161 | /** 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] | 162 | public int getSafeInsetRight() { |
| 163 | return mSafeInsets.right; |
| 164 | } |
| 165 | |
| 166 | /** |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 167 | * Returns the safe insets in a rect in pixel units. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 168 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 169 | * @return a rect which is set to the safe insets. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 170 | * @hide |
| 171 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 172 | public Rect getSafeInsets() { |
| 173 | return new Rect(mSafeInsets); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 177 | * Returns the bounding region of the cutout. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 178 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 179 | * <p> |
| 180 | * <strong>Note:</strong> There may be more than one cutout, in which case the returned |
| 181 | * {@code Region} will be non-contiguous and its bounding rect will be meaningless without |
| 182 | * intersecting it first. |
| 183 | * |
| 184 | * Example: |
| 185 | * <pre> |
| 186 | * // Getting the bounding rectangle of the top display cutout |
| 187 | * Region bounds = displayCutout.getBounds(); |
| 188 | * bounds.op(0, 0, Integer.MAX_VALUE, displayCutout.getSafeInsetTop(), Region.Op.INTERSECT); |
| 189 | * Rect topDisplayCutout = bounds.getBoundingRect(); |
| 190 | * </pre> |
| 191 | * |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 192 | * @return the bounding region of the cutout. Coordinates are relative |
Adrian Roos | 9bbd966 | 2018-03-02 14:31:37 +0100 | [diff] [blame] | 193 | * to the top-left corner of the content view and in pixel units. |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 194 | * @hide |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 195 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 196 | public Region getBounds() { |
| 197 | return Region.obtain(mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 201 | * Returns a list of {@code Rect}s, each of which is the bounding rectangle for a non-functional |
| 202 | * area on the display. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 203 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 204 | * There will be at most one non-functional area per short edge of the device, and none on |
| 205 | * the long edges. |
| 206 | * |
| 207 | * @return a list of bounding {@code Rect}s, one for each display cutout area. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 208 | */ |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 209 | public List<Rect> getBoundingRects() { |
| 210 | List<Rect> result = new ArrayList<>(); |
| 211 | Region bounds = Region.obtain(); |
| 212 | // top |
| 213 | bounds.set(mBounds); |
| 214 | bounds.op(0, 0, Integer.MAX_VALUE, getSafeInsetTop(), Region.Op.INTERSECT); |
| 215 | if (!bounds.isEmpty()) { |
| 216 | result.add(bounds.getBounds()); |
| 217 | } |
| 218 | // left |
| 219 | bounds.set(mBounds); |
| 220 | bounds.op(0, 0, getSafeInsetLeft(), Integer.MAX_VALUE, Region.Op.INTERSECT); |
| 221 | if (!bounds.isEmpty()) { |
| 222 | result.add(bounds.getBounds()); |
| 223 | } |
| 224 | // right & bottom |
| 225 | bounds.set(mBounds); |
| 226 | bounds.op(getSafeInsetLeft() + 1, getSafeInsetTop() + 1, |
| 227 | Integer.MAX_VALUE, Integer.MAX_VALUE, Region.Op.INTERSECT); |
| 228 | if (!bounds.isEmpty()) { |
| 229 | result.add(bounds.getBounds()); |
| 230 | } |
| 231 | bounds.recycle(); |
| 232 | return result; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public int hashCode() { |
| 237 | int result = mSafeInsets.hashCode(); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 238 | result = result * 31 + mBounds.getBounds().hashCode(); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 239 | return result; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public boolean equals(Object o) { |
| 244 | if (o == this) { |
| 245 | return true; |
| 246 | } |
| 247 | if (o instanceof DisplayCutout) { |
| 248 | DisplayCutout c = (DisplayCutout) o; |
| 249 | return mSafeInsets.equals(c.mSafeInsets) |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 250 | && mBounds.equals(c.mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 251 | } |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public String toString() { |
| 257 | return "DisplayCutout{insets=" + mSafeInsets |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 258 | + " boundingRect=" + mBounds.getBounds() |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 259 | + "}"; |
| 260 | } |
| 261 | |
| 262 | /** |
Vishnu Nair | 1d0fa07 | 2018-01-04 07:53:00 -0800 | [diff] [blame] | 263 | * @hide |
| 264 | */ |
| 265 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 266 | final long token = proto.start(fieldId); |
| 267 | mSafeInsets.writeToProto(proto, INSETS); |
| 268 | mBounds.getBounds().writeToProto(proto, BOUNDS); |
| 269 | proto.end(token); |
| 270 | } |
| 271 | |
| 272 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 273 | * Insets the reference frame of the cutout in the given directions. |
| 274 | * |
| 275 | * @return a copy of this instance which has been inset |
| 276 | * @hide |
| 277 | */ |
| 278 | public DisplayCutout inset(int insetLeft, int insetTop, int insetRight, int insetBottom) { |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 279 | if (mBounds.isEmpty() |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 280 | || insetLeft == 0 && insetTop == 0 && insetRight == 0 && insetBottom == 0) { |
| 281 | return this; |
| 282 | } |
| 283 | |
| 284 | Rect safeInsets = new Rect(mSafeInsets); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 285 | Region bounds = Region.obtain(mBounds); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 286 | |
| 287 | // Note: it's not really well defined what happens when the inset is negative, because we |
| 288 | // don't know if the safe inset needs to expand in general. |
| 289 | if (insetTop > 0 || safeInsets.top > 0) { |
| 290 | safeInsets.top = atLeastZero(safeInsets.top - insetTop); |
| 291 | } |
| 292 | if (insetBottom > 0 || safeInsets.bottom > 0) { |
| 293 | safeInsets.bottom = atLeastZero(safeInsets.bottom - insetBottom); |
| 294 | } |
| 295 | if (insetLeft > 0 || safeInsets.left > 0) { |
| 296 | safeInsets.left = atLeastZero(safeInsets.left - insetLeft); |
| 297 | } |
| 298 | if (insetRight > 0 || safeInsets.right > 0) { |
| 299 | safeInsets.right = atLeastZero(safeInsets.right - insetRight); |
| 300 | } |
| 301 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 302 | bounds.translate(-insetLeft, -insetTop); |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 303 | return new DisplayCutout(safeInsets, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 307 | * 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] | 308 | * |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 309 | * @param safeInsets the new safe insets in pixels |
| 310 | * @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] | 311 | * |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 312 | * @hide |
| 313 | */ |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 314 | public DisplayCutout replaceSafeInsets(Rect safeInsets) { |
| 315 | return new DisplayCutout(new Rect(safeInsets), mBounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | private static int atLeastZero(int value) { |
| 319 | return value < 0 ? 0 : value; |
| 320 | } |
| 321 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 322 | |
| 323 | /** |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 324 | * Creates an instance from a bounding rect. |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 325 | * |
| 326 | * @hide |
| 327 | */ |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 328 | @VisibleForTesting |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 329 | public static DisplayCutout fromBoundingRect(int left, int top, int right, int bottom) { |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 330 | Region r = Region.obtain(); |
| 331 | r.set(left, top, right, bottom); |
| 332 | return fromBounds(r); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 333 | } |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 334 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 335 | /** |
| 336 | * Creates an instance from a bounding {@link Path}. |
| 337 | * |
| 338 | * @hide |
| 339 | */ |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 340 | public static DisplayCutout fromBounds(Region region) { |
| 341 | return new DisplayCutout(ZERO_RECT, region, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 345 | * Creates the display cutout according to |
| 346 | * @android:string/config_mainBuiltInDisplayCutoutRectApproximation, which is the closest |
| 347 | * rectangle-base approximation of the cutout. |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 348 | * |
| 349 | * @hide |
| 350 | */ |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 351 | public static DisplayCutout fromResourcesRectApproximation(Resources res, int displayWidth, int displayHeight) { |
| 352 | return fromSpec(res.getString(R.string.config_mainBuiltInDisplayCutoutRectApproximation), |
Adrian Roos | 535c420 | 2018-04-16 16:12:40 +0200 | [diff] [blame] | 353 | displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 357 | * Creates an instance according to @android:string/config_mainBuiltInDisplayCutout. |
| 358 | * |
| 359 | * @hide |
| 360 | */ |
| 361 | public static Path pathFromResources(Resources res, int displayWidth, int displayHeight) { |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 362 | return pathAndDisplayCutoutFromSpec( |
| 363 | res.getString(R.string.config_mainBuiltInDisplayCutout), |
Adrian Roos | 535c420 | 2018-04-16 16:12:40 +0200 | [diff] [blame] | 364 | displayWidth, displayHeight, DENSITY_DEVICE_STABLE / (float) DENSITY_DEFAULT).first; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 368 | * Creates an instance according to the supplied {@link android.util.PathParser.PathData} spec. |
| 369 | * |
| 370 | * @hide |
| 371 | */ |
| 372 | @VisibleForTesting(visibility = PRIVATE) |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 373 | public static DisplayCutout fromSpec(String spec, int displayWidth, int displayHeight, |
| 374 | float density) { |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 375 | return pathAndDisplayCutoutFromSpec(spec, displayWidth, displayHeight, density).second; |
| 376 | } |
| 377 | |
| 378 | private static Pair<Path, DisplayCutout> pathAndDisplayCutoutFromSpec(String spec, |
| 379 | int displayWidth, int displayHeight, float density) { |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 380 | if (TextUtils.isEmpty(spec)) { |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 381 | return NULL_PAIR; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 382 | } |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 383 | synchronized (CACHE_LOCK) { |
| 384 | if (spec.equals(sCachedSpec) && sCachedDisplayWidth == displayWidth |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 385 | && sCachedDisplayHeight == displayHeight |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 386 | && sCachedDensity == density) { |
| 387 | return sCachedCutout; |
| 388 | } |
| 389 | } |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 390 | spec = spec.trim(); |
Adrian Roos | b8b10f8 | 2018-03-15 20:09:42 +0100 | [diff] [blame] | 391 | final float offsetX; |
| 392 | if (spec.endsWith(RIGHT_MARKER)) { |
| 393 | offsetX = displayWidth; |
| 394 | spec = spec.substring(0, spec.length() - RIGHT_MARKER.length()).trim(); |
| 395 | } else { |
| 396 | offsetX = displayWidth / 2f; |
| 397 | } |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 398 | final boolean inDp = spec.endsWith(DP_MARKER); |
| 399 | if (inDp) { |
| 400 | spec = spec.substring(0, spec.length() - DP_MARKER.length()); |
| 401 | } |
| 402 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 403 | String bottomSpec = null; |
| 404 | if (spec.contains(BOTTOM_MARKER)) { |
| 405 | String[] splits = spec.split(BOTTOM_MARKER, 2); |
| 406 | spec = splits[0].trim(); |
| 407 | bottomSpec = splits[1].trim(); |
| 408 | } |
| 409 | |
| 410 | final Path p; |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 411 | final Region r = Region.obtain(); |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 412 | try { |
| 413 | p = PathParser.createPathFromPathData(spec); |
| 414 | } catch (Throwable e) { |
| 415 | Log.wtf(TAG, "Could not inflate cutout: ", e); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 416 | return NULL_PAIR; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | final Matrix m = new Matrix(); |
| 420 | if (inDp) { |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 421 | m.postScale(density, density); |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 422 | } |
Adrian Roos | b8b10f8 | 2018-03-15 20:09:42 +0100 | [diff] [blame] | 423 | m.postTranslate(offsetX, 0); |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 424 | p.transform(m); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 425 | |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 426 | final Rect tmpRect = new Rect(); |
| 427 | toRectAndAddToRegion(p, r, tmpRect); |
| 428 | final int topInset = tmpRect.bottom; |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 429 | |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 430 | final int bottomInset; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 431 | if (bottomSpec != null) { |
| 432 | final Path bottomPath; |
| 433 | try { |
| 434 | bottomPath = PathParser.createPathFromPathData(bottomSpec); |
| 435 | } catch (Throwable e) { |
| 436 | Log.wtf(TAG, "Could not inflate bottom cutout: ", e); |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 437 | return NULL_PAIR; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 438 | } |
| 439 | // Keep top transform |
| 440 | m.postTranslate(0, displayHeight); |
| 441 | bottomPath.transform(m); |
| 442 | p.addPath(bottomPath); |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 443 | toRectAndAddToRegion(bottomPath, r, tmpRect); |
| 444 | bottomInset = displayHeight - tmpRect.top; |
| 445 | } else { |
| 446 | bottomInset = 0; |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 449 | // Reuse tmpRect as the inset rect we store into the DisplayCutout instance. |
| 450 | tmpRect.set(0, topInset, 0, bottomInset); |
| 451 | final DisplayCutout cutout = new DisplayCutout(tmpRect, r, false /* copyArguments */); |
| 452 | |
| 453 | final Pair<Path, DisplayCutout> result = new Pair<>(p, cutout); |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 454 | synchronized (CACHE_LOCK) { |
| 455 | sCachedSpec = spec; |
| 456 | sCachedDisplayWidth = displayWidth; |
Adrian Roos | 51072a8 | 2018-04-10 15:17:08 -0700 | [diff] [blame] | 457 | sCachedDisplayHeight = displayHeight; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 458 | sCachedDensity = density; |
| 459 | sCachedCutout = result; |
| 460 | } |
| 461 | return result; |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 464 | private static void toRectAndAddToRegion(Path p, Region inoutRegion, Rect inoutRect) { |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 465 | final RectF rectF = new RectF(); |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 466 | p.computeBounds(rectF, false /* unused */); |
Adrian Roos | 8c28c7c | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 467 | rectF.round(inoutRect); |
| 468 | inoutRegion.op(inoutRect, Op.UNION); |
Jorim Jaggi | 6064051 | 2018-06-29 01:14:31 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 471 | private static Region boundingRectsToRegion(List<Rect> rects) { |
| 472 | Region result = Region.obtain(); |
| 473 | if (rects != null) { |
| 474 | for (Rect r : rects) { |
| 475 | result.op(r, Region.Op.UNION); |
| 476 | } |
| 477 | } |
| 478 | return result; |
| 479 | } |
| 480 | |
Adrian Roos | 16693f3 | 2018-01-18 17:45:52 +0100 | [diff] [blame] | 481 | /** |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 482 | * Helper class for passing {@link DisplayCutout} through binder. |
| 483 | * |
| 484 | * Needed, because {@code readFromParcel} cannot be used with immutable classes. |
| 485 | * |
| 486 | * @hide |
| 487 | */ |
| 488 | public static final class ParcelableWrapper implements Parcelable { |
| 489 | |
| 490 | private DisplayCutout mInner; |
| 491 | |
| 492 | public ParcelableWrapper() { |
| 493 | this(NO_CUTOUT); |
| 494 | } |
| 495 | |
| 496 | public ParcelableWrapper(DisplayCutout cutout) { |
| 497 | mInner = cutout; |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public int describeContents() { |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public void writeToParcel(Parcel out, int flags) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 507 | writeCutoutToParcel(mInner, out, flags); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Writes a DisplayCutout to a {@link Parcel}. |
| 512 | * |
| 513 | * @see #readCutoutFromParcel(Parcel) |
| 514 | */ |
| 515 | public static void writeCutoutToParcel(DisplayCutout cutout, Parcel out, int flags) { |
| 516 | if (cutout == null) { |
| 517 | out.writeInt(-1); |
| 518 | } else if (cutout == NO_CUTOUT) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 519 | out.writeInt(0); |
| 520 | } else { |
| 521 | out.writeInt(1); |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 522 | out.writeTypedObject(cutout.mSafeInsets, flags); |
| 523 | out.writeTypedObject(cutout.mBounds, flags); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Similar to {@link Creator#createFromParcel(Parcel)}, but reads into an existing |
| 529 | * instance. |
| 530 | * |
| 531 | * Needed for AIDL out parameters. |
| 532 | */ |
| 533 | public void readFromParcel(Parcel in) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 534 | mInner = readCutoutFromParcel(in); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | public static final Creator<ParcelableWrapper> CREATOR = new Creator<ParcelableWrapper>() { |
| 538 | @Override |
| 539 | public ParcelableWrapper createFromParcel(Parcel in) { |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 540 | return new ParcelableWrapper(readCutoutFromParcel(in)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public ParcelableWrapper[] newArray(int size) { |
| 545 | return new ParcelableWrapper[size]; |
| 546 | } |
| 547 | }; |
| 548 | |
Adrian Roos | 1cf58505 | 2018-01-03 18:43:27 +0100 | [diff] [blame] | 549 | /** |
| 550 | * Reads a DisplayCutout from a {@link Parcel}. |
| 551 | * |
| 552 | * @see #writeCutoutToParcel(DisplayCutout, Parcel, int) |
| 553 | */ |
| 554 | public static DisplayCutout readCutoutFromParcel(Parcel in) { |
| 555 | int variant = in.readInt(); |
| 556 | if (variant == -1) { |
| 557 | return null; |
| 558 | } |
| 559 | if (variant == 0) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 560 | return NO_CUTOUT; |
| 561 | } |
| 562 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 563 | Rect safeInsets = in.readTypedObject(Rect.CREATOR); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 564 | Region bounds = in.readTypedObject(Region.CREATOR); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 565 | |
Adrian Roos | 6a4fa0e | 2018-03-05 19:50:16 +0100 | [diff] [blame] | 566 | return new DisplayCutout(safeInsets, bounds, false /* copyArguments */); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | public DisplayCutout get() { |
| 570 | return mInner; |
| 571 | } |
| 572 | |
| 573 | public void set(ParcelableWrapper cutout) { |
| 574 | mInner = cutout.get(); |
| 575 | } |
| 576 | |
| 577 | public void set(DisplayCutout cutout) { |
| 578 | mInner = cutout; |
| 579 | } |
| 580 | |
| 581 | @Override |
| 582 | public int hashCode() { |
| 583 | return mInner.hashCode(); |
| 584 | } |
| 585 | |
| 586 | @Override |
| 587 | public boolean equals(Object o) { |
| 588 | return o instanceof ParcelableWrapper |
| 589 | && mInner.equals(((ParcelableWrapper) o).mInner); |
| 590 | } |
| 591 | |
| 592 | @Override |
| 593 | public String toString() { |
| 594 | return String.valueOf(mInner); |
| 595 | } |
| 596 | } |
| 597 | } |