kshalle@2: package com.example.musicplayer; kshalle@2: kshalle@2: import com.example.musicplayer.util.SystemUiHider; kshalle@2: kshalle@2: import android.annotation.TargetApi; kshalle@2: import android.app.Activity; kshalle@2: import android.os.Build; kshalle@2: import android.os.Bundle; kshalle@2: import android.os.Handler; kshalle@2: import android.view.MotionEvent; kshalle@2: import android.view.View; kshalle@2: kshalle@2: kshalle@2: /** kshalle@2: * An example full-screen activity that shows and hides the system UI (i.e. kshalle@2: * status bar and navigation/system bar) with user interaction. kshalle@2: * kshalle@2: * @see SystemUiHider kshalle@2: */ kshalle@2: public class MainActivity extends Activity { kshalle@2: /** kshalle@2: * Whether or not the system UI should be auto-hidden after kshalle@2: * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds. kshalle@2: */ kshalle@2: private static final boolean AUTO_HIDE = true; kshalle@2: kshalle@2: /** kshalle@2: * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after kshalle@2: * user interaction before hiding the system UI. kshalle@2: */ kshalle@2: private static final int AUTO_HIDE_DELAY_MILLIS = 3000; kshalle@2: kshalle@2: /** kshalle@2: * If set, will toggle the system UI visibility upon interaction. Otherwise, kshalle@2: * will show the system UI visibility upon interaction. kshalle@2: */ kshalle@2: private static final boolean TOGGLE_ON_CLICK = true; kshalle@2: kshalle@2: /** kshalle@2: * The flags to pass to {@link SystemUiHider#getInstance}. kshalle@2: */ kshalle@2: private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION; kshalle@2: kshalle@2: /** kshalle@2: * The instance of the {@link SystemUiHider} for this activity. kshalle@2: */ kshalle@2: private SystemUiHider mSystemUiHider; kshalle@2: kshalle@2: @Override kshalle@2: protected void onCreate(Bundle savedInstanceState) { kshalle@2: super.onCreate(savedInstanceState); kshalle@2: kshalle@2: setContentView(R.layout.activity_main); kshalle@2: kshalle@2: final View controlsView = findViewById(R.id.fullscreen_content_controls); kshalle@2: final View contentView = findViewById(R.id.fullscreen_content); kshalle@2: kshalle@2: // Set up an instance of SystemUiHider to control the system UI for kshalle@2: // this activity. kshalle@2: mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS); kshalle@2: mSystemUiHider.setup(); kshalle@2: mSystemUiHider kshalle@2: .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() { kshalle@2: // Cached values. kshalle@2: int mControlsHeight; kshalle@2: int mShortAnimTime; kshalle@2: kshalle@2: @Override kshalle@2: @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) kshalle@2: public void onVisibilityChange(boolean visible) { kshalle@2: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { kshalle@2: // If the ViewPropertyAnimator API is available kshalle@2: // (Honeycomb MR2 and later), use it to animate the kshalle@2: // in-layout UI controls at the bottom of the kshalle@2: // screen. kshalle@2: if (mControlsHeight == 0) { kshalle@2: mControlsHeight = controlsView.getHeight(); kshalle@2: } kshalle@2: if (mShortAnimTime == 0) { kshalle@2: mShortAnimTime = getResources().getInteger( kshalle@2: android.R.integer.config_shortAnimTime); kshalle@2: } kshalle@2: controlsView.animate() kshalle@2: .translationY(visible ? 0 : mControlsHeight) kshalle@2: .setDuration(mShortAnimTime); kshalle@2: } else { kshalle@2: // If the ViewPropertyAnimator APIs aren't kshalle@2: // available, simply show or hide the in-layout UI kshalle@2: // controls. kshalle@2: controlsView.setVisibility(visible ? View.VISIBLE : View.GONE); kshalle@2: } kshalle@2: kshalle@2: if (visible && AUTO_HIDE) { kshalle@2: // Schedule a hide(). kshalle@2: delayedHide(AUTO_HIDE_DELAY_MILLIS); kshalle@2: } kshalle@2: } kshalle@2: }); kshalle@2: kshalle@2: // Set up the user interaction to manually show or hide the system UI. kshalle@2: contentView.setOnClickListener(new View.OnClickListener() { kshalle@2: @Override kshalle@2: public void onClick(View view) { kshalle@2: if (TOGGLE_ON_CLICK) { kshalle@2: mSystemUiHider.toggle(); kshalle@2: } else { kshalle@2: mSystemUiHider.show(); kshalle@2: } kshalle@2: } kshalle@2: }); kshalle@2: kshalle@2: // Upon interacting with UI controls, delay any scheduled hide() kshalle@2: // operations to prevent the jarring behavior of controls going away kshalle@2: // while interacting with the UI. kshalle@2: findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener); kshalle@2: } kshalle@2: kshalle@2: @Override kshalle@2: protected void onPostCreate(Bundle savedInstanceState) { kshalle@2: super.onPostCreate(savedInstanceState); kshalle@2: kshalle@2: // Trigger the initial hide() shortly after the activity has been kshalle@2: // created, to briefly hint to the user that UI controls kshalle@2: // are available. kshalle@2: delayedHide(100); kshalle@2: } kshalle@2: kshalle@2: kshalle@2: /** kshalle@2: * Touch listener to use for in-layout UI controls to delay hiding the kshalle@2: * system UI. This is to prevent the jarring behavior of controls going away kshalle@2: * while interacting with activity UI. kshalle@2: */ kshalle@2: View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() { kshalle@2: @Override kshalle@2: public boolean onTouch(View view, MotionEvent motionEvent) { kshalle@2: if (AUTO_HIDE) { kshalle@2: delayedHide(AUTO_HIDE_DELAY_MILLIS); kshalle@2: } kshalle@2: return false; kshalle@2: } kshalle@2: }; kshalle@2: kshalle@2: Handler mHideHandler = new Handler(); kshalle@2: Runnable mHideRunnable = new Runnable() { kshalle@2: @Override kshalle@2: public void run() { kshalle@2: mSystemUiHider.hide(); kshalle@2: } kshalle@2: }; kshalle@2: kshalle@2: /** kshalle@2: * Schedules a call to hide() in [delay] milliseconds, canceling any kshalle@2: * previously scheduled calls. kshalle@2: */ kshalle@2: private void delayedHide(int delayMillis) { kshalle@2: mHideHandler.removeCallbacks(mHideRunnable); kshalle@2: mHideHandler.postDelayed(mHideRunnable, delayMillis); kshalle@2: } kshalle@2: }