1 package ${packageName}; 2 3 import ${packageName}.util.SystemUiHider; 4 5 import android.annotation.TargetApi; 6 import android.app.Activity; 7 import android.os.Build; 8 import android.os.Bundle; 9 import android.os.Handler; 10 import android.view.MotionEvent; 11 import android.view.View; 12 <#if parentActivityClass != ""> 13 import android.view.MenuItem; 14 import android.support.v4.app.NavUtils; 15 </#if> 16 17 /** 18 * An example full-screen activity that shows and hides the system UI (i.e. 19 * status bar and navigation/system bar) with user interaction. 20 * 21 * @see SystemUiHider 22 */ 23 public class ${activityClass} extends Activity { 24 /** 25 * Whether or not the system UI should be auto-hidden after 26 * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds. 27 */ 28 private static final boolean AUTO_HIDE = true; 29 30 /** 31 * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after 32 * user interaction before hiding the system UI. 33 */ 34 private static final int AUTO_HIDE_DELAY_MILLIS = 3000; 35 36 /** 37 * If set, will toggle the system UI visibility upon interaction. Otherwise, 38 * will show the system UI visibility upon interaction. 39 */ 40 private static final boolean TOGGLE_ON_CLICK = true; 41 42 /** 43 * The flags to pass to {@link SystemUiHider#getInstance}. 44 */ 45 private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION; 46 47 /** 48 * The instance of the {@link SystemUiHider} for this activity. 49 */ 50 private SystemUiHider mSystemUiHider; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 56 setContentView(R.layout.${layoutName}); 57 <#if parentActivityClass != ""> 58 setupActionBar(); 59 </#if> 60 61 final View controlsView = findViewById(R.id.fullscreen_content_controls); 62 final View contentView = findViewById(R.id.fullscreen_content); 63 64 // Set up an instance of SystemUiHider to control the system UI for 65 // this activity. 66 mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS); 67 mSystemUiHider.setup(); 68 mSystemUiHider 69 .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() { 70 // Cached values. 71 int mControlsHeight; 72 int mShortAnimTime; 73 74 @Override 75 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 76 public void onVisibilityChange(boolean visible) { 77 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 78 // If the ViewPropertyAnimator API is available 79 // (Honeycomb MR2 and later), use it to animate the 80 // in-layout UI controls at the bottom of the 81 // screen. 82 if (mControlsHeight == 0) { 83 mControlsHeight = controlsView.getHeight(); 84 } 85 if (mShortAnimTime == 0) { 86 mShortAnimTime = getResources().getInteger( 87 android.R.integer.config_shortAnimTime); 88 } 89 controlsView.animate() 90 .translationY(visible ? 0 : mControlsHeight) 91 .setDuration(mShortAnimTime); 92 } else { 93 // If the ViewPropertyAnimator APIs aren't 94 // available, simply show or hide the in-layout UI 95 // controls. 96 controlsView.setVisibility(visible ? View.VISIBLE : View.GONE); 97 } 98 99 if (visible && AUTO_HIDE) { 100 // Schedule a hide(). 101 delayedHide(AUTO_HIDE_DELAY_MILLIS); 102 } 103 } 104 }); 105 106 // Set up the user interaction to manually show or hide the system UI. 107 contentView.setOnClickListener(new View.OnClickListener() { 108 @Override 109 public void onClick(View view) { 110 if (TOGGLE_ON_CLICK) { 111 mSystemUiHider.toggle(); 112 } else { 113 mSystemUiHider.show(); 114 } 115 } 116 }); 117 118 // Upon interacting with UI controls, delay any scheduled hide() 119 // operations to prevent the jarring behavior of controls going away 120 // while interacting with the UI. 121 findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener); 122 } 123 124 @Override 125 protected void onPostCreate(Bundle savedInstanceState) { 126 super.onPostCreate(savedInstanceState); 127 128 // Trigger the initial hide() shortly after the activity has been 129 // created, to briefly hint to the user that UI controls 130 // are available. 131 delayedHide(100); 132 } 133 134 <#if parentActivityClass != ""> 135 /** 136 * Set up the {@link android.app.ActionBar}, if the API is available. 137 */ 138 @TargetApi(Build.VERSION_CODES.HONEYCOMB) 139 private void setupActionBar() { 140 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 141 // Show the Up button in the action bar. 142 getActionBar().setDisplayHomeAsUpEnabled(true); 143 } 144 } 145 146 @Override 147 public boolean onOptionsItemSelected(MenuItem item) { 148 switch (item.getItemId()) { 149 case android.R.id.home: 150 // This ID represents the Home or Up button. In the case of this 151 // activity, the Up button is shown. Use NavUtils to allow users 152 // to navigate up one level in the application structure. For 153 // more details, see the Navigation pattern on Android Design: 154 // 155 // http://developer.android.com/design/patterns/navigation.html#up-vs-back 156 // 157 // TODO: If Settings has multiple levels, Up should navigate up 158 // that hierarchy. 159 NavUtils.navigateUpFromSameTask(this); 160 return true; 161 } 162 return super.onOptionsItemSelected(item); 163 } 164 </#if> 165 166 /** 167 * Touch listener to use for in-layout UI controls to delay hiding the 168 * system UI. This is to prevent the jarring behavior of controls going away 169 * while interacting with activity UI. 170 */ 171 View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() { 172 @Override 173 public boolean onTouch(View view, MotionEvent motionEvent) { 174 if (AUTO_HIDE) { 175 delayedHide(AUTO_HIDE_DELAY_MILLIS); 176 } 177 return false; 178 } 179 }; 180 181 Handler mHideHandler = new Handler(); 182 Runnable mHideRunnable = new Runnable() { 183 @Override 184 public void run() { 185 mSystemUiHider.hide(); 186 } 187 }; 188 189 /** 190 * Schedules a call to hide() in [delay] milliseconds, canceling any 191 * previously scheduled calls. 192 */ 193 private void delayedHide(int delayMillis) { 194 mHideHandler.removeCallbacks(mHideRunnable); 195 mHideHandler.postDelayed(mHideRunnable, delayMillis); 196 } 197 } 198