Widget: user defined opacity

This commit is contained in:
Milos Kozak 2022-04-06 00:23:01 +02:00
parent 6711276732
commit 6cc5e285ac
16 changed files with 146 additions and 21 deletions

View file

@ -45,8 +45,17 @@
android:roundIcon="${appIconRound}" android:roundIcon="${appIconRound}"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme.Launcher" > android:theme="@style/AppTheme.Launcher" >
<activity
android:name=".widget.WidgetConfigureActivity"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<receiver <receiver
android:name=".Widget" android:name=".widget.Widget"
android:exported="true" > android:exported="true" >
<intent-filter> <intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

View file

@ -41,6 +41,7 @@ import info.nightscout.androidaps.utils.LocalAlertUtils
import info.nightscout.androidaps.utils.ProcessLifecycleListener import info.nightscout.androidaps.utils.ProcessLifecycleListener
import info.nightscout.androidaps.utils.buildHelper.BuildHelper import info.nightscout.androidaps.utils.buildHelper.BuildHelper
import info.nightscout.androidaps.utils.locale.LocaleHelper import info.nightscout.androidaps.utils.locale.LocaleHelper
import info.nightscout.androidaps.widget.updateWidget
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

View file

@ -2,8 +2,9 @@ package info.nightscout.androidaps.di
import dagger.Module import dagger.Module
import dagger.android.ContributesAndroidInjector import dagger.android.ContributesAndroidInjector
import info.nightscout.androidaps.Widget import info.nightscout.androidaps.widget.WidgetConfigureActivity
import info.nightscout.androidaps.skins.SkinListPreference import info.nightscout.androidaps.skins.SkinListPreference
import info.nightscout.androidaps.widget.Widget
@Module @Module
@Suppress("unused") @Suppress("unused")
@ -11,4 +12,6 @@ abstract class UIModule {
@ContributesAndroidInjector abstract fun skinListPreferenceInjector(): SkinListPreference @ContributesAndroidInjector abstract fun skinListPreferenceInjector(): SkinListPreference
@ContributesAndroidInjector abstract fun aapsWidgetInjector(): Widget @ContributesAndroidInjector abstract fun aapsWidgetInjector(): Widget
@ContributesAndroidInjector abstract fun contributesWidgetConfigureActivity(): WidgetConfigureActivity
} }

View file

@ -15,12 +15,12 @@ import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.configBuilder.RunningConfiguration import info.nightscout.androidaps.plugins.configBuilder.RunningConfiguration
import info.nightscout.androidaps.plugins.general.maintenance.MaintenancePlugin import info.nightscout.androidaps.plugins.general.maintenance.MaintenancePlugin
import info.nightscout.androidaps.queue.commands.Command import info.nightscout.androidaps.queue.commands.Command
import info.nightscout.androidaps.updateWidget
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.LocalAlertUtils import info.nightscout.androidaps.utils.LocalAlertUtils
import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.widget.updateWidget
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 java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps package info.nightscout.androidaps.widget
import android.app.PendingIntent import android.app.PendingIntent
import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetManager
@ -6,10 +6,14 @@ 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.Color
import android.graphics.Paint import android.graphics.Paint
import android.view.View import android.view.View
import android.widget.RemoteViews import android.widget.RemoteViews
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.MainActivity
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.ProfileSealed import info.nightscout.androidaps.data.ProfileSealed
import info.nightscout.androidaps.database.interfaces.end import info.nightscout.androidaps.database.interfaces.end
import info.nightscout.androidaps.extensions.directionToIcon import info.nightscout.androidaps.extensions.directionToIcon
@ -73,13 +77,16 @@ class Widget : AppWidgetProvider() {
private fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) { private fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
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)
val alpha = sp.getInt(WidgetConfigureActivity.PREF_PREFIX_KEY + appWidgetId, WidgetConfigureActivity.DEFAULT_OPACITY)
// Create an Intent to launch MainActivity when clicked // Create an Intent to launch MainActivity when clicked
val intent = Intent(context, MainActivity::class.java).also { it.action = intentAction } val intent = Intent(context, MainActivity::class.java).also { it.action = intentAction }
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
// Widgets allow click handlers to only launch pending intents // Widgets allow click handlers to only launch pending intents
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))
updateBg(views) updateBg(views)
updateTemporaryBasal(views) updateTemporaryBasal(views)
@ -104,11 +111,13 @@ class Widget : AppWidgetProvider() {
} }
) )
views.setImageViewResource(R.id.arrow, trendCalculator.getTrendArrow(overviewData.lastBg).directionToIcon()) views.setImageViewResource(R.id.arrow, trendCalculator.getTrendArrow(overviewData.lastBg).directionToIcon())
views.setInt(R.id.arrow, "setColorFilter", when { views.setInt(
overviewData.isLow -> rh.gc(R.color.low) R.id.arrow, "setColorFilter", when {
overviewData.isHigh -> rh.gc(R.color.high) overviewData.isLow -> rh.gc(R.color.low)
else -> rh.gc(R.color.inrange) overviewData.isHigh -> rh.gc(R.color.high)
}) else -> rh.gc(R.color.inrange)
}
)
val glucoseStatus = glucoseStatusProvider.glucoseStatusData val glucoseStatus = glucoseStatusProvider.glucoseStatusData
if (glucoseStatus != null) { if (glucoseStatus != null) {

View file

@ -0,0 +1,78 @@
package info.nightscout.androidaps.widget
import android.appwidget.AppWidgetManager
import android.content.Intent
import android.os.Bundle
import android.widget.SeekBar
import dagger.android.DaggerActivity
import info.nightscout.androidaps.databinding.WidgetConfigureBinding
import info.nightscout.shared.sharedPreferences.SP
import javax.inject.Inject
/**
* The configuration screen for the [Widget] AppWidget.
*/
class WidgetConfigureActivity : DaggerActivity() {
@Inject lateinit var sp: SP
companion object {
@Suppress("PrivatePropertyName")
const val PREF_PREFIX_KEY = "appwidget_"
const val DEFAULT_OPACITY = 25
}
private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
private var value = 0
private lateinit var binding: WidgetConfigureBinding
public override fun onCreate(icicle: Bundle?) {
super.onCreate(icicle)
// Set the result to CANCELED. This will cause the widget host to cancel
// out of the widget placement if the user presses the back button.
setResult(RESULT_CANCELED)
binding = WidgetConfigureBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onStopTrackingTouch(seekBar: SeekBar) {
// Make sure we pass back the original appWidgetId
val resultValue = Intent()
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
setResult(RESULT_OK, resultValue)
finish()
}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
value = progress
saveTitlePref(appWidgetId, value)
updateWidget(this@WidgetConfigureActivity)
}
})
// Find the widget id from the intent.
appWidgetId = intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID) ?: AppWidgetManager.INVALID_APPWIDGET_ID
// If this activity was started with an intent without an app widget ID, finish with an error.
if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish()
return
}
binding.seekBar.progress = loadTitlePref(appWidgetId)
}
// Write the prefix to the SharedPreferences object for this widget
fun saveTitlePref(appWidgetId: Int, value: Int) {
sp.putInt(PREF_PREFIX_KEY + appWidgetId, value)
}
// Read the prefix from the SharedPreferences object for this widget.
// If there is no preference saved, get the default from a resource
private fun loadTitlePref(appWidgetId: Int): Int = sp.getInt(PREF_PREFIX_KEY + appWidgetId, 25)
}

View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#11000000" />
<corners android:radius="16dp" />
</shape>

View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:labelFor="@id/seekBar"
android:text="@string/configure" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="256" />
</LinearLayout>

View file

@ -5,7 +5,7 @@
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"
android:background="@drawable/widget_background" android:background="@color/black"
android:theme="@style/AppTheme.Launcher.AppWidgetContainer"> android:theme="@style/AppTheme.Launcher.AppWidgetContainer">
<LinearLayout <LinearLayout

View file

@ -1,11 +1,11 @@
<resources> <resources>
<style name="Widget.AndroidAPS.AppWidget.Container" parent="android:Widget"> <style name="Widget.AndroidAPS.AppWidget.Container" parent="android:Widget">
<item name="android:padding">?attr/appWidgetPadding</item> <item name="android:padding">?attr/appWidgetPadding</item>
<item name="android:background">@drawable/app_widget_background</item> <item name="android:background">@drawable/widget_background</item>
</style> </style>
<style name="Widget.AndroidAPS.AppWidget.InnerView" parent="android:Widget"> <style name="Widget.AndroidAPS.AppWidget.InnerView" parent="android:Widget">
<item name="android:padding">?attr/appWidgetPadding</item> <item name="android:padding">?attr/appWidgetPadding</item>
<item name="android:background">@drawable/app_widget_inner_view_background</item> <item name="android:background">@drawable/widget_inner_view_background</item>
</style> </style>
</resources> </resources>

View file

@ -0,0 +1,6 @@
<resources>
<style name="AppTheme.Launcher.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault.DayNight">
<item name="appWidgetRadius">@android:dimen/system_app_widget_background_radius</item>
<item name="appWidgetInnerRadius">@android:dimen/system_app_widget_inner_radius</item>
</style>
</resources>

View file

@ -1209,4 +1209,5 @@
<string name="show_loop_records">Show loop records</string> <string name="show_loop_records">Show loop records</string>
<string name="show_hide_records">Hide loop records</string> <string name="show_hide_records">Hide loop records</string>
<string name="widget_description">AndroidAPS widget</string> <string name="widget_description">AndroidAPS widget</string>
<string name="configure">Configure opacity</string>
</resources> </resources>

View file

@ -1,4 +1,5 @@
<resources> <resources>
<style name="AppTheme.Launcher.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault"> <style name="AppTheme.Launcher.AppWidgetContainerParent" parent="@android:style/Theme.DeviceDefault">
<!-- Radius of the outer bound of widgets to make the rounded corners --> <!-- Radius of the outer bound of widgets to make the rounded corners -->
<item name="appWidgetRadius">16dp</item> <item name="appWidgetRadius">16dp</item>
@ -9,8 +10,7 @@
<item name="appWidgetInnerRadius">8dp</item> <item name="appWidgetInnerRadius">8dp</item>
</style> </style>
<style name="AppTheme.Launcher.AppWidgetContainer" <style name="AppTheme.Launcher.AppWidgetContainer" parent="AppTheme.Launcher.AppWidgetContainerParent">
parent="AppTheme.Launcher.AppWidgetContainerParent">
<!-- Apply padding to avoid the content of the widget colliding with the rounded corners --> <!-- Apply padding to avoid the content of the widget colliding with the rounded corners -->
<item name="appWidgetPadding">16dp</item> <item name="appWidgetPadding">16dp</item>
@ -20,4 +20,5 @@
<item name="defaultTextColor">@color/white</item> <item name="defaultTextColor">@color/white</item>
</style> </style>
</resources> </resources>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="info.nightscout.androidaps.widget.WidgetConfigureActivity"
android:description="@string/widget_description" android:description="@string/widget_description"
android:initialKeyguardLayout="@layout/widget_layout" android:initialKeyguardLayout="@layout/widget_layout"
android:initialLayout="@layout/widget_layout" android:initialLayout="@layout/widget_layout"
@ -8,4 +9,5 @@
android:previewImage="@drawable/widget_preview" android:previewImage="@drawable/widget_preview"
android:resizeMode="horizontal|vertical" android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000" android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" /> android:widgetCategory="home_screen"
android:widgetFeatures="reconfigurable|configuration_optional" />