forked from github/cordova-android
Fixing issue where Android 2.1 and 2.2 don't return the same results on contact.find()
This commit is contained in:
parent
6e39c46b07
commit
2a9bc2ddf8
@ -26,7 +26,10 @@ package com.phonegap;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
@ -168,13 +171,32 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
// Build the ugly where clause and where arguments for one big query.
|
// Build the ugly where clause and where arguments for one big query.
|
||||||
WhereOptions whereOptions = buildWhereClause(fields, searchTerm);
|
WhereOptions whereOptions = buildWhereClause(fields, searchTerm);
|
||||||
|
|
||||||
// Get all the rows where the search term matches the fields passed in.
|
// Get all the id's where the search term matches the fields passed in.
|
||||||
Cursor c = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
|
Cursor idCursor = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
|
||||||
null,
|
new String[] { ContactsContract.Data.CONTACT_ID },
|
||||||
whereOptions.getWhere(),
|
whereOptions.getWhere(),
|
||||||
whereOptions.getWhereArgs(),
|
whereOptions.getWhereArgs(),
|
||||||
ContactsContract.Data.CONTACT_ID + " ASC");
|
ContactsContract.Data.CONTACT_ID + " ASC");
|
||||||
|
|
||||||
|
// Create a set of unique ids
|
||||||
|
//Log.d(LOG_TAG, "ID cursor query returns = " + idCursor.getCount());
|
||||||
|
Set<String> contactIds = new HashSet<String>();
|
||||||
|
while (idCursor.moveToNext()) {
|
||||||
|
contactIds.add(idCursor.getString(idCursor.getColumnIndex(ContactsContract.Data.CONTACT_ID)));
|
||||||
|
}
|
||||||
|
idCursor.close();
|
||||||
|
|
||||||
|
// Build a query that only looks at ids
|
||||||
|
WhereOptions idOptions = buildIdClause(contactIds, searchTerm);
|
||||||
|
|
||||||
|
// Do the id query
|
||||||
|
Cursor c = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
|
||||||
|
null,
|
||||||
|
idOptions.getWhere(),
|
||||||
|
idOptions.getWhereArgs(),
|
||||||
|
ContactsContract.Data.CONTACT_ID + " ASC");
|
||||||
|
|
||||||
|
|
||||||
//Log.d(LOG_TAG, "Cursor length = " + c.getCount());
|
//Log.d(LOG_TAG, "Cursor length = " + c.getCount());
|
||||||
|
|
||||||
String contactId = "";
|
String contactId = "";
|
||||||
@ -193,11 +215,11 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
JSONArray websites = new JSONArray();
|
JSONArray websites = new JSONArray();
|
||||||
JSONArray relationships = new JSONArray();
|
JSONArray relationships = new JSONArray();
|
||||||
|
|
||||||
|
if (c.getCount() > 0) {
|
||||||
while (c.moveToNext() && (contacts.length() <= (limit-1))) {
|
while (c.moveToNext() && (contacts.length() <= (limit-1))) {
|
||||||
try {
|
try {
|
||||||
contactId = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
|
contactId = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
|
||||||
rawId = c.getString(c.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
|
rawId = c.getString(c.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
|
||||||
//Log.d(LOG_TAG, "Contact ID = " + contactId + " Raw ID = " + rawId);
|
|
||||||
|
|
||||||
// If we are in the first row set the oldContactId
|
// If we are in the first row set the oldContactId
|
||||||
if (c.getPosition() == 0) {
|
if (c.getPosition() == 0) {
|
||||||
@ -232,7 +254,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
newContact = false;
|
newContact = false;
|
||||||
contact.put("id", contactId);
|
contact.put("id", contactId);
|
||||||
contact.put("rawId", rawId);
|
contact.put("rawId", rawId);
|
||||||
contact.put("displayName", c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
|
contact.put("displayName", c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab the mimetype of the current row as it will be used in a lot of comparisons
|
// Grab the mimetype of the current row as it will be used in a lot of comparisons
|
||||||
@ -296,17 +318,54 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
// Set the old contact ID
|
// Set the old contact ID
|
||||||
oldContactId = contactId;
|
oldContactId = contactId;
|
||||||
}
|
}
|
||||||
c.close();
|
|
||||||
|
|
||||||
// Push the last contact into the contacts array
|
// Push the last contact into the contacts array
|
||||||
contacts.put(populateContact(contact, organizations, addresses, phones,
|
contacts.put(populateContact(contact, organizations, addresses, phones,
|
||||||
emails, ims, websites, relationships));
|
emails, ims, websites, relationships));
|
||||||
|
}
|
||||||
|
c.close();
|
||||||
|
|
||||||
|
|
||||||
totalEnd = System.currentTimeMillis();
|
totalEnd = System.currentTimeMillis();
|
||||||
Log.d(LOG_TAG,"Total time = " + (totalEnd-totalStart));
|
Log.d(LOG_TAG,"Total time = " + (totalEnd-totalStart));
|
||||||
return contacts;
|
return contacts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a where clause all all the ids passed into the method
|
||||||
|
* @param contactIds a set of unique contact ids
|
||||||
|
* @param searchTerm what to search for
|
||||||
|
* @return an object containing the selection and selection args
|
||||||
|
*/
|
||||||
|
private WhereOptions buildIdClause(Set<String> contactIds, String searchTerm) {
|
||||||
|
WhereOptions options = new WhereOptions();
|
||||||
|
|
||||||
|
// If the user is searching for every contact then short circuit the method
|
||||||
|
// and return a shorter where clause to be searched.
|
||||||
|
if (searchTerm.equals("%")) {
|
||||||
|
options.setWhere("(" + ContactsContract.Data.CONTACT_ID + " LIKE ? )");
|
||||||
|
options.setWhereArgs(new String[] {searchTerm});
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This clause means that there are specific ID's to be populated
|
||||||
|
Iterator<String> it = contactIds.iterator();
|
||||||
|
StringBuffer buffer = new StringBuffer("(");
|
||||||
|
|
||||||
|
while (it.hasNext()) {
|
||||||
|
buffer.append("'" + it.next() + "'");
|
||||||
|
if (it.hasNext()) {
|
||||||
|
buffer.append(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buffer.append(")");
|
||||||
|
|
||||||
|
options.setWhere(ContactsContract.Data.CONTACT_ID + " IN " + buffer.toString());
|
||||||
|
options.setWhereArgs(null);
|
||||||
|
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new contact using a JSONObject to hold all the data.
|
* Create a new contact using a JSONObject to hold all the data.
|
||||||
* @param contact
|
* @param contact
|
||||||
@ -455,9 +514,6 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
}
|
}
|
||||||
options.setWhere(selection.toString());
|
options.setWhere(selection.toString());
|
||||||
|
|
||||||
Log.d(LOG_TAG, "The where clause is:");
|
|
||||||
Log.d(LOG_TAG, selection.toString());
|
|
||||||
|
|
||||||
// Creating the where args array
|
// Creating the where args array
|
||||||
String[] selectionArgs = new String[whereArgs.size()];
|
String[] selectionArgs = new String[whereArgs.size()];
|
||||||
for (int i=0; i<whereArgs.size(); i++) {
|
for (int i=0; i<whereArgs.size(); i++) {
|
||||||
@ -684,32 +740,27 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
|
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
|
||||||
|
|
||||||
//Add contact type
|
//Add contact type
|
||||||
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
|
ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI)
|
||||||
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type)
|
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type)
|
||||||
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name)
|
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
// Modify display name
|
|
||||||
String displayName = getJsonString(contact, "displayName");
|
|
||||||
if (displayName != null) {
|
|
||||||
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
|
|
||||||
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
|
|
||||||
ContactsContract.Data.MIMETYPE + "=?",
|
|
||||||
new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE})
|
|
||||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, getJsonString(contact, "displayName"))
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Modify name
|
// Modify name
|
||||||
JSONObject name;
|
JSONObject name;
|
||||||
try {
|
try {
|
||||||
|
String displayName = getJsonString(contact, "displayName");
|
||||||
|
Log.d(LOG_TAG, "UPdated display name is = " + displayName);
|
||||||
name = contact.getJSONObject("name");
|
name = contact.getJSONObject("name");
|
||||||
if (name != null) {
|
if (displayName != null || name != null) {
|
||||||
ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
|
ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
|
||||||
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
|
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
|
||||||
ContactsContract.Data.MIMETYPE + "=?",
|
ContactsContract.Data.MIMETYPE + "=?",
|
||||||
new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
|
new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
|
||||||
|
|
||||||
|
if (displayName != null) {
|
||||||
|
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
|
||||||
|
}
|
||||||
|
|
||||||
String familyName = getJsonString(name, "familyName");
|
String familyName = getJsonString(name, "familyName");
|
||||||
if (familyName != null) {
|
if (familyName != null) {
|
||||||
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName);
|
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName);
|
||||||
@ -1026,7 +1077,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
// Modify birthday
|
// Modify birthday
|
||||||
String birthday = getJsonString(contact, "birthday");
|
String birthday = getJsonString(contact, "birthday");
|
||||||
if (birthday != null) {
|
if (birthday != null) {
|
||||||
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
|
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
|
||||||
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
|
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
|
||||||
ContactsContract.Data.MIMETYPE + "=? AND " +
|
ContactsContract.Data.MIMETYPE + "=? AND " +
|
||||||
ContactsContract.CommonDataKinds.Event.TYPE + "=?",
|
ContactsContract.CommonDataKinds.Event.TYPE + "=?",
|
||||||
@ -1039,7 +1090,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
// Modify anniversary
|
// Modify anniversary
|
||||||
String anniversary = getJsonString(contact, "anniversary");
|
String anniversary = getJsonString(contact, "anniversary");
|
||||||
if (anniversary != null) {
|
if (anniversary != null) {
|
||||||
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
|
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
|
||||||
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
|
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
|
||||||
ContactsContract.Data.MIMETYPE + "=? AND " +
|
ContactsContract.Data.MIMETYPE + "=? AND " +
|
||||||
ContactsContract.CommonDataKinds.Event.TYPE + "=?",
|
ContactsContract.CommonDataKinds.Event.TYPE + "=?",
|
||||||
@ -1201,20 +1252,16 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name)
|
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
//Add display name
|
|
||||||
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
|
|
||||||
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
|
|
||||||
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
|
||||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, getJsonString(contact, "displayName"))
|
|
||||||
.build());
|
|
||||||
|
|
||||||
// Add name
|
// Add name
|
||||||
try {
|
try {
|
||||||
JSONObject name = contact.getJSONObject("name");
|
JSONObject name = contact.optJSONObject("name");
|
||||||
if (name != null) {
|
String displayName = contact.getString("displayName");
|
||||||
|
Log.d(LOG_TAG, "The passed in display name is = " + displayName);
|
||||||
|
if (displayName != null || name != null) {
|
||||||
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
|
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
|
||||||
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
|
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
|
||||||
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
||||||
|
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
|
||||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, getJsonString(name, "familyName"))
|
.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, getJsonString(name, "familyName"))
|
||||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, getJsonString(name, "middleName"))
|
.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, getJsonString(name, "middleName"))
|
||||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, getJsonString(name, "givenName"))
|
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, getJsonString(name, "givenName"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user