Updated support lib
Can now silence alarm from any instance of the ViewDeparturesActivity
This commit is contained in:
parent
b6291ec4bd
commit
f89ba82d92
@ -5,6 +5,7 @@
|
||||
<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-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 kind="output" path="bin/classes"/>
|
||||
</classpath>
|
||||
|
BIN
libs/android-support-v4.jar
Normal file
BIN
libs/android-support-v4.jar
Normal file
Binary file not shown.
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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() {
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user