From 982cf1605e049c89bdc8ecd49c0612b5f0bbc004 Mon Sep 17 00:00:00 2001
From: Peter De Jonckheere <peter.de-jonckheere.2014@uni.strath.ac.uk>
Date: Wed, 11 Jul 2018 16:35:24 +0100
Subject: [PATCH] Get 2 more places from file

---
 .../sedentary/frontEnd/DataAnalysis.java      |  9 ++-
 .../sedentary/frontEnd/Place.java             | 31 ++++++++++
 .../sedentary/frontEnd/SaveFile.java          | 56 +++++++++++++++++--
 3 files changed, 88 insertions(+), 8 deletions(-)
 create mode 100644 src/main/java/com/notificationFramework/sedentary/frontEnd/Place.java

diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java
index d7bf75a..fe504c3 100644
--- a/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java
+++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/DataAnalysis.java
@@ -10,8 +10,11 @@ import android.util.Log;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
+import java.util.List;
 import java.util.Locale;
 
+import static com.notificationFramework.sedentary.frontEnd.SaveFile.getPlaces;
+
 /**
  * Created by Peter De Jonckheere on 19/06/2018.
  */
@@ -69,7 +72,9 @@ public class DataAnalysis {
 
     }
 
-    private void placeAnalysis(){
-        
+    private void placeAnalysis(Context context){
+        List<Place> places = SaveFile.getPlaces(context);
+
     }
+
 }
\ No newline at end of file
diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/Place.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/Place.java
new file mode 100644
index 0000000..66a3e2a
--- /dev/null
+++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/Place.java
@@ -0,0 +1,31 @@
+package com.notificationFramework.sedentary.frontEnd;
+
+/**
+ * Created by pharmacy on 11/07/2018.
+ */
+
+public class Place {
+
+    private String name;
+    private float likelihood;
+    private int type;
+
+    public Place(String name, float likelihood, int type){
+        this.name = name;
+        this.likelihood = likelihood;
+        this.type = type;
+    }
+    public String getName(){
+        return name;
+    }
+
+    public float getLikelihood(){
+        return likelihood;
+    }
+
+    public int getType(){
+        return type;
+    }
+
+}
+
diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java
index 8966aee..cae3153 100644
--- a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java
+++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java
@@ -298,14 +298,23 @@ public class SaveFile {
                 Context.MODE_PRIVATE);
         double latitude = Double.longBitsToDouble(preferences.getLong(context.getString(R.string.store_lat), 0));
         double longitude = Double.longBitsToDouble(preferences.getLong(context.getString(R.string.store_long), 0));
-        String place = preferences.getString(context.getString(R.string.store_place), "");
-        String placeType = String.valueOf(preferences.getInt(context.getString(R.string.store_place_type), 0));
-        float temperature = preferences.getFloat(context.getString(R.string.store_temp), 0);
         String places = "";
+        for(int i  = 0 ; i < 3; i++) {
+            try {
+                String place = preferences.getString(context.getString(R.string.store_place) + String.valueOf(i), "");
+                place = place.replace( " ", "");
+                String placeType = String.valueOf(preferences.getInt(context.getString(R.string.store_place_type) + String.valueOf(i), 0));
+                String placeLikelihood = String.valueOf(preferences.getFloat(context.getString(R.string.store_place_like) + String.valueOf(i), 0));
+                places = places + place + " " + placeType + " " + placeLikelihood + " ";
+            }catch(NullPointerException | IndexOutOfBoundsException e){
+
+            }
+            }
+        float temperature = preferences.getFloat(context.getString(R.string.store_temp), 0);
         String line;
         ArrayList<String> contents = new ArrayList<String>();
-        String writeLine = String.valueOf(latitude) + " " + String.valueOf(longitude) + " " + place
-                + " " + placeType + " " + String.valueOf(temperature);
+        String writeLine = String.valueOf(latitude) + " " + String.valueOf(longitude) + " " + places
+                + String.valueOf(temperature);
         if (isExternalStorageMounted()) {
             File dir = getDirectory(context);
             File file = new File(dir, "DATALOG.txt");
@@ -387,7 +396,7 @@ public class SaveFile {
                                     + String.valueOf(i), String.valueOf(place.getName()));
                             editor.putInt(context.getString(R.string.store_place_type)
                                     + String.valueOf(i), place.getPlaceTypes().get(i));
-                        } catch (NullPointerException e) {
+                        } catch (NullPointerException | IndexOutOfBoundsException e) {
 
                         }
                     }
@@ -410,6 +419,41 @@ public class SaveFile {
             context.startActivity(i);
         }
     }
+
+    static List<com.notificationFramework.sedentary.frontEnd.Place> getPlaces(Context context){
+        String line = "";
+        String[] items;
+        List<com.notificationFramework.sedentary.frontEnd.Place> places = new ArrayList<>();
+        if (isExternalStorageMounted()) {
+            File dir = getDirectory(context);
+            File file = new File(dir, "DATALOG.txt");
+            //Creates a new file if one doesn't already exist
+            if (!file.exists()) {
+                try {
+                    file.createNewFile();
+                } catch (IOException e) {
+                    Log.e("FILE_ERROR", "COULDN'T CREATE NEW FILE");
+                }
+            }
+            //Reads the current contents of the log file
+            try {
+                if (file.isFile() && file.canRead()) {
+                    BufferedReader br = new BufferedReader(new FileReader(file));
+                    while ((line = br.readLine()) != null) {
+                        items = line.split(" ");
+                        //Need to get other 2 places
+                        com.notificationFramework.sedentary.frontEnd.Place place = new com.notificationFramework.sedentary.frontEnd.Place(
+                                items[2], Float.parseFloat(items[4]), Integer.parseInt(items[3]));
+                        places.add(place);
+                    }
+                }
+            }catch(IOException e){
+
+            }
+        }
+        return places;
+    }
+
     /**
      * Checks if external storage is available on the device.
      *
-- 
GitLab