Continue supporting old APIs.

This commit is contained in:
onurays 2020-04-14 16:29:11 +03:00
parent 190fbb95ec
commit 667b371653

View file

@ -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")