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 cbc2073..b1b74f2 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java @@ -9,9 +9,12 @@ import android.content.SharedPreferences; import android.os.Build; import android.os.Handler; import android.preference.PreferenceManager; +import android.telephony.PhoneStateListener; +import android.telephony.TelephonyManager; import android.util.Log; import android.widget.Toast; +import java.io.IOException; import java.util.Calendar; @@ -26,6 +29,7 @@ public class AlarmReceiver extends BroadcastReceiver { private static AlarmManager alarmManager, graceManager; private static PendingIntent alarmPendingIntent, gracePendingIntent; private static Intent alertActivityIntent, notifyIntent; + private static TelephonyManager telephonyManager; public static volatile String alarmStatus; // Register ALARM_DISMISSED and its brethren here public static final String ALARM_RUNNING = "ALARM_RUNNING"; public static final String ALARM_DISMISSED = "ALARM_DISMISSED"; @@ -35,16 +39,11 @@ public class AlarmReceiver extends BroadcastReceiver { public static long graceEndTime; @Override - public void onReceive(final Context context, Intent intent) { + public void onReceive(final Context context, final Intent intent) { sharedPref = PreferenceManager.getDefaultSharedPreferences(context); - Boolean alarmActive = sharedPref.getBoolean(context.getString(R.string.AlarmActivePref), true); - int gracePeriod = sharedPref.getInt(context.getString(R.string.GracePeriodPref), 60); - String alarmTimeStr = sharedPref.getString(context.getString(R.string.AlarmTimePref), null); - - // TODO remove - if (MainActivity.HYPOALARM_DEBUG) { - gracePeriod = 1; - } + final Boolean alarmActive = sharedPref.getBoolean(context.getString(R.string.AlarmActivePref), true); + final int gracePeriod = sharedPref.getInt(context.getString(R.string.GracePeriodPref), 60); + final String alarmTimeStr = sharedPref.getString(context.getString(R.string.AlarmTimePref), null); if (alarmActive) { // if nothing else happens, assume the alert was ignored. @@ -53,6 +52,17 @@ 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 + 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()); + } + } + // Set a grace period alarm to send SMS Calendar graceCal = Calendar.getInstance(); graceCal.set(Calendar.SECOND, 0); @@ -68,7 +78,7 @@ public class AlarmReceiver extends BroadcastReceiver { } Log.d("AlarmReceiver", "Setting grace alarm for " + MainActivity.debugDate(graceCal)); - // Calculate when the grace period ends + // Calculate when the grace period (converted from minutes to milliseconds) ends graceEndTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000); // Set up intents for later use @@ -124,6 +134,7 @@ public class AlarmReceiver extends BroadcastReceiver { } }, ALERT_LIFE); } + public static void stopAlert(final Context context) { Log.d("AlarmReceiver", "Stopping alarm; status is " + alarmStatus); AlarmKlaxon.stop(context); @@ -147,7 +158,6 @@ public class AlarmReceiver extends BroadcastReceiver { graceManager.cancel(gracePendingIntent); } - // TODO should the snooze reset the time at which the grace period alarm fires? public static void snoozeAlarm(final Context context) { Log.d("AlarmReceiver", "Snoozing alarm"); stopAlert(context);