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

Commit 901d44aa authored by Kelvin Kwan's avatar Kelvin Kwan Committed by Automerger Merge Worker
Browse files

Merge "DocsUI to return correct tree uri on cross profile document" into rvc-dev am: 5e0125d6

Change-Id: I5eec9a286ec515bb5ab3bce230f4831cd6a579f8
parents 3523104b 5e0125d6
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -331,7 +331,7 @@ public class DocumentInfo implements Durable, Parcelable {
    }

    /**
     * Returns a document uri representing this {@link DocumentInfo}. The URI contains user
     * Returns a document uri representing this {@link DocumentInfo}. The URI may contain user
     * information. Use this when uri is needed externally. For usage within DocsUI, use
     * {@link #derivedUri}.
     */
@@ -342,6 +342,18 @@ public class DocumentInfo implements Durable, Parcelable {
        return userId.buildDocumentUriAsUser(authority, documentId);
    }


    /**
     * Returns a tree document uri representing this {@link DocumentInfo}. The URI may contain user
     * information. Use this when uri is needed externally.
     */
    public Uri getTreeDocumentUri() {
        if (UserId.CURRENT_USER.equals(userId)) {
            return DocumentsContract.buildTreeDocumentUri(authority, documentId);
        }
        return userId.buildTreeDocumentUriAsUser(authority, documentId);
    }

    @Override
    public int hashCode() {
        return userId.hashCode() + derivedUri.hashCode() + mimeType.hashCode();
+12 −0
Original line number Diff line number Diff line
@@ -166,6 +166,18 @@ public final class UserId {
        return DocumentsContract.buildDocumentUriAsUser(authority, documentId, mUserHandle);
    }

    /**
     * Returns a tree document uri representing this user.
     */
    public Uri buildTreeDocumentUriAsUser(String authority, String documentId) {
        String authorityWithUserInfo = buildDocumentUriAsUser(authority, documentId).getAuthority();
        Uri treeUri = DocumentsContract.buildTreeDocumentUri(authority, documentId);

        return treeUri.buildUpon()
                .encodedAuthority(authorityWithUserInfo)
                .build();
    }

    /**
     * Starts activity for this user
     */
+2 −4
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.app.Dialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.DocumentsContract;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
@@ -81,8 +80,7 @@ public class ConfirmFragment extends DialogFragment {
                        });
                break;
            case TYPE_OEPN_TREE:
                final Uri uri = DocumentsContract.buildTreeDocumentUri(
                        mTarget.authority, mTarget.documentId);
                final Uri treeUri = mTarget.getTreeDocumentUri();
                final BaseActivity activity = (BaseActivity) getActivity();
                final String target = activity.getCurrentTitle();
                final String text = getString(R.string.open_tree_dialog_title,
@@ -96,7 +94,7 @@ public class ConfirmFragment extends DialogFragment {
                        R.string.allow,
                        (DialogInterface dialog, int id) -> {
                            pickResult.increaseActionCount();
                            mActions.finishPicking(uri);
                            mActions.finishPicking(treeUri);
                        });
                break;

+57 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.documentsui.base;

import static androidx.core.util.Preconditions.checkArgument;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.mock;
@@ -31,6 +33,8 @@ import android.test.suitebuilder.annotation.SmallTest;
import androidx.test.rule.provider.ProviderTestRule;

import com.android.documentsui.InspectorProvider;
import com.android.documentsui.testing.TestProvidersAccess;
import com.android.documentsui.util.VersionUtils;

import org.junit.Before;
import org.junit.Rule;
@@ -145,4 +149,57 @@ public class DocumentInfoTest extends AndroidTestCase {

        assertThat(mimeTypes.contains("text/plain")).isTrue();
    }

    @Test
    public void testGetTreeDocumentUri_currentUser() {
        checkArgument(UserId.CURRENT_USER.equals(TEST_DOC.userId));

        assertThat(TEST_DOC.getTreeDocumentUri())
                .isEqualTo(DocumentsContract.buildTreeDocumentUri(TEST_DOC.authority,
                        TEST_DOC.documentId));
    }

    @Test
    public void testGetTreeDocumentUri_otherUser_shouldHaveDifferentUri() {
        if (VersionUtils.isAtLeastR()) {
            final DocumentInfo doc = createDocInfo("authority.a", "doc.1", "text/plain");
            final DocumentInfo otherUserDoc = createDocInfo("authority.a", "doc.1", "text/plain");
            otherUserDoc.userId = TestProvidersAccess.OtherUser.USER_ID;

            // Make sure they do not return the same tree uri
            assertThat(otherUserDoc.getTreeDocumentUri()).isNotEqualTo(doc.getTreeDocumentUri());
        }
    }

    @Test
    public void testGetTreeDocumentUri_otherUser_sameHostAndPath() {
        if (VersionUtils.isAtLeastR()) {
            final DocumentInfo doc = createDocInfo("authority.a", "doc.1", "text/plain");
            final DocumentInfo otherUserDoc = createDocInfo("authority.a", "doc.1", "text/plain");
            otherUserDoc.userId = TestProvidersAccess.OtherUser.USER_ID;

            // They should have same host(authority without user info) and path
            assertThat(otherUserDoc.getTreeDocumentUri().getHost())
                    .isEqualTo(doc.getTreeDocumentUri().getHost());
            assertThat(otherUserDoc.getTreeDocumentUri().getPath())
                    .isEqualTo(doc.getTreeDocumentUri().getPath());
        }
    }

    @Test
    public void testGetTreeDocumentUri_otherUser_userInfo() {
        if (VersionUtils.isAtLeastR()) {
            final DocumentInfo doc = createDocInfo("authority.a", "doc.1", "text/plain");
            final DocumentInfo otherUserDoc = createDocInfo("authority.a", "doc.1", "text/plain");
            otherUserDoc.userId = TestProvidersAccess.OtherUser.USER_ID;

            // Different user info between doc and otherUserDoc
            assertThat(otherUserDoc.getTreeDocumentUri().getUserInfo())
                    .isNotEqualTo(doc.getTreeDocumentUri().getUserInfo());

            // Same user info within otherUserDoc
            assertThat(otherUserDoc.getTreeDocumentUri().getUserInfo())
                    .isEqualTo(otherUserDoc.getDocumentUri().getUserInfo());
        }
    }
}