Skip to content

Commit

Permalink
✨ [Android] 补充断点下载功能
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie-yellow committed Oct 9, 2023
1 parent 895e818 commit e1a6310
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ class JmmController(
)
}

suspend fun updateDownloadState(downloadController: JmmDownloadController) {
suspend fun updateDownloadState(downloadController: JmmDownloadController, mmid: MMID) :Boolean {
val url = when (downloadController) {
JmmDownloadController.PAUSE -> "file://download.browser.dweb/pause"
JmmDownloadController.CANCEL -> "file://download.browser.dweb/cancel"
JmmDownloadController.RESUME -> "file://download.browser.dweb/resume"
JmmDownloadController.PAUSE -> "file://download.browser.dweb/pause?mmid=$mmid"
JmmDownloadController.CANCEL -> "file://download.browser.dweb/cancel?mmid=$mmid"
JmmDownloadController.RESUME -> "file://download.browser.dweb/resume?mmid=$mmid"
}
jmmNMM.nativeFetch(PureRequest(href = url, method = IpcMethod.GET))
return jmmNMM.nativeFetch(PureRequest(href = url, method = IpcMethod.GET)).boolean()
}

suspend fun closeSelf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class JmmManagerViewHelper(
// }
}

private fun initDownLoadStatusListener() {
private suspend fun initDownLoadStatusListener() {
jmmController.onDownload { downloadInfo ->
if (downloadInfo.id != uiState.jmmAppInstallManifest.id) return@onDownload
if (downloadInfo.downloadStatus == JmmDownloadStatus.IDLE) return@onDownload
Expand All @@ -112,10 +112,9 @@ class JmmManagerViewHelper(
uiState.downloadStatus.value = downloadInfo.downloadStatus
}
}
if (downloadInfo.downloadStatus == JmmDownloadStatus.INSTALLED) { // 移除监听列表
// downLoadObserver?.close()
// TODO 移除监听
}
/*if (downloadInfo.downloadStatus == JmmDownloadStatus.INSTALLED) { // 移除监听列表
}*/
}
}

Expand All @@ -131,11 +130,21 @@ class JmmManagerViewHelper(
}

JmmDownloadStatus.DownLoading -> {
jmmController.updateDownloadState(JmmDownloadController.PAUSE)
val success = jmmController.updateDownloadState(
JmmDownloadController.PAUSE, uiState.jmmAppInstallManifest.id
)
if (success) {
uiState.downloadStatus.value = JmmDownloadStatus.PAUSE
}
}

JmmDownloadStatus.PAUSE -> {
jmmController.updateDownloadState(JmmDownloadController.RESUME)
val success =jmmController.updateDownloadState(
JmmDownloadController.RESUME, uiState.jmmAppInstallManifest.id
)
if (success) {
uiState.downloadStatus.value = JmmDownloadStatus.DownLoading
}
}

/*DownloadStatus.DownLoading, DownloadStatus.PAUSE -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,22 @@ class DownloadNMM :
downloadModel.exists(ipc.remote.mmid)
},
"/pause" bind HttpMethod.Get to defineBooleanResponse {
debugDownload("pause", ipc.remote.mmid)
downloadModel.updateDownloadState(ipc.remote.mmid, JmmDownloadController.PAUSE)
val mmid = request.queryOrNull("mmid") ?: ipc.remote.mmid
debugDownload("pause", "ipc->${ipc.remote.mmid}, mmid->$mmid")
downloadModel.updateDownloadState(mmid, JmmDownloadController.PAUSE)
true
},
/**继续下载*/
"/resume" bind HttpMethod.Get to defineBooleanResponse {
debugDownload("resume", ipc.remote.mmid)
downloadModel.updateDownloadState(ipc.remote.mmid, JmmDownloadController.RESUME)
val mmid = request.queryOrNull("mmid") ?: ipc.remote.mmid
debugDownload("resume", "ipc->${ipc.remote.mmid}, mmid->$mmid")
downloadModel.updateDownloadState(mmid, JmmDownloadController.RESUME)
true
},
"/cancel" bind HttpMethod.Get to defineBooleanResponse {
debugDownload("cancel", ipc.remote.mmid)
downloadModel.updateDownloadState(ipc.remote.mmid, JmmDownloadController.CANCEL)
val mmid = request.queryOrNull("mmid") ?: ipc.remote.mmid
debugDownload("cancel", "ipc->${ipc.remote.mmid}, mmid->$mmid")
downloadModel.updateDownloadState(mmid, JmmDownloadController.CANCEL)
true
},
"/listen" bind HttpMethod.Get to definePureStreamHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.ktor.client.request.prepareGet
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.core.isNotEmpty
import io.ktor.utils.io.core.readBytes
import kotlinx.coroutines.delay
import org.dweb_browser.helper.platform.getKtorClientEngine
import java.io.File

Expand All @@ -27,9 +28,10 @@ object HttpDownload {
val contentLength = httpResponse.headers["content-length"]?.toLong() ?: downloadInfo.size
var currentLength = 0L
val channel: ByteReadChannel = httpResponse.body()
while (!channel.isClosedForRead && !isStop()) {
while (!channel.isClosedForRead) {
val packet = channel.readRemaining(limit = DEFAULT_BUFFER_SIZE.toLong())
while (packet.isNotEmpty && !isStop()) {
while (packet.isNotEmpty) {
while (isStop()) delay(200)
val bytes: ByteArray = packet.readBytes()
file.appendBytes(array = bytes)
currentLength += bytes.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class DownloadModel(val downloadNMM: DownloadNMM) {
private var notificationId = AtomicInteger(999)
}

fun findDownloadApp(mmid: MMID) = downloadAppMap[mmid]
fun exists(mmid: MMID) = downloadAppMap.containsKey(mmid)

private val ioAsyncScope = MainScope() + ioAsyncExceptionHandler // 用于全局的协程调用
Expand All @@ -76,25 +75,29 @@ class DownloadModel(val downloadNMM: DownloadNMM) {
val downloadInfo = jmm.toDownloadInfo(context)
downloadAppMap[jmm.id] = downloadInfo
HttpDownload.downloadAndSave(downloadInfo, isStop = {
// debugDownload("Downloading", "${jmm.id}, downloadStatus=${downloadInfo.downloadStatus}")
when (downloadInfo.downloadStatus) {
JmmDownloadStatus.CANCEL, JmmDownloadStatus.FAIL, JmmDownloadStatus.PAUSE -> true
JmmDownloadStatus.CANCEL, JmmDownloadStatus.FAIL -> {
// 如果是cancel和fail,移除当前下载
downloadAppMap.remove(jmm.id)
downloadingMap.remove(jmm.id)
true
}
JmmDownloadStatus.PAUSE -> true
else -> false
}
}) { current, total ->
debugDownload("Downloading", "current=$current, total=$total")
// debugDownload("Downloading", "current=$current, total=$total")
ioAsyncScope.launch {
downloadInfo.callDownLoadProgress(context, current, total)
}
}
return true
}

internal suspend fun breakDownloadApp() {

}

internal fun updateDownloadState(mmid: MMID, event: JmmDownloadController) {
downloadAppMap[mmid]?.apply {
debugDownload("updateDownloadState", "event=$event, mmid=$mmid")
downloadStatus = when (event) {
JmmDownloadController.CANCEL -> JmmDownloadStatus.CANCEL
JmmDownloadController.RESUME -> JmmDownloadStatus.DownLoading
Expand Down

0 comments on commit e1a6310

Please sign in to comment.