Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. package com.simon.flashbang;
  2.  
  3. import com.simon.flashbang.util.SystemUiHider;
  4. import android.app.Activity;
  5. import android.media.MediaPlayer;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.view.WindowManager;
  11. import android.view.animation.Animation;
  12. import android.view.animation.AnimationUtils;
  13. import android.widget.RelativeLayout;
  14. import android.widget.Toast;
  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 FlashActivity extends Activity {
  23.  
  24. private static boolean PARTY_IS_OVER = false;
  25.  
  26. private static boolean PENDING_EXPLOSION = false;
  27. private RelativeLayout background_layout;
  28. private MediaPlayer mp, mp_final;
  29. /**
  30. * Whether or not the system UI should be auto-hidden after
  31. * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
  32. */
  33.  
  34. private static boolean AUTO_HIDE = false;
  35.  
  36. /**
  37. * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
  38. * user interaction before hiding the system UI.
  39. */
  40. private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
  41.  
  42. /**
  43. * If set, will toggle the system UI visibility upon interaction. Otherwise,
  44. * will show the system UI visibility upon interaction.
  45. */
  46. private static boolean TOGGLE_ON_CLICK = true;
  47.  
  48. /**
  49. * The flags to pass to {@link SystemUiHider#getInstance}.
  50. */
  51. private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
  52.  
  53. /**
  54. * The instance of the {@link SystemUiHider} for this activity.
  55. */
  56. private SystemUiHider mSystemUiHider;
  57.  
  58. @Override
  59. protected void onCreate(Bundle savedInstanceState) {
  60. super.onCreate(savedInstanceState);
  61.  
  62. WindowManager.LayoutParams layout = getWindow().getAttributes();
  63. layout.screenBrightness = 1F;
  64. getWindow().setAttributes(layout);
  65.  
  66. setContentView(R.layout.activity_flash);
  67.  
  68. background_layout = (RelativeLayout) findViewById(R.id.background);
  69.  
  70. final View contentView = findViewById(R.id.controls);
  71.  
  72. // Set up an instance of SystemUiHider to control the system UI for
  73. // this activity.
  74.  
  75. mSystemUiHider = SystemUiHider.getInstance(this, contentView,
  76. HIDER_FLAGS);
  77.  
  78. mSystemUiHider.setup();
  79. mSystemUiHider
  80. .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
  81. // Cached values.
  82. public void onVisibilityChange(boolean visible) {
  83.  
  84. if (visible && AUTO_HIDE) {
  85. // Schedule a hide().
  86. delayedHide(AUTO_HIDE_DELAY_MILLIS);
  87. }
  88. }
  89. });
  90.  
  91. // Set up the user interaction to manually show or hide the system UI.
  92. contentView.setOnClickListener(new View.OnClickListener() {
  93. @Override
  94. public void onClick(View view) {
  95. if (TOGGLE_ON_CLICK && AUTO_HIDE) {
  96. mSystemUiHider.toggle();
  97. } else {
  98. mSystemUiHider.show();
  99. }
  100. }
  101. });
  102.  
  103. mSystemUiHider.hide();
  104. View v = new View(this);
  105.  
  106. v.postDelayed(new Runnable() {
  107.  
  108. public void run() {
  109. mSystemUiHider.show();
  110. }
  111. }, 100);
  112. }
  113.  
  114. /**
  115. * Touch listener to use for in-layout UI controls to delay hiding the
  116. * system UI. This is to prevent the jarring behavior of controls going away
  117. * while interacting with activity UI.
  118. */
  119. View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
  120. @Override
  121. public boolean onTouch(View view, MotionEvent motionEvent) {
  122. if (AUTO_HIDE) {
  123. delayedHide(AUTO_HIDE_DELAY_MILLIS);
  124. }
  125. return false;
  126. }
  127. };
  128.  
  129. Handler mHideHandler = new Handler();
  130. Runnable mHideRunnable = new Runnable() {
  131. @Override
  132. public void run() {
  133.  
  134. if (AUTO_HIDE) {
  135. mSystemUiHider.hide();
  136. }
  137. }
  138. };
  139.  
  140. /**
  141. * Schedules a call to hide() in [delay] milliseconds, canceling any
  142. * previously scheduled calls.
  143. */
  144. private void delayedHide(int delayMillis) {
  145. mHideHandler.removeCallbacks(mHideRunnable);
  146. mHideHandler.postDelayed(mHideRunnable, delayMillis);
  147. }
  148.  
  149. @Override
  150. public void onStart() {
  151. super.onStart();
  152.  
  153. PENDING_EXPLOSION = false;
  154. findViewById(R.id.flashStart).setVisibility(View.VISIBLE);
  155. background_layout.setBackgroundColor(getResources().getColor(
  156. android.R.color.black));
  157. }
  158.  
  159. @Override
  160. protected void onStop() {
  161. super.onStop();
  162.  
  163. PARTY_IS_OVER = true;
  164.  
  165. if (mp != null) {
  166. if (mp.isPlaying()) {
  167. mp.stop();
  168. }
  169. mp.release();
  170. mp = null;
  171. }
  172.  
  173. if (mp_final != null) {
  174. if (mp_final.isPlaying()) {
  175. mp_final.stop();
  176. }
  177. mp_final.release();
  178. mp_final = null;
  179. }
  180.  
  181. if (PENDING_EXPLOSION) {
  182. Toast.makeText(this, "Danger avoided, for now", Toast.LENGTH_SHORT)
  183. .show();
  184. }
  185.  
  186. finish();
  187.  
  188. }
  189.  
  190. @Override
  191. public void onBackPressed() {
  192. if (PENDING_EXPLOSION) {
  193. Toast.makeText(this, "It's too late now, take cover!",
  194. Toast.LENGTH_SHORT).show();
  195. } else {
  196. finish();
  197. }
  198. }
  199.  
  200. public void start(final View v) {
  201. AUTO_HIDE = true;
  202. mSystemUiHider.hide();
  203.  
  204. Animation img_out_anim = AnimationUtils.loadAnimation(this,
  205. R.anim.img_out);
  206. v.startAnimation(img_out_anim);
  207.  
  208. v.postDelayed(new Runnable() {
  209.  
  210. public void run() {
  211. v.setVisibility(View.GONE);
  212. }
  213. }, 299);
  214.  
  215. mp = MediaPlayer.create(this, R.raw.pull_pin_and_bounce);
  216. mp.start();
  217.  
  218. PENDING_EXPLOSION = true;
  219.  
  220. TOGGLE_ON_CLICK = false;
  221.  
  222. int time = getIntent().getIntExtra("com.simon.flashbang.EXTRA1", 7);
  223. int time_final = time * 1000;
  224.  
  225. v.postDelayed(new Runnable() {
  226.  
  227. public void run() {
  228.  
  229. if (PENDING_EXPLOSION) {
  230.  
  231. if (!PARTY_IS_OVER) {
  232. WindowManager.LayoutParams layout = getWindow()
  233. .getAttributes();
  234. layout.screenBrightness = 1F;
  235. getWindow().setAttributes(layout);
  236.  
  237. playFinal();
  238.  
  239. PENDING_EXPLOSION = false;
  240. }
  241.  
  242. background_layout.setBackgroundColor(getResources()
  243. .getColor(android.R.color.white));
  244. }
  245.  
  246. PENDING_EXPLOSION = false;
  247. }
  248. }, time_final);
  249. }
  250.  
  251. public void beginStopCountdown() {
  252. View v = new View(this);
  253. v.postDelayed(new Runnable() {
  254.  
  255. public void run() {
  256. background_layout.setBackgroundColor(getResources().getColor(
  257. android.R.color.black));
  258. mSystemUiHider.show();
  259. findViewById(R.id.returnbtn).setVisibility(View.VISIBLE);
  260.  
  261. PENDING_EXPLOSION = false;
  262. AUTO_HIDE = false;
  263.  
  264. if (mp_final.isPlaying()) {
  265. mp_final.stop();
  266. }
  267. mp_final.release();
  268. mp_final = null;
  269. }
  270. }, 9000);
  271. }
  272.  
  273. public void returnclick(View v) {
  274. PARTY_IS_OVER = true;
  275.  
  276. if (mp != null) {
  277. if (mp.isPlaying()) {
  278. mp.stop();
  279. }
  280. mp.release();
  281. mp = null;
  282. }
  283.  
  284. if (mp_final != null) {
  285. if (mp_final.isPlaying()) {
  286. mp_final.stop();
  287. }
  288. mp_final.release();
  289. mp_final = null;
  290. }
  291.  
  292. finish();
  293. }
  294.  
  295. public void playFinal() {
  296. mp_final = MediaPlayer.create(this, R.raw.explosion);
  297. mp_final.start();
  298.  
  299.  
  300. }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement