diff --git a/app/src/main/java/ani/dantotsu/App.kt b/app/src/main/java/ani/dantotsu/App.kt index 37024e27..d58f7e7f 100644 --- a/app/src/main/java/ani/dantotsu/App.kt +++ b/app/src/main/java/ani/dantotsu/App.kt @@ -121,7 +121,9 @@ class App : MultiDexApplication() { } val useAlarmManager = PrefManager.getVal(PrefName.UseAlarmManager) - TaskScheduler.create(this, useAlarmManager).scheduleAllTasks(this) + val scheduler = TaskScheduler.create(this, useAlarmManager) + scheduler.scheduleAllTasks(this) + scheduler.scheduleSingleWork(this) } private fun setupNotificationChannels() { diff --git a/app/src/main/java/ani/dantotsu/notifications/AlarmManagerScheduler.kt b/app/src/main/java/ani/dantotsu/notifications/AlarmManagerScheduler.kt index 2c729d0b..3cc6b110 100644 --- a/app/src/main/java/ani/dantotsu/notifications/AlarmManagerScheduler.kt +++ b/app/src/main/java/ani/dantotsu/notifications/AlarmManagerScheduler.kt @@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit class AlarmManagerScheduler(private val context: Context) : TaskScheduler { override fun scheduleRepeatingTask(taskType: TaskType, interval: Long) { - if (interval < TimeUnit.MINUTES.toMillis(15)) { + if (interval * 1000 < TimeUnit.MINUTES.toMillis(15)) { cancelTask(taskType) return } diff --git a/app/src/main/java/ani/dantotsu/notifications/TaskScheduler.kt b/app/src/main/java/ani/dantotsu/notifications/TaskScheduler.kt index fa0304ed..f30918ee 100644 --- a/app/src/main/java/ani/dantotsu/notifications/TaskScheduler.kt +++ b/app/src/main/java/ani/dantotsu/notifications/TaskScheduler.kt @@ -1,6 +1,7 @@ package ani.dantotsu.notifications import android.content.Context +import androidx.work.OutOfQuotaPolicy import ani.dantotsu.notifications.anilist.AnilistNotificationWorker import ani.dantotsu.notifications.comment.CommentNotificationWorker import ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker @@ -31,6 +32,31 @@ interface TaskScheduler { } } + fun scheduleSingleWork(context: Context) { + val workManager = androidx.work.WorkManager.getInstance(context) + workManager.enqueueUniqueWork( + CommentNotificationWorker.WORK_NAME, + androidx.work.ExistingWorkPolicy.REPLACE, + androidx.work.OneTimeWorkRequest.Builder(CommentNotificationWorker::class.java) + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) + .build() + ) + workManager.enqueueUniqueWork( + AnilistNotificationWorker.WORK_NAME, + androidx.work.ExistingWorkPolicy.REPLACE, + androidx.work.OneTimeWorkRequest.Builder(AnilistNotificationWorker::class.java) + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) + .build() + ) + workManager.enqueueUniqueWork( + SubscriptionNotificationWorker.WORK_NAME, + androidx.work.ExistingWorkPolicy.REPLACE, + androidx.work.OneTimeWorkRequest.Builder(SubscriptionNotificationWorker::class.java) + .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) + .build() + ) + } + companion object { fun create(context: Context, useAlarmManager: Boolean): TaskScheduler { return if (useAlarmManager) { diff --git a/app/src/main/java/ani/dantotsu/notifications/WorkManagerScheduler.kt b/app/src/main/java/ani/dantotsu/notifications/WorkManagerScheduler.kt index 422f5586..d66c0985 100644 --- a/app/src/main/java/ani/dantotsu/notifications/WorkManagerScheduler.kt +++ b/app/src/main/java/ani/dantotsu/notifications/WorkManagerScheduler.kt @@ -10,7 +10,7 @@ import ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker class WorkManagerScheduler(private val context: Context) : TaskScheduler { override fun scheduleRepeatingTask(taskType: TaskType, interval: Long) { - if (interval < PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS) { + if (interval * 1000 < PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS) { cancelTask(taskType) return } diff --git a/app/src/main/java/ani/dantotsu/notifications/anilist/AnilistNotificationWorker.kt b/app/src/main/java/ani/dantotsu/notifications/anilist/AnilistNotificationWorker.kt index f3506fc8..4c5bf9b6 100644 --- a/app/src/main/java/ani/dantotsu/notifications/anilist/AnilistNotificationWorker.kt +++ b/app/src/main/java/ani/dantotsu/notifications/anilist/AnilistNotificationWorker.kt @@ -10,6 +10,11 @@ class AnilistNotificationWorker(appContext: Context, workerParams: WorkerParamet override suspend fun doWork(): Result { Logger.log("AnilistNotificationWorker: doWork") + if (System.currentTimeMillis() - lastCheck < 60000) { + Logger.log("AnilistNotificationWorker: doWork skipped") + return Result.success() + } + lastCheck = System.currentTimeMillis() return if (AnilistNotificationTask().execute(applicationContext)) { Result.success() } else { @@ -21,5 +26,6 @@ class AnilistNotificationWorker(appContext: Context, workerParams: WorkerParamet companion object { val checkIntervals = arrayOf(0L, 30, 60, 120, 240, 360, 720, 1440) const val WORK_NAME = "ani.dantotsu.notifications.anilist.AnilistNotificationWorker" + private var lastCheck = 0L } } \ No newline at end of file diff --git a/app/src/main/java/ani/dantotsu/notifications/comment/CommentNotificationWorker.kt b/app/src/main/java/ani/dantotsu/notifications/comment/CommentNotificationWorker.kt index 47d61fd5..08ef4ad9 100644 --- a/app/src/main/java/ani/dantotsu/notifications/comment/CommentNotificationWorker.kt +++ b/app/src/main/java/ani/dantotsu/notifications/comment/CommentNotificationWorker.kt @@ -11,6 +11,11 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet CoroutineWorker(appContext, workerParams) { override suspend fun doWork(): Result { Logger.log("CommentNotificationWorker: doWork") + if (System.currentTimeMillis() - lastCheck < 60000) { + Logger.log("CommentNotificationWorker: doWork skipped") + return Result.success() + } + lastCheck = System.currentTimeMillis() return if (CommentNotificationTask().execute(applicationContext)) { Result.success() } else { @@ -30,5 +35,6 @@ class CommentNotificationWorker(appContext: Context, workerParams: WorkerParamet companion object { val checkIntervals = arrayOf(0L, 480, 720, 1440) const val WORK_NAME = "ani.dantotsu.notifications.comment.CommentNotificationWorker" + private var lastCheck = 0L } } \ No newline at end of file diff --git a/app/src/main/java/ani/dantotsu/notifications/subscription/SubscriptionNotificationWorker.kt b/app/src/main/java/ani/dantotsu/notifications/subscription/SubscriptionNotificationWorker.kt index 22086b3b..eecf1add 100644 --- a/app/src/main/java/ani/dantotsu/notifications/subscription/SubscriptionNotificationWorker.kt +++ b/app/src/main/java/ani/dantotsu/notifications/subscription/SubscriptionNotificationWorker.kt @@ -11,7 +11,12 @@ class SubscriptionNotificationWorker(appContext: Context, workerParams: WorkerPa override suspend fun doWork(): Result { Logger.log("SubscriptionNotificationWorker: doWork") - return if (AnilistNotificationTask().execute(applicationContext)) { + if (System.currentTimeMillis() - lastCheck < 60000) { + Logger.log("SubscriptionNotificationWorker: doWork skipped") + return Result.success() + } + lastCheck = System.currentTimeMillis() + return if (SubscriptionNotificationTask().execute(applicationContext)) { Result.success() } else { Logger.log("SubscriptionNotificationWorker: doWork failed") @@ -23,5 +28,6 @@ class SubscriptionNotificationWorker(appContext: Context, workerParams: WorkerPa val checkIntervals = arrayOf(0L, 480, 720, 1440) const val WORK_NAME = "ani.dantotsu.notifications.subscription.SubscriptionNotificationWorker" + private var lastCheck = 0L } } \ No newline at end of file