From 36b7cd26b9d717c69f0cbb66f134957cf53070ce Mon Sep 17 00:00:00 2001
From: Peter De Jonckheere <peter.de-jonckheere.2014@uni.strath.ac.uk>
Date: Mon, 23 Jul 2018 14:57:14 +0100
Subject: [PATCH] Fixed feedback notifications and goal notifications

---
 src/main/AndroidManifest.xml                  |  6 +-
 .../notification/FeedbackNotification.java    |  5 +-
 .../FeedbackNotificationCanceller.java        |  2 +-
 .../sedentary/frontEnd/DataAnalysis.java      | 66 +++++++++++++-
 .../stimulus/FeedbackStimulus.java            | 80 -----------------
 .../stimulus/GoalStimulus.java                | 88 -------------------
 .../stimulus/SedentaryStimulus.java           | 71 ++++++++++++++-
 .../stimulusStrategy/Accelerometer.java       | 70 +++++++++++++--
 .../stimulusStrategy/SigMotionDetect.java     | 66 +++++++++++++-
 9 files changed, 261 insertions(+), 193 deletions(-)
 delete mode 100644 src/main/java/com/notificationFramework/stimulus/FeedbackStimulus.java
 delete mode 100644 src/main/java/com/notificationFramework/stimulus/GoalStimulus.java

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index cd569bb..c9efcf7 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -54,11 +54,9 @@
         </service>
         <service android:name="com.notificationFramework.stimulusStrategy.StepCounter">
         </service>
-        <receiver android:name="com.notificationFramework.stimulus.SedentaryStimulus">
-        </receiver>
-        <receiver android:name="com.notificationFramework.stimulus.GoalStimulus">
+        <receiver android:name="com.notificationFramework.notification.FeedbackNotificationCanceller">
         </receiver>
-        <receiver android:name="com.notificationFramework.stimulus.FeedbackStimulus">
+        <receiver android:name="com.notificationFramework.stimulus.SedentaryStimulus">
         </receiver>
         <receiver android:name="com.notificationFramework.stimulus.BootLauncher">
             <intent-filter>
diff --git a/src/main/java/com/notificationFramework/notification/FeedbackNotification.java b/src/main/java/com/notificationFramework/notification/FeedbackNotification.java
index e26fa6b..bf7c187 100644
--- a/src/main/java/com/notificationFramework/notification/FeedbackNotification.java
+++ b/src/main/java/com/notificationFramework/notification/FeedbackNotification.java
@@ -9,6 +9,7 @@ import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.SystemClock;
 import android.support.v4.app.NotificationCompat;
+import android.util.Log;
 
 import com.notificationFramework.sedentary.frontEnd.R;
 
@@ -50,6 +51,7 @@ public class FeedbackNotification extends SedentaryNotification {
             builder.setVibrate(new long[]{0, 500, 0, 500});
         }
         setCancellingAlarm();
+        Log.d("Feedback", "Should be");
         return builder;
     }
 
@@ -69,6 +71,7 @@ public class FeedbackNotification extends SedentaryNotification {
 
     private void setCancellingAlarm(){
         AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+        Log.d("Cancelling??", "hopefully");
         Intent i = new Intent(this,
                 com.notificationFramework.notification.FeedbackNotificationCanceller.class);
         PendingIntent pi = PendingIntent.getBroadcast(this,
@@ -76,6 +79,6 @@ public class FeedbackNotification extends SedentaryNotification {
         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);
+                SystemClock.elapsedRealtime() + 3000, pi);
     }
 }
diff --git a/src/main/java/com/notificationFramework/notification/FeedbackNotificationCanceller.java b/src/main/java/com/notificationFramework/notification/FeedbackNotificationCanceller.java
index abe4160..0973dac 100644
--- a/src/main/java/com/notificationFramework/notification/FeedbackNotificationCanceller.java
+++ b/src/main/java/com/notificationFramework/notification/FeedbackNotificationCanceller.java
@@ -18,7 +18,7 @@ public class FeedbackNotificationCanceller extends BroadcastReceiver {
     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));
+            nm.cancelAll();
         } catch(NullPointerException e){
             Log.i("NOTIFICATION", "NOTIFICATION WAS CLICKED ON");
         }
diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java
index 57c73b5..499b7b7 100644
--- a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java
+++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java
@@ -1,8 +1,10 @@
 package com.notificationFramework.sedentary.frontEnd;
 
 import android.app.NotificationManager;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.icu.util.RangeValueIterator;
 import android.support.v4.content.LocalBroadcastManager;
@@ -44,9 +46,67 @@ public class DataAnalysis {
                 Log.i("NOTIFICATION", "NOTIFICATION WAS CLICKED ON");
             }
 
-            Intent i = new Intent(context.getApplicationContext(),
-                    com.notificationFramework.stimulus.FeedbackStimulus.class);
-            LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext());
+            Intent i = new Intent("FEEDBACKFILTER");
+            final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext());
+            BroadcastReceiver feedback = new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                    Log.d("FEEDBACK STIM", "EHH HELP");
+                    SharedPreferences shared =
+                            context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
+                    if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
+                        Intent i = new Intent(context.getApplicationContext(),
+                                com.notificationFramework.notification.FeedbackNotification.class);
+                        context.getApplicationContext().startService(i);
+                    }
+                    lbm.unregisterReceiver(this);
+                }
+
+                /**
+                 * Checks the current time against the do not disturb time by first checking if the time spans 2
+                 * days, then checking between the first time and midnight, then midnight and the second time,
+                 * followed finally by the simple check between the two times if the time does not span 2 days.
+                 * It is done in this way to avoid compications with date changes as each time is obtained using
+                 * the current calendar as a template so if done correctly dates should not need to be changed.
+                 *
+                 * @param shared  the SharedPreferences instance set up in the onReceive() method
+                 * @param context the application context which this method has been called from
+                 * @return false if the time is within the do not disturb period and so a notification should
+                 * not be sent, true if a notification should be sent
+                 */
+                private boolean checkTime(SharedPreferences shared, Context context) {
+                    Calendar start = Calendar.getInstance();
+                    start.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_shour), 23));
+                    start.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_smin), 0));
+                    Calendar midnightCal = Calendar.getInstance();
+                    midnightCal.set(Calendar.HOUR_OF_DAY, 23);
+                    midnightCal.set(Calendar.MINUTE, 59);
+                    midnightCal.set(Calendar.SECOND, 59);
+                    Calendar end = Calendar.getInstance();
+                    end.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_ehour), 9));
+                    end.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_emin), 0));
+                    Calendar curCal = Calendar.getInstance();
+                    if (shared.getInt(context.getString(R.string.dnd_shour), 23) > shared.getInt(context.getString(R.string.dnd_ehour), 9)) {
+                        if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(midnightCal.getTime())) {
+                            return false;
+                        } else {
+                            midnightCal.set(Calendar.HOUR_OF_DAY, 0);
+                            midnightCal.set(Calendar.MINUTE, 0);
+                            midnightCal.set(Calendar.SECOND, 1);
+                            if (curCal.getTime().after(midnightCal.getTime()) && curCal.getTime().before(end.getTime())) {
+                                return false;
+                            } else {
+                                return true;
+                            }
+                        }
+                    } else if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(end.getTime())) {
+                        return false;
+                    } else {
+                        return true;
+                    }
+                }
+            };
+            lbm.registerReceiver(feedback, new IntentFilter("FEEDBACKFILTER"));
             lbm.sendBroadcast(i);
             logAcknowledgement(context);
             return true;
diff --git a/src/main/java/com/notificationFramework/stimulus/FeedbackStimulus.java b/src/main/java/com/notificationFramework/stimulus/FeedbackStimulus.java
deleted file mode 100644
index fe93bba..0000000
--- a/src/main/java/com/notificationFramework/stimulus/FeedbackStimulus.java
+++ /dev/null
@@ -1,80 +0,0 @@
-package com.notificationFramework.stimulus;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.SharedPreferences;
-
-import com.notificationFramework.sedentary.frontEnd.R;
-
-import java.util.Calendar;
-
-/**
- * Created by pharmacy on 25/06/2018.
- */
-
-public class FeedbackStimulus extends BroadcastReceiver implements Stimulus {
-
-    /**
-     * Upon receiving a broadcast from a strategy this method is triggered and starts the related
-     * service which sends the notification after checking relevant personalised settings.
-     *
-     * @param context the context of the broadcast
-     * @param intent  the intent with which the broadcast was sent
-     */
-    @Override
-    public void onReceive(Context context, Intent intent) {
-        SharedPreferences shared =
-                context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
-        if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
-            Intent i = new Intent(context.getApplicationContext(),
-                    com.notificationFramework.notification.FeedbackNotification.class);
-            context.getApplicationContext().startService(i);
-        }
-
-    }
-
-    /**
-     * Checks the current time against the do not disturb time by first checking if the time spans 2
-     * days, then checking between the first time and midnight, then midnight and the second time,
-     * followed finally by the simple check between the two times if the time does not span 2 days.
-     * It is done in this way to avoid compications with date changes as each time is obtained using
-     * the current calendar as a template so if done correctly dates should not need to be changed.
-     *
-     * @param shared  the SharedPreferences instance set up in the onReceive() method
-     * @param context the application context which this method has been called from
-     * @return false if the time is within the do not disturb period and so a notification should
-     * not be sent, true if a notification should be sent
-     */
-    private boolean checkTime(SharedPreferences shared, Context context) {
-        Calendar start = Calendar.getInstance();
-        start.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_shour), 23));
-        start.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_smin), 0));
-        Calendar midnightCal = Calendar.getInstance();
-        midnightCal.set(Calendar.HOUR_OF_DAY, 23);
-        midnightCal.set(Calendar.MINUTE, 59);
-        midnightCal.set(Calendar.SECOND, 59);
-        Calendar end = Calendar.getInstance();
-        end.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_ehour), 9));
-        end.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_emin), 0));
-        Calendar curCal = Calendar.getInstance();
-        if (shared.getInt(context.getString(R.string.dnd_shour), 23) > shared.getInt(context.getString(R.string.dnd_ehour), 9)) {
-            if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(midnightCal.getTime())) {
-                return false;
-            } else {
-                midnightCal.set(Calendar.HOUR_OF_DAY, 0);
-                midnightCal.set(Calendar.MINUTE, 0);
-                midnightCal.set(Calendar.SECOND, 1);
-                if (curCal.getTime().after(midnightCal.getTime()) && curCal.getTime().before(end.getTime())) {
-                    return false;
-                } else {
-                    return true;
-                }
-            }
-        } else if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(end.getTime())) {
-            return false;
-        } else {
-            return true;
-        }
-    }
-}
diff --git a/src/main/java/com/notificationFramework/stimulus/GoalStimulus.java b/src/main/java/com/notificationFramework/stimulus/GoalStimulus.java
deleted file mode 100644
index d860545..0000000
--- a/src/main/java/com/notificationFramework/stimulus/GoalStimulus.java
+++ /dev/null
@@ -1,88 +0,0 @@
-package com.notificationFramework.stimulus;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.SharedPreferences;
-
-import com.notificationFramework.sedentary.frontEnd.R;
-
-import java.util.Calendar;
-
-/**
- * Created by Peter De Jonckheere on 09/03/2018.
- * <p>
- * The concrete implementation of the Stimulus interface which handles requests for goal
- * notifications to be sent.
- * </p>
- *
- * @see android.content.BroadcastReceiver
- */
-
-public class GoalStimulus extends BroadcastReceiver implements Stimulus {
-
-    /**
-     * Upon receiving a broadcast from a strategy this method is triggered and starts the related
-     * service which sends the notification after checking relevant personalised settings.
-     *
-     * @param context the context of the broadcast
-     * @param intent  the intent with which the broadcast was sent
-     */
-    @Override
-    public void onReceive(Context context, Intent intent) {
-        SharedPreferences shared =
-                context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
-        if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
-            Intent i = new Intent(context.getApplicationContext(),
-                    com.notificationFramework.notification.GoalNotification.class);
-            context.getApplicationContext().startService(i);
-        }
-
-    }
-
-    /**
-     * Checks the current time against the do not disturb time by first checking if the time spans 2
-     * days, then checking between the first time and midnight, then midnight and the second time,
-     * followed finally by the simple check between the two times if the time does not span 2 days.
-     * It is done in this way to avoid compications with date changes as each time is obtained using
-     * the current calendar as a template so if done correctly dates should not need to be changed.
-     *
-     * @param shared  the SharedPreferences instance set up in the onReceive() method
-     * @param context the application context which this method has been called from
-     * @return false if the time is within the do not disturb period and so a notification should
-     * not be sent, true if a notification should be sent
-     */
-    private boolean checkTime(SharedPreferences shared, Context context) {
-        Calendar start = Calendar.getInstance();
-        start.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_shour), 23));
-        start.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_smin), 0));
-        Calendar midnightCal = Calendar.getInstance();
-        midnightCal.set(Calendar.HOUR_OF_DAY, 23);
-        midnightCal.set(Calendar.MINUTE, 59);
-        midnightCal.set(Calendar.SECOND, 59);
-        Calendar end = Calendar.getInstance();
-        end.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_ehour), 9));
-        end.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_emin), 0));
-        Calendar curCal = Calendar.getInstance();
-        if (shared.getInt(context.getString(R.string.dnd_shour), 23) > shared.getInt(context.getString(R.string.dnd_ehour), 9)) {
-            if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(midnightCal.getTime())) {
-                return false;
-            } else {
-                midnightCal.set(Calendar.HOUR_OF_DAY, 0);
-                midnightCal.set(Calendar.MINUTE, 0);
-                midnightCal.set(Calendar.SECOND, 1);
-                if (curCal.getTime().after(midnightCal.getTime()) && curCal.getTime().before(end.getTime())) {
-                    return false;
-                } else {
-                    return true;
-                }
-            }
-        } else if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(end.getTime())) {
-            return false;
-        } else {
-            return true;
-        }
-    }
-
-}
-
diff --git a/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java b/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java
index d8a5eab..1637f1f 100644
--- a/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java
+++ b/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java
@@ -3,6 +3,7 @@ package com.notificationFramework.stimulus;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.support.v4.content.LocalBroadcastManager;
 import android.util.Log;
@@ -165,10 +166,72 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
         Log.d("AVGTHRESH", String.valueOf(ackThreshold));
         float avgAck = shared.getFloat(context.getString(R.string.avg_ack), 0);
         Log.d("AVGACK", String.valueOf(avgAck));
+        //test
+        avgAck = 1;
+        ackThreshold = 0;
         if(avgAck > ackThreshold){
-                Intent i = new Intent(context.getApplicationContext(),
-                        com.notificationFramework.stimulus.FeedbackStimulus.class);
-                LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext());
+            Log.d("SEDSTIM", "I ASSUME");
+            Intent i = new Intent("FEEDBACKFILTER");
+            final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext());
+            BroadcastReceiver feedback = new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                    Log.d("FEEDBACK STIM", "EHH HELP");
+                    SharedPreferences shared =
+                            context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
+                    if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
+                        Intent i = new Intent(context.getApplicationContext(),
+                                com.notificationFramework.notification.FeedbackNotification.class);
+                        context.getApplicationContext().startService(i);
+                    }
+                    lbm.unregisterReceiver(this);
+                }
+
+                /**
+                 * Checks the current time against the do not disturb time by first checking if the time spans 2
+                 * days, then checking between the first time and midnight, then midnight and the second time,
+                 * followed finally by the simple check between the two times if the time does not span 2 days.
+                 * It is done in this way to avoid compications with date changes as each time is obtained using
+                 * the current calendar as a template so if done correctly dates should not need to be changed.
+                 *
+                 * @param shared  the SharedPreferences instance set up in the onReceive() method
+                 * @param context the application context which this method has been called from
+                 * @return false if the time is within the do not disturb period and so a notification should
+                 * not be sent, true if a notification should be sent
+                 */
+                private boolean checkTime(SharedPreferences shared, Context context) {
+                    Calendar start = Calendar.getInstance();
+                    start.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_shour), 23));
+                    start.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_smin), 0));
+                    Calendar midnightCal = Calendar.getInstance();
+                    midnightCal.set(Calendar.HOUR_OF_DAY, 23);
+                    midnightCal.set(Calendar.MINUTE, 59);
+                    midnightCal.set(Calendar.SECOND, 59);
+                    Calendar end = Calendar.getInstance();
+                    end.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_ehour), 9));
+                    end.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_emin), 0));
+                    Calendar curCal = Calendar.getInstance();
+                    if (shared.getInt(context.getString(R.string.dnd_shour), 23) > shared.getInt(context.getString(R.string.dnd_ehour), 9)) {
+                        if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(midnightCal.getTime())) {
+                            return false;
+                        } else {
+                            midnightCal.set(Calendar.HOUR_OF_DAY, 0);
+                            midnightCal.set(Calendar.MINUTE, 0);
+                            midnightCal.set(Calendar.SECOND, 1);
+                            if (curCal.getTime().after(midnightCal.getTime()) && curCal.getTime().before(end.getTime())) {
+                                return false;
+                            } else {
+                                return true;
+                            }
+                        }
+                    } else if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(end.getTime())) {
+                        return false;
+                    } else {
+                        return true;
+                    }
+                }
+                };
+                lbm.registerReceiver(feedback, new IntentFilter("FEEDBACKFILTER"));
                 lbm.sendBroadcast(i);
             SaveFile.recordNotification(0,0,0,1, context);
             return false;
@@ -180,7 +243,7 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
     private boolean checkContext(SharedPreferences shared, Context context){
         int mostPop = shared.getInt(context.getString(R.string.store_mcp), -1);
         float temp = shared.getFloat(context.getString(R.string.store_temp), 18);
-        int lastPlace = shared.getInt(context.getString(R.string.store_place), 0);
+       // int lastPlace = shared.getInt(context.getString(R.string.store_place), 0);
         int ackTotal = shared.getInt(context.getString(R.string.ack_total), 0);
         int placeAck = shared.getInt(context.getString(R.string.place_ack_total), 0);
         double comp = (double) placeAck / ackTotal;
diff --git a/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java b/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java
index 91473c7..2040862 100644
--- a/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java
+++ b/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java
@@ -3,8 +3,10 @@ package com.notificationFramework.stimulusStrategy;
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.app.Service;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
@@ -16,15 +18,10 @@ import android.support.annotation.Nullable;
 import android.support.v4.content.LocalBroadcastManager;
 import android.util.Log;
 
-import com.google.android.gms.awareness.Awareness;
-import com.google.android.gms.awareness.state.Weather;
-import com.google.android.gms.location.places.PlaceLikelihood;
 import com.notificationFramework.sedentary.frontEnd.R;
-import com.notificationFramework.sedentary.frontEnd.RequestPermission;
 import com.notificationFramework.sedentary.frontEnd.SaveFile;
 
 import java.util.Calendar;
-import java.util.List;
 
 
 /**
@@ -180,9 +177,66 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv
      * class.
      */
     public void goalNotify() {
-        Intent i = new Intent(getBaseContext(),
-                com.notificationFramework.stimulus.GoalStimulus.class);
-        LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
+        Intent i = new Intent("FEEDBACKFILTER");
+        final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this.getApplicationContext());
+        BroadcastReceiver goal = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                SharedPreferences shared =
+                        context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
+                if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
+                    Intent i = new Intent(context.getApplicationContext(),
+                            com.notificationFramework.notification.GoalNotification.class);
+                    context.getApplicationContext().startService(i);
+                }
+                lbm.unregisterReceiver(this);
+            }
+
+            /**
+             * Checks the current time against the do not disturb time by first checking if the time spans 2
+             * days, then checking between the first time and midnight, then midnight and the second time,
+             * followed finally by the simple check between the two times if the time does not span 2 days.
+             * It is done in this way to avoid compications with date changes as each time is obtained using
+             * the current calendar as a template so if done correctly dates should not need to be changed.
+             *
+             * @param shared  the SharedPreferences instance set up in the onReceive() method
+             * @param context the application context which this method has been called from
+             * @return false if the time is within the do not disturb period and so a notification should
+             * not be sent, true if a notification should be sent
+             */
+            private boolean checkTime(SharedPreferences shared, Context context) {
+                Calendar start = Calendar.getInstance();
+                start.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_shour), 23));
+                start.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_smin), 0));
+                Calendar midnightCal = Calendar.getInstance();
+                midnightCal.set(Calendar.HOUR_OF_DAY, 23);
+                midnightCal.set(Calendar.MINUTE, 59);
+                midnightCal.set(Calendar.SECOND, 59);
+                Calendar end = Calendar.getInstance();
+                end.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_ehour), 9));
+                end.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_emin), 0));
+                Calendar curCal = Calendar.getInstance();
+                if (shared.getInt(context.getString(R.string.dnd_shour), 23) > shared.getInt(context.getString(R.string.dnd_ehour), 9)) {
+                    if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(midnightCal.getTime())) {
+                        return false;
+                    } else {
+                        midnightCal.set(Calendar.HOUR_OF_DAY, 0);
+                        midnightCal.set(Calendar.MINUTE, 0);
+                        midnightCal.set(Calendar.SECOND, 1);
+                        if (curCal.getTime().after(midnightCal.getTime()) && curCal.getTime().before(end.getTime())) {
+                            return false;
+                        } else {
+                            return true;
+                        }
+                    }
+                } else if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(end.getTime())) {
+                    return false;
+                } else {
+                    return true;
+                }
+            }
+        };
+        lbm.registerReceiver(goal, new IntentFilter("GOALFILTER"));
         lbm.sendBroadcast(i);
     }
 
diff --git a/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java b/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java
index 96251c6..2efb2bb 100644
--- a/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java
+++ b/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java
@@ -4,8 +4,10 @@ import android.Manifest;
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.app.Service;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.hardware.Sensor;
@@ -260,10 +262,66 @@ public class SigMotionDetect extends Service implements StimulusStrategy {
      * broadcast is then sent to the relevant stimulus class and on to the relevant notification
      * class.
      */
-    public void goalNotify() {
-        Intent i = new Intent(getBaseContext(),
-                com.notificationFramework.stimulus.GoalStimulus.class);
-        LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
+    public void goalNotify() { Intent i = new Intent("FEEDBACKFILTER");
+        final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this.getApplicationContext());
+        BroadcastReceiver goal = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                SharedPreferences shared =
+                        context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
+                if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
+                    Intent i = new Intent(context.getApplicationContext(),
+                            com.notificationFramework.notification.GoalNotification.class);
+                    context.getApplicationContext().startService(i);
+                }
+                lbm.unregisterReceiver(this);
+            }
+
+            /**
+             * Checks the current time against the do not disturb time by first checking if the time spans 2
+             * days, then checking between the first time and midnight, then midnight and the second time,
+             * followed finally by the simple check between the two times if the time does not span 2 days.
+             * It is done in this way to avoid compications with date changes as each time is obtained using
+             * the current calendar as a template so if done correctly dates should not need to be changed.
+             *
+             * @param shared  the SharedPreferences instance set up in the onReceive() method
+             * @param context the application context which this method has been called from
+             * @return false if the time is within the do not disturb period and so a notification should
+             * not be sent, true if a notification should be sent
+             */
+            private boolean checkTime(SharedPreferences shared, Context context) {
+                Calendar start = Calendar.getInstance();
+                start.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_shour), 23));
+                start.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_smin), 0));
+                Calendar midnightCal = Calendar.getInstance();
+                midnightCal.set(Calendar.HOUR_OF_DAY, 23);
+                midnightCal.set(Calendar.MINUTE, 59);
+                midnightCal.set(Calendar.SECOND, 59);
+                Calendar end = Calendar.getInstance();
+                end.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_ehour), 9));
+                end.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_emin), 0));
+                Calendar curCal = Calendar.getInstance();
+                if (shared.getInt(context.getString(R.string.dnd_shour), 23) > shared.getInt(context.getString(R.string.dnd_ehour), 9)) {
+                    if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(midnightCal.getTime())) {
+                        return false;
+                    } else {
+                        midnightCal.set(Calendar.HOUR_OF_DAY, 0);
+                        midnightCal.set(Calendar.MINUTE, 0);
+                        midnightCal.set(Calendar.SECOND, 1);
+                        if (curCal.getTime().after(midnightCal.getTime()) && curCal.getTime().before(end.getTime())) {
+                            return false;
+                        } else {
+                            return true;
+                        }
+                    }
+                } else if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(end.getTime())) {
+                    return false;
+                } else {
+                    return true;
+                }
+            }
+        };
+        lbm.registerReceiver(goal, new IntentFilter("GOALFILTER"));
         lbm.sendBroadcast(i);
     }
 
-- 
GitLab