From 667b3716534b6aeb590c4504de6f1a93f1fa686d Mon Sep 17 00:00:00 2001 From: onurays Date: Tue, 14 Apr 2020 16:29:11 +0300 Subject: [PATCH] Continue supporting old APIs. --- .../im/vector/riotx/core/files/FileSaver.kt | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/core/files/FileSaver.kt b/vector/src/main/java/im/vector/riotx/core/files/FileSaver.kt index 1b9eca984c..f978e20ca9 100644 --- a/vector/src/main/java/im/vector/riotx/core/files/FileSaver.kt +++ b/vector/src/main/java/im/vector/riotx/core/files/FileSaver.kt @@ -16,8 +16,10 @@ package im.vector.riotx.core.files +import android.app.DownloadManager import android.content.ContentValues import android.content.Context +import android.os.Build import android.provider.MediaStore import androidx.annotation.WorkerThread import arrow.core.Try @@ -56,16 +58,22 @@ fun addEntryToDownloadManager(context: Context, title: String = file.name, description: String = file.name) { try { - val contentValues = ContentValues().apply { - put(MediaStore.Downloads.TITLE, title) - put(MediaStore.Downloads.DISPLAY_NAME, description) - put(MediaStore.Downloads.MIME_TYPE, mimeType) - put(MediaStore.Downloads.SIZE, file.length()) - } - context.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)?.let { uri -> - context.contentResolver.openOutputStream(uri)?.use { outputStream -> - outputStream.sink().buffer().write(file.inputStream().use { it.readBytes() }) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val contentValues = ContentValues().apply { + put(MediaStore.Downloads.TITLE, title) + put(MediaStore.Downloads.DISPLAY_NAME, description) + put(MediaStore.Downloads.MIME_TYPE, mimeType) + put(MediaStore.Downloads.SIZE, file.length()) } + context.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)?.let { uri -> + context.contentResolver.openOutputStream(uri)?.use { outputStream -> + outputStream.sink().buffer().write(file.inputStream().use { it.readBytes() }) + } + } + } else { + val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager? + @Suppress("DEPRECATION") + downloadManager?.addCompletedDownload(title, description, true, mimeType, file.absolutePath, file.length(), true) } } catch (e: Exception) { Timber.e(e, "## addEntryToDownloadManager(): Exception")