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