Prevent Android GUI race condition

This commit is contained in:
Butterscotch! committed 2025-12-07 05:12:56 -05:00
1 parent dab6ec28af
commit 364ed3209c
2 files changed
+34 -9

No files matched your search

@@ -14,11 +14,12 @@ import dev.slimevr.tracking.trackers.Tracker
import io.eiren.util.logging.LogManager
import io.ktor.http.CacheControl
import io.ktor.http.CacheControl.Visibility
import io.ktor.server.application.install
import io.ktor.server.engine.EmbeddedServer
import io.ktor.server.engine.embeddedServer
import io.ktor.server.http.content.CachingOptions
import io.ktor.server.http.content.staticResources
import io.ktor.server.netty.Netty
import io.ktor.server.netty.NettyApplicationEngine
import io.ktor.server.plugins.cachingheaders.CachingHeaders
import io.ktor.server.routing.routing
import java.io.File
@@ -26,14 +27,20 @@ import java.time.ZonedDateTime
import kotlin.concurrent.thread
import kotlin.system.exitProcess
lateinit var webServer: EmbeddedServer<NettyApplicationEngine, NettyApplicationEngine.Configuration>
private set
val webServerInitialized: Boolean
get() = ::webServer.isInitialized
lateinit var vrServer: VRServer
private set
val vrServerInitialized: Boolean
get() = ::vrServer.isInitialized
fun main(activity: AppCompatActivity) {
fun startWebServer() {
// Host the web GUI server
embeddedServer(Netty, port = 34536) {
webServer = embeddedServer(Netty, port = 34536) {
routing {
install(CachingHeaders) {
options { _, _ ->
@@ -43,7 +50,9 @@ fun main(activity: AppCompatActivity) {
staticResources("/", "web-gui", "index.html")
}
}.start(wait = false)
}
fun startVRServer(activity: AppCompatActivity) {
thread(start = true, name = "Main VRServer Thread") {
try {
LogManager.initialize(activity.filesDir)
@@ -7,6 +7,8 @@ import android.webkit.WebSettings
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity
import io.eiren.util.logging.LogManager
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
class AndroidJsObject {
@JavascriptInterface
@@ -25,12 +27,22 @@ class MainActivity : AppCompatActivity() {
e1.printStackTrace()
}
// Start the server if it isn't already running
if (!vrServerInitialized) {
LogManager.info("[MainActivity] VRServer isn't running yet, starting it...")
main(this)
} else {
LogManager.info("[MainActivity] VRServer is already running, skipping initialization.")
initLock.withLock {
// Start the GUI if it isn't already running
if (!webServerInitialized) {
LogManager.info("[MainActivity] WebServer isn't running yet, starting it...")
startWebServer()
} else {
LogManager.info("[MainActivity] WebServer is already running, skipping initialization.")
}
// Start the server if it isn't already running
if (!vrServerInitialized) {
LogManager.info("[MainActivity] VRServer isn't running yet, starting it...")
startVRServer(this)
} else {
LogManager.info("[MainActivity] VRServer is already running, skipping initialization.")
}
}
// Load the web GUI web page
@@ -67,4 +79,8 @@ class MainActivity : AppCompatActivity() {
val serviceIntent = Intent(this, ForegroundService::class.java)
startForegroundService(serviceIntent)
}
companion object {
val initLock = ReentrantLock()
}
}