blob: 459e13bd35070f2e0e8891edb7d87dd8f77a6661 [file] [log] [blame]
Steve McKayee7d5872016-11-17 13:59:00 -08001/*
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 */
Ben Linf8f06e92017-01-27 17:15:48 -080016
Steve McKayee7d5872016-11-17 13:59:00 -080017package com.android.documentsui;
18
Ben Linb3c4f122017-03-21 11:35:14 -070019import android.app.AuthenticationRequiredException;
Ben Lin42b83bf2017-01-27 17:15:48 -080020import android.app.PendingIntent;
Ben Lin42b83bf2017-01-27 17:15:48 -080021import android.content.Intent;
Steve McKayee7d5872016-11-17 13:59:00 -080022import android.database.Cursor;
23import android.database.MatrixCursor;
Steve McKayee7d5872016-11-17 13:59:00 -080024import android.os.Bundle;
Steve McKayee7d5872016-11-17 13:59:00 -080025import android.provider.DocumentsContract;
Steve McKayee7d5872016-11-17 13:59:00 -080026
27import java.io.FileNotFoundException;
28
29/**
Ben Linf8f06e92017-01-27 17:15:48 -080030 * 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.
Steve McKayee7d5872016-11-17 13:59:00 -080034 */
Steve McKayf8407972017-02-03 09:42:52 -080035public class DemoProvider extends TestRootProvider {
Steve McKayee7d5872016-11-17 13:59:00 -080036
Steve McKayb9373d02018-05-07 15:56:35 -070037 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
Steve McKayee7d5872016-11-17 13:59:00 -080047 private static final String ROOT_ID = "demo-root";
Steve McKayf8407972017-02-03 09:42:52 -080048 private static final String ROOT_DOC_ID = "root0";
Steve McKayee7d5872016-11-17 13:59:00 -080049
Steve McKayf8407972017-02-03 09:42:52 -080050 public DemoProvider() {
51 super("Demo Root", ROOT_ID, 0, ROOT_DOC_ID);
Steve McKayee7d5872016-11-17 13:59:00 -080052 }
53
54 @Override
55 public Cursor queryDocument(String documentId, String[] projection)
56 throws FileNotFoundException {
Steve McKayf8407972017-02-03 09:42:52 -080057 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.");
Steve McKayee7d5872016-11-17 13:59:00 -080062 addFolder(c, documentId);
63 return c;
64 }
65
66 @Override
67 public Cursor queryChildDocuments(
68 String parentDocumentId, String[] projection, String sortOrder)
69 throws FileNotFoundException {
Steve McKayf8407972017-02-03 09:42:52 -080070 MatrixCursor c = createDocCursor(projection);
71 Bundle extras = c.getExtras();
Steve McKayee7d5872016-11-17 13:59:00 -080072
73 switch (parentDocumentId) {
Steve McKayb9373d02018-05-07 15:56:35 -070074 case DIR_INFO:
75 extras.putString(DocumentsContract.EXTRA_INFO, MSG_INFO);
Ben Lin7b38f342016-12-14 11:52:35 -080076 addFolder(c, "folder");
Steve McKayee7d5872016-11-17 13:59:00 -080077 addFile(c, "zzz");
Ben Lin7b38f342016-12-14 11:52:35 -080078 for (int i = 0; i < 100; i++) {
Jon Mann253a9922017-03-21 18:53:27 -070079 addFile(c, "" + i, DocumentsContract.Document.FLAG_SUPPORTS_SETTINGS);
Ben Lin7b38f342016-12-14 11:52:35 -080080 }
Steve McKayee7d5872016-11-17 13:59:00 -080081 break;
82
Steve McKayb9373d02018-05-07 15:56:35 -070083 case DIR_ERROR:
84 extras.putString(DocumentsContract.EXTRA_ERROR, MSG_ERROR);
Steve McKayee7d5872016-11-17 13:59:00 -080085 break;
86
Steve McKayb9373d02018-05-07 15:56:35 -070087 case DIR_ERROR_AND_INFO:
88 extras.putString(DocumentsContract.EXTRA_INFO, MSG_INFO);
89 extras.putString(DocumentsContract.EXTRA_ERROR, MSG_ERROR_AND_INFO);
Steve McKayee7d5872016-11-17 13:59:00 -080090 break;
91
Steve McKayb9373d02018-05-07 15:56:35 -070092 case DIR_THROW:
Ben Lin7b38f342016-12-14 11:52:35 -080093 throw new RuntimeException();
94
Steve McKayb9373d02018-05-07 15:56:35 -070095 case DIR_AUTH:
Ben Lina40e6e32017-05-09 23:59:43 +000096 Intent intent = new Intent("com.android.documentsui.test.action.AUTHENTICATE");
97 PendingIntent pIntent = PendingIntent.getActivity(getContext(),
Zemiao Zhud00f87c2021-01-21 12:05:54 -080098 AbstractActionHandler.CODE_AUTHENTICATION, intent,
99 PendingIntent.FLAG_IMMUTABLE);
Ben Linb3c4f122017-03-21 11:35:14 -0700100 throw new AuthenticationRequiredException(new UnsupportedOperationException(),
Ben Lina40e6e32017-05-09 23:59:43 +0000101 pIntent);
Ben Lin42b83bf2017-01-27 17:15:48 -0800102
Steve McKayee7d5872016-11-17 13:59:00 -0800103 default:
Steve McKayb9373d02018-05-07 15:56:35 -0700104 addFolder(c, DIR_INFO);
105 addFolder(c, DIR_ERROR);
106 addFolder(c, DIR_ERROR_AND_INFO);
107 addFolder(c, DIR_THROW);
108 addFolder(c, DIR_AUTH);
Steve McKayee7d5872016-11-17 13:59:00 -0800109 break;
110 }
111
112 return c;
113 }
Steve McKayee7d5872016-11-17 13:59:00 -0800114}
Steve McKay33e03e82017-02-03 12:37:29 -0800115