2010-10-19 04:31:16 +08:00
|
|
|
/*
|
|
|
|
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
|
|
|
|
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2010, Nitobi Software Inc.
|
|
|
|
* Copyright (c) 2010, IBM Corporation
|
|
|
|
*/
|
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
|
|
|
|
2011-07-13 00:14:15 +08:00
|
|
|
private 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
|
|
|
|
2011-07-13 00:14:15 +08:00
|
|
|
public static final int UNKNOWN_ERROR = 0;
|
|
|
|
public static final int INVALID_ARGUMENT_ERROR = 1;
|
|
|
|
public static final int TIMEOUT_ERROR = 2;
|
|
|
|
public static final int PENDING_OPERATION_ERROR = 3;
|
|
|
|
public static final int IO_ERROR = 4;
|
|
|
|
public static final int NOT_SUPPORTED_ERROR = 5;
|
|
|
|
public static final int PERMISSION_DENIED_ERROR = 20;
|
|
|
|
|
|
|
|
|
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) {
|
2011-07-13 00:14:15 +08:00
|
|
|
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
|
|
|
|
* do not support this as of PhoneGap 1.0.
|
|
|
|
*/
|
|
|
|
if (android.os.Build.VERSION.RELEASE.startsWith("1.")) {
|
|
|
|
JSONObject res = null;
|
|
|
|
try {
|
|
|
|
res = new JSONObject();
|
|
|
|
res.put("code", NOT_SUPPORTED_ERROR);
|
|
|
|
res.put("message", "Contacts are not supported in Android 1.X devices");
|
|
|
|
} catch (JSONException e) {
|
|
|
|
// This should never happen
|
|
|
|
Log.e(LOG_TAG, e.getMessage(), e);
|
|
|
|
}
|
|
|
|
return new PluginResult(PluginResult.Status.ERROR, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only create the contactAccessor after we check the Android version or the program will crash
|
|
|
|
* older phones.
|
|
|
|
*/
|
|
|
|
if (this.contactAccessor == null) {
|
|
|
|
this.contactAccessor = new ContactAccessorSdk5(this.webView, this.ctx);
|
|
|
|
}
|
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
try {
|
2010-09-11 03:09:40 +08:00
|
|
|
if (action.equals("search")) {
|
2011-01-25 03:04:11 +08:00
|
|
|
JSONArray res = contactAccessor.search(args.getJSONArray(0), args.optJSONObject(1));
|
2011-06-24 22:55:19 +08:00
|
|
|
return new PluginResult(status, res, "navigator.contacts.cast");
|
2010-09-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
else if (action.equals("save")) {
|
2011-06-24 22:55:19 +08:00
|
|
|
String id = contactAccessor.save(args.getJSONObject(0));
|
|
|
|
if (id != null) {
|
|
|
|
JSONObject res = contactAccessor.getContactById(id);
|
|
|
|
if (res != null) {
|
|
|
|
return new PluginResult(status, res);
|
|
|
|
}
|
2011-04-29 02:06:58 +08:00
|
|
|
}
|
2010-09-25 01:22:46 +08:00
|
|
|
}
|
|
|
|
else if (action.equals("remove")) {
|
2010-10-01 11:09:59 +08:00
|
|
|
if (contactAccessor.remove(args.getString(0))) {
|
|
|
|
return new PluginResult(status, result);
|
|
|
|
}
|
2010-09-07 02:13:09 +08:00
|
|
|
}
|
2011-06-24 22:55:19 +08:00
|
|
|
// If we get to this point an error has occurred
|
|
|
|
JSONObject r = new JSONObject();
|
2011-07-13 00:14:15 +08:00
|
|
|
r.put("code", UNKNOWN_ERROR);
|
2011-06-24 22:55:19 +08:00
|
|
|
return new PluginResult(PluginResult.Status.ERROR, r);
|
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
|
|
|
}
|