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:
gmp
2026-08-20 18:24:43 -04:00
parent 2eb18b5c70
commit 2bdff5c8e8
8 changed files with 164 additions and 34 deletions
+24 -1
View File
@@ -363,8 +363,31 @@ new2 = ".method public onCreate(Landroid/os/Bundle;)V\n .locals 3"
if old2 not in t:
sys.exit("ERROR: GioActivity onCreate .locals anchor not found")
t = t.replace(old2, new2)
# 3. onStop hook: persist the session snapshot the moment the activity goes
# away (recents-wipe, app switch). The OS provides no user-space hook for
# the process kill that follows, so this is the last reliable flush. Go
# side: pad_flush_session (impl_android.go) -> Logic.FlushSession.
old3 = """.method public onStop()V
.locals 1
.line 47
iget-object v0, p0, Lorg/gioui/GioActivity;->view:Lorg/gioui/GioView;"""
new3 = """.method public static native padFlushSession()V
.end method
.method public onStop()V
.locals 1
# Pad: persist the session snapshot before the activity goes away.
invoke-static {}, Lorg/gioui/GioActivity;->padFlushSession()V
.line 47
iget-object v0, p0, Lorg/gioui/GioActivity;->view:Lorg/gioui/GioView;"""
if old3 not in t:
sys.exit("ERROR: GioActivity onStop anchor not found")
t = t.replace(old3, new3)
p.write_text(t)
print("patched: GioActivity IME insets wiring")
print("patched: GioActivity IME insets wiring + onStop session flush")
PYEOF
echo "=== apktool rebuild ==="
+24 -1
View File
@@ -362,8 +362,31 @@ new2 = ".method public onCreate(Landroid/os/Bundle;)V\n .locals 3"
if old2 not in t:
sys.exit("ERROR: GioActivity onCreate .locals anchor not found")
t = t.replace(old2, new2)
# 3. onStop hook: persist the session snapshot the moment the activity goes
# away (recents-wipe, app switch). The OS provides no user-space hook for
# the process kill that follows, so this is the last reliable flush. Go
# side: pad_flush_session (impl_android.go) -> Logic.FlushSession.
old3 = """.method public onStop()V
.locals 1
.line 47
iget-object v0, p0, Lorg/gioui/GioActivity;->view:Lorg/gioui/GioView;"""
new3 = """.method public static native padFlushSession()V
.end method
.method public onStop()V
.locals 1
# Pad: persist the session snapshot before the activity goes away.
invoke-static {}, Lorg/gioui/GioActivity;->padFlushSession()V
.line 47
iget-object v0, p0, Lorg/gioui/GioActivity;->view:Lorg/gioui/GioView;"""
if old3 not in t:
sys.exit("ERROR: GioActivity onStop anchor not found")
t = t.replace(old3, new3)
p.write_text(t)
print("patched: GioActivity IME insets wiring")
print("patched: GioActivity IME insets wiring + onStop session flush")
PYEOF
echo "=== apktool rebuild ==="