blob: a113bd13cc21674760457097899396ec87fd05d0 [file] [log] [blame]
Steve McKayffc11082017-01-17 11:12:08 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.documentsui;
17
18import android.content.ContentResolver;
19import android.database.Cursor;
20import android.database.MatrixCursor;
21import android.os.Bundle;
22import android.provider.DocumentsContract.Root;
23
24import java.io.FileNotFoundException;
25
26/**
27 * Test provider w/ support for paging, and various sub-scenarios of paging.
28 */
29public class PagingProvider extends TestRootProvider {
30
31 /**
32 * Pass test result size to inform the provider of the result size. Defaults to 1k.
33 */
Steve McKay71e84b92017-04-24 12:32:55 -070034 private static final String TEST_RECORDSET_COUNT = "test-recordset-size";
35 private static final int DEFAULT_RECORDSET_COUNT = 100;
36 private static final int UNDETERMINED_RECORDSET_COUNT = -1;
Steve McKayffc11082017-01-17 11:12:08 -080037
38 private static final String ROOT_ID = "paging-root";
39 private static final String ROOT_DOC_ID = "root0";
40 private static final int ROOT_FLAGS = Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD;
41
42 public PagingProvider() {
43 super("Paging Root", ROOT_ID, ROOT_FLAGS, ROOT_DOC_ID);
44 }
45
46 @Override
47 public Cursor queryDocument(String documentId, String[] projection)
48 throws FileNotFoundException {
49 MatrixCursor c = createDocCursor(projection);
50 addFolder(c, documentId);
51 return c;
52 }
53
54 @Override
55 public Cursor queryChildDocuments(
56 String parentDocumentId, String[] projection, String sortOrder)
57 throws FileNotFoundException {
58 throw new UnsupportedOperationException();
59 }
60
61 @Override
62 public Cursor queryChildDocuments(
63 String parentDocumentId, String[] projection, Bundle queryArgs)
64 throws FileNotFoundException {
65
Steve McKay961f1ec2017-07-26 17:37:24 -070066 queryArgs = queryArgs != null ? queryArgs : Bundle.EMPTY;
Steve McKayffc11082017-01-17 11:12:08 -080067
68 MatrixCursor c = createDocCursor(projection);
69 Bundle extras = c.getExtras();
70
71 int offset = queryArgs.getInt(ContentResolver.QUERY_ARG_OFFSET, 0);
72 int limit = queryArgs.getInt(ContentResolver.QUERY_ARG_LIMIT, Integer.MIN_VALUE);
Steve McKay71e84b92017-04-24 12:32:55 -070073 int recordsetSize = queryArgs.getInt(TEST_RECORDSET_COUNT, DEFAULT_RECORDSET_COUNT);
Steve McKayffc11082017-01-17 11:12:08 -080074
75 // Can be -1 (magic unknown), or 0 or more, but not less than -1.
76 assert(recordsetSize > -2);
77
78 // Client may force override the recordset size to -1 which is MAGIC unknown value.
79 // Even if, we still need some finite number against to work.
Steve McKay71e84b92017-04-24 12:32:55 -070080 int size = (recordsetSize == UNDETERMINED_RECORDSET_COUNT)
81 ? DEFAULT_RECORDSET_COUNT
Steve McKayffc11082017-01-17 11:12:08 -080082 : recordsetSize;
83
84 // Calculate the number of items to include in the cursor.
85 int numItems = (limit >= 0)
86 ? Math.min(limit, size - offset)
87 : size - offset;
88
89 assert(offset >= 0);
90 assert(numItems >= 0);
91 for (int i = 0; i < numItems; i++) {
92 addFile(c, String.format("%05d", offset + i));
93 }
Steve McKay71e84b92017-04-24 12:32:55 -070094 extras.putInt(ContentResolver.EXTRA_TOTAL_COUNT, recordsetSize);
Steve McKayffc11082017-01-17 11:12:08 -080095 return c;
96 }
97}