From 547061d4b659592816bc0d27eabeb33e2f11a67b Mon Sep 17 00:00:00 2001 From: Mattia Date: Tue, 1 May 2018 22:32:58 +0200 Subject: [PATCH] completamento dei JSON dei dati degli utenti con info sull'app e sul timestamp --- .../it/icarusandroid/MainActivity.java | 4 +- .../it/icarus/Icarus.java | 127 +++++++++++++++--- .../src/main/res/values/app_constants.xml | 1 + 3 files changed, 112 insertions(+), 20 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 6a52999..f5248ea 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 @@ -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); } } 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 495b4c4..e05cc40 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 @@ -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 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() { @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); + } } } diff --git a/IcarusAndroid/icarus/src/main/res/values/app_constants.xml b/IcarusAndroid/icarus/src/main/res/values/app_constants.xml index a53d545..ca7c632 100644 --- a/IcarusAndroid/icarus/src/main/res/values/app_constants.xml +++ b/IcarusAndroid/icarus/src/main/res/values/app_constants.xml @@ -2,4 +2,5 @@ Icarus + 1 \ No newline at end of file -- 2.18.1