cordova-android/framework/src/com/phonegap/ContactManager.java

61 lines
1.7 KiB
Java
Raw Normal View History

package com.phonegap;
import org.json.JSONArray;
import org.json.JSONException;
2010-10-01 11:09:59 +08:00
import org.json.JSONObject;
2010-09-08 02:59:54 +08:00
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import android.util.Log;
public class ContactManager extends Plugin {
2009-11-03 07:43:09 +08:00
private static ContactAccessor contactAccessor;
private static final String LOG_TAG = "Contact Query";
/**
* Constructor.
*/
public ContactManager() {
2009-10-10 01:23:21 +08:00
}
/**
* 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.
*/
public PluginResult execute(String action, JSONArray args, String callbackId) {
if (contactAccessor == null) {
contactAccessor = ContactAccessor.getInstance(webView, ctx);
}
2010-09-08 02:59:54 +08:00
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("search")) {
JSONArray res = contactAccessor.search(args.getJSONArray(0), args.getJSONObject(1));
return new PluginResult(status, res);
}
else if (action.equals("save")) {
// TODO Coming soon!
}
else if (action.equals("remove")) {
2010-10-01 11:09:59 +08:00
if (contactAccessor.remove(args.getString(0))) {
return new PluginResult(status, result);
}
else {
JSONObject r = new JSONObject();
r.put("code", 2);
return new PluginResult(PluginResult.Status.ERROR, r);
}
}
2010-09-08 02:59:54 +08:00
return new PluginResult(status, result);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
2010-09-08 02:59:54 +08:00
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
}