From e5a9cc5a109ca46907006463133a69a1e6ceef8b Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sat, 18 Feb 2023 15:50:07 +0100 Subject: [PATCH] optimize widget calls --- .../info/nightscout/androidaps/MainApp.kt | 2 +- .../implementations/UiInteractionImpl.kt | 4 ++-- .../androidaps/receivers/KeepAliveWorker.kt | 2 -- .../workflow/CalculationWorkflowImpl.kt | 6 +++++- .../nightscout/interfaces/ui/UiInteraction.kt | 2 +- .../implementation/db/CompatDBHelper.kt | 3 +-- .../java/info/nightscout/ui/widget/Widget.kt | 6 +++--- .../ui/widget/WidgetConfigureActivity.kt | 2 +- .../nightscout/workflow/UpdateWidgetWorker.kt | 21 +++++++++++++++++++ .../nightscout/workflow/di/WorkflowModule.kt | 2 ++ 10 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 workflow/src/main/java/info/nightscout/workflow/UpdateWidgetWorker.kt diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.kt b/app/src/main/java/info/nightscout/androidaps/MainApp.kt index 147a60cf94..c3c7c58292 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.kt +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.kt @@ -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) } diff --git a/app/src/main/java/info/nightscout/androidaps/implementations/UiInteractionImpl.kt b/app/src/main/java/info/nightscout/androidaps/implementations/UiInteractionImpl.kt index 23fb25308c..d29579d76c 100644 --- a/app/src/main/java/info/nightscout/androidaps/implementations/UiInteractionImpl.kt +++ b/app/src/main/java/info/nightscout/androidaps/implementations/UiInteractionImpl.kt @@ -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?) { diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveWorker.kt b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveWorker.kt index c5873ee79b..2127470b30 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveWorker.kt @@ -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() diff --git a/app/src/main/java/info/nightscout/androidaps/workflow/CalculationWorkflowImpl.kt b/app/src/main/java/info/nightscout/androidaps/workflow/CalculationWorkflowImpl.kt index f19bde1416..475e4e5f9b 100644 --- a/app/src/main/java/info/nightscout/androidaps/workflow/CalculationWorkflowImpl.kt +++ b/app/src/main/java/info/nightscout/androidaps/workflow/CalculationWorkflowImpl.kt @@ -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) diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/ui/UiInteraction.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/ui/UiInteraction.kt index 2081e83ca1..4cfd0dfad8 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/ui/UiInteraction.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/ui/UiInteraction.kt @@ -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) diff --git a/implementation/src/main/java/info/nightscout/implementation/db/CompatDBHelper.kt b/implementation/src/main/java/info/nightscout/implementation/db/CompatDBHelper.kt index 56515fb76f..3c0866c536 100644 --- a/implementation/src/main/java/info/nightscout/implementation/db/CompatDBHelper.kt +++ b/implementation/src/main/java/info/nightscout/implementation/db/CompatDBHelper.kt @@ -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().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().minOfOrNull { gv -> gv.timestamp }?.let { timestamp -> diff --git a/ui/src/main/java/info/nightscout/ui/widget/Widget.kt b/ui/src/main/java/info/nightscout/ui/widget/Widget.kt index b5ff219736..aa5cccfc8f 100644 --- a/ui/src/main/java/info/nightscout/ui/widget/Widget.kt +++ b/ui/src/main/java/info/nightscout/ui/widget/Widget.kt @@ -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) diff --git a/ui/src/main/java/info/nightscout/ui/widget/WidgetConfigureActivity.kt b/ui/src/main/java/info/nightscout/ui/widget/WidgetConfigureActivity.kt index 4b93788fea..6fd8a1aa82 100644 --- a/ui/src/main/java/info/nightscout/ui/widget/WidgetConfigureActivity.kt +++ b/ui/src/main/java/info/nightscout/ui/widget/WidgetConfigureActivity.kt @@ -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") } }) diff --git a/workflow/src/main/java/info/nightscout/workflow/UpdateWidgetWorker.kt b/workflow/src/main/java/info/nightscout/workflow/UpdateWidgetWorker.kt new file mode 100644 index 0000000000..e22d4d5f78 --- /dev/null +++ b/workflow/src/main/java/info/nightscout/workflow/UpdateWidgetWorker.kt @@ -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() + } +} \ No newline at end of file diff --git a/workflow/src/main/java/info/nightscout/workflow/di/WorkflowModule.kt b/workflow/src/main/java/info/nightscout/workflow/di/WorkflowModule.kt index fa7ce81074..1832c73a9a 100644 --- a/workflow/src/main/java/info/nightscout/workflow/di/WorkflowModule.kt +++ b/workflow/src/main/java/info/nightscout/workflow/di/WorkflowModule.kt @@ -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 } \ No newline at end of file