diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java index 4ea61b44eec0434705f156c97611d3ea74600b09..cd083c2318463fa1cb14d7d89753a836cee58616 100644 --- a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java +++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java @@ -5,6 +5,9 @@ import android.content.SharedPreferences; import android.os.Environment; import android.util.Log; +import com.google.android.gms.awareness.state.Weather; +import com.google.android.gms.location.places.PlaceLikelihood; + import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -16,6 +19,7 @@ import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; +import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -270,6 +274,59 @@ public class SaveFile { } } + + public static void recordData(double latitude, double longitude, + List<PlaceLikelihood> placeLikelihoods, Weather weather, + Context context) { + String places = ""; + String line; + ArrayList<String> contents = new ArrayList<String>(); + for (PlaceLikelihood p : placeLikelihoods) { + places = places + " " + String.valueOf(p.getPlace().getName()) + " " + + String.valueOf(p.getLikelihood() + "\n"); + } + + String writeLine = String.valueOf(latitude) + " " + String.valueOf(longitude) + " " + places + + String.valueOf(weather.getTemperature(Weather.CELSIUS)); + 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) { + contents.add(line + '\n'); + } + } + //Adds the new log to the file up to 1000 items. At 1000 items the oldest is removed + if (file.isFile() & file.canWrite()) { + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + if (contents.size() > 999) { + contents.remove(0); + } + for (String ts : contents) { + bw.write(ts); + } + bw.write(writeLine); + bw.close(); + } + + } catch (IOException e) { + Log.e("FILEERROR", "COULD NOT WRITE TO FILE"); + } + + } + } + /** * 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 90ad4ab4b385619fa57a57de6f9b080074c0b5b5..6d6d28ee091e1cb2841afeddf5fc1240517fe2fd 100644 --- a/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java +++ b/src/main/java/com/notificationFramework/stimulusStrategy/Accelerometer.java @@ -251,11 +251,13 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv Weather weather = Awareness.getSnapshotClient(this).getWeather().getResult() .getWeather(); SharedPreferences.Editor editor = preferences.edit(); - editor.putLong("",Double.doubleToLongBits(latitude)); - editor.putLong("",Double.doubleToLongBits(longitude)); - //editor.putString("", ) place names - //editor.putInt("",) weather so temperature + 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); diff --git a/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java b/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java index 40cbf2136bb473b513c846e021de697be3ab7ec8..8858048e020202378cd629362cff30230e17211f 100644 --- a/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java +++ b/src/main/java/com/notificationFramework/stimulusStrategy/SigMotionDetect.java @@ -143,11 +143,11 @@ public class SigMotionDetect extends Service implements StimulusStrategy { R.integer.alarm_rc, i, 0); SharedPreferences shared = this.getSharedPreferences(getString( R.string.preference_file_key), Context.MODE_PRIVATE); - //int interval = shared.getInt(getString(R.string.daily_goal), getResources().getInteger(R.integer.notify_period_minutes)); - //am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, - // SystemClock.elapsedRealtime() + (1000 * 60 * interval), pi); + int interval = shared.getInt(getString(R.string.daily_goal), getResources().getInteger(R.integer.notify_period_minutes)); + am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, + SystemClock.elapsedRealtime() + (1000 * 60 * interval), pi); //test - am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 3000, pi); + //am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 3000, pi); } /** diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 8fba5625fc28cdb49a153583f7cbbee8a7d1ed1d..6e69134ad39165be02eb76f4414e7df1b9cf749f 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -48,4 +48,9 @@ <string name="avg_ack">Acknowledgements per day</string> <string name="notf_id">Notification ID</string> <string name="feedback">Well done!! Keep moving!</string> + <string name="store_lat">Latitude</string> + <string name="store_long">Longitude</string> + <string name="store_place">Place</string> + <string name="store_placelike">Place Likelihood</string> + <string name="store_temp">Temperature</string> </resources>