mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-24 11:01:20 -08:00
Fix Android inset bug (#1934)
This commit is contained in:
1 parent
d4921c5a25
commit
fe44ec2ae5
6 files changed
+63
-4
No files matched your search
@@ -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(() => {
|
||||
|
||||
Vendored
+1
@@ -9,6 +9,7 @@ interface Window {
|
||||
readonly __ANDROID__:
|
||||
| {
|
||||
isThere: () => boolean;
|
||||
updateInsetBackground: (c: string) => void;
|
||||
}
|
||||
| undefined;
|
||||
}
|
||||
|
||||
@@ -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<ConstraintLayout>(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<ConstraintLayout>(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
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/constraintLayoutView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="dev.slimevr.android.MainActivity">
|
||||
|
||||
@@ -14,6 +14,5 @@
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="windowActionBar">false</item>
|
||||
<item name="windowNoTitle">true</item>
|
||||
<item name="android:windowFullscreen">true</item>
|
||||
</style>
|
||||
</resources>
|
||||
@@ -14,6 +14,5 @@
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="windowActionBar">false</item>
|
||||
<item name="windowNoTitle">true</item>
|
||||
<item name="android:windowFullscreen">true</item>
|
||||
</style>
|
||||
</resources>
|
||||
Reference in new issue
Block a user