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

Commit 3b2ad11f authored by Steve McKay's avatar Steve McKay
Browse files

Enable explicit package for quick view intent.

Define the package in config...suitable for setup via overlay.
Undefined by default makes the feature disabled on phones...n stuff.
Fall back to single-file QUICK_VIEW.
Work around 24963914 (for now).

Bug: 24963914
Change-Id: Ia9c5a606d294ae053fd5e60d015f0aa0010eba4f
parent 5ff818e9
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -16,4 +16,6 @@

<resources>
    <bool name="productivity_device">true</bool>
    <!-- intentionally unset. Vendors should set this in an overlay. -->
    <string name="trusted_quick_viewer_package"></string>
</resources>
+19 −9
Original line number Diff line number Diff line
@@ -323,17 +323,26 @@ public class FilesActivity extends BaseActivity {
    private void openDocument(DocumentInfo doc, @Nullable DocumentContext siblings) {
        Intent intent = null;
        if (siblings != null) {
            QuickViewIntentBuilder builder =
                    new QuickViewIntentBuilder(getPackageManager(), doc, siblings);
            QuickViewIntentBuilder builder = new QuickViewIntentBuilder(
                    getPackageManager(), getResources(), doc, siblings);
            intent = builder.build();
        }

        if (intent != null) {
            // TODO: un-work around issue b/24963914. Should be fixed soon.
            try {
                startActivity(intent);
                return;
            } catch (SecurityException e) {
                // carry on to regular view mode.
                Log.e(TAG, "Caught security error: " + e.getLocalizedMessage());
            }
        }

        // fallback to traditional VIEW action...
        if (intent == null) {
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setData(doc.derivedUri);
        }

        if (DEBUG && intent.getClipData() != null) {
            Log.d(TAG, "Starting intent w/ clip data: " + intent.getClipData());
@@ -341,8 +350,9 @@ public class FilesActivity extends BaseActivity {

        try {
            startActivity(intent);
        } catch (ActivityNotFoundException ex2) {
            Snackbars.makeSnackbar(this, R.string.toast_no_application, Snackbar.LENGTH_SHORT).show();
        } catch (ActivityNotFoundException e) {
            Snackbars.makeSnackbar(
                    this, R.string.toast_no_application, Snackbar.LENGTH_SHORT).show();
        }
    }

+38 −17
Original line number Diff line number Diff line
@@ -25,11 +25,13 @@ import android.content.ClipDescription;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Document;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.android.documentsui.BaseActivity.DocumentContext;
@@ -43,13 +45,20 @@ final class QuickViewIntentBuilder {
    private final DocumentInfo mDocument;
    private final DocumentContext mContext;

    public ClipData mClipData;
    public int mDocumentLocation;
    private PackageManager mPkgManager;
    private final PackageManager mPkgManager;
    private final Resources mResources;

    private ClipData mClipData;
    private int mDocumentLocation;

    public QuickViewIntentBuilder(
            PackageManager pkgManager, DocumentInfo doc, DocumentContext context) {
            PackageManager pkgManager,
            Resources resources,
            DocumentInfo doc,
            DocumentContext context) {

        mPkgManager = pkgManager;
        mResources = resources;
        mDocument = doc;
        mContext = context;
    }
@@ -61,26 +70,38 @@ final class QuickViewIntentBuilder {
    @Nullable Intent build() {
        if (DEBUG) Log.d(TAG, "Preparing intent for doc:" + mDocument.documentId);

        String trustedPkg = mResources.getString(R.string.trusted_quick_viewer_package);

        Intent intent = new Intent(Intent.ACTION_QUICK_VIEW);
        intent.setDataAndType(mDocument.derivedUri, mDocument.mimeType);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        // Try to resolve the intent. If a matching app isn't installed, it won't resolve.
        ComponentName handler = intent.resolveActivity(mPkgManager);
        if (handler == null) {
            return null;
        if (TextUtils.isEmpty(trustedPkg)) {
            if (hasRegisteredHandler(intent)) {
                return intent;
            }

        } else {
            intent.setPackage(trustedPkg);
            if (hasRegisteredHandler(intent)) {
                // We have a trusted handler. Load all of the docs into the intent.
                Cursor cursor = mContext.getCursor();
                for (int i = 0; i < cursor.getCount(); i++) {
                    onNextItem(i, cursor);
                }

        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.putExtra(Intent.EXTRA_INDEX, mDocumentLocation);
                intent.setClipData(mClipData);

                return intent;
            }
        }

        return null;
    }

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

    private void onNextItem(int index, Cursor cursor) {
        cursor.moveToPosition(index);