cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Philipp Kursawe <phil.kurs...@gmail.com>
Subject Why doesn't `cordova.requestPermission` have an `callbackContext` arg?
Date Tue, 28 Jun 2016 08:29:26 GMT
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`?

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message