From fcce44bef109075d8cab59131837633293559e67 Mon Sep 17 00:00:00 2001 From: Hunter Kehoe Date: Mon, 5 Sep 2022 16:52:26 -0600 Subject: [PATCH] clean up old db queries and DownloadIconWorker --- app/src/main/java/io/heckel/ntfy/db/Database.kt | 3 --- .../main/java/io/heckel/ntfy/db/Repository.kt | 4 ---- .../io/heckel/ntfy/msg/DownloadIconWorker.kt | 17 ++++++++++------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/db/Database.kt b/app/src/main/java/io/heckel/ntfy/db/Database.kt index 3ab4ca5..62daf0a 100644 --- a/app/src/main/java/io/heckel/ntfy/db/Database.kt +++ b/app/src/main/java/io/heckel/ntfy/db/Database.kt @@ -383,9 +383,6 @@ interface NotificationDao { @Query("SELECT DISTINCT icon_contentUri FROM notification WHERE deleted != 1 AND icon_contentUri <> ''") fun listActiveIconUris(): List - @Query("SELECT DISTINCT icon_contentUri FROM notification WHERE deleted = 1 AND icon_contentUri <> ''") - fun listDeletedIconUris(): List - @Query("UPDATE notification SET icon_contentUri = null WHERE icon_contentUri = :uri") fun clearIconUri(uri: String) diff --git a/app/src/main/java/io/heckel/ntfy/db/Repository.kt b/app/src/main/java/io/heckel/ntfy/db/Repository.kt index a39f617..cee2985 100644 --- a/app/src/main/java/io/heckel/ntfy/db/Repository.kt +++ b/app/src/main/java/io/heckel/ntfy/db/Repository.kt @@ -96,10 +96,6 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas return notificationDao.listActiveIconUris().toSet() } - fun getDeletedIconUris(): Set { - return notificationDao.listDeletedIconUris().toSet() - } - fun clearIconUri(uri: String) { notificationDao.clearIconUri(uri) } diff --git a/app/src/main/java/io/heckel/ntfy/msg/DownloadIconWorker.kt b/app/src/main/java/io/heckel/ntfy/msg/DownloadIconWorker.kt index 9242743..9da973c 100644 --- a/app/src/main/java/io/heckel/ntfy/msg/DownloadIconWorker.kt +++ b/app/src/main/java/io/heckel/ntfy/msg/DownloadIconWorker.kt @@ -90,14 +90,9 @@ class DownloadIconWorker(private val context: Context, params: WorkerParameters) this.uri = uri // Required for cleanup in onStopped() Log.d(TAG, "Starting download to content URI: $uri") - val contentLength = response.headers["Content-Length"]?.toLongOrNull() var bytesCopied: Long = 0 val outFile = resolver.openOutputStream(uri) ?: throw Exception("Cannot open output stream") - val downloadLimit = if (repository.getAutoDownloadMaxSize() != Repository.AUTO_DOWNLOAD_NEVER && repository.getAutoDownloadMaxSize() != Repository.AUTO_DOWNLOAD_ALWAYS) { - repository.getAutoDownloadMaxSize() - } else { - MAX_ICON_DOWNLOAD_SIZE.toLong() - } + val downloadLimit = getDownloadLimit() outFile.use { fileOut -> val fileIn = response.body!!.byteStream() val buffer = ByteArray(BUFFER_SIZE) @@ -144,11 +139,19 @@ class DownloadIconWorker(private val context: Context, params: WorkerParameters) } private fun shouldAbortDownload(response: Response): Boolean { - val maxAutoDownloadSize = MAX_ICON_DOWNLOAD_SIZE + val maxAutoDownloadSize = getDownloadLimit() val size = response.headers["Content-Length"]?.toLongOrNull() ?: return false // Don't abort here if size unknown return size > maxAutoDownloadSize } + private fun getDownloadLimit(): Long { + return if (repository.getAutoDownloadMaxSize() != Repository.AUTO_DOWNLOAD_NEVER && repository.getAutoDownloadMaxSize() != Repository.AUTO_DOWNLOAD_ALWAYS) { + repository.getAutoDownloadMaxSize() + } else { + MAX_ICON_DOWNLOAD_SIZE.toLong() + } + } + private fun createIconFile(icon: Icon): File { val iconDir = File(context.cacheDir, ICON_CACHE_DIR) if (!iconDir.exists() && !iconDir.mkdirs()) {