diff --git a/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt b/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt index 2cf4919f5e..fa8aedecac 100644 --- a/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/dialogs/CalibrationDialog.kt @@ -26,6 +26,7 @@ class CalibrationDialog : DialogFragmentWithDate() { @Inject lateinit var injector: HasAndroidInjector @Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var profileFunction: ProfileFunction + @Inject lateinit var xdripCalibrations: XdripCalibrations override fun onSaveInstanceState(savedInstanceState: Bundle) { super.onSaveInstanceState(savedInstanceState) @@ -63,7 +64,7 @@ class CalibrationDialog : DialogFragmentWithDate() { activity?.let { activity -> OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.overview_calibration), HtmlHelper.fromHtml(Joiner.on("
").join(actions)), Runnable { aapsLogger.debug("USER ENTRY: CALIBRATION $bg") - XdripCalibrations.sendIntent(bg) + xdripCalibrations.sendIntent(bg) }) } } else diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index c408afb13a..78a0bf0ffc 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -60,6 +60,7 @@ class SmsCommunicatorPlugin @Inject constructor( private val commandQueue: CommandQueueProvider, private val loopPlugin: LoopPlugin, private val iobCobCalculatorPlugin: IobCobCalculatorPlugin, + private val xdripCalibrations: XdripCalibrations, private var otp: OneTimePassword ) : PluginBase(PluginDescription() .mainType(PluginType.GENERAL) @@ -845,7 +846,7 @@ class SmsCommunicatorPlugin @Inject constructor( receivedSms.processed = true messageToConfirm = AuthRequest(injector, receivedSms, reply, passCode, object : SmsAction(cal) { 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))) } }) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.java b/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.java deleted file mode 100644 index 2b00d25ee3..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.java +++ /dev/null @@ -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 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; - } - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.kt b/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.kt new file mode 100644 index 0000000000..9c32ccadef --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/XdripCalibrations.kt @@ -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 + } + } +} \ No newline at end of file diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt index 404646bad5..aac2b47312 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPluginTest.kt @@ -35,6 +35,7 @@ import org.mockito.ArgumentMatchers import org.mockito.Mock import org.mockito.Mockito import org.mockito.Mockito.`when` +import org.mockito.Mockito.anyDouble import org.mockito.invocation.InvocationOnMock import org.mockito.stubbing.Answer import org.powermock.api.mockito.PowerMockito @@ -58,6 +59,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { @Mock lateinit var localProfilePlugin: LocalProfilePlugin @Mock lateinit var treatmentService: TreatmentService @Mock lateinit var otp: OneTimePassword + @Mock lateinit var xdripCalibrations: XdripCalibrations var injector: HasAndroidInjector = HasAndroidInjector { AndroidInjector { @@ -98,14 +100,13 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { `when`(iobCobCalculatorPlugin.getCobInfo(false, "SMS COB")).thenReturn(CobInfo(10.0, 2.0)) `when`(iobCobCalculatorPlugin.lastBg()).thenReturn(reading) - PowerMockito.mockStatic(XdripCalibrations::class.java) PowerMockito.spy(DateUtil::class.java) PowerMockito.mockStatic(SmsManager::class.java) val smsManager = PowerMockito.mock(SmsManager::class.java) `when`(SmsManager.getDefault()).thenReturn(smsManager) `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) Mockito.doAnswer { invocation: InvocationOnMock -> val callback = invocation.getArgument(1) @@ -890,7 +891,7 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() { smsCommunicatorPlugin.processSms(sms) Assert.assertEquals("CAL 0", smsCommunicatorPlugin.messages[0].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 smsCommunicatorPlugin.messages = ArrayList() sms = Sms("1234", "CAL 1")