optimize direct transfer UI and Android queue

This commit is contained in:
zhy200308
2026-08-12 14:06:03 +08:00
parent d7a29e035f
commit 8b3f5c0216
9 changed files with 260 additions and 182 deletions
@@ -17,6 +17,7 @@ import android.util.Size
import android.view.Gravity
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.FrameLayout
@@ -79,15 +80,17 @@ class MainActivity : ComponentActivity() {
@Volatile private var connectedNetwork: Network? = null
private var connectedBaseUrl: String? = null
private val selectedFiles = mutableListOf<SelectedFile>()
private var managingFilesForPair: PairingPayload? = null
private var managingFilesForBaseUrl: String? = null
private var sendStatusText: TextView? = null
private var sendFilesText: TextView? = null
private var sendFilesContainer: LinearLayout? = null
private var sendAddButton: Button? = null
private var sendClearButton: Button? = null
private var sendProgress: ProgressBar? = null
private var sendButton: Button? = null
private var connectingStatusText: TextView? = null
private var downloadStatusText: TextView? = null
@Volatile private var downloadRunning = false
@Volatile private var uploadRunning = false
/** 非 null 表示当前传输应立刻中止(QFT 临时网络断开等)。
* 下载线程每读一个缓冲区就检查一次,避免在 readTimeout 上白等。 */
@Volatile private var transferAbortReason: String? = null
@@ -117,18 +120,16 @@ class MainActivity : ComponentActivity() {
}
private val pickFiles = registerForActivityResult(ActivityResultContracts.OpenMultipleDocuments()) { uris ->
if (uploadRunning) {
sendStatusText?.text = "传输已经开始,当前队列已锁定"
return@registerForActivityResult
}
var unknown = 0
for (uri in uris) {
val file = UploadClient.describeFile(contentResolver, uri)
if (file != null && selectedFiles.none { it.uri == file.uri }) selectedFiles += file else if (file == null) unknown++
}
renderSelectedFiles()
val pair = managingFilesForPair
val baseUrl = managingFilesForBaseUrl
if (pair != null && baseUrl != null) {
showFileSelection(pair, baseUrl)
return@registerForActivityResult
}
if (unknown > 0) {
sendStatusText?.text = "有 $unknown 个文件无法确定大小,已跳过"
} else if (selectedFiles.isNotEmpty()) {
@@ -159,8 +160,14 @@ class MainActivity : ComponentActivity() {
downloadRunning = false
downloadStatusText = null
selectedFiles.clear()
managingFilesForPair = null
managingFilesForBaseUrl = null
uploadRunning = false
sendFilesContainer = null
sendAddButton = null
sendClearButton = null
sendFilesText = null
sendStatusText = null
sendProgress = null
sendButton = null
val root = pageColumn()
root.addView(text("QFT", 30f, Color.WHITE, true))
@@ -195,7 +202,7 @@ class MainActivity : ComponentActivity() {
) { showReceivedFiles() })
root.addView(space(22))
root.addView(text("本机设备:$receiverDeviceLabel", 13f, MUTED))
root.addView(text("QFT v0.4.9 · Android 7+ 到最新", 12f, MUTED))
root.addView(text("QFT v0.4.10 · Android 7+ 到最新", 12f, MUTED))
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
}
@@ -811,12 +818,34 @@ class MainActivity : ComponentActivity() {
})
root.addView(space(14))
root.addView(card().apply {
addView(text("选择文件", 17f, Color.WHITE, true))
addView(text("传输队列", 17f, Color.WHITE, true))
sendFilesText = text("请先选择需要传输的文件", 14f, WARNING)
addView(sendFilesText, matchWrap(top = 8))
addView(button("选择文件(可多选)").apply {
setOnClickListener { showFileSelection(pair, baseUrl) }
}, matchWrap(top = 12))
sendFilesContainer = LinearLayout(this@MainActivity).apply {
orientation = LinearLayout.VERTICAL
}
addView(sendFilesContainer, matchWrap(top = 6))
val actions = LinearLayout(this@MainActivity).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
}
sendAddButton = button("添加文件").apply {
setOnClickListener { pickFiles.launch(arrayOf("*/*")) }
}
sendClearButton = button("清空").apply {
setOnClickListener {
if (!uploadRunning) {
selectedFiles.clear()
renderSelectedFiles()
}
}
}
actions.addView(sendAddButton, LinearLayout.LayoutParams(0, dp(48), 1f).apply {
marginEnd = dp(8)
})
actions.addView(sendClearButton, LinearLayout.LayoutParams(dp(96), dp(48)))
addView(actions, matchWrap(top = 12))
addView(text("按住“拖动”上下移动可调整发送优先级;传输开始后队列会锁定。", 12f, MUTED), matchWrap(top = 8))
})
root.addView(space(14))
root.addView(card().apply {
@@ -826,10 +855,9 @@ class MainActivity : ComponentActivity() {
sendProgress = ProgressBar(this@MainActivity, null, android.R.attr.progressBarStyleHorizontal).apply { max = 1000; progress = 0 }
addView(sendProgress, LinearLayout.LayoutParams(-1, dp(9)).apply { topMargin = dp(12) })
sendButton = button("选择传输文件").apply {
isEnabled = connectedBaseUrl != null
isEnabled = false
setOnClickListener {
if (selectedFiles.isEmpty()) showFileSelection(pair, baseUrl)
else startUploads(pair, baseUrl)
startUploads(pair, baseUrl)
}
}
addView(sendButton, matchWrap(top = 14))
@@ -1008,95 +1036,117 @@ class MainActivity : ComponentActivity() {
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
}
private fun showFileSelection(pair: PairingPayload, baseUrl: String) {
managingFilesForPair = pair
managingFilesForBaseUrl = baseUrl
val root = pageColumn()
root.addView(text("选择发送文件", 28f, Color.WHITE, true))
root.addView(text("可多次添加文件,并在列表中单独移除。", 14f, MUTED))
root.addView(space(14))
val total = selectedFiles.sumOf { it.size }
root.addView(card().apply {
addView(text("已选 ${selectedFiles.size} 个 · ${humanBytes(total)}", 16f, ACCENT, true))
if (selectedFiles.isEmpty()) {
addView(text("请先选择需要传输的文件", 14f, WARNING), matchWrap(top = 8))
} else {
selectedFiles.toList().forEach { file ->
val row = LinearLayout(this@MainActivity).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
}
row.addView(
text("${file.name}\n${humanBytes(file.size)}", 14f, Color.WHITE),
LinearLayout.LayoutParams(0, -2, 1f).apply { marginEnd = dp(8) },
)
row.addView(button("移除").apply {
setOnClickListener {
selectedFiles.removeAll { it.uri == file.uri }
showFileSelection(pair, baseUrl)
}
}, LinearLayout.LayoutParams(dp(82), dp(44)))
addView(row, matchWrap(top = 10))
}
}
})
root.addView(space(14))
root.addView(button("添加文件(可多选)").apply {
setOnClickListener { pickFiles.launch(arrayOf("*/*")) }
})
if (selectedFiles.isNotEmpty()) {
root.addView(space(10))
root.addView(button("清空选择").apply {
setOnClickListener {
selectedFiles.clear()
showFileSelection(pair, baseUrl)
}
})
}
root.addView(space(10))
root.addView(button("完成并返回传输").apply {
isEnabled = selectedFiles.isNotEmpty()
setOnClickListener {
managingFilesForPair = null
managingFilesForBaseUrl = null
showSendReady(pair, baseUrl)
}
})
root.addView(space(10))
root.addView(button("取消并返回传输").apply {
setOnClickListener {
managingFilesForPair = null
managingFilesForBaseUrl = null
showSendReady(pair, baseUrl)
}
})
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
}
private fun renderSelectedFiles() {
val total = selectedFiles.sumOf { it.size }
sendFilesText?.text = if (selectedFiles.isEmpty()) {
"请先选择需要传输的文件"
} else {
buildString {
selectedFiles.take(8).forEach { append("• ${it.name} ${humanBytes(it.size)}\n") }
if (selectedFiles.size > 8) append("…另外 ${selectedFiles.size - 8} 个文件\n")
append("共 ${selectedFiles.size} 个 · ${humanBytes(total)}")
}.trim()
"已选 ${selectedFiles.size} 个 · ${humanBytes(total)} · 按列表顺序发送"
}
sendFilesText?.setTextColor(if (selectedFiles.isEmpty()) WARNING else ACCENT)
val container = sendFilesContainer
container?.removeAllViews()
selectedFiles.toList().forEachIndexed { index, file ->
val row = LinearLayout(this).apply {
orientation = LinearLayout.HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
setPadding(0, dp(4), 0, dp(4))
}
val drag = button("拖动").apply {
isEnabled = !uploadRunning
textSize = 12f
}
row.addView(drag, LinearLayout.LayoutParams(dp(76), dp(44)).apply {
marginEnd = dp(8)
})
row.addView(
text("${index + 1}. ${file.name}\n${humanBytes(file.size)}", 14f, Color.WHITE),
LinearLayout.LayoutParams(0, -2, 1f).apply { marginEnd = dp(8) },
)
row.addView(button("删除").apply {
isEnabled = !uploadRunning
textSize = 12f
setOnClickListener {
if (!uploadRunning) {
selectedFiles.removeAll { it.uri == file.uri }
renderSelectedFiles()
}
}
}, LinearLayout.LayoutParams(dp(76), dp(44)))
container?.addView(row, matchWrap(top = if (index == 0) 2 else 4))
if (container != null && !uploadRunning) {
installFileQueueDrag(drag, row, file, container)
}
}
sendAddButton?.isEnabled = !uploadRunning
sendClearButton?.isEnabled = !uploadRunning && selectedFiles.isNotEmpty()
sendButton?.text = when {
uploadRunning -> "传输中 · 队列已锁定"
selectedFiles.isEmpty() -> "请先添加文件"
else -> "开始传输 ${selectedFiles.size} 个文件"
}
sendButton?.isEnabled = connectedBaseUrl != null && selectedFiles.isNotEmpty() && !uploadRunning
}
private fun installFileQueueDrag(
handle: View,
row: View,
file: SelectedFile,
container: LinearLayout,
) {
handle.setOnTouchListener { view, event ->
if (uploadRunning) return@setOnTouchListener false
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
view.parent?.requestDisallowInterceptTouchEvent(true)
row.alpha = 0.72f
true
}
MotionEvent.ACTION_MOVE -> {
val location = IntArray(2)
container.getLocationOnScreen(location)
val y = event.rawY - location[1]
var target = container.childCount - 1
for (index in 0 until container.childCount) {
val child = container.getChildAt(index)
if (y < child.top + child.height / 2f) {
target = index
break
}
}
val current = selectedFiles.indexOfFirst { it.uri == file.uri }
if (moveQueueItem(selectedFiles, current, target)) {
container.removeView(row)
container.addView(row, target)
}
true
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
view.parent?.requestDisallowInterceptTouchEvent(false)
row.alpha = 1f
view.performClick()
renderSelectedFiles()
true
}
else -> false
}
}
sendButton?.text = if (selectedFiles.isEmpty()) "选择传输文件" else "开始传输"
sendButton?.isEnabled = connectedBaseUrl != null
}
private fun startUploads(pair: PairingPayload, baseUrl: String) {
if (uploadRunning) return
if (selectedFiles.isEmpty()) {
sendStatusText?.setTextColor(WARNING)
sendStatusText?.text = "请先选择需要传输的文件"
showFileSelection(pair, baseUrl)
pickFiles.launch(arrayOf("*/*"))
return
}
val files = selectedFiles.toList()
sendButton?.isEnabled = false
uploadRunning = true
transferAbortReason = null
renderSelectedFiles()
wifiConnector.acquireHighPerfLock()
transferExecutor.execute {
try {
@@ -1132,15 +1182,17 @@ class MainActivity : ComponentActivity() {
runOnUiThread {
sendProgress?.progress = 1000
sendStatusText?.text = "全部完成 · 已发送 ${files.size} 个文件"
sendButton?.isEnabled = true
}
} catch (e: Exception) {
runOnUiThread {
sendStatusText?.text = "传输失败:${e.message}"
sendButton?.isEnabled = true
}
} finally {
wifiConnector.releaseHighPerfLock()
runOnUiThread {
uploadRunning = false
renderSelectedFiles()
}
}
}
}
@@ -0,0 +1,8 @@
package com.qft.optical
/** Move one item while preserving every other item's relative order. */
internal fun <T> moveQueueItem(items: MutableList<T>, from: Int, to: Int): Boolean {
if (from !in items.indices || to !in items.indices || from == to) return false
items.add(to, items.removeAt(from))
return true
}