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: status bar icons go dark on the light theme

Duarte Santose2a757a52de6
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 0c3ffc7b1..e51937661 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
@@ -24,6 +24,7 @@ public class MainActivity extends BridgeActivity {
registerPlugin(PrintPlugin.class);
registerPlugin(RestAlertPlugin.class);
registerPlugin(BackupFolderPlugin.class);
+ registerPlugin(SystemBarsPlugin.class);
super.onCreate(savedInstanceState);
passSystemBarsToPage();
}
diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/SystemBarsPlugin.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/SystemBarsPlugin.java
new file mode 100644
index 000000000..b42f71722
--- /dev/null
+++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/SystemBarsPlugin.java
@@ -0,0 +1,43 @@
+package ch.duartesantos.opengym;
+
+import android.app.Activity;
+import android.os.Build;
+import android.view.Window;
+import androidx.core.view.WindowCompat;
+import androidx.core.view.WindowInsetsControllerCompat;
+import com.getcapacitor.Plugin;
+import com.getcapacitor.PluginCall;
+import com.getcapacitor.PluginMethod;
+import com.getcapacitor.annotation.CapacitorPlugin;
+
+/**
+ * Android 15 draws the page under the status bar and the navigation bar, so the clock, the battery
+ * and the three buttons sit on the page's own background. The system draws them white unless told
+ * otherwise, and on the light theme (#f2f2f7) they all but vanished. The page tells this plugin
+ * which theme it resolved to (lib/system-bars.js), at start and on every change, and the bars
+ * follow with dark icons on light and light icons on dark. Before Android 15 the bars keep their
+ * own background and there is nothing to change.
+ *
+ * Usage from JS:
+ * registerPlugin('SystemBars').setStyle({ light: true })
+ */
+@CapacitorPlugin(name = "SystemBars")
+public class SystemBarsPlugin extends Plugin {
+
+ @PluginMethod
+ public void setStyle(PluginCall call) {
+ final boolean light = Boolean.TRUE.equals(call.getBoolean("light", false));
+ final Activity activity = getActivity();
+ if (Build.VERSION.SDK_INT < 35 || activity == null) {
+ call.resolve();
+ return;
+ }
+ activity.runOnUiThread(() -> {
+ Window window = activity.getWindow();
+ WindowInsetsControllerCompat bars = WindowCompat.getInsetsController(window, window.getDecorView());
+ bars.setAppearanceLightStatusBars(light);
+ bars.setAppearanceLightNavigationBars(light);
+ call.resolve();
+ });
+ }
+}
diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx
index 73434f26b..5b5f8f4a6 100644
--- a/frontend/src/App.jsx
+++ b/frontend/src/App.jsx
@@ -9,6 +9,7 @@ import { effectiveLang } from './lib/default-lang.js'
import { setPlayOnSilent, setVibrate, setAlarmBuzzer } from './lib/sound.js'
import { buzzAsAlarm } from './lib/rest-alert.js'
import { setNav } from './lib/nav.js'
+import { setSystemBarsLight } from './lib/system-bars.js'
import { initBackButton } from './lib/back.js'
import { useWakeLock } from './lib/wakelock.js'
import { installViewportGuard } from './lib/viewport-guard.js'
@@ -60,6 +61,7 @@ function applyPrefs(theme, accent) {
de.dataset.accent = ACCENTS[accent] ? accent : 'lime'
const meta = document.querySelector('meta[name="theme-color"]')
if (meta) meta.content = de.dataset.theme === 'light' ? '#f2f2f7' : '#000000'
+ if (MOBILE) setSystemBarsLight(de.dataset.theme === 'light')
}
function Shell() {
diff --git a/frontend/src/lib/system-bars.js b/frontend/src/lib/system-bars.js
new file mode 100644
index 000000000..fdd24681a
--- /dev/null
+++ b/frontend/src/lib/system-bars.js
@@ -0,0 +1,27 @@
+// Android app: the status bar and the navigation bar draw on top of the page (Android 15, edge to
+// edge), white unless the native side is told the page is light. On the light theme the clock and
+// the battery all but vanished on #f2f2f7. App.jsx calls this with every theme it resolves to; the
+// SystemBars plugin (SystemBarsPlugin.java) flips the icons dark on light and back.
+import { isAndroid } from './mobile.js'
+
+// Registered once, and kept inside an object: a Capacitor plugin proxy answers `then` with a native
+// call that never settles, so a promise resolved with the bare proxy would hang (the Coach hang, #42).
+let pluginP = null
+const barsPlugin = () => pluginP || (pluginP = (async () => {
+ if (!(await isAndroid())) return null
+ const { registerPlugin } = await import('@capacitor/core')
+ return { SystemBars: registerPlugin('SystemBars') }
+})().catch(() => null))
+
+let last = null
+export async function setSystemBarsLight(light) {
+ light = !!light
+ if (last === light) return
+ last = light
+ try {
+ const p = await barsPlugin()
+ if (p) await p.SystemBars.setStyle({ light })
+ } catch {
+ // An older APK without the plugin: the bars stay as they were.
+ }
+}
diff --git a/frontend/src/lib/system-bars.test.js b/frontend/src/lib/system-bars.test.js
new file mode 100644
index 000000000..05c310437
--- /dev/null
+++ b/frontend/src/lib/system-bars.test.js
@@ -0,0 +1,66 @@
+// On the Android app the status bar icons stayed white on the light theme and could not be read
+// (Android 15 draws the page under them). The page tells the native side which theme it resolved
+// to; nothing else may hear about it, and an APK without the plugin must not break the theme.
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { readFileSync } from 'node:fs'
+
+const h = vi.hoisted(() => {
+ vi.stubEnv('VITE_MOBILE', '1')
+ return { platform: 'android', calls: [], fail: false }
+})
+
+const pluginProxy = () => new Proxy({}, {
+ get: (_, prop) => {
+ if (prop === 'then') return () => new Promise(() => {})
+ return async arg => {
+ if (h.fail) throw new Error('"SystemBars" plugin is not implemented on android')
+ h.calls.push([String(prop), arg])
+ }
+ },
+})
+
+vi.mock('@capacitor/core', () => ({
+ Capacitor: { getPlatform: () => h.platform },
+ registerPlugin: () => pluginProxy(),
+}))
+
+beforeEach(() => {
+ vi.resetModules()
+ h.calls = []
+ h.platform = 'android'
+ h.fail = false
+})
+
+describe('system bars follow the theme on Android', () => {
+ it('asks for dark icons on light and light icons on dark, once per change', async () => {
+ const { setSystemBarsLight } = await import('./system-bars.js')
+ await setSystemBarsLight(true)
+ await setSystemBarsLight(true)
+ await setSystemBarsLight(false)
+ expect(h.calls).toEqual([['setStyle', { light: true }], ['setStyle', { light: false }]])
+ })
+
+ it('stays quiet off Android', async () => {
+ h.platform = 'ios'
+ const { setSystemBarsLight } = await import('./system-bars.js')
+ await setSystemBarsLight(true)
+ expect(h.calls).toEqual([])
+ })
+
+ it('an APK without the plugin is not an error', async () => {
+ h.fail = true
+ const { setSystemBarsLight } = await import('./system-bars.js')
+ await expect(setSystemBarsLight(true)).resolves.toBeUndefined()
+ })
+
+ it('the app sends every resolved theme, and the activity registers the plugin', () => {
+ const app = readFileSync(new URL('../App.jsx', import.meta.url), 'utf8')
+ expect(app).toMatch(/function applyPrefs[\s\S]*?setSystemBarsLight\(de\.dataset\.theme === 'light'\)/)
+ const dir = '../../android/app/src/main/java/ch/duartesantos/opengym/'
+ const activity = readFileSync(new URL(dir + 'MainActivity.java', import.meta.url), 'utf8')
+ const plugin = readFileSync(new URL(dir + 'SystemBarsPlugin.java', import.meta.url), 'utf8')
+ expect(activity).toMatch(/registerPlugin\(SystemBarsPlugin\.class\);[\s\S]*super\.onCreate/)
+ expect(plugin).toContain('@CapacitorPlugin(name = "SystemBars")')
+ expect(plugin).toMatch(/setAppearanceLightStatusBars\(light\)/)
+ })
+})