List of usage examples for android.database MatrixCursor addRow
public void addRow(Iterable<?> columnValues)
From source file:com.android.messaging.datamodel.FrequentContactsCursorBuilder.java
/** * Attempt to build the cursor records from the frequent and all contacts cursor if they * are both ready to be consumed.//from w w w. j a v a 2 s . c o m * @return the frequent contact cursor if built successfully, or null if it can't be built yet. */ public Cursor build() { if (mFrequentContactsCursor != null && mAllContactsCursor != null) { Assert.isTrue(!mFrequentContactsCursor.isClosed()); Assert.isTrue(!mAllContactsCursor.isClosed()); // Frequent contacts cursor has one record per contact, plus it doesn't contain info // such as phone number and type. In order for the records to be usable by Bugle, we // would like to populate it with information from the all contacts cursor. final MatrixCursor retCursor = new MatrixCursor(ContactUtil.PhoneQuery.PROJECTION); // First, go through the frequents cursor and take note of all lookup keys and their // corresponding rank in the frequents list. final SimpleArrayMap<String, Integer> lookupKeyToRankMap = new SimpleArrayMap<String, Integer>(); int oldPosition = mFrequentContactsCursor.getPosition(); int rank = 0; mFrequentContactsCursor.moveToPosition(-1); while (mFrequentContactsCursor.moveToNext()) { final String lookupKey = mFrequentContactsCursor.getString(ContactUtil.INDEX_LOOKUP_KEY_FREQUENT); lookupKeyToRankMap.put(lookupKey, rank++); } mFrequentContactsCursor.moveToPosition(oldPosition); // Second, go through the all contacts cursor once and retrieve all information // (multiple phone numbers etc.) and store that in an array list. Since the all // contacts list only contains phone contacts, this step will ensure that we filter // out any invalid/email contacts in the frequents list. final ArrayList<Object[]> rows = new ArrayList<Object[]>(mFrequentContactsCursor.getCount()); oldPosition = mAllContactsCursor.getPosition(); mAllContactsCursor.moveToPosition(-1); while (mAllContactsCursor.moveToNext()) { final String lookupKey = mAllContactsCursor.getString(ContactUtil.INDEX_LOOKUP_KEY); if (lookupKeyToRankMap.containsKey(lookupKey)) { final Object[] row = new Object[ContactUtil.PhoneQuery.PROJECTION.length]; row[ContactUtil.INDEX_DATA_ID] = mAllContactsCursor.getLong(ContactUtil.INDEX_DATA_ID); row[ContactUtil.INDEX_CONTACT_ID] = mAllContactsCursor.getLong(ContactUtil.INDEX_CONTACT_ID); row[ContactUtil.INDEX_LOOKUP_KEY] = mAllContactsCursor.getString(ContactUtil.INDEX_LOOKUP_KEY); row[ContactUtil.INDEX_DISPLAY_NAME] = mAllContactsCursor .getString(ContactUtil.INDEX_DISPLAY_NAME); row[ContactUtil.INDEX_PHOTO_URI] = mAllContactsCursor.getString(ContactUtil.INDEX_PHOTO_URI); row[ContactUtil.INDEX_PHONE_EMAIL] = mAllContactsCursor .getString(ContactUtil.INDEX_PHONE_EMAIL); row[ContactUtil.INDEX_PHONE_EMAIL_TYPE] = mAllContactsCursor .getInt(ContactUtil.INDEX_PHONE_EMAIL_TYPE); row[ContactUtil.INDEX_PHONE_EMAIL_LABEL] = mAllContactsCursor .getString(ContactUtil.INDEX_PHONE_EMAIL_LABEL); rows.add(row); } } mAllContactsCursor.moveToPosition(oldPosition); // Now we have a list of rows containing frequent contacts in alphabetical order. // Therefore, sort all the rows according to their actual ranks in the frequents list. Collections.sort(rows, new Comparator<Object[]>() { @Override public int compare(final Object[] lhs, final Object[] rhs) { final String lookupKeyLhs = (String) lhs[ContactUtil.INDEX_LOOKUP_KEY]; final String lookupKeyRhs = (String) rhs[ContactUtil.INDEX_LOOKUP_KEY]; Assert.isTrue(lookupKeyToRankMap.containsKey(lookupKeyLhs) && lookupKeyToRankMap.containsKey(lookupKeyRhs)); final int rankLhs = lookupKeyToRankMap.get(lookupKeyLhs); final int rankRhs = lookupKeyToRankMap.get(lookupKeyRhs); if (rankLhs < rankRhs) { return -1; } else if (rankLhs > rankRhs) { return 1; } else { // Same rank, so it's two contact records for the same contact. // Perform secondary sorting on the phone type. Always place // mobile before everything else. final int phoneTypeLhs = (int) lhs[ContactUtil.INDEX_PHONE_EMAIL_TYPE]; final int phoneTypeRhs = (int) rhs[ContactUtil.INDEX_PHONE_EMAIL_TYPE]; if (phoneTypeLhs == Phone.TYPE_MOBILE && phoneTypeRhs == Phone.TYPE_MOBILE) { return 0; } else if (phoneTypeLhs == Phone.TYPE_MOBILE) { return -1; } else if (phoneTypeRhs == Phone.TYPE_MOBILE) { return 1; } else { // Use the default sort order, i.e. sort by phoneType value. return phoneTypeLhs < phoneTypeRhs ? -1 : (phoneTypeLhs == phoneTypeRhs ? 0 : 1); } } } }); // Finally, add all the rows to this cursor. for (final Object[] row : rows) { retCursor.addRow(row); } return retCursor; } return null; }