Move json stringify and parsing into interface

This commit is contained in:
Ben Wheeler 2020-08-14 12:14:20 -04:00
parent c301a1090e
commit dcd4222b4c
5 changed files with 22 additions and 20 deletions

View file

@ -630,10 +630,22 @@ public class JavaScriptDirectInterface {
}
}
/**
* Record a user property
* @param prefObjStr single key-value JSON string, like "{\"school\": \"Central High\"}"
*/
@JavascriptInterface
public void setAnalyticsPref(String prefObjStr) {
if (prefObjStr != null) {
_activity.setAnalyticsPref(prefObjStr);
try {
JSONObject jsonObject = new JSONObject(prefObjStr);
JSONArray jsonArray = jsonObject.names();
String key = jsonArray.getString(0);
String value = jsonObject.getString(key);
} catch (JSONException e) {
Log.e(LOG_TAG, "JSON error: " + e.getMessage(), e);
}
_activity.setAnalyticsPref(key, value);
}
}
}

View file

@ -41,9 +41,6 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
/**
* Main activity for Scratch Jr., consisting of a full-screen landscape WebView.
*
@ -525,18 +522,11 @@ public class ScratchJrActivity
/**
* Record a user property
* @param prefObjStr like "{\"school\": \"Central High\"}"
* @param key like "school"
* @param value like "Central High"
*/
public void setAnalyticsPref(String prefObjStr) {
try {
JSONObject jsonObject = new JSONObject(prefObjStr);
JSONArray jsonArray = jsonObject.names();
String key = jsonArray.getString(0);
String value = jsonObject.getString(key);
_FirebaseAnalytics.setUserProperty(key, value);
} catch (JSONException e) {
// don't log anything to Firebase
}
public void setAnalyticsPref(String key, String value) {
_FirebaseAnalytics.setUserProperty(key, value);
}
public void translateAndScaleRectToContainerCoords(RectF rect, float devicePixelRatio) {

View file

@ -261,8 +261,8 @@ export default class Android {
AndroidInterface.setAnalyticsPlacePref(preferredPlace);
}
static setAnalyticsPref (jsonStr) {
AndroidInterface.setAnalyticsPref(jsonStr);
static setAnalyticsPref (obj) {
AndroidInterface.setAnalyticsPref(JSON.stringify(obj));
}
// // Web Wiew delegate call backs

View file

@ -254,7 +254,7 @@ export default class OS {
}
static setAnalyticsPref (key, value) {
tabletInterface.setAnalyticsPref(JSON.stringify({[key]: value}));
tabletInterface.setAnalyticsPref({[key]: value});
}
// Web Wiew delegate call backs

View file

@ -268,8 +268,8 @@ export default class iOS {
window.tablet.setAnalyticsPlacePref(preferredPlace);
}
static setAnalyticsPref (jsonStr) {
window.tablet.setAnalyticsPref(jsonStr);
static setAnalyticsPref (obj) {
window.tablet.setAnalyticsPref(JSON.stringify(obj));
}
// // Web Wiew delegate call backs