Widget: show sensitivity, clickable

This commit is contained in:
Milos Kozak 2022-04-04 11:26:23 +02:00
parent 2525cdaaf0
commit 5cae3bb8be
3 changed files with 60 additions and 1 deletions

View file

@ -1,11 +1,13 @@
package info.nightscout.androidaps package info.nightscout.androidaps
import android.app.PendingIntent
import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider import android.appwidget.AppWidgetProvider
import android.content.ComponentName import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Paint import android.graphics.Paint
import android.view.View
import android.widget.RemoteViews import android.widget.RemoteViews
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.data.ProfileSealed 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.toVisibility
import info.nightscout.androidaps.extensions.valueToUnitsString import info.nightscout.androidaps.extensions.valueToUnitsString
import info.nightscout.androidaps.interfaces.* 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.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
@ -22,6 +26,8 @@ import info.nightscout.androidaps.utils.TrendCalculator
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.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 java.util.*
import javax.inject.Inject import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
@ -41,6 +47,10 @@ class Widget : AppWidgetProvider() {
@Inject lateinit var iobCobCalculator: IobCobCalculator @Inject lateinit var iobCobCalculator: IobCobCalculator
@Inject lateinit var loop: Loop @Inject lateinit var loop: Loop
@Inject lateinit var config: Config @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?) { override fun onReceive(context: Context, intent: Intent?) {
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this) (context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
@ -66,12 +76,19 @@ class Widget : AppWidgetProvider() {
aapsLogger.debug(LTag.WIDGET, "updateAppWidget called") aapsLogger.debug(LTag.WIDGET, "updateAppWidget called")
val views = RemoteViews(context.packageName, R.layout.widget_layout) 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) updateBg(views)
updateTemporaryBasal(views) updateTemporaryBasal(views)
updateExtendedBolus(views) updateExtendedBolus(views)
updateIobCob(views) updateIobCob(views)
updateTemporaryTarget(views) updateTemporaryTarget(views)
updateProfile(views) updateProfile(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)
@ -192,6 +209,34 @@ class Widget : AppWidgetProvider() {
//views.setTextColor(R.id.active_profile, profileTextColor) //views.setTextColor(R.id.active_profile, profileTextColor)
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) { internal fun updateWidget(context: Context) {

View file

@ -1,6 +1,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
style="@style/Widget.AndroidAPS.AppWidget.Container" style="@style/Widget.AndroidAPS.AppWidget.Container"
android:id="@+id/widget_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
@ -337,6 +338,19 @@
android:textStyle="bold" android:textStyle="bold"
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
<TextView
android:id="@+id/variable_sensitivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-9dp"
android:gravity="center_horizontal"
android:paddingBottom="3dp"
android:text="n/a"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:visibility="visible"
tools:ignore="HardcodedText" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

View file

@ -6,6 +6,6 @@
android:minWidth="250dp" android:minWidth="250dp"
android:minHeight="110dp" android:minHeight="110dp"
android:previewImage="@drawable/widget_preview" android:previewImage="@drawable/widget_preview"
android:resizeMode="horizontal" android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000" android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" /> android:widgetCategory="home_screen" />