From e2896cbc5d487f4871f726fe99364783243b834d Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 14 Aug 2018 18:33:29 +0200 Subject: [PATCH] SW: less aggressivity on nsclient connections --- .../androidaps/setupwizard/SWDefinition.java | 7 ++-- .../setupwizard/elements/SWEditString.java | 8 ++++- .../setupwizard/elements/SWEditUrl.java | 9 +++++- .../setupwizard/elements/SWItem.java | 32 +++++++++++++++++-- .../setupwizard/elements/SWRadioButton.java | 2 +- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java index 8f453b8268..be95f3e3ac 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java @@ -58,14 +58,11 @@ import info.nightscout.utils.SP; public class SWDefinition { private static Logger log = LoggerFactory.getLogger(SWDefinition.class); - private String packageName; - private AppCompatActivity activity; private List screens = new ArrayList<>(); public void setActivity(AppCompatActivity activity) { this.activity = activity; - packageName = activity.getPackageName(); } public AppCompatActivity getActivity() { @@ -184,11 +181,13 @@ public class SWDefinition { .visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL))) .add(new SWEditUrl() .preferenceId(R.string.key_nsclientinternal_url) + .updateDelay(5) .label(R.string.nsclientinternal_url_title) .comment(R.string.nsclientinternal_url_dialogmessage)) .add(new SWEditString() .validator(text -> text.length() >= 12) .preferenceId(R.string.key_nsclientinternal_api_secret) + .updateDelay(5) .label(R.string.nsclientinternal_secret_dialogtitle) .comment(R.string.nsclientinternal_secret_dialogmessage)) .add(new SWBreak()) @@ -531,10 +530,12 @@ public class SWDefinition { .visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL))) .add(new SWEditUrl() .preferenceId(R.string.key_nsclientinternal_url) + .updateDelay(5) .label(R.string.nsclientinternal_url_title) .comment(R.string.nsclientinternal_url_dialogmessage)) .add(new SWEditString() .validator(text -> text.length() >= 12) + .updateDelay(5) .preferenceId(R.string.key_nsclientinternal_api_secret) .label(R.string.nsclientinternal_secret_dialogtitle) .comment(R.string.nsclientinternal_secret_dialogmessage)) diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditString.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditString.java index d02f10acf0..1f32e6622d 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditString.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditString.java @@ -21,6 +21,7 @@ public class SWEditString extends SWItem { private static Logger log = LoggerFactory.getLogger(SWEditString.class); private SWTextValidator validator = null; + private int updateDelay = 0; public SWEditString() { super(Type.STRING); @@ -58,7 +59,7 @@ public class SWEditString extends SWItem { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (validator != null && validator.isValid(s.toString())) - save(s.toString()); + save(s.toString(), updateDelay); } @Override @@ -76,4 +77,9 @@ public class SWEditString extends SWItem { this.validator = validator; return this; } + + public SWEditString updateDelay(int updateDelay) { + this.updateDelay = updateDelay; + return this; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditUrl.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditUrl.java index 04b02a5bc2..254723e4e1 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditUrl.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWEditUrl.java @@ -22,6 +22,8 @@ import info.nightscout.utils.SP; public class SWEditUrl extends SWItem { private static Logger log = LoggerFactory.getLogger(SWEditUrl.class); + private int updateDelay = 0; + public SWEditUrl() { super(Type.URL); } @@ -58,7 +60,7 @@ public class SWEditUrl extends SWItem { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (Patterns.WEB_URL.matcher(s).matches()) - save(s.toString()); + save(s.toString(), updateDelay); else MainApp.bus().post(new EventSWLabel(MainApp.gs(R.string.error_url_not_valid))); } @@ -74,4 +76,9 @@ public class SWEditUrl extends SWItem { return this; } + public SWEditUrl updateDelay(int updateDelay) { + this.updateDelay = updateDelay; + return this; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWItem.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWItem.java index 4f454b843d..d9cfe3710d 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWItem.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWItem.java @@ -6,14 +6,23 @@ import android.widget.LinearLayout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.events.EventPreferenceChange; +import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.setupwizard.events.EventSWUpdate; import info.nightscout.utils.SP; public class SWItem { private static Logger log = LoggerFactory.getLogger(SWItem.class); + private static final ScheduledExecutorService eventWorker = Executors.newSingleThreadScheduledExecutor(); + private static ScheduledFuture scheduledEventPost = null; + public enum Type { NONE, TEXT, @@ -66,10 +75,9 @@ public class SWItem { return this; } - public void save(String value) { + public void save(String value, int updateDelay) { SP.putString(preferenceId, value); - MainApp.bus().post(new EventPreferenceChange(preferenceId)); - MainApp.bus().post(new EventSWUpdate()); + scheduleChange(updateDelay); } public static LinearLayout generateLayout(View view) { @@ -83,4 +91,22 @@ public class SWItem { public void processVisibility() { } + + private void scheduleChange(int updateDelay) { + class PostRunnable implements Runnable { + public void run() { + if (L.isEnabled(L.CORE)) + log.debug("Firing EventPreferenceChange"); + MainApp.bus().post(new EventPreferenceChange(preferenceId)); + MainApp.bus().post(new EventSWUpdate()); + scheduledEventPost = null; + } + } + // cancel waiting task to prevent sending multiple posts + if (scheduledEventPost != null) + scheduledEventPost.cancel(false); + Runnable task = new PostRunnable(); + final int sec = updateDelay; + scheduledEventPost = eventWorker.schedule(task, sec, TimeUnit.SECONDS); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWRadioButton.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWRadioButton.java index 5282068094..cf7dbd55fe 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWRadioButton.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWRadioButton.java @@ -69,7 +69,7 @@ public class SWRadioButton extends SWItem { radioGroup.setOnCheckedChangeListener((group, checkedId) -> { int i = (int) group.findViewById(checkedId).getTag(); - save(values()[i]); + save(values()[i], 0); }); layout.addView(radioGroup);