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

Commit d99964fd authored by Tomasz Mikolajewski's avatar Tomasz Mikolajewski
Browse files

Implement getDocumentStreamTypes() in DocumentsProvider.

According to the documentation, getStreamTypes() must return all streamable
MIME types. This CL takes the MIME types from COLUMN_MIME_TYPE as long as
the document is not virtual.

If the provider implements converters, then it should override
getDocumentStreamTypes.

Bug: 27156282
Change-Id: I9ab149e097600a470fc9be8f1270f68929fdf851
parent e4c1ccec
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -31727,6 +31727,7 @@ package android.provider {
    method public java.lang.String createDocument(java.lang.String, java.lang.String, java.lang.String) throws java.io.FileNotFoundException;
    method public final int delete(android.net.Uri, java.lang.String, java.lang.String[]);
    method public void deleteDocument(java.lang.String) throws java.io.FileNotFoundException;
    method public java.lang.String[] getDocumentStreamTypes(java.lang.String, java.lang.String);
    method public java.lang.String getDocumentType(java.lang.String) throws java.io.FileNotFoundException;
    method public final java.lang.String getType(android.net.Uri);
    method public final android.net.Uri insert(android.net.Uri, android.content.ContentValues);
+1 −0
Original line number Diff line number Diff line
@@ -34108,6 +34108,7 @@ package android.provider {
    method public java.lang.String createDocument(java.lang.String, java.lang.String, java.lang.String) throws java.io.FileNotFoundException;
    method public final int delete(android.net.Uri, java.lang.String, java.lang.String[]);
    method public void deleteDocument(java.lang.String) throws java.io.FileNotFoundException;
    method public java.lang.String[] getDocumentStreamTypes(java.lang.String, java.lang.String);
    method public java.lang.String getDocumentType(java.lang.String) throws java.io.FileNotFoundException;
    method public final java.lang.String getType(android.net.Uri);
    method public final android.net.Uri insert(android.net.Uri, android.content.ContentValues);
+1 −0
Original line number Diff line number Diff line
@@ -31740,6 +31740,7 @@ package android.provider {
    method public java.lang.String createDocument(java.lang.String, java.lang.String, java.lang.String) throws java.io.FileNotFoundException;
    method public final int delete(android.net.Uri, java.lang.String, java.lang.String[]);
    method public void deleteDocument(java.lang.String) throws java.io.FileNotFoundException;
    method public java.lang.String[] getDocumentStreamTypes(java.lang.String, java.lang.String);
    method public java.lang.String getDocumentType(java.lang.String) throws java.io.FileNotFoundException;
    method public final java.lang.String getType(android.net.Uri);
    method public final android.net.Uri insert(android.net.Uri, android.content.ContentValues);
+1 −0
Original line number Diff line number Diff line
@@ -358,6 +358,7 @@ public final class DocumentsContract {
         * @see #COLUMN_MIME_TYPE
         * @see DocumentsProvider#openTypedDocument(String, String, Bundle,
         *      android.os.CancellationSignal)
         * @see DocumentsProvider#getDocumentStreamTypes(String, String)
         */
        public static final int FLAG_VIRTUAL_DOCUMENT = 1 << 9;

+69 −0
Original line number Diff line number Diff line
@@ -540,6 +540,7 @@ public abstract class DocumentsProvider extends ContentProvider {
     *            provider.
     * @param signal used by the caller to signal if the request should be
     *            cancelled. May be null.
     * @see #getDocumentStreamTypes(String, String)
     */
    @SuppressWarnings("unused")
    public AssetFileDescriptor openTypedDocument(
@@ -926,6 +927,7 @@ public abstract class DocumentsProvider extends ContentProvider {
     *
     * @see #openDocumentThumbnail(String, Point, CancellationSignal)
     * @see #openTypedDocument(String, String, Bundle, CancellationSignal)
     * @see #getDocumentStreamTypes(String, String)
     */
    @Override
    public final AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, Bundle opts)
@@ -938,6 +940,7 @@ public abstract class DocumentsProvider extends ContentProvider {
     *
     * @see #openDocumentThumbnail(String, Point, CancellationSignal)
     * @see #openTypedDocument(String, String, Bundle, CancellationSignal)
     * @see #getDocumentStreamTypes(String, String)
     */
    @Override
    public final AssetFileDescriptor openTypedAssetFile(
@@ -946,6 +949,55 @@ public abstract class DocumentsProvider extends ContentProvider {
        return openTypedAssetFileImpl(uri, mimeTypeFilter, opts, signal);
    }

    /**
     * Return a list of streamable MIME types matching the filter, which can be passed to
     * {@link #openTypedDocument(String, String, Bundle, CancellationSignal)}.
     *
     * <p>The default implementation returns a MIME type provided by
     * {@link #queryDocument(String, String[])} as long as it matches the filter and the document
     * does not have the {@link Document#FLAG_VIRTUAL_DOCUMENT} flag set.
     *
     * @see #getStreamTypes(Uri, String)
     * @see #openTypedDocument(String, String, Bundle, CancellationSignal)
     */
    public String[] getDocumentStreamTypes(String documentId, String mimeTypeFilter) {
        Cursor cursor = null;
        try {
            cursor = queryDocument(documentId, null);
            if (cursor.moveToFirst()) {
                final String mimeType =
                    cursor.getString(cursor.getColumnIndexOrThrow(Document.COLUMN_MIME_TYPE));
                final long flags =
                    cursor.getLong(cursor.getColumnIndexOrThrow(Document.COLUMN_FLAGS));
                if ((flags & Document.FLAG_VIRTUAL_DOCUMENT) == 0 && mimeType != null &&
                        mimeTypeMatches(mimeTypeFilter, mimeType)) {
                    return new String[] { mimeType };
                }
            }
        } catch (FileNotFoundException e) {
            return null;
        } finally {
            IoUtils.closeQuietly(cursor);
        }

        // No streamable MIME types.
        return null;
    }

    /**
     * Called by a client to determine the types of data streams that this content provider
     * support for the given URI.
     *
     * <p>Overriding this method is deprecated. Override {@link #openTypedDocument} instead.
     *
     * @see #getDocumentStreamTypes(String, String)
     */
    @Override
    public String[] getStreamTypes(Uri uri, String mimeTypeFilter) {
        enforceTree(uri);
        return getDocumentStreamTypes(getDocumentId(uri), mimeTypeFilter);
    }

    /**
     * @hide
     */
@@ -971,4 +1023,21 @@ public abstract class DocumentsProvider extends ContentProvider {
        // For any other yet unhandled case, let the provider subclass handle it.
        return openTypedDocument(documentId, mimeTypeFilter, opts, signal);
    }

    /**
     * @hide
     */
    public static boolean mimeTypeMatches(String filter, String test) {
        if (test == null) {
            return false;
        } else if (filter == null || "*/*".equals(filter)) {
            return true;
        } else if (filter.equals(test)) {
            return true;
        } else if (filter.endsWith("/*")) {
            return filter.regionMatches(0, test, 0, filter.indexOf('/'));
        } else {
            return false;
        }
    }
}