1 /*
2  * Copyright (C) 2018 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 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.graphics.Insets;
23 import android.inputmethodservice.InputMethodService;
24 import android.os.Build;
25 import android.os.CancellationSignal;
26 import android.view.WindowInsets.Type;
27 import android.view.WindowInsets.Type.InsetsType;
28 import android.view.animation.Interpolator;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 
33 /**
34  * Interface to control windows that generate insets.
35  */
36 public interface WindowInsetsController {
37 
38     /**
39      * Makes status bars become opaque with solid dark background and light foreground.
40      * @hide
41      */
42     int APPEARANCE_OPAQUE_STATUS_BARS = 1;
43 
44     /**
45      * Makes navigation bars become opaque with solid dark background and light foreground.
46      * @hide
47      */
48     int APPEARANCE_OPAQUE_NAVIGATION_BARS = 1 << 1;
49 
50     /**
51      * Makes items on system bars become less noticeable without changing the layout of the bars.
52      * @hide
53      */
54     int APPEARANCE_LOW_PROFILE_BARS = 1 << 2;
55 
56     /**
57      * Changes the foreground color for light status bars so that the items on the bar can be read
58      * clearly.
59      */
60     int APPEARANCE_LIGHT_STATUS_BARS = 1 << 3;
61 
62     /**
63      * Changes the foreground color for light navigation bars so that the items on the bar can be
64      * read clearly.
65      */
66     int APPEARANCE_LIGHT_NAVIGATION_BARS = 1 << 4;
67 
68     /**
69      * Makes status bars semi-transparent with dark background and light foreground.
70      * @hide
71      */
72     int APPEARANCE_SEMI_TRANSPARENT_STATUS_BARS = 1 << 5;
73 
74     /**
75      * Makes navigation bars semi-transparent with dark background and light foreground.
76      * @hide
77      */
78     int APPEARANCE_SEMI_TRANSPARENT_NAVIGATION_BARS = 1 << 6;
79 
80     /**
81      * Determines the appearance of system bars.
82      * @hide
83      */
84     @Retention(RetentionPolicy.SOURCE)
85     @IntDef(flag = true, value = {APPEARANCE_OPAQUE_STATUS_BARS, APPEARANCE_OPAQUE_NAVIGATION_BARS,
86             APPEARANCE_LOW_PROFILE_BARS, APPEARANCE_LIGHT_STATUS_BARS,
87             APPEARANCE_LIGHT_NAVIGATION_BARS, APPEARANCE_SEMI_TRANSPARENT_STATUS_BARS,
88             APPEARANCE_SEMI_TRANSPARENT_NAVIGATION_BARS})
89     @interface Appearance {
90     }
91 
92     /**
93      * Option for {@link #setSystemBarsBehavior(int)}. System bars will be forcibly shown on any
94      * user interaction on the corresponding display if navigation bars are hidden by
95      * {@link #hide(int)} or
96      * {@link WindowInsetsAnimationController#setInsetsAndAlpha(Insets, float, float)}.
97      * @deprecated This is not supported on Android {@link Build.VERSION_CODES#S} and later. Use
98      *             {@link #BEHAVIOR_DEFAULT} or {@link #BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE}
99      *             instead.
100      */
101     @Deprecated
102     int BEHAVIOR_SHOW_BARS_BY_TOUCH = 0;
103 
104     /**
105      * The default option for {@link #setSystemBarsBehavior(int)}: Window would like to remain
106      * interactive when hiding navigation bars by calling {@link #hide(int)} or
107      * {@link WindowInsetsAnimationController#setInsetsAndAlpha(Insets, float, float)}.
108      *
109      * <p>When system bars are hidden in this mode, they can be revealed with system gestures, such
110      * as swiping from the edge of the screen where the bar is hidden from.</p>
111      *
112      * <p>When the gesture navigation is enabled, the system gestures can be triggered regardless
113      * the visibility of system bars.</p>
114      */
115     int BEHAVIOR_DEFAULT = 1;
116 
117     /**
118      * Option for {@link #setSystemBarsBehavior(int)}: Window would like to remain interactive when
119      * hiding navigation bars by calling {@link #hide(int)} or
120      * {@link WindowInsetsAnimationController#setInsetsAndAlpha(Insets, float, float)}.
121      *
122      * <p>When system bars are hidden in this mode, they can be revealed with system gestures, such
123      * as swiping from the edge of the screen where the bar is hidden from.</p>
124      * @deprecated Use {@link #BEHAVIOR_DEFAULT} instead.
125      */
126     @Deprecated
127     int BEHAVIOR_SHOW_BARS_BY_SWIPE = BEHAVIOR_DEFAULT;
128 
129     /**
130      * Option for {@link #setSystemBarsBehavior(int)}: Window would like to remain interactive when
131      * hiding navigation bars by calling {@link #hide(int)} or
132      * {@link WindowInsetsAnimationController#setInsetsAndAlpha(Insets, float, float)}.
133      *
134      * <p>When system bars are hidden in this mode, they can be revealed temporarily with system
135      * gestures, such as swiping from the edge of the screen where the bar is hidden from. These
136      * transient system bars will overlay app’s content, may have some degree of transparency, and
137      * will automatically hide after a short timeout.</p>
138      */
139     int BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE = 2;
140 
141     /**
142      * Determines the behavior of system bars when hiding them by calling {@link #hide}.
143      * @hide
144      */
145     @Retention(RetentionPolicy.SOURCE)
146     @IntDef(value = {BEHAVIOR_DEFAULT, BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE})
147     @interface Behavior {
148     }
149 
150     /**
151      * Makes a set of windows that cause insets appear on screen.
152      * <p>
153      * Note that if the window currently doesn't have control over a certain type, it will apply the
154      * change as soon as the window gains control. The app can listen to the event by observing
155      * {@link View#onApplyWindowInsets} and checking visibility with {@link WindowInsets#isVisible}.
156      *
157      * @param types A bitmask of {@link WindowInsets.Type} specifying what windows the app
158      *              would like to make appear on screen.
159      */
show(@nsetsType int types)160     void show(@InsetsType int types);
161 
162     /**
163      * Makes a set of windows causing insets disappear.
164      * <p>
165      * Note that if the window currently doesn't have control over a certain type, it will apply the
166      * change as soon as the window gains control. The app can listen to the event by observing
167      * {@link View#onApplyWindowInsets} and checking visibility with {@link WindowInsets#isVisible}.
168      *
169      * @param types A bitmask of {@link WindowInsets.Type} specifying what windows the app
170      *              would like to make disappear.
171      */
hide(@nsetsType int types)172     void hide(@InsetsType int types);
173 
174     /**
175      * Lets the application control window inset animations in a frame-by-frame manner by modifying
176      * the position of the windows in the system causing insets directly.
177      *
178      * @param types The {@link WindowInsets.Type}s the application has requested to control.
179      * @param durationMillis Duration of animation in
180      *                       {@link java.util.concurrent.TimeUnit#MILLISECONDS}, or -1 if the
181      *                       animation doesn't have a predetermined duration. This value will be
182      *                       passed to {@link WindowInsetsAnimation#getDurationMillis()}
183      * @param interpolator The interpolator used for this animation, or {@code null} if this
184      *                     animation doesn't follow an interpolation curve. This value will be
185      *                     passed to {@link WindowInsetsAnimation#getInterpolator()} and used to
186      *                     calculate {@link WindowInsetsAnimation#getInterpolatedFraction()}.
187      * @param listener The {@link WindowInsetsAnimationControlListener} that gets called when the
188      *                 windows are ready to be controlled, among other callbacks.
189      * @param cancellationSignal A cancellation signal that the caller can use to cancel the
190      *                           request to obtain control, or once they have control, to cancel the
191      *                           control.
192      * @see WindowInsetsAnimation#getFraction()
193      * @see WindowInsetsAnimation#getInterpolatedFraction()
194      * @see WindowInsetsAnimation#getInterpolator()
195      * @see WindowInsetsAnimation#getDurationMillis()
196      */
controlWindowInsetsAnimation(@nsetsType int types, long durationMillis, @Nullable Interpolator interpolator, @Nullable CancellationSignal cancellationSignal, @NonNull WindowInsetsAnimationControlListener listener)197     void controlWindowInsetsAnimation(@InsetsType int types, long durationMillis,
198             @Nullable Interpolator interpolator,
199             @Nullable CancellationSignal cancellationSignal,
200             @NonNull WindowInsetsAnimationControlListener listener);
201 
202     /**
203      * Lets the application add non-controllable listener object that can be called back
204      * when animation is invoked by the system by host calling methods such as {@link #show} or
205      * {@link #hide}.
206      *
207      * The listener is supposed to be used for logging only, using the control or
208      * relying on the timing of the callback in any other way is not supported.
209      *
210      * @param listener The {@link WindowInsetsAnimationControlListener} that gets called when
211      *                 the animation is driven by the system and not the host
212      * @hide
213      */
setSystemDrivenInsetsAnimationLoggingListener( @ullable WindowInsetsAnimationControlListener listener)214     void setSystemDrivenInsetsAnimationLoggingListener(
215             @Nullable WindowInsetsAnimationControlListener listener);
216 
217     /**
218      * Controls the appearance of system bars.
219      * <p>
220      * For example, the following statement adds {@link #APPEARANCE_LIGHT_STATUS_BARS}:
221      * <pre>
222      * setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS)
223      * </pre>
224      * And the following statement clears it:
225      * <pre>
226      * setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS)
227      * </pre>
228      *
229      * @param appearance Bitmask of appearance flags.
230      * @param mask Specifies which flags of appearance should be changed.
231      * @see #getSystemBarsAppearance
232      */
setSystemBarsAppearance(@ppearance int appearance, @Appearance int mask)233     void setSystemBarsAppearance(@Appearance int appearance, @Appearance int mask);
234 
235     /**
236      * Retrieves the requested appearance of system bars.
237      *
238      * @return The requested bitmask of system bar appearance controlled by this window.
239      * @see #setSystemBarsAppearance(int, int)
240      */
getSystemBarsAppearance()241     @Appearance int getSystemBarsAppearance();
242 
243     /**
244      * Notify the caption insets height change. The information will be used on the client side to,
245      * make sure the InsetsState has the correct caption insets.
246      *
247      * @param height the height of caption bar insets.
248      * @hide
249      */
setCaptionInsetsHeight(int height)250     void setCaptionInsetsHeight(int height);
251 
252     /**
253      * Sets the insets height for the IME caption bar, which corresponds to the
254      * "fake" IME navigation bar.
255      *
256      * @param height the insets height of the IME caption bar.
257      * @hide
258      */
setImeCaptionBarInsetsHeight(int height)259     default void setImeCaptionBarInsetsHeight(int height) {
260     }
261 
262     /**
263      * Controls the behavior of system bars.
264      *
265      * @param behavior Determines how the bars behave when being hidden by the application.
266      * @see #getSystemBarsBehavior
267      */
setSystemBarsBehavior(@ehavior int behavior)268     void setSystemBarsBehavior(@Behavior int behavior);
269 
270     /**
271      * Retrieves the requested behavior of system bars.
272      *
273      * @return the system bar behavior controlled by this window.
274      * @see #setSystemBarsBehavior(int)
275      */
getSystemBarsBehavior()276     @Behavior int getSystemBarsBehavior();
277 
278     /**
279      * Disables or enables the animations.
280      *
281      * @hide
282      */
setAnimationsDisabled(boolean disable)283     void setAnimationsDisabled(boolean disable);
284 
285     /**
286      * @hide
287      */
getState()288     InsetsState getState();
289 
290     /**
291      * @return Insets types that have been requested to be visible.
292      * @hide
293      */
getRequestedVisibleTypes()294     @InsetsType int getRequestedVisibleTypes();
295 
296     /**
297      * Adds a {@link OnControllableInsetsChangedListener} to the window insets controller.
298      *
299      * @param listener The listener to add.
300      *
301      * @see OnControllableInsetsChangedListener
302      * @see #removeOnControllableInsetsChangedListener(OnControllableInsetsChangedListener)
303      */
addOnControllableInsetsChangedListener( @onNull OnControllableInsetsChangedListener listener)304     void addOnControllableInsetsChangedListener(
305             @NonNull OnControllableInsetsChangedListener listener);
306 
307     /**
308      * Removes a {@link OnControllableInsetsChangedListener} from the window insets controller.
309      *
310      * @param listener The listener to remove.
311      *
312      * @see OnControllableInsetsChangedListener
313      * @see #addOnControllableInsetsChangedListener(OnControllableInsetsChangedListener)
314      */
removeOnControllableInsetsChangedListener( @onNull OnControllableInsetsChangedListener listener)315     void removeOnControllableInsetsChangedListener(
316             @NonNull OnControllableInsetsChangedListener listener);
317 
318     /**
319      * Listener to be notified when the set of controllable {@link WindowInsets.Type} controlled by
320      * a {@link WindowInsetsController} changes.
321      * <p>
322      * Once a {@link WindowInsets.Type} becomes controllable, the app will be able to control the
323      * window that is causing this type of insets by calling {@link #controlWindowInsetsAnimation}.
324      * <p>
325      * Note: When listening to controllability of the {@link Type#ime},
326      * {@link #controlWindowInsetsAnimation} may still fail in case the {@link InputMethodService}
327      * decides to cancel the show request. This could happen when there is a hardware keyboard
328      * attached.
329      *
330      * @see #addOnControllableInsetsChangedListener(OnControllableInsetsChangedListener)
331      * @see #removeOnControllableInsetsChangedListener(OnControllableInsetsChangedListener)
332      */
333     interface OnControllableInsetsChangedListener {
334 
335         /**
336          * Called when the set of controllable {@link WindowInsets.Type} changes.
337          *
338          * @param controller The controller for which the set of controllable
339          *                   {@link WindowInsets.Type}s are changing.
340          * @param typeMask Bitwise type-mask of the {@link WindowInsets.Type}s the controller is
341          *                 currently able to control.
342          */
onControllableInsetsChanged(@onNull WindowInsetsController controller, @InsetsType int typeMask)343         void onControllableInsetsChanged(@NonNull WindowInsetsController controller,
344                 @InsetsType int typeMask);
345     }
346 }
347