prepare new classes

This commit is contained in:
Milos Kozak 2018-10-13 16:50:47 +02:00
parent 5c9baa31f3
commit ea1e78b169
6 changed files with 158 additions and 89 deletions

View file

@ -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));
}

View file

@ -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);
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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<SmsCommunicatorPlugin.Sms> {
public int compare(SmsCommunicatorPlugin.Sms object1, SmsCommunicatorPlugin.Sms object2) {
return (int) (object1.date - object2.date);
}
activity.runOnUiThread(() -> {
class CustomComparator implements Comparator<Sms> {
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) + " &lt;&lt;&lt; " + (sms.processed ? "" : "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
} else if (sms.sent) {
logText += DateUtil.timeString(sms.date) + " &gt;&gt;&gt; " + (sms.processed ? "" : "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
}
}
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) + " &lt;&lt;&lt; " + (sms.processed ? "" : "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
} else if (sms.sent) {
logText += DateUtil.timeString(sms.date) + " &gt;&gt;&gt; " + (sms.processed ? "" : "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
}
}
logView.setText(Html.fromHtml(logText));
});
}
}

View file

@ -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<String> 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) {