diff --git a/src/com/dougkeen/bart/AlarmBroadcastReceiver.java b/src/com/dougkeen/bart/AlarmBroadcastReceiver.java index 554adce..9f140db 100644 --- a/src/com/dougkeen/bart/AlarmBroadcastReceiver.java +++ b/src/com/dougkeen/bart/AlarmBroadcastReceiver.java @@ -13,7 +13,7 @@ public class AlarmBroadcastReceiver extends BroadcastReceiver { Intent targetIntent = new Intent(Intent.ACTION_VIEW, intent.getData()); targetIntent.putExtra("boardedDeparture", intent.getExtras() .getParcelable("departure")); - targetIntent.putExtra("isAlarm", true); + targetIntent.putExtra("soundAlarm", true); targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(targetIntent); diff --git a/src/com/dougkeen/bart/ViewDeparturesActivity.java b/src/com/dougkeen/bart/ViewDeparturesActivity.java index 19698e7..fdca8a1 100644 --- a/src/com/dougkeen/bart/ViewDeparturesActivity.java +++ b/src/com/dougkeen/bart/ViewDeparturesActivity.java @@ -1,16 +1,25 @@ package com.dougkeen.bart; +import java.io.IOException; import java.util.List; +import android.app.AlertDialog; +import android.app.AlertDialog.Builder; import android.content.ComponentName; +import android.content.ContentResolver; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.ServiceConnection; import android.database.Cursor; import android.graphics.Color; import android.graphics.drawable.GradientDrawable; +import android.media.MediaPlayer; +import android.media.Ringtone; +import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; import android.os.IBinder; import android.os.Parcelable; import android.support.v4.app.LoaderManager.LoaderCallbacks; @@ -66,6 +75,8 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements private EtdService mEtdService; + private Handler mHandler = new Handler(); + private boolean mBound = false; @Override @@ -189,6 +200,70 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + if (intent.getBooleanExtra("soundAlarm", false)) { + 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); + + Builder builder = new AlertDialog.Builder(this); + builder.setMessage("Your train is leaving soon!") + .setCancelable(false) + .setNeutralButton("Silence alarm", + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, + int which) { + silenceAlarm(); + dialog.dismiss(); + } + }).show(); + } + + private boolean tryToPlayRingtone(Uri alertSound) { + mMediaPlayer = MediaPlayer.create(this, alertSound); + if (mMediaPlayer == null) + return false; + mMediaPlayer.setLooping(true); + mMediaPlayer.start(); + return true; + } + + private void silenceAlarm() { + try { + if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { + mMediaPlayer.stop(); + mMediaPlayer.release(); + mMediaPlayer = null; + } + } catch (IllegalStateException e) { + Log.e(Constants.TAG, + "Couldn't stop media player; It was in an invalid state", e); + } } private void setListTitle() {