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