Skip to content

Commit e1a6310

Browse files
committed
✨ [Android] 补充断点下载功能
1 parent 895e818 commit e1a6310

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed

next/kmp/browser/src/androidMain/kotlin/org/dweb_browser/browser/jmm/JmmController.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ class JmmController(
8080
)
8181
}
8282

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

9292
suspend fun closeSelf() {

next/kmp/browser/src/androidMain/kotlin/org/dweb_browser/browser/jmm/ui/JmmManagerViewHelper.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class JmmManagerViewHelper(
9797
// }
9898
}
9999

100-
private fun initDownLoadStatusListener() {
100+
private suspend fun initDownLoadStatusListener() {
101101
jmmController.onDownload { downloadInfo ->
102102
if (downloadInfo.id != uiState.jmmAppInstallManifest.id) return@onDownload
103103
if (downloadInfo.downloadStatus == JmmDownloadStatus.IDLE) return@onDownload
@@ -112,10 +112,9 @@ class JmmManagerViewHelper(
112112
uiState.downloadStatus.value = downloadInfo.downloadStatus
113113
}
114114
}
115-
if (downloadInfo.downloadStatus == JmmDownloadStatus.INSTALLED) { // 移除监听列表
116-
// downLoadObserver?.close()
117-
// TODO 移除监听
118-
}
115+
/*if (downloadInfo.downloadStatus == JmmDownloadStatus.INSTALLED) { // 移除监听列表
116+
117+
}*/
119118
}
120119
}
121120

@@ -131,11 +130,21 @@ class JmmManagerViewHelper(
131130
}
132131

133132
JmmDownloadStatus.DownLoading -> {
134-
jmmController.updateDownloadState(JmmDownloadController.PAUSE)
133+
val success = jmmController.updateDownloadState(
134+
JmmDownloadController.PAUSE, uiState.jmmAppInstallManifest.id
135+
)
136+
if (success) {
137+
uiState.downloadStatus.value = JmmDownloadStatus.PAUSE
138+
}
135139
}
136140

137141
JmmDownloadStatus.PAUSE -> {
138-
jmmController.updateDownloadState(JmmDownloadController.RESUME)
142+
val success =jmmController.updateDownloadState(
143+
JmmDownloadController.RESUME, uiState.jmmAppInstallManifest.id
144+
)
145+
if (success) {
146+
uiState.downloadStatus.value = JmmDownloadStatus.DownLoading
147+
}
139148
}
140149

141150
/*DownloadStatus.DownLoading, DownloadStatus.PAUSE -> {

next/kmp/microService/src/androidMain/kotlin/org/dweb_browser/microservice/sys/download/DownloadNMM.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,22 @@ class DownloadNMM :
5555
downloadModel.exists(ipc.remote.mmid)
5656
},
5757
"/pause" bind HttpMethod.Get to defineBooleanResponse {
58-
debugDownload("pause", ipc.remote.mmid)
59-
downloadModel.updateDownloadState(ipc.remote.mmid, JmmDownloadController.PAUSE)
58+
val mmid = request.queryOrNull("mmid") ?: ipc.remote.mmid
59+
debugDownload("pause", "ipc->${ipc.remote.mmid}, mmid->$mmid")
60+
downloadModel.updateDownloadState(mmid, JmmDownloadController.PAUSE)
6061
true
6162
},
6263
/**继续下载*/
6364
"/resume" bind HttpMethod.Get to defineBooleanResponse {
64-
debugDownload("resume", ipc.remote.mmid)
65-
downloadModel.updateDownloadState(ipc.remote.mmid, JmmDownloadController.RESUME)
65+
val mmid = request.queryOrNull("mmid") ?: ipc.remote.mmid
66+
debugDownload("resume", "ipc->${ipc.remote.mmid}, mmid->$mmid")
67+
downloadModel.updateDownloadState(mmid, JmmDownloadController.RESUME)
6668
true
6769
},
6870
"/cancel" bind HttpMethod.Get to defineBooleanResponse {
69-
debugDownload("cancel", ipc.remote.mmid)
70-
downloadModel.updateDownloadState(ipc.remote.mmid, JmmDownloadController.CANCEL)
71+
val mmid = request.queryOrNull("mmid") ?: ipc.remote.mmid
72+
debugDownload("cancel", "ipc->${ipc.remote.mmid}, mmid->$mmid")
73+
downloadModel.updateDownloadState(mmid, JmmDownloadController.CANCEL)
7174
true
7275
},
7376
"/listen" bind HttpMethod.Get to definePureStreamHandler {

next/kmp/microService/src/androidMain/kotlin/org/dweb_browser/microservice/sys/download/HttpDownload.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.ktor.client.request.prepareGet
77
import io.ktor.utils.io.ByteReadChannel
88
import io.ktor.utils.io.core.isNotEmpty
99
import io.ktor.utils.io.core.readBytes
10+
import kotlinx.coroutines.delay
1011
import org.dweb_browser.helper.platform.getKtorClientEngine
1112
import java.io.File
1213

@@ -27,9 +28,10 @@ object HttpDownload {
2728
val contentLength = httpResponse.headers["content-length"]?.toLong() ?: downloadInfo.size
2829
var currentLength = 0L
2930
val channel: ByteReadChannel = httpResponse.body()
30-
while (!channel.isClosedForRead && !isStop()) {
31+
while (!channel.isClosedForRead) {
3132
val packet = channel.readRemaining(limit = DEFAULT_BUFFER_SIZE.toLong())
32-
while (packet.isNotEmpty && !isStop()) {
33+
while (packet.isNotEmpty) {
34+
while (isStop()) delay(200)
3335
val bytes: ByteArray = packet.readBytes()
3436
file.appendBytes(array = bytes)
3537
currentLength += bytes.size

next/kmp/microService/src/androidMain/kotlin/org/dweb_browser/microservice/sys/download/JmmDownloadModel.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class DownloadModel(val downloadNMM: DownloadNMM) {
6363
private var notificationId = AtomicInteger(999)
6464
}
6565

66-
fun findDownloadApp(mmid: MMID) = downloadAppMap[mmid]
6766
fun exists(mmid: MMID) = downloadAppMap.containsKey(mmid)
6867

6968
private val ioAsyncScope = MainScope() + ioAsyncExceptionHandler // 用于全局的协程调用
@@ -76,25 +75,29 @@ class DownloadModel(val downloadNMM: DownloadNMM) {
7675
val downloadInfo = jmm.toDownloadInfo(context)
7776
downloadAppMap[jmm.id] = downloadInfo
7877
HttpDownload.downloadAndSave(downloadInfo, isStop = {
78+
// debugDownload("Downloading", "${jmm.id}, downloadStatus=${downloadInfo.downloadStatus}")
7979
when (downloadInfo.downloadStatus) {
80-
JmmDownloadStatus.CANCEL, JmmDownloadStatus.FAIL, JmmDownloadStatus.PAUSE -> true
80+
JmmDownloadStatus.CANCEL, JmmDownloadStatus.FAIL -> {
81+
// 如果是cancel和fail,移除当前下载
82+
downloadAppMap.remove(jmm.id)
83+
downloadingMap.remove(jmm.id)
84+
true
85+
}
86+
JmmDownloadStatus.PAUSE -> true
8187
else -> false
8288
}
8389
}) { current, total ->
84-
debugDownload("Downloading", "current=$current, total=$total")
90+
// debugDownload("Downloading", "current=$current, total=$total")
8591
ioAsyncScope.launch {
8692
downloadInfo.callDownLoadProgress(context, current, total)
8793
}
8894
}
8995
return true
9096
}
9197

92-
internal suspend fun breakDownloadApp() {
93-
94-
}
95-
9698
internal fun updateDownloadState(mmid: MMID, event: JmmDownloadController) {
9799
downloadAppMap[mmid]?.apply {
100+
debugDownload("updateDownloadState", "event=$event, mmid=$mmid")
98101
downloadStatus = when (event) {
99102
JmmDownloadController.CANCEL -> JmmDownloadStatus.CANCEL
100103
JmmDownloadController.RESUME -> JmmDownloadStatus.DownLoading

0 commit comments

Comments
 (0)