diff --git a/server/android/.gitignore b/server/android/.gitignore index c39472d4b..64fd0e9a6 100644 --- a/server/android/.gitignore +++ b/server/android/.gitignore @@ -1,3 +1,3 @@ /build -/src/main/resources/web-gui +/src/main/assets/web-gui /secrets diff --git a/server/android/build.gradle.kts b/server/android/build.gradle.kts index 54afaf0a0..97d31d593 100644 --- a/server/android/build.gradle.kts +++ b/server/android/build.gradle.kts @@ -29,7 +29,7 @@ java { } tasks.register("copyGuiAssets") { - val target = layout.projectDirectory.dir("src/main/resources/web-gui") + val target = layout.projectDirectory.dir("src/main/assets/web-gui") delete(target) from(rootProject.layout.projectDirectory.dir("gui/dist")) into(target) @@ -86,10 +86,6 @@ dependencies { implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar")))) androidTestImplementation("androidx.test.ext:junit:1.3.0") androidTestImplementation("androidx.test.espresso:espresso-core:3.7.0") - // For hosting web GUI - implementation("io.ktor:ktor-server-core:2.3.13") - implementation("io.ktor:ktor-server-netty:2.3.13") - implementation("io.ktor:ktor-server-caching-headers:2.3.13") // Serial implementation("com.github.mik3y:usb-serial-for-android:3.7.0") diff --git a/server/android/src/main/java/dev/slimevr/android/Main.kt b/server/android/src/main/java/dev/slimevr/android/Main.kt index 005f8a6ed..686644560 100644 --- a/server/android/src/main/java/dev/slimevr/android/Main.kt +++ b/server/android/src/main/java/dev/slimevr/android/Main.kt @@ -12,50 +12,15 @@ import dev.slimevr.android.tracking.trackers.hid.AndroidHIDManager import dev.slimevr.config.ConfigManager 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.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 kotlinx.coroutines.runBlocking import java.io.File -import java.time.ZonedDateTime import kotlin.concurrent.thread import kotlin.system.exitProcess -lateinit var webServer: NettyApplicationEngine - private set - -val webServerInitialized: Boolean - get() = ::webServer.isInitialized - -var webServerPort = 0 - lateinit var vrServer: VRServer private set val vrServerInitialized: Boolean get() = ::vrServer.isInitialized -fun startWebServer() { - // Host the web GUI server - webServer = embeddedServer(Netty, port = 0) { - routing { - install(CachingHeaders) { - options { _, _ -> - CachingOptions(CacheControl.NoStore(Visibility.Public), ZonedDateTime.now()) - } - } - staticResources("/", "web-gui", "index.html") - } - }.start(wait = false) - webServerPort = runBlocking { webServer.resolvedConnectors().first().port } -} - fun startVRServer(activity: AppCompatActivity) { thread(start = true, name = "Main VRServer Thread") { try { 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 5d6e2b58a..23453d46b 100644 --- a/server/android/src/main/java/dev/slimevr/android/MainActivity.kt +++ b/server/android/src/main/java/dev/slimevr/android/MainActivity.kt @@ -3,12 +3,16 @@ package dev.slimevr.android import android.content.Intent import android.os.Bundle import android.webkit.JavascriptInterface -import android.webkit.WebSettings +import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse import android.webkit.WebView +import android.webkit.WebViewClient import androidx.appcompat.app.AppCompatActivity import io.eiren.util.logging.LogManager import java.util.concurrent.locks.ReentrantLock import kotlin.concurrent.withLock +import java.io.IOException +import java.net.URLConnection class AndroidJsObject { @JavascriptInterface @@ -28,14 +32,6 @@ class MainActivity : AppCompatActivity() { } 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...") @@ -53,6 +49,35 @@ class MainActivity : AppCompatActivity() { // Enable debug mode WebView.setWebContentsDebuggingEnabled(true) + // Handle path resolution + guiWebView.webViewClient = object : WebViewClient() { + override fun shouldInterceptRequest( + view: WebView, + request: WebResourceRequest, + ): WebResourceResponse? { + if ((request.url.scheme != "http" && request.url.scheme != "https") || + request.url.host != "slimevr.gui" + ) { + return null + } + + val path = when (request.url.path) { + null, "", "/" -> "/index.html" + else -> request.url.path + } + + return try { + WebResourceResponse( + URLConnection.guessContentTypeFromName(path) ?: "text/plain", + null, + assets.open("web-gui$path"), + ) + } catch (_: IOException) { + WebResourceResponse(null, null, null) + } + } + } + // Set required features guiWebView.settings.javaScriptEnabled = true guiWebView.settings.domStorageEnabled = true @@ -66,12 +91,8 @@ class MainActivity : AppCompatActivity() { guiWebView.settings.loadWithOverviewMode = true guiWebView.invokeZoomPicker() - // Disable cache! This is all local anyway - guiWebView.settings.cacheMode = WebSettings.LOAD_NO_CACHE - guiWebView.clearCache(true) - // Load GUI page - guiWebView.loadUrl("http://127.0.0.1:$webServerPort/") + guiWebView.loadUrl("https://slimevr.gui/") LogManager.info("[MainActivity] GUI WebView has been initialized and loaded.") // Start a foreground service to notify the user the SlimeVR Server is running diff --git a/server/android/src/main/resources/icon128.png b/server/android/src/main/resources/icon128.png deleted file mode 100644 index 4b0018856..000000000 Binary files a/server/android/src/main/resources/icon128.png and /dev/null differ diff --git a/server/android/src/main/resources/icon16.png b/server/android/src/main/resources/icon16.png deleted file mode 100644 index c3fc44e5c..000000000 Binary files a/server/android/src/main/resources/icon16.png and /dev/null differ diff --git a/server/android/src/main/resources/icon256.png b/server/android/src/main/resources/icon256.png deleted file mode 100644 index 5a28fa19a..000000000 Binary files a/server/android/src/main/resources/icon256.png and /dev/null differ diff --git a/server/android/src/main/resources/icon32.png b/server/android/src/main/resources/icon32.png deleted file mode 100644 index a810a952a..000000000 Binary files a/server/android/src/main/resources/icon32.png and /dev/null differ diff --git a/server/android/src/main/resources/icon48.png b/server/android/src/main/resources/icon48.png deleted file mode 100644 index 6f42ef27b..000000000 Binary files a/server/android/src/main/resources/icon48.png and /dev/null differ diff --git a/server/android/src/main/resources/icon64.png b/server/android/src/main/resources/icon64.png deleted file mode 100644 index c0315fb1b..000000000 Binary files a/server/android/src/main/resources/icon64.png and /dev/null differ