clean up old db queries and DownloadIconWorker

This commit is contained in:
Hunter Kehoe 2022-09-05 16:52:26 -06:00
parent 80cc011852
commit fcce44bef1
3 changed files with 10 additions and 14 deletions

View file

@ -383,9 +383,6 @@ interface NotificationDao {
@Query("SELECT DISTINCT icon_contentUri FROM notification WHERE deleted != 1 AND icon_contentUri <> ''")
fun listActiveIconUris(): List<String>
@Query("SELECT DISTINCT icon_contentUri FROM notification WHERE deleted = 1 AND icon_contentUri <> ''")
fun listDeletedIconUris(): List<String>
@Query("UPDATE notification SET icon_contentUri = null WHERE icon_contentUri = :uri")
fun clearIconUri(uri: String)

View file

@ -96,10 +96,6 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas
return notificationDao.listActiveIconUris().toSet()
}
fun getDeletedIconUris(): Set<String> {
return notificationDao.listDeletedIconUris().toSet()
}
fun clearIconUri(uri: String) {
notificationDao.clearIconUri(uri)
}

View file

@ -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()) {