Implemented wallpaper changer widget (1x1 icon)
This commit is contained in:
1 parent
814db9020a
commit
eb48200149
9 files changed
+150
-75
No files matched your search
+54
@@ -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)
|
||||
)
|
||||
}
|
||||
}
|
||||
+7
@@ -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()
|
||||
}
|
||||
+19
-1
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user