Continue supporting old APIs.

This commit is contained in:
onurays 2020-04-14 16:29:11 +03:00
parent 190fbb95ec
commit 667b371653
1 changed files with 17 additions and 9 deletions

View File

@ -16,8 +16,10 @@
package im.vector.riotx.core.files package im.vector.riotx.core.files
import android.app.DownloadManager
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.os.Build
import android.provider.MediaStore import android.provider.MediaStore
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import arrow.core.Try import arrow.core.Try
@ -56,6 +58,7 @@ fun addEntryToDownloadManager(context: Context,
title: String = file.name, title: String = file.name,
description: String = file.name) { description: String = file.name) {
try { try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put(MediaStore.Downloads.TITLE, title) put(MediaStore.Downloads.TITLE, title)
put(MediaStore.Downloads.DISPLAY_NAME, description) put(MediaStore.Downloads.DISPLAY_NAME, description)
@ -67,6 +70,11 @@ fun addEntryToDownloadManager(context: Context,
outputStream.sink().buffer().write(file.inputStream().use { it.readBytes() }) 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) { } catch (e: Exception) {
Timber.e(e, "## addEntryToDownloadManager(): Exception") Timber.e(e, "## addEntryToDownloadManager(): Exception")
} }