Open cached attachments on Android

This commit is contained in:
OpenClaw committed 2026-03-07 09:42:27 +08:00
1 parent 64e992e8fa
commit e55bd16253
12 files changed
+136 -1

No files matched your search

@@ -13,6 +13,16 @@
android:supportsRtl="true"
android:theme="@android:style/Theme.Material.Light.NoActionBar"
android:usesCleartextTraffic="true">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<activity
android:exported="true"
android:name=".MainActivity">
@@ -0,0 +1,42 @@
package cn.xiaobai.openclawapp.platform
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.FileProvider
import cn.xiaobai.openclawapp.data.AttachmentOpenPayload
import java.io.File
@Composable
actual fun rememberAttachmentOpener(
onResult: (String) -> Unit,
): (AttachmentOpenPayload) -> Unit {
val context = LocalContext.current
return remember(context, onResult) {
{ payload ->
runCatching {
val file = File(context.cacheDir, "attachments/${payload.fileName}")
file.parentFile?.mkdirs()
file.writeBytes(payload.bytes)
val uri = FileProvider.getUriForFile(
context,
context.packageName + ".fileprovider",
file,
)
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(uri, payload.contentType ?: "*/*")
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
onResult("正在打开:${payload.fileName}")
}.onFailure {
val msg = if (it is ActivityNotFoundException) "没有可用应用打开该附件" else (it.message ?: "打开附件失败")
onResult(msg)
}
}
}
}
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="attachments" path="attachments/" />
</paths>
@@ -26,6 +26,7 @@ import cn.xiaobai.openclawapp.feature.config.ServerConfigScreen
import cn.xiaobai.openclawapp.feature.sessions.SessionListScreen
import cn.xiaobai.openclawapp.feature.settings.SettingsScreen
import cn.xiaobai.openclawapp.feature.status.StatusScreen
import cn.xiaobai.openclawapp.platform.rememberAttachmentOpener
import cn.xiaobai.openclawapp.platform.rememberAttachmentPicker
import cn.xiaobai.openclawapp.platform.rememberClipboardSetter
import cn.xiaobai.openclawapp.ui.OpenClawTheme
@@ -101,6 +102,9 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
val copyToClipboard = rememberClipboardSetter { message ->
appState.feedbackMessage = message
}
val openAttachment = rememberAttachmentOpener { message ->
appState.feedbackMessage = message
}
LaunchedEffect(appState.feedbackMessage) {
if (appState.feedbackMessage != null) {
@@ -214,6 +218,14 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
onCopyAttachmentName = { fileName ->
copyToClipboard(fileName)
},
onOpenAttachment = { messageId ->
val payload = appState.repository.attachmentOpenPayload(messageId)
if (payload != null) {
openAttachment(payload)
} else {
appState.feedbackMessage = "当前附件暂无可打开内容"
}
},
)
appState.selectedTab == MainTab.Sessions -> SessionListScreen(
@@ -22,4 +22,11 @@ interface OpenClawRepository {
contentType: String? = null,
)
suspend fun retryAttachment(sessionId: String, messageId: String)
fun attachmentOpenPayload(messageId: String): AttachmentOpenPayload?
}
data class AttachmentOpenPayload(
val fileName: String,
val bytes: ByteArray,
val contentType: String? = null,
)
@@ -67,6 +67,7 @@ class RemoteOpenClawRepository(
private var messageStore by mutableStateOf<Map<String, List<ChatMessage>>>(emptyMap())
private var streamStatusStore by mutableStateOf<Map<String, String>>(emptyMap())
private val attachmentRetryStore = mutableMapOf<String, AttachmentRetryPayload>()
private val attachmentOpenStore = mutableMapOf<String, AttachmentOpenPayload>()
private val textRetryStore = mutableMapOf<String, TextRetryPayload>()
override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId }
@@ -79,6 +80,8 @@ class RemoteOpenClawRepository(
override fun pinnedCount(): Int = sessions.count { it.isPinned }
override fun attachmentOpenPayload(messageId: String): AttachmentOpenPayload? = attachmentOpenStore[messageId]
override fun markSessionOpened(sessionId: String) {
recentSessionId = sessionId
sessions = sessions.map {
@@ -149,6 +152,7 @@ class RemoteOpenClawRepository(
val currentMessages = messageStore[sessionId].orEmpty()
val localId = "local-attachment-$sessionId-${currentMessages.size + 1}"
attachmentRetryStore[localId] = AttachmentRetryPayload(sessionId, fileName, bytes, contentType)
attachmentOpenStore[localId] = AttachmentOpenPayload(fileName, bytes, contentType)
val pending = ChatMessage(
id = localId,
role = MessageRole.User,
@@ -179,6 +183,8 @@ class RemoteOpenClawRepository(
sizeBytes = remote.sizeBytes,
)
attachmentRetryStore.remove(localId)
attachmentOpenStore.remove(localId)
attachmentOpenStore[remote.attachmentId] = AttachmentOpenPayload(remote.fileName, bytes, remote.contentType)
val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) uploaded else msg }
messageStore = messageStore + (sessionId to next)
updateSessionPreview(sessionId, uploaded)
@@ -227,6 +233,8 @@ class RemoteOpenClawRepository(
sizeBytes = remote.sizeBytes,
)
attachmentRetryStore.remove(messageId)
attachmentOpenStore.remove(messageId)
attachmentOpenStore[remote.attachmentId] = AttachmentOpenPayload(remote.fileName, payload.bytes, remote.contentType)
val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == messageId) uploaded else msg }
messageStore = messageStore + (sessionId to next)
updateSessionPreview(sessionId, uploaded)
@@ -56,6 +56,7 @@ fun ChatScreen(
onRetryAttachment: (String) -> Unit,
onRetryMessage: (String) -> Unit,
onCopyAttachmentName: (String) -> Unit,
onOpenAttachment: (String) -> Unit,
) {
var input by remember { mutableStateOf("") }
val listState = rememberLazyListState()
@@ -183,6 +184,7 @@ fun ChatScreen(
onRetryAttachment = onRetryAttachment,
onRetryMessage = onRetryMessage,
onCopyAttachmentName = onCopyAttachmentName,
onOpenAttachment = onOpenAttachment,
)
}
}
@@ -244,6 +246,7 @@ private fun MessageBubble(
onRetryAttachment: (String) -> Unit,
onRetryMessage: (String) -> Unit,
onCopyAttachmentName: (String) -> Unit,
onOpenAttachment: (String) -> Unit,
) {
val alignment = when (message.role) {
MessageRole.User -> Alignment.CenterEnd
@@ -376,6 +379,9 @@ private fun MessageBubble(
)
}
Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
TextButton(onClick = { onOpenAttachment(message.id) }) {
Text("打开")
}
TextButton(onClick = { onCopyAttachmentName(message.text) }) {
Text("复制文件名")
}
@@ -0,0 +1,9 @@
package cn.xiaobai.openclawapp.platform
import androidx.compose.runtime.Composable
import cn.xiaobai.openclawapp.data.AttachmentOpenPayload
@Composable
expect fun rememberAttachmentOpener(
onResult: (String) -> Unit = {},
): (AttachmentOpenPayload) -> Unit
@@ -0,0 +1,12 @@
package cn.xiaobai.openclawapp.platform
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import cn.xiaobai.openclawapp.data.AttachmentOpenPayload
@Composable
actual fun rememberAttachmentOpener(
onResult: (String) -> Unit,
): (AttachmentOpenPayload) -> Unit = remember(onResult) {
{ payload -> onResult("iOS 附件打开还没接好:${payload.fileName}") }
}
@@ -0,0 +1,12 @@
package cn.xiaobai.openclawapp.platform
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import cn.xiaobai.openclawapp.data.AttachmentOpenPayload
@Composable
actual fun rememberAttachmentOpener(
onResult: (String) -> Unit,
): (AttachmentOpenPayload) -> Unit = remember(onResult) {
{ payload -> onResult("Web 附件打开还没接好:${payload.fileName}") }
}
@@ -0,0 +1,12 @@
package cn.xiaobai.openclawapp.platform
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import cn.xiaobai.openclawapp.data.AttachmentOpenPayload
@Composable
actual fun rememberAttachmentOpener(
onResult: (String) -> Unit,
): (AttachmentOpenPayload) -> Unit = remember(onResult) {
{ payload -> onResult("Wasm 附件打开还没接好:${payload.fileName}") }
}
+2 -1
View File
@@ -193,9 +193,10 @@ Android/iOS App
- [x] 复制文件名(Android 真剪贴板)
- [x] 查看附件详情
- [ ] 点击附件打开
- [x] 点击附件打开(Android,基于本地缓存文件)
- [ ] 下载附件
- [ ] iOS / Web 剪贴板
- [ ] iOS / Web 附件打开
### 4.4 iOS / Web 文件选择器