optimize widget calls

This commit is contained in:
Milos Kozak 2023-02-18 15:50:07 +01:00
parent eee6fca854
commit e5a9cc5a10
10 changed files with 37 additions and 13 deletions

View file

@ -151,7 +151,7 @@ class MainApp : DaggerApplication() {
// schedule widget update
refreshWidget = Runnable {
handler.postDelayed(refreshWidget, 60000)
Widget.updateWidget(this)
Widget.updateWidget(this, "ScheduleEveryMin")
}
handler.postDelayed(refreshWidget, 60000)
}

View file

@ -70,8 +70,8 @@ class UiInteractionImpl @Inject constructor(
context.startActivity(i)
}
override fun updateWidget(context: Context) {
Widget.updateWidget(context)
override fun updateWidget(context: Context, from: String) {
Widget.updateWidget(context, from)
}
override fun runWizardDialog(fragmentManager: FragmentManager, carbs: Int?, name: String?) {

View file

@ -33,7 +33,6 @@ import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil
import info.nightscout.shared.utils.T
import info.nightscout.ui.widget.Widget
import kotlinx.coroutines.Dispatchers
import java.util.concurrent.TimeUnit
import javax.inject.Inject
@ -108,7 +107,6 @@ class KeepAliveWorker(
}
lastRun = dateUtil.now()
Widget.updateWidget(context)
localAlertUtils.shortenSnoozeInterval()
localAlertUtils.checkStaleBGAlert()
checkPump()

View file

@ -5,7 +5,6 @@ import android.os.SystemClock
import androidx.work.Data
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkContinuation
import androidx.work.WorkInfo
import androidx.work.WorkManager
import dagger.android.HasAndroidInjector
@ -44,6 +43,7 @@ import info.nightscout.workflow.PrepareTemporaryTargetDataWorker
import info.nightscout.workflow.PrepareTreatmentsDataWorker
import info.nightscout.workflow.UpdateGraphWorker
import info.nightscout.workflow.UpdateIobCobSensWorker
import info.nightscout.workflow.UpdateWidgetWorker
import info.nightscout.workflow.iob.IobCobOref1Worker
import info.nightscout.workflow.iob.IobCobOrefWorker
import io.reactivex.rxjava3.disposables.CompositeDisposable
@ -209,6 +209,10 @@ class CalculationWorkflowImpl @Inject constructor(
.setInputData(dataWorkerStorage.storeInputData(InvokeLoopWorker.InvokeLoopData(cause)))
.build()
)
.then(
job == MAIN_CALCULATION,
OneTimeWorkRequest.Builder(UpdateWidgetWorker::class.java).build()
)
.then(
job == MAIN_CALCULATION,
OneTimeWorkRequest.Builder(PreparePredictionsWorker::class.java)

View file

@ -33,7 +33,7 @@ interface UiInteraction {
*/
fun runAlarm(status: String, title: String, @RawRes soundId: Int = 0)
fun updateWidget(context: Context)
fun updateWidget(context: Context, from: String)
fun runWizardDialog(fragmentManager: FragmentManager, carbs: Int? = null, name: String? = null)
fun runLoopDialog(fragmentManager: FragmentManager, showOkCancel: Int)

View file

@ -45,7 +45,7 @@ class CompatDBHelper @Inject constructor(
.changeObservable()
.doOnSubscribe {
rxBus.send(EventNewBG(null))
uiInteraction.updateWidget(context)
uiInteraction.updateWidget(context, "OnStart")
}
.subscribe {
/**
@ -58,7 +58,6 @@ class CompatDBHelper @Inject constructor(
it.filterIsInstance<GlucoseValue>().maxByOrNull { gv -> gv.timestamp }?.let { gv ->
aapsLogger.debug(LTag.DATABASE, "Firing EventNewBg $gv")
rxBus.send(EventNewBG(gv.timestamp))
uiInteraction.updateWidget(context)
newestGlucoseValue = gv
}
it.filterIsInstance<GlucoseValue>().minOfOrNull { gv -> gv.timestamp }?.let { timestamp ->

View file

@ -69,9 +69,10 @@ class Widget : AppWidgetProvider() {
// many threads were created. Making handler static resolve this issue
private var handler = Handler(HandlerThread(this::class.simpleName + "Handler").also { it.start() }.looper)
fun updateWidget(context: Context) {
fun updateWidget(context: Context, from: String) {
context.sendBroadcast(Intent().also {
it.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, AppWidgetManager.getInstance(context)?.getAppWidgetIds(ComponentName(context, Widget::class.java)))
it.putExtra("from", from)
it.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
})
}
@ -80,6 +81,7 @@ class Widget : AppWidgetProvider() {
override fun onReceive(context: Context, intent: Intent?) {
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
aapsLogger.debug(LTag.WIDGET, "onReceive ${intent?.extras?.getString("from")}")
super.onReceive(context, intent)
}
@ -99,8 +101,6 @@ class Widget : AppWidgetProvider() {
}
private fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
aapsLogger.debug(LTag.WIDGET, "updateAppWidget called")
val views = RemoteViews(context.packageName, R.layout.widget_layout)
val alpha = sp.getInt(WidgetConfigureActivity.PREF_PREFIX_KEY + appWidgetId, WidgetConfigureActivity.DEFAULT_OPACITY)

View file

@ -50,7 +50,7 @@ class WidgetConfigureActivity : DaggerActivity() {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
value = progress
saveTitlePref(appWidgetId, value)
Widget.updateWidget(this@WidgetConfigureActivity)
Widget.updateWidget(this@WidgetConfigureActivity, "WidgetConfigure")
}
})

View file

@ -0,0 +1,21 @@
package info.nightscout.workflow
import android.content.Context
import androidx.work.WorkerParameters
import info.nightscout.core.utils.worker.LoggingWorker
import info.nightscout.interfaces.ui.UiInteraction
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject
class UpdateWidgetWorker(
private val context: Context,
params: WorkerParameters
) : LoggingWorker(context, params, Dispatchers.Default) {
@Inject lateinit var uiInteraction: UiInteraction
override suspend fun doWorkAndLog(): Result {
uiInteraction.updateWidget(context, "WorkFlow")
return Result.success()
}
}

View file

@ -14,6 +14,7 @@ import info.nightscout.workflow.PrepareTemporaryTargetDataWorker
import info.nightscout.workflow.PrepareTreatmentsDataWorker
import info.nightscout.workflow.UpdateGraphWorker
import info.nightscout.workflow.UpdateIobCobSensWorker
import info.nightscout.workflow.UpdateWidgetWorker
import info.nightscout.workflow.iob.IobCobOref1Worker
import info.nightscout.workflow.iob.IobCobOrefWorker
@ -34,5 +35,6 @@ abstract class WorkflowModule {
@ContributesAndroidInjector abstract fun prepareBucketedDataWorkerInjector(): PrepareBucketedDataWorker
@ContributesAndroidInjector abstract fun loadBgDataWorkerInjector(): LoadBgDataWorker
@ContributesAndroidInjector abstract fun invokeLoopWorkerInjector(): InvokeLoopWorker
@ContributesAndroidInjector abstract fun updateWidgetWorkerInjector(): UpdateWidgetWorker
@ContributesAndroidInjector abstract fun dummyWorkerInjector(): DummyWorker
}