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

Commit b7046c1f authored by Kelvin Kwan's avatar Kelvin Kwan
Browse files

DocsUI to return correct tree uri on cross profile document

This is a preparation to enable cross-profile intent handling
for OPEN_DOCUMENT_TREE

Bug: 149773665
Test: atest DocumentsUIGoogleTests
Change-Id: I8ed7b3d4efa24141fd0ea1abe27a70bf451fe1d1
parent 218e63b7
Loading
Loading
Loading
Loading
+13 −1
Original line number Original line 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
     * information. Use this when uri is needed externally. For usage within DocsUI, use
     * {@link #derivedUri}.
     * {@link #derivedUri}.
     */
     */
@@ -342,6 +342,18 @@ public class DocumentInfo implements Durable, Parcelable {
        return userId.buildDocumentUriAsUser(authority, documentId);
        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
    @Override
    public int hashCode() {
    public int hashCode() {
        return userId.hashCode() + derivedUri.hashCode() + mimeType.hashCode();
        return userId.hashCode() + derivedUri.hashCode() + mimeType.hashCode();
+12 −0
Original line number Original line Diff line number Diff line
@@ -166,6 +166,18 @@ public final class UserId {
        return DocumentsContract.buildDocumentUriAsUser(authority, documentId, mUserHandle);
        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
     * Starts activity for this user
     */
     */
+2 −4
Original line number Original line Diff line number Diff line
@@ -22,7 +22,6 @@ import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface;
import android.net.Uri;
import android.net.Uri;
import android.os.Bundle;
import android.os.Bundle;
import android.provider.DocumentsContract;


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


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


package com.android.documentsui.base;
package com.android.documentsui.base;


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

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


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


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


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


        assertThat(mimeTypes.contains("text/plain")).isTrue();
        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());
        }
    }
}
}