| 1 |
package ${packageName}; |
| 2 |
|
| 3 |
import android.annotation.SuppressLint; |
| 4 |
import ${actionBarClassFqcn}; |
| 5 |
import ${superClassFqcn}; |
| 6 |
import android.os.Bundle; |
| 7 |
import android.os.Handler; |
| 8 |
import android.view.MotionEvent; |
| 9 |
import android.view.View; |
| 10 |
<#if parentActivityClass != ""> |
| 11 |
import android.view.MenuItem; |
| 12 |
import android.support.v4.app.NavUtils; |
| 13 |
</#if> |
| 14 |
<#if applicationPackage??> |
| 15 |
import ${applicationPackage}.R; |
| 16 |
</#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 |
public class ${activityClass} extends ${superClass} { |
| 23 |
/** |
| 24 |
* Whether or not the system UI should be auto-hidden after |
| 25 |
* {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds. |
| 26 |
*/ |
| 27 |
private static final boolean AUTO_HIDE = true; |
| 28 |
|
| 29 |
/** |
| 30 |
* If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after |
| 31 |
* user interaction before hiding the system UI. |
| 32 |
*/ |
| 33 |
private static final int AUTO_HIDE_DELAY_MILLIS = 3000; |
| 34 |
|
| 35 |
/** |
| 36 |
* Some older devices needs a small delay between UI widget updates |
| 37 |
* and a change of the status and navigation bar. |
| 38 |
*/ |
| 39 |
private static final int UI_ANIMATION_DELAY = 300; |
| 40 |
|
| 41 |
private View mContentView; |
| 42 |
private View mControlsView; |
| 43 |
private boolean mVisible; |
| 44 |
|
| 45 |
@Override |
| 46 |
protected void onCreate(Bundle savedInstanceState) { |
| 47 |
super.onCreate(savedInstanceState); |
| 48 |
|
| 49 |
setContentView(R.layout.${layoutName}); |
| 50 |
<#if parentActivityClass != ""> |
| 51 |
ActionBar actionBar = get${Support}ActionBar(); |
| 52 |
if (actionBar != null) { |
| 53 |
actionBar.setDisplayHomeAsUpEnabled(true); |
| 54 |
} |
| 55 |
</#if> |
| 56 |
|
| 57 |
mVisible = true; |
| 58 |
mControlsView = findViewById(R.id.fullscreen_content_controls); |
| 59 |
mContentView = findViewById(R.id.fullscreen_content); |
| 60 |
|
| 61 |
|
| 62 |
// Set up the user interaction to manually show or hide the system UI. |
| 63 |
mContentView.setOnClickListener(new View.OnClickListener() { |
| 64 |
@Override |
| 65 |
public void onClick(View view) { |
| 66 |
toggle(); |
| 67 |
} |
| 68 |
}); |
| 69 |
|
| 70 |
// Upon interacting with UI controls, delay any scheduled hide() |
| 71 |
// operations to prevent the jarring behavior of controls going away |
| 72 |
// while interacting with the UI. |
| 73 |
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
protected void onPostCreate(Bundle savedInstanceState) { |
| 78 |
super.onPostCreate(savedInstanceState); |
| 79 |
|
| 80 |
// Trigger the initial hide() shortly after the activity has been |
| 81 |
// created, to briefly hint to the user that UI controls |
| 82 |
// are available. |
| 83 |
delayedHide(100); |
| 84 |
} |
| 85 |
|
| 86 |
<#if parentActivityClass != ""> |
| 87 |
@Override |
| 88 |
public boolean onOptionsItemSelected(MenuItem item) { |
| 89 |
int id = item.getItemId(); |
| 90 |
if (id == android.R.id.home) { |
| 91 |
// This ID represents the Home or Up button. |
| 92 |
NavUtils.navigateUpFromSameTask(this); |
| 93 |
return true; |
| 94 |
} |
| 95 |
return super.onOptionsItemSelected(item); |
| 96 |
} |
| 97 |
|
| 98 |
</#if> |
| 99 |
/** |
| 100 |
* Touch listener to use for in-layout UI controls to delay hiding the |
| 101 |
* system UI. This is to prevent the jarring behavior of controls going away |
| 102 |
* while interacting with activity UI. |
| 103 |
*/ |
| 104 |
private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() { |
| 105 |
@Override |
| 106 |
public boolean onTouch(View view, MotionEvent motionEvent) { |
| 107 |
if (AUTO_HIDE) { |
| 108 |
delayedHide(AUTO_HIDE_DELAY_MILLIS); |
| 109 |
} |
| 110 |
return false; |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
private void toggle() { |
| 115 |
if (mVisible) { |
| 116 |
hide(); |
| 117 |
} else { |
| 118 |
show(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
private void hide() { |
| 123 |
// Hide UI first |
| 124 |
ActionBar actionBar = get${Support}ActionBar(); |
| 125 |
if (actionBar != null) { |
| 126 |
actionBar.hide(); |
| 127 |
} |
| 128 |
mControlsView.setVisibility(View.GONE); |
| 129 |
mVisible = false; |
| 130 |
|
| 131 |
// Schedule a runnable to remove the status and navigation bar after a delay |
| 132 |
mHideHandler.removeCallbacks(mShowPart2Runnable); |
| 133 |
mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY); |
| 134 |
} |
| 135 |
|
| 136 |
private final Runnable mHidePart2Runnable = new Runnable() { |
| 137 |
@SuppressLint("InlinedApi") |
| 138 |
@Override |
| 139 |
public void run() { |
| 140 |
// Delayed removal of status and navigation bar |
| 141 |
|
| 142 |
// Note that some of these constants are new as of API 16 (Jelly Bean) |
| 143 |
// and API 19 (KitKat). It is safe to use them, as they are inlined |
| 144 |
// at compile-time and do nothing on earlier devices. |
| 145 |
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE |
| 146 |
| View.SYSTEM_UI_FLAG_FULLSCREEN |
| 147 |
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
| 148 |
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
| 149 |
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 150 |
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
@SuppressLint("InlinedApi") |
| 155 |
private void show() { |
| 156 |
// Show the system bar |
| 157 |
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 158 |
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); |
| 159 |
mVisible = true; |
| 160 |
|
| 161 |
// Schedule a runnable to display UI elements after a delay |
| 162 |
mHideHandler.removeCallbacks(mHidePart2Runnable); |
| 163 |
mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY); |
| 164 |
} |
| 165 |
|
| 166 |
private final Runnable mShowPart2Runnable = new Runnable() { |
| 167 |
@Override |
| 168 |
public void run() { |
| 169 |
// Delayed display of UI elements |
| 170 |
ActionBar actionBar = get${Support}ActionBar(); |
| 171 |
if (actionBar != null) { |
| 172 |
actionBar.show(); |
| 173 |
} |
| 174 |
mControlsView.setVisibility(View.VISIBLE); |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
private final Handler mHideHandler = new Handler(); |
| 179 |
private final Runnable mHideRunnable = new Runnable() { |
| 180 |
@Override |
| 181 |
public void run() { |
| 182 |
hide(); |
| 183 |
} |
| 184 |
}; |
| 185 |
|
| 186 |
/** |
| 187 |
* Schedules a call to hide() in [delay] milliseconds, canceling any |
| 188 |
* previously scheduled calls. |
| 189 |
*/ |
| 190 |
private void delayedHide(int delayMillis) { |
| 191 |
mHideHandler.removeCallbacks(mHideRunnable); |
| 192 |
mHideHandler.postDelayed(mHideRunnable, delayMillis); |
| 193 |
} |
| 194 |
} |