XdripCalibration -> dagger
This commit is contained in:
parent
235d241736
commit
5a2821acac
5 changed files with 51 additions and 59 deletions
|
@ -26,6 +26,7 @@ class CalibrationDialog : DialogFragmentWithDate() {
|
||||||
@Inject lateinit var injector: HasAndroidInjector
|
@Inject lateinit var injector: HasAndroidInjector
|
||||||
@Inject lateinit var resourceHelper: ResourceHelper
|
@Inject lateinit var resourceHelper: ResourceHelper
|
||||||
@Inject lateinit var profileFunction: ProfileFunction
|
@Inject lateinit var profileFunction: ProfileFunction
|
||||||
|
@Inject lateinit var xdripCalibrations: XdripCalibrations
|
||||||
|
|
||||||
override fun onSaveInstanceState(savedInstanceState: Bundle) {
|
override fun onSaveInstanceState(savedInstanceState: Bundle) {
|
||||||
super.onSaveInstanceState(savedInstanceState)
|
super.onSaveInstanceState(savedInstanceState)
|
||||||
|
@ -63,7 +64,7 @@ class CalibrationDialog : DialogFragmentWithDate() {
|
||||||
activity?.let { activity ->
|
activity?.let { activity ->
|
||||||
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
|
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("<br/>").join(actions)), Runnable {
|
||||||
aapsLogger.debug("USER ENTRY: CALIBRATION $bg")
|
aapsLogger.debug("USER ENTRY: CALIBRATION $bg")
|
||||||
XdripCalibrations.sendIntent(bg)
|
xdripCalibrations.sendIntent(bg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
|
|
|
@ -60,6 +60,7 @@ class SmsCommunicatorPlugin @Inject constructor(
|
||||||
private val commandQueue: CommandQueueProvider,
|
private val commandQueue: CommandQueueProvider,
|
||||||
private val loopPlugin: LoopPlugin,
|
private val loopPlugin: LoopPlugin,
|
||||||
private val iobCobCalculatorPlugin: IobCobCalculatorPlugin,
|
private val iobCobCalculatorPlugin: IobCobCalculatorPlugin,
|
||||||
|
private val xdripCalibrations: XdripCalibrations,
|
||||||
private var otp: OneTimePassword
|
private var otp: OneTimePassword
|
||||||
) : PluginBase(PluginDescription()
|
) : PluginBase(PluginDescription()
|
||||||
.mainType(PluginType.GENERAL)
|
.mainType(PluginType.GENERAL)
|
||||||
|
@ -845,7 +846,7 @@ class SmsCommunicatorPlugin @Inject constructor(
|
||||||
receivedSms.processed = true
|
receivedSms.processed = true
|
||||||
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(cal) {
|
messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(cal) {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
val result = XdripCalibrations.sendIntent(aDouble)
|
val result = xdripCalibrations.sendIntent(aDouble!!)
|
||||||
if (result) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_calibrationsent))) else sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_calibrationfailed)))
|
if (result) sendSMSToAllNumbers(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_calibrationsent))) else sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.smscommunicator_calibrationfailed)))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,54 +0,0 @@
|
||||||
package info.nightscout.androidaps.utils;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.pm.ResolveInfo;
|
|
||||||
import android.os.Bundle;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import info.nightscout.androidaps.Constants;
|
|
||||||
import info.nightscout.androidaps.MainApp;
|
|
||||||
import info.nightscout.androidaps.R;
|
|
||||||
import info.nightscout.androidaps.logging.StacktraceLoggerWrapper;
|
|
||||||
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
|
|
||||||
import info.nightscout.androidaps.services.Intents;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by mike on 10.02.2017.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class XdripCalibrations {
|
|
||||||
private static Logger log = StacktraceLoggerWrapper.getLogger(XdripCalibrations.class);
|
|
||||||
|
|
||||||
public static void confirmAndSendCalibration(final Double bg, final Context parentContext) {
|
|
||||||
if (parentContext != null) {
|
|
||||||
String confirmMessage = String.format(MainApp.gs(R.string.send_calibration), bg);
|
|
||||||
OKDialog.showConfirmation(parentContext, confirmMessage, () -> sendIntent(bg));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean sendIntent(Double bg) {
|
|
||||||
Context context = MainApp.instance().getApplicationContext();
|
|
||||||
Bundle bundle = new Bundle();
|
|
||||||
bundle.putDouble("glucose_number", bg);
|
|
||||||
bundle.putString("units", ConfigBuilderPlugin.getPlugin().getProfileFunction().getUnits().equals(Constants.MGDL) ? "mgdl" : "mmol");
|
|
||||||
bundle.putLong("timestamp", System.currentTimeMillis());
|
|
||||||
Intent intent = new Intent(Intents.ACTION_REMOTE_CALIBRATION);
|
|
||||||
intent.putExtras(bundle);
|
|
||||||
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
|
|
||||||
context.sendBroadcast(intent);
|
|
||||||
List<ResolveInfo> q = MainApp.instance().getApplicationContext().getPackageManager().queryBroadcastReceivers(intent, 0);
|
|
||||||
if (q.size() < 1) {
|
|
||||||
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.xdripnotinstalled));
|
|
||||||
log.debug(MainApp.gs(R.string.xdripnotinstalled));
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
ToastUtils.showToastInUiThread(MainApp.instance().getApplicationContext(), MainApp.gs(R.string.calibrationsent));
|
|
||||||
log.debug(MainApp.gs(R.string.calibrationsent));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package info.nightscout.androidaps.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import info.nightscout.androidaps.Constants
|
||||||
|
import info.nightscout.androidaps.R
|
||||||
|
import info.nightscout.androidaps.logging.AAPSLogger
|
||||||
|
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction
|
||||||
|
import info.nightscout.androidaps.services.Intents
|
||||||
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
||||||
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class XdripCalibrations @Inject constructor(
|
||||||
|
private val aapsLogger: AAPSLogger,
|
||||||
|
private val resourceHelper: ResourceHelper,
|
||||||
|
private val context: Context,
|
||||||
|
private val profileFunction: ProfileFunction
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun sendIntent(bg: Double): Boolean {
|
||||||
|
val bundle = Bundle()
|
||||||
|
bundle.putDouble("glucose_number", bg)
|
||||||
|
bundle.putString("units", if (profileFunction.getUnits() == Constants.MGDL) "mgdl" else "mmol")
|
||||||
|
bundle.putLong("timestamp", System.currentTimeMillis())
|
||||||
|
val intent = Intent(Intents.ACTION_REMOTE_CALIBRATION)
|
||||||
|
intent.putExtras(bundle)
|
||||||
|
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
|
||||||
|
context.sendBroadcast(intent)
|
||||||
|
val q = context.packageManager.queryBroadcastReceivers(intent, 0)
|
||||||
|
return if (q.size < 1) {
|
||||||
|
ToastUtils.showToastInUiThread(context, resourceHelper.gs(R.string.xdripnotinstalled))
|
||||||
|
aapsLogger.debug(resourceHelper.gs(R.string.xdripnotinstalled))
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
ToastUtils.showToastInUiThread(context, resourceHelper.gs(R.string.calibrationsent))
|
||||||
|
aapsLogger.debug(resourceHelper.gs(R.string.calibrationsent))
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -35,6 +35,7 @@ import org.mockito.ArgumentMatchers
|
||||||
import org.mockito.Mock
|
import org.mockito.Mock
|
||||||
import org.mockito.Mockito
|
import org.mockito.Mockito
|
||||||
import org.mockito.Mockito.`when`
|
import org.mockito.Mockito.`when`
|
||||||
|
import org.mockito.Mockito.anyDouble
|
||||||
import org.mockito.invocation.InvocationOnMock
|
import org.mockito.invocation.InvocationOnMock
|
||||||
import org.mockito.stubbing.Answer
|
import org.mockito.stubbing.Answer
|
||||||
import org.powermock.api.mockito.PowerMockito
|
import org.powermock.api.mockito.PowerMockito
|
||||||
|
@ -58,6 +59,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
|
||||||
@Mock lateinit var localProfilePlugin: LocalProfilePlugin
|
@Mock lateinit var localProfilePlugin: LocalProfilePlugin
|
||||||
@Mock lateinit var treatmentService: TreatmentService
|
@Mock lateinit var treatmentService: TreatmentService
|
||||||
@Mock lateinit var otp: OneTimePassword
|
@Mock lateinit var otp: OneTimePassword
|
||||||
|
@Mock lateinit var xdripCalibrations: XdripCalibrations
|
||||||
|
|
||||||
var injector: HasAndroidInjector = HasAndroidInjector {
|
var injector: HasAndroidInjector = HasAndroidInjector {
|
||||||
AndroidInjector {
|
AndroidInjector {
|
||||||
|
@ -98,14 +100,13 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
|
||||||
`when`(iobCobCalculatorPlugin.getCobInfo(false, "SMS COB")).thenReturn(CobInfo(10.0, 2.0))
|
`when`(iobCobCalculatorPlugin.getCobInfo(false, "SMS COB")).thenReturn(CobInfo(10.0, 2.0))
|
||||||
`when`(iobCobCalculatorPlugin.lastBg()).thenReturn(reading)
|
`when`(iobCobCalculatorPlugin.lastBg()).thenReturn(reading)
|
||||||
|
|
||||||
PowerMockito.mockStatic(XdripCalibrations::class.java)
|
|
||||||
PowerMockito.spy(DateUtil::class.java)
|
PowerMockito.spy(DateUtil::class.java)
|
||||||
PowerMockito.mockStatic(SmsManager::class.java)
|
PowerMockito.mockStatic(SmsManager::class.java)
|
||||||
val smsManager = PowerMockito.mock(SmsManager::class.java)
|
val smsManager = PowerMockito.mock(SmsManager::class.java)
|
||||||
`when`(SmsManager.getDefault()).thenReturn(smsManager)
|
`when`(SmsManager.getDefault()).thenReturn(smsManager)
|
||||||
`when`(sp.getString(R.string.key_smscommunicator_allowednumbers, "")).thenReturn("1234;5678")
|
`when`(sp.getString(R.string.key_smscommunicator_allowednumbers, "")).thenReturn("1234;5678")
|
||||||
|
|
||||||
smsCommunicatorPlugin = SmsCommunicatorPlugin(injector, aapsLogger, resourceHelper, sp, constraintChecker, rxBus, profileFunction, fabricPrivacy, activePlugin, commandQueue, loopPlugin, iobCobCalculatorPlugin, otp)
|
smsCommunicatorPlugin = SmsCommunicatorPlugin(injector, aapsLogger, resourceHelper, sp, constraintChecker, rxBus, profileFunction, fabricPrivacy, activePlugin, commandQueue, loopPlugin, iobCobCalculatorPlugin, xdripCalibrations, otp)
|
||||||
smsCommunicatorPlugin.setPluginEnabled(PluginType.GENERAL, true)
|
smsCommunicatorPlugin.setPluginEnabled(PluginType.GENERAL, true)
|
||||||
Mockito.doAnswer { invocation: InvocationOnMock ->
|
Mockito.doAnswer { invocation: InvocationOnMock ->
|
||||||
val callback = invocation.getArgument<Callback>(1)
|
val callback = invocation.getArgument<Callback>(1)
|
||||||
|
@ -890,7 +891,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
|
||||||
smsCommunicatorPlugin.processSms(sms)
|
smsCommunicatorPlugin.processSms(sms)
|
||||||
Assert.assertEquals("CAL 0", smsCommunicatorPlugin.messages[0].text)
|
Assert.assertEquals("CAL 0", smsCommunicatorPlugin.messages[0].text)
|
||||||
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text)
|
Assert.assertEquals("Wrong format", smsCommunicatorPlugin.messages[1].text)
|
||||||
PowerMockito.`when`(XdripCalibrations.sendIntent(anyObject())).thenReturn(true)
|
PowerMockito.`when`(xdripCalibrations.sendIntent(ArgumentMatchers.anyDouble())).thenReturn(true)
|
||||||
//CAL 1
|
//CAL 1
|
||||||
smsCommunicatorPlugin.messages = ArrayList()
|
smsCommunicatorPlugin.messages = ArrayList()
|
||||||
sms = Sms("1234", "CAL 1")
|
sms = Sms("1234", "CAL 1")
|
||||||
|
|
Loading…
Reference in a new issue