SW: less aggressivity on nsclient connections

This commit is contained in:
Milos Kozak 2018-08-14 18:33:29 +02:00
parent 03f8a7c629
commit e2896cbc5d
5 changed files with 49 additions and 9 deletions

View file

@ -58,14 +58,11 @@ import info.nightscout.utils.SP;
public class SWDefinition { public class SWDefinition {
private static Logger log = LoggerFactory.getLogger(SWDefinition.class); private static Logger log = LoggerFactory.getLogger(SWDefinition.class);
private String packageName;
private AppCompatActivity activity; private AppCompatActivity activity;
private List<SWScreen> screens = new ArrayList<>(); private List<SWScreen> screens = new ArrayList<>();
public void setActivity(AppCompatActivity activity) { public void setActivity(AppCompatActivity activity) {
this.activity = activity; this.activity = activity;
packageName = activity.getPackageName();
} }
public AppCompatActivity getActivity() { public AppCompatActivity getActivity() {
@ -184,11 +181,13 @@ public class SWDefinition {
.visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL))) .visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL)))
.add(new SWEditUrl() .add(new SWEditUrl()
.preferenceId(R.string.key_nsclientinternal_url) .preferenceId(R.string.key_nsclientinternal_url)
.updateDelay(5)
.label(R.string.nsclientinternal_url_title) .label(R.string.nsclientinternal_url_title)
.comment(R.string.nsclientinternal_url_dialogmessage)) .comment(R.string.nsclientinternal_url_dialogmessage))
.add(new SWEditString() .add(new SWEditString()
.validator(text -> text.length() >= 12) .validator(text -> text.length() >= 12)
.preferenceId(R.string.key_nsclientinternal_api_secret) .preferenceId(R.string.key_nsclientinternal_api_secret)
.updateDelay(5)
.label(R.string.nsclientinternal_secret_dialogtitle) .label(R.string.nsclientinternal_secret_dialogtitle)
.comment(R.string.nsclientinternal_secret_dialogmessage)) .comment(R.string.nsclientinternal_secret_dialogmessage))
.add(new SWBreak()) .add(new SWBreak())
@ -531,10 +530,12 @@ public class SWDefinition {
.visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL))) .visibility(() -> !NSClientPlugin.getPlugin().isEnabled(PluginType.GENERAL)))
.add(new SWEditUrl() .add(new SWEditUrl()
.preferenceId(R.string.key_nsclientinternal_url) .preferenceId(R.string.key_nsclientinternal_url)
.updateDelay(5)
.label(R.string.nsclientinternal_url_title) .label(R.string.nsclientinternal_url_title)
.comment(R.string.nsclientinternal_url_dialogmessage)) .comment(R.string.nsclientinternal_url_dialogmessage))
.add(new SWEditString() .add(new SWEditString()
.validator(text -> text.length() >= 12) .validator(text -> text.length() >= 12)
.updateDelay(5)
.preferenceId(R.string.key_nsclientinternal_api_secret) .preferenceId(R.string.key_nsclientinternal_api_secret)
.label(R.string.nsclientinternal_secret_dialogtitle) .label(R.string.nsclientinternal_secret_dialogtitle)
.comment(R.string.nsclientinternal_secret_dialogmessage)) .comment(R.string.nsclientinternal_secret_dialogmessage))

View file

@ -21,6 +21,7 @@ public class SWEditString extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWEditString.class); private static Logger log = LoggerFactory.getLogger(SWEditString.class);
private SWTextValidator validator = null; private SWTextValidator validator = null;
private int updateDelay = 0;
public SWEditString() { public SWEditString() {
super(Type.STRING); super(Type.STRING);
@ -58,7 +59,7 @@ public class SWEditString extends SWItem {
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
if (validator != null && validator.isValid(s.toString())) if (validator != null && validator.isValid(s.toString()))
save(s.toString()); save(s.toString(), updateDelay);
} }
@Override @Override
@ -76,4 +77,9 @@ public class SWEditString extends SWItem {
this.validator = validator; this.validator = validator;
return this; return this;
} }
public SWEditString updateDelay(int updateDelay) {
this.updateDelay = updateDelay;
return this;
}
} }

View file

@ -22,6 +22,8 @@ import info.nightscout.utils.SP;
public class SWEditUrl extends SWItem { public class SWEditUrl extends SWItem {
private static Logger log = LoggerFactory.getLogger(SWEditUrl.class); private static Logger log = LoggerFactory.getLogger(SWEditUrl.class);
private int updateDelay = 0;
public SWEditUrl() { public SWEditUrl() {
super(Type.URL); super(Type.URL);
} }
@ -58,7 +60,7 @@ public class SWEditUrl extends SWItem {
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
if (Patterns.WEB_URL.matcher(s).matches()) if (Patterns.WEB_URL.matcher(s).matches())
save(s.toString()); save(s.toString(), updateDelay);
else else
MainApp.bus().post(new EventSWLabel(MainApp.gs(R.string.error_url_not_valid))); MainApp.bus().post(new EventSWLabel(MainApp.gs(R.string.error_url_not_valid)));
} }
@ -74,4 +76,9 @@ public class SWEditUrl extends SWItem {
return this; return this;
} }
public SWEditUrl updateDelay(int updateDelay) {
this.updateDelay = updateDelay;
return this;
}
} }

View file

@ -6,14 +6,23 @@ import android.widget.LinearLayout;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.MainApp;
import info.nightscout.androidaps.events.EventPreferenceChange; import info.nightscout.androidaps.events.EventPreferenceChange;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate; import info.nightscout.androidaps.setupwizard.events.EventSWUpdate;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
public class SWItem { public class SWItem {
private static Logger log = LoggerFactory.getLogger(SWItem.class); private static Logger log = LoggerFactory.getLogger(SWItem.class);
private static final ScheduledExecutorService eventWorker = Executors.newSingleThreadScheduledExecutor();
private static ScheduledFuture<?> scheduledEventPost = null;
public enum Type { public enum Type {
NONE, NONE,
TEXT, TEXT,
@ -66,10 +75,9 @@ public class SWItem {
return this; return this;
} }
public void save(String value) { public void save(String value, int updateDelay) {
SP.putString(preferenceId, value); SP.putString(preferenceId, value);
MainApp.bus().post(new EventPreferenceChange(preferenceId)); scheduleChange(updateDelay);
MainApp.bus().post(new EventSWUpdate());
} }
public static LinearLayout generateLayout(View view) { public static LinearLayout generateLayout(View view) {
@ -83,4 +91,22 @@ public class SWItem {
public void processVisibility() { 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);
}
} }

View file

@ -69,7 +69,7 @@ public class SWRadioButton extends SWItem {
radioGroup.setOnCheckedChangeListener((group, checkedId) -> { radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
int i = (int) group.findViewById(checkedId).getTag(); int i = (int) group.findViewById(checkedId).getTag();
save(values()[i]); save(values()[i], 0);
}); });
layout.addView(radioGroup); layout.addView(radioGroup);