From 7da0fdefcf952e0a2336a5f6319e1d0ec77f51e0 Mon Sep 17 00:00:00 2001
From: Peter De Jonckheere <peter.de-jonckheere.2014@uni.strath.ac.uk>
Date: Fri, 22 Jun 2018 13:30:08 +0100
Subject: [PATCH] Added some more data analysis

---
 .../sedentary/frontEnd/DataAnalysis.java      | 38 +++++++++++++++----
 .../sedentary/frontEnd/SaveFile.java          |  2 +-
 .../stimulus/SedentaryStimulus.java           | 19 +++++++++-
 src/main/res/values/strings.xml               |  3 ++
 4 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java
index eadbfbf..3d3f082 100644
--- a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java
+++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java
@@ -1,36 +1,58 @@
 package com.notificationFramework.sedentary.frontEnd;
 
+import android.content.Context;
+import android.content.SharedPreferences;
 import android.util.Log;
 
-import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
-import java.util.Date;
 import java.util.Locale;
 
 /**
- * Created by pharmacy on 19/06/2018.
+ * Created by Peter De Jonckheere on 19/06/2018.
  */
 
 public class DataAnalysis {
 
-    static boolean analyse(String line, Calendar cal){
+    static boolean analyse(String line, Calendar cal, Context context) {
         Calendar compCal = Calendar.getInstance();
         SimpleDateFormat sdf = new SimpleDateFormat("EEEMMMddHH:mm:sszyyyy", Locale.ENGLISH);
         String items[] = line.split(" ");
         String dateString = items[0] + items[1] + items[2] + items[3] + items[4] + items[5];
         try {
             compCal.setTime(sdf.parse(dateString));
-        }catch(ParseException e){
+        } catch (ParseException e) {
             Log.e("ANALYSING", "PARSINGERROR");
         }
         compCal.add(Calendar.MINUTE, -6);
-        if(compCal.before(cal.getTime())){
+        if (compCal.before(cal.getTime())) {
+            //Remove notification
+            //Give new notification saying well done?
+            logAcknowledgement(context);
             return true;
-        }else{
+        } else {
             return false;
         }
-        }
     }
 
+
+    private static void logAcknowledgement(Context context) {
+        SharedPreferences sharedPref = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
+        int totalAck = sharedPref.getInt(context.getString(R.string.ack_total), 0);
+        int ackDays = sharedPref.getInt(context.getString(R.string.ack_days), 1);
+        float avgAck;
+        int currentDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
+        if (currentDay != sharedPref.getInt(context.getString(R.string.progress_day), 0)) {
+            avgAck = totalAck / ackDays;
+            ackDays++;
+            SharedPreferences.Editor editor = sharedPref.edit();
+            editor.putInt(context.getString(R.string.progress_day), currentDay);
+            editor.putInt(context.getString(R.string.ack_total), totalAck);
+            editor.putInt(context.getString(R.string.ack_days), ackDays);
+            editor.putFloat(context.getString(R.string.avg_ack), avgAck);
+            editor.commit();
+        }
+        totalAck++;
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java
index 3df8628..1592b78 100644
--- a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java
+++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java
@@ -109,7 +109,7 @@ public class SaveFile {
      * @param clicked      1 if a notification has been clicked on, 1 if a goal notification has
      *                     been sent, 0 otherwise
      * @param movement     1 if a movement has been registered, 0 otherwise
-     * @param acknowledged 1 if a notification has been acknowledged by a movement
+     * @param acknowledged 1 if a notification has been acknowledged by a movement, 0 otherwise
      * @param context      the application context which the method has been called from and which
      *                     will be used to access and write to the external storage of the device
      */
diff --git a/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java b/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java
index dfff0a0..422bbe9 100644
--- a/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java
+++ b/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java
@@ -28,6 +28,8 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
      */
     private static StimulusStrategy strategy;
 
+    private final int threshold = 10;
+
     /**
      * Empty constructor required by the manifest to define this class as a broadcast receiver
      */
@@ -84,7 +86,8 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
     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))) {
+        if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))
+                && checkAcknowledged(shared, context)) {
             Intent i = new Intent(context.getApplicationContext(),
                     com.notificationFramework.notification.SedentaryNotification.class);
             context.getApplicationContext().startService(i);
@@ -146,4 +149,18 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
             return true;
         }
     }
+
+    //Returns true if notification is to be sent
+    private boolean checkAcknowledged(SharedPreferences shared, Context context){
+        int period = shared.getInt(context.getString(R.string.daily_goal), context.getResources().getInteger(R.integer.daily_goal_minutes));
+        float ackThreshold = (24/period) * threshold;
+        float avgAck = shared.getFloat(context.getString(R.string.avg_ack), 4);
+        if(avgAck < ackThreshold){
+            //Send well done notification
+            //Record unsent notification for fading and behavioural analysis purposes??
+            return false;
+        }else{
+            return true;
+        }
+    }
 }
diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml
index 6354556..4698da2 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -43,4 +43,7 @@
     <string name="strat_switch">Switch detection method</string>
     <string name="strategy">Current strategy</string>
     <string name="goal_met">Daily Goal Met</string>
+    <string name="ack_total">Acknowledgement Total</string>
+    <string name="ack_days">Acknowledgement Day Count</string>
+    <string name="avg_ack">Acknowledgements per day</string>
 </resources>
-- 
GitLab