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 tab bar sits above the navigation bar and the header below the status bar, so with three-button navigation a tap on a tab reaches the tab instead of Home or Recents, and a cold start no longer leaves Connect under the clock

Duarte Santosaf84f8a7eb88
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 adf4a4152..a37f932f4 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
@@ -1,14 +1,71 @@
package ch.duartesantos.opengym;
+import android.os.Build;
import android.os.Bundle;
+import android.webkit.WebView;
+import androidx.core.graphics.Insets;
+import androidx.core.view.ViewCompat;
+import androidx.core.view.WindowInsetsCompat;
import com.getcapacitor.BridgeActivity;
+import com.getcapacitor.WebViewListener;
public class MainActivity extends BridgeActivity {
+ // Where the status bar (or the camera cutout) ends and the navigation bar starts, in CSS
+ // pixels; negative until the window has said.
+ private float barTop = -1;
+ private float barBottom = -1;
+
@Override
public void onCreate(Bundle savedInstanceState) {
registerPlugin(InstallPlugin.class);
registerPlugin(PrintPlugin.class);
registerPlugin(RestAlertPlugin.class);
super.onCreate(savedInstanceState);
+ passSystemBarsToPage();
+ }
+
+ /**
+ * Android 15 draws an app that targets it edge to edge: the page runs under the status bar and
+ * under the navigation bar. The WebView reports only the camera cutout as
+ * env(safe-area-inset-*), and on some cold starts not even that, so the tab bar sat under the
+ * navigation bar (with three buttons, a tap on a tab hit Home or Recents) and now and then the
+ * header sat under the status bar. The window's own insets go to the page as --native-sat and
+ * --native-sab, which index.css takes over env() wherever they are larger. Before Android 15
+ * the window stops at the bars and there is nothing to pass.
+ */
+ 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) -> {
+ 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.
+ bridge.addWebViewListener(new WebViewListener() {
+ @Override
+ public void onPageLoaded(WebView view) {
+ applyBars(view);
+ }
+ });
+ ViewCompat.requestApplyInsets(web);
+ }
+
+ private void applyBars(WebView web) {
+ if (barTop < 0 || barBottom < 0) return;
+ web.evaluateJavascript(barsScript(barTop, barBottom), null);
+ }
+
+ /** Float.toString: a decimal point in every locale, where String.format could write a comma. */
+ static String barsScript(float top, float bottom) {
+ return "(function(e){if(!e)return;"
+ + "e.style.setProperty('--native-sat','" + Float.toString(top) + "px');"
+ + "e.style.setProperty('--native-sab','" + Float.toString(bottom) + "px')"
+ + "})(document.documentElement)";
}
}
diff --git a/frontend/src/index.css b/frontend/src/index.css
index 3414feff1..0cc8ff311 100644
--- a/frontend/src/index.css
+++ b/frontend/src/index.css
@@ -57,8 +57,13 @@
--ease:cubic-bezier(.32,.72,0,1);
--fast:140ms; --med:220ms;
- --sab:env(safe-area-inset-bottom,0px);
- --sat:env(safe-area-inset-top,0px);
+ /* The system bars the page is drawn under. env() is all a browser and the iOS app give; the
+ Android WebView reports only the camera cutout there, and on Android 15, which draws the app
+ under the status and navigation bars, the app passes the bars themselves as --native-sat and
+ --native-sab (MainActivity.java). The larger one wins, so a WebView that learns to report
+ the bars does not count them twice. */
+ --sab:max(env(safe-area-inset-bottom,0px),var(--native-sab,0px));
+ --sat:max(env(safe-area-inset-top,0px),var(--native-sat,0px));
/* height of the connection indicator under the status bar, 0 while there is none — set by
components/SyncBanner.jsx. Whatever starts at the top of the screen starts below it. */
--conn:0px;
diff --git a/frontend/src/lib/android-system-bars.test.js b/frontend/src/lib/android-system-bars.test.js
new file mode 100644
index 000000000..ed3e53902
--- /dev/null
+++ b/frontend/src/lib/android-system-bars.test.js
@@ -0,0 +1,40 @@
+// Android 15 draws the app under the status bar and the navigation bar, and the WebView reports
+// neither as env(safe-area-inset-*): on a Pixel with three-button navigation the tab bar sat under
+// ◀ ● ■, a tap on Stats hit Recents, and on some cold starts the header sat under the clock with its
+// Connect button out of reach (Android QA, v1.3.9). MainActivity passes the window's own insets to
+// the page, and the stylesheet has to take them. Neither half can run here, so this reads both and
+// checks they agree on the contract.
+import { describe, expect, it } from 'vitest'
+import { readFileSync, readdirSync } from 'node:fs'
+
+const read = rel => readFileSync(new URL(rel, import.meta.url), 'utf8')
+const css = read('../index.css')
+const activity = read('../../android/app/src/main/java/ch/duartesantos/opengym/MainActivity.java')
+
+describe('the system bars on Android', () => {
+ it('the stylesheet takes the larger of env() and what the app passes', () => {
+ expect(css).toMatch(/--sat:max\(env\(safe-area-inset-top,0px\),var\(--native-sat,0px\)\);/)
+ expect(css).toMatch(/--sab:max\(env\(safe-area-inset-bottom,0px\),var\(--native-sab,0px\)\);/)
+ })
+
+ it('nothing reads env(safe-area-inset-*) around the two variables', () => {
+ const dir = new URL('../', import.meta.url)
+ for (const f of readdirSync(dir).filter(n => n.endsWith('.css'))) {
+ const uses = read('../' + f).match(/env\(safe-area-inset-[a-z]+/g) || []
+ expect({ f, uses }).toEqual({ f, uses: f === 'index.css' ? ['env(safe-area-inset-bottom', 'env(safe-area-inset-top'] : [] })
+ }
+ })
+
+ it('the app passes the status and navigation bars, under the names the stylesheet reads', () => {
+ expect(activity).toMatch(/WindowInsetsCompat\.Type\.systemBars\(\) \| WindowInsetsCompat\.Type\.displayCutout\(\)/)
+ expect(activity).toContain("'--native-sat'")
+ expect(activity).toContain("'--native-sab'")
+ // In CSS pixels, not the device's.
+ expect(activity).toMatch(/getDisplayMetrics\(\)\.density/)
+ })
+
+ it('passes them again to every page that loads, and leaves the WebView its own handling', () => {
+ expect(activity).toMatch(/addWebViewListener[\s\S]*onPageLoaded[\s\S]*applyBars\(view\)/)
+ expect(activity).toMatch(/return ViewCompat\.onApplyWindowInsets\(v, insets\);/)
+ })
+})