diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt b/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt index ffd0b2ba..0e95d8f4 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/Fetch.kt @@ -9,6 +9,7 @@ import com.tonyodev.fetch2.util.DEFAULT_ENABLE_LISTENER_NOTIFY_ON_ATTACHED import com.tonyodev.fetch2.util.DEFAULT_INSTANCE_NAMESPACE import com.tonyodev.fetch2core.GLOBAL_FETCH_CONFIGURATION_NOT_SET import com.tonyodev.fetch2.util.createConfigWithNewNamespace +import com.tonyodev.fetch2core.DownloadBlock import com.tonyodev.fetch2core.Func import com.tonyodev.fetch2core.Func2 @@ -340,6 +341,16 @@ interface Fetch { * */ fun addCompletedDownloads(completedDownloads: List, func: Func>? = null, func2: Func? = null): Fetch + /** + * Gets the list of download blocks belonging to a download. List may be empty if + * blocks could not be found for the download id or download has never been processed. + * @param downloadId: Download ID + * @param func Callback the results will be returned on + * @throws FetchException if this instance of Fetch has been closed. + * @return Instance + * */ + fun getDownloadBlocks(downloadId: Int, func: Func>): Fetch + /** * Enable or disable logging. * @param enabled Enable or disable logging. diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/FetchExtentions.kt b/fetch2/src/main/java/com/tonyodev/fetch2/FetchExtentions.kt index 5d85d07e..4bda8057 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/FetchExtentions.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/FetchExtentions.kt @@ -1,6 +1,7 @@ package com.tonyodev.fetch2 import com.tonyodev.fetch2.exception.FetchException +import com.tonyodev.fetch2core.DownloadBlock import com.tonyodev.fetch2core.Func import com.tonyodev.fetch2core.Func2 @@ -230,4 +231,20 @@ fun Fetch.addCompletedDownload(completedDownloads: List, func }) } +/** + * Gets the list of download blocks belonging to a download. List may be empty if + * blocks could not be found for the download id or download has never been processed. + * @param downloadId: Download ID + * @param func Callback the results will be returned on + * @throws FetchException if this instance of Fetch has been closed. + * @return Instance + * */ +fun Fetch.getDownloadBlocks(downloadId: Int, func: (List) -> Unit): Fetch { + return getDownloadBlocks(downloadId, object : Func> { + override fun call(t: List) { + func(t) + } + }) +} + diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/DownloadManager.kt b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/DownloadManager.kt index d5c30326..784160ab 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/DownloadManager.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/DownloadManager.kt @@ -16,4 +16,5 @@ interface DownloadManager : Closeable { fun getDownloads(): List fun getNewFileDownloaderForDownload(download: Download): FileDownloader? fun getFileDownloaderDelegate(): FileDownloader.Delegate + fun getDownloadFileTempDir(download: Download): String } \ No newline at end of file diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/DownloadManagerImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/DownloadManagerImpl.kt index a92f9cd0..b758e9d0 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/DownloadManagerImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/DownloadManagerImpl.kt @@ -239,4 +239,15 @@ class DownloadManagerImpl(private val httpDownloader: Downloader, downloadBlockHandlerWrapper = downloadBlockHandlerWrapper) } + override fun getDownloadFileTempDir(download: Download): String { + val request = getRequestForDownload(download) + return if (fileServerDownloader != null && isFetchFileServerUrl(request.url)) { + fileServerDownloader.getDirectoryForFileDownloaderTypeParallel(request) + ?: fileTempDir + } else { + httpDownloader.getDirectoryForFileDownloaderTypeParallel(request) + ?: fileTempDir + } + } + } \ No newline at end of file diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/ParallelFileDownloaderImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/ParallelFileDownloaderImpl.kt index 35ae55c8..6442d692 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/downloader/ParallelFileDownloaderImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/downloader/ParallelFileDownloaderImpl.kt @@ -124,7 +124,6 @@ class ParallelFileDownloaderImpl(private val initialDownload: Download, } throwExceptionIfFound() completedDownload = true - deleteAllTempFiles() if (md5CheckingEnabled) { if (downloader.verifyContentMD5(openingResponse.request, openingResponse.md5)) { delegate?.onProgress( @@ -233,13 +232,13 @@ class ParallelFileDownloaderImpl(private val initialDownload: Download, if (!file.exists()) { deleteAllTempFiles() } - val previousSliceSize = getPreviousSliceCount(downloadInfo.id) + val previousSliceSize = getPreviousSliceCount(downloadInfo.id, fileTempDir) return if (acceptsRanges) { val fileSliceInfo = getChuckInfo(request) if (previousSliceSize != fileSliceInfo.slicingCount) { deleteAllTempFiles() } - saveCurrentSliceCount(downloadInfo.id, fileSliceInfo.slicingCount) + saveCurrentSliceCount(downloadInfo.id, fileSliceInfo.slicingCount, fileTempDir) var counterBytes = 0L val fileSlices = mutableListOf() for (position in 1..fileSliceInfo.slicingCount) { @@ -256,7 +255,7 @@ class ParallelFileDownloaderImpl(private val initialDownload: Download, position = position, startBytes = startBytes, endBytes = endBytes, - downloaded = getSavedDownloadedInfo(downloadInfo.id, position) + downloaded = getSavedDownloadedInfo(downloadInfo.id, position, fileTempDir) ) downloaded += fileSlice.downloaded fileSlices.add(fileSlice) @@ -269,101 +268,22 @@ class ParallelFileDownloaderImpl(private val initialDownload: Download, if (previousSliceSize != 1) { deleteAllTempFiles() } - saveCurrentSliceCount(downloadInfo.id, 1) + saveCurrentSliceCount(downloadInfo.id, 1, fileTempDir) val fileSlice = FileSlice( id = downloadInfo.id, position = 1, startBytes = 0, endBytes = total, - downloaded = getSavedDownloadedInfo(downloadInfo.id, 1)) + downloaded = getSavedDownloadedInfo(downloadInfo.id, 1, fileTempDir)) downloaded += fileSlice.downloaded listOf(fileSlice) } } - private fun getPreviousSliceCount(id: Int): Int { - var sliceCount = -1 - try { - sliceCount = getSingleLineTextFromFile(getMetaFilePath(id))?.toInt() ?: -1 - } catch (e: Exception) { - } - return sliceCount - } - - private fun saveCurrentSliceCount(id: Int, SliceCount: Int) { - try { - writeTextToFile(getMetaFilePath(id), SliceCount.toString()) - } catch (e: Exception) { - } - } - - private fun getMetaFilePath(id: Int): String { - return "$fileTempDir/$id.meta.txt" - } - private fun getChuckInfo(request: Downloader.ServerRequest): FileSliceInfo { val fileSliceSize = downloader.getFileSlicingCount(request, total) ?: DEFAULT_FILE_SLICE_NO_LIMIT_SET - return if (fileSliceSize == DEFAULT_FILE_SLICE_NO_LIMIT_SET) { - val fileSizeInMb = total.toFloat() / 1024F * 1024F - val fileSizeInGb = total.toFloat() / 1024F * 1024F * 1024F - when { - fileSizeInGb >= 1F -> { - val slices = 6 - val bytesPerSlice = ceil((total.toFloat() / slices.toFloat())).toLong() - FileSliceInfo(slices, bytesPerSlice) - } - fileSizeInMb >= 1F -> { - val slices = 4 - val bytesPerSlice = ceil((total.toFloat() / slices.toFloat())).toLong() - FileSliceInfo(slices, bytesPerSlice) - } - else -> FileSliceInfo(2, total) - } - } else { - val bytesPerSlice = ceil((total.toFloat() / fileSliceSize.toFloat())).toLong() - return FileSliceInfo(fileSliceSize, bytesPerSlice) - } - } - - private fun getDownloadedInfoFilePath(id: Int, position: Int): String { - return "$fileTempDir/$id.$position.txt" - } - - private fun deleteTempFile(id: Int, position: Int) { - try { - val textFile = getFile(getDownloadedInfoFilePath(id, position)) - if (textFile.exists()) { - textFile.delete() - } - } catch (e: Exception) { - } - } - - private fun deleteMetaFile(id: Int) { - try { - val textFile = getFile(getMetaFilePath(id)) - if (textFile.exists()) { - textFile.delete() - } - } catch (e: Exception) { - } - } - - private fun getSavedDownloadedInfo(id: Int, position: Int): Long { - var downloaded = 0L - try { - downloaded = getSingleLineTextFromFile(getDownloadedInfoFilePath(id, position))?.toLong() ?: 0L - } catch (e: Exception) { - } - return downloaded - } - - private fun saveDownloadedInfo(id: Int, position: Int, downloaded: Long) { - try { - writeTextToFile(getDownloadedInfoFilePath(id, position), downloaded.toString()) - } catch (e: Exception) { - } + return getFileSliceInfo(fileSliceSize, total) } private fun getAverageDownloadedBytesPerSecond(): Long { @@ -478,7 +398,7 @@ class ParallelFileDownloaderImpl(private val initialDownload: Download, val hasReportingTimeElapsed = hasIntervalTimeElapsed(reportingStartTime, reportingStopTime, DEFAULT_DOWNLOAD_SPEED_REPORTING_INTERVAL_IN_MILLISECONDS) if (hasReportingTimeElapsed) { - saveDownloadedInfo(fileSlice.id, fileSlice.position, fileSlice.downloaded) + saveDownloadedInfo(fileSlice.id, fileSlice.position, fileSlice.downloaded, fileTempDir) downloadBlock.downloadedBytes = fileSlice.downloaded delegate?.onDownloadBlockUpdated(downloadInfo, downloadBlock, totalDownloadBlocks) reportingStartTime = System.nanoTime() @@ -489,7 +409,7 @@ class ParallelFileDownloaderImpl(private val initialDownload: Download, } } } - saveDownloadedInfo(fileSlice.id, fileSlice.position, fileSlice.downloaded) + saveDownloadedInfo(fileSlice.id, fileSlice.position, fileSlice.downloaded, fileTempDir) downloadBlock.downloadedBytes = fileSlice.downloaded delegate?.onDownloadBlockUpdated(downloadInfo, downloadBlock, totalDownloadBlocks) } else if (downloadResponse == null && !interrupted && !terminated) { @@ -522,9 +442,9 @@ class ParallelFileDownloaderImpl(private val initialDownload: Download, private fun deleteAllTempFiles() { try { for (fileSlice in fileSlices) { - deleteTempFile(fileSlice.id, fileSlice.position) + deleteTempFile(fileSlice.id, fileSlice.position, fileTempDir) } - deleteMetaFile(downloadInfo.id) + deleteMetaFile(downloadInfo.id, fileTempDir) } catch (e: Exception) { } } @@ -549,18 +469,4 @@ class ParallelFileDownloaderImpl(private val initialDownload: Download, } } - data class FileSliceInfo(val slicingCount: Int, val bytesPerFileSlice: Long) - - data class FileSlice(val id: Int = 0, - val position: Int = 0, - val startBytes: Long = 0L, - val endBytes: Long = 0L, - var downloaded: Long = 0L) { - - val isDownloaded: Boolean - get() { - return startBytes + downloaded == endBytes - } - } - } \ No newline at end of file diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandler.kt b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandler.kt index 5cbd6d22..53a1f5ed 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandler.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandler.kt @@ -1,6 +1,7 @@ package com.tonyodev.fetch2.fetch import com.tonyodev.fetch2.* +import com.tonyodev.fetch2core.DownloadBlock import java.io.Closeable /** @@ -45,5 +46,6 @@ interface FetchHandler : Closeable { fun removeListener(listener: FetchListener) fun isDownloading(id: Int): Boolean fun cancelDownload(id: Int): Boolean + fun getDownloadBlocks(downloadId: Int): List } \ No newline at end of file diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt index aa3cebe0..c7829f10 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchHandlerImpl.kt @@ -547,6 +547,50 @@ class FetchHandlerImpl(private val namespace: String, return databaseManager.getDownloadsByRequestIdentifier(identifier) } + override fun getDownloadBlocks(downloadId: Int): List { + startPriorityQueueIfNotStarted() + val download = databaseManager.get(downloadId) + return if (download != null) { + val fileTempDir = downloadManager.getDownloadFileTempDir(download) + val fileSliceInfo = getFileSliceInfo(getPreviousSliceCount(download.id, fileTempDir), download.total) + when { + download.total < 1 -> listOf() + fileSliceInfo.slicingCount < 2 -> { + val downloadBlockInfo = DownloadBlockInfo() + downloadBlockInfo.downloadId = download.id + downloadBlockInfo.blockPosition = 1 + downloadBlockInfo.startByte = 0 + downloadBlockInfo.endByte = download.total + downloadBlockInfo.downloadedBytes = download.downloaded + listOf(downloadBlockInfo) + } + else -> { + var counterBytes = 0L + val downloadBlocksList = mutableListOf() + for (position in 1..fileSliceInfo.slicingCount) { + val startBytes = counterBytes + val endBytes = if (fileSliceInfo.slicingCount == position) { + download.total + } else { + counterBytes + fileSliceInfo.bytesPerFileSlice + } + counterBytes = endBytes + val downloadBlockInfo = DownloadBlockInfo() + downloadBlockInfo.downloadId = download.id + downloadBlockInfo.blockPosition = position + downloadBlockInfo.startByte = startBytes + downloadBlockInfo.endByte = endBytes + downloadBlockInfo.downloadedBytes = getSavedDownloadedInfo(download.id, position, fileTempDir) + downloadBlocksList.add(downloadBlockInfo) + } + downloadBlocksList + } + } + } else { + listOf() + } + } + override fun close() { if (isTerminating) { return diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt index bc3bedcc..b69d8c89 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/fetch/FetchImpl.kt @@ -7,10 +7,7 @@ import com.tonyodev.fetch2.exception.FetchException import com.tonyodev.fetch2.getErrorFromMessage import com.tonyodev.fetch2.fetch.FetchModulesBuilder.Modules import com.tonyodev.fetch2.util.DEFAULT_ENABLE_LISTENER_NOTIFY_ON_ATTACHED -import com.tonyodev.fetch2core.Func -import com.tonyodev.fetch2core.Func2 -import com.tonyodev.fetch2core.HandlerWrapper -import com.tonyodev.fetch2core.Logger +import com.tonyodev.fetch2core.* open class FetchImpl constructor(override val namespace: String, protected val handlerWrapper: HandlerWrapper, @@ -686,6 +683,23 @@ open class FetchImpl constructor(override val namespace: String, } } + override fun getDownloadBlocks(downloadId: Int, func: Func>): Fetch { + synchronized(lock) { + throwExceptionIfClosed() + handlerWrapper.post { + try { + val downloadBlocksList = fetchHandler.getDownloadBlocks(downloadId) + uiHandler.post { + func.call(downloadBlocksList) + } + } catch (e: FetchException) { + logger.e("Fetch with namespace $namespace error", e) + } + } + return this + } + } + override fun setGlobalNetworkType(networkType: NetworkType): Fetch { synchronized(lock) { throwExceptionIfClosed() diff --git a/fetch2/src/main/java/com/tonyodev/fetch2/util/FetchUtils.kt b/fetch2/src/main/java/com/tonyodev/fetch2/util/FetchUtils.kt index 36cad368..9565f730 100644 --- a/fetch2/src/main/java/com/tonyodev/fetch2/util/FetchUtils.kt +++ b/fetch2/src/main/java/com/tonyodev/fetch2/util/FetchUtils.kt @@ -3,11 +3,10 @@ package com.tonyodev.fetch2.util import com.tonyodev.fetch2.Download -import com.tonyodev.fetch2core.Downloader import com.tonyodev.fetch2.FetchConfiguration import com.tonyodev.fetch2.Status -import com.tonyodev.fetch2core.GET_REQUEST_METHOD -import com.tonyodev.fetch2core.getFile +import com.tonyodev.fetch2core.* +import kotlin.math.ceil fun canPauseDownload(download: Download): Boolean { return when (download.status) { @@ -81,6 +80,7 @@ fun deleteRequestTempFiles(fileTempDir: String, try { tempFile.delete() } catch (e: Exception) { + } } } @@ -90,6 +90,95 @@ fun deleteRequestTempFiles(fileTempDir: String, } } +fun getPreviousSliceCount(id: Int, fileTempDir: String): Int { + var sliceCount = -1 + try { + sliceCount = getSingleLineTextFromFile(getMetaFilePath(id, fileTempDir))?.toInt() ?: -1 + } catch (e: Exception) { + + } + return sliceCount +} + +fun getMetaFilePath(id: Int, fileTempDir: String): String { + return "$fileTempDir/$id.meta.txt" +} + +fun saveCurrentSliceCount(id: Int, SliceCount: Int, fileTempDir: String) { + try { + writeTextToFile(getMetaFilePath(id, fileTempDir), SliceCount.toString()) + } catch (e: Exception) { + + } +} + +fun getDownloadedInfoFilePath(id: Int, position: Int, fileTempDir: String): String { + return "$fileTempDir/$id.$position.txt" +} + +fun deleteTempFile(id: Int, position: Int, fileTempDir: String) { + try { + val textFile = getFile(getDownloadedInfoFilePath(id, position, fileTempDir)) + if (textFile.exists()) { + textFile.delete() + } + } catch (e: Exception) { + + } +} + +fun deleteMetaFile(id: Int, fileTempDir: String) { + try { + val textFile = getFile(getMetaFilePath(id, fileTempDir)) + if (textFile.exists()) { + textFile.delete() + } + } catch (e: Exception) { + + } +} + +fun getSavedDownloadedInfo(id: Int, position: Int, fileTempDir: String): Long { + var downloaded = 0L + try { + downloaded = getSingleLineTextFromFile(getDownloadedInfoFilePath(id, position, fileTempDir))?.toLong() ?: 0L + } catch (e: Exception) { + + } + return downloaded +} + +fun saveDownloadedInfo(id: Int, position: Int, downloaded: Long, fileTempDir: String) { + try { + writeTextToFile(getDownloadedInfoFilePath(id, position, fileTempDir), downloaded.toString()) + } catch (e: Exception) { + + } +} + +fun getFileSliceInfo(fileSliceSize: Int, totalBytes: Long): FileSliceInfo { + return if (fileSliceSize == DEFAULT_FILE_SLICE_NO_LIMIT_SET) { + val fileSizeInMb = totalBytes.toFloat() / 1024F * 1024F + val fileSizeInGb = totalBytes.toFloat() / 1024F * 1024F * 1024F + when { + fileSizeInGb >= 1F -> { + val slices = 6 + val bytesPerSlice = ceil((totalBytes.toFloat() / slices.toFloat())).toLong() + FileSliceInfo(slices, bytesPerSlice) + } + fileSizeInMb >= 1F -> { + val slices = 4 + val bytesPerSlice = ceil((totalBytes.toFloat() / slices.toFloat())).toLong() + FileSliceInfo(slices, bytesPerSlice) + } + else -> FileSliceInfo(2, totalBytes) + } + } else { + val bytesPerSlice = ceil((totalBytes.toFloat() / fileSliceSize.toFloat())).toLong() + return FileSliceInfo(fileSliceSize, bytesPerSlice) + } +} + fun createConfigWithNewNamespace(fetchConfiguration: FetchConfiguration, namespace: String): FetchConfiguration { return FetchConfiguration.Builder(fetchConfiguration.appContext) diff --git a/fetch2core/src/main/java/com/tonyodev/fetch2core/FileSlice.kt b/fetch2core/src/main/java/com/tonyodev/fetch2core/FileSlice.kt new file mode 100644 index 00000000..78ea784e --- /dev/null +++ b/fetch2core/src/main/java/com/tonyodev/fetch2core/FileSlice.kt @@ -0,0 +1,13 @@ +package com.tonyodev.fetch2core + +data class FileSlice(val id: Int = 0, + val position: Int = 0, + val startBytes: Long = 0L, + val endBytes: Long = 0L, + var downloaded: Long = 0L) { + + val isDownloaded: Boolean + get() { + return startBytes + downloaded == endBytes + } +} \ No newline at end of file diff --git a/fetch2core/src/main/java/com/tonyodev/fetch2core/FileSliceInfo.kt b/fetch2core/src/main/java/com/tonyodev/fetch2core/FileSliceInfo.kt new file mode 100644 index 00000000..994247e0 --- /dev/null +++ b/fetch2core/src/main/java/com/tonyodev/fetch2core/FileSliceInfo.kt @@ -0,0 +1,3 @@ +package com.tonyodev.fetch2core + +data class FileSliceInfo(val slicingCount: Int, val bytesPerFileSlice: Long) \ No newline at end of file diff --git a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt index eb5abfbf..f51fb0e1 100644 --- a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt +++ b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetch.kt @@ -6,6 +6,8 @@ import com.tonyodev.fetch2.exception.FetchException import com.tonyodev.fetch2.fetch.FetchModulesBuilder import com.tonyodev.fetch2.util.DEFAULT_INSTANCE_NAMESPACE import com.tonyodev.fetch2.Status +import com.tonyodev.fetch2core.DownloadBlock +import com.tonyodev.fetch2core.Func /** * A light weight file download manager for Android with Rx features. @@ -145,6 +147,16 @@ interface RxFetch : Fetch { * */ fun getDownloadsByRequestIdentifier(identifier: Long): Convertible> + /** + * Gets the list of download blocks belonging to a download. List may be empty if + * blocks could not be found for the download id or download has never been processed. + * @param downloadId: Download ID + * @throws FetchException if this instance of Fetch has been closed. + * @return A Convertible object that allows you to get the results as on observable or + * flowable + * */ + fun getDownloadBlocks(downloadId: Int): Convertible> + /** * RX Fetch implementation class. Use this Singleton to get instances of RxFetch or Fetch. * */ diff --git a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt index 10022ded..ee56d922 100644 --- a/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt +++ b/fetch2rx/src/main/java/com/tonyodev/fetch2rx/RxFetchImpl.kt @@ -6,12 +6,9 @@ import com.tonyodev.fetch2.exception.FetchException import com.tonyodev.fetch2.fetch.FetchHandler import com.tonyodev.fetch2.fetch.FetchImpl import com.tonyodev.fetch2.fetch.FetchModulesBuilder.Modules -import com.tonyodev.fetch2core.HandlerWrapper import com.tonyodev.fetch2.fetch.ListenerCoordinator -import com.tonyodev.fetch2core.FAILED_TO_ENQUEUE_REQUEST -import com.tonyodev.fetch2core.DOWNLOAD_NOT_FOUND import com.tonyodev.fetch2.Status -import com.tonyodev.fetch2core.Logger +import com.tonyodev.fetch2core.* import io.reactivex.Flowable import io.reactivex.android.schedulers.AndroidSchedulers @@ -265,6 +262,21 @@ open class RxFetchImpl(namespace: String, } } + override fun getDownloadBlocks(downloadId: Int): Convertible> { + synchronized(lock) { + throwExceptionIfClosed() + val flowable = Flowable.just(downloadId) + .subscribeOn(scheduler) + .flatMap { + throwExceptionIfClosed() + val downloads = fetchHandler.getDownloadBlocks(downloadId) + Flowable.just(downloads) + } + .observeOn(uiSceduler) + return Convertible(flowable) + } + } + companion object { @JvmStatic