From 65943334b19034232df26c2fd07869a52fc293e2 Mon Sep 17 00:00:00 2001 From: "844143714@qq,com" <844143714@qq,com> Date: Sun, 19 Jul 2026 03:37:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/stand/standapp/DeveloperActivity.kt | 2 +- .../java/com/stand/standapp/MainActivity.kt | 74 +++++++++++-------- .../com/stand/standapp/utils/GpioManager.kt | 3 + 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/stand/standapp/DeveloperActivity.kt b/app/src/main/java/com/stand/standapp/DeveloperActivity.kt index 2525bbc..1353729 100644 --- a/app/src/main/java/com/stand/standapp/DeveloperActivity.kt +++ b/app/src/main/java/com/stand/standapp/DeveloperActivity.kt @@ -43,7 +43,7 @@ class DeveloperActivity : AppCompatActivity() { val etUser = EditText(this).apply { hint = "账号" - setText("test007") + setText("test010") } layout.addView(etUser) diff --git a/app/src/main/java/com/stand/standapp/MainActivity.kt b/app/src/main/java/com/stand/standapp/MainActivity.kt index 4b0a660..a9a403f 100644 --- a/app/src/main/java/com/stand/standapp/MainActivity.kt +++ b/app/src/main/java/com/stand/standapp/MainActivity.kt @@ -20,6 +20,9 @@ class MainActivity : AppCompatActivity() { companion object { private var instance: MainActivity? = null + @Volatile + private var geckoRuntime: GeckoRuntime? = null + // 防止对话框堆叠 @Volatile private var isConflictDialogShowing = false @@ -186,7 +189,6 @@ class MainActivity : AppCompatActivity() { } private var geckoSession: GeckoSession? = null - private var geckoRuntime: GeckoRuntime? = null private var pttInterface: AndroidInterface? = null private var canGoBack = false private lateinit var geckoView: GeckoView @@ -275,38 +277,46 @@ class MainActivity : AppCompatActivity() { } private fun createGeckoRuntime(): GeckoRuntime { - // 1. 将配置写入应用的本地沙盒文件 (YAML 格式) - val configFile = java.io.File(filesDir, "geckoview-config.yaml") - try { - val configContent = """ - prefs: - layout.testing.overlay-scrollbars.always-visible: true - """.trimIndent() - configFile.writeText(configContent) - Timber.tag("MainActivity").d("Wrote GeckoView config to: ${configFile.absolutePath}") - } catch (e: Exception) { - Timber.tag("MainActivity").e(e, "Failed to write geckoview-config.yaml") + synchronized(MainActivity::class.java) { + val existing = geckoRuntime + if (existing != null) { + return existing + } + + // 1. 将配置写入应用的本地沙盒文件 (YAML 格式) + val configFile = java.io.File(filesDir, "geckoview-config.yaml") + try { + val configContent = """ + prefs: + layout.testing.overlay-scrollbars.always-visible: true + """.trimIndent() + configFile.writeText(configContent) + Timber.tag("MainActivity").d("Wrote GeckoView config to: ${configFile.absolutePath}") + } catch (e: Exception) { + Timber.tag("MainActivity").e(e, "Failed to write geckoview-config.yaml") + } + + // 2. 通过 GeckoRuntimeSettings.Builder 加载该 YAML 配置文件并创建 GeckoRuntime 实例 + val settingsBuilder = org.mozilla.geckoview.GeckoRuntimeSettings.Builder() + .aboutConfigEnabled(true) + .consoleOutput(true) + + if (configFile.exists()) { + settingsBuilder.configFilePath(configFile.absolutePath) + } + + val runtime = GeckoRuntime.create(this.applicationContext, settingsBuilder.build()) + geckoRuntime = runtime + + // 3. 强制清缓存,确保 JS/CSS 等资源获取最新版本 + try { + // runtime.storageController.clearData(ClearFlags.ALL) + Timber.tag("MainActivity").i("GeckoView cache cleared") + } catch (e: Exception) { + Timber.tag("MainActivity").w(e, "Failed to clear GeckoView cache") + } + return runtime } - - // 2. 通过 GeckoRuntimeSettings.Builder 加载该 YAML 配置文件并创建 GeckoRuntime 实例 - val settingsBuilder = org.mozilla.geckoview.GeckoRuntimeSettings.Builder() - .aboutConfigEnabled(true) - .consoleOutput(true) - - if (configFile.exists()) { - settingsBuilder.configFilePath(configFile.absolutePath) - } - - val runtime = GeckoRuntime.create(this, settingsBuilder.build()) - - // 3. 强制清缓存,确保 JS/CSS 等资源获取最新版本 - try { -// runtime.storageController.clearData(ClearFlags.ALL) - Timber.tag("MainActivity").i("GeckoView cache cleared") - } catch (e: Exception) { - Timber.tag("MainActivity").w(e, "Failed to clear GeckoView cache") - } - return runtime } private fun createSession(): GeckoSession { diff --git a/app/src/main/java/com/stand/standapp/utils/GpioManager.kt b/app/src/main/java/com/stand/standapp/utils/GpioManager.kt index 351ad29..4fe87ce 100644 --- a/app/src/main/java/com/stand/standapp/utils/GpioManager.kt +++ b/app/src/main/java/com/stand/standapp/utils/GpioManager.kt @@ -47,6 +47,9 @@ object GpioManager { handleGpioChange(currentValue) lastGpioValue = currentValue } + } catch (e: InterruptedException) { + Timber.tag("GPIO").i("GPIO thread interrupted, exiting gracefully") + break } catch (e: Exception) { Timber.tag("GPIO").e(e, "Error in GPIO loop") }