From 442d48b4c2927e8c7512c8b73e73fe7cb7a353ec Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 2 Aug 2019 16:31:57 +0200 Subject: [PATCH] fix OPAS fragments --- .../aps/openAPSAMA/OpenAPSAMAFragment.java | 3 +- .../aps/openAPSSMB/OpenAPSSMBFragment.java | 18 ++++++------ .../androidaps/utils/JSONFormatter.java | 28 +++++++++++++------ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.java index 0aadf52cbd..6d465c9a06 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.aps.openAPSAMA; import android.app.Activity; import android.os.Bundle; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -97,7 +98,7 @@ public class OpenAPSAMAFragment extends SubscriberFragment implements View.OnCli currentTempView.setText(JSONFormatter.format(determineBasalAdapterAMAJS.getCurrentTempParam())); try { JSONArray iobArray = new JSONArray(determineBasalAdapterAMAJS.getIobDataParam()); - iobDataView.setText(String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n" + JSONFormatter.format(iobArray.getString(0))); + iobDataView.setText(TextUtils.concat(String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n", JSONFormatter.format(iobArray.getString(0)))); } catch (JSONException e) { log.error("Unhandled exception", e); iobDataView.setText("JSONException"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.java index 18828e840e..028815df34 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.java @@ -2,6 +2,8 @@ package info.nightscout.androidaps.plugins.aps.openAPSSMB; import android.app.Activity; import android.os.Bundle; +import android.text.Html; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -94,26 +96,26 @@ public class OpenAPSSMBFragment extends SubscriberFragment { } DetermineBasalAdapterSMBJS determineBasalAdapterSMBJS = plugin.lastDetermineBasalAdapterSMBJS; if (determineBasalAdapterSMBJS != null) { - glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getGlucoseStatusParam()).toString().trim()); - currentTempView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getCurrentTempParam()).toString().trim()); + glucoseStatusView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getGlucoseStatusParam())); + currentTempView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getCurrentTempParam())); try { JSONArray iobArray = new JSONArray(determineBasalAdapterSMBJS.getIobDataParam()); - iobDataView.setText((String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n" + JSONFormatter.format(iobArray.getString(0))).trim()); + iobDataView.setText(TextUtils.concat(String.format(MainApp.gs(R.string.array_of_elements), iobArray.length()) + "\n", JSONFormatter.format(iobArray.getString(0)))); } catch (JSONException e) { log.error("Unhandled exception", e); iobDataView.setText("JSONException see log for details"); } - profileView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getProfileParam()).toString().trim()); - mealDataView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getMealDataParam()).toString().trim()); - scriptdebugView.setText(determineBasalAdapterSMBJS.getScriptDebug().trim()); + profileView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getProfileParam())); + mealDataView.setText(JSONFormatter.format(determineBasalAdapterSMBJS.getMealDataParam())); + scriptdebugView.setText(determineBasalAdapterSMBJS.getScriptDebug()); if (lastAPSResult != null && lastAPSResult.inputConstraints != null) - constraintsView.setText(lastAPSResult.inputConstraints.getReasons().trim()); + constraintsView.setText(lastAPSResult.inputConstraints.getReasons()); } if (plugin.lastAPSRun != 0) { lastRunView.setText(DateUtil.dateAndTimeFullString(plugin.lastAPSRun)); } if (plugin.lastAutosensResult != null) { - autosensDataView.setText(JSONFormatter.format(plugin.lastAutosensResult.json()).toString().trim()); + autosensDataView.setText(JSONFormatter.format(plugin.lastAutosensResult.json())); } } }); diff --git a/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java b/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java index 159e90fda7..6205beee21 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/JSONFormatter.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.utils; +import android.os.Build; import android.text.Html; import android.text.Spanned; @@ -18,14 +19,23 @@ public class JSONFormatter { private static Logger log = LoggerFactory.getLogger(JSONFormatter.class); public static Spanned format(final String jsonString) { - final JsonVisitor visitor = new JsonVisitor(4, ' '); + final JsonVisitor visitor = new JsonVisitor(1, '\t'); try { if (jsonString.equals("undefined")) return Html.fromHtml("undefined"); else if (jsonString.getBytes()[0] == '[') - return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0)); - else - return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0)); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0), Html.FROM_HTML_MODE_COMPACT); + } else { + return Html.fromHtml(visitor.visit(new JSONArray(jsonString), 0)); + } + else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0), Html.FROM_HTML_MODE_COMPACT); + } else { + return Html.fromHtml(visitor.visit(new JSONObject(jsonString), 0)); + } + } } catch (JSONException e) { log.error("Unhandled exception", e); return Html.fromHtml(""); @@ -33,7 +43,7 @@ public class JSONFormatter { } public static Spanned format(final JSONObject object) { - final JsonVisitor visitor = new JsonVisitor(4, ' '); + final JsonVisitor visitor = new JsonVisitor(1, '\t'); try { return Html.fromHtml(visitor.visit(object, 0)); } catch (JSONException e) { @@ -58,7 +68,7 @@ public class JSONFormatter { } else { ret += write("[", indent); for (int i = 0; i < length; i++) { - ret += visit(array.get(i), indent + 1); + ret += visit(array.get(i), indent); } ret += write("]", indent); } @@ -73,8 +83,8 @@ public class JSONFormatter { final Iterator keys = obj.keys(); while (keys.hasNext()) { final String key = keys.next(); - ret += write("" + key + ": ", indent + 1); - ret += visit(obj.get(key), 0); + ret += write("" + key + ": ", indent); + ret += visit(obj.get(key), indent + 1); ret += "
"; } } @@ -86,7 +96,7 @@ public class JSONFormatter { if (object instanceof JSONArray) { ret += visit((JSONArray) object, indent); } else if (object instanceof JSONObject) { - ret += visit((JSONObject) object, indent); + ret += "
" + visit((JSONObject) object, indent); } else { if (object instanceof String) { ret += write("\"" + ((String) object).replace("<", "<").replace(">", ">") + "\"", indent);