From 9a4c991f0686d270017e0f0624b30864fe6d1c5d Mon Sep 17 00:00:00 2001
From: Peter De Jonckheere <peter.de-jonckheere.2014@uni.strath.ac.uk>
Date: Tue, 3 Jul 2018 14:32:28 +0100
Subject: [PATCH] refactor

---
 src/main/AndroidManifest.xml                  |  1 +
 .../sedentary/frontEnd/SaveFile.java          | 34 +++++++++++++++++++
 .../stimulusStrategy/Accelerometer.java       | 25 +-------------
 .../stimulusStrategy/SigMotionDetect.java     |  4 ---
 4 files changed, 36 insertions(+), 28 deletions(-)

diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml
index 500df57..d83a033 100644
--- a/src/main/AndroidManifest.xml
+++ b/src/main/AndroidManifest.xml
@@ -6,6 +6,7 @@
     <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
     <uses-permission android:name="android.permission.VIBRATE"/>
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
 
diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java
index cd083c2..7d87f8e 100644
--- a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java
+++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java
@@ -1,11 +1,14 @@
 package com.notificationFramework.sedentary.frontEnd;
 
 import android.content.Context;
+import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Environment;
 import android.util.Log;
 
+import com.google.android.gms.awareness.Awareness;
 import com.google.android.gms.awareness.state.Weather;
+import com.google.android.gms.common.api.GoogleApiClient;
 import com.google.android.gms.location.places.PlaceLikelihood;
 
 import java.io.BufferedOutputStream;
@@ -166,6 +169,7 @@ public class SaveFile {
             }
         }
         recordTimeStamp(sent, clicked, movement, acknowledged, context);
+        getExtraData(context);
     }
 
     /**
@@ -327,6 +331,36 @@ public class SaveFile {
         }
     }
 
+
+    private static void getExtraData(Context context){
+        GoogleApiClient client;
+        client = new GoogleApiClient.Builder(context)
+                .addApi(Awareness.getSnapshotClient(context).getApi()).build();
+        client.connect();
+        try {
+            double latitude = Awareness.getSnapshotClient(context).getLocation()
+                    .getResult().getLocation().getLatitude();
+            double longitude = Awareness.getSnapshotClient(context).getLocation()
+                    .getResult().getLocation().getLongitude();
+            List<PlaceLikelihood> placeLikelihoods = Awareness.getSnapshotClient(context)
+                    .getPlaces().getResult().getPlaceLikelihoods();
+            Weather weather = Awareness.getSnapshotClient(context).getWeather().getResult()
+                    .getWeather();
+            SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key),
+                    Context.MODE_PRIVATE);
+            SharedPreferences.Editor editor = preferences.edit();
+            editor.putLong(context.getString(R.string.store_lat),Double.doubleToLongBits(latitude));
+            editor.putLong(context.getString(R.string.store_long),Double.doubleToLongBits(longitude));
+            editor.putString(context.getString(R.string.store_place), String.valueOf(placeLikelihoods.get(0).getPlace().getName()));
+            editor.putFloat(context.getString(R.string.store_placelike), placeLikelihoods.get(0).getLikelihood());
+            editor.putFloat(context.getString(R.string.store_temp),weather.getTemperature(Weather.CELSIUS));
+            editor.commit();
+            recordData(latitude, longitude, placeLikelihoods, weather, context);
+        }catch(SecurityException e){
+            Intent i = new Intent(context, RequestPermission.class);
+            context.startActivity(i);
+        }
+    }
     /**
      * Checks if external storage is available on the device.
      *
diff --git a/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java b/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java
index 6d6d28e..91473c7 100644
--- a/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java
+++ b/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java
@@ -170,7 +170,6 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv
         PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(),
                 R.integer.alarm_rc, i, PendingIntent.FLAG_UPDATE_CURRENT);
         am.cancel(pi);
-        getExtraData();
         SaveFile.recordNotification(0, 0, 1,0, this);
         setUpClock();
     }
@@ -240,29 +239,7 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv
     }
 
 
-    private void getExtraData(){
-        try {
-            double latitude = Awareness.getSnapshotClient(this).getLocation()
-                    .getResult().getLocation().getLatitude();
-            double longitude = Awareness.getSnapshotClient(this).getLocation()
-                    .getResult().getLocation().getLongitude();
-            List<PlaceLikelihood> placeLikelihoods = Awareness.getSnapshotClient(this)
-                    .getPlaces().getResult().getPlaceLikelihoods();
-            Weather weather = Awareness.getSnapshotClient(this).getWeather().getResult()
-                    .getWeather();
-            SharedPreferences.Editor editor = preferences.edit();
-            editor.putLong(this.getString(R.string.store_lat),Double.doubleToLongBits(latitude));
-            editor.putLong(this.getString(R.string.store_long),Double.doubleToLongBits(longitude));
-            editor.putString(this.getString(R.string.store_place), String.valueOf(placeLikelihoods.get(0).getPlace().getName()));
-            editor.putFloat(this.getString(R.string.store_placelike), placeLikelihoods.get(0).getLikelihood());
-            editor.putFloat(this.getString(R.string.store_temp),weather.getTemperature(Weather.CELSIUS));
-            editor.commit();
-            SaveFile.recordData(latitude, longitude, placeLikelihoods, weather, this);
-        }catch(SecurityException e){
-            Intent i = new Intent(this, RequestPermission.class);
-            startActivity(i);
-        }
-    }
+
 
     /**
      * Unused as accuracy is not a major factor when using previous values. Required by
diff --git a/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java b/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java
index 279400b..964ed7d 100644
--- a/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java
+++ b/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java
@@ -89,7 +89,6 @@ public class SigMotionDetect extends Service implements StimulusStrategy {
      */
     private int prevMinutes;
 
-    private GoogleApiClient client;
 
 
     /**
@@ -107,9 +106,6 @@ public class SigMotionDetect extends Service implements StimulusStrategy {
      */
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
-        client = new GoogleApiClient.Builder(this)
-                .addApi(Awareness.getSnapshotClient(this).getApi()).build();
-        client.connect();
         am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
         setUpClock();
         setUpDailyProgress();
-- 
GitLab