From 48e4a041f8d3bd977e510c091e9df3e29c4adbca Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Thu, 20 Sep 2018 17:23:36 +0100 Subject: [PATCH 01/78] - First changes for CustomActions - changed PumpInterface to add CustomActions - added class CustomActions and CustomActionType - changed all PumpImplementations to use new methods - started changing ActionsFragment (most should be done) --- .../androidaps/interfaces/PumpInterface.java | 9 ++ .../plugins/Actions/ActionsFragment.java | 121 ++++++++++++++++++ .../plugins/Actions/defs/CustomAction.java | 43 +++++++ .../Actions/defs/CustomActionType.java | 11 ++ .../plugins/PumpCombo/ComboPlugin.java | 13 ++ .../PumpDanaR/AbstractDanaRPlugin.java | 14 ++ .../plugins/PumpDanaRS/DanaRSPlugin.java | 14 ++ .../plugins/PumpInsight/InsightPlugin.java | 12 ++ .../androidaps/plugins/PumpMDI/MDIPlugin.java | 14 ++ .../PumpVirtual/VirtualPumpPlugin.java | 14 ++ app/src/main/res/layout/actions_fragment.xml | 1 + 11 files changed, 266 insertions(+) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Actions/defs/CustomAction.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Actions/defs/CustomActionType.java diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index 614c6033d3..2f8e92dcc7 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -2,9 +2,13 @@ package info.nightscout.androidaps.interfaces; import org.json.JSONObject; +import java.util.List; + import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.PumpEnactResult; +import info.nightscout.androidaps.plugins.Actions.defs.CustomAction; +import info.nightscout.androidaps.plugins.Actions.defs.CustomActionType; /** * Created by mike on 04.06.2016. @@ -57,4 +61,9 @@ public interface PumpInterface { PumpEnactResult loadTDDs(); + + List getCustomActions(); + + PumpEnactResult executeCustomAction(CustomActionType customActionType); + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java index ea17a30218..6a8422b2dd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java @@ -9,10 +9,16 @@ import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.LinearLayout; import com.crashlytics.android.answers.CustomEvent; import com.squareup.otto.Subscribe; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import info.nightscout.androidaps.Config; import info.nightscout.androidaps.activities.HistoryBrowseActivity; import info.nightscout.androidaps.MainApp; @@ -25,6 +31,8 @@ import info.nightscout.androidaps.events.EventInitializationChanged; import info.nightscout.androidaps.events.EventRefreshOverview; import info.nightscout.androidaps.events.EventTempBasalChange; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.plugins.Actions.defs.CustomAction; +import info.nightscout.androidaps.plugins.Actions.defs.CustomActionType; import info.nightscout.androidaps.plugins.Actions.dialogs.FillDialog; import info.nightscout.androidaps.plugins.Actions.dialogs.NewExtendedBolusDialog; import info.nightscout.androidaps.plugins.Actions.dialogs.NewTempBasalDialog; @@ -49,6 +57,7 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL return actionsPlugin; } + View actionsFragmentView; SingleClickButton profileSwitch; SingleClickButton tempTarget; SingleClickButton extendedBolus; @@ -90,6 +99,8 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL history.setOnClickListener(this); tddStats.setOnClickListener(this); + actionsFragmentView = view; + updateGUI(); return view; } catch (Exception e) { @@ -194,11 +205,121 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().supportsTDDs) tddStats.setVisibility(View.GONE); else tddStats.setVisibility(View.VISIBLE); + + checkCustomActions(); + } }); } + private String activePumpName; + private Map currentCustomActions = new HashMap<>(); + private List customButtons = new ArrayList<>(); + + View.OnClickListener customActionsListener = v -> { + + SingleClickButton btn = (SingleClickButton)v; + + CustomAction customAction = this.currentCustomActions.get(btn.getText().toString()); + + ConfigBuilderPlugin.getActivePump().executeCustomAction(customAction.getCustomActionType()); + + }; + + + + private void checkCustomActions() { + + PumpInterface activePump = ConfigBuilderPlugin.getActivePump(); + + if (activePump==null) { + removeCustomActions(); + return; + } + + String newPump = activePump.getClass().getSimpleName(); + + if (newPump.equals(activePumpName)) + return; + + removeCustomActions(); + + // add new actions + List customActions = activePump.getCustomActions(); + + if (customActions!=null) + { + + LinearLayout ll = (LinearLayout)actionsFragmentView.findViewById(R.id.action_buttons_layout); + + for (CustomAction customAction : customActions) { + // TODO + + + SingleClickButton btn = new SingleClickButton(MainApp.instance().getApplicationContext()); + btn.setText(customAction.getName()); + + //btn.setTextAppearance(R.style.buttonStyle); + + + LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f); + layoutParams.setMargins(10, 3, 10, 3); + + btn.setLayoutParams(layoutParams); + btn.setOnClickListener(customActionsListener); + + + ll.addView(btn); + + this.currentCustomActions.put(customAction.getName(), customAction); + this.customButtons.add(btn); + + + // TODO add to map + + +// + + + + + + } + } + + activePumpName = newPump; + } + + private void removeCustomActions() { + + if (currentCustomActions.size()==0) + return; + + LinearLayout ll = (LinearLayout)actionsFragmentView.findViewById(R.id.action_buttons_layout); + + for (SingleClickButton customButton : customButtons) { + ll.removeView(customButton); + } + + customButtons.clear(); + currentCustomActions.clear(); + } + + @Override public void onClick(View view) { FragmentManager manager = getFragmentManager(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/defs/CustomAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/defs/CustomAction.java new file mode 100644 index 0000000000..f32959cc33 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/defs/CustomAction.java @@ -0,0 +1,43 @@ +package info.nightscout.androidaps.plugins.Actions.defs; + +/** + * Created by andy on 9/20/18. + */ + +public class CustomAction { + + private String name; + private String iconName; + private CustomActionType customActionType; + + + public String getName() { + + return name; + } + + public void setName(String name) { + + this.name = name; + } + + public String getIconName() { + + return iconName; + } + + public void setIconName(String iconName) { + + this.iconName = iconName; + } + + public CustomActionType getCustomActionType() { + + return customActionType; + } + + public void setCustomActionType(CustomActionType customActionType) { + + this.customActionType = customActionType; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/defs/CustomActionType.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/defs/CustomActionType.java new file mode 100644 index 0000000000..640371bf9c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/defs/CustomActionType.java @@ -0,0 +1,11 @@ +package info.nightscout.androidaps.plugins.Actions.defs; + +/** + * Created by andy on 9/20/18. + */ + +public interface CustomActionType { + + String getKey(); + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index f6f1de2ad0..54d6a6d621 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -39,6 +39,8 @@ import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.Actions.defs.CustomAction; +import info.nightscout.androidaps.plugins.Actions.defs.CustomActionType; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions; @@ -1374,4 +1376,15 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint maxIob.setIfSmaller(0d, String.format(MainApp.gs(R.string.limitingmaxiob), 0d, MainApp.gs(R.string.unsafeusage)), this); return maxIob; } + + @Override + public List getCustomActions() { + return null; + } + + @Override + public PumpEnactResult executeCustomAction(CustomActionType customActionType) { + return null; + } + } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java index c50e635eb8..31cb635fec 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java @@ -8,6 +8,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; +import java.util.List; import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.MainApp; @@ -27,6 +28,8 @@ import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.Actions.defs.CustomAction; +import info.nightscout.androidaps.plugins.Actions.defs.CustomActionType; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; @@ -467,4 +470,15 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte } // TODO: daily total constraint + + @Override + public List getCustomActions() { + return null; + } + + @Override + public PumpEnactResult executeCustomAction(CustomActionType customActionType) { + return null; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java index e3c8f9de94..9610a07e75 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java @@ -16,6 +16,8 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -36,6 +38,8 @@ import info.nightscout.androidaps.interfaces.ProfileInterface; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.Actions.defs.CustomAction; +import info.nightscout.androidaps.plugins.Actions.defs.CustomActionType; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.DetailedBolusInfoStorage; import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions; @@ -806,4 +810,14 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte return loadHistory(RecordTypes.RECORD_TYPE_DAILY); } + @Override + public List getCustomActions() { + return null; + } + + @Override + public PumpEnactResult executeCustomAction(CustomActionType customActionType) { + return null; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java index c72da31fdf..b1ac8a065b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java @@ -32,6 +32,8 @@ import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.Actions.defs.CustomAction; +import info.nightscout.androidaps.plugins.Actions.defs.CustomActionType; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderFragment; import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; @@ -932,4 +934,14 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai return insulin; } + @Override + public List getCustomActions() { + return null; + } + + @Override + public PumpEnactResult executeCustomAction(CustomActionType customActionType) { + return null; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java index 27651a7cfb..b40c40acca 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java @@ -5,6 +5,8 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; @@ -17,6 +19,8 @@ import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.Actions.defs.CustomAction; +import info.nightscout.androidaps.plugins.Actions.defs.CustomActionType; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.utils.DateUtil; @@ -239,4 +243,14 @@ public class MDIPlugin extends PluginBase implements PumpInterface { return deviceID(); } + @Override + public List getCustomActions() { + return null; + } + + @Override + public PumpEnactResult executeCustomAction(CustomActionType customActionType) { + return null; + } + } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java index a21b1ca22b..670ee2397b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java @@ -9,6 +9,8 @@ import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + import info.nightscout.androidaps.BuildConfig; import info.nightscout.androidaps.Config; import info.nightscout.androidaps.MainApp; @@ -26,6 +28,8 @@ import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PumpDescription; import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.logging.L; +import info.nightscout.androidaps.plugins.Actions.defs.CustomAction; +import info.nightscout.androidaps.plugins.Actions.defs.CustomActionType; import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; @@ -140,6 +144,16 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { return new PumpEnactResult(); } + @Override + public List getCustomActions() { + return null; + } + + @Override + public PumpEnactResult executeCustomAction(CustomActionType customActionType) { + return null; + } + @Override public boolean isInitialized() { return true; diff --git a/app/src/main/res/layout/actions_fragment.xml b/app/src/main/res/layout/actions_fragment.xml index 3b322b26dd..04f2a9e5d3 100644 --- a/app/src/main/res/layout/actions_fragment.xml +++ b/app/src/main/res/layout/actions_fragment.xml @@ -9,6 +9,7 @@ android:layout_height="wrap_content"> From e2ddb484983d44b1cf500e8ae258ee1ab14f3c2a Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Thu, 20 Sep 2018 18:04:06 +0100 Subject: [PATCH 02/78] Some extension on VirtualPump for testing. --- .../plugins/Actions/ActionsFragment.java | 9 +++--- .../plugins/Actions/defs/CustomAction.java | 16 ++++++---- .../PumpVirtual/VirtualPumpFragment.java | 30 +++++++++++++++++++ 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java index 6a8422b2dd..dd48054738 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java @@ -254,15 +254,15 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL LinearLayout ll = (LinearLayout)actionsFragmentView.findViewById(R.id.action_buttons_layout); for (CustomAction customAction : customActions) { - // TODO - SingleClickButton btn = new SingleClickButton(MainApp.instance().getApplicationContext()); - btn.setText(customAction.getName()); + btn.setText(MainApp.gs(customAction.getName())); + // TODO style and drawableTop //btn.setTextAppearance(R.style.buttonStyle); + LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f); layoutParams.setMargins(10, 3, 10, 3); @@ -273,11 +273,10 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL ll.addView(btn); - this.currentCustomActions.put(customAction.getName(), customAction); + this.currentCustomActions.put(MainApp.gs(customAction.getName()), customAction); this.customButtons.add(btn); - // TODO add to map // Date: Sat, 13 Oct 2018 16:50:47 +0200 Subject: [PATCH 03/78] prepare new classes --- .../info/nightscout/androidaps/logging/L.java | 2 + .../plugins/SmsCommunicator/AuthRequest.java | 54 +++++++++++++++ .../plugins/SmsCommunicator/Sms.java | 51 +++++++++++++++ .../plugins/SmsCommunicator/SmsAction.java | 16 +++++ .../SmsCommunicatorFragment.java | 65 +++++++------------ .../SmsCommunicatorPlugin.java | 59 ++++------------- 6 files changed, 158 insertions(+), 89 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/AuthRequest.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Sms.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsAction.java diff --git a/app/src/main/java/info/nightscout/androidaps/logging/L.java b/app/src/main/java/info/nightscout/androidaps/logging/L.java index c44d2516c2..d47ba313af 100644 --- a/app/src/main/java/info/nightscout/androidaps/logging/L.java +++ b/app/src/main/java/info/nightscout/androidaps/logging/L.java @@ -95,6 +95,7 @@ public class L { public static final String PROFILE = "PROFILE"; public static final String CONFIGBUILDER = "CONFIGBUILDER"; public static final String UI = "UI"; + public static final String SMS = "SMS"; private static void initialize() { logElements = new ArrayList<>(); @@ -117,6 +118,7 @@ public class L { logElements.add(new LogElement(PUMPBTCOMM, false)); logElements.add(new LogElement(PUMPCOMM, true)); logElements.add(new LogElement(PUMPQUEUE, true)); + logElements.add(new LogElement(SMS, true)); logElements.add(new LogElement(UI, true)); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/AuthRequest.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/AuthRequest.java new file mode 100644 index 0000000000..c5dde0f644 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/AuthRequest.java @@ -0,0 +1,54 @@ +package info.nightscout.androidaps.plugins.SmsCommunicator; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import info.nightscout.androidaps.Constants; +import info.nightscout.androidaps.logging.L; +import info.nightscout.utils.DateUtil; + +class AuthRequest { + private static Logger log = LoggerFactory.getLogger(L.SMS); + + private Sms requester; + private String confirmCode; + private Runnable action; + + private long date; + + private boolean processed; + + AuthRequest(SmsCommunicatorPlugin plugin, Sms requester, String requestText, String confirmCode, SmsAction action) { + this.requester = requester; + this.confirmCode = confirmCode; + this.action = action; + + this.date = DateUtil.now(); + + plugin.sendSMS(new Sms(requester.phoneNumber, requestText)); + } + + void action(String codeReceived) { + if (processed) { + if (L.isEnabled(L.SMS)) + log.debug("Already processed"); + return; + } + if (!confirmCode.equals(codeReceived)) { + if (L.isEnabled(L.SMS)) + log.debug("Wrong code"); + return; + } + if (DateUtil.now() - date < Constants.SMS_CONFIRM_TIMEOUT) { + processed = true; + if (L.isEnabled(L.SMS)) + log.debug("Processing confirmed SMS: " + requester.text); + if (action != null) + action.run(); + return; + } + if (L.isEnabled(L.SMS)) + log.debug("Timed out SMS: " + requester.text); + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Sms.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Sms.java new file mode 100644 index 0000000000..a2e56c833d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Sms.java @@ -0,0 +1,51 @@ +package info.nightscout.androidaps.plugins.SmsCommunicator; + +import android.telephony.SmsMessage; + +class Sms { + String phoneNumber; + String confirmCode; // move + String text; + long date; //move + boolean received = false; + boolean sent = false; + boolean processed = false; + + double bolusRequested = 0d; + double tempBasal = 0d; + double calibrationRequested = 0d; + int duration = 0; + + Sms(SmsMessage message) { + phoneNumber = message.getOriginatingAddress(); + text = message.getMessageBody(); + date = message.getTimestampMillis(); + received = true; + } + + Sms(String phoneNumber, String text) { + this.phoneNumber = phoneNumber; + this.text = text; + sent = true; + } + + Sms(String phoneNumber, String text, long date) { + this.phoneNumber = phoneNumber; + this.text = text; + this.date = date; + sent = true; + } + + Sms(String phoneNumber, String text, long date, String confirmCode) { + this.phoneNumber = phoneNumber; + this.text = text; + this.date = date; + this.confirmCode = confirmCode; + sent = true; + } + + public String toString() { + return "SMS from " + phoneNumber + ": " + text; + } +} + diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsAction.java new file mode 100644 index 0000000000..6b0daf9f2d --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsAction.java @@ -0,0 +1,16 @@ +package info.nightscout.androidaps.plugins.SmsCommunicator; + +abstract class SmsAction implements Runnable { + Double d; + Integer i; + + SmsAction() {} + + SmsAction(Double d) { + this.d = d; + } + + SmsAction(Integer i) { + this.i = i; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java index 3f218063ef..0289437124 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorFragment.java @@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.SmsCommunicator; import android.app.Activity; import android.os.Bundle; -import android.support.v4.app.Fragment; import android.text.Html; import android.view.LayoutInflater; import android.view.View; @@ -12,9 +11,6 @@ import android.widget.TextView; import com.squareup.otto.Subscribe; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.Collections; import java.util.Comparator; @@ -22,14 +18,8 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.Common.SubscriberFragment; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommunicatorUpdateGui; import info.nightscout.utils.DateUtil; -import info.nightscout.utils.FabricPrivacy; -/** - * A simple {@link Fragment} subclass. - */ public class SmsCommunicatorFragment extends SubscriberFragment { - private static Logger log = LoggerFactory.getLogger(SmsCommunicatorFragment.class); - TextView logView; public SmsCommunicatorFragment() { @@ -39,18 +29,11 @@ public class SmsCommunicatorFragment extends SubscriberFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - try { - View view = inflater.inflate(R.layout.smscommunicator_fragment, container, false); + View view = inflater.inflate(R.layout.smscommunicator_fragment, container, false); - logView = (TextView) view.findViewById(R.id.smscommunicator_log); + logView = (TextView) view.findViewById(R.id.smscommunicator_log); - updateGUI(); - return view; - } catch (Exception e) { - FabricPrivacy.logException(e); - } - - return null; + return view; } @Subscribe @@ -58,35 +41,31 @@ public class SmsCommunicatorFragment extends SubscriberFragment { updateGUI(); } - @Override protected void updateGUI() { Activity activity = getActivity(); if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - class CustomComparator implements Comparator { - public int compare(SmsCommunicatorPlugin.Sms object1, SmsCommunicatorPlugin.Sms object2) { - return (int) (object1.date - object2.date); - } + activity.runOnUiThread(() -> { + class CustomComparator implements Comparator { + public int compare(Sms object1, Sms object2) { + return (int) (object1.date - object2.date); } - Collections.sort(SmsCommunicatorPlugin.getPlugin().messages, new CustomComparator()); - int messagesToShow = 40; - - int start = Math.max(0, SmsCommunicatorPlugin.getPlugin().messages.size() - messagesToShow); - - String logText = ""; - for (int x = start; x < SmsCommunicatorPlugin.getPlugin().messages.size(); x++) { - SmsCommunicatorPlugin.Sms sms = SmsCommunicatorPlugin.getPlugin().messages.get(x); - if (sms.received) { - logText += DateUtil.timeString(sms.date) + " <<< " + (sms.processed ? "● " : "○ ") + sms.phoneNumber + " " + sms.text + "
"; - } else if (sms.sent) { - logText += DateUtil.timeString(sms.date) + " >>> " + (sms.processed ? "● " : "○ ") + sms.phoneNumber + " " + sms.text + "
"; - } - } - logView.setText(Html.fromHtml(logText)); } + Collections.sort(SmsCommunicatorPlugin.getPlugin().messages, new CustomComparator()); + int messagesToShow = 40; + + int start = Math.max(0, SmsCommunicatorPlugin.getPlugin().messages.size() - messagesToShow); + + String logText = ""; + for (int x = start; x < SmsCommunicatorPlugin.getPlugin().messages.size(); x++) { + Sms sms = SmsCommunicatorPlugin.getPlugin().messages.get(x); + if (sms.received) { + logText += DateUtil.timeString(sms.date) + " <<< " + (sms.processed ? "● " : "○ ") + sms.phoneNumber + " " + sms.text + "
"; + } else if (sms.sent) { + logText += DateUtil.timeString(sms.date) + " >>> " + (sms.processed ? "● " : "○ ") + sms.phoneNumber + " " + sms.text + "
"; + } + } + logView.setText(Html.fromHtml(logText)); }); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 1d36eea817..c18f678235 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -21,8 +21,6 @@ import java.util.List; import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions; -import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.GlucoseStatus; import info.nightscout.androidaps.data.IobTotal; @@ -37,16 +35,19 @@ import info.nightscout.androidaps.interfaces.PluginBase; import info.nightscout.androidaps.interfaces.PluginDescription; import info.nightscout.androidaps.interfaces.PluginType; import info.nightscout.androidaps.interfaces.PumpInterface; +import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; +import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.Loop.LoopPlugin; +import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.SmsCommunicator.events.EventSmsCommunicatorUpdateGui; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; +import info.nightscout.androidaps.services.Intents; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.FabricPrivacy; -import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.T; @@ -56,7 +57,7 @@ import info.nightscout.utils.XdripCalibrations; * Created by mike on 05.08.2016. */ public class SmsCommunicatorPlugin extends PluginBase { - private static Logger log = LoggerFactory.getLogger(SmsCommunicatorPlugin.class); + private static Logger log = LoggerFactory.getLogger(L.SMS); private static SmsCommunicatorPlugin smsCommunicatorPlugin; @@ -70,46 +71,7 @@ public class SmsCommunicatorPlugin extends PluginBase { private List allowedNumbers = new ArrayList<>(); - class Sms { - String phoneNumber; - String text; - long date; - boolean received = false; - boolean sent = false; - boolean processed = false; - - String confirmCode; - double bolusRequested = 0d; - double tempBasal = 0d; - double calibrationRequested = 0d; - int duration = 0; - - Sms(SmsMessage message) { - phoneNumber = message.getOriginatingAddress(); - text = message.getMessageBody(); - date = message.getTimestampMillis(); - received = true; - } - - Sms(String phoneNumber, String text, long date) { - this.phoneNumber = phoneNumber; - this.text = text; - this.date = date; - sent = true; - } - - Sms(String phoneNumber, String text, long date, String confirmCode) { - this.phoneNumber = phoneNumber; - this.text = text; - this.date = date; - this.confirmCode = confirmCode; - sent = true; - } - - public String toString() { - return "SMS from " + phoneNumber + ": " + text; - } - } + private AuthRequest messageToConfirm = null; private Sms cancelTempBasalWaitingForConfirmation = null; private Sms tempBasalWaitingForConfirmation = null; @@ -443,6 +405,10 @@ public class SmsCommunicatorPlugin extends PluginBase { } break; default: // expect passCode here + if (messageToConfirm != null) { + messageToConfirm.action(splited[0]); + messageToConfirm = null; + } if (bolusWaitingForConfirmation != null && !bolusWaitingForConfirmation.processed && bolusWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - bolusWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) { bolusWaitingForConfirmation.processed = true; @@ -561,12 +527,13 @@ public class SmsCommunicatorPlugin extends PluginBase { } } - private void sendSMS(Sms sms) { + void sendSMS(Sms sms) { SmsManager smsManager = SmsManager.getDefault(); sms.text = stripAccents(sms.text); if (sms.text.length() > 140) sms.text = sms.text.substring(0, 139); try { - log.debug("Sending SMS to " + sms.phoneNumber + ": " + sms.text); + if (L.isEnabled(L.SMS)) + log.debug("Sending SMS to " + sms.phoneNumber + ": " + sms.text); smsManager.sendTextMessage(sms.phoneNumber, null, sms.text, null, null); messages.add(sms); } catch (IllegalArgumentException e) { From 903504d72dedeb688641623c935b6cba399ad85d Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 14 Oct 2018 10:55:11 +0200 Subject: [PATCH 04/78] SMSPlugin refactor & cleanup --- .../plugins/SmsCommunicator/Sms.java | 26 +- .../plugins/SmsCommunicator/SmsAction.java | 12 +- .../SmsCommunicatorPlugin.java | 293 ++++++++---------- 3 files changed, 143 insertions(+), 188 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Sms.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Sms.java index a2e56c833d..ec77f03923 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Sms.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/Sms.java @@ -2,20 +2,16 @@ package info.nightscout.androidaps.plugins.SmsCommunicator; import android.telephony.SmsMessage; +import info.nightscout.utils.DateUtil; + class Sms { String phoneNumber; - String confirmCode; // move String text; - long date; //move + long date; boolean received = false; boolean sent = false; boolean processed = false; - double bolusRequested = 0d; - double tempBasal = 0d; - double calibrationRequested = 0d; - int duration = 0; - Sms(SmsMessage message) { phoneNumber = message.getOriginatingAddress(); text = message.getMessageBody(); @@ -26,21 +22,7 @@ class Sms { Sms(String phoneNumber, String text) { this.phoneNumber = phoneNumber; this.text = text; - sent = true; - } - - Sms(String phoneNumber, String text, long date) { - this.phoneNumber = phoneNumber; - this.text = text; - this.date = date; - sent = true; - } - - Sms(String phoneNumber, String text, long date, String confirmCode) { - this.phoneNumber = phoneNumber; - this.text = text; - this.date = date; - this.confirmCode = confirmCode; + this.date = DateUtil.now(); sent = true; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsAction.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsAction.java index 6b0daf9f2d..6e991c8370 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsAction.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsAction.java @@ -1,16 +1,16 @@ package info.nightscout.androidaps.plugins.SmsCommunicator; abstract class SmsAction implements Runnable { - Double d; - Integer i; + Double aDouble; + Integer anInteger; SmsAction() {} - SmsAction(Double d) { - this.d = d; + SmsAction(Double aDouble) { + this.aDouble = aDouble; } - SmsAction(Integer i) { - this.i = i; + SmsAction(Integer anInteger) { + this.anInteger = anInteger; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 6c9c662098..64f7e01d0c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -73,11 +73,6 @@ public class SmsCommunicatorPlugin extends PluginBase { private AuthRequest messageToConfirm = null; - private Sms cancelTempBasalWaitingForConfirmation = null; - private Sms tempBasalWaitingForConfirmation = null; - private Sms bolusWaitingForConfirmation = null; - private Sms calibrationWaitingForConfirmation = null; - private Sms suspendWaitingForConfirmation = null; private Date lastRemoteBolusTime = new Date(0); ArrayList messages = new ArrayList<>(); @@ -158,9 +153,6 @@ public class SmsCommunicatorPlugin extends PluginBase { log.debug(receivedSms.toString()); String[] splited = receivedSms.text.split("\\s+"); - Double amount; - Double tempBasal; - int duration = 0; String passCode; boolean remoteCommandsAllowed = SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false); @@ -192,9 +184,8 @@ public class SmsCommunicatorPlugin extends PluginBase { + MainApp.gs(R.string.sms_bolus) + " " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U " + MainApp.gs(R.string.sms_basal) + " " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)"; - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); receivedSms.processed = true; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Bg")); break; case "LOOP": if (splited.length > 1) @@ -210,7 +201,7 @@ public class SmsCommunicatorPlugin extends PluginBase { MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_STOP")); String reply = MainApp.gs(R.string.smscommunicator_loophasbeendisabled) + " " + MainApp.gs(result.success ? R.string.smscommunicator_tempbasalcanceled : R.string.smscommunicator_tempbasalcancelfailed); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } }); } @@ -223,7 +214,7 @@ public class SmsCommunicatorPlugin extends PluginBase { if (loopPlugin != null && !loopPlugin.isEnabled(PluginType.LOOP)) { loopPlugin.setPluginEnabled(PluginType.LOOP, true); reply = MainApp.gs(R.string.smscommunicator_loophasbeenenabled); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_START")); } receivedSms.processed = true; @@ -240,7 +231,7 @@ public class SmsCommunicatorPlugin extends PluginBase { } else { reply = MainApp.gs(R.string.smscommunicator_loopisdisabled); } - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } receivedSms.processed = true; FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Status")); @@ -250,28 +241,48 @@ public class SmsCommunicatorPlugin extends PluginBase { MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_RESUME")); NSUpload.uploadOpenAPSOffline(0); reply = MainApp.gs(R.string.smscommunicator_loopresumed); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Resume")); break; case "SUSPEND": + int duration = 0; if (splited.length >= 3) duration = SafeParse.stringToInt(splited[2]); duration = Math.max(0, duration); duration = Math.min(180, duration); if (duration == 0) { reply = MainApp.gs(R.string.smscommunicator_wrongduration); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } else if (remoteCommandsAllowed) { passCode = generatePasscode(); reply = String.format(MainApp.gs(R.string.smscommunicator_suspendreplywithcode), duration, passCode); receivedSms.processed = true; - resetWaitingMessages(); - sendSMS(suspendWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); - suspendWaitingForConfirmation.duration = duration; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Suspend")); + messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(duration) { + @Override + public void run() { + ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() { + @Override + public void run() { + if (result.success) { + LoopPlugin.getPlugin().suspendTo(System.currentTimeMillis() + anInteger * 60L * 1000); + NSUpload.uploadOpenAPSOffline(anInteger * 60); + MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_SUSPENDED")); + String reply = MainApp.gs(R.string.smscommunicator_loopsuspended) + " " + + MainApp.gs(result.success ? R.string.smscommunicator_tempbasalcanceled : R.string.smscommunicator_tempbasalcancelfailed); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); + } else { + String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed); + reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); + } + } + }); + + } + }); } else { reply = MainApp.gs(R.string.smscommunicator_remotecommandnotallowed); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } break; } @@ -285,7 +296,7 @@ public class SmsCommunicatorPlugin extends PluginBase { MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); List q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); reply = "TERATMENTS REFRESH " + q.size() + " receivers"; - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); receivedSms.processed = true; FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Treatments_Refresh")); break; @@ -299,7 +310,7 @@ public class SmsCommunicatorPlugin extends PluginBase { MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); List q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); reply = "NSCLIENT RESTART " + q.size() + " receivers"; - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); receivedSms.processed = true; FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Nsclient_Restart")); break; @@ -314,11 +325,11 @@ public class SmsCommunicatorPlugin extends PluginBase { if (result.success) { if (pump != null) { String reply = pump.shortStatus(true); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); } } else { String reply = MainApp.gs(R.string.readstatusfailed); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } } }); @@ -332,32 +343,65 @@ public class SmsCommunicatorPlugin extends PluginBase { passCode = generatePasscode(); reply = String.format(MainApp.gs(R.string.smscommunicator_basalstopreplywithcode), passCode); receivedSms.processed = true; - resetWaitingMessages(); - sendSMS(cancelTempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Basal")); + messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction() { + @Override + public void run() { + ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() { + @Override + public void run() { + if (result.success) { + String reply = MainApp.gs(R.string.smscommunicator_tempbasalcanceled); + reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); + } else { + String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed); + reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); + } + } + }); + } + }); } else { reply = MainApp.gs(R.string.smscommunicator_remotebasalnotallowed); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } } else { - tempBasal = SafeParse.stringToDouble(splited[1]); + Double tempBasal = SafeParse.stringToDouble(splited[1]); Profile profile = ProfileFunctions.getInstance().getProfile(); if (profile == null) { reply = MainApp.gs(R.string.noprofile); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } else { tempBasal = MainApp.getConstraintChecker().applyBasalConstraints(new Constraint<>(tempBasal), profile).value(); if (remoteCommandsAllowed) { passCode = generatePasscode(); reply = String.format(MainApp.gs(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode); receivedSms.processed = true; - resetWaitingMessages(); - sendSMS(tempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); - tempBasalWaitingForConfirmation.tempBasal = tempBasal; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Basal")); + messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(tempBasal) { + @Override + public void run() { + Profile profile = ProfileFunctions.getInstance().getProfile(); + if (profile != null) + ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalAbsolute(aDouble, 30, true, profile, new Callback() { + @Override + public void run() { + if (result.success) { + String reply = String.format(MainApp.gs(R.string.smscommunicator_tempbasalset), result.absolute, result.duration); + reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); + } else { + String reply = MainApp.gs(R.string.smscommunicator_tempbasalfailed); + reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); + } + } + }); + } + }); } else { reply = MainApp.gs(R.string.smscommunicator_remotebasalnotallowed); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } } } @@ -366,41 +410,74 @@ public class SmsCommunicatorPlugin extends PluginBase { case "BOLUS": if (System.currentTimeMillis() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) { reply = MainApp.gs(R.string.smscommunicator_remotebolusnotallowed); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } else if (ConfigBuilderPlugin.getPlugin().getActivePump().isSuspended()) { reply = MainApp.gs(R.string.pumpsuspended); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } else if (splited.length > 1) { - amount = SafeParse.stringToDouble(splited[1]); - amount = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(amount)).value(); - if (amount > 0d && remoteCommandsAllowed) { + Double bolus = SafeParse.stringToDouble(splited[1]); + bolus = MainApp.getConstraintChecker().applyBolusConstraints(new Constraint<>(bolus)).value(); + if (bolus > 0d && remoteCommandsAllowed) { passCode = generatePasscode(); - reply = String.format(MainApp.gs(R.string.smscommunicator_bolusreplywithcode), amount, passCode); + reply = String.format(MainApp.gs(R.string.smscommunicator_bolusreplywithcode), bolus, passCode); receivedSms.processed = true; - resetWaitingMessages(); - sendSMS(bolusWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); - bolusWaitingForConfirmation.bolusRequested = amount; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Bolus")); + messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction(bolus) { + @Override + public void run() { + DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); + detailedBolusInfo.insulin = aDouble; + detailedBolusInfo.source = Source.USER; + ConfigBuilderPlugin.getPlugin().getCommandQueue().bolus(detailedBolusInfo, new Callback() { + @Override + public void run() { + PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); + if (result.success) { + SystemClock.sleep(T.secs(15).msecs()); // wait some time to get history + String reply = String.format(MainApp.gs(R.string.smscommunicator_bolusdelivered), result.bolusDelivered); + if (pump != null) + reply += "\n" + pump.shortStatus(true); + lastRemoteBolusTime = new Date(); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); + } else { + SystemClock.sleep(T.secs(60).msecs()); // wait some time to get history + String reply = MainApp.gs(R.string.smscommunicator_bolusfailed); + if (pump != null) + reply += "\n" + pump.shortStatus(true); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); + } + } + }); + } + }); } else { reply = MainApp.gs(R.string.smscommunicator_remotebolusnotallowed); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } } break; case "CAL": if (splited.length > 1) { - amount = SafeParse.stringToDouble(splited[1]); - if (amount > 0d && remoteCommandsAllowed) { + Double cal = SafeParse.stringToDouble(splited[1]); + if (cal > 0d && remoteCommandsAllowed) { passCode = generatePasscode(); - reply = String.format(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode), amount, passCode); + reply = String.format(MainApp.gs(R.string.smscommunicator_calibrationreplywithcode), cal, passCode); receivedSms.processed = true; - resetWaitingMessages(); - sendSMS(calibrationWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis(), passCode)); - calibrationWaitingForConfirmation.calibrationRequested = amount; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Cal")); + messageToConfirm = new AuthRequest(this, receivedSms, reply, passCode, new SmsAction() { + @Override + public void run() { + boolean result = XdripCalibrations.sendIntent(aDouble); + if (result) { + String reply = MainApp.gs(R.string.smscommunicator_calibrationsent); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); + } else { + String reply = MainApp.gs(R.string.smscommunicator_calibrationfailed); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); + } + } + }); } else { reply = MainApp.gs(R.string.smscommunicator_remotecalibrationnotallowed); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, reply)); } } break; @@ -408,105 +485,9 @@ public class SmsCommunicatorPlugin extends PluginBase { if (messageToConfirm != null) { messageToConfirm.action(splited[0]); messageToConfirm = null; - } - if (bolusWaitingForConfirmation != null && !bolusWaitingForConfirmation.processed && - bolusWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - bolusWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) { - bolusWaitingForConfirmation.processed = true; - DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); - detailedBolusInfo.insulin = bolusWaitingForConfirmation.bolusRequested; - detailedBolusInfo.source = Source.USER; - ConfigBuilderPlugin.getPlugin().getCommandQueue().bolus(detailedBolusInfo, new Callback() { - @Override - public void run() { - PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); - if (result.success) { - SystemClock.sleep(T.secs(15).msecs()); // wait some time to get history - String reply = String.format(MainApp.gs(R.string.smscommunicator_bolusdelivered), result.bolusDelivered); - if (pump != null) - reply += "\n" + pump.shortStatus(true); - lastRemoteBolusTime = new Date(); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } else { - SystemClock.sleep(T.secs(60).msecs()); // wait some time to get history - String reply = MainApp.gs(R.string.smscommunicator_bolusfailed); - if (pump != null) - reply += "\n" + pump.shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } - } - }); - } else if (tempBasalWaitingForConfirmation != null && !tempBasalWaitingForConfirmation.processed && - tempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - tempBasalWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) { - tempBasalWaitingForConfirmation.processed = true; - Profile profile = ProfileFunctions.getInstance().getProfile(); - if (profile != null) - ConfigBuilderPlugin.getPlugin().getCommandQueue().tempBasalAbsolute(tempBasalWaitingForConfirmation.tempBasal, 30, true, profile, new Callback() { - @Override - public void run() { - if (result.success) { - String reply = String.format(MainApp.gs(R.string.smscommunicator_tempbasalset), result.absolute, result.duration); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } else { - String reply = MainApp.gs(R.string.smscommunicator_tempbasalfailed); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } - } - }); - } else if (cancelTempBasalWaitingForConfirmation != null && !cancelTempBasalWaitingForConfirmation.processed && - cancelTempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - cancelTempBasalWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) { - cancelTempBasalWaitingForConfirmation.processed = true; - ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() { - @Override - public void run() { - if (result.success) { - String reply = MainApp.gs(R.string.smscommunicator_tempbasalcanceled); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } else { - String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } - } - }); - } else if (calibrationWaitingForConfirmation != null && !calibrationWaitingForConfirmation.processed && - calibrationWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - calibrationWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) { - calibrationWaitingForConfirmation.processed = true; - boolean result = XdripCalibrations.sendIntent(calibrationWaitingForConfirmation.calibrationRequested); - if (result) { - reply = MainApp.gs(R.string.smscommunicator_calibrationsent); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } else { - reply = MainApp.gs(R.string.smscommunicator_calibrationfailed); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } - } else if (suspendWaitingForConfirmation != null && !suspendWaitingForConfirmation.processed && - suspendWaitingForConfirmation.confirmCode.equals(splited[0]) && System.currentTimeMillis() - suspendWaitingForConfirmation.date < Constants.SMS_CONFIRM_TIMEOUT) { - suspendWaitingForConfirmation.processed = true; - final int dur = suspendWaitingForConfirmation.duration; - ConfigBuilderPlugin.getPlugin().getCommandQueue().cancelTempBasal(true, new Callback() { - @Override - public void run() { - if (result.success) { - LoopPlugin.getPlugin().suspendTo(System.currentTimeMillis() + dur * 60L * 1000); - NSUpload.uploadOpenAPSOffline(dur * 60); - MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_SUSPENDED")); - String reply = MainApp.gs(R.string.smscommunicator_loopsuspended) + " " + - MainApp.gs(result.success ? R.string.smscommunicator_tempbasalcanceled : R.string.smscommunicator_tempbasalcancelfailed); - sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } else { - String reply = MainApp.gs(R.string.smscommunicator_tempbasalcancelfailed); - reply += "\n" + ConfigBuilderPlugin.getPlugin().getActivePump().shortStatus(true); - sendSMS(new Sms(receivedSms.phoneNumber, reply, System.currentTimeMillis())); - } - } - }); } else { - sendSMS(new Sms(receivedSms.phoneNumber, MainApp.gs(R.string.smscommunicator_unknowncommand), System.currentTimeMillis())); + sendSMS(new Sms(receivedSms.phoneNumber, MainApp.gs(R.string.smscommunicator_unknowncommand))); } - resetWaitingMessages(); break; } } @@ -516,7 +497,7 @@ public class SmsCommunicatorPlugin extends PluginBase { public void sendNotificationToAllNumbers(String text) { for (int i = 0; i < allowedNumbers.size(); i++) { - Sms sms = new Sms(allowedNumbers.get(i), text, System.currentTimeMillis()); + Sms sms = new Sms(allowedNumbers.get(i), text); sendSMS(sms); } } @@ -556,14 +537,6 @@ public class SmsCommunicatorPlugin extends PluginBase { return passCode; } - private void resetWaitingMessages() { - tempBasalWaitingForConfirmation = null; - cancelTempBasalWaitingForConfirmation = null; - bolusWaitingForConfirmation = null; - calibrationWaitingForConfirmation = null; - suspendWaitingForConfirmation = null; - } - private static String stripAccents(String s) { s = Normalizer.normalize(s, Normalizer.Form.NFD); s = s.replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""); From cc25520770001974ee0c6749b64b8203b39f51b7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 14 Oct 2018 11:58:19 +0200 Subject: [PATCH 05/78] ignore SMS starting # --- .../plugins/SmsCommunicator/SmsCommunicatorPlugin.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 64f7e01d0c..0fc392fc8a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -482,7 +482,9 @@ public class SmsCommunicatorPlugin extends PluginBase { } break; default: // expect passCode here - if (messageToConfirm != null) { + if (splited[0].startsWith("#")) { + // user text .... ignore + } else if (messageToConfirm != null) { messageToConfirm.action(splited[0]); messageToConfirm = null; } else { From 26cb25893ecb8acb37f66b4c7b81dfaa82a501d5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 14 Oct 2018 12:01:14 +0200 Subject: [PATCH 06/78] remove fabric logging for SMS --- .../plugins/SmsCommunicator/SmsCommunicatorPlugin.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java index 0fc392fc8a..a6fde68028 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SmsCommunicator/SmsCommunicatorPlugin.java @@ -7,7 +7,6 @@ import android.os.SystemClock; import android.telephony.SmsManager; import android.telephony.SmsMessage; -import com.crashlytics.android.answers.CustomEvent; import com.squareup.otto.Subscribe; import org.slf4j.Logger; @@ -47,7 +46,6 @@ import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.services.Intents; import info.nightscout.utils.DecimalFormatter; -import info.nightscout.utils.FabricPrivacy; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import info.nightscout.utils.T; @@ -206,7 +204,6 @@ public class SmsCommunicatorPlugin extends PluginBase { }); } receivedSms.processed = true; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Stop")); break; case "ENABLE": case "START": @@ -218,7 +215,6 @@ public class SmsCommunicatorPlugin extends PluginBase { MainApp.bus().post(new EventRefreshOverview("SMS_LOOP_START")); } receivedSms.processed = true; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Start")); break; case "STATUS": loopPlugin = MainApp.getSpecificPlugin(LoopPlugin.class); @@ -234,7 +230,6 @@ public class SmsCommunicatorPlugin extends PluginBase { sendSMS(new Sms(receivedSms.phoneNumber, reply)); } receivedSms.processed = true; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Status")); break; case "RESUME": LoopPlugin.getPlugin().suspendTo(0); @@ -242,7 +237,6 @@ public class SmsCommunicatorPlugin extends PluginBase { NSUpload.uploadOpenAPSOffline(0); reply = MainApp.gs(R.string.smscommunicator_loopresumed); sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply)); - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Loop_Resume")); break; case "SUSPEND": int duration = 0; @@ -298,7 +292,6 @@ public class SmsCommunicatorPlugin extends PluginBase { reply = "TERATMENTS REFRESH " + q.size() + " receivers"; sendSMS(new Sms(receivedSms.phoneNumber, reply)); receivedSms.processed = true; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Treatments_Refresh")); break; } break; @@ -312,7 +305,6 @@ public class SmsCommunicatorPlugin extends PluginBase { reply = "NSCLIENT RESTART " + q.size() + " receivers"; sendSMS(new Sms(receivedSms.phoneNumber, reply)); receivedSms.processed = true; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Nsclient_Restart")); break; } break; @@ -334,7 +326,6 @@ public class SmsCommunicatorPlugin extends PluginBase { } }); receivedSms.processed = true; - FabricPrivacy.getInstance().logCustom(new CustomEvent("SMS_Pump")); break; case "BASAL": if (splited.length > 1) { From 8b3b1e67ea30e826f64a2a7b12f62732ba03caaa Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Wed, 20 Feb 2019 22:57:31 +0000 Subject: [PATCH 07/78] First implementation --- .../androidaps/interfaces/PumpInterface.java | 2 +- .../plugins/Actions/ActionsFragment.java | 93 +++++++------------ .../plugins/PumpCombo/ComboPlugin.java | 4 +- .../PumpDanaR/AbstractDanaRPlugin.java | 5 +- .../plugins/PumpDanaRS/DanaRSPlugin.java | 4 +- .../plugins/PumpInsight/InsightPlugin.java | 4 +- .../androidaps/plugins/PumpMDI/MDIPlugin.java | 4 +- .../PumpVirtual/VirtualPumpPlugin.java | 4 +- 8 files changed, 45 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java index 2f8e92dcc7..777ae113c4 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PumpInterface.java @@ -64,6 +64,6 @@ public interface PumpInterface { List getCustomActions(); - PumpEnactResult executeCustomAction(CustomActionType customActionType); + void executeCustomAction(CustomActionType customActionType); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java index dd48054738..69fd805dec 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/ActionsFragment.java @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.Actions; import android.app.Activity; import android.content.Intent; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; @@ -68,6 +69,9 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL SingleClickButton tddStats; SingleClickButton history; + private Map pumpCustomActions = new HashMap<>(); + private List pumpCustomButtons = new ArrayList<>(); + public ActionsFragment() { super(); } @@ -203,119 +207,84 @@ public class ActionsFragment extends SubscriberFragment implements View.OnClickL else tempTarget.setVisibility(View.VISIBLE); - if (!ConfigBuilderPlugin.getActivePump().getPumpDescription().supportsTDDs) tddStats.setVisibility(View.GONE); - else tddStats.setVisibility(View.VISIBLE); + if (!pump.getPumpDescription().supportsTDDs) + tddStats.setVisibility(View.GONE); + else + tddStats.setVisibility(View.VISIBLE); - checkCustomActions(); + checkPumpCustomActions(); } }); } - private String activePumpName; - private Map currentCustomActions = new HashMap<>(); - private List customButtons = new ArrayList<>(); - - View.OnClickListener customActionsListener = v -> { + View.OnClickListener pumpCustomActionsListener = v -> { SingleClickButton btn = (SingleClickButton)v; - CustomAction customAction = this.currentCustomActions.get(btn.getText().toString()); + CustomAction customAction = this.pumpCustomActions.get(btn.getText().toString()); ConfigBuilderPlugin.getActivePump().executeCustomAction(customAction.getCustomActionType()); }; + private void checkPumpCustomActions() { - private void checkCustomActions() { + PumpInterface activePump = ConfigBuilderPlugin.getPlugin().getActivePump(); - PumpInterface activePump = ConfigBuilderPlugin.getActivePump(); + removePumpCustomActions(); - if (activePump==null) { - removeCustomActions(); + if (activePump == null) { return; } - String newPump = activePump.getClass().getSimpleName(); - - if (newPump.equals(activePumpName)) - return; - - removeCustomActions(); - - // add new actions List customActions = activePump.getCustomActions(); - if (customActions!=null) - { + if (customActions != null && customActions.size()>0) { - LinearLayout ll = (LinearLayout)actionsFragmentView.findViewById(R.id.action_buttons_layout); + LinearLayout ll = actionsFragmentView.findViewById(R.id.action_buttons_layout); for (CustomAction customAction : customActions) { - SingleClickButton btn = new SingleClickButton(MainApp.instance().getApplicationContext()); + SingleClickButton btn = new SingleClickButton(getContext(), null, android.R.attr.buttonStyle); btn.setText(MainApp.gs(customAction.getName())); - // TODO style and drawableTop - //btn.setTextAppearance(R.style.buttonStyle); - - - LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f); - layoutParams.setMargins(10, 3, 10, 3); + layoutParams.setMargins(20, 8, 20, 8); // 10,3,10,3 btn.setLayoutParams(layoutParams); - btn.setOnClickListener(customActionsListener); + btn.setOnClickListener(pumpCustomActionsListener); + Drawable top = getResources().getDrawable(R.drawable.icon_actions_profileswitch); + btn.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null); ll.addView(btn); - this.currentCustomActions.put(MainApp.gs(customAction.getName()), customAction); - this.customButtons.add(btn); - - - - -// - - - - + this.pumpCustomActions.put(MainApp.gs(customAction.getName()), customAction); + this.pumpCustomButtons.add(btn); } } - activePumpName = newPump; } - private void removeCustomActions() { - if (currentCustomActions.size()==0) + private void removePumpCustomActions() { + + if (pumpCustomActions.size()==0) return; - LinearLayout ll = (LinearLayout)actionsFragmentView.findViewById(R.id.action_buttons_layout); + LinearLayout ll = actionsFragmentView.findViewById(R.id.action_buttons_layout); - for (SingleClickButton customButton : customButtons) { + for (SingleClickButton customButton : pumpCustomButtons) { ll.removeView(customButton); } - customButtons.clear(); - currentCustomActions.clear(); + pumpCustomButtons.clear(); + pumpCustomActions.clear(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java index 54d6a6d621..34882bc14c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpCombo/ComboPlugin.java @@ -1383,8 +1383,8 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint } @Override - public PumpEnactResult executeCustomAction(CustomActionType customActionType) { - return null; + public void executeCustomAction(CustomActionType customActionType) { + } } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java index 31cb635fec..d293dab80c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaR/AbstractDanaRPlugin.java @@ -476,9 +476,10 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte return null; } + @Override - public PumpEnactResult executeCustomAction(CustomActionType customActionType) { - return null; + public void executeCustomAction(CustomActionType customActionType) { + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java index 9610a07e75..76adba94d8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRS/DanaRSPlugin.java @@ -816,8 +816,8 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte } @Override - public PumpEnactResult executeCustomAction(CustomActionType customActionType) { - return null; + public void executeCustomAction(CustomActionType customActionType) { + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java index b1ac8a065b..387b66791d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpInsight/InsightPlugin.java @@ -940,8 +940,8 @@ public class InsightPlugin extends PluginBase implements PumpInterface, Constrai } @Override - public PumpEnactResult executeCustomAction(CustomActionType customActionType) { - return null; + public void executeCustomAction(CustomActionType customActionType) { + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java index b40c40acca..0d9260837c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpMDI/MDIPlugin.java @@ -249,8 +249,8 @@ public class MDIPlugin extends PluginBase implements PumpInterface { } @Override - public PumpEnactResult executeCustomAction(CustomActionType customActionType) { - return null; + public void executeCustomAction(CustomActionType customActionType) { + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java index 670ee2397b..9eabfd0cac 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpVirtual/VirtualPumpPlugin.java @@ -150,8 +150,8 @@ public class VirtualPumpPlugin extends PluginBase implements PumpInterface { } @Override - public PumpEnactResult executeCustomAction(CustomActionType customActionType) { - return null; + public void executeCustomAction(CustomActionType customActionType) { + } @Override From e342f4e2ae03164bde0b173af55a593efa65a828 Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Mon, 4 Mar 2019 23:13:55 +0000 Subject: [PATCH 08/78] d --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d1e1e8a47c..4409852b76 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # AndroidAPS +ddd + * Check the wiki: http://wiki.androidaps.org * Everyone who’s been looping with AndroidAPS needs to fill out the form after 3 days of looping https://docs.google.com/forms/d/14KcMjlINPMJHVt28MDRupa4sz4DDIooI4SrW0P3HSN8/viewform?c=0&w=1 From fda43c423def635a297995dcb444b2916c9133ee Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Mon, 4 Mar 2019 23:23:51 +0000 Subject: [PATCH 09/78] - Change wear versions to highest --- wear/build.gradle | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wear/build.gradle b/wear/build.gradle index cf846f51ae..276a921b7f 100644 --- a/wear/build.gradle +++ b/wear/build.gradle @@ -1,7 +1,8 @@ apply plugin: 'com.android.application' ext { - wearableVersion = "2.0.1" + wearableVersion = "2.4.0" + playServicesWearable = "16.0.1" } def generateGitBuild = { -> @@ -89,7 +90,7 @@ dependencies { //compile "com.ustwo.android:clockwise-wearable:1.0.2" compileOnly "com.google.android.wearable:wearable:${wearableVersion}" implementation "com.google.android.support:wearable:${wearableVersion}" - implementation "com.google.android.gms:play-services-wearable:7.3.0" + implementation "com.google.android.gms:play-services-wearable:${playServicesWearable}" implementation(name:"ustwo-clockwise-debug", ext:"aar") implementation "com.android.support:support-v4:27.0.1" implementation 'com.android.support:wear:27.0.1' From 056ad650cdec4dbbb91198f50bbc46b3db32e06d Mon Sep 17 00:00:00 2001 From: Andy Rozman Date: Tue, 5 Mar 2019 01:05:16 +0000 Subject: [PATCH 10/78] - changed wear versions - added some code to listener - added some log entries --- wear/build.gradle | 4 +- wear/src/main/AndroidManifest.xml | 20 +++- .../androidaps/data/ListenerService.java | 101 ++++++++++++++++-- .../interaction/utils/WearUtil.java | 19 ++++ 4 files changed, 133 insertions(+), 11 deletions(-) create mode 100644 wear/src/main/java/info/nightscout/androidaps/interaction/utils/WearUtil.java diff --git a/wear/build.gradle b/wear/build.gradle index 276a921b7f..1dd07ff1a5 100644 --- a/wear/build.gradle +++ b/wear/build.gradle @@ -1,8 +1,8 @@ apply plugin: 'com.android.application' ext { - wearableVersion = "2.4.0" - playServicesWearable = "16.0.1" + wearableVersion = "2.0.1" + playServicesWearable = "9.4.0" } def generateGitBuild = { -> diff --git a/wear/src/main/AndroidManifest.xml b/wear/src/main/AndroidManifest.xml index e6750c515d..119b83a3f5 100644 --- a/wear/src/main/AndroidManifest.xml +++ b/wear/src/main/AndroidManifest.xml @@ -168,12 +168,28 @@ + - + + + + + + + + + + + + + + + + + - diff --git a/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java b/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java index 52dfc97ad1..326d154079 100644 --- a/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java +++ b/wear/src/main/java/info/nightscout/androidaps/data/ListenerService.java @@ -7,16 +7,19 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.os.SystemClock; import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; +import android.util.Log; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.wearable.ChannelApi; import com.google.android.gms.wearable.DataEvent; import com.google.android.gms.wearable.DataEventBuffer; import com.google.android.gms.wearable.DataMap; @@ -33,12 +36,15 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.interaction.actions.AcceptActivity; import info.nightscout.androidaps.interaction.actions.CPPActivity; import info.nightscout.androidaps.interaction.utils.SafeParse; +import info.nightscout.androidaps.interaction.utils.WearUtil; + /** * Created by emmablack on 12/26/14. */ public class ListenerService extends WearableListenerService implements GoogleApiClient.ConnectionCallbacks, - GoogleApiClient.OnConnectionFailedListener { + GoogleApiClient.OnConnectionFailedListener, ChannelApi.ChannelListener { + private static final String WEARABLE_DATA_PATH = "/nightscout_watch_data"; private static final String WEARABLE_RESEND_PATH = "/nightscout_watch_data_resend"; private static final String WEARABLE_CANCELBOLUS_PATH = "/nightscout_watch_cancel_bolus"; @@ -67,19 +73,35 @@ public class ListenerService extends WearableListenerService implements GoogleAp private static final String ACTION_RESEND_BULK = "com.dexdrip.stephenblack.nightwatch.RESEND_BULK_DATA"; + GoogleApiClient googleApiClient; private long lastRequest = 0; private DismissThread confirmThread; private DismissThread bolusprogressThread; + private static final String TAG = "ListenerService"; + + private DataRequester mDataRequester = null; public class DataRequester extends AsyncTask { Context mContext; + String path; + byte[] payload; - DataRequester(Context context) { - mContext = context; +// DataRequester(Context context) { +// mContext = context; +// } + + + + DataRequester(Context context, String thispath, byte[] thispayload) { + path = thispath; + payload = thispayload; + Log.d(TAG, "DataRequester DataRequester: " + thispath + " lastRequest:" + lastRequest); } + + @Override protected Void doInBackground(Void... params) { if (googleApiClient.isConnected()) { @@ -154,6 +176,9 @@ public class ListenerService extends WearableListenerService implements GoogleAp @Override protected Void doInBackground(Void... params) { + + forceGoogleApiConnect(); + if (googleApiClient.isConnected()) { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await(); @@ -176,7 +201,7 @@ public class ListenerService extends WearableListenerService implements GoogleAp } public void requestData() { - new DataRequester(this).execute(); + sendData(WEARABLE_RESEND_PATH, null); } public void cancelBolus() { @@ -191,7 +216,48 @@ public class ListenerService extends WearableListenerService implements GoogleAp new MessageActionTask(this, WEARABLE_INITIATE_ACTIONSTRING_PATH, actionstring).execute(); } - public void googleApiConnect() { + + private synchronized void sendData(String path, byte[] payload) { + if (path == null) return; + if (mDataRequester != null) { + Log.d(TAG, "sendData DataRequester != null lastRequest:" + WearUtil.dateTimeText(lastRequest)); + if (mDataRequester.getStatus() != AsyncTask.Status.FINISHED) { + Log.d(TAG, "sendData Should be canceled? Let run 'til finished."); + //mDataRequester.cancel(true); + } + //mDataRequester = null; + } + + Log.d(TAG, "sendData: execute lastRequest:" + WearUtil.dateTimeText(lastRequest)); + mDataRequester = (DataRequester) new DataRequester(this, path, payload).execute(); + + +// if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { +// Log.d(TAG, "sendData SDK < M call execute lastRequest:" + WearUtil.dateTimeText(lastRequest)); +// mDataRequester = (DataRequester) new DataRequester(this, path, payload).execute(); +// } else { +// Log.d(TAG, "sendData SDK >= M call executeOnExecutor lastRequest:" + WearUtil.dateTimeText(lastRequest)); +// // TODO xdrip executor +// mDataRequester = (DataRequester) new DataRequester(this, path, payload).executeOnExecutor(xdrip.executor); +// } + } + + + private void googleApiConnect() { + if (googleApiClient != null) { + // Remove old listener(s) + try { + Wearable.ChannelApi.removeListener(googleApiClient, this); + } catch (Exception e) { + // + } + try { + Wearable.MessageApi.removeListener(googleApiClient, this); + } catch (Exception e) { + // + } + } + googleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) @@ -201,6 +267,20 @@ public class ListenerService extends WearableListenerService implements GoogleAp } + + private void forceGoogleApiConnect() { + if ((googleApiClient != null && !googleApiClient.isConnected() && !googleApiClient.isConnecting()) || googleApiClient == null) { + try { + Log.d(TAG, "forceGoogleApiConnect: forcing google api reconnection"); + googleApiConnect(); + Thread.sleep(2000); + } catch (InterruptedException e) { + // + } + } + } + + @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null && ACTION_RESEND.equals(intent.getAction())) { @@ -261,8 +341,10 @@ public class ListenerService extends WearableListenerService implements GoogleAp if (event.getType() == DataEvent.TYPE_CHANGED) { - String path = event.getDataItem().getUri().getPath(); + + Log.d(TAG, "Path: {}" + path + ", Event=" + event); + if (path.equals(OPEN_SETTINGS)) { Intent intent = new Intent(this, AAPSPreferences.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); @@ -480,6 +562,9 @@ public class ListenerService extends WearableListenerService implements GoogleAp @Override public void onConnected(Bundle bundle) { + Log.d(TAG, "onConnected call requestData"); + + Wearable.ChannelApi.addListener(googleApiClient, this); requestData(); } @@ -499,8 +584,10 @@ public class ListenerService extends WearableListenerService implements GoogleAp if (googleApiClient != null && googleApiClient.isConnected()) { googleApiClient.disconnect(); } + if (googleApiClient != null) { Wearable.MessageApi.removeListener(googleApiClient, this); + Wearable.ChannelApi.removeListener(googleApiClient, this); } - } + } } diff --git a/wear/src/main/java/info/nightscout/androidaps/interaction/utils/WearUtil.java b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/WearUtil.java new file mode 100644 index 0000000000..ca63749888 --- /dev/null +++ b/wear/src/main/java/info/nightscout/androidaps/interaction/utils/WearUtil.java @@ -0,0 +1,19 @@ +package info.nightscout.androidaps.interaction.utils; + +import java.time.LocalDateTime; +import java.util.Date; + +/** + * Created by andy on 3/5/19. + */ + +public class WearUtil { + + + public static String dateTimeText(long timeInMs) { + Date d = new Date(timeInMs); + return "" + d.getDay() + "." + d.getMonth() + "." + d.getYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); + } + + +} From 02db9b681d5116954910bc5f0ba05938df2034f7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 5 Mar 2019 23:12:35 +0100 Subject: [PATCH 11/78] fix MidnightTime crash + tests --- .../androidaps/utils/MidnightTime.java | 31 +++++----- .../androidaps/utils/MidnightTimeTest.java | 62 +++++++++++++++++++ 2 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java index d3d75feda3..668ca02e96 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java @@ -5,7 +5,7 @@ import android.util.LongSparseArray; import java.util.Calendar; public class MidnightTime { - private static LongSparseArray times = new LongSparseArray(); + private static final LongSparseArray times = new LongSparseArray<>(); private static long hits = 0; private static long misses = 0; @@ -20,20 +20,23 @@ public class MidnightTime { } public static long calc(long time) { - Long m = (Long) times.get(time); - if (m != null) { - ++hits; - return m; + Long m; + synchronized (times) { + m = times.get(time); + if (m != null) { + ++hits; + return m; + } + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(time); + c.set(Calendar.HOUR_OF_DAY, 0); + c.set(Calendar.MINUTE, 0); + c.set(Calendar.SECOND, 0); + c.set(Calendar.MILLISECOND, 0); + m = c.getTimeInMillis(); + times.append(time, m); + ++misses; } - Calendar c = Calendar.getInstance(); - c.setTimeInMillis(time); - c.set(Calendar.HOUR_OF_DAY, 0); - c.set(Calendar.MINUTE, 0); - c.set(Calendar.SECOND, 0); - c.set(Calendar.MILLISECOND, 0); - m = c.getTimeInMillis(); - times.append(time, m); - ++misses; return m; } diff --git a/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java b/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java new file mode 100644 index 0000000000..4770c658fc --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java @@ -0,0 +1,62 @@ +package info.nightscout.androidaps.utils; + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.Calendar; +import java.util.Date; + +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by mike on 20.11.2017. + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Calendar.class}) +public class MidnightTimeTest { + + @Test + public void calc() { + // We get real midnight + long now = DateUtil.now(); + Assert.assertTrue(now >= MidnightTime.calc()); + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(MidnightTime.calc()); + Assert.assertEquals(c.get(Calendar.HOUR_OF_DAY), 0); + Assert.assertEquals(c.get(Calendar.MINUTE), 0); + Assert.assertEquals(c.get(Calendar.SECOND), 0); + Assert.assertEquals(c.get(Calendar.MILLISECOND), 0); + } + + @Test + public void calc_time() { + // We get real midnight + long now = DateUtil.now(); + long midnight = MidnightTime.calc(now); + Assert.assertTrue(now >= midnight); + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(MidnightTime.calc(now)); + Assert.assertEquals(c.get(Calendar.HOUR_OF_DAY), 0); + Assert.assertEquals(c.get(Calendar.MINUTE), 0); + Assert.assertEquals(c.get(Calendar.SECOND), 0); + Assert.assertEquals(c.get(Calendar.MILLISECOND), 0); + // Assure we get the same time from cache + Assert.assertEquals(midnight, MidnightTime.calc(now)); + } + + @Test + public void log() { + long now = DateUtil.now(); + MidnightTime.calc(now); + Assert.assertEquals(MidnightTime.log(), "Hits: 0 misses: 1 stored: 0"); + } +} From 1a73d6053553b3479fefea7804c8331687afe631 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 5 Mar 2019 23:29:55 +0100 Subject: [PATCH 12/78] fix tests --- .../java/info/nightscout/androidaps/utils/MidnightTimeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java b/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java index 4770c658fc..a15efa6c24 100644 --- a/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java +++ b/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java @@ -57,6 +57,6 @@ public class MidnightTimeTest { public void log() { long now = DateUtil.now(); MidnightTime.calc(now); - Assert.assertEquals(MidnightTime.log(), "Hits: 0 misses: 1 stored: 0"); + Assert.assertTrue(MidnightTime.log().startsWith("Hits:")); } } From 605601bcc643ce7da3b39ec09411d4a0aca8c0c8 Mon Sep 17 00:00:00 2001 From: harisp Date: Wed, 6 Mar 2019 12:10:28 +0100 Subject: [PATCH 13/78] Run loop when TemporyTarget is changed --- .../androidaps/plugins/aps/loop/LoopPlugin.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java index 7ee7cf0348..3296d3d9d5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopPlugin.java @@ -34,6 +34,7 @@ import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.events.EventNewBG; +import info.nightscout.androidaps.events.EventTempTargetChange; import info.nightscout.androidaps.interfaces.APSInterface; import info.nightscout.androidaps.interfaces.Constraint; import info.nightscout.androidaps.interfaces.PluginBase; @@ -175,6 +176,14 @@ public class LoopPlugin extends PluginBase { return loopSuspendedTill; } + @Subscribe + public void onStatusEvent(final EventTempTargetChange ev) { + new Thread(() -> LoopPlugin.getPlugin().invoke("EventTempTargetChange", true)).start(); + FabricPrivacy.getInstance().logCustom(new CustomEvent("TT_Loop_Run")); + } + + + public void suspendTo(long endTime) { loopSuspendedTill = endTime; isSuperBolus = false; From 92cbe45341ed75a52cc3923e14b7c5d20513b533 Mon Sep 17 00:00:00 2001 From: Tanja Schmidt Date: Wed, 6 Mar 2019 19:00:11 +0100 Subject: [PATCH 14/78] Fixed status noticication for charging/uncharging: check battery not power state. --- .../general/nsclient/NsClientReceiverDelegate.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java index 07d76d90fe..8eeee8c43c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NsClientReceiverDelegate.java @@ -48,9 +48,7 @@ class NsClientReceiverDelegate { bus.post(event); context.registerReceiver(chargingStateReceiver, - new IntentFilter(Intent.ACTION_POWER_CONNECTED)); - context.registerReceiver(chargingStateReceiver, - new IntentFilter(Intent.ACTION_POWER_DISCONNECTED)); + new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); EventChargingState eventChargingState = chargingStateReceiver.grabChargingState(context); if (eventChargingState != null) @@ -97,7 +95,7 @@ class NsClientReceiverDelegate { } void processStateChange() { - boolean newAllowedState = allowedChargingState && allowedNetworkState; + boolean newAllowedState = allowedChargingState && allowedNetworkState; if (newAllowedState != allowed) { allowed = newAllowedState; bus.post(new EventPreferenceChange(R.string.key_nsclientinternal_paused)); @@ -109,7 +107,9 @@ class NsClientReceiverDelegate { boolean newAllowedState = true; - if (!ev.isCharging && chargingOnly) newAllowedState = false; + if (!ev.isCharging && chargingOnly) { + newAllowedState = false; + } return newAllowedState; } From a5d1905be894ca885453ca188f9e658bf93b6988 Mon Sep 17 00:00:00 2001 From: Tanja Schmidt Date: Mon, 30 Jul 2018 18:51:36 +0200 Subject: [PATCH 15/78] Allow some objectives to go back. --- .../objectives/ObjectivesFragment.java | 18 ++++++++++++++++++ .../objectives/objectives/Objective.java | 4 ++++ .../objectives/objectives/Objective4.java | 5 +++++ app/src/main/res/layout/objectives_item.xml | 7 +++++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 35 insertions(+) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesFragment.java index 9f1ac748d5..08c063c437 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/ObjectivesFragment.java @@ -120,6 +120,7 @@ public class ObjectivesFragment extends SubscriberFragment { public void onBindViewHolder(@NonNull ViewHolder holder, int position) { Objective objective = ObjectivesPlugin.getPlugin().getObjectives().get(position); holder.title.setText(MainApp.gs(R.string.nth_objective, position + 1)); + holder.revert.setVisibility(View.INVISIBLE); if (objective.getObjective() != 0) { holder.objective.setVisibility(View.VISIBLE); holder.objective.setText(MainApp.gs(objective.getObjective())); @@ -145,6 +146,9 @@ public class ObjectivesFragment extends SubscriberFragment { holder.verify.setVisibility(View.VISIBLE); holder.verify.setEnabled(objective.isCompleted() || enableFake.isChecked()); holder.start.setVisibility(View.GONE); + if(objective.isRevertable()) { + holder.revert.setVisibility(View.VISIBLE); + } holder.progress.setVisibility(View.VISIBLE); holder.progress.removeAllViews(); for (Objective.Task task : objective.getTasks()) { @@ -169,8 +173,20 @@ public class ObjectivesFragment extends SubscriberFragment { scrollToCurrentObjective(); startUpdateTimer(); }); + holder.revert.setOnClickListener((view) -> { + objective.setAccomplishedOn(null); + objective.setStartedOn(null); + if (position > 0) { + Objective prevObj = ObjectivesPlugin.getObjectives().get(position - 1); + prevObj.setAccomplishedOn(null); + } + notifyDataSetChanged(); + scrollToCurrentObjective(); + }); } + + @Override public int getItemCount() { return ObjectivesPlugin.getPlugin().getObjectives().size(); @@ -185,6 +201,7 @@ public class ObjectivesFragment extends SubscriberFragment { public LinearLayout progress; public Button verify; public Button start; + public Button revert; public ViewHolder(View itemView) { super(itemView); @@ -195,6 +212,7 @@ public class ObjectivesFragment extends SubscriberFragment { progress = itemView.findViewById(R.id.objective_progress); verify = itemView.findViewById(R.id.objective_verify); start = itemView.findViewById(R.id.objective_start); + revert = itemView.findViewById(R.id.objective_back); } } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java index a1935c2ec6..83abe15688 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective.java @@ -42,6 +42,10 @@ public abstract class Objective { return true; } + public boolean isRevertable() { + return false; + } + public boolean isAccomplished() { return accomplishedOn != null; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective4.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective4.java index 5708565ebe..1bbb4ef7c2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective4.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/objectives/objectives/Objective4.java @@ -25,4 +25,9 @@ public class Objective4 extends Objective { } }); } + + @Override + public boolean isRevertable() { + return true; + } } diff --git a/app/src/main/res/layout/objectives_item.xml b/app/src/main/res/layout/objectives_item.xml index b6029e5842..283195db23 100644 --- a/app/src/main/res/layout/objectives_item.xml +++ b/app/src/main/res/layout/objectives_item.xml @@ -65,6 +65,13 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/objectives_button_start" /> + +