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

Commit 9d17528e authored by Vladislav Kaznacheev's avatar Vladislav Kaznacheev Committed by android-build-merger
Browse files

Merge "Collect mime type data correctly for all URIs added to ClipData" into nyc-dev

am: e06ef946

* commit 'e06ef946':
  Collect mime type data correctly for all URIs added to ClipData

Change-Id: I6f50e3c880cf7ae6f67f6571cdabe6d68cc1f7c1
parents 13788ba7 e06ef946
Loading
Loading
Loading
Loading
+22 −3
Original line number Diff line number Diff line
@@ -32,7 +32,9 @@ import com.android.documentsui.model.DocumentInfo;
import libcore.io.IoUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
@@ -125,22 +127,39 @@ public final class DocumentClipper {
     */
    public @Nullable ClipData getClipDataForDocuments(List<DocumentInfo> docs) {
        final ContentResolver resolver = mContext.getContentResolver();
        final String[] mimeTypes = getMimeTypes(resolver, docs);
        ClipData clipData = null;
        for (DocumentInfo doc : docs) {
            final Uri uri = DocumentsContract.buildDocumentUri(doc.authority, doc.documentId);
            if (clipData == null) {
                // TODO: figure out what this string should be.
                // Currently it is not displayed anywhere in the UI, but this might change.
                final String label = "";
                clipData = ClipData.newUri(resolver, label, uri);
                clipData = new ClipData(label, mimeTypes, new ClipData.Item(doc.derivedUri));
            } else {
                // TODO: update list of mime types in ClipData.
                clipData.addItem(new ClipData.Item(uri));
                clipData.addItem(new ClipData.Item(doc.derivedUri));
            }
        }
        return clipData;
    }

    private static String[] getMimeTypes(ContentResolver resolver, List<DocumentInfo> docs) {
        final HashSet<String> mimeTypes = new HashSet<>();
        for (DocumentInfo doc : docs) {
            assert(doc != null);
            assert(doc.derivedUri != null);
            final Uri uri = doc.derivedUri;
            if ("content".equals(uri.getScheme())) {
                mimeTypes.add(resolver.getType(uri));
                final String[] streamTypes = resolver.getStreamTypes(uri, "*/*");
                if (streamTypes != null) {
                    mimeTypes.addAll(Arrays.asList(streamTypes));
                }
            }
        }
        return mimeTypes.toArray(new String[0]);
    }

    public void clipDocuments(List<DocumentInfo> docs) {
        ClipData data = getClipDataForDocuments(docs);
        mClipboard.setPrimaryClip(data);