One solution is not to retain the permission (use false
as the third parameter in the callback
). This can lead to excessive permission asking, so storing the result locally solves that:
class MyWebChromeClient : WebChromeClient() {
private var givenPermissions: Set<String> = emptySet()
override fun onGeolocationPermissionsShowPrompt(origin: String, callback: GeolocationPermissions.Callback) {
val onResult = { allow: Boolean -> callback(origin, allow, false) }
when {
origin in givenPermissions -> checkSystemPermission { granted ->
if (!granted) givenPermissions -= origin
onResult(granted)
}
else -> askForPermission { allow ->
when (allow) {
false -> onResult(false)
true -> checkSystemPermission { granted ->
if (granted) givenPermissions += origin
onResult(granted)
}
}
}
}
}
private fun askForPermission(callback: (Boolean) -> Unit) {
// ask for permission
}
private fun checkSystemPermission(callback: (Boolean) -> Unit) {
// check/ask for system permission
}
}
If the system permission is revoked, the WebChromeClient
should be recreated automatically.