From 3b4cb89888dcef9d995883b3d784791907a2f3f0 Mon Sep 17 00:00:00 2001 From: Manuel Beck Date: Sat, 15 Feb 2025 15:31:56 +0100 Subject: [PATCH] Android guide: Improve documentation of onRequestPermissionResult - Mention that it is a forwarded Android method - Add Android documentation link for onRequestPermissionsResult - Handle and document grandResult.length == 0, which can occur, if the request interaction was interrupted - Document code in onRequestPermissionResult and small refactoring --- .../en/11.x/guide/platforms/android/plugin.md | 30 ++++++++++++++----- .../guide/platforms/android/plugin.md | 30 ++++++++++++++----- .../en/12.x/guide/platforms/android/plugin.md | 30 ++++++++++++++----- .../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: