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.database.Cursor;
19 import android.database.MatrixCursor;
20 import android.database.MatrixCursor.RowBuilder;
21 import android.os.Bundle;
22 import android.os.CancellationSignal;
23 import android.os.ParcelFileDescriptor;
24 import android.provider.DocumentsContract;
25 import android.provider.DocumentsContract.Document;
26 import android.provider.DocumentsContract.Root;
27 import android.provider.DocumentsProvider;
28 
29 import java.io.FileNotFoundException;
30 
31 /**
32  * Simple test provider that provides a single root. Subclasess provider support
33  * for returning documents.
34  */
35 abstract class TestRootProvider extends DocumentsProvider {
36 
37     static final String[] DEFAULT_ROOT_PROJECTION = new String[] {
38             Root.COLUMN_ROOT_ID,
39             Root.COLUMN_FLAGS,
40             Root.COLUMN_TITLE,
41             Root.COLUMN_DOCUMENT_ID,
42             Root.COLUMN_AVAILABLE_BYTES
43     };
44 
45     static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] {
46             Document.COLUMN_DOCUMENT_ID,
47             Document.COLUMN_MIME_TYPE,
48             Document.COLUMN_DISPLAY_NAME,
49             Document.COLUMN_LAST_MODIFIED,
50             Document.COLUMN_FLAGS,
51             Document.COLUMN_SIZE,
52     };
53 
54     private final String mRootId;
55     private final String mRootName;
56     private final int mFlags;
57     private final String mRootDocId;
58 
TestRootProvider(String rootName, String rootId, int flags, String rootDocId)59     public TestRootProvider(String rootName, String rootId, int flags, String rootDocId) {
60         mRootName = rootName;
61         mRootId = rootId;
62         mFlags = flags;
63         mRootDocId = rootDocId;
64     }
65 
66     @Override
queryRoots(String[] projection)67     public Cursor queryRoots(String[] projection) throws FileNotFoundException {
68         MatrixCursor c = new MatrixCursor(
69                 projection != null ? projection : DEFAULT_ROOT_PROJECTION);
70         final RowBuilder row = c.newRow();
71         row.add(Root.COLUMN_ROOT_ID, mRootId);
72         row.add(Root.COLUMN_TITLE, mRootName);
73         row.add(Root.COLUMN_FLAGS, mFlags);
74         row.add(Root.COLUMN_DOCUMENT_ID, mRootDocId);
75         row.add(Root.COLUMN_AVAILABLE_BYTES, 1024 * 1024 * 1000);
76         return c;
77     }
78 
addFile(MatrixCursor c, String id)79     protected void addFile(MatrixCursor c, String id) {
80         addFile(c, id, 0);
81     }
82 
addFile(MatrixCursor c, String id, int flags)83     protected void addFile(MatrixCursor c, String id, int flags) {
84         final RowBuilder row = c.newRow();
85         row.add(Document.COLUMN_DOCUMENT_ID, id);
86         row.add(Document.COLUMN_DISPLAY_NAME, id);
87         row.add(Document.COLUMN_SIZE, 0);
88         row.add(Document.COLUMN_MIME_TYPE, "text/plain");
89         row.add(Document.COLUMN_FLAGS, flags);
90         row.add(Document.COLUMN_LAST_MODIFIED, System.currentTimeMillis());
91     }
92 
addFolder(MatrixCursor c, String id)93     protected void addFolder(MatrixCursor c, String id) {
94         addFolder(c, id, 0);
95     }
96 
addFolder(MatrixCursor c, String id, int flags)97     protected void addFolder(MatrixCursor c, String id, int flags) {
98         final RowBuilder row = c.newRow();
99         row.add(Document.COLUMN_DOCUMENT_ID, id);
100         row.add(Document.COLUMN_DISPLAY_NAME, id);
101         row.add(Document.COLUMN_SIZE, 0);
102         row.add(Document.COLUMN_MIME_TYPE, DocumentsContract.Document.MIME_TYPE_DIR);
103         row.add(Document.COLUMN_FLAGS, flags);
104         row.add(Document.COLUMN_LAST_MODIFIED, System.currentTimeMillis());
105     }
106 
createDocCursor(String[] projection)107     protected MatrixCursor createDocCursor(String[] projection) {
108         MatrixCursor c = new MatrixCursor(
109                 projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION);
110         c.setExtras(new Bundle());
111         return c;
112     }
113 
114     @Override
openDocument(String documentId, String mode, CancellationSignal signal)115     public ParcelFileDescriptor openDocument(String documentId, String mode,
116             CancellationSignal signal) throws FileNotFoundException {
117         throw new UnsupportedOperationException("Nope!");
118     }
119 
120     @Override
onCreate()121     public boolean onCreate() {
122         return true;
123     }
124 
125 }
126