Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fcce44be authored by Hunter Kehoe's avatar Hunter Kehoe
Browse files

clean up old db queries and DownloadIconWorker

parent 80cc0118
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -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)

+0 −4
Original line number Diff line number Diff line
@@ -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)
    }
+10 −7
Original line number Diff line number Diff line
@@ -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()) {