1 /* 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 */ 16 package com.android.documentsui; 17 18 import android.content.ContentResolver; 19 import android.database.Cursor; 20 import android.database.MatrixCursor; 21 import android.os.Bundle; 22 import android.provider.DocumentsContract.Root; 23 24 import java.io.FileNotFoundException; 25 26 /** 27 * Test provider w/ support for paging, and various sub-scenarios of paging. 28 */ 29 public class PagingProvider extends TestRootProvider { 30 31 /** 32 * Pass test result size to inform the provider of the result size. Defaults to 1k. 33 */ 34 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; 37 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 PagingProvider()42 public PagingProvider() { 43 super("Paging Root", ROOT_ID, ROOT_FLAGS, ROOT_DOC_ID); 44 } 45 46 @Override queryDocument(String documentId, String[] projection)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 queryChildDocuments( String parentDocumentId, String[] projection, String sortOrder)55 public Cursor queryChildDocuments( 56 String parentDocumentId, String[] projection, String sortOrder) 57 throws FileNotFoundException { 58 throw new UnsupportedOperationException(); 59 } 60 61 @Override queryChildDocuments( String parentDocumentId, String[] projection, Bundle queryArgs)62 public Cursor queryChildDocuments( 63 String parentDocumentId, String[] projection, Bundle queryArgs) 64 throws FileNotFoundException { 65 66 queryArgs = queryArgs != null ? queryArgs : Bundle.EMPTY; 67 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); 73 int recordsetSize = queryArgs.getInt(TEST_RECORDSET_COUNT, DEFAULT_RECORDSET_COUNT); 74 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. 80 int size = (recordsetSize == UNDETERMINED_RECORDSET_COUNT) 81 ? DEFAULT_RECORDSET_COUNT 82 : 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 } 94 extras.putInt(ContentResolver.EXTRA_TOTAL_COUNT, recordsetSize); 95 return c; 96 } 97 } 98