From 01131352ca27b60dd3d10219b5d130c32af21c55 Mon Sep 17 00:00:00 2001
From: Peter De Jonckheere <peter.de-jonckheere.2014@uni.strath.ac.uk>
Date: Mon, 25 Jun 2018 13:29:21 +0100
Subject: [PATCH] Added feedback notification canceller

---
 .../notification/FeedbackNotification.java    | 20 ++++++++++++++
 .../FeedbackNotificationCanceller.java        | 27 +++++++++++++++++++
 src/main/res/values/integers.xml              |  2 ++
 3 files changed, 49 insertions(+)
 create mode 100644 src/main/java/com/notificationFramework/notification/FeedbackNotificationCanceller.java

diff --git a/src/main/java/com/notificationFramework/notification/FeedbackNotification.java b/src/main/java/com/notificationFramework/notification/FeedbackNotification.java
index f730a5c..693bce2 100644
--- a/src/main/java/com/notificationFramework/notification/FeedbackNotification.java
+++ b/src/main/java/com/notificationFramework/notification/FeedbackNotification.java
@@ -1,8 +1,13 @@
 package com.notificationFramework.notification;
 
 import android.annotation.TargetApi;
+import android.app.AlarmManager;
 import android.app.NotificationChannel;
+import android.app.PendingIntent;
+import android.content.Context;
 import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.SystemClock;
 import android.support.v4.app.NotificationCompat;
 
 import com.notificationFramework.sedentary.frontEnd.R;
@@ -13,6 +18,8 @@ import com.notificationFramework.sedentary.frontEnd.R;
 
 public class FeedbackNotification extends SedentaryNotification {
 
+
+    protected int snId = this.getResources().getInteger(R.integer.notffeedback_id);
     /**
      * Called when the service is started. Uses the Notification implementation of this method.
      * @param intent  the intent used to start the service
@@ -41,6 +48,7 @@ public class FeedbackNotification extends SedentaryNotification {
         if (preferences.getBoolean(getString(R.string.vibration), false)) {
             builder.setVibrate(new long[]{0, 500, 0, 500});
         }
+        setCancellingAlarm();
         return builder;
     }
 
@@ -57,4 +65,16 @@ public class FeedbackNotification extends SedentaryNotification {
         nc.setDescription(this.getClass().getName());
         return nc;
     }
+
+    private void setCancellingAlarm(){
+        AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+        Intent i = new Intent(this,
+                com.notificationFramework.notification.FeedbackNotificationCanceller.class);
+        PendingIntent pi = PendingIntent.getBroadcast(this,
+                R.integer.alarm_fnc, i, 0);
+        SharedPreferences shared = this.getSharedPreferences(getString(
+                R.string.preference_file_key), Context.MODE_PRIVATE);
+        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
+                SystemClock.elapsedRealtime() + (1000 * 60 * 2), pi);
+    }
 }
diff --git a/src/main/java/com/notificationFramework/notification/FeedbackNotificationCanceller.java b/src/main/java/com/notificationFramework/notification/FeedbackNotificationCanceller.java
new file mode 100644
index 0000000..abe4160
--- /dev/null
+++ b/src/main/java/com/notificationFramework/notification/FeedbackNotificationCanceller.java
@@ -0,0 +1,27 @@
+package com.notificationFramework.notification;
+
+import android.app.NotificationManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+import com.notificationFramework.sedentary.frontEnd.R;
+
+/**
+ * Created by pharmacy on 25/06/2018.
+ */
+
+public class FeedbackNotificationCanceller extends BroadcastReceiver {
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+        try {
+            nm.cancel(context.getResources().getInteger(R.integer.notffeedback_id));
+        } catch(NullPointerException e){
+            Log.i("NOTIFICATION", "NOTIFICATION WAS CLICKED ON");
+        }
+
+    }
+}
diff --git a/src/main/res/values/integers.xml b/src/main/res/values/integers.xml
index ecb73a5..da39a00 100644
--- a/src/main/res/values/integers.xml
+++ b/src/main/res/values/integers.xml
@@ -5,4 +5,6 @@
     <integer name="step_job">1</integer>
     <integer name="alarm_rc">23</integer>
     <integer name="notf_id">1240</integer>
+    <integer name="notffeedback_id">1983</integer>
+    <integer name="alarm_fnc">1903</integer>
 </resources>
\ No newline at end of file
-- 
GitLab