WindowInsets.java revision fa10423fa00f3495e451016acba9b6848eb995c9
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
18package android.view;
19
20import 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 */
32public final class WindowInsets {
33
34    private Rect mSystemWindowInsets;
35    private Rect mWindowDecorInsets;
36    private Rect mStableInsets;
37    private Rect mTempRect;
38    private boolean mIsRound;
39
40    private boolean mSystemWindowInsetsConsumed = false;
41    private boolean mWindowDecorInsetsConsumed = false;
42    private boolean mStableInsetsConsumed = false;
43
44    private static final Rect EMPTY_RECT = new Rect(0, 0, 0, 0);
45
46    /**
47     * Since new insets may be added in the future that existing apps couldn't
48     * know about, this fully empty constant shouldn't be made available to apps
49     * since it would allow them to inadvertently consume unknown insets by returning it.
50     * @hide
51     */
52    public static final WindowInsets CONSUMED;
53
54    static {
55        CONSUMED = new WindowInsets(null, null, null, false);
56    }
57
58    /** @hide */
59    public WindowInsets(Rect systemWindowInsets, Rect windowDecorInsets, Rect stableInsets,
60            boolean isRound) {
61        mSystemWindowInsetsConsumed = systemWindowInsets == null;
62        mSystemWindowInsets = mSystemWindowInsetsConsumed ? EMPTY_RECT : systemWindowInsets;
63
64        mWindowDecorInsetsConsumed = windowDecorInsets == null;
65        mWindowDecorInsets = mWindowDecorInsetsConsumed ? EMPTY_RECT : windowDecorInsets;
66
67        mStableInsetsConsumed = stableInsets == null;
68        mStableInsets = mStableInsetsConsumed ? EMPTY_RECT : stableInsets;
69
70        mIsRound = isRound;
71    }
72
73    /**
74     * Construct a new WindowInsets, copying all values from a source WindowInsets.
75     *
76     * @param src Source to copy insets from
77     */
78    public WindowInsets(WindowInsets src) {
79        mSystemWindowInsets = src.mSystemWindowInsets;
80        mWindowDecorInsets = src.mWindowDecorInsets;
81        mStableInsets = src.mStableInsets;
82        mSystemWindowInsetsConsumed = src.mSystemWindowInsetsConsumed;
83        mWindowDecorInsetsConsumed = src.mWindowDecorInsetsConsumed;
84        mStableInsetsConsumed = src.mStableInsetsConsumed;
85        mIsRound = src.mIsRound;
86    }
87
88    /** @hide */
89    public WindowInsets(Rect systemWindowInsets) {
90        this(systemWindowInsets, null, null, false);
91    }
92
93    /**
94     * Used to provide a safe copy of the system window insets to pass through
95     * to the existing fitSystemWindows method and other similar internals.
96     * @hide
97     */
98    public Rect getSystemWindowInsets() {
99        if (mTempRect == null) {
100            mTempRect = new Rect();
101        }
102        if (mSystemWindowInsets != null) {
103            mTempRect.set(mSystemWindowInsets);
104        } else {
105            // If there were no system window insets, this is just empty.
106            mTempRect.setEmpty();
107        }
108        return mTempRect;
109    }
110
111    /**
112     * Returns the left system window inset in pixels.
113     *
114     * <p>The system window inset represents the area of a full-screen window that is
115     * partially or fully obscured by the status bar, navigation bar, IME or other system windows.
116     * </p>
117     *
118     * @return The left system window inset
119     */
120    public int getSystemWindowInsetLeft() {
121        return mSystemWindowInsets.left;
122    }
123
124    /**
125     * Returns the top system window inset in pixels.
126     *
127     * <p>The system window inset represents the area of a full-screen window that is
128     * partially or fully obscured by the status bar, navigation bar, IME or other system windows.
129     * </p>
130     *
131     * @return The top system window inset
132     */
133    public int getSystemWindowInsetTop() {
134        return mSystemWindowInsets.top;
135    }
136
137    /**
138     * Returns the right system window inset in pixels.
139     *
140     * <p>The system window inset represents the area of a full-screen window that is
141     * partially or fully obscured by the status bar, navigation bar, IME or other system windows.
142     * </p>
143     *
144     * @return The right system window inset
145     */
146    public int getSystemWindowInsetRight() {
147        return mSystemWindowInsets.right;
148    }
149
150    /**
151     * Returns the bottom system window inset in pixels.
152     *
153     * <p>The system window inset represents the area of a full-screen window that is
154     * partially or fully obscured by the status bar, navigation bar, IME or other system windows.
155     * </p>
156     *
157     * @return The bottom system window inset
158     */
159    public int getSystemWindowInsetBottom() {
160        return mSystemWindowInsets.bottom;
161    }
162
163    /**
164     * Returns the left window decor inset in pixels.
165     *
166     * <p>The window decor inset represents the area of the window content area that is
167     * partially or fully obscured by decorations within the window provided by the framework.
168     * This can include action bars, title bars, toolbars, etc.</p>
169     *
170     * @return The left window decor inset
171     * @hide pending API
172     */
173    public int getWindowDecorInsetLeft() {
174        return mWindowDecorInsets.left;
175    }
176
177    /**
178     * Returns the top window decor inset in pixels.
179     *
180     * <p>The window decor inset represents the area of the window content area that is
181     * partially or fully obscured by decorations within the window provided by the framework.
182     * This can include action bars, title bars, toolbars, etc.</p>
183     *
184     * @return The top window decor inset
185     * @hide pending API
186     */
187    public int getWindowDecorInsetTop() {
188        return mWindowDecorInsets.top;
189    }
190
191    /**
192     * Returns the right window decor inset in pixels.
193     *
194     * <p>The window decor inset represents the area of the window content area that is
195     * partially or fully obscured by decorations within the window provided by the framework.
196     * This can include action bars, title bars, toolbars, etc.</p>
197     *
198     * @return The right window decor inset
199     * @hide pending API
200     */
201    public int getWindowDecorInsetRight() {
202        return mWindowDecorInsets.right;
203    }
204
205    /**
206     * Returns the bottom window decor inset in pixels.
207     *
208     * <p>The window decor inset represents the area of the window content area that is
209     * partially or fully obscured by decorations within the window provided by the framework.
210     * This can include action bars, title bars, toolbars, etc.</p>
211     *
212     * @return The bottom window decor inset
213     * @hide pending API
214     */
215    public int getWindowDecorInsetBottom() {
216        return mWindowDecorInsets.bottom;
217    }
218
219    /**
220     * Returns true if this WindowInsets has nonzero system window insets.
221     *
222     * <p>The system window inset represents the area of a full-screen window that is
223     * partially or fully obscured by the status bar, navigation bar, IME or other system windows.
224     * </p>
225     *
226     * @return true if any of the system window inset values are nonzero
227     */
228    public boolean hasSystemWindowInsets() {
229        return mSystemWindowInsets.left != 0 || mSystemWindowInsets.top != 0 ||
230                mSystemWindowInsets.right != 0 || mSystemWindowInsets.bottom != 0;
231    }
232
233    /**
234     * Returns true if this WindowInsets has nonzero window decor insets.
235     *
236     * <p>The window decor inset represents the area of the window content area that is
237     * partially or fully obscured by decorations within the window provided by the framework.
238     * This can include action bars, title bars, toolbars, etc.</p>
239     *
240     * @return true if any of the window decor inset values are nonzero
241     * @hide pending API
242     */
243    public boolean hasWindowDecorInsets() {
244        return mWindowDecorInsets.left != 0 || mWindowDecorInsets.top != 0 ||
245                mWindowDecorInsets.right != 0 || mWindowDecorInsets.bottom != 0;
246    }
247
248    /**
249     * Returns true if this WindowInsets has any nonzero insets.
250     *
251     * @return true if any inset values are nonzero
252     */
253    public boolean hasInsets() {
254        return hasSystemWindowInsets() || hasWindowDecorInsets();
255    }
256
257    /**
258     * Check if these insets have been fully consumed.
259     *
260     * <p>Insets are considered "consumed" if the applicable <code>consume*</code> methods
261     * have been called such that all insets have been set to zero. This affects propagation of
262     * insets through the view hierarchy; insets that have not been fully consumed will continue
263     * to propagate down to child views.</p>
264     *
265     * <p>The result of this method is equivalent to the return value of
266     * {@link View#fitSystemWindows(android.graphics.Rect)}.</p>
267     *
268     * @return true if the insets have been fully consumed.
269     * @hide Pending API
270     */
271    public boolean isConsumed() {
272        return mSystemWindowInsetsConsumed && mWindowDecorInsetsConsumed && mStableInsetsConsumed;
273    }
274
275    /**
276     * Returns true if the associated window has a round shape.
277     *
278     * <p>A round window's left, top, right and bottom edges reach all the way to the
279     * associated edges of the window but the corners may not be visible. Views responding
280     * to round insets should take care to not lay out critical elements within the corners
281     * where they may not be accessible.</p>
282     *
283     * @return True if the window is round
284     */
285    public boolean isRound() {
286        return mIsRound;
287    }
288
289    /**
290     * Returns a copy of this WindowInsets with the system window insets fully consumed.
291     *
292     * @return A modified copy of this WindowInsets
293     */
294    public WindowInsets consumeSystemWindowInsets() {
295        final WindowInsets result = new WindowInsets(this);
296        result.mSystemWindowInsets = EMPTY_RECT;
297        result.mSystemWindowInsetsConsumed = true;
298        return result;
299    }
300
301    /**
302     * Returns a copy of this WindowInsets with selected system window insets fully consumed.
303     *
304     * @param left true to consume the left system window inset
305     * @param top true to consume the top system window inset
306     * @param right true to consume the right system window inset
307     * @param bottom true to consume the bottom system window inset
308     * @return A modified copy of this WindowInsets
309     * @hide pending API
310     */
311    public WindowInsets consumeSystemWindowInsets(boolean left, boolean top,
312            boolean right, boolean bottom) {
313        if (left || top || right || bottom) {
314            final WindowInsets result = new WindowInsets(this);
315            result.mSystemWindowInsets = new Rect(
316                    left ? 0 : mSystemWindowInsets.left,
317                    top ? 0 : mSystemWindowInsets.top,
318                    right ? 0 : mSystemWindowInsets.right,
319                    bottom ? 0 : mSystemWindowInsets.bottom);
320            result.mSystemWindowInsetsConsumed = !hasSystemWindowInsets();
321            return result;
322        }
323        return this;
324    }
325
326    /**
327     * Returns a copy of this WindowInsets with selected system window insets replaced
328     * with new values.
329     *
330     * @param left New left inset in pixels
331     * @param top New top inset in pixels
332     * @param right New right inset in pixels
333     * @param bottom New bottom inset in pixels
334     * @return A modified copy of this WindowInsets
335     */
336    public WindowInsets replaceSystemWindowInsets(int left, int top,
337            int right, int bottom) {
338        final WindowInsets result = new WindowInsets(this);
339        result.mSystemWindowInsets = new Rect(left, top, right, bottom);
340        result.mSystemWindowInsetsConsumed = !hasSystemWindowInsets();
341        return result;
342    }
343
344    /**
345     * @hide
346     */
347    public WindowInsets consumeWindowDecorInsets() {
348        final WindowInsets result = new WindowInsets(this);
349        result.mWindowDecorInsets.set(0, 0, 0, 0);
350        result.mWindowDecorInsetsConsumed = true;
351        return result;
352    }
353
354    /**
355     * @hide
356     */
357    public WindowInsets consumeWindowDecorInsets(boolean left, boolean top,
358            boolean right, boolean bottom) {
359        if (left || top || right || bottom) {
360            final WindowInsets result = new WindowInsets(this);
361            result.mWindowDecorInsets = new Rect(left ? 0 : mWindowDecorInsets.left,
362                    top ? 0 : mWindowDecorInsets.top,
363                    right ? 0 : mWindowDecorInsets.right,
364                    bottom ? 0 : mWindowDecorInsets.bottom);
365            result.mWindowDecorInsetsConsumed = !hasWindowDecorInsets();
366            return result;
367        }
368        return this;
369    }
370
371    /**
372     * @hide
373     */
374    public WindowInsets replaceWindowDecorInsets(int left, int top, int right, int bottom) {
375        final WindowInsets result = new WindowInsets(this);
376        result.mWindowDecorInsets = new Rect(left, top, right, bottom);
377        result.mWindowDecorInsetsConsumed = !hasWindowDecorInsets();
378        return result;
379    }
380
381    /**
382     * @hide
383     */
384    public int getStableInsetTop() {
385        return mStableInsets.top;
386    }
387
388    /**
389     * @hide
390     */
391    public int getStableInsetLeft() {
392        return mStableInsets.left;
393    }
394
395    /**
396     * @hide
397     */
398    public int getStableInsetRight() {
399        return mStableInsets.right;
400    }
401
402    /**
403     * @hide
404     */
405    public int getStableInsetBottom() {
406        return mStableInsets.bottom;
407    }
408
409    /**
410     * @hide
411     */
412    public boolean hasStableInsets() {
413        return mStableInsets.top != 0 || mStableInsets.left != 0 || mStableInsets.right != 0
414                || mStableInsets.bottom != 0;
415    }
416
417    /**
418     * @hide
419     */
420    public WindowInsets consumeStableInsets() {
421        final WindowInsets result = new WindowInsets(this);
422        result.mStableInsets = EMPTY_RECT;
423        result.mStableInsetsConsumed = true;
424        return result;
425    }
426
427    @Override
428    public String toString() {
429        return "WindowInsets{systemWindowInsets=" + mSystemWindowInsets
430                + " windowDecorInsets=" + mWindowDecorInsets
431                + " stableInsets=" + mStableInsets +
432                (isRound() ? " round}" : "}");
433    }
434}
435