2010-08-14 09:59:33 -04:00
|
|
|
package com.fsck.k9.helper;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2011-03-22 03:06:11 -04:00
|
|
|
import android.util.Log;
|
2010-08-14 09:59:33 -04:00
|
|
|
import android.provider.Contacts;
|
2011-03-22 03:06:11 -04:00
|
|
|
import android.provider.Contacts.ContactMethods;
|
2010-08-14 09:59:33 -04:00
|
|
|
import com.fsck.k9.mail.Address;
|
2011-03-22 03:06:11 -04:00
|
|
|
import com.fsck.k9.K9;
|
|
|
|
|
2010-08-14 09:59:33 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Access the contacts on the device using the old API (introduced in SDK 1).
|
|
|
|
*
|
|
|
|
* @see android.provider.Contacts
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("deprecation")
|
2011-02-06 17:09:48 -05:00
|
|
|
public class ContactsSdk3_4 extends com.fsck.k9.helper.Contacts {
|
2010-08-14 09:59:33 -04:00
|
|
|
/**
|
|
|
|
* The order in which the search results are returned by
|
|
|
|
* {@link #searchContacts(CharSequence)}.
|
|
|
|
*/
|
|
|
|
private static final String SORT_ORDER =
|
2010-10-30 16:35:49 -04:00
|
|
|
Contacts.ContactMethods.TIMES_CONTACTED + " DESC, " +
|
|
|
|
Contacts.ContactMethods.DISPLAY_NAME + ", " +
|
|
|
|
Contacts.ContactMethods._ID;
|
2010-08-14 09:59:33 -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}.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private static final String PROJECTION[] = {
|
2010-10-30 16:35:49 -04:00
|
|
|
Contacts.ContactMethods._ID,
|
|
|
|
Contacts.ContactMethods.DISPLAY_NAME,
|
|
|
|
Contacts.ContactMethods.DATA,
|
|
|
|
Contacts.ContactMethods.PERSON_ID
|
2010-08-17 22:48:55 -04:00
|
|
|
};
|
2010-08-14 09:59:33 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Index of the name field in the projection. This must match the order in
|
|
|
|
* {@link #PROJECTION}.
|
|
|
|
*/
|
|
|
|
private static final int NAME_INDEX = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Index of the email address field in the projection. This must match the
|
|
|
|
* order in {@link #PROJECTION}.
|
|
|
|
*/
|
|
|
|
private static final int EMAIL_INDEX = 2;
|
|
|
|
|
2010-10-30 16:35:49 -04:00
|
|
|
/**
|
|
|
|
* Index of the contact id field in the projection. This must match the order in
|
|
|
|
* {@link #PROJECTION}.
|
|
|
|
*/
|
|
|
|
private static final int CONTACT_ID_INDEX = 3;
|
|
|
|
|
2010-08-14 09:59:33 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public ContactsSdk3_4(final Context context) {
|
2010-08-14 09:59:33 -04:00
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void createContact(final Address email) {
|
2010-08-14 09:59:33 -04:00
|
|
|
final Uri contactUri = Uri.fromParts("mailto", email.getAddress(), null);
|
|
|
|
|
|
|
|
final Intent contactIntent = new Intent(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
|
2011-02-25 13:15:13 -05:00
|
|
|
contactIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2010-08-14 09:59:33 -04:00
|
|
|
contactIntent.setData(contactUri);
|
|
|
|
|
|
|
|
// Pass along full E-mail string for possible create dialog
|
|
|
|
contactIntent.putExtra(Contacts.Intents.EXTRA_CREATE_DESCRIPTION,
|
2010-08-17 22:48:55 -04:00
|
|
|
email.toString());
|
2010-08-14 09:59:33 -04:00
|
|
|
|
|
|
|
// Only provide personal name hint if we have one
|
|
|
|
final String senderPersonal = email.getPersonal();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (senderPersonal != null) {
|
2010-08-14 09:59:33 -04:00
|
|
|
contactIntent.putExtra(Contacts.Intents.Insert.NAME, senderPersonal);
|
|
|
|
}
|
|
|
|
|
2011-01-10 12:47:23 -05:00
|
|
|
mContext.startActivity(contactIntent);
|
2010-08-14 09:59:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getOwnerName() {
|
2010-08-14 09:59:33 -04:00
|
|
|
String name = null;
|
|
|
|
final Cursor c = mContentResolver.query(
|
2010-08-17 22:48:55 -04:00
|
|
|
Uri.withAppendedPath(Contacts.People.CONTENT_URI, "owner"),
|
2011-01-24 21:29:31 -05:00
|
|
|
new String[] {Contacts.ContactMethods.DISPLAY_NAME},
|
2010-08-17 22:48:55 -04:00
|
|
|
null,
|
|
|
|
null,
|
|
|
|
null);
|
2010-08-14 09:59:33 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (c != null) {
|
|
|
|
if (c.getCount() > 0) {
|
2010-08-14 09:59:33 -04:00
|
|
|
c.moveToFirst();
|
2011-01-24 21:29:31 -05:00
|
|
|
name = c.getString(0); // owner's display name
|
2010-08-14 09:59:33 -04:00
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean isInContacts(final String emailAddress) {
|
2010-08-14 09:59:33 -04:00
|
|
|
boolean result = false;
|
|
|
|
|
2010-10-30 16:35:49 -04:00
|
|
|
final Cursor c = getContactByAddress(emailAddress);
|
2010-08-14 09:59:33 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (c != null) {
|
|
|
|
if (c.getCount() > 0) {
|
2010-08-14 09:59:33 -04:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public Cursor searchContacts(final CharSequence constraint) {
|
2010-08-14 09:59:33 -04:00
|
|
|
final String where;
|
|
|
|
final String[] args;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (constraint == null) {
|
2011-01-24 20:32:31 -05:00
|
|
|
where = Contacts.ContactMethods.KIND + " = " + Contacts.KIND_EMAIL;
|
2010-08-14 09:59:33 -04:00
|
|
|
args = null;
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-10-30 16:35:49 -04:00
|
|
|
where = Contacts.ContactMethods.KIND + " = " + Contacts.KIND_EMAIL +
|
2011-01-24 20:32:31 -05:00
|
|
|
" AND " +
|
|
|
|
"(" +
|
|
|
|
// Match if name starts with "constraint"
|
2010-08-17 22:48:55 -04:00
|
|
|
Contacts.People.NAME + " LIKE ?" +
|
2011-01-24 20:32:31 -05:00
|
|
|
" OR " +
|
|
|
|
// Match if name contains a word that starts with "constraint"
|
2010-10-30 16:53:56 -04:00
|
|
|
Contacts.People.NAME + " LIKE ?" +
|
2011-01-24 20:32:31 -05:00
|
|
|
" OR " +
|
|
|
|
// Match if phonetic name starts with "constraint"
|
2011-01-04 09:33:02 -05:00
|
|
|
Contacts.People.PHONETIC_NAME + " LIKE ?" +
|
2011-01-24 20:32:31 -05:00
|
|
|
" OR " +
|
|
|
|
// Match if phonetic name contains a word that starts with "constraint"
|
2011-01-04 09:33:02 -05:00
|
|
|
Contacts.People.PHONETIC_NAME + " LIKE ?" +
|
2011-01-24 20:32:31 -05:00
|
|
|
" OR " +
|
|
|
|
// Match if email address starts with "constraint"
|
2010-08-17 22:48:55 -04:00
|
|
|
Contacts.ContactMethods.DATA + " LIKE ?" +
|
|
|
|
")";
|
2010-08-14 09:59:33 -04:00
|
|
|
final String filter = constraint.toString() + "%";
|
2010-10-30 16:53:56 -04:00
|
|
|
final String filter2 = "% " + filter;
|
2011-01-04 09:33:02 -05:00
|
|
|
args = new String[] {filter, filter2, filter, filter2, filter};
|
2010-08-14 09:59:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
final Cursor c = mContentResolver.query(
|
2010-10-30 16:35:49 -04:00
|
|
|
Contacts.ContactMethods.CONTENT_URI,
|
2010-08-17 22:48:55 -04:00
|
|
|
PROJECTION,
|
|
|
|
where,
|
|
|
|
args,
|
|
|
|
SORT_ORDER);
|
2010-08-14 09:59:33 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (c != null) {
|
2010-08-14 09:59:33 -04:00
|
|
|
/*
|
|
|
|
* 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-30 10:37:34 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getNameForAddress(String address) {
|
|
|
|
if (address == null) {
|
2010-10-08 20:24:43 -04:00
|
|
|
return null;
|
2010-08-30 10:37:34 -04:00
|
|
|
}
|
|
|
|
|
2010-10-30 16:35:49 -04:00
|
|
|
final Cursor c = getContactByAddress(address);
|
2010-08-30 10:37:34 -04:00
|
|
|
|
2010-10-08 20:24:43 -04:00
|
|
|
String name = null;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (c != null) {
|
|
|
|
if (c.getCount() > 0) {
|
2010-10-08 20:24:43 -04:00
|
|
|
c.moveToFirst();
|
|
|
|
name = getName(c);
|
|
|
|
}
|
|
|
|
c.close();
|
2010-08-30 10:37:34 -04:00
|
|
|
}
|
|
|
|
|
2010-10-08 20:24:43 -04:00
|
|
|
return name;
|
2010-08-30 10:37:34 -04:00
|
|
|
}
|
|
|
|
|
2010-08-14 09:59:33 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getName(Cursor c) {
|
2010-08-14 09:59:33 -04:00
|
|
|
return c.getString(NAME_INDEX);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getEmail(Cursor c) {
|
2010-08-14 09:59:33 -04:00
|
|
|
return c.getString(EMAIL_INDEX);
|
|
|
|
}
|
2010-10-30 16:35:49 -04:00
|
|
|
|
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public void markAsContacted(final Address[] addresses) {
|
2010-10-30 16:35:49 -04:00
|
|
|
//TODO: Optimize! Potentially a lot of database queries
|
2011-02-06 17:09:48 -05:00
|
|
|
for (final Address address : addresses) {
|
2010-10-30 16:35:49 -04:00
|
|
|
final Cursor c = getContactByAddress(address.getAddress());
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (c != null) {
|
|
|
|
if (c.getCount() > 0) {
|
2010-10-30 16:35:49 -04:00
|
|
|
c.moveToFirst();
|
|
|
|
final long personId = c.getLong(CONTACT_ID_INDEX);
|
|
|
|
Contacts.People.markAsContacted(mContentResolver, personId);
|
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 03:06:11 -04:00
|
|
|
@Override
|
|
|
|
public Intent contactPickerIntent() {
|
|
|
|
return new Intent(Intent.ACTION_PICK, Contacts.People.CONTENT_URI);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getEmailFromContactPicker(final Intent data) {
|
|
|
|
|
|
|
|
Cursor cursor = null;
|
|
|
|
Cursor cursor2 = null;
|
|
|
|
String email = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Uri result = data.getData();
|
|
|
|
cursor = mContentResolver.query(result, null, null, null, null);
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
String emailId = cursor.getString(cursor.getColumnIndex(Contacts.People.PRIMARY_EMAIL_ID));
|
|
|
|
cursor2 = mContext.getContentResolver().query(ContactMethods.CONTENT_EMAIL_URI, new String[] { ContactMethods.DATA }, "contact_methods._id=?", new String[] { emailId }, null);
|
|
|
|
if (cursor2.moveToFirst()) {
|
|
|
|
email = cursor2.getString(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Failed to get email data", e);
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) {
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
if (cursor2 != null) {
|
|
|
|
cursor2.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return email;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-30 16:35:49 -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
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private Cursor getContactByAddress(String address) {
|
2010-10-30 16:35:49 -04:00
|
|
|
final String where = Contacts.ContactMethods.KIND + " = " + Contacts.KIND_EMAIL +
|
|
|
|
" AND " +
|
|
|
|
Contacts.ContactMethods.DATA + " = ?";
|
|
|
|
final String[] args = new String[] {address};
|
|
|
|
|
|
|
|
final Cursor c = mContentResolver.query(
|
|
|
|
Contacts.ContactMethods.CONTENT_URI,
|
|
|
|
PROJECTION,
|
|
|
|
where,
|
|
|
|
args,
|
|
|
|
SORT_ORDER);
|
|
|
|
return c;
|
|
|
|
}
|
2010-08-14 09:59:33 -04:00
|
|
|
}
|