diff --git a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt index 74bd926..b78df32 100644 --- a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt +++ b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeScreen.kt @@ -137,7 +137,7 @@ fun HomeScreen( albums = albums, scheduleSettings = scheduleSettings, appSettings = appSettings, - onToggleChanger = { viewModel.toggleWallpaperChanger(it) }, + onToggleChanger = { viewModel.toggleWallpaperChanger(it, onlyIfNotScheduled = true) }, onSelectHomeAlbum = { album -> viewModel.selectHomeAlbum(album) }, onSelectLockAlbum = { album -> viewModel.selectLockAlbum(album) }, onUpdateScheduleSettings = { viewModel.updateScheduleSettings(it) }, diff --git a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeViewModel.kt b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeViewModel.kt index 894568e..ef2e3d4 100644 --- a/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeViewModel.kt +++ b/app/src/main/java/com/anthonyla/paperize/presentation/screens/home/HomeViewModel.kt @@ -214,7 +214,7 @@ class HomeViewModel @Inject constructor( } } - fun toggleWallpaperChanger(enabled: Boolean) { + fun toggleWallpaperChanger(enabled: Boolean, onlyIfNotScheduled: Boolean = false) { viewModelScope.launch { // Use atomic update to prevent race conditions with album selection updates settingsRepository.updateEnableChanger(enabled) @@ -245,8 +245,11 @@ class HomeViewModel @Inject constructor( else -> null } - screenType?.let { changeWallpaperNow(it) } - scheduleAlarms(updated) + // Only change wallpaper now if this is not a "check if scheduled" call + if (!onlyIfNotScheduled) { + screenType?.let { changeWallpaperNow(it) } + } + scheduleAlarms(updated, onlyIfNotScheduled) } else { // Not all required albums selected - cancel any existing schedules wallpaperScheduler.cancelAllWallpaperChanges() @@ -320,7 +323,7 @@ class HomeViewModel @Inject constructor( context.startForegroundService(intent) } - private fun scheduleAlarms(settings: ScheduleSettings) { + private fun scheduleAlarms(settings: ScheduleSettings, onlyIfNotScheduled: Boolean = false) { val homeActive = settings.homeEnabled && settings.homeAlbumId != null val lockActive = settings.lockEnabled && settings.lockAlbumId != null @@ -362,7 +365,8 @@ class HomeViewModel @Inject constructor( wallpaperScheduler.scheduleWallpaperChanges( homeIntervalMinutes = homeInterval, lockIntervalMinutes = lockInterval, - synchronized = shouldSync + synchronized = shouldSync, + onlyIfNotScheduled = onlyIfNotScheduled ) } } diff --git a/app/src/main/java/com/anthonyla/paperize/service/alarm/BootReceiver.kt b/app/src/main/java/com/anthonyla/paperize/service/alarm/BootReceiver.kt index 517832c..c4c3758 100644 --- a/app/src/main/java/com/anthonyla/paperize/service/alarm/BootReceiver.kt +++ b/app/src/main/java/com/anthonyla/paperize/service/alarm/BootReceiver.kt @@ -83,7 +83,8 @@ class BootReceiver : BroadcastReceiver() { wallpaperScheduler.scheduleWallpaperChanges( homeIntervalMinutes = homeInterval, lockIntervalMinutes = lockInterval, - synchronized = shouldSync + synchronized = shouldSync, + onlyIfNotScheduled = true // Don't reschedule if already scheduled ) Log.d(TAG, "Wallpaper changes rescheduled successfully") diff --git a/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperScheduler.kt b/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperScheduler.kt index 83f4d16..76dabd2 100644 --- a/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperScheduler.kt +++ b/app/src/main/java/com/anthonyla/paperize/service/worker/WallpaperScheduler.kt @@ -95,13 +95,15 @@ class WallpaperScheduler @Inject constructor( * @param synchronized Whether home and lock should use the same wallpaper (when intervals match) * @param networkRequired Whether network is required * @param requireCharging Whether device must be charging + * @param onlyIfNotScheduled If true, only schedule if work is not already scheduled (for app startup) */ fun scheduleWallpaperChanges( homeIntervalMinutes: Int, lockIntervalMinutes: Int, synchronized: Boolean = false, networkRequired: Boolean = false, - requireCharging: Boolean = false + requireCharging: Boolean = false, + onlyIfNotScheduled: Boolean = false ) { // Don't cancel all first - let UPDATE policy handle existing work // This prevents immediate wallpaper change when just updating intervals @@ -115,46 +117,62 @@ class WallpaperScheduler @Inject constructor( if (shouldSync) { // When synchronized, schedule a single BOTH job that sets both screens // This ensures both screens always show the same wallpaper - scheduleWallpaperChange( - ScreenType.BOTH, - homeIntervalMinutes, - networkRequired, - requireCharging - ) - // Cancel any existing separate HOME/LOCK jobs - cancelWallpaperChange(ScreenType.HOME) - cancelWallpaperChange(ScreenType.LOCK) - Log.d(TAG, "Scheduled synchronized wallpaper changes (BOTH) every $homeIntervalMinutes minutes") + if (!onlyIfNotScheduled || !isWorkScheduled(ScreenType.BOTH)) { + scheduleWallpaperChange( + ScreenType.BOTH, + homeIntervalMinutes, + networkRequired, + requireCharging + ) + // Cancel any existing separate HOME/LOCK jobs + cancelWallpaperChange(ScreenType.HOME) + cancelWallpaperChange(ScreenType.LOCK) + Log.d(TAG, "Scheduled synchronized wallpaper changes (BOTH) every $homeIntervalMinutes minutes") + } else { + Log.d(TAG, "Skipped scheduling BOTH wallpaper changes - already scheduled") + } } else { // Independent schedules - schedule or cancel each separately // Cancel BOTH job if it exists (switching from sync to separate mode) cancelWallpaperChange(ScreenType.BOTH) if (homeIntervalMinutes > 0) { - scheduleWallpaperChange( - ScreenType.HOME, - homeIntervalMinutes, - networkRequired, - requireCharging - ) + if (!onlyIfNotScheduled || !isWorkScheduled(ScreenType.HOME)) { + scheduleWallpaperChange( + ScreenType.HOME, + homeIntervalMinutes, + networkRequired, + requireCharging + ) + } else { + Log.d(TAG, "Skipped scheduling HOME wallpaper changes - already scheduled") + } } else { cancelWallpaperChange(ScreenType.HOME) } if (lockIntervalMinutes > 0) { - scheduleWallpaperChange( - ScreenType.LOCK, - lockIntervalMinutes, - networkRequired, - requireCharging - ) + if (!onlyIfNotScheduled || !isWorkScheduled(ScreenType.LOCK)) { + scheduleWallpaperChange( + ScreenType.LOCK, + lockIntervalMinutes, + networkRequired, + requireCharging + ) + } else { + Log.d(TAG, "Skipped scheduling LOCK wallpaper changes - already scheduled") + } } else { cancelWallpaperChange(ScreenType.LOCK) } } // Schedule daily album refresh to validate all albums - scheduleAlbumRefresh() + if (!onlyIfNotScheduled || !isAlbumRefreshScheduled()) { + scheduleAlbumRefresh() + } else { + Log.d(TAG, "Skipped scheduling album refresh - already scheduled") + } } /** @@ -263,4 +281,34 @@ class WallpaperScheduler @Inject constructor( ScreenType.BOTH -> Constants.WORK_TAG_BOTH } } + + /** + * Check if wallpaper change work is already scheduled + * + * @param screenType Screen type to check + * @return true if work is scheduled (ENQUEUED or RUNNING), false otherwise + */ + private fun isWorkScheduled(screenType: ScreenType): Boolean { + val workName = getWorkName(screenType) + val workInfos = workManager.getWorkInfosForUniqueWork(workName).get() + + return workInfos.any { workInfo -> + workInfo.state == androidx.work.WorkInfo.State.ENQUEUED || + workInfo.state == androidx.work.WorkInfo.State.RUNNING + } + } + + /** + * Check if album refresh work is already scheduled + * + * @return true if work is scheduled (ENQUEUED or RUNNING), false otherwise + */ + private fun isAlbumRefreshScheduled(): Boolean { + val workInfos = workManager.getWorkInfosForUniqueWork(Constants.WORK_NAME_REFRESH).get() + + return workInfos.any { workInfo -> + workInfo.state == androidx.work.WorkInfo.State.ENQUEUED || + workInfo.state == androidx.work.WorkInfo.State.RUNNING + } + } }