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

Commit f9206c9e authored by Erfan Norozi's avatar Erfan Norozi
Browse files

On desktop, open files in a new window

The desktop behaviour relies on `documentLaunchMode` which is implicitly
enabled in all apps unless they override it in their manifest. Apps that
set `documentLaunchMode="never"` will still open in a new window but
will have a weird behaviour if two documents are opened using the same
app (i.e. opening the second document will bring the app window to the
front but the app will still display the first document). See
https://issuetracker.google.com/issues/373759691#comment3

Bug: 373759691
Flag: com.android.documentsui.flags.desktop_file_handling
Test: Unit tests and manual testing

Change-Id: I9d21364879e4f3de780b207365da9bac97ff47ce
parent 4eb69679
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.documentsui;
import static com.android.documentsui.base.DocumentInfo.getCursorInt;
import static com.android.documentsui.base.DocumentInfo.getCursorString;
import static com.android.documentsui.base.SharedMinimal.DEBUG;
import static com.android.documentsui.flags.Flags.desktopFileHandling;

import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
@@ -560,6 +561,15 @@ public abstract class AbstractActionHandler<T extends FragmentActivity & CommonA
        if (doc.isWriteSupported()) {
            flags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
        }
        // On desktop users expect files to open in a new window.
        if (desktopFileHandling()) {
            // The combination of NEW_DOCUMENT and MULTIPLE_TASK allows multiple instances of the
            // same activity to open in separate windows.
            flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
            // If the activity has documentLaunchMode="never", NEW_TASK forces the activity to still
            // open in a new window.
            flags |= Intent.FLAG_ACTIVITY_NEW_TASK;
        }
        intent.setFlags(flags);

        return intent;
+6 −4
Original line number Diff line number Diff line
@@ -25,12 +25,14 @@ java_defaults {
    ],

    static_libs: [
        "androidx.test.rules",
        "androidx.test.espresso.core",
        "androidx.test.ext.truth",
        "androidx.test.rules",
        "androidx.test.uiautomator_uiautomator",
        "docsui-flags-aconfig-java-lib",
        "flag-junit",
        "guava",
        "mockito-target",
        "androidx.test.uiautomator_uiautomator",
    ],
}

@@ -50,11 +52,11 @@ android_library {

    static_libs: [
        "androidx.legacy_legacy-support-v4",
        "androidx.test.rules",
        "androidx.test.espresso.core",
        "androidx.test.rules",
        "androidx.test.uiautomator_uiautomator",
        "mockito-target",
        "ub-janktesthelper",
        "androidx.test.uiautomator_uiautomator",
    ],
}

+35 −0
Original line number Diff line number Diff line
@@ -39,6 +39,9 @@ import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Parcelable;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Path;
import android.util.Pair;
@@ -59,6 +62,7 @@ import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.DocumentStack;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.Shared;
import com.android.documentsui.flags.Flags;
import com.android.documentsui.inspector.InspectorActivity;
import com.android.documentsui.testing.ClipDatas;
import com.android.documentsui.testing.DocumentStackAsserts;
@@ -78,6 +82,7 @@ import com.google.common.collect.Lists;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -103,6 +108,9 @@ public class ActionHandlerTest {
    private TestConfigStore mTestConfigStore;
    private boolean refreshAnswer = false;

    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Parameter(0)
    public boolean isPrivateSpaceEnabled;

@@ -156,6 +164,33 @@ public class ActionHandlerTest {
        assertEquals(expected.toString(), actual.toString());
    }

    @Test
    @DisableFlags({Flags.FLAG_DESKTOP_FILE_HANDLING})
    public void testOpenFileFlags() {
        mHandler.onDocumentOpened(TestEnv.FILE_GIF,
                com.android.documentsui.files.ActionHandler.VIEW_TYPE_PREVIEW,
                com.android.documentsui.files.ActionHandler.VIEW_TYPE_REGULAR, false);

        int expectedFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
        Intent actual = mActivity.startActivity.getLastValue();
        assertEquals(expectedFlags, actual.getFlags());
    }

    @Test
    @EnableFlags({Flags.FLAG_DESKTOP_FILE_HANDLING})
    public void testOpenFileFlagsDesktop() {
        mHandler.onDocumentOpened(TestEnv.FILE_GIF,
                com.android.documentsui.files.ActionHandler.VIEW_TYPE_PREVIEW,
                com.android.documentsui.files.ActionHandler.VIEW_TYPE_REGULAR, false);

        int expectedFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_DOCUMENT
                | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK;
        Intent actual = mActivity.startActivity.getLastValue();
        assertEquals(expectedFlags, actual.getFlags());
    }

    @Test
    public void testSpringOpenDirectory() {
        mHandler.springOpenDirectory(TestEnv.FOLDER_0);