has Contact Data - Android Account

Android examples for Account:Contact

Description

has Contact Data

Demo Code

/**//from   w w w.j  av a  2s.  c  om
 * Property of Matt Allen
 * mattallen092@gmail.com
 * http://mattallensoftware.co.uk/
 *
 * This software is distributed under the Apache v2.0 license and use
 * of the Repay name may not be used without explicit permission from the project owner.
 *
 */
import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import java.util.HashMap;

public class Main{
    private static final String TAG = ContactsContractHelper.class
            .getName();
    private static int[] typesPhone = new int[] {
            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
            ContactsContract.CommonDataKinds.Phone.TYPE_MAIN,
            ContactsContract.CommonDataKinds.Phone.TYPE_HOME,
            ContactsContract.CommonDataKinds.Phone.TYPE_WORK };
    private static int[] typesEmail = new int[] {
            ContactsContract.CommonDataKinds.Phone.TYPE_HOME,
            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
            ContactsContract.CommonDataKinds.Phone.TYPE_WORK };
    public static boolean hasContactData(Context c, String contactID) {
        boolean hasData = false;
        for (int i = 0; i <= typesPhone.length - 1; i++) {
            String[] whereArgs = new String[] { String.valueOf(contactID),
                    String.valueOf(typesPhone[i]) };
            String phoneNum = queryContactForPhoneNum(c, whereArgs);
            if (phoneNum != null) {
                hasData = true;
                break;
            }
        }
        if (!hasData) {
            for (int i = 0; i <= typesEmail.length - 1; i++) {
                String[] whereArgs = new String[] {
                        String.valueOf(contactID),
                        String.valueOf(typesEmail[i]) };
                String email = queryContactForEmail(c, whereArgs);
                if (email != null) {
                    hasData = true;
                    break;
                }
            }
        }
        return hasData;
    }
    private static String queryContactForPhoneNum(Context c,
            String[] whereArgs) {
        String phoneNumber = null;
        Cursor cursor = c.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                        + " = ? and "
                        + ContactsContract.CommonDataKinds.Phone.TYPE
                        + " = ?", whereArgs, null);

        int phoneNumberIndex = cursor
                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            phoneNumber = cursor.getString(phoneNumberIndex);
            cursor.close();
        }
        Log.i(TAG, "Returning phone number: " + phoneNumber);
        return phoneNumber;
    }
    private static String queryContactForEmail(Context c, String[] whereArgs) {
        String phoneNumber = null;
        Cursor cursor = c.getContentResolver().query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID
                        + " = ? and "
                        + ContactsContract.CommonDataKinds.Email.TYPE
                        + " = ?", whereArgs, null);

        int phoneNumberIndex = cursor
                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.ADDRESS);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            phoneNumber = cursor.getString(phoneNumberIndex);
            cursor.close();
        }
        Log.i(TAG, "Returning email address: " + phoneNumber);
        return phoneNumber;
    }
}

Related Tutorials