Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.simon.flashbang;
- import com.simon.flashbang.util.SystemUiHider;
- import android.app.Activity;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.WindowManager;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.RelativeLayout;
- import android.widget.Toast;
- /**
- * An example full-screen activity that shows and hides the system UI (i.e.
- * status bar and navigation/system bar) with user interaction.
- *
- * @see SystemUiHider
- */
- public class FlashActivity extends Activity {
- private static boolean PARTY_IS_OVER = false;
- private static boolean PENDING_EXPLOSION = false;
- private RelativeLayout background_layout;
- private MediaPlayer mp, mp_final;
- /**
- * Whether or not the system UI should be auto-hidden after
- * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
- */
- private static boolean AUTO_HIDE = false;
- /**
- * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
- * user interaction before hiding the system UI.
- */
- private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
- /**
- * If set, will toggle the system UI visibility upon interaction. Otherwise,
- * will show the system UI visibility upon interaction.
- */
- private static boolean TOGGLE_ON_CLICK = true;
- /**
- * The flags to pass to {@link SystemUiHider#getInstance}.
- */
- private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
- /**
- * The instance of the {@link SystemUiHider} for this activity.
- */
- private SystemUiHider mSystemUiHider;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- WindowManager.LayoutParams layout = getWindow().getAttributes();
- layout.screenBrightness = 1F;
- getWindow().setAttributes(layout);
- setContentView(R.layout.activity_flash);
- background_layout = (RelativeLayout) findViewById(R.id.background);
- final View contentView = findViewById(R.id.controls);
- // Set up an instance of SystemUiHider to control the system UI for
- // this activity.
- mSystemUiHider = SystemUiHider.getInstance(this, contentView,
- HIDER_FLAGS);
- mSystemUiHider.setup();
- mSystemUiHider
- .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
- // Cached values.
- public void onVisibilityChange(boolean visible) {
- if (visible && AUTO_HIDE) {
- // Schedule a hide().
- delayedHide(AUTO_HIDE_DELAY_MILLIS);
- }
- }
- });
- // Set up the user interaction to manually show or hide the system UI.
- contentView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (TOGGLE_ON_CLICK && AUTO_HIDE) {
- mSystemUiHider.toggle();
- } else {
- mSystemUiHider.show();
- }
- }
- });
- mSystemUiHider.hide();
- View v = new View(this);
- v.postDelayed(new Runnable() {
- public void run() {
- mSystemUiHider.show();
- }
- }, 100);
- }
- /**
- * Touch listener to use for in-layout UI controls to delay hiding the
- * system UI. This is to prevent the jarring behavior of controls going away
- * while interacting with activity UI.
- */
- View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
- @Override
- public boolean onTouch(View view, MotionEvent motionEvent) {
- if (AUTO_HIDE) {
- delayedHide(AUTO_HIDE_DELAY_MILLIS);
- }
- return false;
- }
- };
- Handler mHideHandler = new Handler();
- Runnable mHideRunnable = new Runnable() {
- @Override
- public void run() {
- if (AUTO_HIDE) {
- mSystemUiHider.hide();
- }
- }
- };
- /**
- * Schedules a call to hide() in [delay] milliseconds, canceling any
- * previously scheduled calls.
- */
- private void delayedHide(int delayMillis) {
- mHideHandler.removeCallbacks(mHideRunnable);
- mHideHandler.postDelayed(mHideRunnable, delayMillis);
- }
- @Override
- public void onStart() {
- super.onStart();
- PENDING_EXPLOSION = false;
- findViewById(R.id.flashStart).setVisibility(View.VISIBLE);
- background_layout.setBackgroundColor(getResources().getColor(
- android.R.color.black));
- }
- @Override
- protected void onStop() {
- super.onStop();
- PARTY_IS_OVER = true;
- if (mp != null) {
- if (mp.isPlaying()) {
- mp.stop();
- }
- mp.release();
- mp = null;
- }
- if (mp_final != null) {
- if (mp_final.isPlaying()) {
- mp_final.stop();
- }
- mp_final.release();
- mp_final = null;
- }
- if (PENDING_EXPLOSION) {
- Toast.makeText(this, "Danger avoided, for now", Toast.LENGTH_SHORT)
- .show();
- }
- finish();
- }
- @Override
- public void onBackPressed() {
- if (PENDING_EXPLOSION) {
- Toast.makeText(this, "It's too late now, take cover!",
- Toast.LENGTH_SHORT).show();
- } else {
- finish();
- }
- }
- public void start(final View v) {
- AUTO_HIDE = true;
- mSystemUiHider.hide();
- Animation img_out_anim = AnimationUtils.loadAnimation(this,
- R.anim.img_out);
- v.startAnimation(img_out_anim);
- v.postDelayed(new Runnable() {
- public void run() {
- v.setVisibility(View.GONE);
- }
- }, 299);
- mp = MediaPlayer.create(this, R.raw.pull_pin_and_bounce);
- mp.start();
- PENDING_EXPLOSION = true;
- TOGGLE_ON_CLICK = false;
- int time = getIntent().getIntExtra("com.simon.flashbang.EXTRA1", 7);
- int time_final = time * 1000;
- v.postDelayed(new Runnable() {
- public void run() {
- if (PENDING_EXPLOSION) {
- if (!PARTY_IS_OVER) {
- WindowManager.LayoutParams layout = getWindow()
- .getAttributes();
- layout.screenBrightness = 1F;
- getWindow().setAttributes(layout);
- playFinal();
- PENDING_EXPLOSION = false;
- }
- background_layout.setBackgroundColor(getResources()
- .getColor(android.R.color.white));
- }
- PENDING_EXPLOSION = false;
- }
- }, time_final);
- }
- public void beginStopCountdown() {
- View v = new View(this);
- v.postDelayed(new Runnable() {
- public void run() {
- background_layout.setBackgroundColor(getResources().getColor(
- android.R.color.black));
- mSystemUiHider.show();
- findViewById(R.id.returnbtn).setVisibility(View.VISIBLE);
- PENDING_EXPLOSION = false;
- AUTO_HIDE = false;
- if (mp_final.isPlaying()) {
- mp_final.stop();
- }
- mp_final.release();
- mp_final = null;
- }
- }, 9000);
- }
- public void returnclick(View v) {
- PARTY_IS_OVER = true;
- if (mp != null) {
- if (mp.isPlaying()) {
- mp.stop();
- }
- mp.release();
- mp = null;
- }
- if (mp_final != null) {
- if (mp_final.isPlaying()) {
- mp_final.stop();
- }
- mp_final.release();
- mp_final = null;
- }
- finish();
- }
- public void playFinal() {
- mp_final = MediaPlayer.create(this, R.raw.explosion);
- mp_final.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement