Skip to content

Commit

Permalink
download manger crashes fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Jul 24, 2018
1 parent 63d4a19 commit 02ee414
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ open class FetchFileServerDownloader @JvmOverloads constructor(
}

override fun getRequestSupportedFileDownloaderTypes(request: Downloader.ServerRequest): Set<Downloader.FileDownloaderType> {
return getRequestSupportedFileDownloaderTypes(request, this)
return try {
getRequestSupportedFileDownloaderTypes(request, this)
} catch (e: Exception) {
mutableSetOf(fileDownloaderType)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ open class HttpUrlConnectionDownloader @JvmOverloads constructor(
}

override fun getRequestSupportedFileDownloaderTypes(request: Downloader.ServerRequest): Set<Downloader.FileDownloaderType> {
return getRequestSupportedFileDownloaderTypes(request, this)
return try {
getRequestSupportedFileDownloaderTypes(request, this)
} catch (e: Exception) {
mutableSetOf(fileDownloaderType)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,46 @@ class DownloadManagerImpl(private val httpDownloader: Downloader,
val downloadExecutor = executor
if (downloadExecutor != null && !downloadExecutor.isShutdown) {
downloadExecutor.execute {
val fileDownloader = getNewFileDownloaderForDownload(download)
val runDownload = synchronized(lock) {
if (currentDownloadsMap.containsKey(download.id)) {
fileDownloader.delegate = getFileDownloaderDelegate()
currentDownloadsMap[download.id] = fileDownloader
downloadManagerCoordinator.addFileDownloader(download.id, fileDownloader)
logger.d("DownloadManager starting download $download")
true
} else {
false
try {
val fileDownloader = getNewFileDownloaderForDownload(download)
val runDownload = synchronized(lock) {
if (currentDownloadsMap.containsKey(download.id)) {
fileDownloader.delegate = getFileDownloaderDelegate()
currentDownloadsMap[download.id] = fileDownloader
downloadManagerCoordinator.addFileDownloader(download.id, fileDownloader)
logger.d("DownloadManager starting download $download")
true
} else {
false
}
}
}
if (runDownload) {
fileDownloader.run()
}
synchronized(lock) {
if (currentDownloadsMap.containsKey(download.id)) {
currentDownloadsMap.remove(download.id)
downloadCounter -= 1
if (runDownload) {
fileDownloader.run()
}
downloadManagerCoordinator.removeFileDownloader(download.id)
removeDownloadMappings(download)
} catch (e: Exception) {

} finally {
removeDownloadMappings(download)
}
}
true
return true
} else {
false
}
}
}

private fun removeDownloadMappings(download: Download) {
synchronized(lock) {
if (currentDownloadsMap.containsKey(download.id)) {
currentDownloadsMap.remove(download.id)
downloadCounter -= 1
}
downloadManagerCoordinator.removeFileDownloader(download.id)
}
}

override fun cancel(downloadId: Int): Boolean {
return synchronized(lock) {
cancelDownloadNoLock(downloadId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ open class OkHttpDownloader @JvmOverloads constructor(
}

override fun getRequestSupportedFileDownloaderTypes(request: Downloader.ServerRequest): Set<Downloader.FileDownloaderType> {
return getRequestSupportedFileDownloaderTypes(request, this)
return try {
getRequestSupportedFileDownloaderTypes(request, this)
} catch (e: Exception) {
mutableSetOf(fileDownloaderType)
}
}

}

0 comments on commit 02ee414

Please sign in to comment.