Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b5dbf286 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add a "DemoProvider" that always shows INFO..." into nyc-andromeda-dev

parents 968169ae ee7d5877
Loading
Loading
Loading
Loading
+12 −0
Original line number Original line Diff line number Diff line
@@ -17,6 +17,18 @@
                <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
                <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
            </intent-filter>
            </intent-filter>
       </provider>
       </provider>
       <!-- Provider that demostrates some features, like display of INFO and ERROR messages. -->
       <provider
            android:name="com.android.documentsui.DemoProvider"
            android:authorities="com.android.documentsui.demoprovider"
            android:exported="true"
            android:grantUriPermissions="true"
            android:permission="android.permission.MANAGE_DOCUMENTS"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
            </intent-filter>
       </provider>
    </application>
    </application>


    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
+158 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.documentsui;

import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MatrixCursor.RowBuilder;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Document;
import android.provider.DocumentsContract.Root;
import android.provider.DocumentsProvider;

import java.io.FileNotFoundException;

/**
 * Provides data view that exercises some of the more esoteric functionality...like
 * display of INFO and ERROR messages.
 *
 * <p>Do not use this provider for automated testing.
 */
public class DemoProvider extends DocumentsProvider {

    private static final String ROOT_ID = "demo-root";

    private static final String[] DEFAULT_ROOT_PROJECTION = new String[] {
            Root.COLUMN_ROOT_ID,
            Root.COLUMN_FLAGS,
            Root.COLUMN_TITLE,
            Root.COLUMN_DOCUMENT_ID,
            Root.COLUMN_AVAILABLE_BYTES
    };

    private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] {
            Document.COLUMN_DOCUMENT_ID,
            Document.COLUMN_MIME_TYPE,
            Document.COLUMN_DISPLAY_NAME,
            Document.COLUMN_LAST_MODIFIED,
            Document.COLUMN_FLAGS,
            Document.COLUMN_SIZE,
    };

    @Override
    public Cursor queryRoots(String[] projection) throws FileNotFoundException {
        MatrixCursor c = new MatrixCursor(
                projection != null ? projection : DEFAULT_ROOT_PROJECTION);
        final RowBuilder row = c.newRow();
        row.add(Root.COLUMN_ROOT_ID, ROOT_ID);
        row.add(Root.COLUMN_FLAGS, 0);
        row.add(Root.COLUMN_TITLE, "Demo Root");
        row.add(Root.COLUMN_DOCUMENT_ID, "root0");
        row.add(Root.COLUMN_AVAILABLE_BYTES, 1024 * 1024 * 100);
        return c;
    }

    @Override
    public Cursor queryDocument(String documentId, String[] projection)
            throws FileNotFoundException {
        MatrixCursor c = new MatrixCursor(
                projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION);
            Bundle extras = new Bundle();
            c.setExtras(extras);
            extras.putString(
                    DocumentsContract.EXTRA_INFO,
                    "This provider is for feature demos only. Do not use from automated tests.");
        addFolder(c, documentId);
        return c;
    }

    @Override
    public Cursor queryChildDocuments(
            String parentDocumentId, String[] projection, String sortOrder)
            throws FileNotFoundException {
        MatrixCursor c = new MatrixCursor(
                projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION);
        Bundle extras = new Bundle();
        c.setExtras(extras);

        switch (parentDocumentId) {
            case "aaa":
                extras.putString(
                        DocumentsContract.EXTRA_INFO,
                        "I'm a synthetic INFO. Don't judge me.");
                addFile(c, "zzz");
                break;

            case "bbb":
                extras.putString(
                        DocumentsContract.EXTRA_INFO,
                        "I'm a synthetic INFO. Don't judge me.");
                break;

            case "ccc":
                extras.putString(
                        DocumentsContract.EXTRA_INFO,
                        "INFO: I'm confused. I've show both ERROR and INFO.");
                extras.putString(
                        DocumentsContract.EXTRA_ERROR,
                        "ERROR: I'm confused. I've show both ERROR and INFO.");
                break;

            default:
                addFolder(c, "aaa");
                addFolder(c, "bbb");
                addFolder(c, "ccc");
                break;
        }

        return c;
    }

    private void addFolder(MatrixCursor c, String id) {
        final RowBuilder row = c.newRow();
        row.add(Document.COLUMN_DOCUMENT_ID, id);
        row.add(Document.COLUMN_DISPLAY_NAME, id);
        row.add(Document.COLUMN_SIZE, 0);
        row.add(Document.COLUMN_MIME_TYPE, DocumentsContract.Document.MIME_TYPE_DIR);
        row.add(Document.COLUMN_FLAGS, 0);
        row.add(Document.COLUMN_LAST_MODIFIED, System.currentTimeMillis());
    }

    private void addFile(MatrixCursor c, String id) {
        final RowBuilder row = c.newRow();
        row.add(Document.COLUMN_DOCUMENT_ID, id);
        row.add(Document.COLUMN_DISPLAY_NAME, id);
        row.add(Document.COLUMN_SIZE, 0);
        row.add(Document.COLUMN_MIME_TYPE, "text/plain");
        row.add(Document.COLUMN_FLAGS, 0);
        row.add(Document.COLUMN_LAST_MODIFIED, System.currentTimeMillis());
    }

    @Override
    public ParcelFileDescriptor openDocument(String documentId, String mode,
            CancellationSignal signal) throws FileNotFoundException {
        throw new UnsupportedOperationException("Nope!");
    }

    @Override
    public boolean onCreate() {
        return true;
    }

}