ActionAlarm

This commit is contained in:
Milos Kozak 2020-12-14 13:52:47 +01:00
parent 4d02587bb7
commit f5f4c655c5
6 changed files with 174 additions and 0 deletions

View file

@ -39,6 +39,7 @@ abstract class AutomationModule {
@ContributesAndroidInjector abstract fun actionLoopResumeInjector(): ActionLoopResume @ContributesAndroidInjector abstract fun actionLoopResumeInjector(): ActionLoopResume
@ContributesAndroidInjector abstract fun actionLoopSuspendInjector(): ActionLoopSuspend @ContributesAndroidInjector abstract fun actionLoopSuspendInjector(): ActionLoopSuspend
@ContributesAndroidInjector abstract fun actionNotificationInjector(): ActionNotification @ContributesAndroidInjector abstract fun actionNotificationInjector(): ActionNotification
@ContributesAndroidInjector abstract fun actionAlarmInjector(): ActionAlarm
@ContributesAndroidInjector abstract fun actionProfileSwitchInjector(): ActionProfileSwitch @ContributesAndroidInjector abstract fun actionProfileSwitchInjector(): ActionProfileSwitch
@ContributesAndroidInjector abstract fun actionProfileSwitchPercentInjector(): ActionProfileSwitchPercent @ContributesAndroidInjector abstract fun actionProfileSwitchPercentInjector(): ActionProfileSwitchPercent
@ContributesAndroidInjector abstract fun actionSendSMSInjector(): ActionSendSMS @ContributesAndroidInjector abstract fun actionSendSMSInjector(): ActionSendSMS

View file

@ -239,6 +239,7 @@ class AutomationPlugin @Inject constructor(
ActionStartTempTarget(injector), ActionStartTempTarget(injector),
ActionStopTempTarget(injector), ActionStopTempTarget(injector),
ActionNotification(injector), ActionNotification(injector),
ActionAlarm(injector),
ActionProfileSwitchPercent(injector), ActionProfileSwitchPercent(injector),
ActionProfileSwitch(injector), ActionProfileSwitch(injector),
ActionSendSMS(injector) ActionSendSMS(injector)

View file

@ -0,0 +1,72 @@
package info.nightscout.androidaps.plugins.general.automation.actions
import android.content.Context
import android.content.Intent
import android.widget.LinearLayout
import androidx.annotation.DrawableRes
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.ErrorHelperActivity
import info.nightscout.androidaps.activities.PreferencesActivity
import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.events.EventRefreshOverview
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.automation.elements.InputString
import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement
import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification
import info.nightscout.androidaps.plugins.general.overview.notifications.NotificationUserMessage
import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.utils.JsonHelper
import info.nightscout.androidaps.utils.alertDialogs.WarningDialog
import info.nightscout.androidaps.utils.resources.ResourceHelper
import org.json.JSONObject
import javax.inject.Inject
class ActionAlarm(injector: HasAndroidInjector) : Action(injector) {
@Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var nsUpload: NSUpload
@Inject lateinit var context: Context
var text = InputString(injector)
override fun friendlyName(): Int = R.string.alarm
override fun shortDescription(): String = resourceHelper.gs(R.string.alarm_message, text.value)
@DrawableRes override fun icon(): Int = R.drawable.ic_access_alarm_24dp
override fun doAction(callback: Callback) {
val i = Intent(context, ErrorHelperActivity::class.java)
i.putExtra("soundid", R.raw.modern_alarm)
i.putExtra("status", text.value)
i.putExtra("title", resourceHelper.gs(R.string.alarm))
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(i)
callback.result(PumpEnactResult(injector).success(true).comment(R.string.ok))?.run()
}
override fun toJSON(): String {
val data = JSONObject().put("text", text.value)
return JSONObject()
.put("type", this.javaClass.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Action {
val o = JSONObject(data)
text.value = JsonHelper.safeGetString(o, "text", "")
return this
}
override fun hasDialog(): Boolean = true
override fun generateDialog(root: LinearLayout) {
LayoutBuilder()
.add(LabelWithElement(injector, resourceHelper.gs(R.string.alarm_short), "", text))
.build(root)
}
}

Binary file not shown.

View file

@ -1174,7 +1174,9 @@
<string name="notification">Notification</string> <string name="notification">Notification</string>
<string name="notification_message">Notification: %1$s</string> <string name="notification_message">Notification: %1$s</string>
<string name="alarm_message">Alarm: %1$s</string>
<string name="message_short">Msg:</string> <string name="message_short">Msg:</string>
<string name="alarm_short">Alarm:</string>
<string name="profilepercentage">Profile percentage</string> <string name="profilepercentage">Profile percentage</string>
<string name="percent_u">Percent [%]:</string> <string name="percent_u">Percent [%]:</string>
<string name="startprofile">Start profile %1$d%% for %2$d min</string> <string name="startprofile">Start profile %1$d%% for %2$d min</string>
@ -1411,5 +1413,6 @@
<string name="filter">Filter</string> <string name="filter">Filter</string>
<string name="copytolocalprofile_invalid">Unable to create local profile. Profile is invalid.</string> <string name="copytolocalprofile_invalid">Unable to create local profile. Profile is invalid.</string>
<string name="cta_dont_kill_my_app_info">Don\'t kill my app?</string> <string name="cta_dont_kill_my_app_info">Don\'t kill my app?</string>
<string name="alarm">Alarm</string>
</resources> </resources>

View file

@ -0,0 +1,97 @@
package info.nightscout.androidaps.plugins.general.automation.actions
import android.content.Context
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.R
import info.nightscout.androidaps.TestBase
import info.nightscout.androidaps.data.PumpEnactResult
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.automation.elements.InputString
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.queue.Callback
import info.nightscout.androidaps.utils.resources.ResourceHelper
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
@RunWith(PowerMockRunner::class)
@PrepareForTest(NSUpload::class, RxBusWrapper::class)
class ActionAlarmTest : TestBase() {
@Mock lateinit var resourceHelper: ResourceHelper
@Mock lateinit var rxBus: RxBusWrapper
@Mock lateinit var nsUpload: NSUpload
@Mock lateinit var context: Context
private lateinit var sut: ActionAlarm
var injector: HasAndroidInjector = HasAndroidInjector {
AndroidInjector {
if (it is ActionAlarm) {
it.resourceHelper = resourceHelper
it.rxBus = rxBus
it.nsUpload = nsUpload
it.context = context
}
if (it is PumpEnactResult) {
it.aapsLogger = aapsLogger
it.resourceHelper = resourceHelper
}
}
}
@Before
fun setup() {
PowerMockito.mockStatic(NSUpload::class.java)
`when`(resourceHelper.gs(R.string.ok)).thenReturn("OK")
`when`(resourceHelper.gs(R.string.alarm)).thenReturn("Alarm")
`when`(resourceHelper.gs(ArgumentMatchers.eq(R.string.alarm_message), ArgumentMatchers.anyString())).thenReturn("Alarm: %s")
sut = ActionAlarm(injector)
}
@Test fun friendlyNameTest() {
Assert.assertEquals(R.string.alarm, sut.friendlyName())
}
@Test fun shortDescriptionTest() {
sut.text = InputString(injector, "Asd")
Assert.assertEquals("Alarm: %s", sut.shortDescription())
}
@Test fun iconTest() {
Assert.assertEquals(R.drawable.ic_access_alarm_24dp, sut.icon())
}
@Test fun doActionTest() {
sut.doAction(object : Callback() {
override fun run() {
Assert.assertTrue(result.success)
}
})
PowerMockito.verifyStatic(NSUpload::class.java, Mockito.times(1))
}
@Test fun hasDialogTest() {
Assert.assertTrue(sut.hasDialog())
}
@Test fun toJSONTest() {
sut.text = InputString(injector, "Asd")
Assert.assertEquals("{\"data\":{\"text\":\"Asd\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.ActionAlarm\"}", sut.toJSON())
}
@Test fun fromJSONTest() {
sut.text = InputString(injector, "Asd")
sut.fromJSON("{\"text\":\"Asd\"}")
Assert.assertEquals("Asd", sut.text.value)
}
}