2010-08-14 09:59:33 -04:00
|
|
|
package com.fsck.k9.helper;
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
2013-06-25 16:47:47 -04:00
|
|
|
import android.content.ContentUris;
|
2010-08-14 09:59:33 -04:00
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2013-02-07 14:01:49 -05:00
|
|
|
import android.net.Uri;
|
2011-03-22 03:06:11 -04:00
|
|
|
import android.content.Intent;
|
2013-06-25 16:47:47 -04:00
|
|
|
import android.provider.ContactsContract;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.fsck.k9.K9;
|
2010-08-14 09:59:33 -04:00
|
|
|
import com.fsck.k9.mail.Address;
|
|
|
|
|
2013-06-25 16:47:47 -04:00
|
|
|
import java.util.ArrayList;
|
2014-10-04 06:45:45 -04:00
|
|
|
import java.util.List;
|
2013-06-25 16:47:47 -04:00
|
|
|
|
2010-08-14 09:59:33 -04:00
|
|
|
/**
|
2013-06-25 16:47:47 -04:00
|
|
|
* Helper class to access the contacts stored on the device.
|
2010-08-14 09:59:33 -04:00
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public class Contacts {
|
2010-08-14 09:59:33 -04:00
|
|
|
/**
|
2013-06-25 16:47:47 -04:00
|
|
|
* The order in which the search results are returned by
|
|
|
|
* {@link #searchContacts(CharSequence)}.
|
2010-08-14 09:59:33 -04:00
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
protected static final String SORT_ORDER =
|
|
|
|
ContactsContract.CommonDataKinds.Email.TIMES_CONTACTED + " DESC, " +
|
|
|
|
ContactsContract.Contacts.DISPLAY_NAME + ", " +
|
|
|
|
ContactsContract.CommonDataKinds.Email._ID;
|
2010-08-14 09:59:33 -04:00
|
|
|
|
|
|
|
/**
|
2013-06-25 16:47:47 -04:00
|
|
|
* Array of columns to load from the database.
|
|
|
|
*
|
|
|
|
* Important: The _ID field is needed by
|
|
|
|
* {@link com.fsck.k9.EmailAddressAdapter} or more specificly by
|
|
|
|
* {@link android.widget.ResourceCursorAdapter}.
|
|
|
|
*/
|
|
|
|
protected static final String PROJECTION[] = {
|
|
|
|
ContactsContract.CommonDataKinds.Email._ID,
|
|
|
|
ContactsContract.Contacts.DISPLAY_NAME,
|
|
|
|
ContactsContract.CommonDataKinds.Email.DATA,
|
|
|
|
ContactsContract.CommonDataKinds.Email.CONTACT_ID
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Index of the name field in the projection. This must match the order in
|
|
|
|
* {@link #PROJECTION}.
|
|
|
|
*/
|
|
|
|
protected static final int NAME_INDEX = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Index of the email address field in the projection. This must match the
|
|
|
|
* order in {@link #PROJECTION}.
|
|
|
|
*/
|
|
|
|
protected static final int EMAIL_INDEX = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Index of the contact id field in the projection. This must match the order in
|
|
|
|
* {@link #PROJECTION}.
|
|
|
|
*/
|
|
|
|
protected static final int CONTACT_ID_INDEX = 3;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get instance of the Contacts class.
|
|
|
|
*
|
|
|
|
* <p>Note: This is left over from the days when we needed to have SDK-specific code to access
|
|
|
|
* the contacts.</p>
|
2010-08-14 09:59:33 -04:00
|
|
|
*
|
|
|
|
* @param context A {@link Context} instance.
|
|
|
|
* @return Appropriate {@link Contacts} instance for this device.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public static Contacts getInstance(Context context) {
|
2013-06-25 16:47:47 -04:00
|
|
|
return new Contacts(context);
|
2010-08-14 09:59:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected Context mContext;
|
|
|
|
protected ContentResolver mContentResolver;
|
2011-03-24 00:11:03 -04:00
|
|
|
protected Boolean mHasContactPicker;
|
2010-08-14 09:59:33 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param context A {@link Context} instance.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
protected Contacts(Context context) {
|
2010-08-14 09:59:33 -04:00
|
|
|
mContext = context;
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the activity to add information to an existing contact or add a
|
|
|
|
* new one.
|
|
|
|
*
|
|
|
|
* @param email An {@link Address} instance containing the email address
|
|
|
|
* of the entity you want to add to the contacts. Optionally
|
|
|
|
* the instance also contains the (display) name of that
|
|
|
|
* entity.
|
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public void createContact(final Address email) {
|
|
|
|
final Uri contactUri = Uri.fromParts("mailto", email.getAddress(), null);
|
|
|
|
|
|
|
|
final Intent contactIntent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
|
|
|
|
contactIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
contactIntent.setData(contactUri);
|
|
|
|
|
|
|
|
// Pass along full E-mail string for possible create dialog
|
|
|
|
contactIntent.putExtra(ContactsContract.Intents.EXTRA_CREATE_DESCRIPTION,
|
|
|
|
email.toString());
|
|
|
|
|
|
|
|
// Only provide personal name hint if we have one
|
|
|
|
final String senderPersonal = email.getPersonal();
|
|
|
|
if (senderPersonal != null) {
|
|
|
|
contactIntent.putExtra(ContactsContract.Intents.Insert.NAME, senderPersonal);
|
|
|
|
}
|
|
|
|
|
|
|
|
mContext.startActivity(contactIntent);
|
|
|
|
}
|
2010-08-14 09:59:33 -04:00
|
|
|
|
2012-04-01 16:46:36 -04:00
|
|
|
/**
|
|
|
|
* Start the activity to add a phone number to an existing contact or add a new one.
|
|
|
|
*
|
|
|
|
* @param phoneNumber
|
|
|
|
* The phone number to add to a contact, or to use when creating a new contact.
|
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public void addPhoneContact(final String phoneNumber) {
|
|
|
|
Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
|
|
|
|
addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
|
|
|
|
addIntent.putExtra(ContactsContract.Intents.Insert.PHONE, Uri.decode(phoneNumber));
|
|
|
|
addIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
mContext.startActivity(addIntent);
|
|
|
|
}
|
2012-04-01 16:46:36 -04:00
|
|
|
|
2010-08-14 09:59:33 -04:00
|
|
|
/**
|
|
|
|
* Check whether the provided email address belongs to one of the contacts.
|
|
|
|
*
|
|
|
|
* @param emailAddress The email address to look for.
|
|
|
|
* @return <tt>true</tt>, if the email address belongs to a contact.
|
|
|
|
* <tt>false</tt>, otherwise.
|
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public boolean isInContacts(final String emailAddress) {
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
final Cursor c = getContactByAddress(emailAddress);
|
|
|
|
|
|
|
|
if (c != null) {
|
|
|
|
if (c.getCount() > 0) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2010-08-14 09:59:33 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the contacts matching the given search term.
|
|
|
|
*
|
2013-06-25 16:47:47 -04:00
|
|
|
* @param constraint The search term to filter the contacts.
|
2010-08-14 09:59:33 -04:00
|
|
|
* @return A {@link Cursor} instance that can be used to get the
|
|
|
|
* matching contacts.
|
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public Cursor searchContacts(final CharSequence constraint) {
|
|
|
|
final String filter = (constraint == null) ? "" : constraint.toString();
|
|
|
|
final Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI, Uri.encode(filter));
|
|
|
|
final Cursor c = mContentResolver.query(
|
|
|
|
uri,
|
|
|
|
PROJECTION,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
SORT_ORDER);
|
|
|
|
|
|
|
|
if (c != null) {
|
|
|
|
/*
|
|
|
|
* To prevent expensive execution in the UI thread:
|
|
|
|
* Cursors get lazily executed, so if you don't call anything on
|
|
|
|
* the cursor before returning it from the background thread you'll
|
|
|
|
* have a complied program for the cursor, but it won't have been
|
|
|
|
* executed to generate the data yet. Often the execution is more
|
|
|
|
* expensive than the compilation...
|
|
|
|
*/
|
|
|
|
c.getCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
2010-08-14 09:59:33 -04:00
|
|
|
|
2010-10-08 20:24:43 -04:00
|
|
|
/**
|
|
|
|
* Get the name of the contact an email address belongs to.
|
|
|
|
*
|
|
|
|
* @param address The email address to search for.
|
|
|
|
* @return The name of the contact the email address belongs to. Or
|
|
|
|
* <tt>null</tt> if there's no matching contact.
|
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public String getNameForAddress(String address) {
|
|
|
|
if (address == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final Cursor c = getContactByAddress(address);
|
|
|
|
|
|
|
|
String name = null;
|
|
|
|
if (c != null) {
|
|
|
|
if (c.getCount() > 0) {
|
|
|
|
c.moveToFirst();
|
|
|
|
name = getName(c);
|
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
2010-08-30 10:37:34 -04:00
|
|
|
|
2010-08-14 09:59:33 -04:00
|
|
|
/**
|
|
|
|
* Extract the name from a {@link Cursor} instance returned by
|
|
|
|
* {@link #searchContacts(CharSequence)}.
|
|
|
|
*
|
|
|
|
* @param cursor The {@link Cursor} instance.
|
|
|
|
* @return The name of the contact in the {@link Cursor}'s current row.
|
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public String getName(Cursor cursor) {
|
|
|
|
return cursor.getString(NAME_INDEX);
|
|
|
|
}
|
2010-08-14 09:59:33 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract the email address from a {@link Cursor} instance returned by
|
|
|
|
* {@link #searchContacts(CharSequence)}.
|
|
|
|
*
|
|
|
|
* @param cursor The {@link Cursor} instance.
|
|
|
|
* @return The email address of the contact in the {@link Cursor}'s current
|
|
|
|
* row.
|
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public String getEmail(Cursor cursor) {
|
|
|
|
return cursor.getString(EMAIL_INDEX);
|
|
|
|
}
|
2010-10-30 16:35:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark contacts with the provided email addresses as contacted.
|
|
|
|
*
|
|
|
|
* @param addresses Array of {@link Address} objects describing the
|
|
|
|
* contacts to be marked as contacted.
|
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public void markAsContacted(final Address[] addresses) {
|
|
|
|
//TODO: Optimize! Potentially a lot of database queries
|
|
|
|
for (final Address address : addresses) {
|
|
|
|
final Cursor c = getContactByAddress(address.getAddress());
|
|
|
|
|
|
|
|
if (c != null) {
|
|
|
|
if (c.getCount() > 0) {
|
|
|
|
c.moveToFirst();
|
|
|
|
final long personId = c.getLong(CONTACT_ID_INDEX);
|
|
|
|
ContactsContract.Contacts.markAsContacted(mContentResolver, personId);
|
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-22 03:06:11 -04:00
|
|
|
|
2011-03-23 16:51:06 -04:00
|
|
|
/**
|
|
|
|
* Creates the intent necessary to open a contact picker.
|
2011-03-22 03:06:11 -04:00
|
|
|
*
|
2011-03-23 16:51:06 -04:00
|
|
|
* @return The intent necessary to open a contact picker.
|
2011-03-22 03:06:11 -04:00
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public Intent contactPickerIntent() {
|
|
|
|
return new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
|
|
|
|
}
|
2011-03-22 03:06:11 -04:00
|
|
|
|
2011-03-23 16:51:06 -04:00
|
|
|
/**
|
2012-04-08 19:03:34 -04:00
|
|
|
* Given a contact picker intent, returns a {@code ContactItem} instance for that contact.
|
2011-03-22 03:06:11 -04:00
|
|
|
*
|
2012-04-08 19:03:34 -04:00
|
|
|
* @param intent
|
|
|
|
* The {@link Intent} returned by the contact picker.
|
|
|
|
*
|
|
|
|
* @return A {@link ContactItem} instance describing the picked contact. Or {@code null} if the
|
|
|
|
* contact doesn't have any email addresses.
|
2011-03-22 03:06:11 -04:00
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public ContactItem extractInfoFromContactPickerIntent(final Intent intent) {
|
|
|
|
Cursor cursor = null;
|
2014-10-04 06:45:45 -04:00
|
|
|
List<String> email = new ArrayList<String>();
|
2013-06-25 16:47:47 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
Uri result = intent.getData();
|
|
|
|
String displayName = null;
|
|
|
|
|
|
|
|
// Get the contact id from the Uri
|
|
|
|
String id = result.getLastPathSegment();
|
|
|
|
|
|
|
|
cursor = mContentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION,
|
|
|
|
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?", new String[] { id }, null);
|
|
|
|
|
|
|
|
if (cursor != null) {
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
String address = cursor.getString(EMAIL_INDEX);
|
|
|
|
if (address != null) {
|
|
|
|
email.add(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (displayName == null) {
|
|
|
|
displayName = cursor.getString(NAME_INDEX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return 'null' if no email addresses have been found
|
|
|
|
if (email.size() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the first email address found as display name
|
|
|
|
if (displayName == null) {
|
|
|
|
displayName = email.get(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ContactItem(displayName, email);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Failed to get email data", e);
|
|
|
|
} finally {
|
|
|
|
Utility.closeQuietly(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2011-03-22 03:06:11 -04:00
|
|
|
|
2013-02-07 14:01:49 -05:00
|
|
|
/**
|
|
|
|
* Get URI to the picture of the contact with the supplied email address.
|
|
|
|
*
|
|
|
|
* @param address
|
|
|
|
* An email address. The contact database is searched for a contact with this email
|
|
|
|
* address.
|
|
|
|
*
|
|
|
|
* @return URI to the picture of the contact with the supplied email address. {@code null} if
|
|
|
|
* no such contact could be found or the contact doesn't have a picture.
|
|
|
|
*/
|
2013-06-25 16:47:47 -04:00
|
|
|
public Uri getPhotoUri(String address) {
|
|
|
|
Long contactId;
|
|
|
|
try {
|
|
|
|
final Cursor c = getContactByAddress(address);
|
|
|
|
if (c == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!c.moveToFirst()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
contactId = c.getLong(CONTACT_ID_INDEX);
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Cursor cur = mContentResolver.query(
|
|
|
|
ContactsContract.Data.CONTENT_URI,
|
|
|
|
null,
|
|
|
|
ContactsContract.Data.CONTACT_ID + "=" + contactId + " AND "
|
|
|
|
+ ContactsContract.Data.MIMETYPE + "='"
|
|
|
|
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
|
|
|
|
null);
|
|
|
|
if (cur == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!cur.moveToFirst()) {
|
|
|
|
cur.close();
|
|
|
|
return null; // no photo
|
|
|
|
}
|
|
|
|
// Ok, they have a photo
|
|
|
|
cur.close();
|
|
|
|
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
|
|
|
|
return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Couldn't fetch photo for contact with email " + address, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2013-02-07 14:01:49 -05:00
|
|
|
|
2011-03-23 16:51:06 -04:00
|
|
|
/**
|
|
|
|
* Does the device actually have a Contacts application suitable for
|
|
|
|
* picking a contact. As hard as it is to believe, some vendors ship
|
|
|
|
* without it.
|
|
|
|
*
|
|
|
|
* @return True, if the device supports picking contacts. False, otherwise.
|
2011-03-22 03:06:11 -04:00
|
|
|
*/
|
|
|
|
public boolean hasContactPicker() {
|
2011-03-24 00:11:03 -04:00
|
|
|
if (mHasContactPicker == null) {
|
2011-10-06 12:28:14 -04:00
|
|
|
mHasContactPicker = !(mContext.getPackageManager().
|
|
|
|
queryIntentActivities(contactPickerIntent(), 0).isEmpty());
|
2011-03-22 03:06:11 -04:00
|
|
|
}
|
2011-03-24 00:11:03 -04:00
|
|
|
return mHasContactPicker;
|
2011-03-22 03:06:11 -04:00
|
|
|
}
|
2013-06-25 16:47:47 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a {@link Cursor} instance that can be used to fetch information
|
|
|
|
* about the contact with the given email address.
|
|
|
|
*
|
|
|
|
* @param address The email address to search for.
|
|
|
|
* @return A {@link Cursor} instance that can be used to fetch information
|
|
|
|
* about the contact with the given email address
|
|
|
|
*/
|
|
|
|
private Cursor getContactByAddress(final String address) {
|
|
|
|
final Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI, Uri.encode(address));
|
|
|
|
final Cursor c = mContentResolver.query(
|
|
|
|
uri,
|
|
|
|
PROJECTION,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
SORT_ORDER);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2010-08-14 09:59:33 -04:00
|
|
|
}
|