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 
17 package com.android.documentsui;
18 
19 import android.app.AuthenticationRequiredException;
20 import android.app.PendingIntent;
21 import android.content.Intent;
22 import android.database.Cursor;
23 import android.database.MatrixCursor;
24 import android.os.Bundle;
25 import android.provider.DocumentsContract;
26 
27 import java.io.FileNotFoundException;
28 
29 /**
30  * Provides data view that exercises some of the more esoteric functionality...like display of INFO
31  * and ERROR messages.
32  * <p>
33  * Do not use this provider for automated testing.
34  */
35 public class DemoProvider extends TestRootProvider {
36 
37     public static final String DIR_INFO = "show info";
38     public static final String DIR_ERROR = "show error";
39     public static final String DIR_ERROR_AND_INFO = "show both error and info";
40     public static final String DIR_THROW = "throw a nice exception";
41     public static final String DIR_AUTH = "throw a authentication exception";
42 
43     public static final String MSG_INFO = "All files in this root support settings.";
44     public static final String MSG_ERROR = "I'm an error. Don't judge me.";
45     public static final String MSG_ERROR_AND_INFO = "ERROR: Both ERROR and INFO returned.";
46 
47     private static final String ROOT_ID = "demo-root";
48     private static final String ROOT_DOC_ID = "root0";
49 
DemoProvider()50     public DemoProvider() {
51         super("Demo Root", ROOT_ID, 0, ROOT_DOC_ID);
52     }
53 
54     @Override
queryDocument(String documentId, String[] projection)55     public Cursor queryDocument(String documentId, String[] projection)
56             throws FileNotFoundException {
57         MatrixCursor c = createDocCursor(projection);
58         Bundle extras = c.getExtras();
59         extras.putString(
60                 DocumentsContract.EXTRA_INFO,
61                 "This provider is for feature demos only. Do not use from automated tests.");
62         addFolder(c, documentId);
63         return c;
64     }
65 
66     @Override
queryChildDocuments( String parentDocumentId, String[] projection, String sortOrder)67     public Cursor queryChildDocuments(
68             String parentDocumentId, String[] projection, String sortOrder)
69             throws FileNotFoundException {
70         MatrixCursor c = createDocCursor(projection);
71         Bundle extras = c.getExtras();
72 
73         switch (parentDocumentId) {
74             case DIR_INFO:
75                 extras.putString(DocumentsContract.EXTRA_INFO, MSG_INFO);
76                 addFolder(c, "folder");
77                 addFile(c, "zzz");
78                 for (int i = 0; i < 100; i++) {
79                     addFile(c, "" + i, DocumentsContract.Document.FLAG_SUPPORTS_SETTINGS);
80                 }
81                 break;
82 
83             case DIR_ERROR:
84                 extras.putString(DocumentsContract.EXTRA_ERROR, MSG_ERROR);
85                 break;
86 
87             case DIR_ERROR_AND_INFO:
88                 extras.putString(DocumentsContract.EXTRA_INFO, MSG_INFO);
89                 extras.putString(DocumentsContract.EXTRA_ERROR, MSG_ERROR_AND_INFO);
90                 break;
91 
92             case DIR_THROW:
93                 throw new RuntimeException();
94 
95             case DIR_AUTH:
96                 Intent intent = new Intent("com.android.documentsui.test.action.AUTHENTICATE");
97                 PendingIntent pIntent = PendingIntent.getActivity(getContext(),
98                         AbstractActionHandler.CODE_AUTHENTICATION, intent, 0);
99                 throw new AuthenticationRequiredException(new UnsupportedOperationException(),
100                         pIntent);
101 
102             default:
103                 addFolder(c, DIR_INFO);
104                 addFolder(c, DIR_ERROR);
105                 addFolder(c, DIR_ERROR_AND_INFO);
106                 addFolder(c, DIR_THROW);
107                 addFolder(c, DIR_AUTH);
108                 break;
109         }
110 
111         return c;
112     }
113 }
114 
115