From 02b3da97e05e3d2c12fdfdf94758fcde6557a200 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 25 Dec 2016 11:29:13 +0100 Subject: [PATCH] send important confirmations to all phone numbers --- .../SmsCommunicatorPlugin.java | 80 +++++++++++-------- 1 file changed, 46 insertions(+), 34 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 50ea260884..11dfa750a4 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 @@ -71,6 +71,14 @@ public class SmsCommunicatorPlugin implements PluginBase { sent = true; } + public Sms(String phoneNumber, String text, Date 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; } @@ -183,7 +191,6 @@ public class SmsCommunicatorPlugin implements PluginBase { Double amount = 0d; Double tempBasal = 0d; String passCode = ""; - Sms newSms = null; if (splited.length > 0) { switch (splited[0].toUpperCase()) { @@ -194,7 +201,7 @@ public class SmsCommunicatorPlugin implements PluginBase { if (loopPlugin != null && loopPlugin.isEnabled(PluginBase.LOOP)) { loopPlugin.setFragmentEnabled(PluginBase.LOOP, false); reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeendisabled); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } receivedSms.processed = true; break; @@ -203,7 +210,7 @@ public class SmsCommunicatorPlugin implements PluginBase { if (loopPlugin != null && !loopPlugin.isEnabled(PluginBase.LOOP)) { loopPlugin.setFragmentEnabled(PluginBase.LOOP, true); reply = MainApp.sResources.getString(R.string.smscommunicator_loophasbeenenabled); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } receivedSms.processed = true; break; @@ -215,7 +222,7 @@ public class SmsCommunicatorPlugin implements PluginBase { } else { reply = MainApp.sResources.getString(R.string.smscommunicator_loopisdisabled); } - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } receivedSms.processed = true; break; @@ -229,7 +236,7 @@ public class SmsCommunicatorPlugin implements PluginBase { MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); List q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); reply = "TERATMENTS REFRESH " + q.size() + " receivers"; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); receivedSms.processed = true; break; } @@ -241,7 +248,7 @@ public class SmsCommunicatorPlugin implements PluginBase { MainApp.instance().getApplicationContext().sendBroadcast(restartNSClient); List q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(restartNSClient, 0); reply = "NSCLIENT RESTART " + q.size() + " receivers"; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); receivedSms.processed = true; break; } @@ -250,7 +257,7 @@ public class SmsCommunicatorPlugin implements PluginBase { DanaRPlugin danaRPlugin = (DanaRPlugin) MainApp.getSpecificPlugin(DanaRPlugin.class); if (danaRPlugin != null && danaRPlugin.isEnabled(PluginBase.PUMP)) reply = danaRPlugin.shortStatus(); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); receivedSms.processed = true; break; case "BASAL": @@ -261,13 +268,11 @@ public class SmsCommunicatorPlugin implements PluginBase { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalstopreplywithcode), passCode); receivedSms.processed = true; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); - newSms.confirmCode = passCode; resetWaitingMessages(); - cancelTempBasalWaitingForConfirmation = newSms; + sendSMS(cancelTempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); } else { reply = MainApp.sResources.getString(R.string.remotebasalnotallowed); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } } else { tempBasal = SafeParse.stringToDouble(splited[1]); @@ -276,14 +281,12 @@ public class SmsCommunicatorPlugin implements PluginBase { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_basalreplywithcode), tempBasal, passCode); receivedSms.processed = true; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); - newSms.tempBasal = tempBasal; - newSms.confirmCode = passCode; resetWaitingMessages(); - tempBasalWaitingForConfirmation = newSms; - } else { + sendSMS(tempBasalWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); + tempBasalWaitingForConfirmation.tempBasal = tempBasal; + } else { reply = MainApp.sResources.getString(R.string.remotebasalnotallowed); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } } } @@ -291,7 +294,7 @@ public class SmsCommunicatorPlugin implements PluginBase { case "BOLUS": if (new Date().getTime() - lastRemoteBolusTime.getTime() < Constants.remoteBolusMinDistance) { reply = MainApp.sResources.getString(R.string.remotebolusnotallowed); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } else if (splited.length > 1) { amount = SafeParse.stringToDouble(splited[1]); amount = MainApp.getConfigBuilder().applyBolusConstraints(amount); @@ -300,14 +303,12 @@ public class SmsCommunicatorPlugin implements PluginBase { passCode = generatePasscode(); reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_bolusreplywithcode), amount, passCode); receivedSms.processed = true; - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); - newSms.bolusRequested = amount; - newSms.confirmCode = passCode; resetWaitingMessages(); - bolusWaitingForConfirmation = newSms; + sendSMS(bolusWaitingForConfirmation = new Sms(receivedSms.phoneNumber, reply, new Date(), passCode)); + bolusWaitingForConfirmation.bolusRequested = amount; } else { reply = MainApp.sResources.getString(R.string.remotebolusnotallowed); - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } } break; @@ -323,11 +324,12 @@ public class SmsCommunicatorPlugin implements PluginBase { reply = String.format(MainApp.sResources.getString(R.string.bolusdelivered), result.bolusDelivered); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); lastRemoteBolusTime = new Date(); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date())); } else { reply = MainApp.sResources.getString(R.string.bolusfailed); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); } } else if (tempBasalWaitingForConfirmation != null && !tempBasalWaitingForConfirmation.processed && tempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - tempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { @@ -339,11 +341,12 @@ public class SmsCommunicatorPlugin implements PluginBase { if (result.success) { reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalset), result.absolute, result.duration); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date())); } else { reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalfailed); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); } } else if (cancelTempBasalWaitingForConfirmation != null && !cancelTempBasalWaitingForConfirmation.processed && cancelTempBasalWaitingForConfirmation.confirmCode.equals(splited[0]) && new Date().getTime() - cancelTempBasalWaitingForConfirmation.date.getTime() < CONFIRM_TIMEOUT) { @@ -355,30 +358,39 @@ public class SmsCommunicatorPlugin implements PluginBase { if (result.success) { reply = String.format(MainApp.sResources.getString(R.string.smscommunicator_tempbasalcanceled)); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMSToAllNumbers(new Sms(receivedSms.phoneNumber, reply, new Date())); } else { reply = MainApp.sResources.getString(R.string.smscommunicator_tempbasalcancelfailed); if (danaRPlugin != null) reply += "\n" + danaRPlugin.shortStatus(); + sendSMS(new Sms(receivedSms.phoneNumber, reply, new Date())); } - newSms = new Sms(receivedSms.phoneNumber, reply, new Date()); } } else { - newSms = new Sms(receivedSms.phoneNumber, MainApp.sResources.getString(R.string.smscommunicator_unknowncommand), new Date()); + sendSMS(new Sms(receivedSms.phoneNumber, MainApp.sResources.getString(R.string.smscommunicator_unknowncommand), new Date())); } resetWaitingMessages(); break; } } - if (newSms != null) { - SmsManager smsManager = SmsManager.getDefault(); - newSms.text = stripAccents(newSms.text); - if (newSms.text.length() > 140) newSms.text = newSms.text.substring(0, 139); - smsManager.sendTextMessage(newSms.phoneNumber, null, newSms.text, null, null); - messages.add(newSms); - } MainApp.bus().post(new EventSmsCommunicatorUpdateGui()); } + public void sendSMSToAllNumbers(Sms sms) { + for (int i = 0; i < allowedNumbers.size(); i++) { + sms.phoneNumber = allowedNumbers.get(i); + sendSMS(sms); + } + } + + public 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); + smsManager.sendTextMessage(sms.phoneNumber, null, sms.text, null, null); + messages.add(sms); + } + private String generatePasscode() { int startChar1 = 'A'; // on iphone 1st char is uppercase :) String passCode = Character.toString((char) (startChar1 + Math.random() * ('z' - 'a' + 1)));