Add check to prevent unnecessary rescheduling on app startup
Prevent redundant WorkManager job rescheduling when app starts: **Problem:** - Every time app starts, wallpaper change workers were being rescheduled - Same with album refresh worker - This caused unnecessary database writes and WorkManager operations - Even when jobs were already correctly scheduled **Solution:** Added `onlyIfNotScheduled` parameter throughout the scheduling chain: 1. **WallpaperScheduler:** - Added `isWorkScheduled()` and `isAlbumRefreshScheduled()` helpers - Check WorkInfo state (ENQUEUED or RUNNING) before scheduling - Skip scheduling if work already exists and `onlyIfNotScheduled = true` 2. **HomeViewModel:** - Added `onlyIfNotScheduled` parameter to `toggleWallpaperChanger()` - Added `onlyIfNotScheduled` parameter to `scheduleAlarms()` - Skip immediate wallpaper change when checking if scheduled - Propagate parameter through scheduling chain 3. **BootReceiver:** - Use `onlyIfNotScheduled = true` on device boot - Prevents rescheduling if jobs survived device restart 4. **HomeScreen:** - Auto-toggle uses `onlyIfNotScheduled = true` - Prevents rescheduling on every app startup - User actions still always reschedule (parameter defaults to false) **Behavior:** - App startup: Only schedule if not already scheduled - Device boot: Only schedule if not already scheduled - User actions (album selection, settings changes): Always reschedule - Album refresh: Only schedule if not already scheduled This improves app startup performance and reduces unnecessary WorkManager operations while maintaining correct scheduling behavior. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
49fecf56af
commit
1006902d07
4 files changed
+84
-31
No files matched your search
@@ -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) },
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user