隐式联接列如何处理 Android 联系人数据?

How do implicit joined columns work with Android contacts data?

我正在查询 ContactsContract.Data table 以查找 phone 条记录。

我在创建新文件时遇到错误 CursorLoader:

java.lang.IllegalArgumentException: Invalid column deleted

我的代码:

import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;

...

String[] projection = {
    Phone.DELETED,
    Phone.LOOKUP_KEY,
    Phone.NUMBER,
    Phone.TYPE,
    Phone.LABEL,
    Data.MIMETYPE,
    Data.DISPLAY_NAME_PRIMARY
};

// "mimetype = ? AND deleted = ?"
String selection = Data.MIMETYPE + " = ? AND " Phone.DELETED + " = ?";  
String[] args = {Phone.CONTENT_ITEM_TYPE, "0"};

return new CursorLoader(
    this,
    Data.CONTENT_URI,
    projection,
    selection,
    args,
    null);

知道为什么 Phone.DELETED 列没有包含在游标中吗? documentation 确实说 -

Some columns from the associated raw contact are also available through an implicit join.

您似乎发现了一个已在许多地方记录但尚未实​​现的功能。我打开了一个 bug 来跟踪这个问题 - 让我们看看 AOSP 的人对这个问题有什么看法 (bug report)。

同时,您可以使用以下解决方法:

Uri uri = ContactsContract.RawContactsEntity.CONTENT_URI;

String[] projection = {
    Phone._ID,
    Phone.DELETED,
    //Phone.LOOKUP_KEY,
    Phone.NUMBER,
    Phone.TYPE,
    Phone.LABEL,
    Data.MIMETYPE,
    Data.DISPLAY_NAME_PRIMARY
};

String selection = Data.MIMETYPE + " = ? AND " + Data.DELETED + " = ?";
String[] args = {
    Phone.CONTENT_ITEM_TYPE, "0"
};

return new CursorLoader(
this,
uri,
projection,
selection,
args,
null);

变化:

  1. 使用 RawContactsEntity's URI
  2. LOOKUP_KEY 无法通过上述 URI 访问 - 如果您确实需要此列,则必须执行其他查询
  3. 如果您要在 CursorAdapter 中使用结果 Cursor,则需要
  4. _ID 列。

编辑:按照@MichaelAlanHuff 的要求,我正在post编辑此答案所基于的部分代码

来自com.android.providers.contacts.ContactsProvider2#queryLocal()ContactsProvider2的源代码):

protected Cursor queryLocal(final Uri uri, final String[] projection, String selection,
String[] selectionArgs, String sortOrder, final long directoryId,
final CancellationSignal cancellationSignal) {


    final SQLiteDatabase db = mDbHelper.get().getReadableDatabase();

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    String groupBy = null;
    String having = null;
    String limit = getLimit(uri);
    boolean snippetDeferred = false;

    // The expression used in bundleLetterCountExtras() to get count.
    String addressBookIndexerCountExpression = null;

    final int match = sUriMatcher.match(uri);
    switch (match) {


        ...

        case DATA:
        case PROFILE_DATA:
            {
                final String usageType = uri.getQueryParameter(DataUsageFeedback.USAGE_TYPE);
                final int typeInt = getDataUsageFeedbackType(usageType, USAGE_TYPE_ALL);
                setTablesAndProjectionMapForData(qb, uri, projection, false, typeInt);
                if (uri.getBooleanQueryParameter(Data.VISIBLE_CONTACTS_ONLY, false)) {
                    qb.appendWhere(" AND " + Data.CONTACT_ID + " in " + Tables.DEFAULT_DIRECTORY);
                }
                break;
            }


            ...

    }



    qb.setStrict(true);

    // Auto-rewrite SORT_KEY_{PRIMARY, ALTERNATIVE} sort orders.
    String localizedSortOrder = getLocalizedSortOrder(sortOrder);
    Cursor cursor = query(db, qb, projection, selection, selectionArgs, localizedSortOrder, groupBy,
    having, limit, cancellationSignal);

    if (readBooleanQueryParameter(uri, Contacts.EXTRA_ADDRESS_BOOK_INDEX, false)) {
        bundleFastScrollingIndexExtras(cursor, uri, db, qb, selection,
        selectionArgs, sortOrder, addressBookIndexerCountExpression,
        cancellationSignal);
    }
    if (snippetDeferred) {
        cursor = addDeferredSnippetingExtra(cursor);
    }

    return cursor;


}

如您所见,可以更改用于构建查询的 SQLiteQueryBuilder 的其他两种方法:setTablesAndProjectionMapForData() 和其他 query() 方法。

com.android.providers.contacts.ContactsProvider2#setTablesAndProjectionMapForData() 来源:

private void setTablesAndProjectionMapForData(SQLiteQueryBuilder qb, Uri uri,
        String[] projection, boolean distinct, boolean addSipLookupColumns, Integer usageType) {
    StringBuilder sb = new StringBuilder();
    sb.append(Views.DATA);
    sb.append(" data");

    appendContactPresenceJoin(sb, projection, RawContacts.CONTACT_ID);
    appendContactStatusUpdateJoin(sb, projection, ContactsColumns.LAST_STATUS_UPDATE_ID);
    appendDataPresenceJoin(sb, projection, DataColumns.CONCRETE_ID);
    appendDataStatusUpdateJoin(sb, projection, DataColumns.CONCRETE_ID);

    appendDataUsageStatJoin(
            sb, usageType == null ? USAGE_TYPE_ALL : usageType, DataColumns.CONCRETE_ID);

    qb.setTables(sb.toString());

    boolean useDistinct = distinct || !ContactsDatabaseHelper.isInProjection(
            projection, DISTINCT_DATA_PROHIBITING_COLUMNS);
    qb.setDistinct(useDistinct);

    final ProjectionMap projectionMap;
    if (addSipLookupColumns) {
        projectionMap =
                useDistinct ? sDistinctDataSipLookupProjectionMap : sDataSipLookupProjectionMap;
    } else {
        projectionMap = useDistinct ? sDistinctDataProjectionMap : sDataProjectionMap;
    }

    qb.setProjectionMap(projectionMap);
    appendAccountIdFromParameter(qb, uri);
}

在这里您可以看到使用 StringBuilder 的最终查询的 table 参数的构造,它被传递给几个 append*() 方法。我不会 post 他们的源代码,但他们确实 join 出现在方法名称中的 table 。如果加入 rawContacts table,我希望在此处看到对 appendRawContactJoin() 之类的调用...

为了完整性:我提到的另一个 query() 方法没有修改 table 参数:

private Cursor query(final SQLiteDatabase db, SQLiteQueryBuilder qb, String[] projection,
        String selection, String[] selectionArgs, String sortOrder, String groupBy,
        String having, String limit, CancellationSignal cancellationSignal) {
    if (projection != null && projection.length == 1
            && BaseColumns._COUNT.equals(projection[0])) {
        qb.setProjectionMap(sCountProjectionMap);
    }
    final Cursor c = qb.query(db, projection, selection, selectionArgs, groupBy, having,
            sortOrder, limit, cancellationSignal);
    if (c != null) {
        c.setNotificationUri(getContext().getContentResolver(), ContactsContract.AUTHORITY_URI);
    }
    return c;
}

检查上述方法链后我得出结论,有一个官方记录的功能未实现。