override fun query()

in ContentProviderPagingKotlin/app/src/main/kotlin/com.example.android.contentproviderpaging/ImageProvider.kt [53:89]


    override fun query(uri: Uri, projection: Array<String>?, queryArgs: Bundle,
                       cancellationSignal: CancellationSignal?): Cursor? {
        val 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.
        when (match) {
            IMAGES -> {
            }
            else -> return null
        }
        val result = MatrixCursor(resolveDocumentProjection(projection))

        val files = mBaseDir!!.listFiles()
        val offset = queryArgs.getInt(ContentResolver.QUERY_ARG_OFFSET, 0)
        val 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 IllegalArgumentException("Offset must not be less than 0")
        }
        if (limit < 0) {
            throw IllegalArgumentException("Limit must not be less than 0")
        }

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

        val maxIndex = Math.min(offset + limit, files.size)
        for (i in offset until maxIndex) {
            includeFile(result, files[i])
        }

        val bundle = constructExtras(queryArgs, files)
        result.extras = bundle
        return result
    }