fix OPAS fragments

This commit is contained in:
Milos Kozak 2019-08-02 16:31:57 +02:00
parent b0eecac00e
commit 442d48b4c2
3 changed files with 31 additions and 18 deletions

View file

@ -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");

View file

@ -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()));
}
}
});

View file

@ -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] == '[')
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
}
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<String> keys = obj.keys();
while (keys.hasNext()) {
final String key = keys.next();
ret += write("<b>" + key + "</b>: ", indent + 1);
ret += visit(obj.get(key), 0);
ret += write("<b>" + key + "</b>: ", indent);
ret += visit(obj.get(key), indent + 1);
ret += "<br>";
}
}
@ -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 += "<br>" + visit((JSONObject) object, indent);
} else {
if (object instanceof String) {
ret += write("\"" + ((String) object).replace("<", "&lt;").replace(">", "&gt;") + "\"", indent);