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