private static Cursor getCursorForConstruction(Context context, long contactId, int queryType) { final Cursor cursor; if (queryType == QUERY_TYPE_EMAIL) { cursor = context .getContentResolver() .query( Queries.EMAIL.getContentUri(), Queries.EMAIL.getProjection(), Queries.EMAIL.getProjection()[Queries.Query.CONTACT_ID] + " =?", new String[] {String.valueOf(contactId)}, null); } else { cursor = context .getContentResolver() .query( Queries.PHONE.getContentUri(), Queries.PHONE.getProjection(), Queries.PHONE.getProjection()[Queries.Query.CONTACT_ID] + " =?", new String[] {String.valueOf(contactId)}, null); } return removeDuplicateDestinations(cursor); }
private static Cursor getCursorForConstruction( Context context, long contactId, Long directoryId, String lookupKey, int queryType) { final Cursor cursor; final String desiredMimeType; if (queryType == QUERY_TYPE_EMAIL) { final Uri uri; final StringBuilder selection = new StringBuilder(); selection.append(Queries.EMAIL.getProjection()[Queries.Query.CONTACT_ID]); selection.append(" = ?"); if (directoryId == null || lookupKey == null) { uri = Queries.EMAIL.getContentUri(); desiredMimeType = null; } else { final Uri.Builder builder = Contacts.getLookupUri(contactId, lookupKey).buildUpon(); builder .appendPath(Contacts.Entity.CONTENT_DIRECTORY) .appendQueryParameter( ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId)); uri = builder.build(); desiredMimeType = ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE; } cursor = context .getContentResolver() .query( uri, Queries.EMAIL.getProjection(), selection.toString(), new String[] {String.valueOf(contactId)}, null); } else { final Uri uri; final StringBuilder selection = new StringBuilder(); selection.append(Queries.PHONE.getProjection()[Queries.Query.CONTACT_ID]); selection.append(" = ?"); if (lookupKey == null) { uri = Queries.PHONE.getContentUri(); desiredMimeType = null; } else { final Uri.Builder builder = Contacts.getLookupUri(contactId, lookupKey).buildUpon(); builder .appendPath(Contacts.Entity.CONTENT_DIRECTORY) .appendQueryParameter( ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId)); uri = builder.build(); desiredMimeType = ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE; } cursor = context .getContentResolver() .query( uri, Queries.PHONE.getProjection(), selection.toString(), new String[] {String.valueOf(contactId)}, null); } final Cursor resultCursor = removeUndesiredDestinations(cursor, desiredMimeType, lookupKey); cursor.close(); return resultCursor; }