@ShortName("Contacts")
@Permissions(values={"android.permission.READ_CONTACTS"})
public class ContactsWrapper {
private static final String[] people_projection = {Contacts.People.TIMES_CONTACTED,
Contacts.Phones.NUMBER, Contacts.People.LAST_TIME_CONTACTED,
Contacts.People.DISPLAY_NAME, Contacts.People.NAME, Contacts.People.NOTES, Contacts.People.STARRED, BaseColumns._ID};
/**
* Returns a List of Contact objects with all the contacts. This list can be very large.
*/
public List GetAll() {
return getAllContacts(null, null);
}
/**
* Returns a List of Contact objects with all contacts matching the given name.
*Name - The name to search for.
*Exact - If True then only contacts with the exact name value (case sensitive) will return
*, otherwise all contacts names that include the Name string will return (case insensitive).
*/
public List FindByName(String Name, boolean Exact) {
if (!Exact)
return getAllContacts(Contacts.People.NAME + " LIKE ?", new String[] {"%" + Name + "%"});
else
return getAllContacts(Contacts.People.NAME + " = ?", new String[] {Name});
}
/**
* Returns a List of Contact objects with all contacts matching the given email.
*Email - The email to search for.
*Exact - If True then only contacts with the exact email address (case sensitive) will return
*, otherwise all contacts email addresses that include the Email string will return (case insensitive).
*/
public List FindByMail(String Email, boolean Exact) {
ContentResolver cr = BA.applicationContext.getContentResolver();
String sel, args;
if (!Exact) {
sel = " LIKE ?";
args = "%" + Email + "%";
}
else {
sel = " = ?";
args = Email;
}
Cursor crsr = cr.query(Contacts.ContactMethods.CONTENT_EMAIL_URI, new String[] {Contacts.ContactMethods.PERSON_ID,
Contacts.ContactMethods.DATA}, Contacts.ContactMethods.DATA + sel, new String[] {args}, null);
StringBuilder sb = new StringBuilder();
while (crsr.moveToNext()) {
for (int i = 0;i < crsr.getColumnCount();i++) {
sb.append(crsr.getString(0)).append(",");
}
}
int count = crsr.getCount();
crsr.close();
if (count == 0) {
List l = new List();
l.Initialize();
return l;
}
sb.setLength(sb.length() - 1);
String selection = BaseColumns._ID + " IN (" + sb.toString() + ")";
return getAllContacts(selection, null);
}
/**
* Returns the Contact with the specified Id.
* Returns Null if no matching contact found.
*/
public Contact GetById(int Id) {
List l = getAllContacts(BaseColumns._ID + " = ?", new String[] {String.valueOf(Id)});
if (l.getSize() == 0)
return null;
else
return (Contact) l.Get(0);
}
private List getAllContacts(String selection, String[] args) {
ContentResolver cr = BA.applicationContext.getContentResolver();
Cursor crsr = cr.query(Contacts.People.CONTENT_URI, people_projection, selection, args, null);
List l = new List();
l.Initialize();
HashMap<String, Integer> m = new HashMap<String, Integer>();
for (int col = 0;col < crsr.getColumnCount();col++) {
m.put(crsr.getColumnName(col), col);
}
while (crsr.moveToNext()) {
Contact contact = new Contact(
crsr.getString(m.get(Contacts.People.DISPLAY_NAME)),
crsr.getString(m.get(Contacts.Phones.NUMBER)),
crsr.getInt(m.get(Contacts.People.STARRED)) > 0,
crsr.getInt(m.get(BaseColumns._ID)),
crsr.getString(m.get(Contacts.People.NOTES)),
crsr.getInt(m.get(Contacts.People.TIMES_CONTACTED)),
crsr.getLong(m.get(Contacts.People.LAST_TIME_CONTACTED)),
crsr.getString(m.get(Contacts.People.NAME)));
l.Add(contact);
}
crsr.close();
return l;
}