RandomBg: add wakelock

This commit is contained in:
Milos Kozak 2023-10-15 21:15:34 +02:00
parent f0d21f0e7a
commit ec712f6be1

View file

@ -1,7 +1,10 @@
package app.aaps.plugins.source package app.aaps.plugins.source
import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler import android.os.Handler
import android.os.HandlerThread import android.os.HandlerThread
import android.os.PowerManager
import android.os.SystemClock import android.os.SystemClock
import app.aaps.core.interfaces.configuration.Config import app.aaps.core.interfaces.configuration.Config
import app.aaps.core.interfaces.logging.AAPSLogger import app.aaps.core.interfaces.logging.AAPSLogger
@ -31,6 +34,7 @@ import kotlin.math.sin
@Singleton @Singleton
class RandomBgPlugin @Inject constructor( class RandomBgPlugin @Inject constructor(
private val context: Context,
injector: HasAndroidInjector, injector: HasAndroidInjector,
rh: ResourceHelper, rh: ResourceHelper,
aapsLogger: AAPSLogger, aapsLogger: AAPSLogger,
@ -51,6 +55,7 @@ class RandomBgPlugin @Inject constructor(
private val handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper) private val handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
private lateinit var refreshLoop: Runnable private lateinit var refreshLoop: Runnable
private var wakeLock: PowerManager.WakeLock? = null
companion object { companion object {
@ -71,6 +76,7 @@ class RandomBgPlugin @Inject constructor(
override fun advancedFilteringSupported(): Boolean = true override fun advancedFilteringSupported(): Boolean = true
@SuppressLint("WakelockTimeout")
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
val cal = GregorianCalendar() val cal = GregorianCalendar()
@ -79,11 +85,14 @@ class RandomBgPlugin @Inject constructor(
cal[Calendar.MINUTE] -= cal[Calendar.MINUTE] % 5 cal[Calendar.MINUTE] -= cal[Calendar.MINUTE] % 5
handler.postAtTime(refreshLoop, SystemClock.uptimeMillis() + cal.timeInMillis + T.mins(5).msecs() + 1000 - System.currentTimeMillis()) handler.postAtTime(refreshLoop, SystemClock.uptimeMillis() + cal.timeInMillis + T.mins(5).msecs() + 1000 - System.currentTimeMillis())
disposable.clear() disposable.clear()
wakeLock = (context.getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AAPS:RandomBgPlugin")
wakeLock?.acquire()
} }
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
handler.removeCallbacks(refreshLoop) handler.removeCallbacks(refreshLoop)
if (wakeLock?.isHeld == true) wakeLock?.release()
} }
override fun specialEnableCondition(): Boolean { override fun specialEnableCondition(): Boolean {
@ -106,7 +115,7 @@ class RandomBgPlugin @Inject constructor(
value = bgMgdl, value = bgMgdl,
raw = 0.0, raw = 0.0,
noise = null, noise = null,
trendArrow = GlucoseValue.TrendArrow.values().toList().shuffled().first(), trendArrow = GlucoseValue.TrendArrow.entries.shuffled().first(),
sourceSensor = GlucoseValue.SourceSensor.RANDOM sourceSensor = GlucoseValue.SourceSensor.RANDOM
) )
disposable += repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, emptyList(), null)) disposable += repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, emptyList(), null))