From fe44ec2ae5378e67ce8550a6ac7afb6badaaa097 Mon Sep 17 00:00:00 2001 From: abb128 Date: Mon, 21 Sep 2026 19:17:02 +0200 Subject: [PATCH] Fix Android inset bug (#1934) --- gui/app/src/AppLayout.tsx | 7 +++ gui/app/src/vite-env.d.ts | 1 + .../java/dev/slimevr/android/MainActivity.kt | 56 ++++++++++++++++++- .../src/main/res/layout/activity_main.xml | 1 + .../src/main/res/values-night/themes.xml | 1 - server/android/src/main/res/values/themes.xml | 1 - 6 files changed, 63 insertions(+), 4 deletions(-) diff --git a/gui/app/src/AppLayout.tsx b/gui/app/src/AppLayout.tsx index ab067c3a9..7698616ea 100644 --- a/gui/app/src/AppLayout.tsx +++ b/gui/app/src/AppLayout.tsx @@ -26,6 +26,13 @@ export function AppLayout() { `${config.textSize}rem` ); } + + if (window.__ANDROID__?.isThere()) { + const color = window + .getComputedStyle(document.body) + .getPropertyValue('--background-80'); + window.__ANDROID__?.updateInsetBackground(color); + } }, [config]); useLayoutEffect(() => { diff --git a/gui/app/src/vite-env.d.ts b/gui/app/src/vite-env.d.ts index 1b4825861..ab109f1a6 100644 --- a/gui/app/src/vite-env.d.ts +++ b/gui/app/src/vite-env.d.ts @@ -9,6 +9,7 @@ interface Window { readonly __ANDROID__: | { isThere: () => boolean; + updateInsetBackground: (c: string) => void; } | undefined; } diff --git a/server/android/src/main/java/dev/slimevr/android/MainActivity.kt b/server/android/src/main/java/dev/slimevr/android/MainActivity.kt index e8573d3be..127bcaf2e 100644 --- a/server/android/src/main/java/dev/slimevr/android/MainActivity.kt +++ b/server/android/src/main/java/dev/slimevr/android/MainActivity.kt @@ -6,9 +6,11 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.content.pm.PackageManager +import android.graphics.Color import android.os.Build import android.os.Bundle import android.util.Log +import android.view.WindowInsetsController import android.webkit.JavascriptInterface import android.webkit.WebResourceRequest import android.webkit.WebResourceResponse @@ -16,13 +18,19 @@ import android.webkit.WebView import android.webkit.WebViewClient import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity +import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import java.io.IOException import java.net.URLConnection -class AndroidJsObject { +class AndroidJsObject(val updateBackground: (String) -> Unit) { @JavascriptInterface fun isThere(): Boolean = true + + @JavascriptInterface + fun updateInsetBackground(color: String) = updateBackground(color) } class MainActivity : AppCompatActivity() { @@ -34,6 +42,30 @@ class MainActivity : AppCompatActivity() { override fun onReceive(context: Context?, intent: Intent?) = finishAndRemoveTask() } + private fun updateBackground(newColor: String) { + val rgb = newColor.split(",").map { it.trim().toInt() } + if (rgb.size != 3) { + Log.e(TAG, "RGB string is malformed: $newColor") + return + } + + val colorInt = Color.rgb(rgb[0], rgb[1], rgb[2]) + + val parentView = findViewById(R.id.constraintLayoutView) + parentView.setBackgroundColor(colorInt) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + window.isNavigationBarContrastEnforced = false + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + val mask = WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS or WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS + val appearance = if (Color.luminance(colorInt) > 0.5) mask else 0 + + window.decorView.windowInsetsController?.setSystemBarsAppearance(appearance, mask) + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -89,7 +121,27 @@ class MainActivity : AppCompatActivity() { guiWebView.settings.javaScriptEnabled = true guiWebView.settings.domStorageEnabled = true - guiWebView.addJavascriptInterface(AndroidJsObject(), "__ANDROID__") + val parentView = findViewById(R.id.constraintLayoutView) + + ViewCompat.setOnApplyWindowInsetsListener(parentView) { v, windowInsets -> + val insets = windowInsets.getInsets( + WindowInsetsCompat.Type.systemBars() + or WindowInsetsCompat.Type.displayCutout() + or WindowInsetsCompat.Type.ime(), + ) + + v.setPadding(insets.left, insets.top, insets.right, insets.bottom) + WindowInsetsCompat.CONSUMED + } + + guiWebView.addJavascriptInterface( + AndroidJsObject(updateBackground = { newColor -> + parentView.post { + updateBackground(newColor) + } + }), + "__ANDROID__", + ) guiWebView.settings.setSupportZoom(true) guiWebView.settings.useWideViewPort = true diff --git a/server/android/src/main/res/layout/activity_main.xml b/server/android/src/main/res/layout/activity_main.xml index 60d2a2564..7b7a96e52 100644 --- a/server/android/src/main/res/layout/activity_main.xml +++ b/server/android/src/main/res/layout/activity_main.xml @@ -2,6 +2,7 @@ diff --git a/server/android/src/main/res/values-night/themes.xml b/server/android/src/main/res/values-night/themes.xml index 8143d873a..d823c0bfb 100644 --- a/server/android/src/main/res/values-night/themes.xml +++ b/server/android/src/main/res/values-night/themes.xml @@ -14,6 +14,5 @@ false true - true diff --git a/server/android/src/main/res/values/themes.xml b/server/android/src/main/res/values/themes.xml index 7100c341f..8c97e8688 100644 --- a/server/android/src/main/res/values/themes.xml +++ b/server/android/src/main/res/values/themes.xml @@ -14,6 +14,5 @@ false true - true