19 | * For more on system bars, see System Bars. 22 | * 23 | * @see android.view.View#setSystemUiVisibility(int) 24 | * @see android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN 25 | */ 26 | public 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 | *
34 | * This flag isn't used on newer devices because the action 36 | * bar, 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 87 | * The activity whose window's system UI should be controlled by 88 | * this class. 89 | * @param anchorView 90 | * The view on which {@link View#setSystemUiVisibility(int)} will 91 | * be called. 92 | * @param flags 93 | * Either 0 or any combination of {@link #FLAG_FULLSCREEN}, 94 | * {@link #FLAG_HIDE_NAVIGATION}, and 95 | * {@link #FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES}. 96 | */ 97 | public static SystemUiHider getInstance(Activity activity, View anchorView, 98 | int flags) { 99 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 100 | return new SystemUiHiderHoneycomb(activity, anchorView, flags); 101 | } else { 102 | return new SystemUiHiderBase(activity, anchorView, flags); 103 | } 104 | } 105 | 106 | protected SystemUiHider(Activity activity, View anchorView, int flags) { 107 | mActivity = activity; 108 | mAnchorView = anchorView; 109 | mFlags = flags; 110 | } 111 | 112 | /** 113 | * Sets up the system UI hider. Should be called from 114 | * {@link Activity#onCreate}. 115 | */ 116 | public abstract void setup(); 117 | 118 | /** 119 | * Returns whether or not the system UI is visible. 120 | */ 121 | public abstract boolean isVisible(); 122 | 123 | /** 124 | * Hide the system UI. 125 | */ 126 | public abstract void hide(); 127 | 128 | /** 129 | * Show the system UI. 130 | */ 131 | public abstract void show(); 132 | 133 | /** 134 | * Toggle the visibility of the system UI. 135 | */ 136 | public void toggle() { 137 | if (isVisible()) { 138 | hide(); 139 | } else { 140 | show(); 141 | } 142 | } 143 | 144 | /** 145 | * Registers a callback, to be triggered when the system UI visibility 146 | * changes. 147 | */ 148 | public void setOnVisibilityChangeListener( 149 | OnVisibilityChangeListener listener) { 150 | if (listener == null) { 151 | listener = sDummyListener; 152 | } 153 | 154 | mOnVisibilityChangeListener = listener; 155 | } 156 | 157 | /** 158 | * A dummy no-op callback for use when there is no other listener set. 159 | */ 160 | private static OnVisibilityChangeListener sDummyListener = new OnVisibilityChangeListener() { 161 | @Override 162 | public void onVisibilityChange(boolean visible) { 163 | } 164 | }; 165 | 166 | /** 167 | * A callback interface used to listen for system UI visibility changes. 168 | */ 169 | public interface OnVisibilityChangeListener { 170 | /** 171 | * Called when the system UI visibility has changed. 172 | * 173 | * @param visible 174 | * True if the system UI is visible. 175 | */ 176 | public void onVisibilityChange(boolean visible); 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /src/com/sprintwind/packetcapturetool/util/SystemUiHiderBase.java: -------------------------------------------------------------------------------- 1 | package com.sprintwind.packetcapturetool.util; 2 | 3 | import android.app.Activity; 4 | import android.view.View; 5 | import android.view.WindowManager; 6 | 7 | /** 8 | * A base implementation of {@link SystemUiHider}. Uses APIs available in all 9 | * API levels to show and hide the status bar. 10 | */ 11 | public class SystemUiHiderBase extends SystemUiHider { 12 | /** 13 | * Whether or not the system UI is currently visible. This is a cached value 14 | * from calls to {@link #hide()} and {@link #show()}. 15 | */ 16 | private boolean mVisible = true; 17 | 18 | /** 19 | * Constructor not intended to be called by clients. Use 20 | * {@link SystemUiHider#getInstance} to obtain an instance. 21 | */ 22 | protected SystemUiHiderBase(Activity activity, View anchorView, int flags) { 23 | super(activity, anchorView, flags); 24 | } 25 | 26 | @Override 27 | public void setup() { 28 | if ((mFlags & FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES) == 0) { 29 | mActivity.getWindow().setFlags( 30 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 31 | | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 32 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 33 | | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 34 | } 35 | } 36 | 37 | @Override 38 | public boolean isVisible() { 39 | return mVisible; 40 | } 41 | 42 | @Override 43 | public void hide() { 44 | if ((mFlags & FLAG_FULLSCREEN) != 0) { 45 | mActivity.getWindow().setFlags( 46 | WindowManager.LayoutParams.FLAG_FULLSCREEN, 47 | WindowManager.LayoutParams.FLAG_FULLSCREEN); 48 | } 49 | mOnVisibilityChangeListener.onVisibilityChange(false); 50 | mVisible = false; 51 | } 52 | 53 | @Override 54 | public void show() { 55 | if ((mFlags & FLAG_FULLSCREEN) != 0) { 56 | mActivity.getWindow().setFlags(0, 57 | WindowManager.LayoutParams.FLAG_FULLSCREEN); 58 | } 59 | mOnVisibilityChangeListener.onVisibilityChange(true); 60 | mVisible = true; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/com/sprintwind/packetcapturetool/util/SystemUiHiderHoneycomb.java: -------------------------------------------------------------------------------- 1 | package com.sprintwind.packetcapturetool.util; 2 | 3 | import android.annotation.TargetApi; 4 | import android.app.Activity; 5 | import android.os.Build; 6 | import android.view.View; 7 | import 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) 15 | public 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, 46 | int flags) { 47 | super(activity, anchorView, flags); 48 | 49 | mShowFlags = View.SYSTEM_UI_FLAG_VISIBLE; 50 | mHideFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE; 51 | mTestFlags = View.SYSTEM_UI_FLAG_LOW_PROFILE; 52 | 53 | if ((mFlags & FLAG_FULLSCREEN) != 0) { 54 | // If the client requested fullscreen, add flags relevant to hiding 55 | // the status bar. Note that some of these constants are new as of 56 | // API 16 (Jelly Bean). It is safe to use them, as they are inlined 57 | // at compile-time and do nothing on pre-Jelly Bean devices. 58 | mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; 59 | mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 60 | | View.SYSTEM_UI_FLAG_FULLSCREEN; 61 | } 62 | 63 | if ((mFlags & FLAG_HIDE_NAVIGATION) != 0) { 64 | // If the client requested hiding navigation, add relevant flags. 65 | mShowFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; 66 | mHideFlags |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 67 | | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; 68 | mTestFlags |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; 69 | } 70 | } 71 | 72 | /** {@inheritDoc} */ 73 | @Override 74 | public void setup() { 75 | mAnchorView 76 | .setOnSystemUiVisibilityChangeListener(mSystemUiVisibilityChangeListener); 77 | } 78 | 79 | /** {@inheritDoc} */ 80 | @Override 81 | public void hide() { 82 | mAnchorView.setSystemUiVisibility(mHideFlags); 83 | } 84 | 85 | /** {@inheritDoc} */ 86 | @Override 87 | public void show() { 88 | mAnchorView.setSystemUiVisibility(mShowFlags); 89 | } 90 | 91 | /** {@inheritDoc} */ 92 | @Override 93 | public boolean isVisible() { 94 | return mVisible; 95 | } 96 | 97 | private View.OnSystemUiVisibilityChangeListener mSystemUiVisibilityChangeListener = new View.OnSystemUiVisibilityChangeListener() { 98 | @Override 99 | public void onSystemUiVisibilityChange(int vis) { 100 | // Test against mTestFlags to see if the system UI is visible. 101 | if ((vis & mTestFlags) != 0) { 102 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 103 | // Pre-Jelly Bean, we must manually hide the action bar 104 | // and use the old window flags API. 105 | mActivity.getActionBar().hide(); 106 | mActivity.getWindow().setFlags( 107 | WindowManager.LayoutParams.FLAG_FULLSCREEN, 108 | WindowManager.LayoutParams.FLAG_FULLSCREEN); 109 | } 110 | 111 | // Trigger the registered listener and cache the visibility 112 | // state. 113 | mOnVisibilityChangeListener.onVisibilityChange(false); 114 | mVisible = false; 115 | 116 | } else { 117 | mAnchorView.setSystemUiVisibility(mShowFlags); 118 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 119 | // Pre-Jelly Bean, we must manually show the action bar 120 | // and use the old window flags API. 121 | mActivity.getActionBar().show(); 122 | mActivity.getWindow().setFlags(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 | --------------------------------------------------------------------------------