1package ${packageName}.util; 2 3import android.annotation.TargetApi; 4import android.app.Activity; 5import android.os.Build; 6import android.view.View; 7import android.view.WindowManager; 8 9/** 10 * An API 11+ implementation of {@link SystemUiHider}. Uses APIs available in 11 * Honeycomb and later (specifically {@link View#setSystemUiVisibility(int)}) to 12 * show and hide the system UI. 13 */ 14@TargetApi(Build.VERSION_CODES.HONEYCOMB) 15public class SystemUiHiderHoneycomb extends SystemUiHiderBase { 16 /** 17 * Flags for {@link View#setSystemUiVisibility(int)} to use when showing the 18 * system UI. 19 */ 20 private int mShowFlags; 21 22 /** 23 * Flags for {@link View#setSystemUiVisibility(int)} to use when hiding the 24 * system UI. 25 */ 26 private int mHideFlags; 27 28 /** 29 * Flags to test against the first parameter in 30 * {@link android.view.View.OnSystemUiVisibilityChangeListener#onSystemUiVisibilityChange(int)} 31 * to determine the system UI visibility state. 32 */ 33 private int mTestFlags; 34 35 /** 36 * Whether or not the system UI is currently visible. This is cached from 37 * {@link android.view.View.OnSystemUiVisibilityChangeListener}. 38 */ 39 private boolean mVisible = true; 40 41 /** 42 * Constructor not intended to be called by clients. Use 43 * {@link SystemUiHider#getInstance} to obtain an instance. 44 */ 45 protected SystemUiHiderHoneycomb(Activity activity, View anchorView, int flags) { 46 super(activity, anchorView, flags); 47 48 mShowFlags = View.SYSTEM_UI_FLAG_VISIBLE; 49 mHideFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE; 50 mTestFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE; 51 52 if ((mFlags & FLAG_FULLSCREEN) != 0) { 53 // If the client requested fullscreen, add flags relevant to hiding 54 // the status bar. Note that some of these constants are new as of 55 // API 16 (Jelly Bean). It is safe to use them, as they are inlined 56 // at compile-time and do nothing on pre-Jelly Bean devices. 57 mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; 58 mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 59 | View.SYSTEM_UI_FLAG_FULLSCREEN; 60 } 61 62 if ((mFlags & FLAG_HIDE_NAVIGATION) != 0) { 63 // If the client requested hiding navigation, add relevant flags. 64 mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; 65 mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 66 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; 67 mTestFlags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; 68 } 69 } 70 71 /** {@inheritDoc} */ 72 @Override 73 public void setup() { 74 mAnchorView.setOnSystemUiVisibilityChangeListener(mSystemUiVisibilityChangeListener); 75 } 76 77 /** {@inheritDoc} */ 78 @Override 79 public void hide() { 80 mAnchorView.setSystemUiVisibility(mHideFlags); 81 } 82 83 /** {@inheritDoc} */ 84 @Override 85 public void show() { 86 mAnchorView.setSystemUiVisibility(mShowFlags); 87 } 88 89 /** {@inheritDoc} */ 90 @Override 91 public boolean isVisible() { 92 return mVisible; 93 } 94 95 private View.OnSystemUiVisibilityChangeListener mSystemUiVisibilityChangeListener 96 = new View.OnSystemUiVisibilityChangeListener() { 97 @Override 98 public void onSystemUiVisibilityChange(int vis) { 99 // Test against mTestFlags to see if the system UI is visible. 100 if ((vis & mTestFlags) != 0) { 101 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 102 // Pre-Jelly Bean, we must manually hide the action bar 103 // and use the old window flags API. 104 mActivity.getActionBar().hide(); 105 mActivity.getWindow().setFlags( 106 WindowManager.LayoutParams.FLAG_FULLSCREEN, 107 WindowManager.LayoutParams.FLAG_FULLSCREEN); 108 } 109 110 // Trigger the registered listener and cache the visibility 111 // state. 112 mOnVisibilityChangeListener.onVisibilityChange(false); 113 mVisible = false; 114 115 } else { 116 mAnchorView.setSystemUiVisibility(mShowFlags); 117 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 118 // Pre-Jelly Bean, we must manually show the action bar 119 // and use the old window flags API. 120 mActivity.getActionBar().show(); 121 mActivity.getWindow().setFlags( 122 0, 123 WindowManager.LayoutParams.FLAG_FULLSCREEN); 124 } 125 126 // Trigger the registered listener and cache the visibility 127 // state. 128 mOnVisibilityChangeListener.onVisibilityChange(true); 129 mVisible = true; 130 } 131 } 132 }; 133} 134