From e55bd16253e8fd6634905c2adb4f86ff87a598b3 Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Sat, 7 Mar 2026 09:42:27 +0800 Subject: [PATCH] Open cached attachments on Android --- .../src/androidMain/AndroidManifest.xml | 10 +++++ .../platform/AttachmentOpener.android.kt | 42 +++++++++++++++++++ .../src/androidMain/res/xml/file_paths.xml | 4 ++ .../kotlin/cn/xiaobai/openclawapp/App.kt | 12 ++++++ .../openclawapp/data/OpenClawRepository.kt | 7 ++++ .../data/RemoteOpenClawRepository.kt | 8 ++++ .../openclawapp/feature/chat/ChatScreen.kt | 6 +++ .../openclawapp/platform/AttachmentOpener.kt | 9 ++++ .../platform/AttachmentOpener.ios.kt | 12 ++++++ .../platform/AttachmentOpener.js.kt | 12 ++++++ .../platform/AttachmentOpener.wasmJs.kt | 12 ++++++ docs/MVP_PROGRESS.md | 3 +- 12 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.android.kt create mode 100644 composeApp/src/androidMain/res/xml/file_paths.xml create mode 100644 composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.kt create mode 100644 composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.ios.kt create mode 100644 composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.js.kt create mode 100644 composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.wasmJs.kt diff --git a/composeApp/src/androidMain/AndroidManifest.xml b/composeApp/src/androidMain/AndroidManifest.xml index d8b6fa9..9e21e8d 100644 --- a/composeApp/src/androidMain/AndroidManifest.xml +++ b/composeApp/src/androidMain/AndroidManifest.xml @@ -13,6 +13,16 @@ android:supportsRtl="true" android:theme="@android:style/Theme.Material.Light.NoActionBar" android:usesCleartextTraffic="true"> + + + + diff --git a/composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.android.kt b/composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.android.kt new file mode 100644 index 0000000..e6b7b7f --- /dev/null +++ b/composeApp/src/androidMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.android.kt @@ -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) + } + } + } +} diff --git a/composeApp/src/androidMain/res/xml/file_paths.xml b/composeApp/src/androidMain/res/xml/file_paths.xml new file mode 100644 index 0000000..dc39105 --- /dev/null +++ b/composeApp/src/androidMain/res/xml/file_paths.xml @@ -0,0 +1,4 @@ + + + + diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt index f3fbb2a..bd3d812 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt @@ -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( diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt index 6ea0678..ec1b214 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt @@ -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, +) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt index ea6b717..f710193 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -67,6 +67,7 @@ class RemoteOpenClawRepository( private var messageStore by mutableStateOf>>(emptyMap()) private var streamStatusStore by mutableStateOf>(emptyMap()) private val attachmentRetryStore = mutableMapOf() + private val attachmentOpenStore = mutableMapOf() private val textRetryStore = mutableMapOf() 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) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt index 8eaee15..e3c90bf 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt @@ -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("复制文件名") } diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.kt new file mode 100644 index 0000000..62259b7 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.kt @@ -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 diff --git a/composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.ios.kt b/composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.ios.kt new file mode 100644 index 0000000..ea5670e --- /dev/null +++ b/composeApp/src/iosMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.ios.kt @@ -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}") } +} diff --git a/composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.js.kt b/composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.js.kt new file mode 100644 index 0000000..bb9bedc --- /dev/null +++ b/composeApp/src/jsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.js.kt @@ -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}") } +} diff --git a/composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.wasmJs.kt b/composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.wasmJs.kt new file mode 100644 index 0000000..c700b03 --- /dev/null +++ b/composeApp/src/wasmJsMain/kotlin/cn/xiaobai/openclawapp/platform/AttachmentOpener.wasmJs.kt @@ -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}") } +} diff --git a/docs/MVP_PROGRESS.md b/docs/MVP_PROGRESS.md index 469aadb..0686683 100644 --- a/docs/MVP_PROGRESS.md +++ b/docs/MVP_PROGRESS.md @@ -193,9 +193,10 @@ Android/iOS App - [x] 复制文件名(Android 真剪贴板) - [x] 查看附件详情 -- [ ] 点击附件打开 +- [x] 点击附件打开(Android,基于本地缓存文件) - [ ] 下载附件 - [ ] iOS / Web 剪贴板 +- [ ] iOS / Web 附件打开 ### 4.4 iOS / Web 文件选择器