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:
co-authored by
Claude Opus 5
parent
416b783647
commit
5b02d40802
@@ -149,6 +149,20 @@ class MainActivity : ComponentActivity() {
|
|||||||
screen = Screen.SETTINGS
|
screen = Screen.SETTINGS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Shizuku can be started, stopped or authorised in its own app, where nothing
|
||||||
|
// calls back into this process. Asking again each time this screen comes
|
||||||
|
// forward is what makes the banner right after the user has been away to fix
|
||||||
|
// it — which is exactly the moment they look at it.
|
||||||
|
val lifecycleOwner = androidx.compose.ui.platform.LocalLifecycleOwner.current
|
||||||
|
androidx.compose.runtime.DisposableEffect(lifecycleOwner) {
|
||||||
|
val obs = androidx.lifecycle.LifecycleEventObserver { _, event ->
|
||||||
|
if (event == androidx.lifecycle.Lifecycle.Event.ON_RESUME) {
|
||||||
|
vm.refreshShizuku()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lifecycleOwner.lifecycle.addObserver(obs)
|
||||||
|
onDispose { lifecycleOwner.lifecycle.removeObserver(obs) }
|
||||||
|
}
|
||||||
androidx.compose.runtime.LaunchedEffect(autorun) {
|
androidx.compose.runtime.LaunchedEffect(autorun) {
|
||||||
if (autorun) vm.run(devUpload = true)
|
if (autorun) vm.run(devUpload = true)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,6 +103,23 @@ class RunViewModel(app: Application) : AndroidViewModel(app) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Re-reads the shell tier's state, for when it changed somewhere this process cannot see.
|
||||||
|
*
|
||||||
|
* Permission can be granted inside Shizuku's own app, and Shizuku can be started or stopped
|
||||||
|
* there too; none of that calls back here. Asking again on resume is the only way to be right
|
||||||
|
* after the user has been somewhere else to fix it.
|
||||||
|
*/
|
||||||
|
fun refreshShizuku() {
|
||||||
|
val st = ShizukuAvailability.current(getApplication())
|
||||||
|
state = state.copy(
|
||||||
|
shizukuNotice = ShizukuAvailability.describe(st),
|
||||||
|
shizukuReady = st == ShizukuAvailability.State.READY,
|
||||||
|
shizukuHint = ShizukuAvailability.actionHint(st),
|
||||||
|
shizukuState = st,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
stopShizukuObserver()
|
stopShizukuObserver()
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
|
|||||||
+14
-2
@@ -98,20 +98,32 @@ object ShizukuAvailability {
|
|||||||
.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
|
.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reports the state now and on every binder transition. Returns a function that removes the
|
* Reports the state now, on every binder transition, and when a permission request is
|
||||||
* listeners again (call it from onCleared).
|
* 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 {
|
fun observe(context: Context, onChange: (State) -> Unit): () -> Unit {
|
||||||
val app = context.applicationContext
|
val app = context.applicationContext
|
||||||
val received = Shizuku.OnBinderReceivedListener { onChange(current(app)) }
|
val received = Shizuku.OnBinderReceivedListener { onChange(current(app)) }
|
||||||
val dead = Shizuku.OnBinderDeadListener { 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.
|
// "Sticky" fires immediately if the binder already arrived before we registered.
|
||||||
runCatching { Shizuku.addBinderReceivedListenerSticky(received) }
|
runCatching { Shizuku.addBinderReceivedListenerSticky(received) }
|
||||||
runCatching { Shizuku.addBinderDeadListener(dead) }
|
runCatching { Shizuku.addBinderDeadListener(dead) }
|
||||||
|
runCatching { Shizuku.addRequestPermissionResultListener(permission) }
|
||||||
onChange(current(app))
|
onChange(current(app))
|
||||||
return {
|
return {
|
||||||
runCatching { Shizuku.removeBinderReceivedListener(received) }
|
runCatching { Shizuku.removeBinderReceivedListener(received) }
|
||||||
runCatching { Shizuku.removeBinderDeadListener(dead) }
|
runCatching { Shizuku.removeBinderDeadListener(dead) }
|
||||||
|
runCatching { Shizuku.removeRequestPermissionResultListener(permission) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user