From 5cae3bb8be3d29b740e18ed622ec7555522b1754 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 4 Apr 2022 11:26:23 +0200 Subject: [PATCH] Widget: show sensitivity, clickable --- .../java/info/nightscout/androidaps/Widget.kt | 45 +++++++++++++++++++ app/src/main/res/layout/widget_layout.xml | 14 ++++++ app/src/main/res/xml/widget_info.xml | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/info/nightscout/androidaps/Widget.kt b/app/src/main/java/info/nightscout/androidaps/Widget.kt index 634a477da4..2d8268626e 100644 --- a/app/src/main/java/info/nightscout/androidaps/Widget.kt +++ b/app/src/main/java/info/nightscout/androidaps/Widget.kt @@ -1,11 +1,13 @@ package info.nightscout.androidaps +import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.ComponentName import android.content.Context import android.content.Intent import android.graphics.Paint +import android.view.View import android.widget.RemoteViews import dagger.android.HasAndroidInjector import info.nightscout.androidaps.data.ProfileSealed @@ -15,6 +17,8 @@ import info.nightscout.androidaps.extensions.isInProgress import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.androidaps.extensions.valueToUnitsString import info.nightscout.androidaps.interfaces.* +import info.nightscout.androidaps.plugins.aps.openAPSSMB.DetermineBasalResultSMB +import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker import info.nightscout.androidaps.plugins.general.overview.OverviewData import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider import info.nightscout.androidaps.utils.DateUtil @@ -22,6 +26,8 @@ import info.nightscout.androidaps.utils.TrendCalculator import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.LTag +import info.nightscout.shared.sharedPreferences.SP +import java.util.* import javax.inject.Inject import kotlin.math.abs @@ -41,6 +47,10 @@ class Widget : AppWidgetProvider() { @Inject lateinit var iobCobCalculator: IobCobCalculator @Inject lateinit var loop: Loop @Inject lateinit var config: Config + @Inject lateinit var sp: SP + @Inject lateinit var constraintChecker: ConstraintChecker + + private val intentAction = "OpenApp" override fun onReceive(context: Context, intent: Intent?) { (context.applicationContext as HasAndroidInjector).androidInjector().inject(this) @@ -66,12 +76,19 @@ class Widget : AppWidgetProvider() { aapsLogger.debug(LTag.WIDGET, "updateAppWidget called") val views = RemoteViews(context.packageName, R.layout.widget_layout) + // Create an Intent to launch MainActivity when clicked + val intent = Intent(context, MainActivity::class.java).also { it.action = intentAction } + val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) + // Widgets allow click handlers to only launch pending intents + views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent) + updateBg(views) updateTemporaryBasal(views) updateExtendedBolus(views) updateIobCob(views) updateTemporaryTarget(views) updateProfile(views) + updateSensitivity(views) // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views) @@ -192,6 +209,34 @@ class Widget : AppWidgetProvider() { //views.setTextColor(R.id.active_profile, profileTextColor) views.setTextColor(R.id.active_profile, profileTextColor) } + + fun updateSensitivity(views: RemoteViews) { + if (sp.getBoolean(R.string.key_openapsama_useautosens, false) && constraintChecker.isAutosensModeEnabled().value()) + views.setImageViewResource(R.id.sensitivity_icon, R.drawable.ic_swap_vert_black_48dp_green) + else + views.setImageViewResource(R.id.sensitivity_icon, R.drawable.ic_x_swap_vert) + views.setTextViewText(R.id.sensitivity, overviewData.lastAutosensData?.let { autosensData -> + String.format(Locale.ENGLISH, "%.0f%%", autosensData.autosensResult.ratio * 100) + } ?: "") + + // Show variable sensitivity + val request = loop.lastRun?.request + if (request is DetermineBasalResultSMB) { + val isfMgdl = profileFunction.getProfile()?.getIsfMgdl() + val variableSens = request.variableSens + if (variableSens != isfMgdl && variableSens != null && isfMgdl != null) { + views.setTextViewText( + R.id.variable_sensitivity, + String.format( + Locale.getDefault(), "%1$.1f→%2$.1f", + Profile.toUnits(isfMgdl, isfMgdl * Constants.MGDL_TO_MMOLL, profileFunction.getUnits()), + Profile.toUnits(variableSens, variableSens * Constants.MGDL_TO_MMOLL, profileFunction.getUnits()) + ) + ) + views.setViewVisibility(R.id.variable_sensitivity, View.VISIBLE) + } else views.setViewVisibility(R.id.variable_sensitivity, View.GONE) + } else views.setViewVisibility(R.id.variable_sensitivity, View.GONE) + } } internal fun updateWidget(context: Context) { diff --git a/app/src/main/res/layout/widget_layout.xml b/app/src/main/res/layout/widget_layout.xml index 3403ed4d64..e12a005ccc 100644 --- a/app/src/main/res/layout/widget_layout.xml +++ b/app/src/main/res/layout/widget_layout.xml @@ -1,6 +1,7 @@ + + diff --git a/app/src/main/res/xml/widget_info.xml b/app/src/main/res/xml/widget_info.xml index d371d3e934..819118751e 100644 --- a/app/src/main/res/xml/widget_info.xml +++ b/app/src/main/res/xml/widget_info.xml @@ -6,6 +6,6 @@ android:minWidth="250dp" android:minHeight="110dp" android:previewImage="@drawable/widget_preview" - android:resizeMode="horizontal" + android:resizeMode="horizontal|vertical" android:updatePeriodMillis="86400000" android:widgetCategory="home_screen" /> \ No newline at end of file