diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java index d8d230d..e02fd6d 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java @@ -7,6 +7,7 @@ import android.media.MediaPlayer; import android.media.RingtoneManager; import android.net.Uri; import android.os.Vibrator; +import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; @@ -18,33 +19,47 @@ public class AlarmKlaxon { private static final float IN_CALL_VOLUME = 0.125f; private static MediaPlayer mediaPlayer = null; private static TelephonyManager telephonyManager; + private static PhoneStateListener phoneStateListener; + private static int initialCallState; private static Vibrator vibrator; public static void start(final Context context) { /** * - * TODO add raw ring tone to use as fallback - * TODO add in-call ring tone + * TODO allow user to select alarm tone + * TODO add raw alarm tone to use as fallback + * TODO add in-call alarm tone * TODO lower volume if in a call * TODO cancel noise if a call comes in (add TelephonyManager listener which cancels the alert but calls the notification) * - * TODO start telephony listener - * - * TODO snooze 5 minutes on press back button or home button (new runnable) - * TODO remove back/home button and most top icons - * TODO fix glowpad/seekarc */ + vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.cancel(); vibrator.vibrate(vPattern, 0); - if (true) - return; // TODO remove after testing -- is mediaplayer responsible for delays? - telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); + initialCallState = telephonyManager.getCallState(); + phoneStateListener = new PhoneStateListener() { + @Override + public void onCallStateChanged(int state, String ignored) { + // The user might already be in a call when the alarm fires. When + // we register onCallStateChanged, we get the initial in-call state + // which kills the alarm. Check against the initial call state so + // we don't kill the alarm during a call. + if (state != TelephonyManager.CALL_STATE_IDLE && state != initialCallState) { + stopAudio(context); + // TODO stop alarm from snoozing or turning off? + } + } + }; + telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); + boolean inTelephoneCall = initialCallState != TelephonyManager.CALL_STATE_IDLE; + // TODO select alarm tone? // Use the default alarm tone... Uri alarmNoise = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); + stopAudio(context); mediaPlayer = new MediaPlayer(); mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { @@ -57,24 +72,21 @@ public class AlarmKlaxon { }); try { - /* - * TODO find out if we're in a call if (inTelephoneCall) { - Log.v("Using the in-call alarm"); - sMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME); - setDataSourceFromResource(context, sMediaPlayer, R.raw.in_call_alarm); + Log.d("AlarmKlaxon", "Using the in-call alarm"); + mediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME); + setDataSourceFromResource(context, mediaPlayer, R.raw.in_call_alarm); } else { - */ - mediaPlayer.setDataSource(context, alarmNoise); - startAudio(context); - //} + mediaPlayer.setDataSource(context, alarmNoise); + startAudio(context); + } } catch (Exception ex) { // The alarmNoise may be on the sd card which could be busy right // now. Use the fallback ringtone. try { // Reset the media player to clear the error state. mediaPlayer.reset(); - //setDataSourceFromResource(this, mediaPlayer, R.raw.fallbackring); + setDataSourceFromResource(context, mediaPlayer, R.raw.fallbackring); startAudio(context); } catch (Exception ex2) { // At this point we just don't play anything. @@ -86,9 +98,8 @@ public class AlarmKlaxon { public static void stop(final Context context) { vibrator.cancel(); - if (true) - return; // TODO remove after testing stopAudio(context); + telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); } private static void startAudio(final Context context) throws IOException { @@ -118,6 +129,7 @@ public class AlarmKlaxon { mediaPlayer = null; } } + // Load ringtone from a resource private static void setDataSourceFromResource(Context context, MediaPlayer player, int res) throws IOException { AssetFileDescriptor afd = context.getResources().openRawResourceFd(res); @@ -126,4 +138,5 @@ public class AlarmKlaxon { afd.close(); } } + } diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/GraceReceiver.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/GraceReceiver.java index 2db3a08..82f7e7c 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/GraceReceiver.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/GraceReceiver.java @@ -74,7 +74,9 @@ public class GraceReceiver extends BroadcastReceiver { private void sendText(Context context) { SmsManager sms = SmsManager.getDefault(); // TODO uncomment sendTextMessage - //sms.sendTextMessage(phoneNumber, null, message, null, null); + if (!MainActivity.HYPOALARM_DEBUG) { + sms.sendTextMessage(phoneNumber, null, message, null, null); + } Toast.makeText(context, message, Toast.LENGTH_LONG).show(); Log.d("GraceReceiver", "Sending sms to " + phoneNumber + " with message: " + message); } diff --git a/HypoAlarm/src/main/res/raw/fallbackring.ogg b/HypoAlarm/src/main/res/raw/fallbackring.ogg new file mode 100644 index 0000000..9d80037 Binary files /dev/null and b/HypoAlarm/src/main/res/raw/fallbackring.ogg differ diff --git a/HypoAlarm/src/main/res/raw/in_call_alarm.ogg b/HypoAlarm/src/main/res/raw/in_call_alarm.ogg new file mode 100644 index 0000000..9d80037 Binary files /dev/null and b/HypoAlarm/src/main/res/raw/in_call_alarm.ogg differ