mirror of
https://github.com/mueller-ma/PrepaidBalance.git
synced 2024-09-20 00:12:15 +02:00
parent
3a1c465ac0
commit
5ea4e25ceb
@ -32,6 +32,19 @@
|
||||
<receiver
|
||||
android:name=".work.RetryBroadcastReceiver"
|
||||
android:exported="false" />
|
||||
|
||||
<receiver
|
||||
android:name=".widget.Widget"
|
||||
android:label="@string/app_name"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.appwidget.provider"
|
||||
android:resource="@xml/widget" />
|
||||
</receiver>
|
||||
</application>
|
||||
|
||||
</manifest>
|
@ -0,0 +1,64 @@
|
||||
package com.github.muellerma.prepaidbalance.widget
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.widget.RemoteViews
|
||||
import com.github.muellerma.prepaidbalance.R
|
||||
import com.github.muellerma.prepaidbalance.room.AppDatabase
|
||||
import com.github.muellerma.prepaidbalance.ui.MainActivity
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
class Widget : AppWidgetProvider(), CoroutineScope {
|
||||
override val coroutineContext: CoroutineContext get() = Dispatchers.IO + Job()
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
// There may be multiple widgets active, so update all of them
|
||||
appWidgetIds.forEach { appWidgetId ->
|
||||
Log.d(TAG, "Update widget with ID $appWidgetId")
|
||||
launch {
|
||||
setupWidget(context, appWidgetId, appWidgetManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAppWidgetOptionsChanged(
|
||||
context: Context,
|
||||
appWidgetManager: AppWidgetManager,
|
||||
appWidgetId: Int,
|
||||
newOptions: Bundle?
|
||||
) {
|
||||
Log.d(TAG, "onAppWidgetOptionsChanged()")
|
||||
launch {
|
||||
setupWidget(context, appWidgetId, appWidgetManager)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun setupWidget(context: Context, widgetId: Int, widgetManager: AppWidgetManager) {
|
||||
val database = AppDatabase.get(context)
|
||||
val latestInDb = database.balanceDao().getLatest()
|
||||
val textForWidget = latestInDb?.balance?.toString() ?: "0"
|
||||
Log.d(TAG, "textForWidget = $textForWidget")
|
||||
|
||||
val intent = Intent(context, MainActivity::class.java)
|
||||
val pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_IMMUTABLE)
|
||||
|
||||
RemoteViews(context.packageName, R.layout.widget).apply {
|
||||
setTextViewText(R.id.balance, textForWidget)
|
||||
setOnClickPendingIntent(android.R.id.background, pendingIntent)
|
||||
widgetManager.updateAppWidget(widgetId, this)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = Widget::class.java.simpleName
|
||||
}
|
||||
}
|
9
app/src/main/res/drawable/widget_background.xml
Normal file
9
app/src/main/res/drawable/widget_background.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
|
||||
<solid android:color="?android:attr/colorBackground" />
|
||||
|
||||
<corners android:radius="?attr/homeScreenWidgetBackgroundRadius" />
|
||||
</shape>
|
29
app/src/main/res/layout/widget.xml
Normal file
29
app/src/main/res/layout/widget.xml
Normal file
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@android:id/background"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:theme="@style/HomeScreenWidgetTheme"
|
||||
android:background="@drawable/widget_background">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/balance"
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="20sp"
|
||||
tools:text="10.45" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/btn_open_main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_margin="4dp"
|
||||
android:padding="0dp"
|
||||
android:alpha="0.3"
|
||||
android:scaleType="fitCenter"
|
||||
android:src="@mipmap/ic_launcher"
|
||||
tools:ignore="ContentDescription" />
|
||||
</FrameLayout>
|
29
app/src/main/res/layout/widget_preview.xml
Normal file
29
app/src/main/res/layout/widget_preview.xml
Normal file
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@android:id/background"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:theme="@style/HomeScreenWidgetTheme"
|
||||
android:background="@drawable/widget_background">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/balance"
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="20sp"
|
||||
android:text="10.45" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/btn_open_main"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_margin="4dp"
|
||||
android:padding="0dp"
|
||||
android:alpha="0.3"
|
||||
android:scaleType="fitCenter"
|
||||
android:src="@mipmap/ic_launcher"
|
||||
tools:ignore="ContentDescription" />
|
||||
</FrameLayout>
|
6
app/src/main/res/values-v31/styles.xml
Normal file
6
app/src/main/res/values-v31/styles.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="HomeScreenWidgetTheme" parent="Theme.Material3.DynamicColors.DayNight">
|
||||
<item name="homeScreenWidgetBackgroundRadius">@android:dimen/system_app_widget_background_radius</item>
|
||||
</style>
|
||||
</resources>
|
4
app/src/main/res/values/attr.xml
Normal file
4
app/src/main/res/values/attr.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<attr name="homeScreenWidgetBackgroundRadius" format="dimension" />
|
||||
</resources>
|
@ -48,6 +48,7 @@
|
||||
<string name="close">Close</string>
|
||||
<string name="no_response_saved">No response saved</string>
|
||||
<string name="last_update">Last update: %s</string>
|
||||
<string name="widget_description">Show latest balance</string>
|
||||
|
||||
<!-- Export as CSV -->
|
||||
<string name="export">Export as CSV</string>
|
||||
|
7
app/src/main/res/values/styles.xml
Normal file
7
app/src/main/res/values/styles.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="HomeScreenWidgetTheme" parent="Theme.Material3.DynamicColors.DayNight">
|
||||
<item name="homeScreenWidgetBackgroundRadius">8dp</item>
|
||||
<item name="colorPrimary">?android:attr/textColorSecondary</item>
|
||||
</style>
|
||||
</resources>
|
13
app/src/main/res/xml/widget.xml
Normal file
13
app/src/main/res/xml/widget.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:minWidth="40dp"
|
||||
android:minHeight="40dp"
|
||||
android:targetCellWidth="1"
|
||||
android:targetCellHeight="1"
|
||||
android:updatePeriodMillis="86400000"
|
||||
android:description="@string/widget_description"
|
||||
android:previewLayout="@layout/widget_preview"
|
||||
android:initialLayout="@layout/widget"
|
||||
android:resizeMode="horizontal|vertical"
|
||||
android:widgetCategory="home_screen"
|
||||
android:widgetFeatures="" />
|
Loading…
Reference in New Issue
Block a user