/src/com/sector67/space/LaunchActivity.java

https://github.com/gerges/apollo67 · Java · 143 lines · 110 code · 26 blank · 7 comment · 11 complexity · 20bfa4cac78885da88ac4755007c7989 MD5 · raw file

  1. package com.sector67.space;
  2. import java.util.Locale;
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5. import android.app.Activity;
  6. import android.app.AlarmManager;
  7. import android.app.PendingIntent;
  8. import android.content.BroadcastReceiver;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.IntentFilter;
  12. import android.media.MediaPlayer;
  13. import android.os.Bundle;
  14. import android.os.PowerManager;
  15. import android.os.SystemClock;
  16. import android.speech.tts.TextToSpeech;
  17. import android.util.Log;
  18. import android.view.Window;
  19. import com.sector67.space.service.CamcorderReciever;
  20. import com.sector67.space.service.CameraReciever;
  21. import com.sector67.space.service.LocationService;
  22. public class LaunchActivity extends Activity implements TextToSpeech.OnInitListener {
  23. private PendingIntent mLocationAlarmSender;
  24. private PendingIntent mCamcorderSender;
  25. private static PowerManager.WakeLock wakeLock;
  26. private BroadcastReceiver locationReciever;
  27. private TextToSpeech mTts;
  28. private boolean speechReady;
  29. public LaunchActivity() {
  30. }
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. requestWindowFeature(Window.FEATURE_NO_TITLE);
  34. setContentView(R.layout.launch);
  35. Log.d(LaunchActivity.class.getName(), "Entering Launch Activity");
  36. mTts = new TextToSpeech(this, this);
  37. Intent camcorderIntent = new Intent(getBaseContext(), CamcorderReciever.class);
  38. camcorderIntent.putExtra("timeToRecord", 300*1000);
  39. mCamcorderSender = PendingIntent.getBroadcast(getBaseContext(), 0, camcorderIntent, 0);
  40. mLocationAlarmSender = PendingIntent.getService(LaunchActivity.this,
  41. 0, new Intent(LaunchActivity.this, LocationService.class), 0);
  42. //Wait for the right moment
  43. long firstTime = SystemClock.elapsedRealtime() + 31000;
  44. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  45. am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 60*1000, mLocationAlarmSender);
  46. am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 300*1000, mCamcorderSender);
  47. MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.launch_countdown);
  48. mPlayer.start();
  49. Timer timer = new Timer();
  50. timer.schedule( new TimerTask(){
  51. public void run() {
  52. nextActivity();
  53. }
  54. }, 331*1000);
  55. //Register for location updates
  56. IntentFilter locationFilter;
  57. locationFilter = new IntentFilter(LocationService.LOCATION_UPDATE);
  58. locationReciever = new LocationServiceReciever();
  59. registerReceiver(locationReciever, locationFilter);
  60. }
  61. protected void onResume() {
  62. super.onResume();
  63. }
  64. public void onDestroy() {
  65. super.onDestroy();
  66. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
  67. am.cancel(mCamcorderSender);
  68. }
  69. private void stopCameraAndCamcorder() {
  70. //Stop Camcorder
  71. Intent stopCamcorderReciever = new Intent(LaunchActivity.this, CamcorderReciever.class);
  72. stopCamcorderReciever.putExtra("action", "stop");
  73. sendBroadcast(stopCamcorderReciever);
  74. //Stop Camera
  75. Intent stopCameraReciever = new Intent(LaunchActivity.this, CameraReciever.class);
  76. stopCameraReciever.putExtra("action", "stop");
  77. sendBroadcast(stopCameraReciever);
  78. }
  79. private void nextActivity() {
  80. //aquire wakelock
  81. PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
  82. wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK| PowerManager.ON_AFTER_RELEASE, LaunchActivity.class.getName());
  83. wakeLock.acquire();
  84. Intent spaceIntent = new Intent(getBaseContext(), SpaceActivity.class);
  85. stopCameraAndCamcorder();
  86. startActivity(spaceIntent);
  87. finish();
  88. //release wakelock
  89. if (wakeLock != null) wakeLock.release();
  90. }
  91. public class LocationServiceReciever extends BroadcastReceiver {
  92. @Override
  93. public void onReceive(Context context, Intent intent) {
  94. double altitude = intent.getDoubleExtra(LocationService.ALTITUDE, 0);
  95. double longitude = intent.getDoubleExtra(LocationService.LONGITUDE, 0);
  96. double lattitude = intent.getDoubleExtra(LocationService.LATTITUDE, 0);
  97. String message = "loc, longitude " + Double.toString(longitude) + ", lattitude " + Double.toString(lattitude) + ", altitude " + Double.toString(altitude);
  98. if(null != mTts && speechReady) {
  99. mTts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
  100. }
  101. }
  102. }
  103. @Override
  104. public void onInit(int status) {
  105. if (status == TextToSpeech.SUCCESS) {
  106. int result = mTts.setLanguage(Locale.US);
  107. if (result == TextToSpeech.LANG_MISSING_DATA ||
  108. result == TextToSpeech.LANG_NOT_SUPPORTED) {
  109. Log.e(SpaceActivity.class.getName(), "Language is not available.");
  110. }
  111. speechReady = true;
  112. } else {
  113. // Initialization failed.
  114. Log.e(SpaceActivity.class.getName(), "Could not initialize TextToSpeech.");
  115. }
  116. }
  117. }