Switched workmanager to alarmmanager to improve reliability.
This commit is contained in:
1 parent
0bca639e3a
commit
eb99329521
18 files changed
+137
-208
No files matched your search
Generated
+1
-1
@@ -15,7 +15,7 @@
|
||||
</deviceKey>
|
||||
</Target>
|
||||
</runningDeviceTargetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2024-04-08T04:25:59.382027900Z" />
|
||||
<timeTargetWasSelectedWithDropDown value="2024-04-23T03:07:04.964035600Z" />
|
||||
</State>
|
||||
</entry>
|
||||
</value>
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
android:name="android.software.paperize"
|
||||
android:required="true" />
|
||||
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:name=".App"
|
||||
@@ -21,7 +21,6 @@
|
||||
android:theme="@style/Theme.Paperize"
|
||||
android:enableOnBackInvokedCallback="true"
|
||||
tools:targetApi="tiramisu">
|
||||
|
||||
<activity
|
||||
android:name=".feature.wallpaper.presentation.MainActivity"
|
||||
android:exported="true"
|
||||
@@ -32,10 +31,6 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<provider
|
||||
android:name="androidx.startup.InitializationProvider"
|
||||
android:authorities="${applicationId}.androidx-startup"
|
||||
tools:node="remove" />
|
||||
<receiver android:name=".feature.wallpaper.alarmmanager.WallpaperReceiver" />
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -1,21 +1,14 @@
|
||||
package com.anthonyla.paperize
|
||||
|
||||
import android.app.Application
|
||||
import android.util.Log
|
||||
import androidx.work.Configuration
|
||||
import com.anthonyla.paperize.feature.wallpaper.workmanager.WallpaperWorkerFactory
|
||||
import dagger.hilt.android.HiltAndroidApp
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Application class for Hilt
|
||||
*/
|
||||
@HiltAndroidApp
|
||||
class App: Application(), Configuration.Provider {
|
||||
@Inject lateinit var workerFactory: WallpaperWorkerFactory
|
||||
override val workManagerConfiguration: Configuration
|
||||
get() = Configuration.Builder()
|
||||
.setMinimumLoggingLevel(Log.DEBUG)
|
||||
.setWorkerFactory(workerFactory)
|
||||
.build()
|
||||
class App: Application() {
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,7 @@ package com.anthonyla.paperize.di
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import androidx.hilt.work.HiltWorkerFactory
|
||||
import androidx.room.Room
|
||||
import androidx.work.WorkerFactory
|
||||
import com.anthonyla.paperize.data.settings.SettingsDataStore
|
||||
import com.anthonyla.paperize.data.settings.SettingsDataStoreImpl
|
||||
import com.anthonyla.paperize.feature.wallpaper.data.data_source.AlbumDatabase
|
||||
@@ -13,7 +11,6 @@ import com.anthonyla.paperize.feature.wallpaper.data.repository.AlbumRepositoryI
|
||||
import com.anthonyla.paperize.feature.wallpaper.data.repository.SelectedAlbumRepositoryImpl
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.repository.AlbumRepository
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.repository.SelectedAlbumRepository
|
||||
import com.anthonyla.paperize.feature.wallpaper.workmanager.WallpaperWorkerFactory
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.anthonyla.paperize.feature.wallpaper.alarmmanager
|
||||
|
||||
import android.app.WallpaperManager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.ImageDecoder
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.core.net.toUri
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.repository.SelectedAlbumRepository
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.IOException
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class WallpaperReceiver: BroadcastReceiver() {
|
||||
@Inject lateinit var repository: SelectedAlbumRepository
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
if (context != null) {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
Log.d("WallpaperReceiver", "Changing wallpaper. Available wallpaper: " + repository.countWallpapersInRotation())
|
||||
changeWallpaper(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun changeWallpaper(context: Context) {
|
||||
val wallpaperManager = WallpaperManager.getInstance(context)
|
||||
val wallpaper = repository.getRandomWallpaperInRotation()
|
||||
if (wallpaper != null) {
|
||||
try {
|
||||
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
val source = ImageDecoder.createSource(
|
||||
context.contentResolver,
|
||||
wallpaper.wallpaperUri.toUri()
|
||||
)
|
||||
ImageDecoder.decodeBitmap(source)
|
||||
} else {
|
||||
context.contentResolver.openInputStream(wallpaper.wallpaperUri.toUri())
|
||||
?.use { inputStream ->
|
||||
BitmapFactory.decodeStream(inputStream)
|
||||
}
|
||||
}
|
||||
bitmap?.let { image -> wallpaperManager.setBitmap(image) }
|
||||
repository.upsertWallpaper(wallpaper.copy(isInRotation = false))
|
||||
} catch (e: IOException) {
|
||||
Log.e("WallpaperReceiver", "Error changing wallpaper", e)
|
||||
}
|
||||
}
|
||||
else {
|
||||
repository.setAllWallpapersInRotation()
|
||||
}
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.anthonyla.paperize.feature.wallpaper.alarmmanager
|
||||
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class WallpaperScheduler (
|
||||
private val context: Context
|
||||
) {
|
||||
private val alarmManager = context.getSystemService(AlarmManager::class.java)
|
||||
|
||||
fun scheduleWallpaperChanger(timeInMinutes: Long) {
|
||||
val intervalInMillis = TimeUnit.MINUTES.toMillis(timeInMinutes)
|
||||
alarmManager.setRepeating(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
System.currentTimeMillis(),
|
||||
intervalInMillis,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
0,
|
||||
Intent(context, WallpaperReceiver::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
Log.d("WallpaperReceiver", "Scheduled wallpaper changer every $timeInMinutes minutes")
|
||||
}
|
||||
|
||||
fun cancelWallpaperChanger() {
|
||||
alarmManager.cancel(
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
0,
|
||||
Intent(context, WallpaperReceiver::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
Log.d("WallpaperReceiver", "Cancelled wallpaper changer")
|
||||
}
|
||||
}
|
||||
+3
@@ -28,6 +28,9 @@ interface SelectedAlbumDao {
|
||||
@Query("SELECT * FROM wallpaper ORDER BY RANDOM() LIMIT 1")
|
||||
fun getRandomWallpaperInRotation(): Wallpaper?
|
||||
|
||||
@Query("SELECT COUNT(*) FROM wallpaper WHERE isInRotation = 1")
|
||||
fun countWallpapersInRotation(): Int
|
||||
|
||||
@Query("UPDATE wallpaper SET isInRotation = 1")
|
||||
suspend fun setAllWallpapersInRotation()
|
||||
|
||||
|
||||
+4
@@ -24,6 +24,10 @@ class SelectedAlbumRepositoryImpl(
|
||||
return dao.getRandomWallpaperInRotation()
|
||||
}
|
||||
|
||||
override fun countWallpapersInRotation(): Int {
|
||||
return dao.countWallpapersInRotation()
|
||||
}
|
||||
|
||||
override suspend fun setAllWallpapersInRotation() {
|
||||
dao.setAllWallpapersInRotation()
|
||||
}
|
||||
|
||||
+5
@@ -21,6 +21,11 @@ interface SelectedAlbumRepository {
|
||||
*/
|
||||
fun getRandomWallpaperInRotation(): Wallpaper?
|
||||
|
||||
/**
|
||||
* Get count of wallpapers in rotation
|
||||
*/
|
||||
fun countWallpapersInRotation(): Int
|
||||
|
||||
/**
|
||||
* Add all wallpapers back into rotation
|
||||
*/
|
||||
|
||||
+3
-1
@@ -18,6 +18,7 @@ import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleEventObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import com.anthonyla.paperize.feature.wallpaper.alarmmanager.WallpaperScheduler
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsEvent
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsViewModel
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.settings_screen.SettingsViewModel
|
||||
@@ -37,6 +38,7 @@ class MainActivity : ComponentActivity() {
|
||||
// Show splash screen until app data is fully loaded and ready to be displayed
|
||||
val splashScreen = installSplashScreen()
|
||||
super.onCreate(savedInstanceState)
|
||||
val wallpaperScheduler = WallpaperScheduler(this)
|
||||
splashScreen.setKeepOnScreenCondition {
|
||||
settingsViewModel.shouldNotBypassSplashScreen
|
||||
|| albumsViewModel.shouldNotBypassSplashScreen
|
||||
@@ -54,7 +56,7 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
PaperizeTheme(settingsViewModel.state) {
|
||||
Surface(tonalElevation = 5.dp) {
|
||||
PaperizeApp(albumsViewModel, settingsViewModel, wallpaperScreenViewModel)
|
||||
PaperizeApp(albumsViewModel, settingsViewModel, wallpaperScreenViewModel, wallpaperScheduler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-3
@@ -18,6 +18,7 @@ import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.navigation.navArgument
|
||||
import com.anthonyla.paperize.data.Contact
|
||||
import com.anthonyla.paperize.feature.wallpaper.alarmmanager.WallpaperScheduler
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.add_album_screen.AddAlbumScreen
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsEvent
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsViewModel
|
||||
@@ -31,7 +32,6 @@ import com.anthonyla.paperize.feature.wallpaper.presentation.settings_screen.Set
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.WallpaperEvent
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.WallpaperScreenViewModel
|
||||
import com.anthonyla.paperize.feature.wallpaper.util.navigation.NavScreens
|
||||
import com.anthonyla.paperize.feature.wallpaper.workmanager.WorkerRepository
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
@@ -44,7 +44,7 @@ fun PaperizeApp(
|
||||
albumsViewModel: AlbumsViewModel,
|
||||
settingsViewModel: SettingsViewModel,
|
||||
wallpaperScreenViewModel: WallpaperScreenViewModel,
|
||||
repository: WorkerRepository
|
||||
wallpaperScheduler: WallpaperScheduler
|
||||
) {
|
||||
val navController = rememberNavController()
|
||||
val albumState = albumsViewModel.state.collectAsStateWithLifecycle()
|
||||
@@ -87,7 +87,7 @@ fun PaperizeApp(
|
||||
)
|
||||
)) } ?: run {
|
||||
wallpaperScreenViewModel.onEvent(WallpaperEvent.Reset)
|
||||
repository.cancelWorker()
|
||||
wallpaperScheduler.cancelWallpaperChanger()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,6 +116,9 @@ fun PaperizeApp(
|
||||
},
|
||||
onAlbumViewClick = {
|
||||
navController.navigate("${NavScreens.AlbumView.route}/$it")
|
||||
},
|
||||
onScheduleWallpaperChanger = { timeInMinutes ->
|
||||
wallpaperScheduler.scheduleWallpaperChanger(timeInMinutes)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
+4
-1
@@ -38,6 +38,7 @@ fun HomeScreen(
|
||||
onContactClick: () -> Unit,
|
||||
navigateToAddWallpaperScreen: (String) -> Unit,
|
||||
onAlbumViewClick: (String) -> Unit,
|
||||
onScheduleWallpaperChanger: (Long) -> Unit
|
||||
) {
|
||||
var tabIndex by rememberSaveable { mutableIntStateOf(0) }
|
||||
val pagerState = rememberPagerState { tabItems.size }
|
||||
@@ -100,7 +101,9 @@ fun HomeScreen(
|
||||
}
|
||||
HorizontalPager(state = pagerState, beyondBoundsPageCount = 1) { index ->
|
||||
when(index) {
|
||||
0 -> WallpaperScreen()
|
||||
0 -> WallpaperScreen(
|
||||
onScheduleWallpaperChanger = { onScheduleWallpaperChanger(it) }
|
||||
)
|
||||
1 -> LibraryScreen(
|
||||
onAddNewAlbumClick = { addAlbumDialog = true },
|
||||
onViewAlbum = { onAlbumViewClick(it) }
|
||||
|
||||
-2
@@ -5,9 +5,7 @@ import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
|
||||
|
||||
sealed class WallpaperEvent {
|
||||
data class UpdateSelectedAlbum(val albumWithWallpaperAndFolder: AlbumWithWallpaperAndFolder): WallpaperEvent()
|
||||
data class StartWallpaperWorker(val time: Long): WallpaperEvent()
|
||||
data class ExitRotation(val wallpaper: Wallpaper): WallpaperEvent()
|
||||
object Reset : WallpaperEvent()
|
||||
object Refresh : WallpaperEvent()
|
||||
object StopWallpaperWorker : WallpaperEvent()
|
||||
}
|
||||
+3
-14
@@ -2,13 +2,7 @@ package com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.app.WallpaperManager
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.ImageDecoder
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
@@ -24,22 +18,17 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.net.toUri
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsViewModel
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.components.AlbumBottomSheet
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.components.CurrentSelectedAlbum
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.IOException
|
||||
|
||||
@Composable
|
||||
fun WallpaperScreen(
|
||||
albumsViewModel: AlbumsViewModel = hiltViewModel(),
|
||||
wallpaperScreenViewModel: WallpaperScreenViewModel = hiltViewModel()
|
||||
wallpaperScreenViewModel: WallpaperScreenViewModel = hiltViewModel(),
|
||||
onScheduleWallpaperChanger: (Long) -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val albumState = albumsViewModel.state.collectAsStateWithLifecycle()
|
||||
@@ -74,7 +63,7 @@ fun WallpaperScreen(
|
||||
if ((selectedState.value.selectedAlbum?.album?.initialAlbumName ?: "") != album.album.initialAlbumName) {
|
||||
wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(album))
|
||||
}
|
||||
wallpaperScreenViewModel.onEvent(WallpaperEvent.StartWallpaperWorker(1))
|
||||
onScheduleWallpaperChanger(1)
|
||||
openBottomSheet = false
|
||||
}
|
||||
)
|
||||
|
||||
-8
@@ -10,7 +10,6 @@ import androidx.lifecycle.viewModelScope
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.repository.SelectedAlbumRepository
|
||||
import com.anthonyla.paperize.feature.wallpaper.workmanager.WorkerRepository
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
@@ -24,7 +23,6 @@ import javax.inject.Inject
|
||||
class WallpaperScreenViewModel @Inject constructor (
|
||||
application: Application,
|
||||
private val repository: SelectedAlbumRepository,
|
||||
private val workerRepository: WorkerRepository
|
||||
) : AndroidViewModel(application) {
|
||||
var shouldNotBypassSplashScreen by mutableStateOf(true)
|
||||
private val context: Context get() = getApplication<Application>().applicationContext
|
||||
@@ -65,12 +63,6 @@ class WallpaperScreenViewModel @Inject constructor (
|
||||
repository.upsertSelectedAlbum(newSelectedAlbum)
|
||||
}
|
||||
}
|
||||
is WallpaperEvent.StartWallpaperWorker -> {
|
||||
workerRepository.scheduleWallpaperChanger(event.time)
|
||||
}
|
||||
is WallpaperEvent.StopWallpaperWorker -> {
|
||||
workerRepository.cancelWorker()
|
||||
}
|
||||
is WallpaperEvent.ExitRotation -> {
|
||||
viewModelScope.launch {
|
||||
repository.upsertWallpaper(event.wallpaper.copy(isInRotation = false))
|
||||
|
||||
-104
@@ -1,104 +0,0 @@
|
||||
package com.anthonyla.paperize.feature.wallpaper.workmanager
|
||||
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.WallpaperManager
|
||||
import android.content.Context
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.ImageDecoder
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.net.toUri
|
||||
import androidx.hilt.work.HiltWorker
|
||||
import androidx.work.CoroutineWorker
|
||||
import androidx.work.WorkerParameters
|
||||
import com.anthonyla.paperize.R
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.repository.SelectedAlbumRepository
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedInject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.IOException
|
||||
|
||||
/**
|
||||
* Worker class to change wallpaper periodically
|
||||
*/
|
||||
@HiltWorker
|
||||
class WallpaperWorker @AssistedInject constructor(
|
||||
@Assisted private val repository: SelectedAlbumRepository,
|
||||
@Assisted private val appContext: Context,
|
||||
@Assisted workerParams: WorkerParameters
|
||||
) : CoroutineWorker(appContext, workerParams) {
|
||||
|
||||
companion object {
|
||||
const val CHANNEL_ID = "wallpaper_worker_channel"
|
||||
const val NOTIFICATION_ID = 1
|
||||
}
|
||||
override suspend fun doWork(): Result {
|
||||
return try {
|
||||
withContext(Dispatchers.IO) {
|
||||
changeWallpaper(appContext)
|
||||
createNotificationChannel()
|
||||
showNotification()
|
||||
}
|
||||
Result.success()
|
||||
} catch (e: Exception) {
|
||||
Result.failure()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun changeWallpaper(context: Context) {
|
||||
val wallpaperManager = WallpaperManager.getInstance(context)
|
||||
val wallpaper = repository.getRandomWallpaperInRotation()
|
||||
if (wallpaper != null) {
|
||||
try {
|
||||
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
val source = ImageDecoder.createSource(
|
||||
context.contentResolver,
|
||||
wallpaper.wallpaperUri.toUri()
|
||||
)
|
||||
ImageDecoder.decodeBitmap(source)
|
||||
} else {
|
||||
context.contentResolver.openInputStream(wallpaper.wallpaperUri.toUri())
|
||||
?.use { inputStream ->
|
||||
BitmapFactory.decodeStream(inputStream)
|
||||
}
|
||||
}
|
||||
bitmap?.let { bmp ->
|
||||
wallpaperManager.setBitmap(bmp)
|
||||
}
|
||||
repository.upsertWallpaper(wallpaper.copy(isInRotation = false))
|
||||
} catch (e: IOException) {
|
||||
Log.e("WallpaperWorker", "Error changing wallpaper", e)
|
||||
}
|
||||
}
|
||||
else {
|
||||
repository.setAllWallpapersInRotation()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createNotificationChannel() {
|
||||
val name = "Wallpaper Worker"
|
||||
val descriptionText = "Notification for Wallpaper Worker"
|
||||
val importance = NotificationManager.IMPORTANCE_DEFAULT
|
||||
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
|
||||
description = descriptionText
|
||||
}
|
||||
val notificationManager: NotificationManager =
|
||||
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
private fun showNotification() {
|
||||
val builder = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_launcher_foreground)
|
||||
.setContentTitle("Wallpaper Worker")
|
||||
.setContentText("Wallpaper Worker is running")
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
|
||||
val notificationManager: NotificationManager =
|
||||
applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.notify(NOTIFICATION_ID, builder.build())
|
||||
}
|
||||
}
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package com.anthonyla.paperize.feature.wallpaper.workmanager
|
||||
|
||||
import android.content.Context
|
||||
import androidx.work.ListenableWorker
|
||||
import androidx.work.WorkerFactory
|
||||
import androidx.work.WorkerParameters
|
||||
import com.anthonyla.paperize.feature.wallpaper.domain.repository.SelectedAlbumRepository
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class WallpaperWorkerFactory @Inject constructor(
|
||||
private val selectedAlbumRepository: SelectedAlbumRepository
|
||||
) : WorkerFactory() {
|
||||
override fun createWorker(
|
||||
appContext: Context,
|
||||
workerClassName: String,
|
||||
workerParameters: WorkerParameters
|
||||
): ListenableWorker = WallpaperWorker(
|
||||
selectedAlbumRepository,
|
||||
appContext,
|
||||
workerParameters
|
||||
)
|
||||
}
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
package com.anthonyla.paperize.feature.wallpaper.workmanager
|
||||
|
||||
import android.content.Context
|
||||
import androidx.work.ExistingPeriodicWorkPolicy
|
||||
import androidx.work.PeriodicWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Repository class to handle WallpaperWorker scheduling
|
||||
*/
|
||||
class WorkerRepository @Inject constructor(context: Context) {
|
||||
private val workManager = WorkManager.getInstance(context)
|
||||
fun scheduleWallpaperChanger(time: Long) {
|
||||
val workRequest = PeriodicWorkRequestBuilder<WallpaperWorker>(
|
||||
15, TimeUnit.MINUTES
|
||||
)
|
||||
workManager.enqueueUniquePeriodicWork(
|
||||
"WallpaperWorker",
|
||||
ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
|
||||
workRequest.build()
|
||||
)
|
||||
}
|
||||
|
||||
fun cancelWorker() {
|
||||
workManager.cancelAllWork()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user