diff --git a/IcarusAndroid/.idea/caches/build_file_checksums.ser b/IcarusAndroid/.idea/caches/build_file_checksums.ser
index ba777d47a33b1c13fa587329c5deccb710ae2ad1..88145cc900a6004c46abc7506b510ba51ec50728 100644
Binary files a/IcarusAndroid/.idea/caches/build_file_checksums.ser and b/IcarusAndroid/.idea/caches/build_file_checksums.ser differ
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 57e2062040c5dacfe77ce035048e095f6bbc9637..8be1b322a03f9ed9fbbf89aa15381bb249615d66 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
@@ -1,5 +1,6 @@
package framework.everywaretechnologies.it.icarusandroid;
+import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@@ -8,6 +9,7 @@ import android.widget.EditText;
import android.widget.Switch;
import framework.everywaretechnologies.it.icarus.Icarus;
+import framework.everywaretechnologies.it.icarus.UserSettingsResource;
//classe di test Icarus framework
@@ -16,7 +18,13 @@ public class MainActivity extends AppCompatActivity {
private final static String CLASS_NAME="MainActivity";
+ private SharedPreferences myPrefs;
+ private boolean lastBoolPref;
+ private int lastIntPref;
+
private Switch enteredExitedScreenTrackSwitch;
+ private Switch boolPrefSwitch;
+ private Switch intPrefSwitch;
private Icarus instance;
@Override
@@ -28,6 +36,20 @@ public class MainActivity extends AppCompatActivity {
instance.setApplicationContext(getApplicationContext());
enteredExitedScreenTrackSwitch=findViewById(R.id.entered_exited_screen_track_switch);
+ boolPrefSwitch=findViewById(R.id.pref_bool_switch);
+ intPrefSwitch=findViewById(R.id.pref_int_switch);
+
+ //creo due sharedPreferences fittizie per poter testare il cambio di pref di Icarus
+ myPrefs=getSharedPreferences("MyPref",0);
+ lastBoolPref=myPrefs.getBoolean("boolPref",false);
+ lastIntPref=myPrefs.getInt("intPref",-1);
+
+ //setto gli switch in base allo stato nelle shared preferences
+ if(lastBoolPref) boolPrefSwitch.setChecked(true);
+ else boolPrefSwitch.setChecked(false);
+
+ if(lastIntPref==1)intPrefSwitch.setChecked(false);
+ else intPrefSwitch.setChecked(true);
}
//------------------------- UI EVENT -----------------------
@@ -43,4 +65,26 @@ public class MainActivity extends AppCompatActivity {
instance.trackScreen(MainActivity.this,eventType);
}
+
+ public void onBtnUserPrefChanges(View view){
+
+ Log.d(CLASS_NAME,"on btn log user changes");
+
+ //prendo i valori degli switch
+ boolean boolSwitchValue;
+ int intSwitchValue;
+
+ if(boolPrefSwitch.isChecked()) boolSwitchValue=Boolean.valueOf(boolPrefSwitch.getTextOn().toString());
+ else boolSwitchValue=Boolean.valueOf(boolPrefSwitch.getTextOff().toString());
+
+ if(intPrefSwitch.isChecked()) intSwitchValue=Integer.valueOf(intPrefSwitch.getTextOn().toString());
+ else intSwitchValue=Integer.valueOf(intPrefSwitch.getTextOff().toString());
+
+ UserSettingsResource userSettingsResource=new UserSettingsResource(myPrefs);
+
+ userSettingsResource.addBoolean("boolPref",boolSwitchValue);
+ userSettingsResource.addInt("intPref",intSwitchValue);
+
+ instance.logUserSettings(userSettingsResource);
+ }
}
diff --git a/IcarusAndroid/app/src/main/res/layout/activity_main.xml b/IcarusAndroid/app/src/main/res/layout/activity_main.xml
index 3cc99b1a7e64876be5f81b71606e5c206f77cdd5..873f17b6841d845281107943329f949b18599e98 100644
--- a/IcarusAndroid/app/src/main/res/layout/activity_main.xml
+++ b/IcarusAndroid/app/src/main/res/layout/activity_main.xml
@@ -13,6 +13,7 @@
android:layout_height="wrap_content"
android:text="Track screen"
android:textColor="@android:color/holo_red_light"
+ android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
@@ -26,7 +27,8 @@
android:splitTrack="false"
android:text="Tipo di evento"
android:textOff="EXITED"
- android:textOn="ENTERED" />
+ android:textOn="ENTERED"
+ android:textSize="18sp" />
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/IcarusAndroid/icarus/build.gradle b/IcarusAndroid/icarus/build.gradle
index 4bba02278983839eb0692dd5545b325e05b13090..fb0c77bb53dcb5ab9bda6cb9d5837564078f8790 100644
--- a/IcarusAndroid/icarus/build.gradle
+++ b/IcarusAndroid/icarus/build.gradle
@@ -21,7 +21,6 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
-
}
dependencies {
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 62b011d6baa7a131c889310524de644197fd1c83..91c180c8b981af698188c262e29e5761e9ad8ded 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
@@ -20,14 +20,17 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.LinkedList;
+import java.util.Map;
import java.util.Queue;
+import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Consumer;
public class Icarus {
- enum Categories{
+ enum Categories{ //categorie di evento
APPLICATION,ACTION,NOTIFICATION;
}
@@ -63,9 +66,13 @@ public class Icarus {
//----------------- LOG PREFERENCE CHANGES -----------------------
- public void logUserSettings(SharedPreferences mySharedPrefs){
+ public void logUserSettings(UserSettingsResource userSettingsResource){
Log.d(curContext.getString(R.string.icarus_class),"log pref changes");
+
+ JSONObject prefsChangesToLog=userSettingsResource.getUserSettingsChangesInJSONFormat();
+ postIcarusHttpRequest(prefsChangesToLog);
+ userSettingsResource.clear();
}
//---------------------- LOG SCREEN TRACKING ------------------------------
@@ -99,7 +106,7 @@ public class Icarus {
//---------------------- LOG EVENT TRACKING --------------------------
- public void logEvent(String eventName,String category,String label){
+ public void logEvent(String eventName,String category,String label){ //TODO: ci saranno altri parametri da passargli come quello dell'attività
Log.d(curContext.getString(R.string.icarus_class),"log event");
@@ -122,7 +129,7 @@ public class Icarus {
if(pendingResources.peek() != null){ //se ho qualche elemento pendente da inviare
- Log.d(curContext.getString(R.string.icarus_class),"messaggi pendenti");
+ Log.d(curContext.getString(R.string.icarus_class),"messaggio pendente");
JSONObject pendingResource=pendingResources.poll();
postIcarusHttpRequest(pendingResource);
}
@@ -135,6 +142,7 @@ public class Icarus {
Log.d(curContext.getString(R.string.icarus_class),"onVolley response NEGATIVE");
+ //se per qualche motivo la richiesta http non va a buon fine, salvo il payload che avrei dovuto mandare in cache
pendingResourceLock.lock();
pendingResources.add(icarusPayload);
pendingResourceLock.unlock();
@@ -143,4 +151,19 @@ public class Icarus {
mVolleyRequestQueue.add(jsonObjectRequest);//faccio effettivamente la richiesta
}
+
+ //SharedPreferences utility methods
+ private void editIntPref(SharedPreferences settings,String nomePref,int newValue){
+
+ SharedPreferences.Editor editor=settings.edit();
+ editor.putInt(nomePref,newValue);
+ editor.commit();
+ }
+
+ private void editBoolPref(SharedPreferences settings,String nomePref,boolean newValue){
+
+ SharedPreferences.Editor editor=settings.edit();
+ editor.putBoolean(nomePref,newValue);
+ editor.commit();
+ }
}
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 4b2ba0ffa2cfbc57b23944f2fd98525095e1be59..52fee67eb217578c0d43b0de8722d50fd185e07f 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,9 +1,226 @@
package framework.everywaretechnologies.it.icarus;
-public class UserSettingsResource extends IcarusResource {
+import android.content.SharedPreferences;
- public UserSettingsResource(){
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.util.HashMap;
+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 Map prefsOldNew; //il secondo String è in realtà un JSONObject convertito in String
+ private SharedPreferences callerSharedPrefs;
+
+ public UserSettingsResource(SharedPreferences _callerSharedPrefs){
super();
+
+ prefsOldNew=new HashMap<>();
+ callerSharedPrefs=_callerSharedPrefs; //così posso modificare le SharedPreferences dell'app chiamante
}
+
+ //a seconda del tipo di preferenza il chiamante invocherà uno di questi metodi
+
+ public void addBoolean(String prefName, boolean newValue ) {
+
+ JSONObject jsonObject=new JSONObject();
+ try {
+
+ if (callerSharedPrefs.contains(prefName + "_old")) { //se ho già toccato quella pref in passato
+
+ boolean oldValue=callerSharedPrefs.getBoolean(prefName+"_old",false);
+
+ if(oldValue!=newValue){
+
+ jsonObject.put("old", oldValue);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putBoolean(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } else { //se è la prima volta che l'utente tocca quella pref
+
+ jsonObject.put("old", null);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putBoolean(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void addInt(String prefName, int newValue ){
+
+ JSONObject jsonObject=new JSONObject();
+ try {
+
+ if (callerSharedPrefs.contains(prefName + "_old")) {
+
+ int oldValue=callerSharedPrefs.getInt(prefName+"_old",-1);
+
+ if(oldValue!=newValue){
+
+ jsonObject.put("old", oldValue);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putInt(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } else { //se è la prima volta che l'utente tocca quella pref
+
+ jsonObject.put("old", null);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putInt(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void addFloat(String prefName, float newValue ){
+
+ JSONObject jsonObject=new JSONObject();
+ try {
+
+ if (callerSharedPrefs.contains(prefName + "_old")) {
+
+ float oldValue=callerSharedPrefs.getFloat(prefName+"_old",-1);
+
+ if(oldValue!=newValue){
+
+ jsonObject.put("old", oldValue);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putFloat(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } else { //se è la prima volta che l'utente tocca quella pref
+
+ jsonObject.put("old", null);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putFloat(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void addLong(String prefName, long newValue ){
+
+ JSONObject jsonObject=new JSONObject();
+ try {
+
+ if (callerSharedPrefs.contains(prefName + "_old")) {
+
+ long oldValue=callerSharedPrefs.getLong(prefName+"_old",-1);
+
+ if(oldValue!=newValue){
+
+ jsonObject.put("old", oldValue);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putFloat(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } else { //se è la prima volta che l'utente tocca quella pref
+
+ jsonObject.put("old", null);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putFloat(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void addString(String prefName, String newValue ){
+
+ JSONObject jsonObject=new JSONObject();
+ try {
+
+ if (callerSharedPrefs.contains(prefName + "_old")) {
+
+ String oldValue=callerSharedPrefs.getString(prefName+"_old","");
+
+ if(!oldValue.equals(newValue)){
+
+ jsonObject.put("old", oldValue);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putString(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } else { //se è la prima volta che l'utente tocca quella pref
+
+ jsonObject.put("old", null);
+ jsonObject.put("new",newValue);
+ prefsOldNew.put(prefName,jsonObject.toString());
+
+ //update shared preferences dell'app chiamante
+ SharedPreferences.Editor editor=callerSharedPrefs.edit();
+ editor.putString(prefName+"_old",newValue);
+ editor.apply(); //apply aggiorna le prefs in bg
+ }
+
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public JSONObject getUserSettingsChangesInJSONFormat(){
+
+ return new JSONObject(prefsOldNew);
+ }
+
+ public void clear(){ //reset di tutti i cambiamenti delle pref
+
+ prefsOldNew.clear();
+ }
+
}