cordova-plugin-android-permissions icon indicating copy to clipboard operation
cordova-plugin-android-permissions copied to clipboard

checkPermission or hasPermission with multiple permissions

Open kdgcom opened this issue 7 years ago • 2 comments

Hi.

In readme, there is a example for multiple permission :

var list = [
  permissions.CAMERA,
  permissions.GET_ACCOUNTS
];

permissions.hasPermission(list, callback, null);

I don't know why the author used deprecated 'hasPermission' method (actually it use 'checkPermission' method internally), but anyway I tried like this :

        var list = [
            permissions.WRITE_EXTERNAL_STORAGE,
            permissions.CAMERA,
            permissions.READ_PHONE_STATE,
        ];

        permissions.checkPermission(list, requestPermission, function(e){console.log(e); });

        function requestPermission( status, again )
        {
            if ( !status.hasPermission )
                permissions.requestPermissions( list, cb, function(e){console.log(e);});
            else
....
        }

then, status.hasPermission is always false regardless of real status. So, I investigated the java source :

    private void checkPermissionAction(CallbackContext callbackContext, JSONArray permission) {
        if (permission == null || permission.length() == 0 || permission.length() > 1) {
            JSONObject returnObj = new JSONObject();
            addProperty(returnObj, KEY_ERROR, ACTION_CHECK_PERMISSION);
            addProperty(returnObj, KEY_MESSAGE, "One time one permission only.");
            callbackContext.error(returnObj);
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            JSONObject returnObj = new JSONObject();
            addProperty(returnObj, KEY_RESULT_PERMISSION, true);
            callbackContext.success(returnObj);
        } else {
            try {
                JSONObject returnObj = new JSONObject();
                addProperty(returnObj, KEY_RESULT_PERMISSION, cordova.hasPermission(permission.getString(0)));
                callbackContext.success(returnObj);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

It seems that 'checkPermission' method doesn't accept multiple permission. so, please correct the example code to avoid these confusion. Or, if I have some misconception, please let me know.

kdgcom avatar Jun 19 '18 05:06 kdgcom

It a good question, sadly, I had a long time not to maintain this plugin. I already forgot why only check one permission at a time. Seems there was some issue when trying to check multi permissions at the same time. Or maybe it is a limitation from the response format. I'll spend some time to review this plugin.

NeoLSN avatar Jun 20 '18 08:06 NeoLSN

private fun checkPermission(): Boolean { if ( ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) { return true } return false }

IamMuhammadHasib avatar Dec 23 '21 05:12 IamMuhammadHasib