summaryrefslogtreecommitdiff
path: root/gipfel-droid/app
diff options
context:
space:
mode:
Diffstat (limited to 'gipfel-droid/app')
-rw-r--r--gipfel-droid/app/build.gradle.kts31
-rw-r--r--gipfel-droid/app/src/main/AndroidManifest.xml29
-rw-r--r--gipfel-droid/app/src/main/kotlin/org/flpsed/gipfel/AssetServer.kt102
-rw-r--r--gipfel-droid/app/src/main/kotlin/org/flpsed/gipfel/MainActivity.kt111
-rw-r--r--gipfel-droid/app/src/main/res/xml/network_security_config.xml11
5 files changed, 284 insertions, 0 deletions
diff --git a/gipfel-droid/app/build.gradle.kts b/gipfel-droid/app/build.gradle.kts
new file mode 100644
index 0000000..8c11d71
--- /dev/null
+++ b/gipfel-droid/app/build.gradle.kts
@@ -0,0 +1,31 @@
+plugins {
+ id("com.android.application")
+ id("org.jetbrains.kotlin.android")
+}
+
+android {
+ namespace = "org.flpsed.gipfel"
+ compileSdk = 35
+
+ defaultConfig {
+ applicationId = "org.flpsed.gipfel"
+ minSdk = 29
+ targetSdk = 35
+ versionCode = 1
+ versionName = "0.1.0"
+ }
+
+ buildTypes {
+ release {
+ isMinifyEnabled = false
+ }
+ }
+
+ compileOptions {
+ sourceCompatibility = JavaVersion.VERSION_17
+ targetCompatibility = JavaVersion.VERSION_17
+ }
+ kotlinOptions {
+ jvmTarget = "17"
+ }
+}
diff --git a/gipfel-droid/app/src/main/AndroidManifest.xml b/gipfel-droid/app/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..8be2e74
--- /dev/null
+++ b/gipfel-droid/app/src/main/AndroidManifest.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+
+ <!-- The whole point of this wrapper: the browser apps cannot declare
+ ACCESS_MEDIA_LOCATION, so the OS redacts GPS EXIF from every image
+ they read. We declare and request it and get the original bytes. -->
+ <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
+ <uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
+ <!-- required merely to create the loopback ServerSocket; the app
+ never talks to any other host -->
+ <uses-permission android:name="android.permission.INTERNET" />
+
+ <application
+ android:label="gipfel"
+ android:allowBackup="false"
+ android:hardwareAccelerated="true"
+ android:networkSecurityConfig="@xml/network_security_config">
+ <activity
+ android:name=".MainActivity"
+ android:exported="true"
+ android:theme="@android:style/Theme.Material.NoActionBar"
+ android:configChanges="orientation|screenSize|screenLayout|keyboardHidden|uiMode|density|smallestScreenSize">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+</manifest>
diff --git a/gipfel-droid/app/src/main/kotlin/org/flpsed/gipfel/AssetServer.kt b/gipfel-droid/app/src/main/kotlin/org/flpsed/gipfel/AssetServer.kt
new file mode 100644
index 0000000..5221840
--- /dev/null
+++ b/gipfel-droid/app/src/main/kotlin/org/flpsed/gipfel/AssetServer.kt
@@ -0,0 +1,102 @@
+package org.flpsed.gipfel
+
+import java.io.ByteArrayOutputStream
+import java.io.IOException
+import java.io.InputStream
+import java.io.OutputStream
+import java.net.ServerSocket
+import java.net.Socket
+import kotlin.concurrent.thread
+
+/**
+ * Minimal loopback HTTP server that serves the bundled assets.
+ *
+ * The WebView refuses fetch() on file:// origins, and WASM streaming
+ * instantiation needs a real HTTP origin, so we serve the site from
+ * 127.0.0.1 on an ephemeral port. Loopback only; nothing is exposed.
+ */
+class AssetServer(private val assetManager: android.content.res.AssetManager) : AutoCloseable {
+ private val server = ServerSocket(0, 4, java.net.InetAddress.getByName("127.0.0.1"))
+ val port: Int get() = server.localPort
+
+ @Volatile
+ private var running = true
+
+ init {
+ thread(isDaemon = true, name = "gipfel-http") {
+ while (running) {
+ try {
+ val socket = server.accept()
+ thread(isDaemon = true) { handle(socket) }
+ } catch (_: IOException) {
+ running = false
+ }
+ }
+ }
+ }
+
+ private fun handle(socket: Socket) {
+ socket.use { s ->
+ try {
+ val input = s.getInputStream()
+ val requestLine = input.readLine() ?: return
+ val path = requestLine.split(" ").getOrNull(1) ?: "/"
+ // read (and ignore) the remaining headers so the client is happy
+ while (true) {
+ val line = input.readLine() ?: break
+ if (line.isEmpty()) break
+ }
+ val asset = if (path == "/" || path == "/index.html") "www/index.html"
+ else "www/" + path.trimStart('/')
+ val data = try {
+ assetManager.open(asset).use { it.readBytes() }
+ } catch (_: IOException) {
+ respond(s, 404, "text/plain", "not found".toByteArray())
+ return
+ }
+ respond(s, 200, mime(asset), data)
+ } catch (_: IOException) {
+ }
+ }
+ }
+
+ private fun respond(s: Socket, code: Int, ctype: String, body: ByteArray) {
+ val out: OutputStream = s.getOutputStream()
+ val header = buildString {
+ append("HTTP/1.1 $code OK\r\n")
+ append("Content-Type: $ctype\r\n")
+ append("Content-Length: ${body.size}\r\n")
+ append("Cache-Control: no-cache\r\n")
+ append("Access-Control-Allow-Origin: *\r\n")
+ append("Connection: close\r\n")
+ append("\r\n")
+ }
+ out.write(header.toByteArray())
+ out.write(body)
+ out.flush()
+ }
+
+ private fun mime(path: String): String = when {
+ path.endsWith(".wasm") -> "application/wasm"
+ path.endsWith(".js") -> "application/javascript"
+ path.endsWith(".css") -> "text/css"
+ path.endsWith(".html") -> "text/html"
+ path.endsWith(".gz") -> "application/gzip"
+ else -> "application/octet-stream"
+ }
+
+ private fun InputStream.readLine(): String? {
+ val buf = ByteArrayOutputStream(64)
+ while (true) {
+ val b = read()
+ if (b == -1) return if (buf.size() == 0) null else buf.toString("ISO-8859-1")
+ if (b == '\n'.code) return buf.toString("ISO-8859-1").trimEnd('\r')
+ buf.write(b)
+ }
+ }
+
+ override fun close() {
+ running = false
+ try { server.close() } catch (_: IOException) {}
+ }
+}
diff --git a/gipfel-droid/app/src/main/kotlin/org/flpsed/gipfel/MainActivity.kt b/gipfel-droid/app/src/main/kotlin/org/flpsed/gipfel/MainActivity.kt
new file mode 100644
index 0000000..a9e371a
--- /dev/null
+++ b/gipfel-droid/app/src/main/kotlin/org/flpsed/gipfel/MainActivity.kt
@@ -0,0 +1,111 @@
+package org.flpsed.gipfel
+
+import android.Manifest
+import android.app.Activity
+import android.content.Intent
+import android.content.pm.PackageManager
+import android.net.Uri
+import android.os.Bundle
+import android.webkit.ValueCallback
+import android.webkit.WebChromeClient
+import android.webkit.WebView
+import android.webkit.WebViewClient
+
+class MainActivity : Activity() {
+
+ private lateinit var web: WebView
+ private var server: AssetServer? = null
+ private var filePathCallback: ValueCallback<Array<Uri>>? = null
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ web = WebView(this)
+ web.setBackgroundColor(android.graphics.Color.parseColor("#141418"))
+ setContentView(web)
+ with(web.settings) {
+ javaScriptEnabled = true
+ domStorageEnabled = true // localStorage: profiles & settings
+ useWideViewPort = true // honour the viewport meta tag
+ loadWithOverviewMode = true
+ mediaPlaybackRequiresUserGesture = false
+ }
+ web.webViewClient = WebViewClient()
+ web.webChromeClient = object : WebChromeClient() {
+ override fun onShowFileChooser(
+ view: WebView,
+ callback: ValueCallback<Array<Uri>>,
+ params: FileChooserParams
+ ): Boolean {
+ filePathCallback?.onReceiveValue(null)
+ filePathCallback = callback
+ // params.createIntent() carries the accept="image/*" filter
+ startActivityForResult(params.createIntent(), REQ_FILE)
+ return true
+ }
+ }
+
+ val srv = AssetServer(assets)
+ server = srv
+ web.loadUrl("http://127.0.0.1:${srv.port}/index.html")
+
+ requestMediaPermissions()
+ }
+
+ /**
+ * The reason this wrapper exists: with ACCESS_MEDIA_LOCATION granted the
+ * OS hands us the ORIGINAL files (GPS EXIF intact) instead of the
+ * location-redacted copies browsers are forced to see.
+ */
+ private fun requestMediaPermissions() {
+ val needed = listOf(
+ Manifest.permission.READ_MEDIA_IMAGES,
+ Manifest.permission.ACCESS_MEDIA_LOCATION
+ ).filter { checkSelfPermission(it) != PackageManager.PERMISSION_GRANTED }
+ if (needed.isNotEmpty()) {
+ requestPermissions(needed.toTypedArray(), REQ_PERMS)
+ }
+ }
+
+ override fun onRequestPermissionsResult(
+ requestCode: Int, permissions: Array<out String>, grantResults: IntArray
+ ) {
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults)
+ // no special handling: without the grant the app still works,
+ // the OS just redacts GPS EXIF exactly like in a browser
+ }
+
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
+ if (requestCode == REQ_FILE) {
+ val cb = filePathCallback
+ filePathCallback = null
+ val uris: Array<Uri>? = if (resultCode == RESULT_OK && data != null) {
+ val clip = data.clipData
+ when {
+ clip != null -> Array(clip.itemCount) { clip.getItemAt(it).uri }
+ data.data != null -> arrayOf(data.data!!)
+ else -> null
+ }
+ } else null
+ cb?.onReceiveValue(uris)
+ } else {
+ super.onActivityResult(requestCode, resultCode, data)
+ }
+ }
+
+ @Deprecated("Deprecated in Java")
+ override fun onBackPressed() {
+ if (web.canGoBack()) web.goBack() else @Suppress("DEPRECATION") super.onBackPressed()
+ }
+
+ override fun onDestroy() {
+ server?.close()
+ web.destroy()
+ super.onDestroy()
+ }
+
+ companion object {
+ private const val REQ_PERMS = 1
+ private const val REQ_FILE = 2
+ }
+}
diff --git a/gipfel-droid/app/src/main/res/xml/network_security_config.xml b/gipfel-droid/app/src/main/res/xml/network_security_config.xml
new file mode 100644
index 0000000..2434fb8
--- /dev/null
+++ b/gipfel-droid/app/src/main/res/xml/network_security_config.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<network-security-config>
+ <!-- the bundled site is served from a loopback HTTP server so that
+ fetch(), WASM streaming and (later) service workers behave like a
+ real origin; nothing else may talk cleartext -->
+ <domain-config cleartextTrafficPermitted="true">
+ <domain includeSubdomains="false">127.0.0.1</domain>
+ <domain includeSubdomains="false">localhost</domain>
+ </domain-config>
+ <base-config cleartextTrafficPermitted="false" />
+</network-security-config>