pedja

Untitled

Apr 29th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. package com.preezm.preezm;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.content.res.AssetFileDescriptor;
  6. import android.graphics.SurfaceTexture;
  7. import android.media.MediaPlayer;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.text.TextUtils;
  12. import android.view.Surface;
  13. import android.view.TextureView;
  14. import android.view.ViewGroup;
  15.  
  16. import com.crashlytics.android.Crashlytics;
  17. import com.preezm.preezm.model.Country;
  18. import com.preezm.preezm.model.User;
  19.  
  20. import java.io.IOException;
  21.  
  22. /**
  23. * Created by pedja on 1.4.15. 11.19.
  24. * This class is part of the Preezm
  25. * Copyright © 2015 ${OWNER}
  26. */
  27. public class SplashActivity extends AbsActivity implements Runnable, TextureView.SurfaceTextureListener
  28. {
  29. public static final int SPLASH_DELAY = 3000;
  30.  
  31. Handler handler;
  32. private MediaPlayer mMediaPlayer;
  33. TextureView tvIntro;
  34.  
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState)
  37. {
  38. super.onCreate(savedInstanceState);
  39. //debug
  40. //finish();
  41. //startActivity(new Intent(this, ContactImportActivity.class));
  42. //debug end
  43. setContentView(R.layout.activity_splash);
  44.  
  45. handler = new Handler();
  46.  
  47. tvIntro = (TextureView) findViewById(R.id.tvIntro);
  48. tvIntro.setSurfaceTextureListener(this);
  49. logUsersCountryAsync();
  50. }
  51.  
  52. private void logUsersCountryAsync()
  53. {
  54. new AsyncTask<Void, Void, Void>()
  55. {
  56.  
  57. @Override
  58. protected Void doInBackground(Void... params)
  59. {
  60. Country.findUsersCountry();
  61. return null;
  62. }
  63. }.execute();
  64. }
  65.  
  66. @Override
  67. protected void onResume()
  68. {
  69. handler.postDelayed(this, SPLASH_DELAY);
  70. super.onResume();
  71. }
  72.  
  73. @Override
  74. protected void onStop()
  75. {
  76. super.onStop();
  77. handler.removeCallbacks(this);
  78. if(mMediaPlayer != null)
  79. {
  80. mMediaPlayer.pause();
  81. }
  82. }
  83.  
  84. @Override
  85. public void run()
  86. {
  87. continueSignUpProcess(this, false);
  88. }
  89.  
  90. public static void continueSignUpProcess(Activity activity, boolean skipSignup)
  91. {
  92. activity.finish();
  93. //sign-up cases:
  94. //1. StartActivity - email in user object is null or empty
  95. //2. DialogVerificationCode - token is null in user object
  96. //3. ECardEditActivity (personal) - user object doesn't have any ecards
  97. //4. PreezmIDCreationActivity - user object doesn't have preezm id
  98. //5. MainActivity - all of the above is if-else, this last case is else. is this ok?
  99. User user = User.get();
  100. if(TextUtils.isEmpty(user.getEmail()) && !skipSignup)
  101. {
  102. //1.
  103. activity.startActivity(new Intent(activity, StartActivity.class));
  104. }
  105. else if(TextUtils.isEmpty(user.getToken()))
  106. {
  107. //2.
  108. //start sign-up activity and show verification code dialog
  109. activity.startActivity(new Intent(activity, SignUpActivity.class).setAction(SignUpActivity.INTENT_ACTION_SHOW_VERIFICATION_DIALOG));
  110. }
  111. else if(user.getEcards().isEmpty())
  112. {
  113. //3.
  114. ECardEditActivity.start(activity, ECardEditActivity.AFTER_SAVE_ACTION_ADD_PROFESSIONAL, -1, true, -1, false);
  115. }
  116. else if(TextUtils.isEmpty(user.getPreezm_id()))
  117. {
  118. //4.
  119. activity.startActivity(new Intent(activity, PreezmIDCreationActivity.class));
  120. }
  121. else
  122. {
  123. //5.
  124. MainActivity.start(activity);
  125. }
  126. }
  127.  
  128. @Override
  129. public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height)
  130. {
  131. Surface surface = new Surface(surfaceTexture);
  132.  
  133. try
  134. {
  135. AssetFileDescriptor afd = getAssets().openFd("splash_video.mp4");
  136. mMediaPlayer = new MediaPlayer();
  137. mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
  138. mMediaPlayer.setSurface(surface);
  139. mMediaPlayer.setLooping(false);
  140. mMediaPlayer.prepareAsync();
  141.  
  142. // Play video when the media source is ready for playback.
  143. mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
  144. {
  145. @Override
  146. public void onPrepared(MediaPlayer mediaPlayer)
  147. {
  148. int surfaceWidth = tvIntro.getWidth();
  149. int surfaceHeight = tvIntro.getHeight();
  150.  
  151. float videoWidth = mediaPlayer.getVideoWidth();
  152. float videoHeight = mediaPlayer.getVideoHeight();
  153.  
  154. float ratio_width = surfaceWidth / videoWidth;
  155. float ratio_height = surfaceHeight / videoHeight;
  156. float aspect = videoWidth / videoHeight;
  157.  
  158. ViewGroup.LayoutParams layoutParams = tvIntro.getLayoutParams();
  159.  
  160. if (ratio_width < ratio_height)
  161. {
  162. layoutParams.width = (int) (surfaceHeight * aspect);
  163. layoutParams.height = surfaceHeight;
  164. }
  165. else
  166. {
  167. layoutParams.width = surfaceWidth;
  168. layoutParams.height = (int) (surfaceWidth / aspect);
  169. }
  170.  
  171. tvIntro.setLayoutParams(layoutParams);
  172. mediaPlayer.start();
  173. }
  174. });
  175. }
  176. catch (IOException e)
  177. {
  178. if (BuildConfig.DEBUG) e.printStackTrace();
  179. Crashlytics.logException(e);
  180. }
  181. }
  182.  
  183. @Override
  184. public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
  185. {
  186.  
  187. }
  188.  
  189. @Override
  190. public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
  191. {
  192. return false;
  193. }
  194.  
  195. @Override
  196. public void onSurfaceTextureUpdated(SurfaceTexture surface)
  197. {
  198.  
  199. }
  200.  
  201. @Override
  202. protected void onDestroy()
  203. {
  204. super.onDestroy();
  205. if (mMediaPlayer != null)
  206. {
  207. mMediaPlayer.stop();
  208. mMediaPlayer.release();
  209. mMediaPlayer = null;
  210. }
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment