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

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

Merge "Wire DocsUI w/ quick view feature flags." into arc-apps

parents f4ed293e f4e2b65c
Loading
Loading
Loading
Loading
+23 −5
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.android.documentsui.base.Shared.MAX_DOCS_IN_INTENT;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Intent;
import android.content.QuickViewConstants;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
@@ -57,6 +58,15 @@ public final class QuickViewIntentBuilder {
    public static final String IGNORE_DEBUG_PROP = "*disabled*";
    private static final String TAG = "QuickViewIntentBuilder";

    private static final String[] IN_ARCHIVE_FEATURES = {};
    private static final String[] FULL_FEATURES = {
            QuickViewConstants.FEATURE_VIEW,
            QuickViewConstants.FEATURE_EDIT,
            QuickViewConstants.FEATURE_SEND,
            QuickViewConstants.FEATURE_DOWNLOAD,
            QuickViewConstants.FEATURE_PRINT
    };

    private final DocumentInfo mDocument;
    private final Model mModel;

@@ -96,6 +106,8 @@ public final class QuickViewIntentBuilder {
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.setPackage(trustedPkg);
            if (hasRegisteredHandler(intent)) {
                includeQuickViewFeaturesFlag(intent, mDocument);

                final ArrayList<Uri> uris = new ArrayList<>();
                final int documentLocation = collectViewableUris(uris);
                final Range<Integer> range = computeSiblingsRange(uris, documentLocation);
@@ -196,6 +208,17 @@ public final class QuickViewIntentBuilder {
        return documentLocation;
    }

    private boolean hasRegisteredHandler(Intent intent) {
        // Try to resolve the intent. If a matching app isn't installed, it won't resolve.
        return intent.resolveActivity(mPackageMgr) != null;
    }

    private static void includeQuickViewFeaturesFlag(Intent intent, DocumentInfo doc) {
        intent.putExtra(
                Intent.EXTRA_QUICK_VIEW_FEATURES,
                doc.isInArchive() ? IN_ARCHIVE_FEATURES : FULL_FEATURES);
    }

    private static Range<Integer> computeSiblingsRange(List<Uri> uris, int documentLocation) {
        // Restrict number of siblings to avoid hitting the IPC limit.
        // TODO: Remove this restriction once ClipData can hold an arbitrary number of
@@ -215,9 +238,4 @@ public final class QuickViewIntentBuilder {

        return new Range(firstSibling, lastSibling);
    }

    private boolean hasRegisteredHandler(Intent intent) {
        // Try to resolve the intent. If a matching app isn't installed, it won't resolve.
        return intent.resolveActivity(mPackageMgr) != null;
    }
}
+69 −0
Original line number Diff line number Diff line
package com.android.documentsui.files;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

import android.content.Intent;
import android.content.QuickViewConstants;
import android.content.pm.PackageManager;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import com.android.documentsui.testing.TestEnv;
import com.android.documentsui.testing.TestPackageManager;
import com.android.documentsui.testing.TestResources;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class QuickViewIntentBuilderTest {

    private PackageManager mPm;
    private TestEnv mEnv;
    private TestResources mRes;

    @Before
    public void setUp() {
        mPm = TestPackageManager.create();
        mEnv = TestEnv.create();
        mRes = TestResources.create();

        mRes.setQuickViewerPackage("com.android.documentsui");
    }

    @Test
    public void testSetsNoFeatures_InArchiveDocument() {
        QuickViewIntentBuilder builder =
                new QuickViewIntentBuilder(mPm, mRes, TestEnv.FILE_IN_ARCHIVE, mEnv.archiveModel);

        Intent intent = builder.build();

        String[] features = intent.getStringArrayExtra(Intent.EXTRA_QUICK_VIEW_FEATURES);
        assertEquals(0, features.length);
    }

    @Test
    public void testSetsFullFeatures_RegularDocument() {
        QuickViewIntentBuilder builder =
                new QuickViewIntentBuilder(mPm, mRes, TestEnv.FILE_JPG, mEnv.model);

        Intent intent = builder.build();

        Set<String> features = new HashSet<>(
                Arrays.asList(intent.getStringArrayExtra(Intent.EXTRA_QUICK_VIEW_FEATURES)));

        assertEquals("Unexpected features set: " + features, 5, features.size());
        assertTrue(features.contains(QuickViewConstants.FEATURE_VIEW));
        assertTrue(features.contains(QuickViewConstants.FEATURE_EDIT));
        assertTrue(features.contains(QuickViewConstants.FEATURE_SEND));
        assertTrue(features.contains(QuickViewConstants.FEATURE_DOWNLOAD));
        assertTrue(features.contains(QuickViewConstants.FEATURE_PRINT));
    }
}