android.content.ContentResolver#QUERY_ARG_OFFSET源码实例Demo

下面列出了android.content.ContentResolver#QUERY_ARG_OFFSET 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: storage-samples   文件: ImageProvider.java
@Override
public Cursor query(Uri uri, String[] projection, Bundle queryArgs,
        CancellationSignal cancellationSignal) {
    int match = sUriMatcher.match(uri);
    // We only support a query for multiple images, return null for other form of queries
    // including a query for a single image.
    switch (match) {
        case IMAGES:
            break;
        default:
            return null;
    }
    MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));

    File[] files = mBaseDir.listFiles();
    int offset = queryArgs.getInt(ContentResolver.QUERY_ARG_OFFSET, 0);
    int limit = queryArgs.getInt(ContentResolver.QUERY_ARG_LIMIT, Integer.MAX_VALUE);
    Log.d(TAG, "queryChildDocuments with Bundle, Uri: " +
            uri + ", offset: " + offset + ", limit: " + limit);
    if (offset < 0) {
        throw new IllegalArgumentException("Offset must not be less than 0");
    }
    if (limit < 0) {
        throw new IllegalArgumentException("Limit must not be less than 0");
    }

    if (offset >= files.length) {
        return result;
    }

    for (int i = offset, maxIndex = Math.min(offset + limit, files.length); i < maxIndex; i++) {
        includeFile(result, files[i]);
    }

    Bundle bundle = new Bundle();
    bundle.putInt(ContentResolver.EXTRA_SIZE, files.length);
    String[] honoredArgs = new String[2];
    int size = 0;
    if (queryArgs.containsKey(ContentResolver.QUERY_ARG_OFFSET)) {
        honoredArgs[size++] = ContentResolver.QUERY_ARG_OFFSET;
    }
    if (queryArgs.containsKey(ContentResolver.QUERY_ARG_LIMIT)) {
        honoredArgs[size++] = ContentResolver.QUERY_ARG_LIMIT;
    }
    if (size != honoredArgs.length) {
        honoredArgs = Arrays.copyOf(honoredArgs, size);
    }
    bundle.putStringArray(ContentResolver.EXTRA_HONORED_ARGS, honoredArgs);
    result.setExtras(bundle);
    return result;
}
 
@Override
public Cursor query(Uri uri, String[] projection, Bundle queryArgs,
        CancellationSignal cancellationSignal) {
    int match = sUriMatcher.match(uri);
    // We only support a query for multiple images, return null for other form of queries
    // including a query for a single image.
    switch (match) {
        case IMAGES:
            break;
        default:
            return null;
    }
    MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));

    File[] files = mBaseDir.listFiles();
    int offset = queryArgs.getInt(ContentResolver.QUERY_ARG_OFFSET, 0);
    int limit = queryArgs.getInt(ContentResolver.QUERY_ARG_LIMIT, Integer.MAX_VALUE);
    Log.d(TAG, "queryChildDocuments with Bundle, Uri: " +
            uri + ", offset: " + offset + ", limit: " + limit);
    if (offset < 0) {
        throw new IllegalArgumentException("Offset must not be less than 0");
    }
    if (limit < 0) {
        throw new IllegalArgumentException("Limit must not be less than 0");
    }

    if (offset >= files.length) {
        return result;
    }

    for (int i = offset, maxIndex = Math.min(offset + limit, files.length); i < maxIndex; i++) {
        includeFile(result, files[i]);
    }

    Bundle bundle = new Bundle();
    bundle.putInt(ContentResolver.EXTRA_TOTAL_SIZE, files.length);
    String[] honoredArgs = new String[2];
    int size = 0;
    if (queryArgs.containsKey(ContentResolver.QUERY_ARG_OFFSET)) {
        honoredArgs[size++] = ContentResolver.QUERY_ARG_OFFSET;
    }
    if (queryArgs.containsKey(ContentResolver.QUERY_ARG_LIMIT)) {
        honoredArgs[size++] = ContentResolver.QUERY_ARG_LIMIT;
    }
    if (size != honoredArgs.length) {
        honoredArgs = Arrays.copyOf(honoredArgs, size);
    }
    bundle.putStringArray(ContentResolver.EXTRA_HONORED_ARGS, honoredArgs);
    result.setExtras(bundle);
    return result;
}