Skip to content

Commit e192637

Browse files
authored
doc(android): Improve documentation of onRequestPermissionResult (#1400)
1 parent aba7056 commit e192637

File tree

4 files changed

+88
-32
lines changed

4 files changed

+88
-32
lines changed

Diff for: www/docs/en/11.x/guide/platforms/android/plugin.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,35 @@ protected void getReadPermission(int requestCode)
344344
}
345345
```
346346

347-
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
348-
every plugin should override. An example of this can be found below:
347+
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:
349348

350349
```java
351-
public void onRequestPermissionResult(int requestCode, String[] permissions,
352-
int[] grantResults) throws JSONException
350+
@Override
351+
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException
353352
{
354-
for(int r:grantResults)
353+
// It is possible that the permissions request interaction with the user is interrupted.
354+
// In this case you will receive an empty permissions and grantResults array, which should be
355+
// treated as a cancellation.
356+
boolean granted = grantResults.length != 0;
357+
358+
// Check if a permission is denied by user
359+
for(int grantResult : grantResults)
355360
{
356-
if(r == PackageManager.PERMISSION_DENIED)
361+
if(grantResult == PackageManager.PERMISSION_DENIED)
357362
{
358-
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
359-
return;
363+
granted = false;
364+
break;
360365
}
361366
}
367+
368+
// User denied a permission
369+
if(!granted)
370+
{
371+
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
372+
return;
373+
}
374+
375+
// Handle the request with the granted permission
362376
switch(requestCode)
363377
{
364378
case SEARCH_REQ_CODE:

Diff for: www/docs/en/12.x-2025.01/guide/platforms/android/plugin.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -348,21 +348,35 @@ protected void getReadPermission(int requestCode)
348348
}
349349
```
350350

351-
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
352-
every plugin should override. An example of this can be found below:
351+
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:
353352

354353
```java
355-
public void onRequestPermissionResult(int requestCode, String[] permissions,
356-
int[] grantResults) throws JSONException
354+
@Override
355+
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException
357356
{
358-
for(int r:grantResults)
357+
// It is possible that the permissions request interaction with the user is interrupted.
358+
// In this case you will receive an empty permissions and grantResults array, which should be
359+
// treated as a cancellation.
360+
boolean granted = grantResults.length != 0;
361+
362+
// Check if a permission is denied by user
363+
for(int grantResult : grantResults)
359364
{
360-
if(r == PackageManager.PERMISSION_DENIED)
365+
if(grantResult == PackageManager.PERMISSION_DENIED)
361366
{
362-
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
363-
return;
367+
granted = false;
368+
break;
364369
}
365370
}
371+
372+
// User denied a permission
373+
if(!granted)
374+
{
375+
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
376+
return;
377+
}
378+
379+
// Handle the request with the granted permission
366380
switch(requestCode)
367381
{
368382
case SEARCH_REQ_CODE:

Diff for: www/docs/en/12.x/guide/platforms/android/plugin.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -348,21 +348,35 @@ protected void getReadPermission(int requestCode)
348348
}
349349
```
350350

351-
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
352-
every plugin should override. An example of this can be found below:
351+
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:
353352

354353
```java
355-
public void onRequestPermissionResult(int requestCode, String[] permissions,
356-
int[] grantResults) throws JSONException
354+
@Override
355+
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException
357356
{
358-
for(int r:grantResults)
357+
// It is possible that the permissions request interaction with the user is interrupted.
358+
// In this case you will receive an empty permissions and grantResults array, which should be
359+
// treated as a cancellation.
360+
boolean granted = grantResults.length != 0;
361+
362+
// Check if a permission is denied by user
363+
for(int grantResult : grantResults)
359364
{
360-
if(r == PackageManager.PERMISSION_DENIED)
365+
if(grantResult == PackageManager.PERMISSION_DENIED)
361366
{
362-
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
363-
return;
367+
granted = false;
368+
break;
364369
}
365370
}
371+
372+
// User denied a permission
373+
if(!granted)
374+
{
375+
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
376+
return;
377+
}
378+
379+
// Handle the request with the granted permission
366380
switch(requestCode)
367381
{
368382
case SEARCH_REQ_CODE:

Diff for: www/docs/en/dev/guide/platforms/android/plugin.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -348,21 +348,35 @@ protected void getReadPermission(int requestCode)
348348
}
349349
```
350350

351-
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
352-
every plugin should override. An example of this can be found below:
351+
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:
353352

354353
```java
355-
public void onRequestPermissionResult(int requestCode, String[] permissions,
356-
int[] grantResults) throws JSONException
354+
@Override
355+
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException
357356
{
358-
for(int r:grantResults)
357+
// It is possible that the permissions request interaction with the user is interrupted.
358+
// In this case you will receive an empty permissions and grantResults array, which should be
359+
// treated as a cancellation.
360+
boolean granted = grantResults.length != 0;
361+
362+
// Check if a permission is denied by user
363+
for(int grantResult : grantResults)
359364
{
360-
if(r == PackageManager.PERMISSION_DENIED)
365+
if(grantResult == PackageManager.PERMISSION_DENIED)
361366
{
362-
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
363-
return;
367+
granted = false;
368+
break;
364369
}
365370
}
371+
372+
// User denied a permission
373+
if(!granted)
374+
{
375+
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR));
376+
return;
377+
}
378+
379+
// Handle the request with the granted permission
366380
switch(requestCode)
367381
{
368382
case SEARCH_REQ_CODE:

0 commit comments

Comments
 (0)