app: notice when Shizuku is authorised

The banner watched only for the binder arriving or dying, and granting
permission does neither. So after the user tapped the banner, answered
the dialog and came back, it still read "running but not authorised" —
wrong at precisely the moment they were looking for confirmation that it
had worked.

Two listeners were missing, because there are two ways this changes.
Answering our own request now fires OnRequestPermissionResultListener.
That is not enough on its own: permission can equally be granted inside
Shizuku's own app, and Shizuku started or stopped there, none of which
calls back into this process — so the state is re-read whenever the
screen comes forward, which is the only thing that covers every route.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
mrambossek
2026-08-02 08:37:45 +02:00
co-authored by Claude Opus 5
parent 416b783647
commit 5b02d40802
3 changed files with 45 additions and 2 deletions
@@ -98,20 +98,32 @@ object ShizukuAvailability {
.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
/**
* Reports the state now and on every binder transition. Returns a function that removes the
* listeners again (call it from onCleared).
* Reports the state now, on every binder transition, and when a permission request is
* answered. Returns a function that removes the listeners again (call it from onCleared).
*
* The permission listener matters as much as the binder ones: granting permission does not
* make the binder arrive or die, so without it the banner still read "running but not
* authorised" after the user had just authorised it — the one moment they are looking for
* confirmation that it worked.
*
* It is still not sufficient on its own. Permission can be granted inside Shizuku's own app,
* where nothing calls back into this process at all, so callers should re-check on resume as
* well; see [current].
*/
fun observe(context: Context, onChange: (State) -> Unit): () -> Unit {
val app = context.applicationContext
val received = Shizuku.OnBinderReceivedListener { onChange(current(app)) }
val dead = Shizuku.OnBinderDeadListener { onChange(current(app)) }
val permission = Shizuku.OnRequestPermissionResultListener { _, _ -> onChange(current(app)) }
// "Sticky" fires immediately if the binder already arrived before we registered.
runCatching { Shizuku.addBinderReceivedListenerSticky(received) }
runCatching { Shizuku.addBinderDeadListener(dead) }
runCatching { Shizuku.addRequestPermissionResultListener(permission) }
onChange(current(app))
return {
runCatching { Shizuku.removeBinderReceivedListener(received) }
runCatching { Shizuku.removeBinderDeadListener(dead) }
runCatching { Shizuku.removeRequestPermissionResultListener(permission) }
}
}
}