PruneCacheWorker.kt 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2023 Tusky Contributors
  3. *
  4. * This file is a part of Tusky.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under the terms of the
  7. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  11. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. * Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  15. * see <http://www.gnu.org/licenses>.
  16. */
  17. package com.keylesspalace.tusky.worker
  18. import android.app.Notification
  19. import android.content.Context
  20. import android.util.Log
  21. import androidx.work.CoroutineWorker
  22. import androidx.work.ForegroundInfo
  23. import androidx.work.ListenableWorker
  24. import androidx.work.WorkerParameters
  25. import com.keylesspalace.tusky.R
  26. import com.keylesspalace.tusky.components.notifications.NotificationHelper
  27. import com.keylesspalace.tusky.components.notifications.NotificationHelper.NOTIFICATION_ID_PRUNE_CACHE
  28. import com.keylesspalace.tusky.db.AccountManager
  29. import com.keylesspalace.tusky.db.AppDatabase
  30. import javax.inject.Inject
  31. /** Prune the database cache of old statuses. */
  32. class PruneCacheWorker(
  33. appContext: Context,
  34. workerParams: WorkerParameters,
  35. private val appDatabase: AppDatabase,
  36. private val accountManager: AccountManager
  37. ) : CoroutineWorker(appContext, workerParams) {
  38. val notification: Notification = NotificationHelper.createWorkerNotification(applicationContext, R.string.notification_prune_cache)
  39. override suspend fun doWork(): Result {
  40. for (account in accountManager.accounts) {
  41. Log.d(TAG, "Pruning database using account ID: ${account.id}")
  42. appDatabase.timelineDao().cleanup(account.id, MAX_STATUSES_IN_CACHE)
  43. }
  44. return Result.success()
  45. }
  46. override suspend fun getForegroundInfo() = ForegroundInfo(NOTIFICATION_ID_PRUNE_CACHE, notification)
  47. companion object {
  48. private const val TAG = "PruneCacheWorker"
  49. private const val MAX_STATUSES_IN_CACHE = 1000
  50. const val PERIODIC_WORK_TAG = "PruneCacheWorker_periodic"
  51. }
  52. class Factory @Inject constructor(
  53. private val appDatabase: AppDatabase,
  54. private val accountManager: AccountManager
  55. ) : ChildWorkerFactory {
  56. override fun createWorker(appContext: Context, params: WorkerParameters): ListenableWorker {
  57. return PruneCacheWorker(appContext, params, appDatabase, accountManager)
  58. }
  59. }
  60. }