Adam Powell | 46e38fd | 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 | |
| 20 | import android.graphics.Rect; |
| 21 | |
| 22 | /** |
| 23 | * Describes a set of insets for window content. |
| 24 | * |
| 25 | * <p>WindowInsets are immutable and may be expanded to include more inset types in the future. |
| 26 | * To adjust insets, use one of the supplied clone methods to obtain a new WindowInsets instance |
| 27 | * with the adjusted properties.</p> |
| 28 | * |
| 29 | * @see View.OnApplyWindowInsetsListener |
| 30 | * @see View#onApplyWindowInsets(WindowInsets) |
| 31 | */ |
| 32 | public class WindowInsets { |
| 33 | private Rect mSystemWindowInsets; |
| 34 | private Rect mWindowDecorInsets; |
| 35 | private Rect mTempRect; |
| 36 | |
| 37 | private static final Rect EMPTY_RECT = new Rect(0, 0, 0, 0); |
| 38 | |
| 39 | /** |
| 40 | * Since new insets may be added in the future that existing apps couldn't |
| 41 | * know about, this fully empty constant shouldn't be made available to apps |
| 42 | * since it would allow them to inadvertently consume unknown insets by returning it. |
| 43 | * @hide |
| 44 | */ |
| 45 | public static final WindowInsets EMPTY = new WindowInsets(EMPTY_RECT, EMPTY_RECT); |
| 46 | |
| 47 | /** @hide */ |
| 48 | public WindowInsets(Rect systemWindowInsets, Rect windowDecorInsets) { |
| 49 | mSystemWindowInsets = systemWindowInsets; |
| 50 | mWindowDecorInsets = windowDecorInsets; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Construct a new WindowInsets, copying all values from a source WindowInsets. |
| 55 | * |
| 56 | * @param src Source to copy insets from |
| 57 | */ |
| 58 | public WindowInsets(WindowInsets src) { |
| 59 | mSystemWindowInsets = src.mSystemWindowInsets; |
| 60 | mWindowDecorInsets = src.mWindowDecorInsets; |
| 61 | } |
| 62 | |
| 63 | /** @hide */ |
| 64 | public WindowInsets(Rect systemWindowInsets) { |
| 65 | mSystemWindowInsets = systemWindowInsets; |
| 66 | mWindowDecorInsets = EMPTY_RECT; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Used to provide a safe copy of the system window insets to pass through |
| 71 | * to the existing fitSystemWindows method and other similar internals. |
| 72 | * @hide |
| 73 | */ |
| 74 | public Rect getSystemWindowInsets() { |
| 75 | if (mTempRect == null) { |
| 76 | mTempRect = new Rect(); |
| 77 | } |
| 78 | mTempRect.set(mSystemWindowInsets); |
| 79 | return mTempRect; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the left system window inset in pixels. |
| 84 | * |
| 85 | * <p>The system window inset represents the area of a full-screen window that is |
| 86 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 87 | * </p> |
| 88 | * |
| 89 | * @return The left system window inset |
| 90 | */ |
| 91 | public int getSystemWindowInsetLeft() { |
| 92 | return mSystemWindowInsets.left; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the top system window inset in pixels. |
| 97 | * |
| 98 | * <p>The system window inset represents the area of a full-screen window that is |
| 99 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 100 | * </p> |
| 101 | * |
| 102 | * @return The top system window inset |
| 103 | */ |
| 104 | public int getSystemWindowInsetTop() { |
| 105 | return mSystemWindowInsets.top; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the right system window inset in pixels. |
| 110 | * |
| 111 | * <p>The system window inset represents the area of a full-screen window that is |
| 112 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 113 | * </p> |
| 114 | * |
| 115 | * @return The right system window inset |
| 116 | */ |
| 117 | public int getSystemWindowInsetRight() { |
| 118 | return mSystemWindowInsets.right; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the bottom system window inset in pixels. |
| 123 | * |
| 124 | * <p>The system window inset represents the area of a full-screen window that is |
| 125 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 126 | * </p> |
| 127 | * |
| 128 | * @return The bottom system window inset |
| 129 | */ |
| 130 | public int getSystemWindowInsetBottom() { |
| 131 | return mSystemWindowInsets.bottom; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the left window decor inset in pixels. |
| 136 | * |
| 137 | * <p>The window decor inset represents the area of the window content area that is |
| 138 | * partially or fully obscured by decorations within the window provided by the framework. |
| 139 | * This can include action bars, title bars, toolbars, etc.</p> |
| 140 | * |
| 141 | * @return The left window decor inset |
| 142 | */ |
| 143 | public int getWindowDecorInsetLeft() { |
| 144 | return mWindowDecorInsets.left; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the top window decor inset in pixels. |
| 149 | * |
| 150 | * <p>The window decor inset represents the area of the window content area that is |
| 151 | * partially or fully obscured by decorations within the window provided by the framework. |
| 152 | * This can include action bars, title bars, toolbars, etc.</p> |
| 153 | * |
| 154 | * @return The top window decor inset |
| 155 | */ |
| 156 | public int getWindowDecorInsetTop() { |
| 157 | return mWindowDecorInsets.top; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the right window decor inset in pixels. |
| 162 | * |
| 163 | * <p>The window decor inset represents the area of the window content area that is |
| 164 | * partially or fully obscured by decorations within the window provided by the framework. |
| 165 | * This can include action bars, title bars, toolbars, etc.</p> |
| 166 | * |
| 167 | * @return The right window decor inset |
| 168 | */ |
| 169 | public int getWindowDecorInsetRight() { |
| 170 | return mWindowDecorInsets.right; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns the bottom window decor inset in pixels. |
| 175 | * |
| 176 | * <p>The window decor inset represents the area of the window content area that is |
| 177 | * partially or fully obscured by decorations within the window provided by the framework. |
| 178 | * This can include action bars, title bars, toolbars, etc.</p> |
| 179 | * |
| 180 | * @return The bottom window decor inset |
| 181 | */ |
| 182 | public int getWindowDecorInsetBottom() { |
| 183 | return mWindowDecorInsets.bottom; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns true if this WindowInsets has nonzero system window insets. |
| 188 | * |
| 189 | * <p>The system window inset represents the area of a full-screen window that is |
| 190 | * partially or fully obscured by the status bar, navigation bar, IME or other system windows. |
| 191 | * </p> |
| 192 | * |
| 193 | * @return true if any of the system window inset values are nonzero |
| 194 | */ |
| 195 | public boolean hasSystemWindowInsets() { |
| 196 | return mSystemWindowInsets.left != 0 || mSystemWindowInsets.top != 0 || |
| 197 | mSystemWindowInsets.right != 0 || mSystemWindowInsets.bottom != 0; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns true if this WindowInsets has nonzero window decor insets. |
| 202 | * |
| 203 | * <p>The window decor inset represents the area of the window content area that is |
| 204 | * partially or fully obscured by decorations within the window provided by the framework. |
| 205 | * This can include action bars, title bars, toolbars, etc.</p> |
| 206 | * |
| 207 | * @return true if any of the window decor inset values are nonzero |
| 208 | */ |
| 209 | public boolean hasWindowDecorInsets() { |
| 210 | return mWindowDecorInsets.left != 0 || mWindowDecorInsets.top != 0 || |
| 211 | mWindowDecorInsets.right != 0 || mWindowDecorInsets.bottom != 0; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns true if this WindowInsets has any nonzero insets. |
| 216 | * |
| 217 | * @return true if any inset values are nonzero |
| 218 | */ |
| 219 | public boolean hasInsets() { |
| 220 | return hasSystemWindowInsets() || hasWindowDecorInsets(); |
| 221 | } |
| 222 | |
| 223 | public WindowInsets cloneWithSystemWindowInsetsConsumed() { |
| 224 | final WindowInsets result = new WindowInsets(this); |
| 225 | result.mSystemWindowInsets = new Rect(0, 0, 0, 0); |
| 226 | return result; |
| 227 | } |
| 228 | |
| 229 | public WindowInsets cloneWithSystemWindowInsetsConsumed(boolean left, boolean top, |
| 230 | boolean right, boolean bottom) { |
| 231 | if (left || top || right || bottom) { |
| 232 | final WindowInsets result = new WindowInsets(this); |
| 233 | result.mSystemWindowInsets = new Rect(left ? 0 : mSystemWindowInsets.left, |
| 234 | top ? 0 : mSystemWindowInsets.top, |
| 235 | right ? 0 : mSystemWindowInsets.right, |
| 236 | bottom ? 0 : mSystemWindowInsets.bottom); |
| 237 | return result; |
| 238 | } |
| 239 | return this; |
| 240 | } |
| 241 | |
| 242 | public WindowInsets cloneWithSystemWindowInsets(int left, int top, int right, int bottom) { |
| 243 | final WindowInsets result = new WindowInsets(this); |
| 244 | result.mSystemWindowInsets = new Rect(left, top, right, bottom); |
| 245 | return result; |
| 246 | } |
| 247 | |
| 248 | public WindowInsets cloneWithWindowDecorInsetsConsumed() { |
| 249 | final WindowInsets result = new WindowInsets(this); |
| 250 | result.mWindowDecorInsets.set(0, 0, 0, 0); |
| 251 | return result; |
| 252 | } |
| 253 | |
| 254 | public WindowInsets cloneWithWindowDecorInsetsConsumed(boolean left, boolean top, |
| 255 | boolean right, boolean bottom) { |
| 256 | if (left || top || right || bottom) { |
| 257 | final WindowInsets result = new WindowInsets(this); |
| 258 | result.mWindowDecorInsets = new Rect(left ? 0 : mWindowDecorInsets.left, |
| 259 | top ? 0 : mWindowDecorInsets.top, |
| 260 | right ? 0 : mWindowDecorInsets.right, |
| 261 | bottom ? 0 : mWindowDecorInsets.bottom); |
| 262 | return result; |
| 263 | } |
| 264 | return this; |
| 265 | } |
| 266 | |
| 267 | public WindowInsets cloneWithWindowDecorInsets(int left, int top, int right, int bottom) { |
| 268 | final WindowInsets result = new WindowInsets(this); |
| 269 | result.mWindowDecorInsets = new Rect(left, top, right, bottom); |
| 270 | return result; |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public String toString() { |
| 275 | return "WindowInsets{systemWindowInsets=" + mSystemWindowInsets + " windowDecorInsets=" + |
| 276 | mWindowDecorInsets + "}"; |
| 277 | } |
| 278 | } |