From ef9e4ddd58584e0f4ef999bc5f996c5f9ad47777 Mon Sep 17 00:00:00 2001 From: Peter De Jonckheere <peter.de-jonckheere.2014@uni.strath.ac.uk> Date: Wed, 4 Jul 2018 14:08:05 +0100 Subject: [PATCH] Refactoring --- .../sedentary/frontEnd/SaveFile.java | 83 +++++++++++++------ .../stimulus/SedentaryStimulus.java | 4 + 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java index 7d87f8e..35cc69e 100644 --- a/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java +++ b/src/main/java/com/notificationFramework/sedentary/frontEnd/SaveFile.java @@ -4,12 +4,20 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Environment; +import android.support.annotation.NonNull; import android.util.Log; import com.google.android.gms.awareness.Awareness; +import com.google.android.gms.awareness.snapshot.LocationResponse; +import com.google.android.gms.awareness.snapshot.PlacesResponse; +import com.google.android.gms.awareness.snapshot.WeatherResponse; 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 com.google.android.gms.tasks.OnCompleteListener; +import com.google.android.gms.tasks.Task; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import java.io.BufferedOutputStream; import java.io.BufferedReader; @@ -20,6 +28,8 @@ import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.io.Serializable; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Calendar; import java.util.List; @@ -84,7 +94,7 @@ public class SaveFile { } } }catch(NullPointerException e){ - + Log.e("SPLIT", "NULL POINTER"); } } //Adds the new log to the file up to 1000 items. At 1000 items the oldest is removed @@ -279,9 +289,16 @@ public class SaveFile { } - public static void recordData(double latitude, double longitude, - List<PlaceLikelihood> placeLikelihoods, Weather weather, - Context context) { + public static void recordData(Context context) { + SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), + 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)); + Type type = new TypeToken<List<PlaceLikelihood>>(){}.getType(); + Gson gson = new Gson(); + String placelikelihood = preferences.getString(context.getString(R.string.store_place), ""); + List<PlaceLikelihood> placeLikelihoods = gson.fromJson(placelikelihood, type); + float temperature = preferences.getFloat(context.getString(R.string.store_temp), 0); String places = ""; String line; ArrayList<String> contents = new ArrayList<String>(); @@ -291,7 +308,7 @@ public class SaveFile { } String writeLine = String.valueOf(latitude) + " " + String.valueOf(longitude) + " " + places - + String.valueOf(weather.getTemperature(Weather.CELSIUS)); + + String.valueOf(temperature); if (isExternalStorageMounted()) { File dir = getDirectory(context); File file = new File(dir, "DATALOG.txt"); @@ -332,30 +349,48 @@ public class SaveFile { } - private static void getExtraData(Context context){ + private static void getExtraData(final 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); + Awareness.getSnapshotClient(context).getLocation().addOnCompleteListener(new OnCompleteListener<LocationResponse>() { + @Override + public void onComplete(@NonNull Task<LocationResponse> task) { + 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(task.getResult().getLocation().getLatitude())); + editor.putLong(context.getString(R.string.store_long),Double.doubleToLongBits(task.getResult().getLocation().getLongitude())); + editor.commit(); + } + + }); + Awareness.getSnapshotClient(context).getPlaces().addOnCompleteListener(new OnCompleteListener<PlacesResponse>() { + @Override + public void onComplete(@NonNull Task<PlacesResponse> task) { + SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), + Context.MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + List<PlaceLikelihood> placeLikelihoods = task.getResult().getPlaceLikelihoods(); + Gson gson = new Gson(); + editor.putString(context.getString(R.string.store_place), + gson.toJson(placeLikelihoods)); + editor.commit(); + } + }); + Awareness.getSnapshotClient(context).getWeather().addOnCompleteListener(new OnCompleteListener<WeatherResponse>() { + @Override + public void onComplete(@NonNull Task<WeatherResponse> task) { + SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), + Context.MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.putFloat(context.getString(R.string.store_temp),task.getResult().getWeather().getTemperature(Weather.CELSIUS)); + editor.commit(); + } + }); + recordData(context); }catch(SecurityException e){ Intent i = new Intent(context, RequestPermission.class); context.startActivity(i); diff --git a/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java b/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java index e84189f..ba174c5 100644 --- a/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java +++ b/src/main/java/com/notificationFramework/stimulus/SedentaryStimulus.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.support.v4.content.LocalBroadcastManager; +import android.util.Log; import com.notificationFramework.sedentary.frontEnd.R; import com.notificationFramework.sedentary.frontEnd.SaveFile; @@ -90,9 +91,12 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus { 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)) && checkAcknowledged(shared, context)) { + Log.d("SEDSTIM", "SENDING NOTIFICATION"); Intent i = new Intent(context.getApplicationContext(), com.notificationFramework.notification.SedentaryNotification.class); context.getApplicationContext().startService(i); + } else{ + Log.d("SEDSTIM", "SENDING NOTIFICATION FAILED"); } if (strategy == null) { -- GitLab