Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions file-download/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
38 changes: 38 additions & 0 deletions file-download/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apply from: "../android-configs/lib-config.gradle"

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'

implementation 'androidx.appcompat:appcompat:1.4.1'

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android")

def coroutinesVersion = "1.3.7"

constraints {
implementation("androidx.core:core-ktx") {
version {
require("1.2.0")
}
}
implementation("androidx.appcompat:appcompat") {
version {
require("1.0.2")
}
}
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core") {
version {
require(coroutinesVersion)
}
}
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android") {
version {
require(coroutinesVersion)
}
}
}

}
2 changes: 2 additions & 0 deletions file-download/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.file_download"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.touchin.roboswag.file_download

interface DownloadFileManager {

suspend fun download(fileName: String, fileUri: String, headers: Map<String, String> = emptyMap()): String

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package ru.touchin.roboswag.file_download

import android.annotation.SuppressLint
import android.app.DownloadManager
import android.content.Context
import android.content.Context.DOWNLOAD_SERVICE
import android.database.Cursor
import android.net.Uri
import android.os.Environment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import java.io.File

class DownloadFileManagerImpl(
private val context: Context
) : DownloadFileManager {

private val downloadManager: DownloadManager = context.getSystemService(DOWNLOAD_SERVICE) as DownloadManager

override suspend fun download(fileName: String, fileUri: String, headers: Map<String, String>): String {
val result = withContext(Dispatchers.Default) {
async {
downloadWithStatus(fileName, fileUri, headers)
}
}
return result.await()

}

private fun downloadWithStatus(fileName: String, fileUri: String, headers: Map<String, String>): String {
var downloadId: Long
DownloadManager.Request(Uri.parse(fileUri))
.apply {
setTitle(fileName)
setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)

headers.forEach { (key, value) ->
addRequestHeader(key, value)
}

allowScanningByMediaScanner()
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
downloadId = downloadManager.enqueue(this)
}
return getStatus(downloadId, fileUri)

}

@SuppressLint("Range")
private fun getStatus(id: Long, url: String): String {
val query = DownloadManager.Query().setFilterById(id)
var status: Int
var downloading = true
do {
val cursor: Cursor = downloadManager.query(query)
cursor.moveToFirst()
val statusId = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
if (isFinished(statusId)) {
downloading = false
}
status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
cursor.close()
} while (downloading)

return statusMessage(url, File(Environment.DIRECTORY_DOWNLOADS), status)

}

private fun statusMessage(url: String, directory: File, status: Int): String {
val msg = when (status) {
DownloadManager.STATUS_FAILED -> "Download has been failed, please try again"
DownloadManager.STATUS_PAUSED -> "Paused"
DownloadManager.STATUS_PENDING -> "Pending"
DownloadManager.STATUS_RUNNING -> "Downloading..."
DownloadManager.STATUS_SUCCESSFUL -> "PDF downloaded successfully in $directory" + File.separator + url.substring(
url.lastIndexOf("/") + 1
)
else -> "There's nothing to download"
}
return msg

}

private fun isFinished(status: Int): Boolean {
return status == DownloadManager.STATUS_SUCCESSFUL
|| status == DownloadManager.STATUS_FAILED

}

}