From 20b6a1b2acb6831b46639a7945d23074dd2e0a45 Mon Sep 17 00:00:00 2001 From: Mattia Date: Mon, 30 Apr 2018 18:33:01 +0200 Subject: [PATCH] aggiunto ai log delle preference anche delle preferenze di sistema e iniziato ad implementare il tracking degli eventi --- .../it/icarusandroid/MainActivity.java | 2 +- .../it/icarus/EventTrackResource.java | 103 +++++++++++++++++- .../it/icarus/Icarus.java | 9 +- .../it/icarus/IcarusResource.java | 2 + .../it/icarus/UserSettingsResource.java | 63 ++++++++++- 5 files changed, 173 insertions(+), 6 deletions(-) diff --git a/IcarusAndroid/app/src/main/java/framework/everywaretechnologies/it/icarusandroid/MainActivity.java b/IcarusAndroid/app/src/main/java/framework/everywaretechnologies/it/icarusandroid/MainActivity.java index 8be1b32..6a52999 100644 --- a/IcarusAndroid/app/src/main/java/framework/everywaretechnologies/it/icarusandroid/MainActivity.java +++ b/IcarusAndroid/app/src/main/java/framework/everywaretechnologies/it/icarusandroid/MainActivity.java @@ -80,7 +80,7 @@ public class MainActivity extends AppCompatActivity { if(intPrefSwitch.isChecked()) intSwitchValue=Integer.valueOf(intPrefSwitch.getTextOn().toString()); else intSwitchValue=Integer.valueOf(intPrefSwitch.getTextOff().toString()); - UserSettingsResource userSettingsResource=new UserSettingsResource(myPrefs); + UserSettingsResource userSettingsResource=new UserSettingsResource(myPrefs,MainActivity.this); userSettingsResource.addBoolean("boolPref",boolSwitchValue); userSettingsResource.addInt("intPref",intSwitchValue); diff --git a/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/EventTrackResource.java b/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/EventTrackResource.java index dccedcf..0e50dc9 100644 --- a/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/EventTrackResource.java +++ b/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/EventTrackResource.java @@ -1,9 +1,110 @@ package framework.everywaretechnologies.it.icarus; +import org.json.JSONException; +import org.json.JSONObject; + public class EventTrackResource extends IcarusResource { - public EventTrackResource(){ + private String eventName; + private String eventCategory; + private String eventLabel; + + private int heading; + private int course; + private int speed; + + //i due successivi attributi sono quelli ritornati dall'ActivityRecognition di Google che dev'essere implementata nell'app chiamante + //convertiti da quelli di Google a quelli del server.. vedi iMoveAndroid per il metodo di mapping tra i valori ritornati da Google e quelli + //per il server di ewtech. Fare copia incolla del metodo in progetti futuri. Non lo metto qua perchè non ho accesso ai codici delle attività + //non avendo la dipendenza all'ActivityRecognition all'interno di questa libreria + private String mostProbableUserActivityName; + private int confidence; + + private boolean accessibilityEnabled; + + private String[] userActivityToTrack; + + public EventTrackResource(String _eventName, String _eventCategory, String _eventLabel, int _heading, int _course, int _speed, + String _mostProbableUserActivityName, int _confidence, boolean _accessibilityEnabled){ super(); + + eventName=_eventName; + eventCategory=_eventCategory; + eventLabel=_eventLabel; + heading=_heading; + course=_course; + speed=_speed; + mostProbableUserActivityName=_mostProbableUserActivityName; + confidence=_confidence; + accessibilityEnabled=_accessibilityEnabled; + + userActivityToTrack=new String[]{"stationary","walking","running","automotive","cycling","unknown"}; } + + public JSONObject getEventTrackResourceInJSONFormat(){ + + JSONObject userActivityJSONObject=new JSONObject(); + + try{ + + for(String activityType: userActivityToTrack){ + + if(activityType.equalsIgnoreCase(mostProbableUserActivityName)) + userActivityJSONObject.put(activityType,true); + else + userActivityJSONObject.put(activityType,false); + + } + userActivityJSONObject.put("confidence",confidence); + userActivityJSONObject.put("startTimestamp",System.currentTimeMillis()); + + } catch (JSONException jsonException){ + + jsonException.printStackTrace(); + } + + JSONObject directionJSONObject=new JSONObject(); + + try { + + directionJSONObject.put("heading",heading); //0..360 + directionJSONObject.put("course",course); //0..360 + + } catch (JSONException jsonException){ + + jsonException.printStackTrace(); + } + + JSONObject eventContextJSONObject=new JSONObject(); + + try { + + eventContextJSONObject.put("speed",speed); + eventContextJSONObject.put("activity",userActivityJSONObject); + eventContextJSONObject.put("direction",directionJSONObject); + + } catch (JSONException jsonException){ + + jsonException.printStackTrace(); + } + + JSONObject toSend=new JSONObject(); + + try { + + toSend.put("EventName",eventName); + toSend.put("EventCategory",eventCategory); + toSend.put("EventLabel",eventLabel+"TalkBack"+(accessibilityEnabled?"_ON":"_OFF")); + toSend.put("EventContext",eventContextJSONObject); + + } catch (JSONException jsonException){ + + jsonException.printStackTrace(); + } + + return toSend; + } + + } diff --git a/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/Icarus.java b/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/Icarus.java index 91c180c..495b4c4 100644 --- a/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/Icarus.java +++ b/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/Icarus.java @@ -30,11 +30,13 @@ import java.util.function.Consumer; public class Icarus { - enum Categories{ //categorie di evento + enum EventCategories{ //categorie di evento APPLICATION,ACTION,NOTIFICATION; } + + private final static String ICARUS_URL="https://webdev.ewlab.di.unimi.it/icarus/str"; private static final Icarus ourInstance = new Icarus(); @@ -71,7 +73,8 @@ public class Icarus { Log.d(curContext.getString(R.string.icarus_class),"log pref changes"); JSONObject prefsChangesToLog=userSettingsResource.getUserSettingsChangesInJSONFormat(); - postIcarusHttpRequest(prefsChangesToLog); + if(prefsChangesToLog.length()>0) //se non ho un JSON vuoto, cioè qualche pref è cambiata + postIcarusHttpRequest(prefsChangesToLog); userSettingsResource.clear(); } @@ -106,7 +109,7 @@ public class Icarus { //---------------------- LOG EVENT TRACKING -------------------------- - public void logEvent(String eventName,String category,String label){ //TODO: ci saranno altri parametri da passargli come quello dell'attività + public void logEvent(EventTrackResource eventTrackResource){ Log.d(curContext.getString(R.string.icarus_class),"log event"); diff --git a/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/IcarusResource.java b/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/IcarusResource.java index 2384493..5543e49 100644 --- a/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/IcarusResource.java +++ b/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/IcarusResource.java @@ -1,5 +1,7 @@ package framework.everywaretechnologies.it.icarus; +import android.content.Context; + public abstract class IcarusResource { public IcarusResource(){} diff --git a/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/UserSettingsResource.java b/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/UserSettingsResource.java index 52fee67..c87e48e 100644 --- a/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/UserSettingsResource.java +++ b/IcarusAndroid/icarus/src/main/java/framework/everywaretechnologies/it/icarus/UserSettingsResource.java @@ -1,6 +1,9 @@ package framework.everywaretechnologies.it.icarus; +import android.content.Context; import android.content.SharedPreferences; +import android.provider.Settings; +import android.util.Log; import org.json.JSONException; import org.json.JSONObject; @@ -10,15 +13,19 @@ import java.util.Map; public class UserSettingsResource extends IcarusResource { //classe da non serializzare direttamente con Gson, ma pensata per essere compilata dall'app chiamante + private final static String USER_SETTINGS_RES_CLASS="UserSettingsResource"; + private Map prefsOldNew; //il secondo String è in realtà un JSONObject convertito in String private SharedPreferences callerSharedPrefs; + private Context c; - public UserSettingsResource(SharedPreferences _callerSharedPrefs){ + public UserSettingsResource(SharedPreferences _callerSharedPrefs,Context _c){ super(); prefsOldNew=new HashMap<>(); callerSharedPrefs=_callerSharedPrefs; //così posso modificare le SharedPreferences dell'app chiamante + c=_c; } //a seconda del tipo di preferenza il chiamante invocherà uno di questi metodi @@ -215,6 +222,7 @@ public class UserSettingsResource extends IcarusResource { //classe da non seria public JSONObject getUserSettingsChangesInJSONFormat(){ + checkSystemPreferences(); return new JSONObject(prefsOldNew); } @@ -223,4 +231,57 @@ public class UserSettingsResource extends IcarusResource { //classe da non seria prefsOldNew.clear(); } + private void checkSystemPreferences(){ + + int accessibilityDisplayColorInversionEnabled=-1; + try{ + + accessibilityDisplayColorInversionEnabled=Settings.Secure.getInt(c.getContentResolver(), Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED); + int oldAccessibilityDisplayColorInversion=callerSharedPrefs.getInt("accessibility_color_inversion_enabled",-1); + + if(oldAccessibilityDisplayColorInversion!=accessibilityDisplayColorInversionEnabled){ + + JSONObject jsonObject=new JSONObject(); + jsonObject.put("old",oldAccessibilityDisplayColorInversion); + jsonObject.put("new",accessibilityDisplayColorInversionEnabled); + prefsOldNew.put("accessibility_color_inversion_enabled",jsonObject.toString()); + + SharedPreferences.Editor editor=callerSharedPrefs.edit(); + editor.putInt("accessibility_color_inversion_enabled",accessibilityDisplayColorInversionEnabled); + editor.apply(); + } + + } catch (Settings.SettingNotFoundException e){ + Log.i(USER_SETTINGS_RES_CLASS,"Setting Accessibility color inversion not found"); + + } catch (JSONException e) { + e.printStackTrace(); + } + + + float fontSize=-1; + try { + + fontSize=Settings.System.getFloat(c.getContentResolver(), Settings.System.FONT_SCALE); + float oldFontSize=callerSharedPrefs.getFloat("font_size",-1); + + if(oldFontSize!=fontSize){ //se dall'ultimo aggiornamento delle pref è cambiato + + JSONObject jsonObject=new JSONObject(); + jsonObject.put("old",oldFontSize); + jsonObject.put("new",fontSize); + prefsOldNew.put("font_size",jsonObject.toString()); + + SharedPreferences.Editor editor=callerSharedPrefs.edit(); + editor.putFloat("font_size",fontSize); + editor.apply(); + } + } catch (Settings.SettingNotFoundException e){ + Log.i(USER_SETTINGS_RES_CLASS,"Setting Font size not found"); + + } catch (JSONException e) { + e.printStackTrace(); + } + } + } -- 2.18.1