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