Mercurial > cgi-bin > hgwebdir.cgi > FriendStream > ASProto
view 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 source
1 package com.example.musicplayer;
3 import com.example.musicplayer.util.SystemUiHider;
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;
14 /**
15 * An example full-screen activity that shows and hides the system UI (i.e.
16 * status bar and navigation/system bar) with user interaction.
17 *
18 * @see SystemUiHider
19 */
20 public class MainActivity extends Activity {
21 /**
22 * Whether or not the system UI should be auto-hidden after
23 * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
24 */
25 private static final boolean AUTO_HIDE = true;
27 /**
28 * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
29 * user interaction before hiding the system UI.
30 */
31 private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
33 /**
34 * If set, will toggle the system UI visibility upon interaction. Otherwise,
35 * will show the system UI visibility upon interaction.
36 */
37 private static final boolean TOGGLE_ON_CLICK = true;
39 /**
40 * The flags to pass to {@link SystemUiHider#getInstance}.
41 */
42 private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
44 /**
45 * The instance of the {@link SystemUiHider} for this activity.
46 */
47 private SystemUiHider mSystemUiHider;
49 @Override
50 protected void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
53 setContentView(R.layout.activity_main);
55 final View controlsView = findViewById(R.id.fullscreen_content_controls);
56 final View contentView = findViewById(R.id.fullscreen_content);
58 // Set up an instance of SystemUiHider to control the system UI for
59 // this activity.
60 mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
61 mSystemUiHider.setup();
62 mSystemUiHider
63 .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
64 // Cached values.
65 int mControlsHeight;
66 int mShortAnimTime;
68 @Override
69 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
70 public void onVisibilityChange(boolean visible) {
71 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
72 // If the ViewPropertyAnimator API is available
73 // (Honeycomb MR2 and later), use it to animate the
74 // in-layout UI controls at the bottom of the
75 // screen.
76 if (mControlsHeight == 0) {
77 mControlsHeight = controlsView.getHeight();
78 }
79 if (mShortAnimTime == 0) {
80 mShortAnimTime = getResources().getInteger(
81 android.R.integer.config_shortAnimTime);
82 }
83 controlsView.animate()
84 .translationY(visible ? 0 : mControlsHeight)
85 .setDuration(mShortAnimTime);
86 } else {
87 // If the ViewPropertyAnimator APIs aren't
88 // available, simply show or hide the in-layout UI
89 // controls.
90 controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
91 }
93 if (visible && AUTO_HIDE) {
94 // Schedule a hide().
95 delayedHide(AUTO_HIDE_DELAY_MILLIS);
96 }
97 }
98 });
100 // Set up the user interaction to manually show or hide the system UI.
101 contentView.setOnClickListener(new View.OnClickListener() {
102 @Override
103 public void onClick(View view) {
104 if (TOGGLE_ON_CLICK) {
105 mSystemUiHider.toggle();
106 } else {
107 mSystemUiHider.show();
108 }
109 }
110 });
112 // Upon interacting with UI controls, delay any scheduled hide()
113 // operations to prevent the jarring behavior of controls going away
114 // while interacting with the UI.
115 findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
116 }
118 @Override
119 protected void onPostCreate(Bundle savedInstanceState) {
120 super.onPostCreate(savedInstanceState);
122 // Trigger the initial hide() shortly after the activity has been
123 // created, to briefly hint to the user that UI controls
124 // are available.
125 delayedHide(100);
126 }
129 /**
130 * Touch listener to use for in-layout UI controls to delay hiding the
131 * system UI. This is to prevent the jarring behavior of controls going away
132 * while interacting with activity UI.
133 */
134 View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
135 @Override
136 public boolean onTouch(View view, MotionEvent motionEvent) {
137 if (AUTO_HIDE) {
138 delayedHide(AUTO_HIDE_DELAY_MILLIS);
139 }
140 return false;
141 }
142 };
144 Handler mHideHandler = new Handler();
145 Runnable mHideRunnable = new Runnable() {
146 @Override
147 public void run() {
148 mSystemUiHider.hide();
149 }
150 };
152 /**
153 * Schedules a call to hide() in [delay] milliseconds, canceling any
154 * previously scheduled calls.
155 */
156 private void delayedHide(int delayMillis) {
157 mHideHandler.removeCallbacks(mHideRunnable);
158 mHideHandler.postDelayed(mHideRunnable, delayMillis);
159 }
160 }
