1 /* 2 * Copyright (C) 2017 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 17 package com.android.documentsui.testing; 18 19 import android.content.pm.ProviderInfo; 20 import android.database.Cursor; 21 import android.database.MatrixCursor; 22 import android.net.Uri; 23 import android.os.Bundle; 24 import android.os.CancellationSignal; 25 import android.os.ParcelFileDescriptor; 26 import android.provider.DocumentsContract.Document; 27 import android.provider.DocumentsProvider; 28 29 import com.android.documentsui.base.DocumentInfo; 30 31 import java.io.FileNotFoundException; 32 33 /** 34 * Test doubles of {@link DocumentsProvider} to isolate document providers. This is not registered 35 * or exposed through AndroidManifest, but only used locally. 36 */ 37 public class TestDocumentsProvider extends DocumentsProvider { 38 39 private String[] DOCUMENTS_PROJECTION = new String[] { 40 Document.COLUMN_DOCUMENT_ID, 41 Document.COLUMN_MIME_TYPE, 42 Document.COLUMN_DISPLAY_NAME, 43 Document.COLUMN_LAST_MODIFIED, 44 Document.COLUMN_FLAGS, 45 Document.COLUMN_SUMMARY, 46 Document.COLUMN_SIZE, 47 Document.COLUMN_ICON 48 }; 49 50 private Cursor mNextChildDocuments; 51 private Cursor mNextRecentDocuments; 52 53 public TestDocumentsProvider(String authority) { 54 ProviderInfo info = new ProviderInfo(); 55 info.authority = authority; 56 attachInfoForTesting(null, info); 57 } 58 59 @Override 60 public boolean refresh(Uri url, Bundle args, CancellationSignal signal) { 61 return true; 62 } 63 64 @Override 65 public Cursor queryRoots(String[] projection) throws FileNotFoundException { 66 return null; 67 } 68 69 @Override 70 public Cursor queryDocument(String documentId, String[] projection) 71 throws FileNotFoundException { 72 return null; 73 } 74 75 @Override 76 public Cursor queryChildDocuments(String parentDocumentId, String[] projection, 77 String sortOrder) throws FileNotFoundException { 78 return mNextChildDocuments; 79 } 80 81 @Override 82 public ParcelFileDescriptor openDocument(String documentId, String mode, 83 CancellationSignal signal) throws FileNotFoundException { 84 return null; 85 } 86 87 @Override 88 public Cursor queryRecentDocuments(String rootId, String[] projection) { 89 return mNextRecentDocuments; 90 } 91 92 @Override 93 public boolean onCreate() { 94 return true; 95 } 96 97 /** 98 * Sets the next return value for {@link #queryChildDocuments(String, String[], String)}. 99 * @param docs docs to return for next query. 100 */ 101 public void setNextChildDocumentsReturns(DocumentInfo... docs) { 102 mNextChildDocuments = createDocumentsCursor(docs); 103 } 104 105 public void setNextRecentDocumentsReturns(DocumentInfo... docs) { 106 mNextRecentDocuments = createDocumentsCursor(docs); 107 } 108 109 private Cursor createDocumentsCursor(DocumentInfo... docs) { 110 MatrixCursor cursor = new MatrixCursor(DOCUMENTS_PROJECTION); 111 for (DocumentInfo doc : docs) { 112 cursor.newRow() 113 .add(Document.COLUMN_DOCUMENT_ID, doc.documentId) 114 .add(Document.COLUMN_MIME_TYPE, doc.mimeType) 115 .add(Document.COLUMN_DISPLAY_NAME, doc.displayName) 116 .add(Document.COLUMN_LAST_MODIFIED, doc.lastModified) 117 .add(Document.COLUMN_FLAGS, doc.flags) 118 .add(Document.COLUMN_SUMMARY, doc.summary) 119 .add(Document.COLUMN_SIZE, doc.size) 120 .add(Document.COLUMN_ICON, doc.icon); 121 } 122 123 return cursor; 124 } 125 } 126