teaching machines

Final Project- Jacoblj

December 20, 2011 by . Filed under cs491 mobile, fall 2011, postmortems.

For my final app I decided to make an alarm clock. My goal was to make an alarm clock that ran faster than the one I am using now (I have a very slow android phone). I would like to say that I have it completely finished and that I am ready to use it, but this is not the case. I got caught up with putting in some extras that I would like and have not figured out a few of the functions that my alarm should have, such as setting multiple alarms, which it needs to do before I can use it.

To start with my app goes into a menu screen where currently it displays the time, and two buttons. One to go the the flashlight (for a flashlight I made a white screen that will show since my phone doesn’t have a flash), and one that will go to the alarm menu. The alarm menu is just a list view like we have seen before. The first item is to add new alarms which are defaulted to noon, after that are the alarms. Once an alarm is clicked on it will take you to an edit activity. In the edit activity you can set the time, set if the alarm will vibrate, select a ringtone to play for the alarm and write any notes that you want to include with the alarm. Once you save it the alarm is automatically set.

 

 

 

 

 

At first I had some issues with choosing the alarm tone. I tried to make my own list that would populate itself with the tones available on the phone. After a while of frustration and research I found the RingtoneManager. This made the list much easier. All I ended up needing to do was put a call out to the RingtoneManager using an intent and wait for it to send the list back to me.

// I started a new ringtone_picker event and sent it the information I needed along with the title of the list view it makes

Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER);

intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);

intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TITLE, “Select Tone”);

intent.putExtra( RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)null);

startActivityForResult(intent, 789);

//It makes the above alertDialog for me and then once an item is selected I save the uri and name of the item

Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

songName = intent.getStringExtra(RingtoneManager.EXTRA_RINGTONE_TITLE).toString();

if (uri != null) {

song = uri.toString();

}

 

After this I made an AlarmManager to set up the alarmManager. The alarm manager lets you pass it an intent that is to start up at sometime in the future. To do this you need to make a Calendar object that has the time to set the alarm for. I did this by using a TimePicker in my edit alarm menu and pulling the information from it.

//set the calendar cal to the alarm time

int hour = alarmTime.getCurrentHour();

int min = alarmTime.getCurrentMinute();

Date setTime = new Date();

setTime.setHours(hour);

setTime.setMinutes(min);

cal.setTime(setTime);

 

//set the intent, putting the alarm as an extra to be started once the alarm hits the correct time

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);

intent.putExtra(“alarm”, alarm);

//this code sets up the alarmManager to the correct time. The pendingIntent hold the intent to be started, the AlarmManager is then set to RTC_WAKEUP which is a value that tells it to wake the phone when the alarm goes off and allows me to pass in the desired time in millis for the system.

PendingIntent sender = PendingIntent.getBroadcast(getBaseContext(), 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

 

After the time hits I have a receiver that will be called by the AlarmManager. The receiver in turn calls my alarm Activity, which has the time as well as a snooze and stop button. Once the activity starts, before the alarm goes off I added some text to speech so my app will tell you the current time and then only after it is completed play your alarm.

First the alarm starts to vibrate, which is fairly simple to start. This gets the vibrator_service and sends it a patter to vibrate. It starts imediately and the vibrates for 700 millis and stops for 700 millis. The one in the vibrate(pattern, 1) makes it repeat.

v = (Vibrator) getSystemService(VIBRATOR_SERVICE);

long[] pattern = {

0,

700,

700

};

// Vibrate for 700 milliseconds

v.vibrate(pattern, 1);

 

// To start with I make a string with the time, this was a bit annoying because it has to be done phonetically instead of just writing it out. For instance I had to add “oh” in from of numbers under ten for the minutes.

String tTs = “The time is ” + speakTime;

//next I check to see if the phone has text to speech. I actually had to download it on mine before I used it, so I need to make the check earlier in the program so it doesn’t download instead of starting the alarm the first time. If there is no text to speech software I download it and then call the onInit () of the TextToSpeech.

Intent checkIntent = new Intent();

checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);

startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

 

// Next we need to make a hash map of key pairs. This is needed to check if the utterance is complete. I am not sure why, but it will not work without this.

We have mTts speak the string we made earlier.

HashMap<String, String> params = new HashMap<String, String>();

params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));

params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, “theUtId”);

mTts.speak(tTs, TextToSpeech.QUEUE_FLUSH, params);

 

After it is done with the string onUtteranceCompleted gets called (you need to make your activity an utteranceCompletedListener too), and then we can set up a media player and start the alarm music.

The snooze and stop buttons are pretty similar functionally. They first pause the alarm tone and mTts if they are not null, and then they stop the vibration. They will both finish the Activity. The only difference is that before snooze finishes it will call a new AlarmManger five minutes away.

All in all I am fairly impressed with how far I got on this alarm. I needed to do a fair amount of research in order to get it up and running. I still have a bunch of tweaks to make before I am ready to use it, but I have a lot of the basic ideas down already.