Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap Android Plugin: Success & Failure Callback not getting called

I developed an Android PhoneGap Plugin. The plugin is successfully getting called, but the callback is not getting invoked. I have no idea where I have missed anything.

Does anybody have any idea as to what could be wrong when the callback are not getting invoked?

Following is my code:

JS File Contents:

var SharedPreferencePlugin = function() {};

SharedPreferencePlugin.prototype.getvalues = function(content, success, fail) {
    return PhoneGap.exec( 
        function(args) {
            console.log("success called from plugin's js file");    
        }, 
        function(args) { 
            console.log("failure called from plugin's js file");
        }, 
        'SharedPreferencePlugin', 
        'getvalues', 
        [content]
    );
};

SharedPreferencePlugin.prototype.update = function(itemName, success, fail) {
    return PhoneGap.exec( 
        function(args) {
            console.log("success called from plugin's js file");    
        }, 
        function(args) { 
            console.log("failure called from plugin's js file");
        }, 
        'SharedPreferencePlugin', 
        'update', 
        [itemName]
    );
};

PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin('SharedPreferencePlugin', new SharedPreferencePlugin());
});

Java file:

public class SharedPreferencePlugin extends Plugin{

    public static final String GET_ACTION = "getvalues";
    public static final String UPDATE_ACTION = "update";
    static Context staticContext = MainActivity.staticContext;
    SharedPreferences dataStorage = staticContext.getSharedPreferences(MainActivity.PREFS_NAME, 0);

    public PluginResult execute(String action, JSONArray data, String callbackId)
    {
        Log.d("SharedPreferencePlugin", "Plugin Called with action: " + action);
        PluginResult result = null;
        if(action.equals(GET_ACTION))
        {
            Log.d("SharedPrferencePlugin", "inside if for 'getvalues'");
            JSONArray savedData = getPreferences();
            Log.d("SharedPreferencePlugin", "Data: " + savedData);
            result = new PluginResult(Status.OK, savedData);
        }
        else if(action.equals(UPDATE_ACTION))
        {
            try
            {
                updateSharedPreferences(data.getJSONObject(0).getString("itemName"));
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("status", "success");

                result = new PluginResult(PluginResult.Status.OK, jsonObject);
            }
            catch(JSONException ex)
            {
                Log.d("SharedPreferencePlugin", "Got JSONException: " + ex.getMessage());
                result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
            }
        }
        else
        {
            result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
            Log.d("SharedPreferencePlugin", "Invalid action: " + action + " obtained.");
        }
        return result;
    }

    public void updateSharedPreferences(String itemName)
    {
        Log.d("SharedPreferencePlugin", "Inside updateSharedPreferences, value passed: " + itemName);
        SharedPreferences tmpPreferenceReference = staticContext.getSharedPreferences(MainActivity.PREFS_NAME, 0);
        SharedPreferences.Editor editor = tmpPreferenceReference.edit();

        if(itemName.equals(tmpPreferenceReference.getString(MainActivity.NAME_ITEM1, "")))
        {
            Integer tmpInt = Integer.parseInt(tmpPreferenceReference.getString(MainActivity.QUANTITY_ITEM1, "0")) - 1;
            editor.putString(MainActivity.QUANTITY_ITEM1, tmpInt.toString());
        }
        editor.commit();
    }

    protected JSONArray getPreferences()
    {
        ArrayList<String> arrItemNames = new ArrayList<String>();
        ArrayList<String> arrItemQuantities = new ArrayList<String>();

        arrItemNames.add(0, dataStorage.getString(MainActivity.NAME_ITEM1, ""));
        arrItemNames.add(1, dataStorage.getString(MainActivity.NAME_ITEM2, ""));
        arrItemQuantities.add(0, dataStorage.getString(MainActivity.QUANTITY_ITEM1, ""));
        arrItemQuantities.add(0, dataStorage.getString(MainActivity.QUANTITY_ITEM2, ""));

        //-------------------------------------------------------------------
        ArrayList<ArrayList> tempArrayList = new ArrayList<ArrayList>();
        tempArrayList.add(arrItemNames);
        tempArrayList.add(arrItemQuantities);

        JSONArray jsonData = new JSONArray(tempArrayList);
        //-------------------------------------------------------------------

        return jsonData;
    }
}

HTML CODE TO CALL THE PLUGIN:

function test()
            {
                console.log("Test called");
                window.plugins.SharedPreferencePlugin.getvalues({},
                    function() { // Success function
                        console.log("success called");
                    }, 
                    function() {  // Failure function
                        console.log('Share failed');
                    }
                );
            }

Any help is highly appreciated.

Thanks.

like image 840
Mahendra Liya Avatar asked Oct 24 '22 18:10

Mahendra Liya


2 Answers

When you say it is definitely running the native code, how do you know this? Are you seeing the Log.d's or are you actually putting a break point in and stepping through and seeing if the return result; line is being executed?

Also, which version of phonegap are you using?

like image 122
Dean Wild Avatar answered Nov 03 '22 03:11

Dean Wild


If you are using PhoneGap 1.2 you should remove the line:

PluginManager.addService("SharedPreferencePlugin","com.devapps.mmvspinningwheel.SharedPreferencePlugin");

as it is not required. Also, you should probably move PhoneGap.addConstructor() to the bottom of your .js file.

Dean is not wrong as there are some devices like the HTC that console.log does not work properly on, as well what version of Android are you testing with?

like image 28
Simon MacDonald Avatar answered Nov 03 '22 04:11

Simon MacDonald