diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index d2aa4f8..f0a7dfa 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -30,8 +30,8 @@ android { * versionCode 每次发版 +1(Android 靠它判断"能不能覆盖安装", * 改小了会装不上)。 */ - versionCode = 235 - versionName = "2.0.35" + versionCode = 236 + versionName = "2.0.36" ndk { // Chaquopy 只支持这两个;Termux bootstrap 也只准备了这两份 diff --git a/android/app/src/main/java/com/doubao/lynxclone/ui/AiSurfaces.kt b/android/app/src/main/java/com/doubao/lynxclone/ui/AiSurfaces.kt index 993c97f..c94c02b 100644 --- a/android/app/src/main/java/com/doubao/lynxclone/ui/AiSurfaces.kt +++ b/android/app/src/main/java/com/doubao/lynxclone/ui/AiSurfaces.kt @@ -1,10 +1,14 @@ package com.doubao.lynxclone.ui import android.annotation.SuppressLint +import android.content.Intent import android.graphics.Color as AndroidColor +import android.net.Uri import android.webkit.JavascriptInterface +import android.webkit.WebSettings import android.webkit.WebView import android.webkit.WebViewClient +import androidx.activity.compose.BackHandler import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.core.Spring import androidx.compose.animation.core.animateFloatAsState @@ -35,6 +39,7 @@ import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material.icons.filled.Close import androidx.compose.material.icons.filled.PowerSettingsNew import androidx.compose.material3.Button @@ -44,6 +49,7 @@ import androidx.compose.material3.OutlinedButton import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateListOf @@ -84,6 +90,24 @@ fun AiHtmlScreenOverlay(screen: AiSurface.Screen?, onClose: () -> Unit, onResult LaunchedEffect(screen) { if (screen != null) last = screen } val shown = screen ?: last + // WebView 实例 + "能不能回退"。 + // ★ 拿到了 WebView 才能调 goBack();canGoBack 要轮询(WebView 没有变更回调)。 + var web by remember { mutableStateOf(null) } + var canGoBack by remember { mutableStateOf(false) } + LaunchedEffect(web, screen) { + while (web != null && screen != null) { + canGoBack = web?.canGoBack() == true + kotlinx.coroutines.delay(400) + } + canGoBack = false + } + + // 系统返回键:能回退就先回退(用户点进去了一页,返回键该带他回来), + // 不能回退才关掉整个界面 —— 比直接关掉符合直觉。 + BackHandler(enabled = screen != null) { + if (web?.canGoBack() == true) web?.goBack() else onClose() + } + AnimatedVisibility( visible = screen != null, enter = fadeIn(tween(180)) + @@ -110,6 +134,25 @@ fun AiHtmlScreenOverlay(screen: AiSurface.Screen?, onClose: () -> Unit, onResult color = doubaoText(), modifier = Modifier.weight(1f), ) + // 「返回上一页」只在真能回退时出现 —— 一直显示一个点了没反应的 + // 箭头比不显示更让人困惑 + AnimatedVisibility( + visible = canGoBack, + enter = fadeIn(tween(150)), + exit = fadeOut(tween(120)), + ) { + Box( + Modifier.size(34.dp).clip(CircleShape).clickable { web?.goBack() }, + contentAlignment = Alignment.Center, + ) { + Icon( + Icons.AutoMirrored.Filled.ArrowBack, + "返回上一页", + tint = doubaoText(), + modifier = Modifier.size(20.dp), + ) + } + } Box( Modifier.size(34.dp).clip(CircleShape).clickable { onClose() }, contentAlignment = Alignment.Center, @@ -117,7 +160,7 @@ fun AiHtmlScreenOverlay(screen: AiSurface.Screen?, onClose: () -> Unit, onResult Icon(Icons.Filled.Close, "关闭", tint = doubaoText(), modifier = Modifier.size(20.dp)) } } - AiWebView(shown, onResult) + AiWebView(shown, onResult, onClose, onWebView = { web = it }) } } } @@ -125,57 +168,259 @@ fun AiHtmlScreenOverlay(screen: AiSurface.Screen?, onClose: () -> Unit, onResult /** * 渲染 AI 写的 HTML。 * - * ## 两个安全/健壮性决定 + * ## JS 开着,但 `@JavascriptInterface` 只在"我们自己那一页"暴露 * - * 1. **禁 JS**:AI 写的 HTML 是"内容",不是"程序"。允许 JS 的话它会拿到 - * `@JavascriptInterface` 那个桥,等于把 App 的内部接口暴露给一段 - * 模型生成的脚本。禁掉之后照样能渲染表格/图表(纯 CSS 足够), - * 而"可点按钮"用下面 data 属性的方式实现,不需要 JS。 + * 用户要求"支持 js"。开了 JS 之后最大的风险不是 JS 本身,而是 + * **`addJavascriptInterface` 注入的那个桥**:它对**之后加载的每一个页面** + * 都可见。AI 的页面里如果有个链接,用户点进去落到一个外部站点, + * 那个站点的 JS 也能调我们的桥 —— 等于把 App 的内部接口交给它。 * - * 2. **可点按钮不用 JS 实现**:给 WebView 装一个自定义 `WebViewClient`, - * 在 `shouldOverrideUrlLoading` 里拦 `doubao://pick?value=...` 这种链接。 - * 界面里写 `