/remindme/src/com/google/marvin/remindme/RemindMe.java

http://eyes-free.googlecode.com/ · Java · 154 lines · 125 code · 22 blank · 7 comment · 25 complexity · 14497a07817915f48ddea861a57e50c7 MD5 · raw file

  1. package com.google.marvin.remindme;
  2. import com.google.tts.TTS;
  3. import java.util.Calendar;
  4. import android.app.Activity;
  5. import android.app.AlarmManager;
  6. import android.app.PendingIntent;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.SharedPreferences;
  10. import android.content.SharedPreferences.Editor;
  11. import android.media.AudioManager;
  12. import android.media.MediaPlayer;
  13. import android.media.MediaPlayer.OnCompletionListener;
  14. import android.net.Uri;
  15. import android.os.Bundle;
  16. import android.preference.PreferenceManager;
  17. /**
  18. * An eyes-free, talking reminder alarm
  19. *
  20. * @author clchen@google.com (Charles L. Chen)
  21. */
  22. public class RemindMe extends Activity {
  23. public TTS tts;
  24. public NumberEntryView numberEntryView;
  25. public RecordReminderView recordReminderView;
  26. public ConfirmationView confirmationView;
  27. public boolean alreadyQuit;
  28. private long triggerTime;
  29. private TTS.InitListener ttsInitListener = new TTS.InitListener() {
  30. public void onInit(int version) {
  31. showNumberEntryView();
  32. }
  33. };
  34. /** Called when the activity is first created. */
  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setVolumeControlStream(AudioManager.STREAM_MUSIC);
  39. tts = new TTS(this, ttsInitListener, true);
  40. alreadyQuit = false;
  41. }
  42. public void showNumberEntryView() {
  43. dismissViews();
  44. numberEntryView = new NumberEntryView(this);
  45. setContentView(numberEntryView);
  46. tts.speak("Remind you at?", 0, null);
  47. }
  48. private boolean validTime(String timeStr) {
  49. if (timeStr.length() != 4) {
  50. return false;
  51. }
  52. if ((timeStr.charAt(0) != '0') && (timeStr.charAt(0) != '1') && (timeStr.charAt(0) != '2')) {
  53. return false;
  54. }
  55. if ((timeStr.charAt(2) != '0') && (timeStr.charAt(2) != '1') && (timeStr.charAt(2) != '2')
  56. && (timeStr.charAt(2) != '3') && (timeStr.charAt(2) != '4') && (timeStr.charAt(2) != '5')) {
  57. return false;
  58. }
  59. return true;
  60. }
  61. public void setTime(String timeStr) {
  62. if (!validTime(timeStr)) {
  63. numberEntryView.reset();
  64. return;
  65. }
  66. Calendar cal = Calendar.getInstance();
  67. int hour = Integer.parseInt(timeStr.substring(0,2));
  68. int minute = Integer.parseInt(timeStr.substring(2));
  69. cal.setTimeInMillis(System.currentTimeMillis());
  70. cal.set(Calendar.HOUR_OF_DAY, hour);
  71. cal.set(Calendar.MINUTE, minute);
  72. cal.set(Calendar.SECOND, 0);
  73. triggerTime = cal.getTimeInMillis();
  74. if (triggerTime < System.currentTimeMillis()){
  75. triggerTime = triggerTime + 86400000; //Add 24 hours
  76. }
  77. // Set the alarm here using AlarmManager
  78. showRecordingView();
  79. }
  80. public void showRecordingView() {
  81. dismissViews();
  82. recordReminderView = new RecordReminderView(this);
  83. setContentView(recordReminderView);
  84. tts.speak("Touch the screen to begin recording; lift your finger up when you are done.", 0, null);
  85. }
  86. public void showConfirmationView() {
  87. dismissViews();
  88. confirmationView = new ConfirmationView(this);
  89. setContentView(confirmationView);
  90. new MediaPlayer();
  91. MediaPlayer mplayer = MediaPlayer.create(this, Uri.parse("/sdcard/remindme/note00.amr"));
  92. mplayer.setOnCompletionListener(new OnCompletionListener(){
  93. public void onCompletion(MediaPlayer arg0) {
  94. Calendar cal = Calendar.getInstance();
  95. cal.setTimeInMillis(triggerTime);
  96. int hour = cal.get(Calendar.HOUR_OF_DAY);
  97. int min = cal.get(Calendar.MINUTE);
  98. if (alreadyQuit){
  99. return;
  100. }
  101. tts.speak("will be set for " + hour + " " + min + " Press call to confirm, back to cancel.", 0, null);
  102. }
  103. });
  104. mplayer.start();
  105. }
  106. public void confirmAlarm(){
  107. alreadyQuit = true;
  108. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  109. Editor editor = prefs.edit();
  110. editor.commit();
  111. AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  112. int flags = Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY;
  113. Intent intent = new Intent(this, ReminderSpeakerActivity.class);
  114. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  115. PendingIntent speakerIntent = PendingIntent.getActivity(this, 0, intent, flags);
  116. am.set(AlarmManager.RTC_WAKEUP, triggerTime, speakerIntent);
  117. tts.stop();
  118. finish();
  119. }
  120. private void dismissViews() {
  121. if (numberEntryView != null) {
  122. numberEntryView.shutdown();
  123. }
  124. numberEntryView = null;
  125. }
  126. @Override
  127. protected void onDestroy(){
  128. dismissViews();
  129. tts.shutdown();
  130. super.onDestroy();
  131. }
  132. }