diff --git a/HypoAlarm/src/main/AndroidManifest.xml b/HypoAlarm/src/main/AndroidManifest.xml index 55165fc..ea8a7ac 100644 --- a/HypoAlarm/src/main/AndroidManifest.xml +++ b/HypoAlarm/src/main/AndroidManifest.xml @@ -50,6 +50,8 @@ + + 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 e02fd6d..99cb422 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmKlaxon.java @@ -27,10 +27,7 @@ public class AlarmKlaxon { /** * * 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 prevent alarm from being ignored or snoozing if a call comes in? * */ @@ -40,6 +37,7 @@ public class AlarmKlaxon { telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); initialCallState = telephonyManager.getCallState(); + boolean inTelephoneCall = initialCallState != TelephonyManager.CALL_STATE_IDLE; phoneStateListener = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String ignored) { @@ -49,12 +47,10 @@ public class AlarmKlaxon { // 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... @@ -65,7 +61,7 @@ public class AlarmKlaxon { mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { - Log.e("AlarmAlertActivity", "Error occurred while playing audio. Stopping alarm."); + Log.e("AlarmKlaxon", "Error occurred while playing audio. Stopping alarm."); stopAudio(context); return true; } @@ -90,10 +86,9 @@ public class AlarmKlaxon { startAudio(context); } catch (Exception ex2) { // At this point we just don't play anything. - Log.e("AlarmAlertActivity", "Failed to play fallback ringtone", ex2); + Log.e("AlarmKlaxon", "Failed to play fallback ringtone", ex2); } } - } public static void stop(final Context context) { @@ -138,5 +133,4 @@ public class AlarmKlaxon { afd.close(); } } - } diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java index b1b74f2..d381f7d 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java @@ -17,11 +17,6 @@ import android.widget.Toast; import java.io.IOException; import java.util.Calendar; - -/** - * TODO change alarm state if a phone call comes in - */ - public class AlarmReceiver extends BroadcastReceiver { private static final int SNOOZE_TIME = 1000*60; //1000*60*5; // Snooze for 5 minutes if need be private static final int ALERT_LIFE = 1000*10; //TODO 1000*60*2; // 2 minutes @@ -52,44 +47,38 @@ public class AlarmReceiver extends BroadcastReceiver { // Cancel the pre-alarm notification, if it exists context.stopService(new Intent(context, PreAlarmNotify.class)); - // TODO: delay the alarm if user is in a phone call, until the phone call ends + // If dialing, active in a phone call, or on hold, don't bother with the alarm, just reset it for tomorrow telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); - // If in a phone call, delay the alarm until the call ends - while (telephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - Log.e("AlarmReceiver", "Error sleeping: " + e.toString()); + if (telephonyManager.getCallState() != TelephonyManager.CALL_STATE_OFFHOOK) { + + // Set a grace period alarm to send SMS + Calendar graceCal = Calendar.getInstance(); + graceCal.set(Calendar.SECOND, 0); + graceCal.add(Calendar.MINUTE, gracePeriod); + graceManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + Intent graceIntent = new Intent(context, GraceReceiver.class); + gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0); + graceManager.cancel(gracePendingIntent); + if (Build.VERSION.SDK_INT >= 19) { + graceManager.setExact(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent); + } else { + graceManager.set(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent); } + Log.d("AlarmReceiver", "Setting grace alarm for " + MainActivity.debugDate(graceCal)); + + // Calculate when the grace period (converted from minutes to milliseconds) ends + graceEndTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000); + + // Set up intents for later use + notifyIntent = new Intent(context, AlarmNotify.class); + alertActivityIntent = new Intent(context, AlarmAlertActivity.class); + alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | + Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + + // Allow user to acknowledge alarm and cancel grace alarm + startAlert(context); } - // Set a grace period alarm to send SMS - Calendar graceCal = Calendar.getInstance(); - graceCal.set(Calendar.SECOND, 0); - graceCal.add(Calendar.MINUTE, gracePeriod); - graceManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - Intent graceIntent = new Intent(context, GraceReceiver.class); - gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0); - graceManager.cancel(gracePendingIntent); - if (Build.VERSION.SDK_INT >= 19) { - graceManager.setExact(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent); - } else { - graceManager.set(AlarmManager.RTC_WAKEUP, graceCal.getTimeInMillis(), gracePendingIntent); - } - Log.d("AlarmReceiver", "Setting grace alarm for " + MainActivity.debugDate(graceCal)); - - // Calculate when the grace period (converted from minutes to milliseconds) ends - graceEndTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000); - - // Set up intents for later use - notifyIntent = new Intent(context, AlarmNotify.class); - alertActivityIntent = new Intent(context, AlarmAlertActivity.class); - alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | - Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); - - // Allow user to acknowledge alarm and cancel grace alarm - startAlert(context); - // Reset for tomorrow; as of API 19, setRepeating() is inexact, so we use setExact() Calendar cal = MainActivity.TimeStringToCalendar(alarmTimeStr); // Advance the calendar to tomorrow diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java index 8d22866..61dbeb3 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java @@ -56,8 +56,6 @@ import java.util.regex.Pattern; // More than one phone number? // Alerts via Whatsapp and other protocols? -// TODO: klaxon - public class MainActivity extends ActionBarActivity { public static int ALARM_REQUEST = 1; public static int GRACE_REQUEST = 2;