Adding queries for addresses and organization

This commit is contained in:
macdonst
2010-09-17 17:15:30 -04:00
parent c2bcc29cfb
commit ca4d7f7fd2
3 changed files with 207 additions and 58 deletions
@@ -63,9 +63,18 @@ public class ContactAccessorSdk5 extends ContactAccessor {
@Override
public void search(JSONArray filter, JSONObject options) {
String searchTerm = "";
int limit = Integer.MAX_VALUE;
boolean multiple = true;
try {
searchTerm = options.getString("filter");
if (searchTerm.length()==0) searchTerm = "%";
multiple = options.getBoolean("multiple");
if (multiple) {
limit = options.getInt("limit");
}
System.out.println("Limit = " + limit);
System.out.println("Multiple = " + multiple);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -81,7 +90,8 @@ public class ContactAccessorSdk5 extends ContactAccessor {
new String[] {searchTerm},
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
JSONArray contacts = new JSONArray();
while (cursor.moveToNext()) {
int pos = 0;
while (cursor.moveToNext() && (pos < limit)) {
JSONObject contact = new JSONObject();
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
@@ -97,51 +107,160 @@ public class ContactAccessorSdk5 extends ContactAccessor {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
//if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
if (phones.moveToFirst()) {
Log.d(LOG_TAG, "We found a phone!");
JSONArray phoneNumbers = new JSONArray();
JSONObject phoneNumber = new JSONObject();
try {
phoneNumber.put("primary", true);
phoneNumber.put("value", phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace('\'', '`'));
phoneNumbers.put(phoneNumber);
contact.put("phoneNumbers", phoneNumbers);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
phones.close();
//}
Cursor emails = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null);
if (emails.moveToFirst()) {
Log.d(LOG_TAG, "We found an email!");
JSONArray emailAddresses = new JSONArray();
JSONObject email = new JSONObject();
try {
email.put("primary", true);
email.put("value", emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).replace('\'', '`'));
emailAddresses.put(email);
contact.put("emails", emailAddresses);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
emails.close();
try {
contact.put("name", nameQuery(cr, contactId));
contact.put("phoneNumbers", phoneQuery(cr, contactId));
contact.put("emails", emailQuery(cr, contactId));
contact.put("addresses", addressQuery(cr, contactId));
contact.put("organizations", organizationQuery(cr, contactId));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
contacts.put(contact);
pos++;
}
cursor.close();
mView.loadUrl("javascript:navigator.service.contacts.droidDone('" + contacts.toString() + "');");
}
private JSONArray organizationQuery(ContentResolver cr, String contactId) {
// TODO Fix the query URI
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
JSONArray organizations = new JSONArray();
JSONObject organization = new JSONObject();
while (cursor.moveToNext()) {
Log.d(LOG_TAG, "We found a phone!");
try {
organization.put("department", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT)));
organization.put("description", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION)));
// TODO No endDate
// organization.put("endDate", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization)));
organization.put("location", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION)));
organization.put("name", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY)));
// TODO no startDate
// organization.put("startDate", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization)));
organization.put("title", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)));
organizations.put(organization);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
cursor.close();
return organizations;
}
private JSONArray addressQuery(ContentResolver cr, String contactId) {
// TODO Fix the query URI
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
JSONArray addresses = new JSONArray();
JSONObject address = new JSONObject();
while (cursor.moveToNext()) {
Log.d(LOG_TAG, "We found a phone!");
try {
address.put("formatted", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS)));
address.put("streetAddress", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
address.put("locality", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)));
address.put("region", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)));
address.put("postalCode", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)));
address.put("country", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)));
addresses.put(address);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
cursor.close();
return addresses;
}
private JSONObject nameQuery(ContentResolver cr, String contactId) {
// TODO Fix the query URI
Cursor name = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
JSONObject contactName = new JSONObject();
if (name.moveToFirst()) {
Log.d(LOG_TAG, "We found a name!");
try {
contactName.put("familyName", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)));
contactName.put("givenName", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)));
contactName.put("middleName", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME)));
contactName.put("honorificPrefix", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX)));
contactName.put("honorificSuffix", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX)));
contactName.put("formatted", name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX))
+ " " + name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME))
+ " " + name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME))
+ " " + name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))
+ " " + name.getString(name.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX)));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
name.close();
return contactName;
}
private JSONArray phoneQuery(ContentResolver cr, String contactId) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
JSONArray phoneNumbers = new JSONArray();
JSONObject phoneNumber = new JSONObject();
while (phones.moveToNext()) {
Log.d(LOG_TAG, "We found a phone!");
try {
phoneNumber.put("primary", false); // Android does not store primary attribute
phoneNumber.put("value", phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace('\'', '`'));
phoneNumber.put("type", phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)));
phoneNumbers.put(phoneNumber);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
phones.close();
return phoneNumbers;
}
private JSONArray emailQuery(ContentResolver cr, String contactId) {
Cursor emails = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null);
JSONArray emailAddresses = new JSONArray();
JSONObject email = new JSONObject();
while (emails.moveToNext()) {
Log.d(LOG_TAG, "We found an email!");
try {
email.put("primary", false);
email.put("value", emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)).replace('\'', '`'));
email.put("type", emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)));
emailAddresses.put(email);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
emails.close();
return emailAddresses;
}
}