Home | History | Annotate | Download | only in documentsui
      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     private static final String ROOT_ID = "demo-root";
     38     private static final String ROOT_DOC_ID = "root0";
     39 
     40     public DemoProvider() {
     41         super("Demo Root", ROOT_ID, 0, ROOT_DOC_ID);
     42     }
     43 
     44     @Override
     45     public Cursor queryDocument(String documentId, String[] projection)
     46             throws FileNotFoundException {
     47         MatrixCursor c = createDocCursor(projection);
     48         Bundle extras = c.getExtras();
     49         extras.putString(
     50                 DocumentsContract.EXTRA_INFO,
     51                 "This provider is for feature demos only. Do not use from automated tests.");
     52         addFolder(c, documentId);
     53         return c;
     54     }
     55 
     56     @Override
     57     public Cursor queryChildDocuments(
     58             String parentDocumentId, String[] projection, String sortOrder)
     59             throws FileNotFoundException {
     60         MatrixCursor c = createDocCursor(projection);
     61         Bundle extras = c.getExtras();
     62 
     63         switch (parentDocumentId) {
     64             case "show info":
     65                 extras.putString(
     66                         DocumentsContract.EXTRA_INFO,
     67                         "All files in this root support settings from owner.");
     68                 addFolder(c, "folder");
     69                 addFile(c, "zzz");
     70                 for (int i = 0; i < 100; i++) {
     71                     addFile(c, "" + i, DocumentsContract.Document.FLAG_SUPPORTS_SETTINGS);
     72                 }
     73                 break;
     74 
     75             case "show error":
     76                 extras.putString(
     77                         DocumentsContract.EXTRA_ERROR,
     78                         "I'm a synthetic ERROR. Don't judge me.");
     79                 break;
     80 
     81             case "show both error and info":
     82                 extras.putString(
     83                         DocumentsContract.EXTRA_INFO,
     84                         "INFO: I'm confused. I've show both ERROR and INFO.");
     85                 extras.putString(
     86                         DocumentsContract.EXTRA_ERROR,
     87                         "ERROR: I'm confused. I've show both ERROR and INFO.");
     88                 break;
     89 
     90             case "throw a nice exception":
     91                 throw new RuntimeException();
     92 
     93             case "throw a authentication exception":
     94                 Intent intent = new Intent("com.android.documentsui.test.action.AUTHENTICATE");
     95                 PendingIntent pIntent = PendingIntent.getActivity(getContext(),
     96                         AbstractActionHandler.CODE_AUTHENTICATION, intent, 0);
     97                 throw new AuthenticationRequiredException(new UnsupportedOperationException(),
     98                         pIntent);
     99 
    100             default:
    101                 addFolder(c, "show info");
    102                 addFolder(c, "show error");
    103                 addFolder(c, "show both error and info");
    104                 addFolder(c, "throw a nice exception");
    105                 addFolder(c, "throw a authentication exception");
    106                 break;
    107         }
    108 
    109         return c;
    110     }
    111 }
    112 
    113