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 package com.android.documentsui;
17 
18 import static android.provider.DocumentsContract.Document.FLAG_SUPPORTS_METADATA;
19 import static android.provider.DocumentsContract.Document.FLAG_SUPPORTS_SETTINGS;
20 
21 import android.content.Context;
22 import android.content.pm.ProviderInfo;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.database.MatrixCursor.RowBuilder;
26 import android.media.ExifInterface;
27 import android.os.Bundle;
28 import android.provider.DocumentsContract.Document;
29 
30 import java.io.FileNotFoundException;
31 
32 /**
33  * Content Provider for testing the Document Inspector.
34  *
35  *  Structure of the provider.
36  *
37  *         Top ------------> Middle  ------> Bottom -------> Dummy21 50B
38  *         openInProvider    Dummy1 50B      Dummy11 50B     Dummy22 150B
39  *         test.txt          Dummy2 150B     Dummy12 150B    Dummy23 100B
40  *         update.txt        Dummy3 100B     Dummy13 100B
41  *         test.jpg
42  *         invalid.jpg
43  */
44 public class InspectorProvider extends TestRootProvider {
45 
46     public static final String AUTHORITY = "com.android.documentsui.inspectorprovider";
47     public static final String OPEN_IN_PROVIDER_TEST = "OpenInProviderTest";
48     public static final String ROOT_ID = "inspector-root";
49     // Number of items under the root path of InspectorProvider.
50     public static final String NUMBER_OF_ITEMS = "6";
51     // Virtual jpeg files for test metadata loading from provider.
52     // TEST_JPEG is a normal jpeg file with legal exif data.
53     // INVALID_JPEG is a invalid jpeg file with broken exif data.
54     // TEST_JPEG_WIDTH, TEST_JPEG_HEIGHT are exif information for TEST_JPEG.
55     public static final String TEST_JPEG = "test.jpg";
56     public static final String INVALID_JPEG = "invalid.jpg";
57     public static final int TEST_JPEG_WIDTH = 3840;
58     public static final int TEST_JPEG_HEIGHT = 2160;
59 
60     private static final String ROOT_DOC_ID = "root0";
61     private static final int ROOT_FLAGS = 0;
62 
InspectorProvider()63     public InspectorProvider() {
64         super("Inspector Root", ROOT_ID, ROOT_FLAGS, ROOT_DOC_ID);
65     }
66 
67     @Override
queryDocument(String documentId, String[] projection)68     public Cursor queryDocument(String documentId, String[] projection)
69             throws FileNotFoundException {
70 
71         if (OPEN_IN_PROVIDER_TEST.equals(documentId)) {
72             MatrixCursor c = createDocCursor(projection);
73             addFile(c, OPEN_IN_PROVIDER_TEST, FLAG_SUPPORTS_SETTINGS);
74             return c;
75         }
76 
77         MatrixCursor c = createDocCursor(projection);
78         addFolder(c, documentId);
79         return c;
80     }
81 
82     @Override
attachInfo(Context context, ProviderInfo info)83     public void attachInfo(Context context, ProviderInfo info) {
84         // prevent the testing from Security Exception comes from DocumentsProvider
85         info.exported = true;
86         info.grantUriPermissions = true;
87         info.writePermission = android.Manifest.permission.MANAGE_DOCUMENTS;
88         info.readPermission = android.Manifest.permission.MANAGE_DOCUMENTS;
89 
90         super.attachInfo(context, info);
91     }
92 
93     @Override
queryChildDocuments(String s, String[] projection, String s1)94     public Cursor queryChildDocuments(String s, String[] projection, String s1)
95             throws FileNotFoundException {
96 
97         if("Top".equals(s)) {
98             MatrixCursor c = createDocCursor(projection);
99             addFolder(c, "Middle");
100             addFileWithSize(c, "dummy1", 50);
101             addFileWithSize(c, "dummy2", 150);
102             addFileWithSize(c, "dummy3", 100);
103             return c;
104         }
105         else if("Middle".equals(s)) {
106             MatrixCursor c = createDocCursor(projection);
107             addFolder(c, "Bottom");
108             addFileWithSize(c, "dummy11", 50);
109             addFileWithSize(c, "dummy12", 150);
110             addFileWithSize(c, "dummy13", 100);
111             return c;
112         }
113         else if("Bottom".equals(s)) {
114             MatrixCursor c = createDocCursor(projection);
115             addFileWithSize(c, "dummy21", 50);
116             addFileWithSize(c, "dummy22", 150);
117             addFileWithSize(c, "dummy23", 100);
118             return c;
119         }
120         else {
121             MatrixCursor c = createDocCursor(projection);
122             // If you add folder or file here, please modify NUMBER_OF_ITEMS above for
123             // #testFolderDetails in InspectorUiTest accordingly.
124             addFolder(c, "Top");
125             addFile(c, OPEN_IN_PROVIDER_TEST, FLAG_SUPPORTS_SETTINGS);
126             addFile(c, "test.txt");
127             addFile(c, "update.txt");
128             addFile(c, TEST_JPEG, FLAG_SUPPORTS_METADATA);
129             addFile(c, INVALID_JPEG, FLAG_SUPPORTS_METADATA);
130             return c;
131         }
132     }
133 
addFileWithSize(MatrixCursor c, String id, long size)134     private void addFileWithSize(MatrixCursor c, String id, long size) {
135         final RowBuilder row = c.newRow();
136         row.add(Document.COLUMN_DOCUMENT_ID, id);
137         row.add(Document.COLUMN_DISPLAY_NAME, id);
138         row.add(Document.COLUMN_SIZE, size);
139         row.add(Document.COLUMN_MIME_TYPE, "text/plain");
140         row.add(Document.COLUMN_FLAGS, 0);
141         row.add(Document.COLUMN_LAST_MODIFIED, System.currentTimeMillis());
142     }
143 
144     @Override
getDocumentMetadata(String documentId)145     public Bundle getDocumentMetadata(String documentId) throws FileNotFoundException {
146         if (TEST_JPEG.contentEquals(documentId)) {
147             Bundle metaData = new Bundle();
148             metaData.putInt(ExifInterface.TAG_IMAGE_WIDTH, TEST_JPEG_WIDTH);
149             metaData.putInt(ExifInterface.TAG_IMAGE_LENGTH, TEST_JPEG_HEIGHT);
150             return metaData;
151         } else if (INVALID_JPEG.contentEquals(documentId)) {
152             // Suppose there are some errors occurs.
153             // Return null makes DocumentsContract throw a RemoteExcpetion,
154             // and rethrow a RemoteException when using ContentResolver.
155             return null;
156         }
157 
158         return super.getDocumentMetadata(documentId);
159     }
160 }
161