AndroidAPS/app/src/main/java/info/nightscout/androidaps/receivers/TimeDateOrTZChangeReceiver.kt

76 lines
3.1 KiB
Kotlin
Raw Normal View History

2020-03-16 21:40:29 +01:00
package info.nightscout.androidaps.receivers
import android.content.Context
import android.content.Intent
import com.google.gson.Gson
2020-03-18 00:22:21 +01:00
import dagger.android.DaggerBroadcastReceiver
2020-03-16 21:40:29 +01:00
import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.interfaces.PumpInterface
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.utils.TimeChangeType
import java.util.*
2020-03-16 21:40:29 +01:00
import javax.inject.Inject
2020-03-18 00:22:21 +01:00
class TimeDateOrTZChangeReceiver : DaggerBroadcastReceiver() {
2020-03-16 21:40:29 +01:00
@Inject lateinit var aapsLogger: AAPSLogger
@Inject lateinit var activePlugin: ActivePluginProvider
val gson: Gson = Gson()
private var isDST = false
init {
isDST = calculateDST()
}
private fun calculateDST(): Boolean {
val timeZone = TimeZone.getDefault()
val nowDate = Date()
return if (timeZone.useDaylightTime()) {
timeZone.inDaylightTime(nowDate)
} else {
false
}
}
2020-03-16 21:40:29 +01:00
override fun onReceive(context: Context, intent: Intent) {
2020-03-18 00:22:21 +01:00
super.onReceive(context, intent)
2020-03-16 21:40:29 +01:00
val action = intent.action
2020-03-31 09:48:46 +02:00
val activePump: PumpInterface = activePlugin.activePump
aapsLogger.debug(LTag.PUMP,"TimeDateOrTZChangeReceiver::Date, Time and/or TimeZone changed. [action={}]", action)
2020-07-07 17:16:22 +02:00
try {
aapsLogger.debug(LTag.PUMP, "TimeDateOrTZChangeReceiver::Intent::{}", gson.toJson(intent))
} catch (ignored : Exception) {}
2020-07-07 17:16:22 +02:00
when {
action == null -> {
aapsLogger.error(LTag.PUMP,"TimeDateOrTZChangeReceiver::Action is null. Exiting.")
}
Intent.ACTION_TIMEZONE_CHANGED == action -> {
aapsLogger.info(LTag.PUMP,"TimeDateOrTZChangeReceiver::Timezone changed. Notifying pump driver.")
activePump.timezoneOrDSTChanged(TimeChangeType.TimezoneChange)
}
Intent.ACTION_TIME_CHANGED == action -> {
val currentDst = calculateDST()
if (currentDst == isDST) {
aapsLogger.info(LTag.PUMP,"TimeDateOrTZChangeReceiver::Time changed (manual). Notifying pump driver.")
activePump.timezoneOrDSTChanged(TimeChangeType.ManualTimeChange)
} else {
2020-07-07 17:16:22 +02:00
if (currentDst) {
aapsLogger.info(LTag.PUMP,"TimeDateOrTZChangeReceiver::DST started. Notifying pump driver.")
activePump.timezoneOrDSTChanged(TimeChangeType.DST_Started)
} else {
aapsLogger.info(LTag.PUMP,"TimeDateOrTZChangeReceiver::DST ended. Notifying pump driver.")
activePump.timezoneOrDSTChanged(TimeChangeType.DST_Ended)
}
}
2020-07-07 17:16:22 +02:00
isDST = currentDst
}
else -> {
aapsLogger.error(LTag.PUMP,"TimeDateOrTZChangeReceiver::Unknown action received [name={}]. Exiting.", action)
}
2020-03-16 21:40:29 +01:00
}
}
}