Download part removed after no updates for 10 seconds, still need to fix the UI

This commit is contained in:
Nicholas Wallace
2025-11-02 18:29:09 -07:00
parent 3fd452dd53
commit 4e26179427
2 changed files with 89 additions and 50 deletions
@@ -127,6 +127,11 @@ class DownloadItemManager(
val internalProgressCallback = val internalProgressCallback =
object : InternalProgressCallback { object : InternalProgressCallback {
override fun onProgress(totalBytesWritten: Long, progress: Long) { override fun onProgress(totalBytesWritten: Long, progress: Long) {
// Store time progress was last changed to prevent stale downloads from getting
// stuck in an infinite loop
if (downloadItemPart.bytesDownloaded != totalBytesWritten) {
downloadItemPart.lastUpdateTime = System.currentTimeMillis()
}
downloadItemPart.bytesDownloaded = totalBytesWritten downloadItemPart.bytesDownloaded = totalBytesWritten
downloadItemPart.progress = progress downloadItemPart.progress = progress
} }
@@ -215,6 +220,21 @@ class DownloadItemManager(
downloadItem?.let { checkDownloadItemFinished(it) } downloadItem?.let { checkDownloadItemFinished(it) }
} }
currentDownloadItemParts.remove(downloadItemPart) currentDownloadItemParts.remove(downloadItemPart)
} else {
// Check for stalled downloads
val currentTimeMillis = System.currentTimeMillis()
val lastUpdateTime = downloadItemPart.lastUpdateTime ?: currentTimeMillis
val timeSinceLastUpdate = currentTimeMillis - lastUpdateTime
val stallTimeoutMillis = 10 * 1000 // 10 seconds
if (timeSinceLastUpdate > stallTimeoutMillis) {
Log.w(
tag,
"Download stalled for ${downloadItemPart.filename}, removing from download queue."
)
downloadItemPart.failed = true
clientEventEmitter.onDownloadItemPartUpdate(downloadItemPart)
currentDownloadItemParts.remove(downloadItemPart)
}
} }
} }
@@ -33,11 +33,24 @@ data class DownloadItemPart(
@JsonIgnore val finalDestinationUri: Uri, @JsonIgnore val finalDestinationUri: Uri,
val finalDestinationSubfolder: String, val finalDestinationSubfolder: String,
var downloadId: Long?, var downloadId: Long?,
var lastUpdateTime: Long?,
var progress: Long, var progress: Long,
var bytesDownloaded: Long var bytesDownloaded: Long
) { ) {
companion object { companion object {
fun make(downloadItemId:String, filename:String, fileSize: Long, destinationFile: File, finalDestinationFile: File, subfolder:String, serverPath:String, localFolder: LocalFolder, ebookFile: EBookFile?, audioTrack: AudioTrack?, episode: PodcastEpisode?) :DownloadItemPart { fun make(
downloadItemId: String,
filename: String,
fileSize: Long,
destinationFile: File,
finalDestinationFile: File,
subfolder: String,
serverPath: String,
localFolder: LocalFolder,
ebookFile: EBookFile?,
audioTrack: AudioTrack?,
episode: PodcastEpisode?
): DownloadItemPart {
val destinationUri = Uri.fromFile(destinationFile) val destinationUri = Uri.fromFile(destinationFile)
val finalDestinationUri = Uri.fromFile(finalDestinationFile) val finalDestinationUri = Uri.fromFile(finalDestinationFile)
@@ -47,7 +60,10 @@ data class DownloadItemPart(
} }
val downloadUri = Uri.parse(downloadUrl) val downloadUri = Uri.parse(downloadUrl)
Log.d("DownloadItemPart", "Audio File Destination Uri: $destinationUri | Final Destination Uri: $finalDestinationUri | Download URI $downloadUri") Log.d(
"DownloadItemPart",
"Audio File Destination Uri: $destinationUri | Final Destination Uri: $finalDestinationUri | Download URI $downloadUri"
)
return DownloadItemPart( return DownloadItemPart(
id = DeviceManager.getBase64Id(finalDestinationFile.absolutePath), id = DeviceManager.getBase64Id(finalDestinationFile.absolutePath),
downloadItemId, downloadItemId,
@@ -70,6 +86,7 @@ data class DownloadItemPart(
finalDestinationUri = finalDestinationUri, finalDestinationUri = finalDestinationUri,
finalDestinationSubfolder = subfolder, finalDestinationSubfolder = subfolder,
downloadId = null, downloadId = null,
lastUpdateTime = null,
progress = 0, progress = 0,
bytesDownloaded = 0 bytesDownloaded = 0
) )
@@ -77,10 +94,12 @@ data class DownloadItemPart(
} }
@get:JsonIgnore @get:JsonIgnore
val isInternalStorage get() = localFolderId.startsWith("internal-") val isInternalStorage
get() = localFolderId.startsWith("internal-")
@get:JsonIgnore @get:JsonIgnore
val serverUrl get() = uri.toString() val serverUrl
get() = uri.toString()
@JsonIgnore @JsonIgnore
fun getDownloadRequest(): DownloadManager.Request { fun getDownloadRequest(): DownloadManager.Request {