diff app/src/main/java/com/example/musicplayer/MainActivity.java @ 2:3485a304a057

adding new branch with the original tutorial code -- later merge to main and mod
author kshalle
date Thu, 23 Apr 2015 10:53:54 -0700
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/app/src/main/java/com/example/musicplayer/MainActivity.java	Thu Apr 23 10:53:54 2015 -0700
     1.3 @@ -0,0 +1,160 @@
     1.4 +package com.example.musicplayer;
     1.5 +
     1.6 +import com.example.musicplayer.util.SystemUiHider;
     1.7 +
     1.8 +import android.annotation.TargetApi;
     1.9 +import android.app.Activity;
    1.10 +import android.os.Build;
    1.11 +import android.os.Bundle;
    1.12 +import android.os.Handler;
    1.13 +import android.view.MotionEvent;
    1.14 +import android.view.View;
    1.15 +
    1.16 +
    1.17 +/**
    1.18 + * An example full-screen activity that shows and hides the system UI (i.e.
    1.19 + * status bar and navigation/system bar) with user interaction.
    1.20 + *
    1.21 + * @see SystemUiHider
    1.22 + */
    1.23 +public class MainActivity extends Activity {
    1.24 +    /**
    1.25 +     * Whether or not the system UI should be auto-hidden after
    1.26 +     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
    1.27 +     */
    1.28 +    private static final boolean AUTO_HIDE = true;
    1.29 +
    1.30 +    /**
    1.31 +     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
    1.32 +     * user interaction before hiding the system UI.
    1.33 +     */
    1.34 +    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
    1.35 +
    1.36 +    /**
    1.37 +     * If set, will toggle the system UI visibility upon interaction. Otherwise,
    1.38 +     * will show the system UI visibility upon interaction.
    1.39 +     */
    1.40 +    private static final boolean TOGGLE_ON_CLICK = true;
    1.41 +
    1.42 +    /**
    1.43 +     * The flags to pass to {@link SystemUiHider#getInstance}.
    1.44 +     */
    1.45 +    private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
    1.46 +
    1.47 +    /**
    1.48 +     * The instance of the {@link SystemUiHider} for this activity.
    1.49 +     */
    1.50 +    private SystemUiHider mSystemUiHider;
    1.51 +
    1.52 +    @Override
    1.53 +    protected void onCreate(Bundle savedInstanceState) {
    1.54 +        super.onCreate(savedInstanceState);
    1.55 +
    1.56 +        setContentView(R.layout.activity_main);
    1.57 +
    1.58 +        final View controlsView = findViewById(R.id.fullscreen_content_controls);
    1.59 +        final View contentView = findViewById(R.id.fullscreen_content);
    1.60 +
    1.61 +        // Set up an instance of SystemUiHider to control the system UI for
    1.62 +        // this activity.
    1.63 +        mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
    1.64 +        mSystemUiHider.setup();
    1.65 +        mSystemUiHider
    1.66 +                .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
    1.67 +                    // Cached values.
    1.68 +                    int mControlsHeight;
    1.69 +                    int mShortAnimTime;
    1.70 +
    1.71 +                    @Override
    1.72 +                    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    1.73 +                    public void onVisibilityChange(boolean visible) {
    1.74 +                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    1.75 +                            // If the ViewPropertyAnimator API is available
    1.76 +                            // (Honeycomb MR2 and later), use it to animate the
    1.77 +                            // in-layout UI controls at the bottom of the
    1.78 +                            // screen.
    1.79 +                            if (mControlsHeight == 0) {
    1.80 +                                mControlsHeight = controlsView.getHeight();
    1.81 +                            }
    1.82 +                            if (mShortAnimTime == 0) {
    1.83 +                                mShortAnimTime = getResources().getInteger(
    1.84 +                                        android.R.integer.config_shortAnimTime);
    1.85 +                            }
    1.86 +                            controlsView.animate()
    1.87 +                                    .translationY(visible ? 0 : mControlsHeight)
    1.88 +                                    .setDuration(mShortAnimTime);
    1.89 +                        } else {
    1.90 +                            // If the ViewPropertyAnimator APIs aren't
    1.91 +                            // available, simply show or hide the in-layout UI
    1.92 +                            // controls.
    1.93 +                            controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
    1.94 +                        }
    1.95 +
    1.96 +                        if (visible && AUTO_HIDE) {
    1.97 +                            // Schedule a hide().
    1.98 +                            delayedHide(AUTO_HIDE_DELAY_MILLIS);
    1.99 +                        }
   1.100 +                    }
   1.101 +                });
   1.102 +
   1.103 +        // Set up the user interaction to manually show or hide the system UI.
   1.104 +        contentView.setOnClickListener(new View.OnClickListener() {
   1.105 +            @Override
   1.106 +            public void onClick(View view) {
   1.107 +                if (TOGGLE_ON_CLICK) {
   1.108 +                    mSystemUiHider.toggle();
   1.109 +                } else {
   1.110 +                    mSystemUiHider.show();
   1.111 +                }
   1.112 +            }
   1.113 +        });
   1.114 +
   1.115 +        // Upon interacting with UI controls, delay any scheduled hide()
   1.116 +        // operations to prevent the jarring behavior of controls going away
   1.117 +        // while interacting with the UI.
   1.118 +        findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
   1.119 +    }
   1.120 +
   1.121 +    @Override
   1.122 +    protected void onPostCreate(Bundle savedInstanceState) {
   1.123 +        super.onPostCreate(savedInstanceState);
   1.124 +
   1.125 +        // Trigger the initial hide() shortly after the activity has been
   1.126 +        // created, to briefly hint to the user that UI controls
   1.127 +        // are available.
   1.128 +        delayedHide(100);
   1.129 +    }
   1.130 +
   1.131 +
   1.132 +    /**
   1.133 +     * Touch listener to use for in-layout UI controls to delay hiding the
   1.134 +     * system UI. This is to prevent the jarring behavior of controls going away
   1.135 +     * while interacting with activity UI.
   1.136 +     */
   1.137 +    View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
   1.138 +        @Override
   1.139 +        public boolean onTouch(View view, MotionEvent motionEvent) {
   1.140 +            if (AUTO_HIDE) {
   1.141 +                delayedHide(AUTO_HIDE_DELAY_MILLIS);
   1.142 +            }
   1.143 +            return false;
   1.144 +        }
   1.145 +    };
   1.146 +
   1.147 +    Handler mHideHandler = new Handler();
   1.148 +    Runnable mHideRunnable = new Runnable() {
   1.149 +        @Override
   1.150 +        public void run() {
   1.151 +            mSystemUiHider.hide();
   1.152 +        }
   1.153 +    };
   1.154 +
   1.155 +    /**
   1.156 +     * Schedules a call to hide() in [delay] milliseconds, canceling any
   1.157 +     * previously scheduled calls.
   1.158 +     */
   1.159 +    private void delayedHide(int delayMillis) {
   1.160 +        mHideHandler.removeCallbacks(mHideRunnable);
   1.161 +        mHideHandler.postDelayed(mHideRunnable, delayMillis);
   1.162 +    }
   1.163 +}