You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by Philipp Kursawe <ph...@gmail.com> on 2016/06/28 08:29:26 UTC

Why doesn't `cordova.requestPermission` have an `callbackContext` arg?

I wonder about this API design decision.

The current API forces us to save the callback context in some state
variable to have access to it
in the plugins `onRequestPermissionResult` like this:

private void requestPermissionAction(CallbackContext callbackContext,
JSONArray permission) {
  this.permissionsCallback = callbackContext;
  cordova.requestPermission(this, REQUEST_CODE_ENABLE_PERMISSION,
permission.getString(0));
}

@Override
public void onRequestPermissionResult(int requestCode, String[]
permissions, int[] grantResults) {
  if (this.permissionsCallback == null) {
    return;
  }

  if (permissions != null && permissions.length > 0) {
    this.permissionsCallback.success();
  } else {
    this.permissionsCallback.error("No permission");
  }
  this.permissionsCallback = null;
}

This works but limits the concurrent calls to the plugins
`requestPermissionAction` to one caller from the script at a time (that
means until the success or error callbacks are called in the script).
Otherwise the `this.permissionCallback` would be set again for a second
call and when the first calls `onRequestPermissionResult` is called it will
actually call the handlers of the second `requestPermissionAction`
callbackContext.

Wouldn't this API signature make more sense?

cordova.requestPermission(CordovaPlugin plugin, int requestCode, String
permission, CallbackContext callbackContext);

@Override
public void onRequestPermissionResult(int requestCode, String[]
permissions, int[] grantResults, CallbackContext callbackContext) {
  if (permissions != null && permissions.length > 0) {
    permissionsCallback.success();
  } else {
    permissionsCallback.error("No permission");
  }
}


And while we are at: Change `permission[]` and `grantResults[]` to an
object `results[permission] = grantResult`?

Re: Why doesn't `cordova.requestPermission` have an `callbackContext` arg?

Posted by Philipp Kursawe <ph...@gmail.com>.
I see. Cordova already uses a `permissionResultCallbacks` that holds the
user provided `requestCode`. It could additionally hold the
`callbackContext`.
And when android calls `CordovaInterfaceImpl.onRequestPermissionResult` it
could call a new plugin function with a new signature that contains the
`callbackContext`.


On Tue, Jun 28, 2016 at 10:36 AM, julio cesar sanchez <
jcesarmobile@gmail.com> wrote:

> onRequestPermissionResult is an Android method we have to override, so we
> can't choose which params to pass.
>
>
> https://developer.android.com/reference/android/support/v4/app/ActivityCompat.OnRequestPermissionsResultCallback.html
>
>
> 2016-06-28 10:29 GMT+02:00 Philipp Kursawe <ph...@gmail.com>:
>
> > I wonder about this API design decision.
> >
> > The current API forces us to save the callback context in some state
> > variable to have access to it
> > in the plugins `onRequestPermissionResult` like this:
> >
> > private void requestPermissionAction(CallbackContext callbackContext,
> > JSONArray permission) {
> >   this.permissionsCallback = callbackContext;
> >   cordova.requestPermission(this, REQUEST_CODE_ENABLE_PERMISSION,
> > permission.getString(0));
> > }
> >
> > @Override
> > public void onRequestPermissionResult(int requestCode, String[]
> > permissions, int[] grantResults) {
> >   if (this.permissionsCallback == null) {
> >     return;
> >   }
> >
> >   if (permissions != null && permissions.length > 0) {
> >     this.permissionsCallback.success();
> >   } else {
> >     this.permissionsCallback.error("No permission");
> >   }
> >   this.permissionsCallback = null;
> > }
> >
> > This works but limits the concurrent calls to the plugins
> > `requestPermissionAction` to one caller from the script at a time (that
> > means until the success or error callbacks are called in the script).
> > Otherwise the `this.permissionCallback` would be set again for a second
> > call and when the first calls `onRequestPermissionResult` is called it
> will
> > actually call the handlers of the second `requestPermissionAction`
> > callbackContext.
> >
> > Wouldn't this API signature make more sense?
> >
> > cordova.requestPermission(CordovaPlugin plugin, int requestCode, String
> > permission, CallbackContext callbackContext);
> >
> > @Override
> > public void onRequestPermissionResult(int requestCode, String[]
> > permissions, int[] grantResults, CallbackContext callbackContext) {
> >   if (permissions != null && permissions.length > 0) {
> >     permissionsCallback.success();
> >   } else {
> >     permissionsCallback.error("No permission");
> >   }
> > }
> >
> >
> > And while we are at: Change `permission[]` and `grantResults[]` to an
> > object `results[permission] = grantResult`?
> >
>

Re: Why doesn't `cordova.requestPermission` have an `callbackContext` arg?

Posted by julio cesar sanchez <jc...@gmail.com>.
onRequestPermissionResult is an Android method we have to override, so we
can't choose which params to pass.

https://developer.android.com/reference/android/support/v4/app/ActivityCompat.OnRequestPermissionsResultCallback.html


2016-06-28 10:29 GMT+02:00 Philipp Kursawe <ph...@gmail.com>:

> I wonder about this API design decision.
>
> The current API forces us to save the callback context in some state
> variable to have access to it
> in the plugins `onRequestPermissionResult` like this:
>
> private void requestPermissionAction(CallbackContext callbackContext,
> JSONArray permission) {
>   this.permissionsCallback = callbackContext;
>   cordova.requestPermission(this, REQUEST_CODE_ENABLE_PERMISSION,
> permission.getString(0));
> }
>
> @Override
> public void onRequestPermissionResult(int requestCode, String[]
> permissions, int[] grantResults) {
>   if (this.permissionsCallback == null) {
>     return;
>   }
>
>   if (permissions != null && permissions.length > 0) {
>     this.permissionsCallback.success();
>   } else {
>     this.permissionsCallback.error("No permission");
>   }
>   this.permissionsCallback = null;
> }
>
> This works but limits the concurrent calls to the plugins
> `requestPermissionAction` to one caller from the script at a time (that
> means until the success or error callbacks are called in the script).
> Otherwise the `this.permissionCallback` would be set again for a second
> call and when the first calls `onRequestPermissionResult` is called it will
> actually call the handlers of the second `requestPermissionAction`
> callbackContext.
>
> Wouldn't this API signature make more sense?
>
> cordova.requestPermission(CordovaPlugin plugin, int requestCode, String
> permission, CallbackContext callbackContext);
>
> @Override
> public void onRequestPermissionResult(int requestCode, String[]
> permissions, int[] grantResults, CallbackContext callbackContext) {
>   if (permissions != null && permissions.length > 0) {
>     permissionsCallback.success();
>   } else {
>     permissionsCallback.error("No permission");
>   }
> }
>
>
> And while we are at: Change `permission[]` and `grantResults[]` to an
> object `results[permission] = grantResult`?
>