diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputOption.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputOption.java new file mode 100644 index 0000000000..3c4dbd67a2 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputOption.java @@ -0,0 +1,19 @@ +package info.nightscout.androidaps.plugins.general.automation.elements; + +public class InputOption { + private int stringRes; + private String value; + + public InputOption(int stringRes, String value) { + this.stringRes = stringRes; + this.value = value; + } + + public int getStringRes() { + return stringRes; + } + + public String getValue() { + return value; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputSelect.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputSelect.java new file mode 100644 index 0000000000..c57ed4c94a --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputSelect.java @@ -0,0 +1,93 @@ +package info.nightscout.androidaps.plugins.general.automation.elements; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.LinearLayout; +import android.widget.Spinner; + +import java.util.ArrayList; +import java.util.List; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; + +public class InputSelect extends Element { + + private InputOption selected; + private ArrayList options; + + public InputSelect() { + super(); + } + + public InputSelect(ArrayList options) { + super(); + this.options = options; + } + + public InputSelect(ArrayList options, InputOption selected) { + super(); + this.options = options; + this.selected = selected; + } + + public InputSelect(InputSelect another) { + super(); + options = another.options; + selected = another.selected; + } + + @Override + public void addToLayout(LinearLayout root) { + ArrayAdapter adapter = new ArrayAdapter<>(root.getContext(), + R.layout.spinner_centered, labels()); + Spinner spinner = new Spinner(root.getContext()); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ); + spinnerParams.setMargins(0, MainApp.dpToPx(4), 0, MainApp.dpToPx(4)); + spinner.setLayoutParams(spinnerParams); + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + setValue(options.get(position)); + } + + @Override + public void onNothingSelected(AdapterView parent) { + } + }); + spinner.setSelection(selected == null ? 0 : options.indexOf(selected)); + root.addView(spinner); + } + + public String getValue() { + return selected == null ? null : selected.getValue(); + } + + public InputSelect setValue(InputOption selected) { + this.selected = selected; + return this; + } + + public InputSelect setValue(String selected) { + for (InputOption o: options) { + if (o.getValue().equals(selected)) { + this.selected = o; + break; + } + } + return this; + } + + private List labels() { + List list = new ArrayList<>(); + for (InputOption o : options) { + list.add(MainApp.gs(o.getStringRes())); + } + return list; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocation.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocation.java index 19965cfc9a..46f3491d21 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocation.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerLocation.java @@ -13,12 +13,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Arrays; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.general.automation.elements.InputButton; import info.nightscout.androidaps.plugins.general.automation.elements.InputDouble; +import info.nightscout.androidaps.plugins.general.automation.elements.InputOption; +import info.nightscout.androidaps.plugins.general.automation.elements.InputSelect; import info.nightscout.androidaps.plugins.general.automation.elements.InputString; import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; @@ -31,9 +35,17 @@ import info.nightscout.androidaps.utils.T; public class TriggerLocation extends Trigger { private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); + private static final String modeEntered = "entered"; + private static final String modeLeft = "left"; + private static final ArrayList modes = new ArrayList<>(Arrays.asList( + new InputOption(R.string.location_entered, modeEntered), + new InputOption(R.string.location_left, modeLeft) + )); + InputDouble latitude = new InputDouble(0d, -90d, +90d, 0.000001d, new DecimalFormat("0.000000")); InputDouble longitude = new InputDouble(0d, -180d, +180d, 0.000001d, new DecimalFormat("0.000000")); InputDouble distance = new InputDouble(200d, 0, 100000, 10d, new DecimalFormat("0")); + InputSelect mode = new InputSelect(modes); InputString name = new InputString(); private Runnable buttonAction = () -> { @@ -54,6 +66,7 @@ public class TriggerLocation extends Trigger { latitude = new InputDouble(triggerLocation.latitude); longitude = new InputDouble(triggerLocation.longitude); distance = new InputDouble(triggerLocation.distance); + mode = new InputSelect(triggerLocation.mode); lastRun = triggerLocation.lastRun; name = triggerLocation.name; } @@ -72,7 +85,8 @@ public class TriggerLocation extends Trigger { a.setLongitude(longitude.getValue()); double calculatedDistance = location.distanceTo(a); - if (calculatedDistance < distance.getValue()) { + if ((mode.equals(modeEntered) && calculatedDistance < distance.getValue()) || + (mode.equals(modeLeft) && calculatedDistance > distance.getValue())) { if (L.isEnabled(L.AUTOMATION)) log.debug("Ready for execution: " + friendlyDescription()); return true; @@ -90,6 +104,7 @@ public class TriggerLocation extends Trigger { data.put("longitude", longitude.getValue()); data.put("distance", distance.getValue()); data.put("name", name.getValue()); + data.put("mode", mode.getValue()); data.put("lastRun", lastRun); o.put("data", data); } catch (JSONException e) { @@ -106,6 +121,7 @@ public class TriggerLocation extends Trigger { longitude.setValue(JsonHelper.safeGetDouble(d, "longitude")); distance.setValue(JsonHelper.safeGetDouble(d, "distance")); name.setValue(JsonHelper.safeGetString(d, "name")); + mode.setValue(JsonHelper.safeGetString(d, "mode", modeEntered)); lastRun = JsonHelper.safeGetLong(d, "lastRun"); } catch (Exception e) { log.error("Unhandled exception", e); @@ -154,6 +170,11 @@ public class TriggerLocation extends Trigger { return this; } + TriggerLocation setMode(String value) { + mode.setValue(value); + return this; + } + @Override public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { new LayoutBuilder() @@ -162,6 +183,7 @@ public class TriggerLocation extends Trigger { .add(new LabelWithElement(MainApp.gs(R.string.latitude_short), "", latitude)) .add(new LabelWithElement(MainApp.gs(R.string.longitude_short), "", longitude)) .add(new LabelWithElement(MainApp.gs(R.string.distance_short), "", distance)) + .add(new LabelWithElement(MainApp.gs(R.string.location_mode), "", mode)) .add(new InputButton(MainApp.gs(R.string.currentlocation), buttonAction), LocationService.getLastLocation() != null) .build(root); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f5d78bc687..6ee34e1f39 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1411,6 +1411,9 @@ Dist [m]: Name: Location is %1$s + On location + entered + left Last bolus ago Last bolus time %1$s %2$s min ago COB