1package ${packageName}.util; 2 3import android.app.Activity; 4import android.os.Build; 5import android.view.View; 6 7/** 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. 18 * <p> 19 * For more on system bars, see <a href= 20 * "http://developer.android.com/design/get-started/ui-overview.html#system-bars" 21 * > System Bars</a>. 22 * 23 * @see android.view.View#setSystemUiVisibility(int) 24 * @see android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN 25 */ 26public abstract class SystemUiHider { 27 /** 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. 33 * <p> 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. 38 */ 39 public static final int FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES = 0x1; 40 41 /** 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. 45 */ 46 public static final int FLAG_FULLSCREEN = 0x2; 47 48 /** 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. 53 */ 54 public static final int FLAG_HIDE_NAVIGATION = FLAG_FULLSCREEN | 0x4; 55 56 /** 57 * The activity associated with this UI hider object. 58 */ 59 protected Activity mActivity; 60 61 /** 62 * The view on which {@link View#setSystemUiVisibility(int)} will be called. 63 */ 64 protected View mAnchorView; 65 66 /** 67 * The current UI hider flags. 68 * 69 * @see #FLAG_FULLSCREEN 70 * @see #FLAG_HIDE_NAVIGATION 71 * @see #FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES 72 */ 73 protected int mFlags; 74 75 /** 76 * The current visibility callback. 77 */ 78 protected OnVisibilityChangeListener mOnVisibilityChangeListener = sDummyListener; 79 80 /** 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 84 * the device. 85 * 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}. 93 */ 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); 97 } else { 98 return new SystemUiHiderBase(activity, anchorView, flags); 99 } 100 } 101 102 protected SystemUiHider(Activity activity, View anchorView, int flags) { 103 mActivity = activity; 104 mAnchorView = anchorView; 105 mFlags = flags; 106 } 107 108 /** 109 * Sets up the system UI hider. Should be called from 110 * {@link Activity#onCreate}. 111 */ 112 public abstract void setup(); 113 114 /** 115 * Returns whether or not the system UI is visible. 116 */ 117 public abstract boolean isVisible(); 118 119 /** 120 * Hide the system UI. 121 */ 122 public abstract void hide(); 123 124 /** 125 * Show the system UI. 126 */ 127 public abstract void show(); 128 129 /** 130 * Toggle the visibility of the system UI. 131 */ 132 public void toggle() { 133 if (isVisible()) { 134 hide(); 135 } else { 136 show(); 137 } 138 } 139 140 /** 141 * Registers a callback, to be triggered when the system UI visibility 142 * changes. 143 */ 144 public void setOnVisibilityChangeListener(OnVisibilityChangeListener listener) { 145 if (listener == null) { 146 listener = sDummyListener; 147 } 148 149 mOnVisibilityChangeListener = listener; 150 } 151 152 /** 153 * A dummy no-op callback for use when there is no other listener set. 154 */ 155 private static OnVisibilityChangeListener sDummyListener = new OnVisibilityChangeListener() { 156 @Override 157 public void onVisibilityChange(boolean visible) { 158 } 159 }; 160 161 /** 162 * A callback interface used to listen for system UI visibility changes. 163 */ 164 public interface OnVisibilityChangeListener { 165 /** 166 * Called when the system UI visibility has changed. 167 * 168 * @param visible True if the system UI is visible. 169 */ 170 public void onVisibilityChange(boolean visible); 171 } 172} 173