nsclient plugin tweaking

This commit is contained in:
Milos Kozak 2017-06-13 21:25:50 +02:00
parent 43b70f9efc
commit 0b322e53fc
3 changed files with 22 additions and 26 deletions

View file

@ -58,8 +58,6 @@ public class NSClientInternalFragment extends Fragment implements View.OnClickLi
private CheckBox autoscrollCheckbox; private CheckBox autoscrollCheckbox;
private CheckBox pausedCheckbox; private CheckBox pausedCheckbox;
String status = "";
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@ -119,7 +117,7 @@ public class NSClientInternalFragment extends Fragment implements View.OnClickLi
builder.setMessage("Clear queue? All data in queue will be lost!"); builder.setMessage("Clear queue? All data in queue will be lost!");
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
getPlugin().queue().clearQueue(); UploadQueue.clearQueue();
updateGUI(); updateGUI();
Answers.getInstance().logCustom(new CustomEvent("NSClientClearQueue")); Answers.getInstance().logCustom(new CustomEvent("NSClientClearQueue"));
} }
@ -176,12 +174,13 @@ public class NSClientInternalFragment extends Fragment implements View.OnClickLi
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
logTextView.setText(getPlugin().textLog); NSClientInternalPlugin.updateLog();
logTextView.setText(NSClientInternalPlugin.textLog);
if (getPlugin().autoscroll) { if (getPlugin().autoscroll) {
logScrollview.fullScroll(ScrollView.FOCUS_DOWN); logScrollview.fullScroll(ScrollView.FOCUS_DOWN);
} }
urlTextView.setText(getPlugin().url()); urlTextView.setText(getPlugin().url());
Spanned queuetext = Html.fromHtml(MainApp.sResources.getString(R.string.queue) + " <b>" + getPlugin().queue().size() + "</b>"); Spanned queuetext = Html.fromHtml(MainApp.sResources.getString(R.string.queue) + " <b>" + UploadQueue.size() + "</b>");
queueTextView.setText(queuetext); queueTextView.setText(queuetext);
statusTextView.setText(getPlugin().status); statusTextView.setText(getPlugin().status);
} }

View file

@ -9,14 +9,12 @@ import android.os.HandlerThread;
import android.os.IBinder; import android.os.IBinder;
import android.text.Html; import android.text.Html;
import android.text.Spanned; import android.text.Spanned;
import android.text.TextUtils;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -36,29 +34,28 @@ import info.nightscout.utils.ToastUtils;
public class NSClientInternalPlugin implements PluginBase { public class NSClientInternalPlugin implements PluginBase {
private static Logger log = LoggerFactory.getLogger(NSClientInternalPlugin.class); private static Logger log = LoggerFactory.getLogger(NSClientInternalPlugin.class);
boolean fragmentEnabled = true; private boolean fragmentEnabled = true;
boolean fragmentVisible = true; private boolean fragmentVisible = true;
static public Handler handler; static public Handler handler;
static private HandlerThread handlerThread;
public List<EventNSClientNewLog> listLog = new ArrayList<EventNSClientNewLog>(); private static List<EventNSClientNewLog> listLog = new ArrayList<>();
public Spanned textLog = Html.fromHtml(""); static Spanned textLog = Html.fromHtml("");
public boolean paused = false; public boolean paused = false;
public boolean autoscroll = true; boolean autoscroll = true;
public String status = ""; public String status = "";
public NSClientService nsClientService = null; public NSClientService nsClientService = null;
public NSClientInternalPlugin() { NSClientInternalPlugin() {
MainApp.bus().register(this); MainApp.bus().register(this);
paused = SP.getBoolean(R.string.key_nsclientinternal_paused, false); paused = SP.getBoolean(R.string.key_nsclientinternal_paused, false);
autoscroll = SP.getBoolean(R.string.key_nsclientinternal_autoscroll, true); autoscroll = SP.getBoolean(R.string.key_nsclientinternal_autoscroll, true);
if (handler == null) { if (handler == null) {
handlerThread = new HandlerThread(NSClientInternalPlugin.class.getSimpleName() + "Handler"); HandlerThread handlerThread = new HandlerThread(NSClientInternalPlugin.class.getSimpleName() + "Handler");
handlerThread.start(); handlerThread.start();
handler = new Handler(handlerThread.getLooper()); handler = new Handler(handlerThread.getLooper());
} }
@ -129,7 +126,7 @@ public class NSClientInternalPlugin implements PluginBase {
if (type == GENERAL) this.fragmentVisible = fragmentVisible; if (type == GENERAL) this.fragmentVisible = fragmentVisible;
} }
ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) { public void onServiceDisconnected(ComponentName name) {
log.debug("Service is disconnected"); log.debug("Service is disconnected");
@ -162,12 +159,12 @@ public class NSClientInternalPlugin implements PluginBase {
MainApp.bus().post(new EventNSClientUpdateGUI()); MainApp.bus().post(new EventNSClientUpdateGUI());
} }
public void clearLog() { void clearLog() {
handler.post(new Runnable() { handler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
listLog = new ArrayList<EventNSClientNewLog>(); listLog = new ArrayList<>();
updateLog(); MainApp.bus().post(new EventNSClientUpdateGUI());
} }
}); });
} }
@ -176,31 +173,30 @@ public class NSClientInternalPlugin implements PluginBase {
handler.post(new Runnable() { handler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
listLog.add(ev); listLog.add(ev);
// remove the first line if log is too large // remove the first line if log is too large
if (listLog.size() >= Constants.MAX_LOG_LINES) { if (listLog.size() >= Constants.MAX_LOG_LINES) {
listLog.remove(0); listLog.remove(0);
} }
updateLog(); MainApp.bus().post(new EventNSClientUpdateGUI());
} }
}); });
} }
private void updateLog() { static void updateLog() {
try { try {
StringBuilder newTextLog = new StringBuilder(); StringBuilder newTextLog = new StringBuilder();
for (EventNSClientNewLog log : listLog) { List<EventNSClientNewLog> temporaryList = new ArrayList<>(listLog);
for (EventNSClientNewLog log : temporaryList) {
newTextLog.append(log.toPreparedHtml()); newTextLog.append(log.toPreparedHtml());
} }
textLog = Html.fromHtml(newTextLog.toString()); textLog = Html.fromHtml(newTextLog.toString());
MainApp.bus().post(new EventNSClientUpdateGUI());
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "Out of memory!\nStop using this phone !!!", R.raw.error); ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), "Out of memory!\nStop using this phone !!!", R.raw.error);
} }
} }
public void resend(String reason) { void resend(String reason) {
if (nsClientService != null) if (nsClientService != null)
nsClientService.resend(reason); nsClientService.resend(reason);
} }

View file

@ -22,9 +22,10 @@ public class EventNSClientNewLog {
this.logText = logText; this.logText = logText;
} }
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
public StringBuilder toPreparedHtml() { public StringBuilder toPreparedHtml() {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
stringBuilder.append(timeFormat.format(date)); stringBuilder.append(timeFormat.format(date));
stringBuilder.append(" <b>"); stringBuilder.append(" <b>");
stringBuilder.append(action); stringBuilder.append(action);