优化大文件稳定并发传输与打包流程
This commit is contained in:
@@ -76,7 +76,7 @@ class MainActivity : ComponentActivity() {
|
||||
private lateinit var progress: ProgressBar
|
||||
|
||||
private var pendingPair: PairingPayload? = null
|
||||
private var connectedNetwork: Network? = null
|
||||
@Volatile private var connectedNetwork: Network? = null
|
||||
private var connectedBaseUrl: String? = null
|
||||
private val selectedFiles = mutableListOf<SelectedFile>()
|
||||
private var managingFilesForPair: PairingPayload? = null
|
||||
@@ -146,6 +146,7 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
|
||||
private fun showHome(message: String? = null) {
|
||||
transferAbortReason = "已返回首页,传输已停止"
|
||||
pairConnectionAttempt++
|
||||
stopCamera()
|
||||
scannerMode = ScannerMode.NONE
|
||||
@@ -602,6 +603,7 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
private fun connectPair(pair: PairingPayload) {
|
||||
val attempt = ++pairConnectionAttempt
|
||||
transferAbortReason = null
|
||||
showConnecting(pair, pair.wifi?.let { "正在请求连接 ${it.ssid}…" } ?: "正在使用当前局域网探测电脑…")
|
||||
wifiConnector.connect(
|
||||
pairing = pair,
|
||||
@@ -611,6 +613,10 @@ class MainActivity : ComponentActivity() {
|
||||
onAvailable = available@{ network ->
|
||||
if (attempt != pairConnectionAttempt) return@available
|
||||
connectedNetwork = network
|
||||
// A retained WifiNetworkSpecifier request may deliver a fresh
|
||||
// Network after a brief radio fluctuation. Do not restart the
|
||||
// page or transfer; chunk retries will use this new handle.
|
||||
if (connectedBaseUrl != null) return@available
|
||||
transferExecutor.execute {
|
||||
if (attempt != pairConnectionAttempt) return@execute
|
||||
runOnUiThread { updateConnectingStatus("网络路由已就绪 · 正在 UDP 快速发现电脑…") }
|
||||
@@ -657,14 +663,10 @@ class MainActivity : ComponentActivity() {
|
||||
},
|
||||
onUnavailable = { message -> runOnUiThread {
|
||||
if (attempt != pairConnectionAttempt) return@runOnUiThread
|
||||
// Huawei can report onLost while a bound local-only HTTP
|
||||
// socket is unwinding. Let DownloadClient surface its precise
|
||||
// HTTP/timeout cause instead of replacing it with this generic
|
||||
// message mid-retry.
|
||||
// onLost is recovered inside WifiConnector. onUnavailable is
|
||||
// terminal (system/user rejected the retained request), so it
|
||||
// is the only network callback that aborts an active transfer.
|
||||
if (pair.direction == TransferDirection.DOWNLOAD_TO_PHONE && downloadRunning) {
|
||||
// 让下载线程立刻收手并如实报告断网,而不是继续跑完
|
||||
// 5 次「重试 → 12 秒超时」,最后给出一个与真因无关的
|
||||
// 「下载失败,已重试 N 次」。
|
||||
transferAbortReason = message
|
||||
downloadStatusText?.apply {
|
||||
text = "QFT 本地网络已断开,正在结束当前下载请求…"
|
||||
@@ -705,8 +707,8 @@ class MainActivity : ComponentActivity() {
|
||||
private fun showPairError(pair: PairingPayload?, message: String) {
|
||||
// Do not unregister here: Android may still be displaying its
|
||||
// Wi-Fi-selection sheet, and unregistering at that moment produces
|
||||
// "the app cancelled the device selection request". The next attempt
|
||||
// (or returning home) releases the previous request safely.
|
||||
// "the app cancelled the device selection request". A retry reuses
|
||||
// this retained request; returning home is what explicitly releases it.
|
||||
pairConnectionAttempt++
|
||||
connectedNetwork = null
|
||||
connectedBaseUrl = null
|
||||
@@ -718,7 +720,7 @@ class MainActivity : ComponentActivity() {
|
||||
root.addView(button("重试连接").apply { setOnClickListener { ensureWifiPermissionThenConnect(pair) } })
|
||||
root.addView(space(10))
|
||||
}
|
||||
root.addView(button("重新扫码").apply { setOnClickListener { requestScanner(ScannerMode.PAIR) } })
|
||||
root.addView(button("返回首页重新扫码").apply { setOnClickListener { showHome() } })
|
||||
root.addView(space(10))
|
||||
root.addView(button("返回首页").apply { setOnClickListener { showHome() } })
|
||||
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
|
||||
@@ -776,7 +778,13 @@ class MainActivity : ComponentActivity() {
|
||||
root.addView(space(18))
|
||||
root.addView(card().apply {
|
||||
addView(text("扫码配置已完成", 17f, SUCCESS, true))
|
||||
addView(text("电脑已自动确认会话,正在通过本地网络直接传输。", 14f, MUTED), matchWrap(top = 8))
|
||||
addView(text(
|
||||
if (total >= 32L * 1024L * 1024L)
|
||||
"连接将保持到返回首页;正在通过本地网络进行最高 8 路并行直传。"
|
||||
else
|
||||
"连接将保持到返回首页;正在通过本地网络直接传输。",
|
||||
14f, MUTED,
|
||||
), matchWrap(top = 8))
|
||||
addView(text(fileName, 15f, Color.WHITE), matchWrap(top = 12))
|
||||
addView(text(humanBytes(total), 13f, MUTED), matchWrap(top = 4))
|
||||
})
|
||||
@@ -801,7 +809,7 @@ class MainActivity : ComponentActivity() {
|
||||
val saved = DownloadClient.download(
|
||||
context = this@MainActivity,
|
||||
resolver = contentResolver,
|
||||
network = connectedNetwork,
|
||||
networkProvider = { connectedNetwork },
|
||||
baseUrl = baseUrl,
|
||||
pairing = pair,
|
||||
deviceName = receiverDeviceLabel,
|
||||
@@ -980,11 +988,12 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
UploadClient.upload(
|
||||
resolver = contentResolver,
|
||||
network = connectedNetwork,
|
||||
networkProvider = { connectedNetwork },
|
||||
baseUrl = baseUrl,
|
||||
pairing = pair,
|
||||
deviceName = receiverDeviceLabel,
|
||||
file = file,
|
||||
abortReason = { transferAbortReason },
|
||||
) { sent, total ->
|
||||
runOnUiThread {
|
||||
val p = if (total == 0L) 1000 else ((sent.toDouble() / total) * 1000).toInt().coerceIn(0, 1000)
|
||||
|
||||
Reference in New Issue
Block a user