Flush the session snapshot on activity onStop (Android)
The OS provides no user-space hook for a process kill, but the activity onStop fires on every 'going away' transition the framework still controls: entering recents (the swipe-wipe path), app switch, and home. Recents-wipe then kills the process right after onStop returns, so that moment is the last reliable flush. - Logic.FlushSession (any goroutine, buffered, non-blocking) -> flushSessionSave on the owner: persist the snapshot now, bypassing the rate limit (still honoring the restore-pending suppression). saveSessionIfChanged's persist tail is deduplicated into writeSession. - JNI: GioActivity.onStop (patched into the smali by the build scripts, in sync) now calls the static native padFlushSession, which maps to the pad_flush_session cgo export. Verified on emulator: the flush fires on home/app-switch and when the app is backgrounded into recents before a kill; a state change made inside the 250ms rate window and then backed out of the app persists the latest position. A hard kill while foregrounded (force-stop, memory pressure) still has no hook — the immediate edit saves plus the 250ms rate window bound that loss.
This commit is contained in:
+48
-28
@@ -1,4 +1,5 @@
|
||||
//+build !darwin !linux
|
||||
//go:build !darwin || !linux
|
||||
// +build !darwin !linux
|
||||
|
||||
package main
|
||||
|
||||
@@ -17,18 +18,25 @@ import (
|
||||
"unsafe"
|
||||
|
||||
"gioui.org/app"
|
||||
"gioui.org/io/event"
|
||||
_ "gioui.org/app/permission/storage"
|
||||
"gioui.org/io/event"
|
||||
|
||||
"pad/internal/editor"
|
||||
)
|
||||
|
||||
type JNIEnv = C.JNIEnv
|
||||
|
||||
var (
|
||||
startpath="/storage/emulated/0/Notes"
|
||||
jvm uintptr
|
||||
theJVM *C.JavaVM
|
||||
startpath = "/storage/emulated/0/Notes"
|
||||
jvm uintptr
|
||||
theJVM *C.JavaVM
|
||||
)
|
||||
|
||||
func impl_start() { }
|
||||
// activeLogic is set in main before the window loop: the onStop JNI hook
|
||||
// (pad_flush_session, below) needs a handle to the logic it serves.
|
||||
var activeLogic *editor.Logic
|
||||
|
||||
func impl_start() {}
|
||||
|
||||
func handleEvent(e event.Event) {
|
||||
switch e := e.(type) {
|
||||
@@ -41,26 +49,26 @@ func handleEvent(e event.Event) {
|
||||
}
|
||||
|
||||
func RunInJVM(f func(env *C.JNIEnv)) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
var env *C.JNIEnv
|
||||
var detach bool
|
||||
if res := C.GetEnv(theJVM, &env, C.JNI_VERSION_1_6); res != C.JNI_OK {
|
||||
if res != C.JNI_EDETACHED {
|
||||
panic(fmt.Errorf("JNI GetEnv failed with error %d", res))
|
||||
}
|
||||
if C.AttachCurrentThread(theJVM, &env, nil) != C.JNI_OK {
|
||||
panic(errors.New("runInJVM: AttachCurrentThread failed"))
|
||||
}
|
||||
detach = true
|
||||
}
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
var env *C.JNIEnv
|
||||
var detach bool
|
||||
if res := C.GetEnv(theJVM, &env, C.JNI_VERSION_1_6); res != C.JNI_OK {
|
||||
if res != C.JNI_EDETACHED {
|
||||
panic(fmt.Errorf("JNI GetEnv failed with error %d", res))
|
||||
}
|
||||
if C.AttachCurrentThread(theJVM, &env, nil) != C.JNI_OK {
|
||||
panic(errors.New("runInJVM: AttachCurrentThread failed"))
|
||||
}
|
||||
detach = true
|
||||
}
|
||||
|
||||
if detach {
|
||||
defer func() {
|
||||
C.DetachCurrentThread(theJVM)
|
||||
}()
|
||||
}
|
||||
f(env)
|
||||
if detach {
|
||||
defer func() {
|
||||
C.DetachCurrentThread(theJVM)
|
||||
}()
|
||||
}
|
||||
f(env)
|
||||
}
|
||||
|
||||
// SetGestureExclusions forwards the selection-handle grab boxes (view-local
|
||||
@@ -68,6 +76,19 @@ func RunInJVM(f func(env *C.JNIEnv)) {
|
||||
// an edge handle are not stolen by the system back gesture (see
|
||||
// jni_android.c). It is called from the frame loop, which runs on the Android
|
||||
// UI thread. No-op (C-side) before API 29.
|
||||
// pad_flush_session is the cgo export behind GioActivity.onStop (the
|
||||
// build script patches onStop into the smali and declares the matching
|
||||
// static native method). The OS gives no user-space hook for the process
|
||||
// kill that follows a recents-wipe, so onStop is the last reliable moment
|
||||
// to persist the session snapshot.
|
||||
//
|
||||
//export pad_flush_session
|
||||
func pad_flush_session() {
|
||||
if activeLogic != nil {
|
||||
activeLogic.FlushSession()
|
||||
}
|
||||
}
|
||||
|
||||
func SetGestureExclusions(rects [][4]int) {
|
||||
if theJVM == nil {
|
||||
return
|
||||
@@ -97,14 +118,14 @@ func sessionFilePath() string {
|
||||
func OpenFile(path string) {
|
||||
var env *C.JNIEnv
|
||||
var detach bool
|
||||
if res := C.GetEnv(theJVM, &env, C.JNI_VERSION_1_6); res != C.JNI_OK {
|
||||
if res := C.GetEnv(theJVM, &env, C.JNI_VERSION_1_6); res != C.JNI_OK {
|
||||
if res != C.JNI_EDETACHED {
|
||||
panic(fmt.Errorf("JNI GetEnv failed with error %d", res))
|
||||
}
|
||||
if C.AttachCurrentThread(theJVM, &env, nil) != C.JNI_OK {
|
||||
panic(errors.New("OpenFile: AttachCurrentThread failed"))
|
||||
}
|
||||
detach = true
|
||||
detach = true
|
||||
}
|
||||
|
||||
if detach {
|
||||
@@ -116,4 +137,3 @@ func OpenFile(path string) {
|
||||
C.open_file_in_termux(env, cpath)
|
||||
C.free(unsafe.Pointer(cpath))
|
||||
}
|
||||
|
||||
|
||||
+11
-4
@@ -1,16 +1,18 @@
|
||||
//+build !android
|
||||
//go:build !android
|
||||
// +build !android
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"pad/internal/editor"
|
||||
"path/filepath"
|
||||
|
||||
"gioui.org/io/event"
|
||||
)
|
||||
|
||||
var (
|
||||
startpath="."
|
||||
startpath = "."
|
||||
)
|
||||
|
||||
// sessionFilePath is where the relaunch session file (spec §7) lives, off
|
||||
@@ -23,10 +25,15 @@ func sessionFilePath() string {
|
||||
return filepath.Join(os.TempDir(), "pad", "session.json")
|
||||
}
|
||||
|
||||
func handleEvent(e event.Event) { }
|
||||
func handleEvent(e event.Event) {}
|
||||
|
||||
func OpenFile(path string) { }
|
||||
func OpenFile(path string) {}
|
||||
|
||||
// SetGestureExclusions is a no-op off Android (system gesture exclusion
|
||||
// rects are an Android API 29+ feature).
|
||||
func SetGestureExclusions(rects [][4]int) {}
|
||||
|
||||
// activeLogic exists on the Android build (the onStop JNI hook, see
|
||||
// impl_android.go); on other platforms it is unused but main assigns it.
|
||||
var activeLogic *editor.Logic
|
||||
var _ = activeLogic // keep staticcheck quiet on non-Android builds
|
||||
|
||||
@@ -191,3 +191,14 @@ void SetGestureExclusions(JNIEnv *env, jint nrects, const jint *xyxy) {
|
||||
(*env)->DeleteLocalRef(env, runnable);
|
||||
(*env)->DeleteLocalRef(env, list);
|
||||
}
|
||||
|
||||
// Java_org_gioui_GioActivity_padFlushSession
|
||||
//
|
||||
// Called from GioActivity.onStop (injected by the build script): persist
|
||||
// the session snapshot before the process is likely killed. The Go side
|
||||
// (pad_flush_session) only enqueues a request on the logic goroutine's
|
||||
// channel, so this returns immediately on the UI thread.
|
||||
void
|
||||
Java_org_gioui_GioActivity_padFlushSession(JNIEnv *env, jclass cls) {
|
||||
pad_flush_session();
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ func run(w *app.Window) error {
|
||||
log.Printf("using filesystem at / (startup directory: %s)", startAbs)
|
||||
|
||||
logic := editor.NewLogic(fs, startAbs, OpenFile)
|
||||
activeLogic = logic
|
||||
renderer := ui.New(ui.Theme{FontSize: 14}, shaper)
|
||||
|
||||
// In-app frame profiler (default off). Enabled by the presence of a marker
|
||||
|
||||
Reference in New Issue
Block a user