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

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

Send virtual files with CATEGORY_TYPED_OPENABLE.

Test: Unit tests. Also tested manually with a testing apk.
Bug: 28409713
Change-Id: I086664d092f70ae4bba5dcf364c642ccb36590db
parent 21cf5edd
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -54,16 +54,23 @@ import java.util.function.Predicate;
public class Model {

    /**
     * Filter that passes (returns true) all non-virtual, non-partial files, not archived files.
     * Filter that passes (returns true) all non-partial files and non-archived files.
     */
    public static final Predicate<Cursor> CONCRETE_FILE_FILTER = (Cursor c) -> {
    public static final Predicate<Cursor> NO_PARTIAL_NOR_ARCHIVED_FILE_FILTER = (Cursor c) -> {
        int flags = getCursorInt(c, Document.COLUMN_FLAGS);
        String authority = getCursorString(c, RootCursorWrapper.COLUMN_AUTHORITY);
        return (flags & Document.FLAG_VIRTUAL_DOCUMENT) == 0
                && (flags & Document.FLAG_PARTIAL) == 0
        return (flags & Document.FLAG_PARTIAL) == 0
                && !ArchivesProvider.AUTHORITY.equals(authority);
    };

    /**
     * Filter that passes (returns true) only virtual documents.
     */
    public static final Predicate<Cursor> VIRTUAL_DOCUMENT_FILTER  = (Cursor c) -> {
        int flags = getCursorInt(c, Document.COLUMN_FLAGS);
        return (flags & Document.FLAG_VIRTUAL_DOCUMENT) != 0;
    };

    private static final Predicate<Cursor> ANY_FILE_FILTER = (Cursor c) -> true;

    private static final String TAG = "Model";
+12 −7
Original line number Diff line number Diff line
@@ -291,23 +291,21 @@ public class ActionHandler<T extends Activity & Addons> extends AbstractActionHa

        // Model must be accessed in UI thread, since underlying cursor is not threadsafe.
        List<DocumentInfo> docs =
                mScope.model.loadDocuments(selection, Model.CONCRETE_FILE_FILTER);
                mScope.model.loadDocuments(selection, Model.NO_PARTIAL_NOR_ARCHIVED_FILE_FILTER);

        List<DocumentInfo> virtualDocs =
                mScope.model.loadDocuments(selection, Model.VIRTUAL_DOCUMENT_FILTER);

        Intent intent;

        if (docs.size() == 1) {
            final DocumentInfo doc = docs.get(0);

            intent = new Intent(Intent.ACTION_SEND);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            DocumentInfo doc = docs.get(0);
            intent.setType(doc.mimeType);
            intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);

        } else if (docs.size() > 1) {
            intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addCategory(Intent.CATEGORY_DEFAULT);

            final ArrayList<String> mimeTypes = new ArrayList<>();
            final ArrayList<Uri> uris = new ArrayList<>();
@@ -320,9 +318,16 @@ public class ActionHandler<T extends Activity & Addons> extends AbstractActionHa
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

        } else {
            // Everything filtered out, nothing to share.
            return;
        }

        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        if (virtualDocs.size() > 0) {
            intent.addCategory(Intent.CATEGORY_TYPED_OPENABLE);
        }

        Intent chooserIntent = Intent.createChooser(
                intent, mActivity.getResources().getText(R.string.share_via));

+40 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static com.android.documentsui.testing.IntentAsserts.assertHasExtraUri;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import android.content.Intent;
@@ -35,6 +36,7 @@ import android.support.test.runner.AndroidJUnit4;

import com.android.documentsui.R;
import com.android.documentsui.TestActionModeAddons;
import com.android.documentsui.archives.ArchivesProvider;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.DocumentStack;
import com.android.documentsui.base.RootInfo;
@@ -172,9 +174,25 @@ public class ActionHandlerTest {

        Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
        assertHasAction(intent, Intent.ACTION_SEND);
        assertFalse(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
        assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
        assertHasExtraUri(intent, Intent.EXTRA_STREAM);
    }

    @Test
    public void testShareSelectedDocuments_ArchivedFile() {
        mEnv = TestEnv.create(ArchivesProvider.AUTHORITY);
        mHandler.reset(mEnv.model, false);

        mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
        mEnv.selectionMgr.clearSelection();
        mEnv.selectDocument(TestEnv.FILE_PDF);
        mHandler.shareSelectedDocuments();

        Intent intent = mActivity.startActivity.getLastValue();
        assertNull(intent);
    }

    @Test
    public void testShareSelectedDocuments_Multiple() {
        mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
@@ -183,20 +201,39 @@ public class ActionHandlerTest {

        Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
        assertHasAction(intent, Intent.ACTION_SEND_MULTIPLE);
        assertFalse(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
        assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
        assertHasExtraList(intent, Intent.EXTRA_STREAM, 2);
    }

    @Test
    public void testShareSelectedDocuments_OmitsVirtualFiles() {
    public void testShareSelectedDocuments_VirtualFiles() {
        mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
        mEnv.selectionMgr.clearSelection();
        mEnv.selectDocument(TestEnv.FILE_VIRTUAL);
        mHandler.shareSelectedDocuments();

        Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
        assertHasAction(intent, Intent.ACTION_SEND);
        assertTrue(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
        assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
        assertHasExtraUri(intent, Intent.EXTRA_STREAM);
    }

    @Test
    public void testShareSelectedDocuments_RegularAndVirtualFiles() {
        mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
        mEnv.selectDocument(TestEnv.FILE_PNG);
        mEnv.selectDocument(TestEnv.FILE_VIRTUAL);
        mHandler.shareSelectedDocuments();

        Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
        assertHasAction(intent, Intent.ACTION_SEND_MULTIPLE);
        assertTrue(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
        assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
        assertHasExtraList(intent, Intent.EXTRA_STREAM, 3);
    }

    @Test
    public void testShareSelectedDocuments_OmitsPartialFiles() {
        mActivity.resources.strings.put(R.string.share_via, "Sharezilla!");
@@ -206,6 +243,8 @@ public class ActionHandlerTest {

        Intent intent = assertHasExtraIntent(mActivity.startActivity.getLastValue());
        assertHasAction(intent, Intent.ACTION_SEND_MULTIPLE);
        assertFalse(intent.hasCategory(Intent.CATEGORY_TYPED_OPENABLE));
        assertFalse(intent.hasCategory(Intent.CATEGORY_OPENABLE));
        assertHasExtraList(intent, Intent.EXTRA_STREAM, 2);
    }