Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onRequestPermissionsResult never called #1393

Closed
a-ferrari opened this issue Jan 28, 2022 · 3 comments
Closed

onRequestPermissionsResult never called #1393

a-ferrari opened this issue Jan 28, 2022 · 3 comments

Comments

@a-ferrari
Copy link

a-ferrari commented Jan 28, 2022

onRequestPermissionsResult never called after calling cordova.requestPermissions but it is called the deprecated method onRequestPermissionResult

Problem

api level version:<=29

After calling cordova.requestPermissions(this, 1, permissions) and give the rights on the device, onRequestPermissionsResult isn't called ( but It is called onRequestPermissionResult that is deprecated)

If I call cordova.requestPermission (without s) onRequestPermissionResult (without s) is correctly called

Information

Command or Code

package com.thesis.plugins;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaWebView;

import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;

import android.provider.MediaStore;
import android.os.Build;
import android.content.ContentValues;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.widget.Toast;


/**
 * This class echoes a string called from JavaScript.
 */
public class CordovaAndroidThesisStore extends CordovaPlugin {


    private CallbackContext _tmpCallbackContext;
    private JSONArray _tmpArgs;

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("store")) {

            //String message = args.getString(0);
            //cordova.getActivity().runOnUiThread(()
            cordova.getThreadPool().execute(() -> {
                try {
                    store(args, callbackContext);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            });


            return true;



        }
        return false;
    }

    private void store(JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
            this.store(args.getString(0), args.getString(1), callbackContext);
        }else{
            if(cordova.hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE))
            {
                this.store(args.getString(0), args.getString(1), callbackContext);
            }
        else
            {
                this._tmpCallbackContext= callbackContext;
                this._tmpArgs= args;
                String [] permissions= {Manifest.permission.WRITE_EXTERNAL_STORAGE};
               // cordova.requestPermissions(this, 1, permissions); DOSN'T WORK
                cordova.requestPermission(this, 1, permissions[0]);
              
            }

        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                          int[] grantResults) throws JSONException
    {
        for(int r:grantResults)
        {
            if(r == PackageManager.PERMISSION_DENIED)
            {
                this._tmpCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "NO_PERMISSION"));
                return;
            }
        }
        if(requestCode==1) {
            this.store(_tmpArgs.getString(0), _tmpArgs.getString(1),  _tmpCallbackContext);
        }

    }


    @Override
    public void onRequestPermissionResult(int requestCode, String[] permissions,
                                           int[] grantResults) throws JSONException
    {
        for(int r:grantResults)
        {
            if(r == PackageManager.PERMISSION_DENIED)
            {
                this._tmpCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "NO_PERMISSION"));
                return;
            }
        }
        if(requestCode==1) {
            this.store(_tmpArgs.getString(0), _tmpArgs.getString(1),  _tmpCallbackContext);
        }

    }


    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
      
      }
    

    protected void store(String byteString, String fileName, CallbackContext callbackContext) {

        final CordovaInterface _cordova = cordova;   
        Context context = this.cordova.getActivity();
        Toast toast = Toast.makeText(context, "YEAAA",  Toast.LENGTH_LONG);
        toast.show();
/*
       TODO
        */
    }
}

Environment, Platform, Device

Version information

Cordova Packages:

cli: 11.0.0
    common: 4.0.2
    create: 4.0.0
    lib: 11.0.0
        common: 4.0.2
        fetch: 3.0.1
        serve: 4.0.0

Project Installed Platforms:

android: 10.1.1

Project Installed Plugins:

com-thesis-plugins-pdfstore: 0.0.1

Environment:

OS: macOS Monterey 12.0.1 (21A559) (darwin 21.1.0) x64
Node: v12.14.0
npm: 6.14.5

Checklist

@a-ferrari
Copy link
Author

The reason is the implementation of onRequestPermissionResult in CordovaInterfaceImpl

/**
     * Called by the system when the user grants permissions
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    public void onRequestPermissionResult(int requestCode, String[] permissions,
                                          int[] grantResults) throws JSONException {
        Pair<CordovaPlugin, Integer> callback = permissionResultCallbacks.getAndRemoveCallback(requestCode);
        if(callback != null) {
            callback.first.onRequestPermissionResult(callback.second, permissions, grantResults);
        }
    }

@Chuckytuh
Copy link
Contributor

This seems a duplicate of #1388

@a-ferrari
Copy link
Author

This seems a duplicate of #1388

Yes It is

Thanks
Alberto

rmoehn added a commit to GuidedTrack/cordova-plugin-mindease that referenced this issue Oct 24, 2023
As noted in the comment, the former is deprecated in favour of the
latter, but the latter is never called. See also:

- apache/cordova-android#1388
- apache/cordova-android#1393

I hope the comment will help us fix the problem quickly when the Cordova
folks decide to remove the deprecated method. (And hopefully make sure
that the new method is called.)

Addresses: https://trello.com/c/zsmadleg/2549-mind-ease-notifications-probably-broken-on-android
rmoehn added a commit to GuidedTrack/cordova-plugin-mindease that referenced this issue Oct 24, 2023
As noted in the comment, the former is deprecated in favour of the
latter, but the latter is never called. See also:

- apache/cordova-android#1388
- apache/cordova-android#1393

I hope the comment will help us fix the problem quickly when the Cordova
folks decide to remove the deprecated method. (And hopefully make sure
that the new method is called.)

Addresses: https://trello.com/c/zsmadleg/2549-mind-ease-notifications-probably-broken-on-android
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants