2020-03-16 21:40:29 +01:00
|
|
|
package info.nightscout.androidaps.receivers
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
2020-04-25 22:10:58 +02:00
|
|
|
import com.google.gson.Gson
|
2020-03-18 00:22:21 +01:00
|
|
|
import dagger.android.DaggerBroadcastReceiver
|
2021-04-14 00:45:30 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ActivePlugin
|
2021-04-14 19:36:13 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.Pump
|
2021-12-10 15:19:19 +01:00
|
|
|
import info.nightscout.shared.logging.AAPSLogger
|
|
|
|
import info.nightscout.shared.logging.BundleLogger
|
|
|
|
import info.nightscout.shared.logging.LTag
|
2020-04-25 22:10:58 +02:00
|
|
|
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
|
2021-04-14 00:45:30 +02:00
|
|
|
@Inject lateinit var activePlugin: ActivePlugin
|
2020-04-26 16:17:19 +02:00
|
|
|
val gson: Gson = Gson()
|
2020-04-25 22:10:58 +02:00
|
|
|
|
|
|
|
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
|
2021-04-14 19:36:13 +02:00
|
|
|
val activePump: Pump = activePlugin.activePump
|
2020-04-25 22:10:58 +02:00
|
|
|
|
2020-07-26 11:11:14 +02:00
|
|
|
aapsLogger.debug(LTag.PUMP, "TimeDateOrTZChangeReceiver::Date, Time and/or TimeZone changed. [action={}]", action)
|
|
|
|
aapsLogger.debug(LTag.PUMP, "TimeDateOrTZChangeReceiver::Intent::{}", BundleLogger.log(intent.extras))
|
2020-04-25 22:10:58 +02:00
|
|
|
|
2020-07-07 17:16:22 +02:00
|
|
|
when {
|
|
|
|
action == null -> {
|
2020-07-26 11:11:14 +02:00
|
|
|
aapsLogger.error(LTag.PUMP, "TimeDateOrTZChangeReceiver::Action is null. Exiting.")
|
2020-07-07 17:16:22 +02:00
|
|
|
}
|
2020-07-26 11:11:14 +02:00
|
|
|
|
2020-07-07 17:16:22 +02:00
|
|
|
Intent.ACTION_TIMEZONE_CHANGED == action -> {
|
2020-07-26 11:11:14 +02:00
|
|
|
aapsLogger.info(LTag.PUMP, "TimeDateOrTZChangeReceiver::Timezone changed. Notifying pump driver.")
|
2021-01-07 03:56:54 +01:00
|
|
|
activePump.timezoneOrDSTChanged(TimeChangeType.TimezoneChanged)
|
2020-07-07 17:16:22 +02:00
|
|
|
}
|
2020-07-26 11:11:14 +02:00
|
|
|
|
2020-07-07 17:16:22 +02:00
|
|
|
Intent.ACTION_TIME_CHANGED == action -> {
|
|
|
|
val currentDst = calculateDST()
|
|
|
|
if (currentDst == isDST) {
|
2020-07-26 11:11:14 +02:00
|
|
|
aapsLogger.info(LTag.PUMP, "TimeDateOrTZChangeReceiver::Time changed (manual). Notifying pump driver.")
|
2021-01-07 03:56:54 +01:00
|
|
|
activePump.timezoneOrDSTChanged(TimeChangeType.TimeChanged)
|
2020-04-25 22:10:58 +02:00
|
|
|
} else {
|
2020-07-07 17:16:22 +02:00
|
|
|
if (currentDst) {
|
2020-07-26 11:11:14 +02:00
|
|
|
aapsLogger.info(LTag.PUMP, "TimeDateOrTZChangeReceiver::DST started. Notifying pump driver.")
|
2021-12-13 16:55:47 +01:00
|
|
|
activePump.timezoneOrDSTChanged(TimeChangeType.DSTStarted)
|
2020-07-07 17:16:22 +02:00
|
|
|
} else {
|
2020-07-26 11:11:14 +02:00
|
|
|
aapsLogger.info(LTag.PUMP, "TimeDateOrTZChangeReceiver::DST ended. Notifying pump driver.")
|
2021-12-13 16:55:47 +01:00
|
|
|
activePump.timezoneOrDSTChanged(TimeChangeType.DSTEnded)
|
2020-07-07 17:16:22 +02:00
|
|
|
}
|
2020-04-25 22:10:58 +02:00
|
|
|
}
|
2020-07-07 17:16:22 +02:00
|
|
|
isDST = currentDst
|
|
|
|
}
|
2020-07-26 11:11:14 +02:00
|
|
|
|
2020-07-07 17:16:22 +02:00
|
|
|
else -> {
|
2020-07-26 11:11:14 +02:00
|
|
|
aapsLogger.error(LTag.PUMP, "TimeDateOrTZChangeReceiver::Unknown action received [name={}]. Exiting.", action)
|
2020-04-25 22:10:58 +02:00
|
|
|
}
|
2020-03-16 21:40:29 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 22:10:58 +02:00
|
|
|
}
|