SmsCommunicatorFragment -> kt

This commit is contained in:
Milos Kozak 2019-11-22 00:33:13 +01:00
parent d227013496
commit a62750c3a3
9 changed files with 100 additions and 125 deletions

View file

@ -204,7 +204,7 @@ public class MainApp extends Application {
pluginsList.add(SourcePoctechPlugin.getPlugin());
pluginsList.add(SourceTomatoPlugin.getPlugin());
pluginsList.add(SourceEversensePlugin.getPlugin());
if (!Config.NSCLIENT) pluginsList.add(SmsCommunicatorPlugin.getPlugin());
if (!Config.NSCLIENT) pluginsList.add(SmsCommunicatorPlugin.INSTANCE);
pluginsList.add(FoodPlugin.getPlugin());
pluginsList.add(WearPlugin.initPlugin(this));

View file

@ -184,7 +184,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre
addPreferencesFromResourceIfEnabled(NSClientPlugin.getPlugin(), PluginType.GENERAL);
addPreferencesFromResourceIfEnabled(TidepoolPlugin.INSTANCE, PluginType.GENERAL);
addPreferencesFromResourceIfEnabled(SmsCommunicatorPlugin.getPlugin(), PluginType.GENERAL);
addPreferencesFromResourceIfEnabled(SmsCommunicatorPlugin.INSTANCE, PluginType.GENERAL);
addPreferencesFromResourceIfEnabled(AutomationPlugin.INSTANCE, PluginType.GENERAL);
addPreferencesFromResource(R.xml.pref_others);

View file

@ -36,7 +36,7 @@ public class ActionSendSMS extends Action {
@Override
public void doAction(Callback callback) {
boolean result = SmsCommunicatorPlugin.getPlugin().sendNotificationToAllNumbers(text.getValue());
boolean result = SmsCommunicatorPlugin.INSTANCE.sendNotificationToAllNumbers(text.getValue());
if (callback != null)
callback.result(new PumpEnactResult().success(result).comment(result ? R.string.ok : R.string.danar_error)).run();

View file

@ -1,83 +0,0 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import java.util.Collections;
import java.util.Comparator;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSmsCommunicatorUpdateGui;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.FabricPrivacy;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
public class SmsCommunicatorFragment extends Fragment {
private CompositeDisposable disposable = new CompositeDisposable();
TextView logView;
public SmsCommunicatorFragment() {
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.smscommunicator_fragment, container, false);
logView = (TextView) view.findViewById(R.id.smscommunicator_log);
return view;
}
@Override
public synchronized void onResume() {
super.onResume();
disposable.add(RxBus.INSTANCE
.toObservable(EventSmsCommunicatorUpdateGui.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(event -> updateGui(), FabricPrivacy::logException)
);
updateGui();
}
@Override
public synchronized void onPause() {
super.onPause();
disposable.clear();
}
protected void updateGui() {
class CustomComparator implements Comparator<Sms> {
public int compare(Sms object1, Sms object2) {
return (int) (object1.date - object2.date);
}
}
Collections.sort(SmsCommunicatorPlugin.getPlugin().getMessages(), new CustomComparator());
int messagesToShow = 40;
int start = Math.max(0, SmsCommunicatorPlugin.getPlugin().getMessages().size() - messagesToShow);
String logText = "";
for (int x = start; x < SmsCommunicatorPlugin.getPlugin().getMessages().size(); x++) {
Sms sms = SmsCommunicatorPlugin.getPlugin().getMessages().get(x);
if (sms.ignored) {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + "" + sms.phoneNumber + " <b>" + sms.text + "</b><br>";
} else 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

@ -0,0 +1,70 @@
package info.nightscout.androidaps.plugins.general.smsCommunicator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.plugins.bus.RxBus.toObservable
import info.nightscout.androidaps.plugins.general.smsCommunicator.events.EventSmsCommunicatorUpdateGui
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.HtmlHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.smscommunicator_fragment.*
import java.util.*
import kotlin.math.max
class SmsCommunicatorFragment : Fragment() {
private val disposable = CompositeDisposable()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.smscommunicator_fragment, container, false)
}
@Synchronized
override fun onResume() {
super.onResume()
disposable.add(toObservable(EventSmsCommunicatorUpdateGui::class.java)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ updateGui() }) { FabricPrivacy.logException(it) }
)
updateGui()
}
@Synchronized
override fun onPause() {
super.onPause()
disposable.clear()
}
fun updateGui() {
class CustomComparator : Comparator<Sms> {
override fun compare(object1: Sms, object2: Sms): Int {
return (object1.date - object2.date).toInt()
}
}
Collections.sort(SmsCommunicatorPlugin.messages, CustomComparator())
val messagesToShow = 40
val start = max(0, SmsCommunicatorPlugin.messages.size - messagesToShow)
var logText = ""
for (x in start until SmsCommunicatorPlugin.messages.size) {
val sms = SmsCommunicatorPlugin.messages[x]
when {
sms.ignored -> {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + "" + sms.phoneNumber + " <b>" + sms.text + "</b><br>"
}
sms.received -> {
logText += DateUtil.timeString(sms.date) + " &lt;&lt;&lt; " + (if (sms.processed) "" else "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>"
}
sms.sent -> {
logText += DateUtil.timeString(sms.date) + " &gt;&gt;&gt; " + (if (sms.processed) "" else "") + sms.phoneNumber + " <b>" + sms.text + "</b><br>"
}
}
}
smscommunicator_log?.text = HtmlHelper.fromHtml(logText)
}
}

View file

@ -49,7 +49,7 @@ import java.util.*
/**
* Created by mike on 05.08.2016.
*/
class SmsCommunicatorPlugin internal constructor() : PluginBase(PluginDescription()
object SmsCommunicatorPlugin : PluginBase(PluginDescription()
.mainType(PluginType.GENERAL)
.fragmentClass(SmsCommunicatorFragment::class.java.name)
.pluginName(R.string.smscommunicator)
@ -57,12 +57,17 @@ class SmsCommunicatorPlugin internal constructor() : PluginBase(PluginDescriptio
.preferencesId(R.xml.pref_smscommunicator)
.description(R.string.description_sms_communicator)
) {
private val log = LoggerFactory.getLogger(L.SMS)
private val disposable = CompositeDisposable()
var allowedNumbers: MutableList<String> = ArrayList()
var messageToConfirm: AuthRequest? = null
var lastRemoteBolusTime: Long = 0
var messages = ArrayList<Sms>()
init {
processSettings(null)
}
override fun onStart() {
super.onStart()
disposable.add(toObservable(EventPreferenceChange::class.java)
@ -772,18 +777,6 @@ class SmsCommunicatorPlugin internal constructor() : PluginBase(PluginDescriptio
return passCode
}
companion object {
private val log = LoggerFactory.getLogger(L.SMS)
private var smsCommunicatorPlugin: SmsCommunicatorPlugin? = null
@JvmStatic
val plugin: SmsCommunicatorPlugin?
get() {
if (smsCommunicatorPlugin == null) {
smsCommunicatorPlugin = SmsCommunicatorPlugin()
}
return smsCommunicatorPlugin
}
private fun stripAccents(str: String): String {
var s = str
s = Normalizer.normalize(s, Normalizer.Form.NFD)
@ -805,8 +798,3 @@ class SmsCommunicatorPlugin internal constructor() : PluginBase(PluginDescriptio
return countNumbers > 1
}
}
init {
processSettings(null)
}
}

View file

@ -50,7 +50,7 @@ public class CommandSetProfile extends Command {
// Send SMS notification if ProfileSwitch is comming from NS
ProfileSwitch profileSwitch = TreatmentsPlugin.getPlugin().getProfileSwitchFromHistory(System.currentTimeMillis());
if (profileSwitch != null && r.enacted && profileSwitch.source == Source.NIGHTSCOUT) {
SmsCommunicatorPlugin smsCommunicatorPlugin = SmsCommunicatorPlugin.getPlugin();
SmsCommunicatorPlugin smsCommunicatorPlugin = SmsCommunicatorPlugin.INSTANCE;
if (smsCommunicatorPlugin.isEnabled(PluginType.GENERAL)) {
smsCommunicatorPlugin.sendNotificationToAllNumbers(MainApp.gs(R.string.profile_set_ok));
}

View file

@ -103,7 +103,7 @@ public class DataService extends IntentService {
) {
handleNewDataFromNSClient(intent);
} else if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(action)) {
SmsCommunicatorPlugin.getPlugin().handleNewData(intent);
SmsCommunicatorPlugin.INSTANCE.handleNewData(intent);
}
if (L.isEnabled(L.DATASERVICE))

View file

@ -758,7 +758,7 @@ public class SmsCommunicatorPluginTest {
when(SmsManager.getDefault()).thenReturn(smsManager);
when(SP.getString(R.string.key_smscommunicator_allowednumbers, "")).thenReturn("1234;5678");
smsCommunicatorPlugin = SmsCommunicatorPlugin.getPlugin();
smsCommunicatorPlugin = SmsCommunicatorPlugin.INSTANCE;
smsCommunicatorPlugin.setPluginEnabled(PluginType.GENERAL, true);
mockStatic(LoopPlugin.class);