2009-11-09 17:45:02 -08:00
|
|
|
package com.phonegap;
|
2009-09-30 15:34:28 -07:00
|
|
|
|
2010-09-06 13:13:09 -05:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
2010-10-01 11:09:59 +08:00
|
|
|
import org.json.JSONObject;
|
2010-09-07 13:59:54 -05:00
|
|
|
import com.phonegap.api.Plugin;
|
|
|
|
import com.phonegap.api.PluginResult;
|
2009-11-03 16:51:40 -08:00
|
|
|
import android.util.Log;
|
2009-09-30 15:34:28 -07:00
|
|
|
|
2010-10-04 23:58:14 -05:00
|
|
|
public class ContactManager extends Plugin {
|
2009-11-02 15:43:09 -08:00
|
|
|
|
2010-09-10 15:09:40 -04:00
|
|
|
private static ContactAccessor contactAccessor;
|
2009-11-03 16:51:40 -08:00
|
|
|
private static final String LOG_TAG = "Contact Query";
|
2010-09-06 13:13:09 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public ContactManager() {
|
2009-10-09 10:23:21 -07:00
|
|
|
}
|
2010-06-04 13:32:00 -07:00
|
|
|
|
2010-09-06 13:13:09 -05:00
|
|
|
/**
|
|
|
|
* Executes the request and returns CommandResult.
|
|
|
|
*
|
|
|
|
* @param action The command to execute.
|
|
|
|
* @param args JSONArry of arguments for the command.
|
|
|
|
* @return A CommandResult object with a status and message.
|
|
|
|
*/
|
2010-09-07 13:59:54 -05:00
|
|
|
public PluginResult execute(String action, JSONArray args) {
|
2010-09-10 15:09:40 -04:00
|
|
|
if (contactAccessor == null) {
|
|
|
|
contactAccessor = ContactAccessor.getInstance(webView, ctx);
|
|
|
|
}
|
2010-09-07 13:59:54 -05:00
|
|
|
PluginResult.Status status = PluginResult.Status.OK;
|
2010-09-06 13:13:09 -05:00
|
|
|
String result = "";
|
|
|
|
|
|
|
|
try {
|
2010-09-10 15:09:40 -04: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-24 13:22:46 -04: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-06 13:13:09 -05:00
|
|
|
}
|
2010-09-07 13:59:54 -05:00
|
|
|
return new PluginResult(status, result);
|
2010-09-06 13:13:09 -05:00
|
|
|
} catch (JSONException e) {
|
2010-09-24 13:22:46 -04:00
|
|
|
Log.e(LOG_TAG, e.getMessage(), e);
|
2010-09-07 13:59:54 -05:00
|
|
|
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
2010-09-06 13:13:09 -05:00
|
|
|
}
|
|
|
|
}
|
2009-09-30 15:34:28 -07:00
|
|
|
}
|