2010-08-14 09:59:33 -04:00
|
|
|
package com.fsck.k9.helper;
|
|
|
|
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.os.Build;
|
2011-03-22 03:06:11 -04:00
|
|
|
import android.content.Intent;
|
2010-08-14 09:59:33 -04:00
|
|
|
import android.util.Log;
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.mail.Address;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class to access the contacts stored on the device. This is needed
|
|
|
|
* because the original contacts API introduced with SDK 1 was deprecated with
|
|
|
|
* SDK 5 and will eventually be removed in newer SDK versions.
|
|
|
|
* A class that uses the latest contacts API available on the device will be
|
|
|
|
* loaded at runtime.
|
|
|
|
*
|
|
|
|
* @see ContactsSdk3_4
|
|
|
|
* @see ContactsSdk5
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public abstract class Contacts {
|
2010-08-14 09:59:33 -04:00
|
|
|
/**
|
|
|
|
* Instance of the SDK specific class that interfaces with the contacts
|
|
|
|
* API.
|
|
|
|
*/
|
|
|
|
private static Contacts sInstance = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get SDK specific instance of the Contacts class.
|
|
|
|
*
|
|
|
|
* @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) {
|
|
|
|
if (sInstance == null) {
|
2010-08-14 09:59:33 -04:00
|
|
|
/*
|
|
|
|
* Check the version of the SDK we are running on. Choose an
|
|
|
|
* implementation class designed for that version of the SDK.
|
|
|
|
*/
|
|
|
|
int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
|
|
|
|
|
|
|
|
String className = null;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (sdkVersion <= Build.VERSION_CODES.DONUT) {
|
2010-08-14 09:59:33 -04:00
|
|
|
className = "com.fsck.k9.helper.ContactsSdk3_4";
|
2011-02-06 17:09:48 -05:00
|
|
|
} else if (sdkVersion <= Build.VERSION_CODES.ECLAIR_MR1) {
|
2011-01-24 21:34:02 -05:00
|
|
|
/*
|
|
|
|
* The new API was introduced with SDK 5. But Android versions < 2.2
|
|
|
|
* need some additional code to be able to search for phonetic names.
|
|
|
|
*/
|
|
|
|
className = "com.fsck.k9.helper.ContactsSdk5p";
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2010-08-14 09:59:33 -04:00
|
|
|
className = "com.fsck.k9.helper.ContactsSdk5";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the required class by name and instantiate it.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
try {
|
|
|
|
Class <? extends Contacts > clazz =
|
2010-08-14 09:59:33 -04:00
|
|
|
Class.forName(className).asSubclass(Contacts.class);
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
Constructor <? extends Contacts > constructor = clazz.getConstructor(Context.class);
|
2010-08-14 09:59:33 -04:00
|
|
|
sInstance = constructor.newInstance(context);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (ClassNotFoundException e) {
|
2010-08-14 09:59:33 -04:00
|
|
|
Log.e(K9.LOG_TAG, "Couldn't find class: " + className, e);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (InstantiationException e) {
|
2010-08-14 09:59:33 -04:00
|
|
|
Log.e(K9.LOG_TAG, "Couldn't instantiate class: " + className, e);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (IllegalAccessException e) {
|
2010-08-14 09:59:33 -04:00
|
|
|
Log.e(K9.LOG_TAG, "Couldn't access class: " + className, e);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (NoSuchMethodException e) {
|
2010-08-14 09:59:33 -04:00
|
|
|
Log.e(K9.LOG_TAG, "Couldn't find constructor of class: " + className, e);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (IllegalArgumentException e) {
|
2010-08-14 09:59:33 -04:00
|
|
|
Log.e(K9.LOG_TAG, "Wrong arguments for constructor of class: " + className, e);
|
2011-02-06 17:09:48 -05:00
|
|
|
} catch (InvocationTargetException e) {
|
2010-08-14 09:59:33 -04:00
|
|
|
Log.e(K9.LOG_TAG, "Couldn't invoke constructor of class: " + className, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected Context mContext;
|
|
|
|
protected ContentResolver mContentResolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the device's owner.
|
|
|
|
*
|
|
|
|
* @return The name of the owner if available. <tt>null</tt>, otherwise.
|
|
|
|
*/
|
|
|
|
public abstract String getOwnerName();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-01-10 12:47:23 -05:00
|
|
|
public abstract void createContact(Address email);
|
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.
|
|
|
|
*/
|
|
|
|
public abstract boolean isInContacts(String emailAddress);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the contacts matching the given search term.
|
|
|
|
*
|
|
|
|
* @param filter The search term to filter the contacts.
|
|
|
|
* @return A {@link Cursor} instance that can be used to get the
|
|
|
|
* matching contacts.
|
|
|
|
*/
|
|
|
|
public abstract Cursor searchContacts(CharSequence filter);
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public abstract String getNameForAddress(String address);
|
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.
|
|
|
|
*/
|
|
|
|
public abstract String getName(Cursor cursor);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public abstract String getEmail(Cursor cursor);
|
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.
|
|
|
|
*/
|
|
|
|
public abstract void markAsContacted(final Address[] addresses);
|
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
|
|
|
*/
|
|
|
|
public abstract Intent contactPickerIntent();
|
|
|
|
|
2011-03-23 16:51:06 -04:00
|
|
|
/**
|
|
|
|
* Given a contact picker intent, returns the primary email address of that
|
|
|
|
* contact.
|
2011-03-22 03:06:11 -04:00
|
|
|
*
|
2011-03-23 16:51:06 -04:00
|
|
|
* @param intent The {@link Intent} returned by this contact picker.
|
|
|
|
* @return The primary email address of the picked contact.
|
2011-03-22 03:06:11 -04:00
|
|
|
*/
|
|
|
|
public abstract String getEmailFromContactPicker(final Intent intent);
|
|
|
|
|
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() {
|
|
|
|
if (mContext.getPackageManager().queryIntentActivities(contactPickerIntent(), 0).size() > 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-08-14 09:59:33 -04:00
|
|
|
}
|