Commit 547061d4 authored by Mattia Ducci's avatar Mattia Ducci

completamento dei JSON dei dati degli utenti con info sull'app e sul timestamp

parent 20b6a1b2
......@@ -63,7 +63,7 @@ public class MainActivity extends AppCompatActivity {
else
eventType=enteredExitedScreenTrackSwitch.getTextOff().toString();
instance.trackScreen(MainActivity.this,eventType);
instance.trackScreen(MainActivity.this,eventType,true);
}
public void onBtnUserPrefChanges(View view){
......@@ -85,6 +85,6 @@ public class MainActivity extends AppCompatActivity {
userSettingsResource.addBoolean("boolPref",boolSwitchValue);
userSettingsResource.addInt("intPref",intSwitchValue);
instance.logUserSettings(userSettingsResource);
instance.logUserSettings(userSettingsResource,true);
}
}
......@@ -4,7 +4,11 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityManager;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
......@@ -20,9 +24,12 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
......@@ -68,20 +75,23 @@ public class Icarus {
//----------------- LOG PREFERENCE CHANGES -----------------------
public void logUserSettings(UserSettingsResource userSettingsResource){
public void logUserSettings(UserSettingsResource userSettingsResource, boolean debug){
Log.d(curContext.getString(R.string.icarus_class),"log pref changes");
JSONObject prefsChangesToLog=userSettingsResource.getUserSettingsChangesInJSONFormat();
if(prefsChangesToLog.length()>0) //se non ho un JSON vuoto, cioè qualche pref è cambiata
postIcarusHttpRequest(prefsChangesToLog);
if(prefsChangesToLog.length()>0){ //se non ho un JSON vuoto, cioè qualche pref è cambiata
postIcarusHttpRequest(prefsChangesToLog,debug);
Log.d(curContext.getString(R.string.icarus_class),"log pref changes -> Volley");
}
userSettingsResource.clear();
}
//---------------------- LOG SCREEN TRACKING ------------------------------
public void trackScreen(Context c,String screenEventType){
public void trackScreen(Context c,String screenEventType,boolean debug){
Log.d(curContext.getString(R.string.icarus_class),"log screen track");
......@@ -96,7 +106,7 @@ public class Icarus {
try {
JSONObject icarusPayload=new JSONObject(screenTrackJson);
postIcarusHttpRequest(icarusPayload);
postIcarusHttpRequest(icarusPayload,debug);
Log.d(curContext.getString(R.string.icarus_class),"log screen track -> Volley");
......@@ -109,7 +119,7 @@ public class Icarus {
//---------------------- LOG EVENT TRACKING --------------------------
public void logEvent(EventTrackResource eventTrackResource){
public void logEvent(EventTrackResource eventTrackResource, boolean debug){
Log.d(curContext.getString(R.string.icarus_class),"log event");
......@@ -119,9 +129,11 @@ public class Icarus {
//--------------------------- UTILITY ----------------------------------
private void postIcarusHttpRequest(final JSONObject icarusPayload){
private void postIcarusHttpRequest(final JSONObject icarusPayload, final boolean debug){
final JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, ICARUS_URL, icarusPayload, new Response.Listener<JSONObject>() {
JSONObject toSendJson=createJSONToSend(icarusPayload,debug); //aggiunge informazioni al JSON ottenuto attraverso la raccolta dati dell'utente
final JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, ICARUS_URL, toSendJson, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
......@@ -134,7 +146,7 @@ public class Icarus {
Log.d(curContext.getString(R.string.icarus_class),"messaggio pendente");
JSONObject pendingResource=pendingResources.poll();
postIcarusHttpRequest(pendingResource);
postIcarusHttpRequest(pendingResource,debug);
}
pendingResourceLock.unlock();
......@@ -155,18 +167,97 @@ public class Icarus {
mVolleyRequestQueue.add(jsonObjectRequest);//faccio effettivamente la richiesta
}
//SharedPreferences utility methods
private void editIntPref(SharedPreferences settings,String nomePref,int newValue){
public JSONObject createJSONToSend(JSONObject userData, boolean debug){
JSONObject toSend=new JSONObject();
try {
// ------------------- TIME JSON ----------------
long utcTime=System.currentTimeMillis(); //ritorna il tempo UTC tra ora e il 1 gennaio 1970 alle 00:00
TimeZone myTimeZone=TimeZone.getDefault();
int timeOffsetBetweenMeAndGMT=myTimeZone.getOffset(utcTime); //ms
long userTime=utcTime+timeOffsetBetweenMeAndGMT;
JSONObject timeJSON=new JSONObject();
timeJSON.put("utc",utcTime);
timeJSON.put("user",userTime);
SharedPreferences.Editor editor=settings.edit();
editor.putInt(nomePref,newValue);
editor.commit();
//---------------------- APP JSON ---------------------
//trovo il nome dell'app che sta chiamando questo metodo
PackageManager pm=curContext.getPackageManager();
CharSequence appName=pm.getApplicationLabel(curContext.getApplicationInfo());
//trovo versione dell'app chiamante
int clientAppVersion;
try {
PackageInfo pinfo=pm.getPackageInfo(curContext.getPackageName(),0);
clientAppVersion=pinfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
clientAppVersion=0;
}
//controllo se l'accessibilità è attivata o no
AccessibilityManager am=(AccessibilityManager)curContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
boolean accessibilityEnabled=false;
try {
accessibilityEnabled=am.isTouchExplorationEnabled();
} catch (NullPointerException npe){
Log.e(curContext.getString(R.string.icarus_class),"errore ottenere accessibilityManager");
}
JSONObject appInfoJSON=new JSONObject();
appInfoJSON.put("lang", Locale.getDefault().getLanguage());
appInfoJSON.put("log_lib","Icarus:"+curContext.getResources().getInteger(R.integer.lib_version));
appInfoJSON.put("appname",appName.toString());
appInfoJSON.put("appversion",clientAppVersion);
appInfoJSON.put("os", Build.VERSION.SDK_INT);
appInfoJSON.put("device",getDeviceName());
appInfoJSON.put("uuid",UUID.randomUUID().toString());
appInfoJSON.put("talkback",accessibilityEnabled);
//---------------- GENERAL JSON --------------------
toSend.put("debug",debug);
toSend.put("timestamp",timeJSON);
toSend.put("appData",appInfoJSON);
toSend.put("userdata",userData);
} catch (JSONException e) {
e.printStackTrace();
}
return toSend;
}
private void editBoolPref(SharedPreferences settings,String nomePref,boolean newValue){
public String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.toLowerCase().startsWith(manufacturer.toLowerCase())) {
return capitalize(model);
} else {
return capitalize(manufacturer) + " " + model;
}
}
SharedPreferences.Editor editor=settings.edit();
editor.putBoolean(nomePref,newValue);
editor.commit();
private String capitalize(String s) {
if (s == null || s.length() == 0) {
return "";
}
char first = s.charAt(0);
if (Character.isUpperCase(first)) {
return s;
} else {
return Character.toUpperCase(first) + s.substring(1);
}
}
}
......@@ -2,4 +2,5 @@
<resources>
<string name="icarus_class">Icarus</string>
<integer name="lib_version">1</integer>
</resources>
\ No newline at end of file
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