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