Adding Contact.remove method

This commit is contained in:
macdonst 2010-10-01 11:09:59 +08:00
parent 668bc9e0ca
commit 6071b9c75a
6 changed files with 86 additions and 18 deletions

View File

@ -28,7 +28,14 @@ var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, ad
}; };
Contact.prototype.remove = function(contact) { Contact.prototype.remove = function(successCB, errorCB) {
if (this.id == null) {
var errorObj = new ContactError();
errorObj.code = ContactError.NOT_FOUND_ERROR;
errorCB(errorObj);
}
PhoneGap.execAsync(successCB, errorCB, "Contacts", "remove", [this.id]);
}; };
Contact.prototype.clone = function() { Contact.prototype.clone = function() {
@ -37,7 +44,7 @@ Contact.prototype.clone = function() {
return clonedContact; return clonedContact;
}; };
Contact.prototype.save = function(contact) { Contact.prototype.save = function(win, fail) {
}; };
@ -118,14 +125,14 @@ var ContactError = function() {
this.code=null; this.code=null;
}; };
ContactError.INVALID_ARGUMENT_ERROR = 0; ContactError.UNKNOWN_ERROR = 0;
ContactError.IO_ERROR = 1; ContactError.INVALID_ARGUMENT_ERROR = 1;
ContactError.NOT_FOUND_ERROR = 2; ContactError.NOT_FOUND_ERROR = 2;
ContactError.NOT_SUPPORTED_ERROR = 3; ContactError.TIMEOUT_ERROR = 3;
ContactError.PENDING_OPERATION_ERROR = 4; ContactError.PENDING_OPERATION_ERROR = 4;
ContactError.PERMISSION_DENIED_ERROR = 5; ContactError.IO_ERROR = 5;
ContactError.TIMEOUT_ERROR = 6; ContactError.NOT_SUPPORTED_ERROR = 6;
ContactError.UNKNOWN_ERROR = 7; ContactError.PERMISSION_DENIED_ERROR = 20;
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {
if(typeof navigator.service == "undefined") navigator.service = new Object(); if(typeof navigator.service == "undefined") navigator.service = new Object();

View File

@ -977,17 +977,23 @@ var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, ad
}; };
Contact.prototype.remove = function(contact) { Contact.prototype.remove = function(successCB, errorCB) {
if (this.id == null) {
var errorObj = new ContactError();
errorObj.code = ContactError.NOT_FOUND_ERROR;
errorCB(errorObj);
}
PhoneGap.execAsync(successCB, errorCB, "Contacts", "remove", [this.id]);
}; };
Contact.prototype.clone = function() { Contact.prototype.clone = function() {
console.log("PhoneGap clone version 2");
var clonedContact = PhoneGap.clone(this); var clonedContact = PhoneGap.clone(this);
clonedContact.id = null; clonedContact.id = null;
return clonedContact; return clonedContact;
}; };
Contact.prototype.save = function(contact) { Contact.prototype.save = function(win, fail) {
}; };
@ -1068,14 +1074,14 @@ var ContactError = function() {
this.code=null; this.code=null;
}; };
ContactError.INVALID_ARGUMENT_ERROR = 0; ContactError.UNKNOWN_ERROR = 0;
ContactError.IO_ERROR = 1; ContactError.INVALID_ARGUMENT_ERROR = 1;
ContactError.NOT_FOUND_ERROR = 2; ContactError.NOT_FOUND_ERROR = 2;
ContactError.NOT_SUPPORTED_ERROR = 3; ContactError.TIMEOUT_ERROR = 3;
ContactError.PENDING_OPERATION_ERROR = 4; ContactError.PENDING_OPERATION_ERROR = 4;
ContactError.PERMISSION_DENIED_ERROR = 5; ContactError.IO_ERROR = 5;
ContactError.TIMEOUT_ERROR = 6; ContactError.NOT_SUPPORTED_ERROR = 6;
ContactError.UNKNOWN_ERROR = 7; ContactError.PERMISSION_DENIED_ERROR = 20;
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {
if(typeof navigator.service == "undefined") navigator.service = new Object(); if(typeof navigator.service == "undefined") navigator.service = new Object();

View File

@ -83,8 +83,18 @@ public abstract class ContactAccessor {
return sInstance; return sInstance;
} }
/**
* Handles adding a JSON Contact object into the database.
*/
public abstract void save();
/** /**
* Handles searching through SDK-specific contacts API. * Handles searching through SDK-specific contacts API.
*/ */
public abstract void search(JSONArray filter, JSONObject options); public abstract void search(JSONArray filter, JSONObject options);
/**
* Handles removing a contact from the database.
*/
public abstract boolean remove(String id);
} }

View File

@ -31,6 +31,7 @@ import android.app.Activity;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.Contacts.ContactMethods; import android.provider.Contacts.ContactMethods;
import android.provider.Contacts.ContactMethodsColumns; import android.provider.Contacts.ContactMethodsColumns;
import android.provider.Contacts.Organizations; import android.provider.Contacts.Organizations;
@ -359,4 +360,22 @@ public class ContactAccessorSdk3_4 extends ContactAccessor {
} }
return emails; return emails;
} }
@Override
public void save() {
// TODO Auto-generated method stub
}
@Override
public boolean remove(String id) {
ContentResolver cr = mApp.getContentResolver();
int result = cr.delete(People.CONTENT_URI, "people._id = ?", new String[] {id});
Log.d(LOG_TAG, "Content URI = " + People.CONTENT_URI);
Log.d(LOG_TAG, "Where = " + "people._id = ?");
Log.d(LOG_TAG, "Number of rows deleted = " + result);
return (result > 0) ? true : false;
}
} }

View File

@ -550,4 +550,22 @@ public class ContactAccessorSdk5 extends ContactAccessor {
cursor.close(); cursor.close();
return retVal; return retVal;
} }
@Override
public void save() {
// TODO Auto-generated method stub
}
@Override
public boolean remove(String id) {
ContentResolver cr = mApp.getContentResolver();
int result = cr.delete(ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.CONTACT_ID + " = ?",
new String[] {id});
Log.d(LOG_TAG, "Content URI = " + ContactsContract.Data.CONTENT_URI);
Log.d(LOG_TAG, "Where = " + ContactsContract.Data.CONTACT_ID + " = ?");
Log.d(LOG_TAG, "Number of rows deleted = " + result);
return (result > 0) ? true : false;
}
} }

View File

@ -2,6 +2,7 @@ package com.phonegap;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import com.phonegap.api.Plugin; import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult; import com.phonegap.api.PluginResult;
@ -69,7 +70,14 @@ public class ContactManager implements Plugin {
// TODO Coming soon! // TODO Coming soon!
} }
else if (action.equals("remove")) { else if (action.equals("remove")) {
// TODO Coming soon! 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);
}
} }
return new PluginResult(status, result); return new PluginResult(status, result);
} catch (JSONException e) { } catch (JSONException e) {