Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DocumentsProvider: What Is content://com.android.documentsui.recents/state And Why Does It Not Like Me?

Tags:

android

I am attempting to implement a DocumentsProvider as a demo for my book. While the provider shows up in a consumer sample app, as soon as I click on it in the Storage Access Framework's UI, I get the following stack trace:

09-15 18:40:46.290    1765-1829/com.android.documentsui E/AndroidRuntime﹕ FATAL EXCEPTION: ProviderExecutor: com.commonsware.android.documents.provider
    Process: com.android.documentsui, PID: 1765
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at com.android.documentsui.ProviderExecutor.run(ProviderExecutor.java:107)
     Caused by: java.lang.UnsupportedOperationException: Unsupported Uri content://com.android.documentsui.recents/state/com.commonsware.android.documents.provider/thisIsMyRoot/
            at com.android.documentsui.RecentsProvider.query(RecentsProvider.java:192)
            at android.content.ContentProvider.query(ContentProvider.java:857)
            at android.content.ContentProvider$Transport.query(ContentProvider.java:200)
            at android.content.ContentResolver.query(ContentResolver.java:461)
            at android.content.ContentResolver.query(ContentResolver.java:404)
            at com.android.documentsui.DirectoryLoader.loadInBackground(DirectoryLoader.java:124)
            at com.android.documentsui.DirectoryLoader.loadInBackground(DirectoryLoader.java:65)
            at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
            at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
            at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at com.android.documentsui.ProviderExecutor.run(ProviderExecutor.java:107)

content://com.commonsware.android.documents.provider/thisIsMyRoot/ is presumably the Uri generated for my documents root, based upon queryRoots() implementation. But I have no idea what content://com.android.documentsui.recents/state/com.commonsware.android.documents.provider/thisIsMyRoot/ or what I am supposed to doing to prevent this error.

Here is the DocumentsProvider implementation, designed to serve files from assets/:

/***
  Copyright (c) 2014 CommonsWare, LLC
  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.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.documents.provider;

import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract.Document;
import android.provider.DocumentsContract.Root;
import android.provider.DocumentsProvider;
import android.util.Log;
import android.webkit.MimeTypeMap;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

public class DemoDocumentProvider extends DocumentsProvider {
  private static final String[] SUPPORTED_ROOT_PROJECTION=new String[] {
      Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_TITLE,
      Root.COLUMN_DOCUMENT_ID, Root.COLUMN_ICON };
  private static final String[] SUPPORTED_DOCUMENT_PROJECTION=
      new String[] { Document.COLUMN_DOCUMENT_ID,
          Document.COLUMN_MIME_TYPE, Document.COLUMN_DISPLAY_NAME,
          Document.COLUMN_FLAGS};
  private static final String ROOT_ID="thisIsMyRoot";
  private static final String ROOT_DOCUMENT_ID="thisCannotBeEmpty";
  private AssetManager assets;

  @Override
  public boolean onCreate() {
    assets=getContext().getAssets();

    return(true);
  }

  @Override
  public Cursor queryRoots(String[] projection)
      throws FileNotFoundException {
    String[] netProjection=
        netProjection(projection, SUPPORTED_ROOT_PROJECTION);
    MatrixCursor result=new MatrixCursor(netProjection);
    MatrixCursor.RowBuilder row=result.newRow();

    row.add(Root.COLUMN_ROOT_ID, ROOT_ID);
    row.add(Root.COLUMN_ICON, R.drawable.ic_launcher);
    row.add(Root.COLUMN_FLAGS, Root.FLAG_LOCAL_ONLY);
    row.add(Root.COLUMN_TITLE, getContext().getString(R.string.root));
    row.add(Root.COLUMN_DOCUMENT_ID, ROOT_DOCUMENT_ID);

    return(result);
  }

  @Override
  public Cursor queryChildDocuments(String parentDocumentId,
                                    String[] projection,
                                    String sortOrder)
      throws FileNotFoundException {
    String[] netProjection=
        netProjection(projection, SUPPORTED_DOCUMENT_PROJECTION);
    MatrixCursor result=new MatrixCursor(netProjection);

    parentDocumentId=fixUpDocumentId(parentDocumentId);

    try {
      String[] children=assets.list(parentDocumentId);

      for (String child : children) {
        addDocumentRow(result, child, parentDocumentId+child);
      }
    }
    catch (IOException e) {
      Log.e(getClass().getSimpleName(), "Exception reading asset dir", e);
    }

    return(result);
  }

  @Override
  public Cursor queryDocument(String documentId, String[] projection)
      throws FileNotFoundException {
    String[] netProjection=
        netProjection(projection, SUPPORTED_DOCUMENT_PROJECTION);
    MatrixCursor result=new MatrixCursor(netProjection);

    documentId=fixUpDocumentId(documentId);

    try {
      addDocumentRow(result, Uri.parse(documentId).getLastPathSegment(),
                      documentId);
    }
    catch (IOException e) {
      Log.e(getClass().getSimpleName(), "Exception reading asset dir", e);
    }

    return(result);
  }

  @Override
  public ParcelFileDescriptor openDocument(String documentId,
                                           String mode,
                                           CancellationSignal signal)
      throws FileNotFoundException {
    ParcelFileDescriptor[] pipe=null;

    try {
      pipe=ParcelFileDescriptor.createPipe();
      AssetManager assets=getContext().getResources().getAssets();

      new TransferThread(assets.open(documentId),
          new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1])).start();
    }
    catch (IOException e) {
      Log.e(getClass().getSimpleName(), "Exception opening pipe", e);
      throw new FileNotFoundException("Could not open pipe for: "
          + documentId);
    }

    return(pipe[0]);
  }

  private void addDocumentRow(MatrixCursor result, String child,
                              String assetPath) throws IOException {
    MatrixCursor.RowBuilder row=result.newRow();

    row.add(Document.COLUMN_DOCUMENT_ID, assetPath);

    if (isDirectory(assetPath)) {
      row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR);
    }
    else {
      row.add(Document.COLUMN_MIME_TYPE,
          MimeTypeMap.getFileExtensionFromUrl(assetPath));
      row.add(Document.COLUMN_SIZE, lastModified(assetPath));
    }

    row.add(Document.COLUMN_DISPLAY_NAME, child);
    row.add(Document.COLUMN_FLAGS, 0);
  }

  private boolean isDirectory(String assetPath) throws IOException {
    return(assets.list(assetPath).length>1);
  }

  private long lastModified(String assetPath) throws IOException {
    return(assets.openFd(assetPath).getLength());
  }

  private String fixUpDocumentId(String documentId) {
    if (ROOT_DOCUMENT_ID.equals(documentId)) {
      return("");
    }

    return(documentId);
  }

  private static String[] netProjection(String[] requested, String[] supported) {
    if (requested==null) {
      return(supported);
    }

    ArrayList<String> result=new ArrayList<String>();

    for (String request : requested) {
      for (String support : supported) {
        if (request.equals(support)) {
          result.add(request);
          break;
        }
      }
    }

    return(result.toArray(new String[0]));
  }

  static class TransferThread extends Thread {
    InputStream in;
    OutputStream out;

    TransferThread(InputStream in, OutputStream out) {
      this.in=in;
      this.out=out;
    }

    @Override
    public void run() {
      byte[] buf=new byte[1024];
      int len;

      try {
        while ((len=in.read(buf)) >= 0) {
          out.write(buf, 0, len);
        }

        in.close();
        out.flush();
        out.close();
      }
      catch (IOException e) {
        Log.e(getClass().getSimpleName(),
            "Exception transferring file", e);
      }
    }
  }
}

So, my question is: where am I going wrong?

like image 932
CommonsWare Avatar asked Sep 15 '14 22:09

CommonsWare


People also ask

What is a document provider?

Document provider—A content provider that allows a storage service (such as Google Drive) to reveal the files it manages. A document provider is implemented as a subclass of the DocumentsProvider class.

How do you use a content provider?

To access the content, define a content provider URI address. Create a database to store the application data. Implement the six abstract methods of ContentProvider class. Register the content provider in AndroidManifest.


1 Answers

I've created a gist, it has the working code.

Issue:

In case of the root dir, the queryDocument implementation is returning empty string document id. RecentProvider tries to match the uri against state/*/*/*, where the last segment is document id. Since document id is empty string, uri match fails, hence the unsupported uri exception is thrown.

Solution: I've provided totally 4 fixes to make the solution completely work. I've left comments in the code.

Fix 1: While querying the child documents, if the parent is root, then the assetPath should be just the filename. If the parent is directory within the assets folder, then assetPath should be directory/filename. Also you need to add file separator between the components.

Change

addDocumentRow(result, child, parentDocumentId+child);

to

addDocumentRow(result, child, parentDocumentId == "" ? child : parentDocumentId + File.separator + child);

Fix 2: This provides the fix for the unsupported uri exception. Document id should never be null.

Remove documentId=fixUpDocumentId(documentId); from queryDocument.

Fix 3 & 4: Determine whether the file is directory. In case of root, the asset directory should be empty string. Non-empty directory will have atleast 1 file.

Change :

private boolean isDirectory(String assetPath) throws IOException {
    return(assets.list(assetPath).length>1);
}

to

private boolean isDirectory(String assetPath) throws IOException {
    // Fix 3 : Call fixUpDocumentId. In case of root, the call should be assets.list("").
    assetPath = fixUpDocumentId(assetPath);

    // Fix 4 : Empty directories are not included in apk. Non empty directory will have atleast 1 file.
    return(assets.list(assetPath).length>=1);
}

Here is the screenrecord:

Screen record

like image 167
Manish Mulimani Avatar answered Sep 20 '22 19:09

Manish Mulimani