Implemented wallpaper changer widget (1x1 icon)

This commit is contained in:
Anthony La committed 2025-04-01 13:46:23 -07:00
1 parent 814db9020a
commit eb48200149
9 files changed
+150 -75

No files matched your search

+9
View File
@@ -55,6 +55,15 @@
<action android:name="com.anthonyla.paperize.SHORTCUT" />
</intent-filter>
</receiver>
<receiver android:name="com.anthonyla.paperize.feature.wallpaper.glance_widget.PaperizeWidgetReceiver"
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/paperize_widget_info" />
</receiver>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
@@ -0,0 +1,54 @@
package com.anthonyla.paperize.feature.wallpaper.glance_widget
import android.content.Context
import android.content.Intent
import androidx.compose.runtime.Composable
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.Image
import androidx.glance.ImageProvider
import androidx.glance.action.clickable
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.provideContent
import androidx.glance.layout.Box
import androidx.glance.layout.ContentScale
import androidx.glance.layout.fillMaxSize
import com.anthonyla.paperize.R
import androidx.glance.ColorFilter
import androidx.glance.GlanceTheme
import com.anthonyla.paperize.feature.wallpaper.wallpaper_alarmmanager.WallpaperBootAndChangeReceiver
class PaperizeWidget : GlanceAppWidget() {
override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent {
WidgetContent(context)
}
}
}
@Composable
private fun WidgetContent(context: Context) {
val ACTION_CHANGE_WALLPAPER = "com.anthonyla.paperize.SHORTCUT"
Box(
modifier = GlanceModifier
.fillMaxSize()
.clickable(
{
val intent = Intent(ACTION_CHANGE_WALLPAPER).apply {
setClass(context, WallpaperBootAndChangeReceiver::class.java)
addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
}
context.sendBroadcast(intent)
}
)
) {
Image(
provider = ImageProvider(R.drawable.next_wallpaper_icon),
contentDescription = "Paperize Widget",
modifier = GlanceModifier.fillMaxSize(),
contentScale = ContentScale.Fit,
colorFilter = ColorFilter.tint(GlanceTheme.colors.primaryContainer)
)
}
}
@@ -0,0 +1,7 @@
package com.anthonyla.paperize.feature.wallpaper.glance_widget
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
class PaperizeWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = PaperizeWidget()
}
@@ -1,5 +1,4 @@
package com.anthonyla.paperize.feature.wallpaper.wallpaper_alarmmanager
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
@@ -13,6 +12,7 @@ import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import androidx.core.content.edit
/**
* Receiver for boot and time change and wallpaper change events to restart alarm manager
@@ -21,15 +21,20 @@ import kotlin.coroutines.EmptyCoroutineContext
class WallpaperBootAndChangeReceiver : BroadcastReceiver() {
companion object {
const val ACTION_SHORTCUT = "com.anthonyla.paperize.SHORTCUT"
private const val PREFS_NAME = "wallpaper_receiver_prefs"
private const val LAST_EXECUTION_TIME = "last_execution_time"
private const val MIN_INTERVAL_MS = 3000
}
@Inject lateinit var settingsDataStoreImpl: SettingsDataStore
override fun onReceive(context: Context, intent: Intent) = goAsync {
try {
if (intent.action == ACTION_SHORTCUT && !canExecute(context)) return@goAsync
when(intent.action) {
Intent.ACTION_BOOT_COMPLETED,
Intent.ACTION_TIME_CHANGED,
Intent.ACTION_TIMEZONE_CHANGED,
ACTION_SHORTCUT -> {
saveExecutionTime(context)
val scheduler = WallpaperAlarmSchedulerImpl(context)
val toggleChanger = settingsDataStoreImpl.getBoolean(SettingsConstants.ENABLE_CHANGER) ?: false
val selectedAlbum = settingsDataStoreImpl.getString(SettingsConstants.HOME_ALBUM_NAME) ?: settingsDataStoreImpl.getString(SettingsConstants.LOCK_ALBUM_NAME) ?: ""
@@ -66,6 +71,19 @@ class WallpaperBootAndChangeReceiver : BroadcastReceiver() {
}
}
private fun canExecute(context: Context): Boolean {
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val lastExecution = prefs.getLong(LAST_EXECUTION_TIME, 0)
val currentTime = System.currentTimeMillis()
return currentTime - lastExecution >= MIN_INTERVAL_MS
}
private fun saveExecutionTime(context: Context) {
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
prefs.edit() { putLong(LAST_EXECUTION_TIME, System.currentTimeMillis()) }
}
/* https://stackoverflow.com/questions/74111692/run-coroutine-functions-on-broadcast-receiver */
private fun BroadcastReceiver.goAsync(
context: CoroutineContext = EmptyCoroutineContext,
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M240,560h80q0,-59 43,-99.5T466,420q36,0 67,16.5t51,43.5h-64v80h200v-200h-80v62q-32,-38 -76.5,-60T466,340q-95,0 -160.5,64T240,560ZM480,880q-83,0 -156,-31.5T197,763q-54,-54 -85.5,-127T80,480q0,-83 31.5,-156T197,197q54,-54 127,-85.5T480,80q83,0 156,31.5T763,197q54,54 85.5,127T880,480q0,83 -31.5,156T763,763q-54,54 -127,85.5T480,880ZM480,800q134,0 227,-93t93,-227q0,-134 -93,-227t-227,-93q-134,0 -227,93t-93,227q0,134 93,227t227,93ZM480,480Z"
android:fillColor="#e3e3e3"/>
</vector>
@@ -0,0 +1,9 @@
<?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:updatePeriodMillis="0"
android:initialLayout="@layout/glance_default_loading_layout"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"
android:previewImage="@drawable/next_wallpaper_icon" />