Is it possible to call a PhoneGap plugin from Java without going through the browser? For example, calling a JavaScript plugin like this: "cordova.exec(successCallback, failureCallback, 'CallListPlugin', 'list', params)"
I am curious about how to make this call directly from the MainActivity. Any insights or suggestions?
Thank you!
CallListPlugin.java
package com.example.app;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
// more imports...
public class CallListPlugin extends CordovaPlugin {
// Actions
private static final String ACTION = "list";
private static final String CONTACT_ACTION = "contact";
private static final String SHOW_ACTION = "show";
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
boolean actionValid = true;
if (ACTION.equals(action)) {
try {
int limit = -1;
// Limit by date
if (!data.isNull(0)) {
String d = data.getString(0);
// logic for time period limit...
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
// more logic...
JSONObject callInfo = getCallListing(limiter);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, callInfo);
callbackContext.sendPluginResult(pluginResult);
callbackContext.success();
} catch (JSONException jsonEx) {
Log.d(TAG, "Got JSON Exception " + jsonEx.getMessage());
callbackContext.error(jsonEx.getMessage());
actionValid = false;
}
} else if (SHOW_ACTION.equals(action)) {
try {
if (!data.isNull(0)) {
viewContact(data.getString(0));
}
} catch (JSONException | Exception e) {
Log.d(TAG, "Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
actionValid = false;
}
} else if (CONTACT_ACTION.equals(action)) {
try {
String contactInfo = getContactNameFromNumber(data.getString(0));
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, contactInfo);
callbackContext.sendPluginResult(pluginResult);
callbackContext.success();
} catch (JSONException | Exception e) {
Log.d(TAG, "Error: " + e.getMessage());
callbackContext.error(e.getMessage());
actionValid = false;
}
} else {
actionValid = false;
Log.d(TAG, "Invalid action: " + action);
}
return actionValid;
}
// Other methods and functions...
}