|
| 1 | +package in.ac.dtu.subtlenews; |
| 2 | + |
| 3 | +import in.ac.dtu.subtlenews.util.SystemUiHider; |
| 4 | + |
| 5 | +import android.annotation.TargetApi; |
| 6 | +import android.app.Activity; |
| 7 | +import android.content.Intent; |
| 8 | +import android.os.Build; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.os.Handler; |
| 11 | +import android.view.MotionEvent; |
| 12 | +import android.view.View; |
| 13 | +import android.view.MenuItem; |
| 14 | +import android.support.v4.app.NavUtils; |
| 15 | + |
| 16 | +/** |
| 17 | + * An example full-screen activity that shows and hides the system UI (i.e. |
| 18 | + * status bar and navigation/system bar) with user interaction. |
| 19 | + * |
| 20 | + * @see SystemUiHider |
| 21 | + */ |
| 22 | +public class InstapaperViewer extends Activity { |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * Whether or not the system UI should be auto-hidden after |
| 27 | + * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds. |
| 28 | + */ |
| 29 | + private static final boolean AUTO_HIDE = true; |
| 30 | + |
| 31 | + /** |
| 32 | + * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after |
| 33 | + * user interaction before hiding the system UI. |
| 34 | + */ |
| 35 | + private static final int AUTO_HIDE_DELAY_MILLIS = 3000; |
| 36 | + |
| 37 | + /** |
| 38 | + * If set, will toggle the system UI visibility upon interaction. Otherwise, |
| 39 | + * will show the system UI visibility upon interaction. |
| 40 | + */ |
| 41 | + private static final boolean TOGGLE_ON_CLICK = true; |
| 42 | + |
| 43 | + /** |
| 44 | + * The flags to pass to {@link SystemUiHider#getInstance}. |
| 45 | + */ |
| 46 | + private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION; |
| 47 | + |
| 48 | + /** |
| 49 | + * The instance of the {@link SystemUiHider} for this activity. |
| 50 | + */ |
| 51 | + private SystemUiHider mSystemUiHider; |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void onCreate(Bundle savedInstanceState) { |
| 55 | + super.onCreate(savedInstanceState); |
| 56 | + |
| 57 | + setContentView(R.layout.activity_instapaper_viewer); |
| 58 | + setupActionBar(); |
| 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.summary_back_button).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 | + /** |
| 134 | + * Set up the {@link android.app.ActionBar}, if the API is available. |
| 135 | + */ |
| 136 | + @TargetApi(Build.VERSION_CODES.HONEYCOMB) |
| 137 | + private void setupActionBar() { |
| 138 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { |
| 139 | + // Show the Up button in the action bar. |
| 140 | + getActionBar().setDisplayHomeAsUpEnabled(true); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 146 | + int id = item.getItemId(); |
| 147 | + if (id == android.R.id.home) { |
| 148 | + // This ID represents the Home or Up button. In the case of this |
| 149 | + // activity, the Up button is shown. Use NavUtils to allow users |
| 150 | + // to navigate up one level in the application structure. For |
| 151 | + // more details, see the Navigation pattern on Android Design: |
| 152 | + // |
| 153 | + // http://developer.android.com/design/patterns/navigation.html#up-vs-back |
| 154 | + // |
| 155 | + // TODO: If Settings has multiple levels, Up should navigate up |
| 156 | + // that hierarchy. |
| 157 | + NavUtils.navigateUpFromSameTask(this); |
| 158 | + return true; |
| 159 | + } |
| 160 | + return super.onOptionsItemSelected(item); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Touch listener to use for in-layout UI controls to delay hiding the |
| 165 | + * system UI. This is to prevent the jarring behavior of controls going away |
| 166 | + * while interacting with activity UI. |
| 167 | + */ |
| 168 | + View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() { |
| 169 | + @Override |
| 170 | + public boolean onTouch(View view, MotionEvent motionEvent) { |
| 171 | + if (AUTO_HIDE) { |
| 172 | + delayedHide(AUTO_HIDE_DELAY_MILLIS); |
| 173 | + } |
| 174 | + return false; |
| 175 | + } |
| 176 | + }; |
| 177 | + |
| 178 | + Handler mHideHandler = new Handler(); |
| 179 | + Runnable mHideRunnable = new Runnable() { |
| 180 | + @Override |
| 181 | + public void run() { |
| 182 | + mSystemUiHider.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 | +} |
0 commit comments