From 2b90edc3f4fee6a0985ee904b445e004b872c451 Mon Sep 17 00:00:00 2001 From: Jonas Thelemann Date: Sun, 22 Nov 2026 16:54:48 +0100 Subject: [PATCH 1/4] feat(offline): show a hint on a first launch without internet --- app/src/main/AndroidManifest.xml | 1 + .../main/java/si/maev/twa/LauncherActivity.kt | 37 +++++++++++++++++++ app/src/main/res/layout/activity_offline.xml | 28 ++++++++++++++ app/src/main/res/values-de/strings.xml | 2 + app/src/main/res/values-night/colors.xml | 1 + app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 3 ++ play-notes/first-launch/de-DE.txt | 1 + play-notes/first-launch/en-US.txt | 1 + 9 files changed, 75 insertions(+) create mode 100644 app/src/main/res/layout/activity_offline.xml create mode 100644 play-notes/first-launch/de-DE.txt create mode 100644 play-notes/first-launch/en-US.txt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0c3540e..c36dc80 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,6 +23,7 @@ the abbreviated format. --> + (R.id.retryButton).setOnClickListener { retryLaunch() } + } + } + + // the web app's service worker, which serves its offline page, only exists after one online launch + override fun shouldLaunchImmediately(): Boolean = false + + private fun retryLaunch() { + if (!isOnline()) return + getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE).edit { putBoolean(KEY_LAUNCHED_BEFORE, true) } + launchTwa() + } + + private fun isOnline(): Boolean { + val manager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val capabilities = manager.getNetworkCapabilities(manager.activeNetwork) + return capabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true } override fun createTwaLauncher(): TwaLauncher { @@ -40,4 +72,9 @@ class LauncherActivity : com.google.androidbrowserhelper.trusted.LauncherActivit // mirrors the web app's `protocol_handlers`, which Chrome does not apply to a TWA override fun getProtocolHandlers(): Map = mapOf("web+vibetype" to "https://${getString(R.string.hostName)}/open?url=%s".toUri()) + + private companion object { + const val PREFERENCES_NAME = "si.maev.twa.launch" + const val KEY_LAUNCHED_BEFORE = "launchedBefore" + } } diff --git a/app/src/main/res/layout/activity_offline.xml b/app/src/main/res/layout/activity_offline.xml new file mode 100644 index 0000000..8d91ab8 --- /dev/null +++ b/app/src/main/res/layout/activity_offline.xml @@ -0,0 +1,28 @@ + + + + + +