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 8be1b322a03f9ed9fbbf89aa15381bb249615d66..6a52999fe4328caf2e22a81069023e816490a32d 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 dccedcf1830fe9f69badcafc9af87549f04efccc..0e50dc91ac29b6b259b1342f4370b98783562cfd 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 91c180c8b981af698188c262e29e5761e9ad8ded..495b4c493c0960835423dceb603c9ecc969a0c18 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 238449315ba6ed5a9b991a18d6e4c1f7210bc75a..5543e495301db000b487e626dc12b58e61391717 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 52fee67eb217578c0d43b0de8722d50fd185e07f..c87e48ecef30750cea0530a0f27bfd6776a3507d 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(); + } + } + }