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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ dependencies {
smallIcon = smallIcon, //It is required
showSpeed = true, //Default: true
showSize = true, //Default: true
showTime = true //Default: true
showTime = true, //Default: true
showCancelAction = true, //Default: true
showPauseAction = true, //Default: true
showResumeAction = true, //Default: true
showRetryAction= true, //Default: true
)
).build(this)
```
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/khush/sample/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ class MainFragment : Fragment() {
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path,
fileName = "Sample_Video_1.mp4",
tag = "Video",
metaData = "158"
metaData = "158",
customTitle = "Video 1 File"
)
}

Expand Down Expand Up @@ -232,7 +233,7 @@ class MainFragment : Fragment() {
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path,
fileName = "Sample_Pdf_1.pdf",
tag = "Document",
metaData = "5"
metaData = "5",
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion ketch/src/main/java/com/ketch/DownloadModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ data class DownloadModel(
val lastModified: Long,
val eTag: String,
val metaData: String,
val failureReason: String
val failureReason: String,
)
26 changes: 21 additions & 5 deletions ketch/src/main/java/com/ketch/Ketch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,14 @@ class Ketch private constructor(
if (!::okHttpClient.isInitialized) {
okHttpClient = OkHttpClient
.Builder()
.connectTimeout(downloadConfig.connectTimeOutInMs, TimeUnit.MILLISECONDS)
.readTimeout(downloadConfig.readTimeOutInMs, TimeUnit.MILLISECONDS)
.connectTimeout(
downloadConfig.connectTimeOutInMs,
TimeUnit.MILLISECONDS
)
.readTimeout(
downloadConfig.readTimeOutInMs,
TimeUnit.MILLISECONDS
)
.build()
}

Expand Down Expand Up @@ -183,6 +189,7 @@ class Ketch private constructor(
* @param metaData Optional metaData set for adding any extra download info
* @param headers Optional headers sent when making api call for file download
* @param supportPauseResume Optional flag to enable pause and resume functionality
* @param customTitle Name of the tile that show in notification progress
* @return Unique Download ID associated with current download
*/
fun download(
Expand All @@ -193,6 +200,7 @@ class Ketch private constructor(
metaData: String = "",
headers: HashMap<String, String> = hashMapOf(),
supportPauseResume: Boolean = true,
customTitle: String? = null,
): Int {
val downloadRequest = prepareDownloadRequest(
url = url,
Expand All @@ -202,6 +210,7 @@ class Ketch private constructor(
headers = headers,
metaData = metaData,
supportPauseResume = supportPauseResume,
customTitle = customTitle
)
downloadManager.downloadAsync(downloadRequest)
return downloadRequest.id
Expand All @@ -226,6 +235,7 @@ class Ketch private constructor(
metaData: String = "",
headers: HashMap<String, String> = hashMapOf(),
supportPauseResume: Boolean = true,
customTitle: String?
): Int {
val downloadRequest = mutex.withLock {
prepareDownloadRequest(
Expand All @@ -236,6 +246,7 @@ class Ketch private constructor(
headers = headers,
metaData = metaData,
supportPauseResume = supportPauseResume,
customTitle = customTitle
)
}
downloadManager.download(downloadRequest)
Expand Down Expand Up @@ -517,15 +528,17 @@ class Ketch private constructor(
* @param status
* @return [DownloadModel] if present else null
*/
suspend fun getDownloadModelByStatus(status: Status) = downloadManager.getDownloadModelByStatus(status)
suspend fun getDownloadModelByStatus(status: Status) =
downloadManager.getDownloadModelByStatus(status)

/**
* Suspend function to get download model by list of tags
*
* @param tags
* @return List of [DownloadModel]
*/
suspend fun getDownloadModelByTags(tags: List<String>) = downloadManager.getDownloadModelByTags(tags)
suspend fun getDownloadModelByTags(tags: List<String>) =
downloadManager.getDownloadModelByTags(tags)

/**
* Suspend function to get download model by list of ids
Expand All @@ -541,7 +554,8 @@ class Ketch private constructor(
* @param statuses
* @return List of [DownloadModel]
*/
suspend fun getDownloadModelByStatuses(statuses: List<Status>) = downloadManager.getDownloadModelByStatuses(statuses)
suspend fun getDownloadModelByStatuses(statuses: List<Status>) =
downloadManager.getDownloadModelByStatuses(statuses)

private fun prepareDownloadRequest(
url: String,
Expand All @@ -551,6 +565,7 @@ class Ketch private constructor(
headers: HashMap<String, String>,
metaData: String,
supportPauseResume: Boolean,
customTitle: String?,
): DownloadRequest {
require(url.isNotEmpty() && path.isNotEmpty() && fileName.isNotEmpty()) {
"Missing ${if (url.isEmpty()) "url" else if (path.isEmpty()) "path" else "fileName"}"
Expand All @@ -569,6 +584,7 @@ class Ketch private constructor(
headers = headers,
metaData = metaData,
supportPauseResume = supportPauseResume,
customTitle = customTitle
)

return downloadRequest
Expand Down
6 changes: 5 additions & 1 deletion ketch/src/main/java/com/ketch/NotificationConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ data class NotificationConfig(
val showSpeed: Boolean = true,
val showSize: Boolean = true,
val showTime: Boolean = true,
val smallIcon: Int
val smallIcon: Int,
val showCancelAction: Boolean = true,
val showPauseAction: Boolean = true,
val showResumeAction: Boolean = true,
val showRetryAction: Boolean = true,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.ketch.internal.database
import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [DownloadEntity::class], version = 1)
@Database(entities = [DownloadEntity::class], version = 2)
internal abstract class DownloadDatabase : RoomDatabase() {
abstract fun downloadDao(): DownloadDao
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ internal data class DownloadEntity(
var eTag: String = "",
var userAction: String = UserAction.DEFAULT.toString(),
var metaData: String = "",
var failureReason: String = ""
var failureReason: String = "",
var customTitle: String?,
)
48 changes: 32 additions & 16 deletions ketch/src/main/java/com/ketch/internal/download/DownloadManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ internal class DownloadManager(
msg = "Download in Progress. FileName: ${downloadEntity?.fileName}, " +
"ID: ${downloadEntity?.id}, " +
"Size in bytes: ${downloadEntity?.totalBytes}, " +
"downloadPercent: ${if (downloadEntity != null && downloadEntity.totalBytes.toInt() != 0) {
((downloadEntity.downloadedBytes * 100) / downloadEntity.totalBytes).toInt()
} else {
0
}}%, " +
"downloadPercent: ${
if (downloadEntity != null && downloadEntity.totalBytes.toInt() != 0) {
((downloadEntity.downloadedBytes * 100) / downloadEntity.totalBytes).toInt()
} else {
0
}
}%, " +
"downloadSpeedInBytesPerMilliSeconds: ${downloadEntity?.speedInBytePerMs} b/ms"
)

Expand Down Expand Up @@ -190,7 +192,8 @@ internal class DownloadManager(
uuid = downloadWorkRequest.id.toString(),
lastModified = System.currentTimeMillis(),
userAction = UserAction.START.toString(),
metaData = downloadRequest.metaData
metaData = downloadRequest.metaData,
customTitle = downloadRequest.customTitle
)
)
}
Expand Down Expand Up @@ -219,7 +222,8 @@ internal class DownloadManager(
tag = downloadEntity.tag,
id = downloadEntity.id,
headers = WorkUtil.jsonToHashMap(downloadEntity.headersJson),
metaData = downloadEntity.metaData
metaData = downloadEntity.metaData,
customTitle = downloadEntity.customTitle,
)
)
}
Expand Down Expand Up @@ -247,7 +251,8 @@ internal class DownloadManager(
context = context,
notificationConfig = notificationConfig,
requestId = id,
fileName = downloadEntity.fileName
fileName = downloadEntity.fileName,
customTitle = downloadEntity.customTitle
).sendDownloadCancelledNotification()
}
}
Expand Down Expand Up @@ -284,7 +289,8 @@ internal class DownloadManager(
tag = downloadEntity.tag,
id = downloadEntity.id,
headers = WorkUtil.jsonToHashMap(downloadEntity.headersJson),
metaData = downloadEntity.metaData
metaData = downloadEntity.metaData,
customTitle = downloadEntity.customTitle
)
)
}
Expand Down Expand Up @@ -417,7 +423,10 @@ internal class DownloadManager(
}
downloadDao.remove(it.id)
removeNotification(context, it.id) // In progress notification
removeNotification(context, it.id + 1) // Cancelled, Paused, Failed, Success notification
removeNotification(
context,
it.id + 1
) // Cancelled, Paused, Failed, Success notification
}
}
}
Expand All @@ -433,7 +442,10 @@ internal class DownloadManager(
deleteFileIfExists(path, fileName)
}
removeNotification(context, it.id) // In progress notification
removeNotification(context, it.id + 1) // Cancelled, Paused, Failed, Success notification
removeNotification(
context,
it.id + 1
) // Cancelled, Paused, Failed, Success notification
}
downloadDao.deleteAll()
}
Expand All @@ -451,7 +463,10 @@ internal class DownloadManager(
}
downloadDao.remove(it.id)
removeNotification(context, it.id) // In progress notification
removeNotification(context, it.id + 1) // Cancelled, Paused, Failed, Success notification
removeNotification(
context,
it.id + 1
) // Cancelled, Paused, Failed, Success notification
}
}
}
Expand Down Expand Up @@ -485,11 +500,12 @@ internal class DownloadManager(
}

fun observeDownloadsByStatus(status: Status): Flow<List<DownloadModel>> {
return downloadDao.getAllEntityByStatusFlow(status.name).distinctUntilChanged().map { entityList ->
entityList.map { entity ->
entity.toDownloadModel()
return downloadDao.getAllEntityByStatusFlow(status.name).distinctUntilChanged()
.map { entityList ->
entityList.map { entity ->
entity.toDownloadModel()
}
}
}
}

fun observeDownloadsByIds(ids: List<Int>): Flow<List<DownloadModel?>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal data class DownloadRequest(
val url: String,
val path: String,
val fileName: String,
val customTitle: String?,
val tag: String,
val id: Int = getUniqueId(url, path, fileName),
val headers: HashMap<String, String> = hashMapOf(),
Expand Down
Loading