121 lines
4.7 KiB
Kotlin
121 lines
4.7 KiB
Kotlin
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.content.res.Configuration
|
||
import android.graphics.Color
|
||
import android.os.Bundle
|
||
import android.util.Log
|
||
import android.webkit.JavascriptInterface
|
||
import android.webkit.WebView
|
||
import androidx.activity.enableEdgeToEdge
|
||
import androidx.annotation.Keep
|
||
import androidx.core.graphics.Insets
|
||
import androidx.core.view.ViewCompat
|
||
import androidx.core.view.WindowInsetsCompat
|
||
|
||
class MainActivity : TauriActivity() {
|
||
private var webView: WebView? = null
|
||
private var busy = false
|
||
|
||
override fun onCreate(savedInstanceState: Bundle?) {
|
||
enableEdgeToEdge()
|
||
super.onCreate(savedInstanceState)
|
||
// Tauri's activity draws edge to edge, and the WebView does not reliably report the bars as
|
||
// env(safe-area-inset-*): the clock sat over the title and the navigation bar over *Bring her
|
||
// back from a file* (keep-her-safe-apps review). So the page is kept between the bars, and
|
||
// the bars show the garden's own paper, light or dark as the system is.
|
||
paintBehindBars()
|
||
val bars = WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()
|
||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(android.R.id.content)) { view, insets ->
|
||
val inset = insets.getInsets(bars)
|
||
view.setPadding(inset.left, inset.top, inset.right, inset.bottom)
|
||
WindowInsetsCompat.Builder(insets)
|
||
.setInsets(WindowInsetsCompat.Type.systemBars(), Insets.NONE)
|
||
.setInsets(WindowInsetsCompat.Type.displayCutout(), Insets.NONE)
|
||
.build()
|
||
}
|
||
}
|
||
|
||
// The manifest lets the activity live through a theme change (uiMode), so the paint follows it here.
|
||
override fun onConfigurationChanged(newConfig: Configuration) {
|
||
super.onConfigurationChanged(newConfig)
|
||
paintBehindBars()
|
||
}
|
||
|
||
private fun paintBehindBars() {
|
||
val night = (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
|
||
window.decorView.setBackgroundColor(Color.parseColor(if (night) PAPER_DARK else PAPER))
|
||
}
|
||
|
||
// *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
|
||
// --paper in packages/ui/src/tokens.css, light and dark; a test holds them equal.
|
||
private const val PAPER = "#fbf6f1"
|
||
private const val PAPER_DARK = "#1d1819"
|
||
}
|
||
}
|