improved icon cache clean up
This commit is contained in:
parent
f68bb5f379
commit
80cc011852
1 changed files with 12 additions and 6 deletions
|
@ -2,6 +2,7 @@ package io.heckel.ntfy.work
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import androidx.core.content.FileProvider
|
||||||
import androidx.work.CoroutineWorker
|
import androidx.work.CoroutineWorker
|
||||||
import androidx.work.WorkerParameters
|
import androidx.work.WorkerParameters
|
||||||
import io.heckel.ntfy.BuildConfig
|
import io.heckel.ntfy.BuildConfig
|
||||||
|
@ -10,6 +11,7 @@ import io.heckel.ntfy.db.Repository
|
||||||
import io.heckel.ntfy.msg.DownloadIconWorker
|
import io.heckel.ntfy.msg.DownloadIconWorker
|
||||||
import io.heckel.ntfy.ui.DetailAdapter
|
import io.heckel.ntfy.ui.DetailAdapter
|
||||||
import io.heckel.ntfy.util.Log
|
import io.heckel.ntfy.util.Log
|
||||||
|
import io.heckel.ntfy.util.fileStat
|
||||||
import io.heckel.ntfy.util.topicShortUrl
|
import io.heckel.ntfy.util.topicShortUrl
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
@ -65,18 +67,22 @@ class DeleteWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx
|
||||||
|
|
||||||
private fun deleteExpiredIcons() {
|
private fun deleteExpiredIcons() {
|
||||||
Log.d(TAG, "Deleting icons for deleted notifications")
|
Log.d(TAG, "Deleting icons for deleted notifications")
|
||||||
val resolver = applicationContext.contentResolver
|
|
||||||
val repository = Repository.getInstance(applicationContext)
|
val repository = Repository.getInstance(applicationContext)
|
||||||
val activeIconUris = repository.getActiveIconUris()
|
val activeIconUris = repository.getActiveIconUris()
|
||||||
val expiredIconUris = repository.getDeletedIconUris()
|
val activeIconFilenames = activeIconUris.map{ fileStat(applicationContext, Uri.parse(it)).filename }.toSet()
|
||||||
val urisToDelete = expiredIconUris.minus(activeIconUris)
|
val iconDir = File(applicationContext.cacheDir, DownloadIconWorker.ICON_CACHE_DIR)
|
||||||
urisToDelete.forEach { uri ->
|
val allIconFilenames = iconDir.listFiles().map{ file -> file.name }
|
||||||
|
val filenamesToDelete = allIconFilenames.minus(activeIconFilenames)
|
||||||
|
filenamesToDelete.forEach { filename ->
|
||||||
try {
|
try {
|
||||||
val deleted = resolver.delete(Uri.parse(uri), null, null) > 0
|
val file = File(iconDir, filename)
|
||||||
|
val deleted = file.delete()
|
||||||
if (!deleted) {
|
if (!deleted) {
|
||||||
Log.w(TAG, "Unable to delete icon at $uri")
|
Log.w(TAG, "Unable to delete icon: $filename")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val uri = FileProvider.getUriForFile(applicationContext,
|
||||||
|
DownloadIconWorker.FILE_PROVIDER_AUTHORITY, file).toString()
|
||||||
repository.clearIconUri(uri)
|
repository.clearIconUri(uri)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.w(TAG, "Failed to delete icon: ${e.message}", e)
|
Log.w(TAG, "Failed to delete icon: ${e.message}", e)
|
||||||
|
|
Loading…
Reference in a new issue