From 5af62e61cdfb4368ce731e425f26b1ca4d3c87aa Mon Sep 17 00:00:00 2001 From: dadaloop82 Date: Mon, 4 May 2026 17:36:11 +0000 Subject: [PATCH] fix(kiosk): APK install error reporting + gateway pre-configuration ErrorReporter: - Init ErrorReporter at Setup onCreate using any previously saved URL (before the fix, init() was only called at step 3, so install errors happening in step 4 were silently dropped) - Add ErrorReporter.reportMessage() call in the STATUS_FAILURE else branch of installWithPackageInstaller (was showing error UI but sending nothing) Gateway pre-configuration: - finishSetup() now detects has_scale=true + gateway installed - If so, POSTs scale_enabled=true + scale_gateway_url=ws://127.0.0.1:8765 to the EverShelf server's save_settings API endpoint - This means the webapp works with the scale out-of-the-box after setup without the user having to go into web Settings and configure it manually --- .../dadaloop/evershelf/kiosk/SetupActivity.kt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/evershelf-kiosk/app/src/main/kotlin/it/dadaloop/evershelf/kiosk/SetupActivity.kt b/evershelf-kiosk/app/src/main/kotlin/it/dadaloop/evershelf/kiosk/SetupActivity.kt index 7da4c21..0f06b2a 100644 --- a/evershelf-kiosk/app/src/main/kotlin/it/dadaloop/evershelf/kiosk/SetupActivity.kt +++ b/evershelf-kiosk/app/src/main/kotlin/it/dadaloop/evershelf/kiosk/SetupActivity.kt @@ -152,6 +152,10 @@ class SetupActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_setup) prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + // Init ErrorReporter immediately using any previously saved URL (so install + // errors during setup are reported even before the user confirms the URL) + val savedUrl = prefs.getString(KEY_URL, "") ?: "" + if (savedUrl.isNotEmpty()) ErrorReporter.init(this, savedUrl) bindViews() // Restore step from instance state (e.g. after recreate() for locale change) val savedStep = savedInstanceState?.getInt("step", -1) ?: -1 @@ -863,6 +867,11 @@ class SetupActivity : AppCompatActivity() { else -> { val msg = intent?.getStringExtra(android.content.pm.PackageInstaller.EXTRA_STATUS_MESSAGE) ?: "status=$status" setGatewayUI("❌", getString(R.string.install_error_install), msg, 0xFFf87171.toInt()) + ErrorReporter.reportMessage( + "install-failure", + "PackageInstaller failed: status=$status msg=$msg", + mapOf("pkg" to targetPkg, "status" to status, "msg" to msg) + ) val pkgInstalled = try { packageManager.getPackageInfo(targetPkg, 0); true } catch (_: Exception) { false } if (pkgInstalled) runOnUiThread { offerUninstallAndRetry(file, targetPkg) } } @@ -924,6 +933,32 @@ class SetupActivity : AppCompatActivity() { private fun finishSetup() { prefs.edit().putBoolean(KEY_SETUP_COMPLETE, true).apply() + // ── Pre-configure Scale Gateway URL in webapp settings ────────────────── + // If the user has a scale and the gateway is installed, write the WebSocket + // URL and enable the scale in the webapp's server settings (.env via API) + // so the webapp works out-of-the-box without manual settings configuration. + if (prefs.getBoolean(KEY_HAS_SCALE, false) && isGatewayInstalled()) { + val baseUrl = (prefs.getString(KEY_URL, "") ?: "").trimEnd('/') + if (baseUrl.isNotEmpty()) { + val gwUrl = "ws://127.0.0.1:8765" + Thread { + try { + val url = "$baseUrl/api/index.php?action=save_settings" + val body = """{"scale_enabled":true,"scale_gateway_url":"$gwUrl"}""" + val conn = (java.net.URL(url).openConnection() as java.net.HttpURLConnection).apply { + requestMethod = "POST" + setRequestProperty("Content-Type", "application/json") + connectTimeout = 5000 + readTimeout = 5000 + doOutput = true + } + conn.outputStream.use { it.write(body.toByteArray()) } + conn.inputStream.close() + conn.disconnect() + } catch (_: Exception) {} + }.start() + } + } setResult(RESULT_OK) finish() }