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

Commit ec40c5c0 authored by felkachang's avatar felkachang Committed by Felka Chang
Browse files

fix crashes by the specified ACTION_VIEW intent

The intent with ACTION_VIEW and authority
com.android.externalstorage.documents: will make DocumentsUI crash.
The root cause is that the intent will produce the follwoing Uri.
content://com.android.externalstorage.documents/document/primary
The path segment 0 is "document" and not equal with "root" so
DocumentsContract.getRootId throw the IllegalArgumentException.

The solution is to check whether the uri is the root uri by using
try catch IllegalArgumentException when it is calling
DocumentsContract.getRootId.

To add testcase for testing the solution.

Fixes: 69887605
Test: atest DocumentsUITests

Change-Id: I9ac4dd20d56b0cfb2ad71df487ec26de8168e6b0
parent 0af43022
Loading
Loading
Loading
Loading
+19 −2
Original line number Diff line number Diff line
@@ -756,7 +756,11 @@ public final class Metrics {

        switch (uri.getAuthority()) {
            case Providers.AUTHORITY_MEDIA:
                switch (DocumentsContract.getRootId(uri)) {
                String rootId = getRootIdSafely(uri);
                if (rootId == null) {
                    return ROOT_NONE;
                }
                switch (rootId) {
                    case Providers.ROOT_ID_AUDIO:
                        return ROOT_AUDIO;
                    case Providers.ROOT_ID_IMAGES:
@@ -767,7 +771,11 @@ public final class Metrics {
                        return ROOT_OTHER;
                }
            case Providers.AUTHORITY_STORAGE:
                if (Providers.ROOT_ID_HOME.equals(DocumentsContract.getRootId(uri))) {
                rootId = getRootIdSafely(uri);
                if (rootId == null) {
                    return ROOT_NONE;
                }
                if (Providers.ROOT_ID_HOME.equals(rootId)) {
                    return ROOT_HOME;
                } else {
                    return ROOT_DEVICE_STORAGE;
@@ -971,4 +979,13 @@ public final class Metrics {
        int systemProvider;
        int externalProvider;
    }

    private static String getRootIdSafely(Uri uri) {
        try {
            return DocumentsContract.getRootId(uri);
        } catch (IllegalArgumentException iae) {
            Log.w(TAG, "Invalid root Uri " + uri.toSafeString());
        }
        return null;
    }
}
+43 −0
Original line number Diff line number Diff line
package com.android.documentsui;

import static junit.framework.Assert.fail;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import com.android.documentsui.base.Providers;
import com.android.documentsui.base.State;

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

/**
 * Tests for the specialized behaviors provided by Metrics.
 */
@RunWith(AndroidJUnit4.class)
@SmallTest
public class MetricsTest {
    @Test
    public void logActivityLaunch_storageAuthority_shouldNotCrash() {
        final Intent intent = new Intent(null, Uri.parse(
                "content://" + Providers.AUTHORITY_STORAGE + "/document/primary:"));
        final State state = new State();
        state.action = State.ACTION_BROWSE;
        Metrics.logActivityLaunch(InstrumentationRegistry.getTargetContext(),
                state, intent);
    }

    @Test
    public void logActivityLaunch_mediaAuthority_shouldNotCrash() {
        final Intent intent = new Intent(null, Uri.parse(
                "content://" + Providers.AUTHORITY_MEDIA + "/document/primary:"));
        final State state = new State();
        state.action = State.ACTION_BROWSE;
        Metrics.logActivityLaunch(InstrumentationRegistry.getTargetContext(),
                state, intent);
    }
}