Updated support lib

Can now silence alarm from any instance of the ViewDeparturesActivity
This commit is contained in:
Doug Keen 2012-09-18 13:47:42 -07:00
parent b6291ec4bd
commit f89ba82d92
5 changed files with 72 additions and 39 deletions

View File

@ -5,6 +5,7 @@
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/> <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="lib" path="libs/commons-io-2.0.1.jar"/> <classpathentry kind="lib" path="libs/commons-io-2.0.1.jar"/>
<classpathentry kind="lib" path="libs/commons-lang3-3.1.jar"/> <classpathentry kind="lib" path="libs/commons-lang3-3.1.jar"/>
<classpathentry kind="lib" path="libs/android-support-v4.jar"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/> <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="output" path="bin/classes"/> <classpathentry kind="output" path="bin/classes"/>
</classpath> </classpath>

BIN
libs/android-support-v4.jar Normal file

Binary file not shown.

View File

@ -1,20 +1,25 @@
package com.dougkeen.bart; package com.dougkeen.bart;
import com.dougkeen.bart.model.Departure;
import android.app.Application; import android.app.Application;
import android.media.MediaPlayer;
import com.dougkeen.bart.model.Departure;
public class BartRunnerApplication extends Application { public class BartRunnerApplication extends Application {
private Departure mBoardedDeparture; private Departure mBoardedDeparture;
private boolean playAlarmRingtone; private boolean mPlayAlarmRingtone;
private boolean mAlarmSounding;
private MediaPlayer mAlarmMediaPlayer;
public boolean shouldPlayAlarmRingtone() { public boolean shouldPlayAlarmRingtone() {
return playAlarmRingtone; return mPlayAlarmRingtone;
} }
public void setPlayAlarmRingtone(boolean playAlarmRingtone) { public void setPlayAlarmRingtone(boolean playAlarmRingtone) {
this.playAlarmRingtone = playAlarmRingtone; this.mPlayAlarmRingtone = playAlarmRingtone;
} }
public Departure getBoardedDeparture() { public Departure getBoardedDeparture() {
@ -24,4 +29,21 @@ public class BartRunnerApplication extends Application {
public void setBoardedDeparture(Departure boardedDeparture) { public void setBoardedDeparture(Departure boardedDeparture) {
this.mBoardedDeparture = 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;
}
} }

View File

@ -166,7 +166,7 @@ public class NotificationService extends Service implements EtdServiceListener {
cancelAlarm(); cancelAlarm();
if (mAlertLeadTime > 0) { if (mAlertLeadTime > 0) {
long alertTime = getAlertClockTime(); long alertTime = getAlarmClockTime();
if (alertTime > System.currentTimeMillis()) { if (alertTime > System.currentTimeMillis()) {
refreshAlarmPendingIntent(); refreshAlarmPendingIntent();
mAlarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, mAlarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
@ -182,7 +182,7 @@ public class NotificationService extends Service implements EtdServiceListener {
SystemClock.elapsedRealtime() + 100, mAlarmPendingIntent); SystemClock.elapsedRealtime() + 100, mAlarmPendingIntent);
} }
private long getAlertClockTime() { private long getAlarmClockTime() {
final Departure boardedDeparture = ((BartRunnerApplication) getApplication()) final Departure boardedDeparture = ((BartRunnerApplication) getApplication())
.getBoardedDeparture(); .getBoardedDeparture();
return boardedDeparture.getMeanEstimate() - mAlertLeadTime * 60 * 1000; return boardedDeparture.getMeanEstimate() - mAlertLeadTime * 60 * 1000;
@ -202,16 +202,16 @@ public class NotificationService extends Service implements EtdServiceListener {
.getMeanSecondsLeft() || boardedDeparture .getMeanSecondsLeft() || boardedDeparture
.getUncertaintySeconds() != departure .getUncertaintySeconds() != departure
.getUncertaintySeconds())) { .getUncertaintySeconds())) {
long initialAlertClockTime = getAlertClockTime(); long initialAlertClockTime = getAlarmClockTime();
boardedDeparture.mergeEstimate(departure); boardedDeparture.mergeEstimate(departure);
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
if (initialAlertClockTime > now final long newAlarmClockTime = getAlarmClockTime();
&& getAlertClockTime() <= System.currentTimeMillis()) { if (initialAlertClockTime > now && newAlarmClockTime <= now) {
// Alert time was changed to the past // Alert time was changed to the past
triggerAlarmImmediately(); triggerAlarmImmediately();
} else if (getAlertClockTime() > System.currentTimeMillis()) { } else if (newAlarmClockTime > now) {
// Alert time is still in the future // Alert time is still in the future
setAlarm(); setAlarm();
} }
@ -299,9 +299,11 @@ public class NotificationService extends Service implements EtdServiceListener {
mStationPair.getOrigin().shortName + " to " mStationPair.getOrigin().shortName + " to "
+ mStationPair.getDestination().shortName) + mStationPair.getDestination().shortName)
.setContentText(minutesText + " until departure") .setContentText(minutesText + " until departure")
.setSubText(
"Alert " + mAlertLeadTime + " minutes before departure")
.setContentIntent(mNotificationIntent).setWhen(0); .setContentIntent(mNotificationIntent).setWhen(0);
mNotificationManager.notify(DEPARTURE_NOTIFICATION_ID, mNotificationManager.notify(DEPARTURE_NOTIFICATION_ID,
notificationBuilder.getNotification()); notificationBuilder.build());
} }
private int getPollIntervalMillis() { private int getPollIntervalMillis() {

View File

@ -188,38 +188,12 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (((BartRunnerApplication) getApplication()) final BartRunnerApplication bartRunnerApplication = (BartRunnerApplication) getApplication();
.shouldPlayAlarmRingtone()) { if (bartRunnerApplication.shouldPlayAlarmRingtone()) {
soundTheAlarm(); soundTheAlarm();
} }
}
private MediaPlayer mMediaPlayer;
private void soundTheAlarm() {
Uri alertSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alertSound == null || !tryToPlayRingtone(alertSound)) {
alertSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alertSound == null || !tryToPlayRingtone(alertSound)) {
alertSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
if (mMediaPlayer == null) {
tryToPlayRingtone(alertSound);
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
silenceAlarm();
}
}, 20000);
((BartRunnerApplication) getApplication()).setPlayAlarmRingtone(false);
if (bartRunnerApplication.isAlarmSounding()) {
Builder builder = new AlertDialog.Builder(this); Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.train_alert_text) builder.setMessage(R.string.train_alert_text)
.setCancelable(false) .setCancelable(false)
@ -233,22 +207,56 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements
} }
}).show(); }).show();
} }
}
private void soundTheAlarm() {
final BartRunnerApplication application = (BartRunnerApplication) getApplication();
Uri alertSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alertSound == null || !tryToPlayRingtone(alertSound)) {
alertSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alertSound == null || !tryToPlayRingtone(alertSound)) {
alertSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
if (application.getAlarmMediaPlayer() == null) {
tryToPlayRingtone(alertSound);
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
silenceAlarm();
}
}, 20000);
application.setPlayAlarmRingtone(false);
application.setAlarmSounding(true);
}
private boolean tryToPlayRingtone(Uri alertSound) { private boolean tryToPlayRingtone(Uri alertSound) {
mMediaPlayer = MediaPlayer.create(this, alertSound); MediaPlayer mediaPlayer = MediaPlayer.create(this, alertSound);
if (mMediaPlayer == null) if (mediaPlayer == null)
return false; return false;
mMediaPlayer.setLooping(true); mediaPlayer.setLooping(true);
mMediaPlayer.start(); mediaPlayer.start();
((BartRunnerApplication) getApplication())
.setAlarmMediaPlayer(mediaPlayer);
return true; return true;
} }
private void silenceAlarm() { private void silenceAlarm() {
final BartRunnerApplication application = (BartRunnerApplication) getApplication();
final MediaPlayer mediaPlayer = application.getAlarmMediaPlayer();
application.setAlarmSounding(false);
application.setAlarmMediaPlayer(null);
try { try {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mMediaPlayer.stop(); mediaPlayer.stop();
mMediaPlayer.release(); mediaPlayer.release();
mMediaPlayer = null;
} }
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
Log.e(Constants.TAG, Log.e(Constants.TAG,