Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
the abbreviated format. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
Expand Down
63 changes: 63 additions & 0 deletions app/src/main/java/si/maev/twa/LauncherActivity.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package si.maev.twa

import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import androidx.browser.customtabs.CustomTabsCallback
import androidx.browser.customtabs.CustomTabsCallback.NAVIGATION_FINISHED
import androidx.core.content.FileProvider
import androidx.core.content.edit
import androidx.core.net.toUri
import com.google.androidbrowserhelper.trusted.QualityEnforcer
import com.google.androidbrowserhelper.trusted.SessionStore
import com.google.androidbrowserhelper.trusted.SharedPreferencesTokenStore
import java.io.File
import java.lang.ref.WeakReference

class LauncherActivity : com.google.androidbrowserhelper.trusted.LauncherActivity() {

Expand All @@ -26,6 +35,53 @@ class LauncherActivity : com.google.androidbrowserhelper.trusted.LauncherActivit
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION,
)
super.onCreate(savedInstanceState)
if (isFinishing) return

// a share or link that arrives while the hint shows starts another instance, which replaces it
offlineHint?.get()?.finish()

if (hasLoadedBefore() || isOnline()) {
launchTwa()
} else {
offlineHint = WeakReference(this)
setContentView(R.layout.activity_offline)
findViewById<Button>(R.id.retryButton).setOnClickListener { if (isOnline()) launchTwa() }
}
}

// the web app's service worker, which serves its offline page, only exists after one online launch
override fun shouldLaunchImmediately(): Boolean = false

// set once the browser finished loading a page, as a launch that gets interrupted earlier has not registered the service worker yet
override fun getCustomTabsCallback(): CustomTabsCallback {
val preferences = applicationContext.getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE)

return object : QualityEnforcer() {
override fun onNavigationEvent(navigationEvent: Int, extras: Bundle?) {
super.onNavigationEvent(navigationEvent, extras)
if (navigationEvent == NAVIGATION_FINISHED) preferences.edit { putBoolean(KEY_LOADED_BEFORE, true) }
}
}
}

override fun onDestroy() {
if (offlineHint?.get() === this) offlineHint = null
super.onDestroy()
}

private fun hasLoadedBefore() =
getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE).getBoolean(KEY_LOADED_BEFORE, false)

// an always-on VPN such as Tailscale is the active network and reports internet even with nothing underneath it, so only non-VPN networks count
@Suppress("DEPRECATION") // `allNetworks` has no synchronous replacement
private fun isOnline(): Boolean {
val manager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return manager.allNetworks.any { network ->
manager.getNetworkCapabilities(network)?.run {
hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
} == true
}
}

override fun createTwaLauncher(): TwaLauncher {
Expand All @@ -40,4 +96,11 @@ 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<String, Uri> =
mapOf("web+vibetype" to "https://${getString(R.string.hostName)}/open?url=%s".toUri())

private companion object {
const val KEY_LOADED_BEFORE = "loadedBefore"
const val PREFERENCES_NAME = "si.maev.twa.launch"

var offlineHint: WeakReference<LauncherActivity>? = null
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/offline_retry_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- mirrors the web app's action-primary and action-primary-hover colors -->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#117a23">
<item>
<shape android:shape="rectangle">
<corners android:radius="16dp" />
<solid android:color="#15852a" />
</shape>
</item>
</ripple>
34 changes: 34 additions & 0 deletions app/src/main/res/layout/activity_offline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor"
android:fitsSystemWindows="true"
android:gravity="center"
android:orientation="vertical">

<!-- margins instead of padding on the root, as fitsSystemWindows replaces that padding with the system bar insets -->
<TextView
android:id="@+id/offlineMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:gravity="center"
android:text="@string/offlineMessage"
android:textColor="@color/offlineTextColor"
android:textSize="18sp" />

<Button
android:id="@+id/retryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@drawable/offline_retry_button"
android:minHeight="48dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:text="@string/offlineRetry"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- The default is generated from the shortcuts in `app/build.gradle.kts`, which lint does not see. -->
<string name="shortcut_name_0" tools:ignore="ExtraTranslation">Veranstaltungen entdecken</string>
<string name="offlineMessage">Du bist offline. Verbinde dich mit dem Internet und versuche es dann erneut.</string>
<string name="offlineRetry">Erneut versuchen</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<!-- splash screen background, mirrors TwaManifest.themeColorDark in app/build.gradle.kts -->
<color name="backgroundColor">#191911</color>
<color name="offlineTextColor">#ffffff</color>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<!-- splash screen background, mirrors TwaManifest.themeColor in app/build.gradle.kts -->
<color name="backgroundColor">#ffffff</color>
<color name="offlineTextColor">#000000</color>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@
}
}
</string>

<string name="offlineMessage">You\'re offline. Reconnect to the internet, then try again.</string>
<string name="offlineRetry">Try again</string>
</resources>
1 change: 1 addition & 0 deletions play-notes/first-launch/de-DE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wenn du die App zum ersten Mal ohne Internet öffnest, zeigt sie jetzt einen Hinweis an und lässt dich es erneut versuchen.
1 change: 1 addition & 0 deletions play-notes/first-launch/en-US.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When you open the app for the first time without internet, it now shows a hint and lets you try again.
Loading