This is an automated email from the ASF dual-hosted git repository. erisu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/cordova-docs.git
The following commit(s) were added to refs/heads/master by this push: new e19263747f doc(android): Improve documentation of onRequestPermissionResult (#1400) e19263747f is described below commit e19263747fc1f735fa174050dddde5919c176dac Author: Manuel Beck <manuelbec...@outlook.de> AuthorDate: Mon Mar 24 15:31:46 2025 +0100 doc(android): Improve documentation of onRequestPermissionResult (#1400) --- www/docs/en/11.x/guide/platforms/android/plugin.md | 30 ++++++++++++++++------ .../12.x-2025.01/guide/platforms/android/plugin.md | 30 ++++++++++++++++------ www/docs/en/12.x/guide/platforms/android/plugin.md | 30 ++++++++++++++++------ www/docs/en/dev/guide/platforms/android/plugin.md | 30 ++++++++++++++++------ 4 files changed, 88 insertions(+), 32 deletions(-) diff --git a/www/docs/en/11.x/guide/platforms/android/plugin.md b/www/docs/en/11.x/guide/platforms/android/plugin.md index 3c1c14f275..289c6b77b6 100644 --- a/www/docs/en/11.x/guide/platforms/android/plugin.md +++ b/www/docs/en/11.x/guide/platforms/android/plugin.md @@ -344,21 +344,35 @@ protected void getReadPermission(int requestCode) } ``` -This will call the activity and cause a prompt to appear, asking for the permission. Once the user has the permission, the result must be handled with the `onRequestPermissionResult` method, which -every plugin should override. An example of this can be found below: +This will call the activity and cause a prompt to appear, asking for the permission. Once the user has the permission, the result must be handled with the `onRequestPermissionResult` method, a forwarded [Android method](https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[])), which every plugin should override. An example of this can be found below: ```java -public void onRequestPermissionResult(int requestCode, String[] permissions, - int[] grantResults) throws JSONException +@Override +public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { - for(int r:grantResults) + // It is possible that the permissions request interaction with the user is interrupted. + // In this case you will receive an empty permissions and grantResults array, which should be + // treated as a cancellation. + boolean granted = grantResults.length != 0; + + // Check if a permission is denied by user + for(int grantResult : grantResults) { - if(r == PackageManager.PERMISSION_DENIED) + if(grantResult == PackageManager.PERMISSION_DENIED) { - this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); - return; + granted = false; + break; } } + + // User denied a permission + if(!granted) + { + this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); + return; + } + + // Handle the request with the granted permission switch(requestCode) { case SEARCH_REQ_CODE: diff --git a/www/docs/en/12.x-2025.01/guide/platforms/android/plugin.md b/www/docs/en/12.x-2025.01/guide/platforms/android/plugin.md index e66506c2a9..f6d9aa0457 100644 --- a/www/docs/en/12.x-2025.01/guide/platforms/android/plugin.md +++ b/www/docs/en/12.x-2025.01/guide/platforms/android/plugin.md @@ -348,21 +348,35 @@ protected void getReadPermission(int requestCode) } ``` -This will call the activity and cause a prompt to appear, asking for the permission. Once the user has the permission, the result must be handled with the `onRequestPermissionResult` method, which -every plugin should override. An example of this can be found below: +This will call the activity and cause a prompt to appear, asking for the permission. Once the user has the permission, the result must be handled with the `onRequestPermissionResult` method, a forwarded [Android method](https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[])), which every plugin should override. An example of this can be found below: ```java -public void onRequestPermissionResult(int requestCode, String[] permissions, - int[] grantResults) throws JSONException +@Override +public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { - for(int r:grantResults) + // It is possible that the permissions request interaction with the user is interrupted. + // In this case you will receive an empty permissions and grantResults array, which should be + // treated as a cancellation. + boolean granted = grantResults.length != 0; + + // Check if a permission is denied by user + for(int grantResult : grantResults) { - if(r == PackageManager.PERMISSION_DENIED) + if(grantResult == PackageManager.PERMISSION_DENIED) { - this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); - return; + granted = false; + break; } } + + // User denied a permission + if(!granted) + { + this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); + return; + } + + // Handle the request with the granted permission switch(requestCode) { case SEARCH_REQ_CODE: diff --git a/www/docs/en/12.x/guide/platforms/android/plugin.md b/www/docs/en/12.x/guide/platforms/android/plugin.md index e66506c2a9..f6d9aa0457 100644 --- a/www/docs/en/12.x/guide/platforms/android/plugin.md +++ b/www/docs/en/12.x/guide/platforms/android/plugin.md @@ -348,21 +348,35 @@ protected void getReadPermission(int requestCode) } ``` -This will call the activity and cause a prompt to appear, asking for the permission. Once the user has the permission, the result must be handled with the `onRequestPermissionResult` method, which -every plugin should override. An example of this can be found below: +This will call the activity and cause a prompt to appear, asking for the permission. Once the user has the permission, the result must be handled with the `onRequestPermissionResult` method, a forwarded [Android method](https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[])), which every plugin should override. An example of this can be found below: ```java -public void onRequestPermissionResult(int requestCode, String[] permissions, - int[] grantResults) throws JSONException +@Override +public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { - for(int r:grantResults) + // It is possible that the permissions request interaction with the user is interrupted. + // In this case you will receive an empty permissions and grantResults array, which should be + // treated as a cancellation. + boolean granted = grantResults.length != 0; + + // Check if a permission is denied by user + for(int grantResult : grantResults) { - if(r == PackageManager.PERMISSION_DENIED) + if(grantResult == PackageManager.PERMISSION_DENIED) { - this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); - return; + granted = false; + break; } } + + // User denied a permission + if(!granted) + { + this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); + return; + } + + // Handle the request with the granted permission switch(requestCode) { case SEARCH_REQ_CODE: diff --git a/www/docs/en/dev/guide/platforms/android/plugin.md b/www/docs/en/dev/guide/platforms/android/plugin.md index e66506c2a9..f6d9aa0457 100644 --- a/www/docs/en/dev/guide/platforms/android/plugin.md +++ b/www/docs/en/dev/guide/platforms/android/plugin.md @@ -348,21 +348,35 @@ protected void getReadPermission(int requestCode) } ``` -This will call the activity and cause a prompt to appear, asking for the permission. Once the user has the permission, the result must be handled with the `onRequestPermissionResult` method, which -every plugin should override. An example of this can be found below: +This will call the activity and cause a prompt to appear, asking for the permission. Once the user has the permission, the result must be handled with the `onRequestPermissionResult` method, a forwarded [Android method](https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[])), which every plugin should override. An example of this can be found below: ```java -public void onRequestPermissionResult(int requestCode, String[] permissions, - int[] grantResults) throws JSONException +@Override +public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { - for(int r:grantResults) + // It is possible that the permissions request interaction with the user is interrupted. + // In this case you will receive an empty permissions and grantResults array, which should be + // treated as a cancellation. + boolean granted = grantResults.length != 0; + + // Check if a permission is denied by user + for(int grantResult : grantResults) { - if(r == PackageManager.PERMISSION_DENIED) + if(grantResult == PackageManager.PERMISSION_DENIED) { - this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); - return; + granted = false; + break; } } + + // User denied a permission + if(!granted) + { + this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); + return; + } + + // Handle the request with the granted permission switch(requestCode) { case SEARCH_REQ_CODE: --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For additional commands, e-mail: commits-h...@cordova.apache.org