public Cursor query()

in ContentProviderPaging/app/src/main/java/com/example/android/contentproviderpaging/ImageProvider.java [87:136]


    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;
    }