Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

Android 15: the system bars are read on the WebView's parent, so the WebView keeps its own insets listener and still reports the camera cutout itself

Duarte Santos489c09162df5
diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/MainActivity.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/MainActivity.java
index a37f932f4..fb5b5a8cb 100644
--- a/frontend/android/app/src/main/java/ch/duartesantos/opengym/MainActivity.java
+++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/MainActivity.java
@@ -2,6 +2,7 @@ package ch.duartesantos.opengym;
import android.os.Build;
import android.os.Bundle;
+import android.view.View;
import android.webkit.WebView;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
@@ -36,14 +37,17 @@ public class MainActivity extends BridgeActivity {
private void passSystemBarsToPage() {
if (Build.VERSION.SDK_INT < 35 || bridge == null) return;
WebView web = bridge.getWebView();
- if (web == null) return;
- ViewCompat.setOnApplyWindowInsetsListener(web, (v, insets) -> {
+ if (web == null || !(web.getParent() instanceof View)) return;
+ // Read on the WebView's parent, which hands them on to the WebView untouched. A listener on
+ // the WebView itself would replace the one the WebView installs for its own insets, and
+ // env(safe-area-inset-top) went to 0 on every start.
+ View holder = (View) web.getParent();
+ ViewCompat.setOnApplyWindowInsetsListener(holder, (v, insets) -> {
Insets bars = insets.getInsets(WindowInsetsCompat.Type.systemBars() | WindowInsetsCompat.Type.displayCutout());
float density = v.getResources().getDisplayMetrics().density;
barTop = bars.top / density;
barBottom = bars.bottom / density;
applyBars(web);
- // The WebView's own handling, which is where env(safe-area-inset-*) comes from.
return ViewCompat.onApplyWindowInsets(v, insets);
});
// A page that loads (the first one, or a reload) starts without them.
@@ -53,7 +57,7 @@ public class MainActivity extends BridgeActivity {
applyBars(view);
}
});
- ViewCompat.requestApplyInsets(web);
+ ViewCompat.requestApplyInsets(holder);
}
private void applyBars(WebView web) {
diff --git a/frontend/src/lib/android-system-bars.test.js b/frontend/src/lib/android-system-bars.test.js
index ed3e53902..5a09b138c 100644
--- a/frontend/src/lib/android-system-bars.test.js
+++ b/frontend/src/lib/android-system-bars.test.js
@@ -33,8 +33,15 @@ describe('the system bars on Android', () => {
expect(activity).toMatch(/getDisplayMetrics\(\)\.density/)
})
- it('passes them again to every page that loads, and leaves the WebView its own handling', () => {
+ it('passes them again to every page that loads', () => {
expect(activity).toMatch(/addWebViewListener[\s\S]*onPageLoaded[\s\S]*applyBars\(view\)/)
+ })
+
+ // The first version set its listener on the WebView, which replaced the WebView's own: env()
+ // reported 0 on every start, where it had reported the cutout on most.
+ it('reads them on the WebView\'s parent, and leaves the WebView its own listener', () => {
+ expect(activity).toMatch(/ViewCompat\.setOnApplyWindowInsetsListener\(holder,/)
+ expect(activity).not.toMatch(/setOnApplyWindowInsetsListener\(web\b/)
expect(activity).toMatch(/return ViewCompat\.onApplyWindowInsets\(v, insets\);/)
})
})