Port Contacts to CordovaPlugin.

This commit is contained in:
Braden Shepherdson 2012-10-11 16:03:49 -04:00
parent fe0876ded6
commit d72a8cbf89
2 changed files with 30 additions and 18 deletions

View File

@ -18,14 +18,15 @@
*/ */
package org.apache.cordova; package org.apache.cordova;
import org.apache.cordova.api.Plugin; import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult; import org.apache.cordova.api.PluginResult;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.util.Log; import android.util.Log;
public class ContactManager extends Plugin { public class ContactManager extends CordovaPlugin {
private ContactAccessor contactAccessor; private ContactAccessor contactAccessor;
private static final String LOG_TAG = "Contact Query"; private static final String LOG_TAG = "Contact Query";
@ -47,21 +48,19 @@ public class ContactManager extends Plugin {
/** /**
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
* *
* @param action The action to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the plugin. * @param args JSONArray of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript. * @param callbackContext The callback context used when calling back into JavaScript.
* @return A PluginResult object with a status and message. * @return True if the action was valid, false otherwise.
*/ */
public PluginResult execute(String action, JSONArray args, String callbackId) { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
/** /**
* Check to see if we are on an Android 1.X device. If we are return an error as we * Check to see if we are on an Android 1.X device. If we are return an error as we
* do not support this as of Cordova 1.0. * do not support this as of Cordova 1.0.
*/ */
if (android.os.Build.VERSION.RELEASE.startsWith("1.")) { if (android.os.Build.VERSION.RELEASE.startsWith("1.")) {
return new PluginResult(PluginResult.Status.ERROR, ContactManager.NOT_SUPPORTED_ERROR); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, ContactManager.NOT_SUPPORTED_ERROR));
return true;
} }
/** /**
@ -75,27 +74,29 @@ public class ContactManager extends Plugin {
try { try {
if (action.equals("search")) { if (action.equals("search")) {
JSONArray res = contactAccessor.search(args.getJSONArray(0), args.optJSONObject(1)); JSONArray res = contactAccessor.search(args.getJSONArray(0), args.optJSONObject(1));
return new PluginResult(status, res); callbackContext.success(res);
} }
else if (action.equals("save")) { else if (action.equals("save")) {
String id = contactAccessor.save(args.getJSONObject(0)); String id = contactAccessor.save(args.getJSONObject(0));
if (id != null) { if (id != null) {
JSONObject res = contactAccessor.getContactById(id); JSONObject res = contactAccessor.getContactById(id);
if (res != null) { if (res != null) {
return new PluginResult(status, res); callbackContext.success(res);
} }
} }
} }
else if (action.equals("remove")) { else if (action.equals("remove")) {
if (contactAccessor.remove(args.getString(0))) { if (contactAccessor.remove(args.getString(0))) {
return new PluginResult(status, result); callbackContext.success();
} }
} }
// If we get to this point an error has occurred else {
return new PluginResult(PluginResult.Status.ERROR, ContactManager.UNKNOWN_ERROR); return false;
}
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
} }
return true;
} }
} }

View File

@ -1,5 +1,7 @@
package org.apache.cordova.api; package org.apache.cordova.api;
import org.json.JSONArray;
import android.util.Log; import android.util.Log;
import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaWebView;
@ -60,6 +62,15 @@ public class CallbackContext {
sendPluginResult(new PluginResult(PluginResult.Status.OK, message)); sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
} }
/**
* Helper for success callbacks that just returns the Status.OK by default
*
* @param message The message to add to the success result.
*/
public void success(JSONArray message) {
sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
}
/** /**
* Helper for success callbacks that just returns the Status.OK by default * Helper for success callbacks that just returns the Status.OK by default
* *
@ -87,4 +98,4 @@ public class CallbackContext {
public void error(String message) { public void error(String message) {
sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message)); sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message));
} }
} }