[tauri] KeepHerSafeApps: App words for Keep her safe, and Android's own save screen through a narrow bridge

This commit is contained in:
2026-11-17 20:31:35 +01:00
parent 0b72ae05a2
commit 3c4463dbdd
9 changed files with 341 additions and 13 deletions
@@ -0,0 +1,87 @@
package cz.mareknovak.domi.rosea
// Rosea's own MainActivity, copied over the one `tauri android init` generates by
// apps/rosea-tauri/scripts/android.ts (gen/ is never committed; `android.ts check` compares it).
// .agents/features/keep-her-safe-apps.md A2–A5.
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.webkit.JavascriptInterface
import android.webkit.WebView
import androidx.activity.enableEdgeToEdge
import androidx.annotation.Keep
class MainActivity : TauriActivity() {
private var webView: WebView? = null
private var busy = false
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
}
// *Save her to a file*: Android WebView does nothing with a blob download, so the game asks
// here instead (window.roseaFile.save), and Android's own save screen lets the player choose
// where the file goes. No permission is needed for it on any Android version.
override fun onWebViewCreate(webView: WebView) {
this.webView = webView
webView.addJavascriptInterface(RoseFile(), "roseaFile")
}
/** Tells the game whether the file was written (A4): true, or false when the player stepped back. */
private fun answer(written: Boolean) {
busy = false
val view = webView ?: return
view.post {
view.evaluateJavascript("window.dispatchEvent(new CustomEvent('rosea-file-saved', { detail: $written }))", null)
}
}
@Keep
inner class RoseFile {
/** Only a rose file: its dated name and a bounded text (A3). One save at a time. */
@Keep
@JavascriptInterface
fun save(name: String, text: String) {
if (!NAME.matches(name) || text.length > MAX_CHARS) {
Log.w(TAG, "refused a file that is not a rose file: ${name.take(64)}, ${text.length} chars")
answer(false)
return
}
runOnUiThread {
if (busy) return@runOnUiThread
busy = true
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/json"
putExtra(Intent.EXTRA_TITLE, name)
}
launchActivityForResult(intent) { result ->
val uri = result?.data?.data
if (result?.resultCode != Activity.RESULT_OK || uri == null) {
answer(false)
return@launchActivityForResult
}
val written = try {
contentResolver.openOutputStream(uri, "wt")?.use { out ->
out.write(text.toByteArray(Charsets.UTF_8))
true
} ?: false
} catch (error: Exception) {
Log.e(TAG, "the rose file could not be written", error)
false
}
answer(written)
}
}
}
}
companion object {
private const val TAG = "RoseaFile"
private val NAME = Regex("^rose-\\d{4}-\\d{2}-\\d{2}\\.json$")
private const val MAX_CHARS = 8 * 1024 * 1024
}
}