PageRenderTime 38ms CodeModel.GetById 2ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

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