diff --git a/.classpath b/.classpath index 4b421f7..1e34f60 100644 --- a/.classpath +++ b/.classpath @@ -5,6 +5,7 @@ + diff --git a/libs/android-support-v4.jar b/libs/android-support-v4.jar new file mode 100644 index 0000000..feaf44f Binary files /dev/null and b/libs/android-support-v4.jar differ diff --git a/src/com/dougkeen/bart/BartRunnerApplication.java b/src/com/dougkeen/bart/BartRunnerApplication.java index 4e6c6d6..175d81e 100644 --- a/src/com/dougkeen/bart/BartRunnerApplication.java +++ b/src/com/dougkeen/bart/BartRunnerApplication.java @@ -1,20 +1,25 @@ package com.dougkeen.bart; -import com.dougkeen.bart.model.Departure; - import android.app.Application; +import android.media.MediaPlayer; + +import com.dougkeen.bart.model.Departure; public class BartRunnerApplication extends Application { private Departure mBoardedDeparture; - private boolean playAlarmRingtone; + private boolean mPlayAlarmRingtone; + + private boolean mAlarmSounding; + + private MediaPlayer mAlarmMediaPlayer; public boolean shouldPlayAlarmRingtone() { - return playAlarmRingtone; + return mPlayAlarmRingtone; } public void setPlayAlarmRingtone(boolean playAlarmRingtone) { - this.playAlarmRingtone = playAlarmRingtone; + this.mPlayAlarmRingtone = playAlarmRingtone; } public Departure getBoardedDeparture() { @@ -24,4 +29,21 @@ public class BartRunnerApplication extends Application { public void setBoardedDeparture(Departure boardedDeparture) { this.mBoardedDeparture = boardedDeparture; } + + public boolean isAlarmSounding() { + return mAlarmSounding; + } + + public void setAlarmSounding(boolean alarmSounding) { + this.mAlarmSounding = alarmSounding; + } + + public MediaPlayer getAlarmMediaPlayer() { + return mAlarmMediaPlayer; + } + + public void setAlarmMediaPlayer(MediaPlayer alarmMediaPlayer) { + this.mAlarmMediaPlayer = alarmMediaPlayer; + } + } \ No newline at end of file diff --git a/src/com/dougkeen/bart/NotificationService.java b/src/com/dougkeen/bart/NotificationService.java index 0d43f19..530c757 100644 --- a/src/com/dougkeen/bart/NotificationService.java +++ b/src/com/dougkeen/bart/NotificationService.java @@ -166,7 +166,7 @@ public class NotificationService extends Service implements EtdServiceListener { cancelAlarm(); if (mAlertLeadTime > 0) { - long alertTime = getAlertClockTime(); + long alertTime = getAlarmClockTime(); if (alertTime > System.currentTimeMillis()) { refreshAlarmPendingIntent(); mAlarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, @@ -182,7 +182,7 @@ public class NotificationService extends Service implements EtdServiceListener { SystemClock.elapsedRealtime() + 100, mAlarmPendingIntent); } - private long getAlertClockTime() { + private long getAlarmClockTime() { final Departure boardedDeparture = ((BartRunnerApplication) getApplication()) .getBoardedDeparture(); return boardedDeparture.getMeanEstimate() - mAlertLeadTime * 60 * 1000; @@ -202,16 +202,16 @@ public class NotificationService extends Service implements EtdServiceListener { .getMeanSecondsLeft() || boardedDeparture .getUncertaintySeconds() != departure .getUncertaintySeconds())) { - long initialAlertClockTime = getAlertClockTime(); + long initialAlertClockTime = getAlarmClockTime(); boardedDeparture.mergeEstimate(departure); final long now = System.currentTimeMillis(); - if (initialAlertClockTime > now - && getAlertClockTime() <= System.currentTimeMillis()) { + final long newAlarmClockTime = getAlarmClockTime(); + if (initialAlertClockTime > now && newAlarmClockTime <= now) { // Alert time was changed to the past triggerAlarmImmediately(); - } else if (getAlertClockTime() > System.currentTimeMillis()) { + } else if (newAlarmClockTime > now) { // Alert time is still in the future setAlarm(); } @@ -299,9 +299,11 @@ public class NotificationService extends Service implements EtdServiceListener { mStationPair.getOrigin().shortName + " to " + mStationPair.getDestination().shortName) .setContentText(minutesText + " until departure") + .setSubText( + "Alert " + mAlertLeadTime + " minutes before departure") .setContentIntent(mNotificationIntent).setWhen(0); mNotificationManager.notify(DEPARTURE_NOTIFICATION_ID, - notificationBuilder.getNotification()); + notificationBuilder.build()); } private int getPollIntervalMillis() { diff --git a/src/com/dougkeen/bart/ViewDeparturesActivity.java b/src/com/dougkeen/bart/ViewDeparturesActivity.java index 7b11a0b..2920b80 100644 --- a/src/com/dougkeen/bart/ViewDeparturesActivity.java +++ b/src/com/dougkeen/bart/ViewDeparturesActivity.java @@ -188,15 +188,30 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); - if (((BartRunnerApplication) getApplication()) - .shouldPlayAlarmRingtone()) { + final BartRunnerApplication bartRunnerApplication = (BartRunnerApplication) getApplication(); + if (bartRunnerApplication.shouldPlayAlarmRingtone()) { soundTheAlarm(); } + + if (bartRunnerApplication.isAlarmSounding()) { + Builder builder = new AlertDialog.Builder(this); + builder.setMessage(R.string.train_alert_text) + .setCancelable(false) + .setNeutralButton(R.string.silence_alarm, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, + int which) { + silenceAlarm(); + dialog.dismiss(); + } + }).show(); + } } - private MediaPlayer mMediaPlayer; - private void soundTheAlarm() { + final BartRunnerApplication application = (BartRunnerApplication) getApplication(); + Uri alertSound = RingtoneManager .getDefaultUri(RingtoneManager.TYPE_ALARM); @@ -208,7 +223,7 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements .getDefaultUri(RingtoneManager.TYPE_RINGTONE); } } - if (mMediaPlayer == null) { + if (application.getAlarmMediaPlayer() == null) { tryToPlayRingtone(alertSound); } mHandler.postDelayed(new Runnable() { @@ -218,37 +233,30 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements } }, 20000); - ((BartRunnerApplication) getApplication()).setPlayAlarmRingtone(false); - - Builder builder = new AlertDialog.Builder(this); - builder.setMessage(R.string.train_alert_text) - .setCancelable(false) - .setNeutralButton(R.string.silence_alarm, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, - int which) { - silenceAlarm(); - dialog.dismiss(); - } - }).show(); + application.setPlayAlarmRingtone(false); + application.setAlarmSounding(true); } private boolean tryToPlayRingtone(Uri alertSound) { - mMediaPlayer = MediaPlayer.create(this, alertSound); - if (mMediaPlayer == null) + MediaPlayer mediaPlayer = MediaPlayer.create(this, alertSound); + if (mediaPlayer == null) return false; - mMediaPlayer.setLooping(true); - mMediaPlayer.start(); + mediaPlayer.setLooping(true); + mediaPlayer.start(); + ((BartRunnerApplication) getApplication()) + .setAlarmMediaPlayer(mediaPlayer); return true; } private void silenceAlarm() { + final BartRunnerApplication application = (BartRunnerApplication) getApplication(); + final MediaPlayer mediaPlayer = application.getAlarmMediaPlayer(); + application.setAlarmSounding(false); + application.setAlarmMediaPlayer(null); try { - if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { - mMediaPlayer.stop(); - mMediaPlayer.release(); - mMediaPlayer = null; + if (mediaPlayer != null && mediaPlayer.isPlaying()) { + mediaPlayer.stop(); + mediaPlayer.release(); } } catch (IllegalStateException e) { Log.e(Constants.TAG,