From fa4c1152d372d56edef6cb633c0ad80c1b857b14 Mon Sep 17 00:00:00 2001 From: tim Date: Wed, 26 Mar 2014 14:23:03 +0200 Subject: [PATCH] Additional notification cancellations. --- .../main/java/za/org/treehouse/hypoalarm/AlarmNotify.java | 4 ---- .../main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java | 6 ++++++ .../za/org/treehouse/hypoalarm/CancelGraceReceiver.java | 5 +++++ .../main/java/za/org/treehouse/hypoalarm/MainActivity.java | 5 +++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java index a58840c..2e407cf 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java @@ -9,10 +9,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.ColorMatrix; -import android.graphics.ColorMatrixColorFilter; -import android.graphics.Paint; import android.os.IBinder; import android.preference.PreferenceManager; import android.util.Log; 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 f50ac89..85a092a 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmReceiver.java @@ -1,6 +1,7 @@ package za.org.treehouse.hypoalarm; import android.app.AlarmManager; +import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; @@ -23,6 +24,11 @@ public class AlarmReceiver extends BroadcastReceiver { Boolean alarmActive = sharedPref.getBoolean(context.getString(R.string.AlarmActivePref), true); if (alarmActive) { + + // Cancel notification if it's not already cancelled. + NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + nm.cancel(AlarmNotify.notifyID); + // Set a grace period alarm to send SMS int gracePeriod = sharedPref.getInt(context.getString(R.string.GracePeriodPref), 60); diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java index 23d1019..bd486b2 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java @@ -1,6 +1,7 @@ package za.org.treehouse.hypoalarm; import android.app.AlarmManager; +import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; @@ -11,6 +12,10 @@ import android.widget.Toast; public class CancelGraceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { + // Cancel notification if it's not already cancelled. + NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + nm.cancel(AlarmNotify.notifyID); + AlarmManager graceManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent graceIntent = new Intent(context, GraceReceiver.class); PendingIntent gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0); 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 dd9ec28..8789a5c 100644 --- a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/MainActivity.java @@ -3,6 +3,7 @@ package za.org.treehouse.hypoalarm; import android.app.AlarmManager; import android.app.AlertDialog; import android.app.Dialog; +import android.app.NotificationManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.ComponentName; @@ -114,6 +115,10 @@ public class MainActivity extends ActionBarActivity { editor.commit(); if (!b) { + // Cancel notification if it's not already cancelled. + NotificationManager nm = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE); + nm.cancel(AlarmNotify.notifyID); + // Cancel any current alarm alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); Intent alarmIntent = new Intent(getActivity(), AlarmReceiver.class);