1 package ${packageName}.util;
3 import android.app.Activity;
4 import android.os.Build;
5 import android.view.View;
8 * A utility class that helps with showing and hiding system UI such as the
9 * status bar and navigation/system bar. This class uses backward-compatibility
10 * techniques described in <a href=
11 * "http://developer.android.com/training/backward-compatible-ui/index.html">
12 * Creating Backward-Compatible UIs</a> to ensure that devices running any
13 * version of ndroid OS are supported. More specifically, there are separate
14 * implementations of this abstract class: for newer devices,
15 * {@link #getInstance} will return a {@link SystemUiHiderHoneycomb} instance,
16 * while on older devices {@link #getInstance} will return a
17 * {@link SystemUiHiderBase} instance.
19 * For more on system bars, see <a href=
20 * "http://developer.android.com/design/get-started/ui-overview.html#system-bars"
23 * @see android.view.View#setSystemUiVisibility(int)
24 * @see android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
26 public abstract class SystemUiHider {
28 * When this flag is set, the
29 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN}
30 * flag will be set on older devices, making the status bar "float" on top
31 * of the activity layout. This is most useful when there are no controls at
32 * the top of the activity layout.
34 * This flag isn't used on newer devices because the <a
35 * href="http://developer.android.com/design/patterns/actionbar.html">action
36 * bar</a>, the most important structural element of an Android app, should
37 * be visible and not obscured by the system UI.
39 public static final int FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES = 0x1;
42 * When this flag is set, {@link #show()} and {@link #hide()} will toggle
43 * the visibility of the status bar. If there is a navigation bar, show and
44 * hide will toggle low profile mode.
46 public static final int FLAG_FULLSCREEN = 0x2;
49 * When this flag is set, {@link #show()} and {@link #hide()} will toggle
50 * the visibility of the navigation bar, if it's present on the device and
51 * the device allows hiding it. In cases where the navigation bar is present
52 * but cannot be hidden, show and hide will toggle low profile mode.
54 public static final int FLAG_HIDE_NAVIGATION = FLAG_FULLSCREEN | 0x4;
57 * The activity associated with this UI hider object.
59 protected Activity mActivity;
62 * The view on which {@link View#setSystemUiVisibility(int)} will be called.
64 protected View mAnchorView;
67 * The current UI hider flags.
69 * @see #FLAG_FULLSCREEN
70 * @see #FLAG_HIDE_NAVIGATION
71 * @see #FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES
76 * The current visibility callback.
78 protected OnVisibilityChangeListener mOnVisibilityChangeListener = sDummyListener;
81 * Creates and returns an instance of {@link SystemUiHider} that is
82 * appropriate for this device. The object will be either a
83 * {@link SystemUiHiderBase} or {@link SystemUiHiderHoneycomb} depending on
86 * @param activity The activity whose window's system UI should be
87 * controlled by this class.
88 * @param anchorView The view on which
89 * {@link View#setSystemUiVisibility(int)} will be called.
90 * @param flags Either 0 or any combination of {@link #FLAG_FULLSCREEN},
91 * {@link #FLAG_HIDE_NAVIGATION}, and
92 * {@link #FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES}.
94 public static SystemUiHider getInstance(Activity activity, View anchorView, int flags) {
95 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
96 return new SystemUiHiderHoneycomb(activity, anchorView, flags);
98 return new SystemUiHiderBase(activity, anchorView, flags);
102 protected SystemUiHider(Activity activity, View anchorView, int flags) {
103 mActivity = activity;
104 mAnchorView = anchorView;
109 * Sets up the system UI hider. Should be called from
110 * {@link Activity#onCreate}.
112 public abstract void setup();
115 * Returns whether or not the system UI is visible.
117 public abstract boolean isVisible();
120 * Hide the system UI.
122 public abstract void hide();
125 * Show the system UI.
127 public abstract void show();
130 * Toggle the visibility of the system UI.
132 public void toggle() {
141 * Registers a callback, to be triggered when the system UI visibility
144 public void setOnVisibilityChangeListener(OnVisibilityChangeListener listener) {
145 if (listener == null) {
146 listener = sDummyListener;
149 mOnVisibilityChangeListener = listener;
153 * A dummy no-op callback for use when there is no other listener set.
155 private static OnVisibilityChangeListener sDummyListener = new OnVisibilityChangeListener() {
157 public void onVisibilityChange(boolean visible) {
162 * A callback interface used to listen for system UI visibility changes.
164 public interface OnVisibilityChangeListener {
166 * Called when the system UI visibility has changed.
168 * @param visible True if the system UI is visible.
170 public void onVisibilityChange(boolean visible);