From 0727aa33f1863c4e0c78bc5c56cb09e1cad122dc Mon Sep 17 00:00:00 2001 From: LukeMackin Date: Tue, 21 Jul 2026 14:55:57 +0800 Subject: [PATCH] =?UTF-8?q?build:=20v6=20=E2=80=94=20WebView=E7=83=AD?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BF=AE=E5=A4=8D=20+=20fetch=E8=B7=A8?= =?UTF-8?q?=E5=9F=9F=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - versionCode 5→6 - WebView allowFileAccessFromFileURLs + mixedContent - index.html 版本号v6 - index.html fetch检查更新+下载热更新完整链路 --- .../client-android/android/app/build.gradle | 2 +- .../android/app/src/main/assets/index.html | 52 ++++---- .../main/java/dev/yuzu/gca/MainActivity.kt | 111 ++++++------------ 3 files changed, 64 insertions(+), 101 deletions(-) diff --git a/packages/client-android/android/app/build.gradle b/packages/client-android/android/app/build.gradle index cfd261f..ef6adbf 100644 --- a/packages/client-android/android/app/build.gradle +++ b/packages/client-android/android/app/build.gradle @@ -9,7 +9,7 @@ android { applicationId "dev.yuzu.gca" minSdk 24 targetSdk 35 - versionCode 5 + versionCode 6 versionName "0.1.0" } diff --git a/packages/client-android/android/app/src/main/assets/index.html b/packages/client-android/android/app/src/main/assets/index.html index 9446123..6a42e11 100644 --- a/packages/client-android/android/app/src/main/assets/index.html +++ b/packages/client-android/android/app/src/main/assets/index.html @@ -3,45 +3,49 @@

Yuzu GCA

-
version v0.1.0-dav-android-5
+
version v0.1.0-dav-android-6
diff --git a/packages/client-android/android/app/src/main/java/dev/yuzu/gca/MainActivity.kt b/packages/client-android/android/app/src/main/java/dev/yuzu/gca/MainActivity.kt index 7bb00e1..94a3e33 100644 --- a/packages/client-android/android/app/src/main/java/dev/yuzu/gca/MainActivity.kt +++ b/packages/client-android/android/app/src/main/java/dev/yuzu/gca/MainActivity.kt @@ -7,6 +7,8 @@ import android.graphics.Color import android.net.Uri import android.os.Bundle import android.provider.Settings +import android.webkit.WebView +import android.webkit.WebViewClient import android.widget.Button import android.widget.LinearLayout import android.widget.ScrollView @@ -29,92 +31,40 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val scroll = ScrollView(this) - val layout = LinearLayout(this).apply { - orientation = LinearLayout.VERTICAL - setPadding(24, 24, 24, 24) - } - - val title = TextView(this).apply { - text = "Yuzu GCA" - textSize = 20f - setPadding(0, 0, 0, 4) - } - layout.addView(title) - - val versionInfo = TextView(this).apply { - text = "v0.1.0-dav-android-5-debug" - textSize = 12f - setTextColor(Color.GRAY) - setPadding(0, 0, 0, 12) - } - layout.addView(versionInfo) - - val channelLabel = TextView(this).apply { - text = "选择更新渠道:" - textSize = 13f - setPadding(0, 8, 0, 4) - } - layout.addView(channelLabel) - - // 渠道按钮行 - val channelRow = LinearLayout(this).apply { - orientation = LinearLayout.HORIZONTAL - setPadding(0, 0, 0, 12) - } - - fun makeChannelBtn(label: String, channel: String): Button { - return Button(this).apply { - text = label - textSize = 12f - setOnClickListener { - selectedChannel = channel - highlightChannel(channelRow, this) + val wv = WebView(this).apply { + WebView.setWebContentsDebuggingEnabled(true) + settings.javaScriptEnabled = true + settings.domStorageEnabled = true + settings.allowFileAccess = true + settings.allowFileAccessFromFileURLs = true + settings.allowUniversalAccessFromFileURLs = true + settings.mixedContentMode = android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW + webViewClient = object : WebViewClient() { + override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { + if (url.startsWith("http")) { view.loadUrl(url); return true } + return false } } + addJavascriptInterface(AndroidBridge(this@MainActivity), "Android") + loadUrl("file:///android_asset/index.html") } - - val btnStable = makeChannelBtn("Stable", "stable") - val btnBeta = makeChannelBtn("Beta", "beta") - val btnNightly = makeChannelBtn("Nightly", "nightly") - - channelRow.addView(btnStable) - channelRow.addView(btnBeta) - channelRow.addView(btnNightly) - layout.addView(channelRow) - highlightChannel(channelRow, btnStable) - - logView = TextView(this).apply { - textSize = 12f - setTextColor(Color.LTGRAY) - } - layout.addView(logView) - - val btn = Button(this).apply { - text = "检查更新" - setOnClickListener { - val ch = selectedChannel - CoroutineScope(Dispatchers.IO).launch { doOtaTest(ch) } - } - } - layout.addView(btn) - - scroll.addView(layout) - setContentView(scroll) - - appendLog("渠道: stable | 点击[检查更新]开始") + setContentView(wv) } - private fun highlightChannel(row: LinearLayout, active: Button) { - for (i in 0 until row.childCount) { - val b = row.getChildAt(i) as Button - b.isSelected = (b == active) + inner class AndroidBridge(private val ctx: MainActivity) { + @android.webkit.JavascriptInterface + fun checkUpdate(channel: String) { + CoroutineScope(Dispatchers.IO).launch { doOtaTest(channel) } } + @android.webkit.JavascriptInterface + fun log(msg: String) { appendLog(msg) } } private fun appendLog(msg: String) { Log.i(TAG, msg) - runOnUiThread { logView.append("$msg\n") } + if (::logView.isInitialized) { + runOnUiThread { logView.append("$msg\n") } + } } companion object { private const val TAG = "GCA-OTA" } @@ -163,6 +113,15 @@ class MainActivity : AppCompatActivity() { manifestTag = android.getString("tag") appendLog(" 渠道: $channel 版本: $manifestTag") appendLog(" SHA256: ${manifestSha256.take(16)}...") + + // 版本比较:一致则跳过 + val localTag = "v0.1.0-dav-android-${packageManager.getPackageInfo(packageName, 0).versionCode}" + if (manifestTag == localTag) { + appendLog(" ✅ 已是最新版本,无需更新") + appendLog("=== $channel 渠道测试完成 ===") + return@withContext + } + appendLog(" 发现新版本,开始更新...") } catch (e: Exception) { appendLog(" ❌ 解析失败: ${e.message}") return@withContext