From ea1e78b16921fea12e0ceaf5ceccb01ed4a46c2b Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 13 Oct 2018 16:50:47 +0200 Subject: [PATCH] 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) {