Commit bc5e1a9b authored by Mattia Ducci's avatar Mattia Ducci

implementato aggiornamento di userSettings

parent 0a11b6f8
package framework.everywaretechnologies.it.icarusandroid; package framework.everywaretechnologies.it.icarusandroid;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
...@@ -8,6 +9,7 @@ import android.widget.EditText; ...@@ -8,6 +9,7 @@ import android.widget.EditText;
import android.widget.Switch; import android.widget.Switch;
import framework.everywaretechnologies.it.icarus.Icarus; import framework.everywaretechnologies.it.icarus.Icarus;
import framework.everywaretechnologies.it.icarus.UserSettingsResource;
//classe di test Icarus framework //classe di test Icarus framework
...@@ -16,7 +18,13 @@ public class MainActivity extends AppCompatActivity { ...@@ -16,7 +18,13 @@ public class MainActivity extends AppCompatActivity {
private final static String CLASS_NAME="MainActivity"; private final static String CLASS_NAME="MainActivity";
private SharedPreferences myPrefs;
private boolean lastBoolPref;
private int lastIntPref;
private Switch enteredExitedScreenTrackSwitch; private Switch enteredExitedScreenTrackSwitch;
private Switch boolPrefSwitch;
private Switch intPrefSwitch;
private Icarus instance; private Icarus instance;
@Override @Override
...@@ -28,6 +36,20 @@ public class MainActivity extends AppCompatActivity { ...@@ -28,6 +36,20 @@ public class MainActivity extends AppCompatActivity {
instance.setApplicationContext(getApplicationContext()); instance.setApplicationContext(getApplicationContext());
enteredExitedScreenTrackSwitch=findViewById(R.id.entered_exited_screen_track_switch); 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 ----------------------- //------------------------- UI EVENT -----------------------
...@@ -43,4 +65,26 @@ public class MainActivity extends AppCompatActivity { ...@@ -43,4 +65,26 @@ public class MainActivity extends AppCompatActivity {
instance.trackScreen(MainActivity.this,eventType); 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);
}
} }
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Track screen" android:text="Track screen"
android:textColor="@android:color/holo_red_light" android:textColor="@android:color/holo_red_light"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
...@@ -26,7 +27,8 @@ ...@@ -26,7 +27,8 @@
android:splitTrack="false" android:splitTrack="false"
android:text="Tipo di evento" android:text="Tipo di evento"
android:textOff="EXITED" android:textOff="EXITED"
android:textOn="ENTERED" /> android:textOn="ENTERED"
android:textSize="18sp" />
<Button <Button
android:id="@+id/btn_send_screen_track_event" android:id="@+id/btn_send_screen_track_event"
...@@ -35,4 +37,41 @@ ...@@ -35,4 +37,41 @@
android:onClick="onBtnScreenEventTrack" android:onClick="onBtnScreenEventTrack"
android:text="Invia screen track event" /> android:text="Invia screen track event" />
<TextView
android:id="@+id/lbl_user_prefs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User preferences"
android:textColor="@android:color/holo_red_light"
android:textSize="24sp" />
<Switch
android:id="@+id/pref_bool_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showText="true"
android:splitTrack="false"
android:text="PrefBool"
android:textOff="false"
android:textOn="true"
android:textSize="18sp" />
<Switch
android:id="@+id/pref_int_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showText="true"
android:splitTrack="false"
android:text="PrefInt"
android:textOff="1"
android:textOn="2"
android:textSize="18sp" />
<Button
android:id="@+id/btn_send_pref_changes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onBtnUserPrefChanges"
android:text="Invia pref changes" />
</android.widget.LinearLayout> </android.widget.LinearLayout>
\ No newline at end of file
...@@ -21,7 +21,6 @@ android { ...@@ -21,7 +21,6 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
} }
} }
} }
dependencies { dependencies {
......
...@@ -20,14 +20,17 @@ import org.json.JSONException; ...@@ -20,14 +20,17 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map;
import java.util.Queue; import java.util.Queue;
import java.util.Set;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
public class Icarus { public class Icarus {
enum Categories{ enum Categories{ //categorie di evento
APPLICATION,ACTION,NOTIFICATION; APPLICATION,ACTION,NOTIFICATION;
} }
...@@ -63,9 +66,13 @@ public class Icarus { ...@@ -63,9 +66,13 @@ public class Icarus {
//----------------- LOG PREFERENCE CHANGES ----------------------- //----------------- LOG PREFERENCE CHANGES -----------------------
public void logUserSettings(SharedPreferences mySharedPrefs){ public void logUserSettings(UserSettingsResource userSettingsResource){
Log.d(curContext.getString(R.string.icarus_class),"log pref changes"); Log.d(curContext.getString(R.string.icarus_class),"log pref changes");
JSONObject prefsChangesToLog=userSettingsResource.getUserSettingsChangesInJSONFormat();
postIcarusHttpRequest(prefsChangesToLog);
userSettingsResource.clear();
} }
//---------------------- LOG SCREEN TRACKING ------------------------------ //---------------------- LOG SCREEN TRACKING ------------------------------
...@@ -99,7 +106,7 @@ public class Icarus { ...@@ -99,7 +106,7 @@ public class Icarus {
//---------------------- LOG EVENT TRACKING -------------------------- //---------------------- 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"); Log.d(curContext.getString(R.string.icarus_class),"log event");
...@@ -122,7 +129,7 @@ public class Icarus { ...@@ -122,7 +129,7 @@ public class Icarus {
if(pendingResources.peek() != null){ //se ho qualche elemento pendente da inviare 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(); JSONObject pendingResource=pendingResources.poll();
postIcarusHttpRequest(pendingResource); postIcarusHttpRequest(pendingResource);
} }
...@@ -135,6 +142,7 @@ public class Icarus { ...@@ -135,6 +142,7 @@ public class Icarus {
Log.d(curContext.getString(R.string.icarus_class),"onVolley response NEGATIVE"); 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(); pendingResourceLock.lock();
pendingResources.add(icarusPayload); pendingResources.add(icarusPayload);
pendingResourceLock.unlock(); pendingResourceLock.unlock();
...@@ -143,4 +151,19 @@ public class Icarus { ...@@ -143,4 +151,19 @@ public class Icarus {
mVolleyRequestQueue.add(jsonObjectRequest);//faccio effettivamente la richiesta 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();
}
} }
package framework.everywaretechnologies.it.icarus; 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<String,String> prefsOldNew; //il secondo String è in realtà un JSONObject convertito in String
private SharedPreferences callerSharedPrefs;
public UserSettingsResource(SharedPreferences _callerSharedPrefs){
super(); 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();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment