Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 18 | package android.view; |
| 19 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 21 | import android.graphics.Rect; |
| 22 | |
| 23 | /** |
| 24 | * Describes a set of insets for window content. |
| 25 | * |
| 26 | * <p>WindowInsets are immutable and may be expanded to include more inset types in the future. |
| 27 | * To adjust insets, use one of the supplied clone methods to obtain a new WindowInsets instance |
| 28 | * with the adjusted properties.</p> |
| 29 | * |
| 30 | * @see View.OnApplyWindowInsetsListener |
| 31 | * @see View#onApplyWindowInsets(WindowInsets) |
| 32 | */ |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 33 | public final class WindowInsets { |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 34 | |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 35 | private Rect mSystemWindowInsets; |
| 36 | private Rect mWindowDecorInsets; |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 37 | private Rect mStableInsets; |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 38 | private Rect mTempRect; |
Adam Powell | 973ddaa | 2014-04-15 17:38:54 -0700 | [diff] [blame] | 39 | private boolean mIsRound; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 40 | private DisplayCutout mDisplayCutout; |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 41 | |
Jorim Jaggi | 0ffd49c | 2016-02-12 15:04:21 -0800 | [diff] [blame] | 42 | /** |
| 43 | * In multi-window we force show the navigation bar. Because we don't want that the surface size |
| 44 | * changes in this mode, we instead have a flag whether the navigation bar size should always |
| 45 | * be consumed, so the app is treated like there is no virtual navigation bar at all. |
| 46 | */ |
| 47 | private boolean mAlwaysConsumeNavBar; |
| 48 | |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 49 | private boolean mSystemWindowInsetsConsumed = false; |
| 50 | private boolean mWindowDecorInsetsConsumed = false; |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 51 | private boolean mStableInsetsConsumed = false; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 52 | private boolean mDisplayCutoutConsumed = false; |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 53 | |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 54 | private static final Rect EMPTY_RECT = new Rect(0, 0, 0, 0); |
| 55 | |
| 56 | /** |
| 57 | * Since new insets may be added in the future that existing apps couldn't |
| 58 | * know about, this fully empty constant shouldn't be made available to apps |
| 59 | * since it would allow them to inadvertently consume unknown insets by returning it. |
| 60 | * @hide |
| 61 | */ |
Adam Powell | 720924b | 2014-06-12 14:51:10 -0700 | [diff] [blame] | 62 | public static final WindowInsets CONSUMED; |
| 63 | |
| 64 | static { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 65 | CONSUMED = new WindowInsets(null, null, null, false, false, null); |
Adam Powell | 720924b | 2014-06-12 14:51:10 -0700 | [diff] [blame] | 66 | } |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 67 | |
| 68 | /** @hide */ |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 69 | public WindowInsets(Rect systemWindowInsets, Rect windowDecorInsets, Rect stableInsets, |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 70 | boolean isRound, boolean alwaysConsumeNavBar, DisplayCutout displayCutout) { |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 71 | mSystemWindowInsetsConsumed = systemWindowInsets == null; |
| 72 | mSystemWindowInsets = mSystemWindowInsetsConsumed ? EMPTY_RECT : systemWindowInsets; |
| 73 | |
| 74 | mWindowDecorInsetsConsumed = windowDecorInsets == null; |
| 75 | mWindowDecorInsets = mWindowDecorInsetsConsumed ? EMPTY_RECT : windowDecorInsets; |
| 76 | |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 77 | mStableInsetsConsumed = stableInsets == null; |
| 78 | mStableInsets = mStableInsetsConsumed ? EMPTY_RECT : stableInsets; |
| 79 | |
Adam Powell | 973ddaa | 2014-04-15 17:38:54 -0700 | [diff] [blame] | 80 | mIsRound = isRound; |
Jorim Jaggi | 0ffd49c | 2016-02-12 15:04:21 -0800 | [diff] [blame] | 81 | mAlwaysConsumeNavBar = alwaysConsumeNavBar; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 82 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 83 | mDisplayCutoutConsumed = displayCutout == null; |
| 84 | mDisplayCutout = (mDisplayCutoutConsumed || displayCutout.isEmpty()) |
| 85 | ? null : displayCutout; |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Construct a new WindowInsets, copying all values from a source WindowInsets. |
| 90 | * |
| 91 | * @param src Source to copy insets from |
| 92 | */ |
| 93 | public WindowInsets(WindowInsets src) { |
| 94 | mSystemWindowInsets = src.mSystemWindowInsets; |
| 95 | mWindowDecorInsets = src.mWindowDecorInsets; |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 96 | mStableInsets = src.mStableInsets; |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 97 | mSystemWindowInsetsConsumed = src.mSystemWindowInsetsConsumed; |
| 98 | mWindowDecorInsetsConsumed = src.mWindowDecorInsetsConsumed; |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 99 | mStableInsetsConsumed = src.mStableInsetsConsumed; |
Adam Powell | 973ddaa | 2014-04-15 17:38:54 -0700 | [diff] [blame] | 100 | mIsRound = src.mIsRound; |
Jorim Jaggi | e5638a6 | 2016-03-25 22:57:01 -0700 | [diff] [blame] | 101 | mAlwaysConsumeNavBar = src.mAlwaysConsumeNavBar; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 102 | mDisplayCutout = src.mDisplayCutout; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 103 | mDisplayCutoutConsumed = src.mDisplayCutoutConsumed; |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** @hide */ |
| 107 | public WindowInsets(Rect systemWindowInsets) { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 108 | this(systemWindowInsets, null, null, false, false, null); |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Used to provide a safe copy of the system window insets to pass through |
| 113 | * to the existing fitSystemWindows method and other similar internals. |
| 114 | * @hide |
| 115 | */ |
| 116 | public Rect getSystemWindowInsets() { |
| 117 | if (mTempRect == null) { |
| 118 | mTempRect = new Rect(); |
| 119 | } |
Justin Koh | eba8782 | 2014-06-06 12:13:16 -0700 | [diff] [blame] | 120 | if (mSystemWindowInsets != null) { |
| 121 | mTempRect.set(mSystemWindowInsets); |
| 122 | } else { |
| 123 | // If there were no system window insets, this is just empty. |
| 124 | mTempRect.setEmpty(); |
| 125 | } |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 126 | return mTempRect; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns the left system window inset in pixels. |
| 131 | * |
| 132 | * <p>The system window inset represents the area of a full-screen window that is |
| 133 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 134 | * </p> |
| 135 | * |
| 136 | * @return The left system window inset |
| 137 | */ |
| 138 | public int getSystemWindowInsetLeft() { |
| 139 | return mSystemWindowInsets.left; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns the top system window inset in pixels. |
| 144 | * |
| 145 | * <p>The system window inset represents the area of a full-screen window that is |
| 146 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 147 | * </p> |
| 148 | * |
| 149 | * @return The top system window inset |
| 150 | */ |
| 151 | public int getSystemWindowInsetTop() { |
| 152 | return mSystemWindowInsets.top; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns the right system window inset in pixels. |
| 157 | * |
| 158 | * <p>The system window inset represents the area of a full-screen window that is |
| 159 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 160 | * </p> |
| 161 | * |
| 162 | * @return The right system window inset |
| 163 | */ |
| 164 | public int getSystemWindowInsetRight() { |
| 165 | return mSystemWindowInsets.right; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns the bottom system window inset in pixels. |
| 170 | * |
| 171 | * <p>The system window inset represents the area of a full-screen window that is |
| 172 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 173 | * </p> |
| 174 | * |
| 175 | * @return The bottom system window inset |
| 176 | */ |
| 177 | public int getSystemWindowInsetBottom() { |
| 178 | return mSystemWindowInsets.bottom; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns the left window decor inset in pixels. |
| 183 | * |
| 184 | * <p>The window decor inset represents the area of the window content area that is |
| 185 | * partially or fully obscured by decorations within the window provided by the framework. |
| 186 | * This can include action bars, title bars, toolbars, etc.</p> |
| 187 | * |
| 188 | * @return The left window decor inset |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 189 | * @hide pending API |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 190 | */ |
| 191 | public int getWindowDecorInsetLeft() { |
| 192 | return mWindowDecorInsets.left; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns the top window decor inset in pixels. |
| 197 | * |
| 198 | * <p>The window decor inset represents the area of the window content area that is |
| 199 | * partially or fully obscured by decorations within the window provided by the framework. |
| 200 | * This can include action bars, title bars, toolbars, etc.</p> |
| 201 | * |
| 202 | * @return The top window decor inset |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 203 | * @hide pending API |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 204 | */ |
| 205 | public int getWindowDecorInsetTop() { |
| 206 | return mWindowDecorInsets.top; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns the right window decor inset in pixels. |
| 211 | * |
| 212 | * <p>The window decor inset represents the area of the window content area that is |
| 213 | * partially or fully obscured by decorations within the window provided by the framework. |
| 214 | * This can include action bars, title bars, toolbars, etc.</p> |
| 215 | * |
| 216 | * @return The right window decor inset |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 217 | * @hide pending API |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 218 | */ |
| 219 | public int getWindowDecorInsetRight() { |
| 220 | return mWindowDecorInsets.right; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns the bottom window decor inset in pixels. |
| 225 | * |
| 226 | * <p>The window decor inset represents the area of the window content area that is |
| 227 | * partially or fully obscured by decorations within the window provided by the framework. |
| 228 | * This can include action bars, title bars, toolbars, etc.</p> |
| 229 | * |
| 230 | * @return The bottom window decor inset |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 231 | * @hide pending API |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 232 | */ |
| 233 | public int getWindowDecorInsetBottom() { |
| 234 | return mWindowDecorInsets.bottom; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns true if this WindowInsets has nonzero system window insets. |
| 239 | * |
| 240 | * <p>The system window inset represents the area of a full-screen window that is |
| 241 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 242 | * </p> |
| 243 | * |
| 244 | * @return true if any of the system window inset values are nonzero |
| 245 | */ |
| 246 | public boolean hasSystemWindowInsets() { |
| 247 | return mSystemWindowInsets.left != 0 || mSystemWindowInsets.top != 0 || |
| 248 | mSystemWindowInsets.right != 0 || mSystemWindowInsets.bottom != 0; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns true if this WindowInsets has nonzero window decor insets. |
| 253 | * |
| 254 | * <p>The window decor inset represents the area of the window content area that is |
| 255 | * partially or fully obscured by decorations within the window provided by the framework. |
| 256 | * This can include action bars, title bars, toolbars, etc.</p> |
| 257 | * |
| 258 | * @return true if any of the window decor inset values are nonzero |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 259 | * @hide pending API |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 260 | */ |
| 261 | public boolean hasWindowDecorInsets() { |
| 262 | return mWindowDecorInsets.left != 0 || mWindowDecorInsets.top != 0 || |
| 263 | mWindowDecorInsets.right != 0 || mWindowDecorInsets.bottom != 0; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns true if this WindowInsets has any nonzero insets. |
| 268 | * |
| 269 | * @return true if any inset values are nonzero |
| 270 | */ |
| 271 | public boolean hasInsets() { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 272 | return hasSystemWindowInsets() || hasWindowDecorInsets() || hasStableInsets() |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 273 | || mDisplayCutout != null; |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Adam Powell | 973ddaa | 2014-04-15 17:38:54 -0700 | [diff] [blame] | 276 | /** |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 277 | * Returns the display cutout if there is one. |
| 278 | * |
| 279 | * @return the display cutout or null if there is none |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 280 | * @see DisplayCutout |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 281 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 282 | @Nullable |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 283 | public DisplayCutout getDisplayCutout() { |
| 284 | return mDisplayCutout; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Returns a copy of this WindowInsets with the cutout fully consumed. |
| 289 | * |
| 290 | * @return A modified copy of this WindowInsets |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 291 | */ |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 292 | public WindowInsets consumeDisplayCutout() { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 293 | final WindowInsets result = new WindowInsets(this); |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 294 | result.mDisplayCutout = null; |
| 295 | result.mDisplayCutoutConsumed = true; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 296 | return result; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /** |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 301 | * Check if these insets have been fully consumed. |
| 302 | * |
| 303 | * <p>Insets are considered "consumed" if the applicable <code>consume*</code> methods |
| 304 | * have been called such that all insets have been set to zero. This affects propagation of |
| 305 | * insets through the view hierarchy; insets that have not been fully consumed will continue |
| 306 | * to propagate down to child views.</p> |
| 307 | * |
| 308 | * <p>The result of this method is equivalent to the return value of |
| 309 | * {@link View#fitSystemWindows(android.graphics.Rect)}.</p> |
| 310 | * |
| 311 | * @return true if the insets have been fully consumed. |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 312 | */ |
| 313 | public boolean isConsumed() { |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 314 | return mSystemWindowInsetsConsumed && mWindowDecorInsetsConsumed && mStableInsetsConsumed |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 315 | && mDisplayCutoutConsumed; |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
Adam Powell | 973ddaa | 2014-04-15 17:38:54 -0700 | [diff] [blame] | 319 | * Returns true if the associated window has a round shape. |
| 320 | * |
| 321 | * <p>A round window's left, top, right and bottom edges reach all the way to the |
| 322 | * associated edges of the window but the corners may not be visible. Views responding |
| 323 | * to round insets should take care to not lay out critical elements within the corners |
| 324 | * where they may not be accessible.</p> |
| 325 | * |
| 326 | * @return True if the window is round |
| 327 | */ |
| 328 | public boolean isRound() { |
| 329 | return mIsRound; |
| 330 | } |
| 331 | |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 332 | /** |
| 333 | * Returns a copy of this WindowInsets with the system window insets fully consumed. |
| 334 | * |
| 335 | * @return A modified copy of this WindowInsets |
| 336 | */ |
| 337 | public WindowInsets consumeSystemWindowInsets() { |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 338 | final WindowInsets result = new WindowInsets(this); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 339 | result.mSystemWindowInsets = EMPTY_RECT; |
| 340 | result.mSystemWindowInsetsConsumed = true; |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 341 | return result; |
| 342 | } |
| 343 | |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 344 | /** |
| 345 | * Returns a copy of this WindowInsets with selected system window insets fully consumed. |
| 346 | * |
| 347 | * @param left true to consume the left system window inset |
| 348 | * @param top true to consume the top system window inset |
| 349 | * @param right true to consume the right system window inset |
| 350 | * @param bottom true to consume the bottom system window inset |
| 351 | * @return A modified copy of this WindowInsets |
| 352 | * @hide pending API |
| 353 | */ |
| 354 | public WindowInsets consumeSystemWindowInsets(boolean left, boolean top, |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 355 | boolean right, boolean bottom) { |
| 356 | if (left || top || right || bottom) { |
| 357 | final WindowInsets result = new WindowInsets(this); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 358 | result.mSystemWindowInsets = new Rect( |
| 359 | left ? 0 : mSystemWindowInsets.left, |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 360 | top ? 0 : mSystemWindowInsets.top, |
| 361 | right ? 0 : mSystemWindowInsets.right, |
| 362 | bottom ? 0 : mSystemWindowInsets.bottom); |
| 363 | return result; |
| 364 | } |
| 365 | return this; |
| 366 | } |
| 367 | |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 368 | /** |
| 369 | * Returns a copy of this WindowInsets with selected system window insets replaced |
| 370 | * with new values. |
| 371 | * |
| 372 | * @param left New left inset in pixels |
| 373 | * @param top New top inset in pixels |
| 374 | * @param right New right inset in pixels |
| 375 | * @param bottom New bottom inset in pixels |
| 376 | * @return A modified copy of this WindowInsets |
| 377 | */ |
| 378 | public WindowInsets replaceSystemWindowInsets(int left, int top, |
| 379 | int right, int bottom) { |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 380 | final WindowInsets result = new WindowInsets(this); |
| 381 | result.mSystemWindowInsets = new Rect(left, top, right, bottom); |
| 382 | return result; |
| 383 | } |
| 384 | |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 385 | /** |
Adam Powell | d72068b | 2014-09-12 14:42:25 -0700 | [diff] [blame] | 386 | * Returns a copy of this WindowInsets with selected system window insets replaced |
| 387 | * with new values. |
| 388 | * |
| 389 | * @param systemWindowInsets New system window insets. Each field is the inset in pixels |
| 390 | * for that edge |
| 391 | * @return A modified copy of this WindowInsets |
| 392 | */ |
| 393 | public WindowInsets replaceSystemWindowInsets(Rect systemWindowInsets) { |
| 394 | final WindowInsets result = new WindowInsets(this); |
| 395 | result.mSystemWindowInsets = new Rect(systemWindowInsets); |
Adam Powell | d72068b | 2014-09-12 14:42:25 -0700 | [diff] [blame] | 396 | return result; |
| 397 | } |
| 398 | |
| 399 | /** |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 400 | * @hide |
| 401 | */ |
| 402 | public WindowInsets consumeWindowDecorInsets() { |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 403 | final WindowInsets result = new WindowInsets(this); |
| 404 | result.mWindowDecorInsets.set(0, 0, 0, 0); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 405 | result.mWindowDecorInsetsConsumed = true; |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 406 | return result; |
| 407 | } |
| 408 | |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 409 | /** |
| 410 | * @hide |
| 411 | */ |
| 412 | public WindowInsets consumeWindowDecorInsets(boolean left, boolean top, |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 413 | boolean right, boolean bottom) { |
| 414 | if (left || top || right || bottom) { |
| 415 | final WindowInsets result = new WindowInsets(this); |
| 416 | result.mWindowDecorInsets = new Rect(left ? 0 : mWindowDecorInsets.left, |
| 417 | top ? 0 : mWindowDecorInsets.top, |
| 418 | right ? 0 : mWindowDecorInsets.right, |
| 419 | bottom ? 0 : mWindowDecorInsets.bottom); |
| 420 | return result; |
| 421 | } |
| 422 | return this; |
| 423 | } |
| 424 | |
Adam Powell | f4a3941 | 2014-05-05 17:29:17 -0700 | [diff] [blame] | 425 | /** |
| 426 | * @hide |
| 427 | */ |
| 428 | public WindowInsets replaceWindowDecorInsets(int left, int top, int right, int bottom) { |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 429 | final WindowInsets result = new WindowInsets(this); |
| 430 | result.mWindowDecorInsets = new Rect(left, top, right, bottom); |
| 431 | return result; |
| 432 | } |
| 433 | |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 434 | /** |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 435 | * Returns the top stable inset in pixels. |
| 436 | * |
| 437 | * <p>The stable inset represents the area of a full-screen window that <b>may</b> be |
| 438 | * partially or fully obscured by the system UI elements. This value does not change |
| 439 | * based on the visibility state of those elements; for example, if the status bar is |
| 440 | * normally shown, but temporarily hidden, the stable inset will still provide the inset |
| 441 | * associated with the status bar being shown.</p> |
| 442 | * |
| 443 | * @return The top stable inset |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 444 | */ |
| 445 | public int getStableInsetTop() { |
| 446 | return mStableInsets.top; |
| 447 | } |
| 448 | |
| 449 | /** |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 450 | * Returns the left stable inset in pixels. |
| 451 | * |
| 452 | * <p>The stable inset represents the area of a full-screen window that <b>may</b> be |
| 453 | * partially or fully obscured by the system UI elements. This value does not change |
| 454 | * based on the visibility state of those elements; for example, if the status bar is |
| 455 | * normally shown, but temporarily hidden, the stable inset will still provide the inset |
| 456 | * associated with the status bar being shown.</p> |
| 457 | * |
| 458 | * @return The left stable inset |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 459 | */ |
| 460 | public int getStableInsetLeft() { |
| 461 | return mStableInsets.left; |
| 462 | } |
| 463 | |
| 464 | /** |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 465 | * Returns the right stable inset in pixels. |
| 466 | * |
| 467 | * <p>The stable inset represents the area of a full-screen window that <b>may</b> be |
| 468 | * partially or fully obscured by the system UI elements. This value does not change |
| 469 | * based on the visibility state of those elements; for example, if the status bar is |
| 470 | * normally shown, but temporarily hidden, the stable inset will still provide the inset |
| 471 | * associated with the status bar being shown.</p> |
| 472 | * |
| 473 | * @return The right stable inset |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 474 | */ |
| 475 | public int getStableInsetRight() { |
| 476 | return mStableInsets.right; |
| 477 | } |
| 478 | |
| 479 | /** |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 480 | * Returns the bottom stable inset in pixels. |
| 481 | * |
| 482 | * <p>The stable inset represents the area of a full-screen window that <b>may</b> be |
| 483 | * partially or fully obscured by the system UI elements. This value does not change |
| 484 | * based on the visibility state of those elements; for example, if the status bar is |
| 485 | * normally shown, but temporarily hidden, the stable inset will still provide the inset |
| 486 | * associated with the status bar being shown.</p> |
| 487 | * |
| 488 | * @return The bottom stable inset |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 489 | */ |
| 490 | public int getStableInsetBottom() { |
| 491 | return mStableInsets.bottom; |
| 492 | } |
| 493 | |
| 494 | /** |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 495 | * Returns true if this WindowInsets has nonzero stable insets. |
| 496 | * |
| 497 | * <p>The stable inset represents the area of a full-screen window that <b>may</b> be |
| 498 | * partially or fully obscured by the system UI elements. This value does not change |
| 499 | * based on the visibility state of those elements; for example, if the status bar is |
| 500 | * normally shown, but temporarily hidden, the stable inset will still provide the inset |
| 501 | * associated with the status bar being shown.</p> |
| 502 | * |
| 503 | * @return true if any of the stable inset values are nonzero |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 504 | */ |
| 505 | public boolean hasStableInsets() { |
| 506 | return mStableInsets.top != 0 || mStableInsets.left != 0 || mStableInsets.right != 0 |
| 507 | || mStableInsets.bottom != 0; |
| 508 | } |
| 509 | |
| 510 | /** |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 511 | * Returns a copy of this WindowInsets with the stable insets fully consumed. |
| 512 | * |
| 513 | * @return A modified copy of this WindowInsets |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 514 | */ |
| 515 | public WindowInsets consumeStableInsets() { |
| 516 | final WindowInsets result = new WindowInsets(this); |
| 517 | result.mStableInsets = EMPTY_RECT; |
| 518 | result.mStableInsetsConsumed = true; |
| 519 | return result; |
| 520 | } |
| 521 | |
Jorim Jaggi | 0ffd49c | 2016-02-12 15:04:21 -0800 | [diff] [blame] | 522 | /** |
| 523 | * @hide |
| 524 | */ |
| 525 | public boolean shouldAlwaysConsumeNavBar() { |
| 526 | return mAlwaysConsumeNavBar; |
| 527 | } |
| 528 | |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 529 | @Override |
| 530 | public String toString() { |
Adrian Roos | fa10423 | 2014-06-20 16:10:14 -0700 | [diff] [blame] | 531 | return "WindowInsets{systemWindowInsets=" + mSystemWindowInsets |
| 532 | + " windowDecorInsets=" + mWindowDecorInsets |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 533 | + " stableInsets=" + mStableInsets |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 534 | + (mDisplayCutout != null ? " cutout=" + mDisplayCutout : "") |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 535 | + (isRound() ? " round" : "") |
| 536 | + "}"; |
Adam Powell | 50d7bfd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 537 | } |
| 538 | } |