You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by br...@apache.org on 2012/10/12 23:06:18 UTC
[11/18] android commit: Port Contacts to CordovaPlugin.
Port Contacts to CordovaPlugin.
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/d72a8cbf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/d72a8cbf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/d72a8cbf
Branch: refs/heads/master
Commit: d72a8cbf8954aae7a868112d74b55c8a1dac23a4
Parents: fe0876d
Author: Braden Shepherdson <br...@chromium.org>
Authored: Thu Oct 11 16:03:49 2012 -0400
Committer: Braden Shepherdson <br...@chromium.org>
Committed: Thu Oct 11 16:03:49 2012 -0400
----------------------------------------------------------------------
.../src/org/apache/cordova/ContactManager.java | 35 ++++++++-------
.../org/apache/cordova/api/CallbackContext.java | 13 +++++-
2 files changed, 30 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/d72a8cbf/framework/src/org/apache/cordova/ContactManager.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/ContactManager.java b/framework/src/org/apache/cordova/ContactManager.java
index 31b5452..a47f3d1 100755
--- a/framework/src/org/apache/cordova/ContactManager.java
+++ b/framework/src/org/apache/cordova/ContactManager.java
@@ -18,14 +18,15 @@
*/
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
-public class ContactManager extends Plugin {
+public class ContactManager extends CordovaPlugin {
private ContactAccessor contactAccessor;
private static final String LOG_TAG = "Contact Query";
@@ -47,21 +48,19 @@ public class ContactManager extends Plugin {
/**
* Executes the request and returns PluginResult.
*
- * @param action The action to execute.
- * @param args JSONArry of arguments for the plugin.
- * @param callbackId The callback id used when calling back into JavaScript.
- * @return A PluginResult object with a status and message.
+ * @param action The action to execute.
+ * @param args JSONArray of arguments for the plugin.
+ * @param callbackContext The callback context used when calling back into JavaScript.
+ * @return True if the action was valid, false otherwise.
*/
- public PluginResult execute(String action, JSONArray args, String callbackId) {
- PluginResult.Status status = PluginResult.Status.OK;
- String result = "";
-
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
/**
* 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.
*/
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 {
if (action.equals("search")) {
JSONArray res = contactAccessor.search(args.getJSONArray(0), args.optJSONObject(1));
- return new PluginResult(status, res);
+ callbackContext.success(res);
}
else if (action.equals("save")) {
String id = contactAccessor.save(args.getJSONObject(0));
if (id != null) {
JSONObject res = contactAccessor.getContactById(id);
if (res != null) {
- return new PluginResult(status, res);
+ callbackContext.success(res);
}
}
}
else if (action.equals("remove")) {
if (contactAccessor.remove(args.getString(0))) {
- return new PluginResult(status, result);
+ callbackContext.success();
}
}
- // If we get to this point an error has occurred
- return new PluginResult(PluginResult.Status.ERROR, ContactManager.UNKNOWN_ERROR);
+ else {
+ return false;
+ }
} catch (JSONException 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;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/d72a8cbf/framework/src/org/apache/cordova/api/CallbackContext.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/api/CallbackContext.java b/framework/src/org/apache/cordova/api/CallbackContext.java
index c9dc9d7..c098d40 100644
--- a/framework/src/org/apache/cordova/api/CallbackContext.java
+++ b/framework/src/org/apache/cordova/api/CallbackContext.java
@@ -1,5 +1,7 @@
package org.apache.cordova.api;
+import org.json.JSONArray;
+
import android.util.Log;
import org.apache.cordova.CordovaWebView;
@@ -65,6 +67,15 @@ public class CallbackContext {
*
* @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
+ *
+ * @param message The message to add to the success result.
+ */
public void success() {
sendPluginResult(new PluginResult(PluginResult.Status.OK));
}
@@ -87,4 +98,4 @@ public class CallbackContext {
public void error(String message) {
sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message));
}
-}
\ No newline at end of file
+}