Widget: resolve db access in main thread

This commit is contained in:
Milos Kozak 2022-06-21 11:05:31 +02:00
parent bd979d24e5
commit 5a0fc8da74

View file

@ -8,6 +8,8 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.graphics.Paint import android.graphics.Paint
import android.os.Handler
import android.os.HandlerThread
import android.view.View import android.view.View
import android.widget.RemoteViews import android.widget.RemoteViews
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
@ -26,7 +28,6 @@ import info.nightscout.androidaps.plugins.general.overview.OverviewData
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.TrendCalculator import info.nightscout.androidaps.utils.TrendCalculator
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
@ -53,6 +54,7 @@ class Widget : AppWidgetProvider() {
@Inject lateinit var sp: SP @Inject lateinit var sp: SP
@Inject lateinit var constraintChecker: ConstraintChecker @Inject lateinit var constraintChecker: ConstraintChecker
private var handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
private val intentAction = "OpenApp" private val intentAction = "OpenApp"
override fun onReceive(context: Context, intent: Intent?) { override fun onReceive(context: Context, intent: Intent?) {
@ -88,6 +90,7 @@ class Widget : AppWidgetProvider() {
views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent) views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent)
views.setInt(R.id.widget_layout, "setBackgroundColor", Color.argb(alpha, 0, 0, 0)) views.setInt(R.id.widget_layout, "setBackgroundColor", Color.argb(alpha, 0, 0, 0))
handler.post {
updateBg(views) updateBg(views)
updateTemporaryBasal(views) updateTemporaryBasal(views)
updateExtendedBolus(views) updateExtendedBolus(views)
@ -95,10 +98,10 @@ class Widget : AppWidgetProvider() {
updateTemporaryTarget(views) updateTemporaryTarget(views)
updateProfile(views) updateProfile(views)
updateSensitivity(views) updateSensitivity(views)
// Instruct the widget manager to update the widget // Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views) appWidgetManager.updateAppWidget(appWidgetId, views)
} }
}
private fun updateBg(views: RemoteViews) { private fun updateBg(views: RemoteViews) {
val units = profileFunction.getUnits() val units = profileFunction.getUnits()