2009-11-10 09:45:02 +08:00
|
|
|
package com.phonegap;
|
2009-10-01 06:34:28 +08:00
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
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;
|
2009-11-04 08:51:40 +08:00
|
|
|
import android.util.Log;
|
2009-10-01 06:34:28 +08:00
|
|
|
|
2010-10-05 12:58:14 +08:00
|
|
|
public class ContactManager extends Plugin {
|
2009-11-03 07:43:09 +08:00
|
|
|
|
2010-09-11 03:09:40 +08:00
|
|
|
private static ContactAccessor contactAccessor;
|
2009-11-04 08:51:40 +08:00
|
|
|
private static final String LOG_TAG = "Contact Query";
|
2010-09-07 02:13:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public ContactManager() {
|
2009-10-10 01:23:21 +08:00
|
|
|
}
|
2010-06-05 04:32:00 +08:00
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
/**
|
2010-10-06 09:35:51 +08:00
|
|
|
* Executes the request and returns PluginResult.
|
2010-09-07 02:13:09 +08:00
|
|
|
*
|
2010-10-06 09:35:51 +08:00
|
|
|
* @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.
|
2010-09-07 02:13:09 +08:00
|
|
|
*/
|
2010-10-06 09:35:51 +08:00
|
|
|
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
2010-09-11 03:09:40 +08:00
|
|
|
if (contactAccessor == null) {
|
|
|
|
contactAccessor = ContactAccessor.getInstance(webView, ctx);
|
|
|
|
}
|
2010-09-08 02:59:54 +08:00
|
|
|
PluginResult.Status status = PluginResult.Status.OK;
|
2010-09-07 02:13:09 +08:00
|
|
|
String result = "";
|
|
|
|
|
|
|
|
try {
|
2010-09-11 03:09:40 +08:00
|
|
|
if (action.equals("search")) {
|
2010-10-04 09:47:12 +08:00
|
|
|
JSONArray res = contactAccessor.search(args.getJSONArray(0), args.getJSONObject(1));
|
|
|
|
return new PluginResult(status, res);
|
2010-09-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
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-07 02:13:09 +08:00
|
|
|
}
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(status, result);
|
2010-09-07 02:13:09 +08:00
|
|
|
} catch (JSONException e) {
|
2010-09-25 01:22:46 +08:00
|
|
|
Log.e(LOG_TAG, e.getMessage(), e);
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
2010-09-07 02:13:09 +08:00
|
|
|
}
|
|
|
|
}
|
2009-10-01 06:34:28 +08:00
|
|
|
}
|