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

Commit 0a956a61 authored by Vladimir Komsiyski's avatar Vladimir Komsiyski
Browse files

Allow launching applications not pre-declared in ComputerControlSession.

The `launchApplication` API in `ComputerControlSession` no longer requires the package name to be included in `getTargetPackageNames()` at session creation. Instead, when `computerControlActivityPolicyStrict` is enabled, the service dynamically adds an activity policy exemption for the launched package. This is a temporary change to support a broader range of applications until a per-app consent model is implemented. The documentation is updated to reflect that an `IllegalArgumentException` is thrown if the package lacks a launcher activity.

Bug: 444600407
Flag: android.companion.virtualdevice.flags.computer_control_activity_policy_strict
Test: presubmit
Change-Id: I6e87641075f205c4b2b39042f8b53febe9cee6d8
parent d3051712
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -159,9 +159,7 @@ public final class ComputerControlSession implements AutoCloseable {
    /**
     * Launches an application's launcher activity in the computer control session.
     *
     * <p>The application with the given package name must have a launcher activity and the
     * package name must have been declared during the session creation.</p>
     *
     * @throws IllegalArgumentException if the package does not have a launcher activity.
     * @see ComputerControlSessionParams#getTargetPackageNames()
     */
    public void launchApplication(@NonNull String packageName) {
+1 −3
Original line number Diff line number Diff line
@@ -105,9 +105,7 @@ public final class ComputerControlSession implements AutoCloseable {
    /**
     * Launches an application's launcher activity in the computer control session.
     *
     * <p>The application with the given package name must have a launcher activity and the
     * package name must have been included during the session creation.</p>
     *
     * @throws IllegalArgumentException if the package does not have a launcher activity.
     * @see Params#getTargetPackageNames()
     */
    public void launchApplication(@NonNull String packageName) {
+7 −4
Original line number Diff line number Diff line
@@ -320,10 +320,13 @@ final class ComputerControlSessionImpl extends IComputerControlSession.Stub
        return mOwnerPackageName;
    }

    public void launchApplication(@NonNull String packageName) {
        if (!mParams.getTargetPackageNames().contains(Objects.requireNonNull(packageName))) {
            throw new IllegalArgumentException(
                    "Package " + packageName + " is not allowed to be launched in this session.");
    @Override
    public void launchApplication(@NonNull String packageName) throws RemoteException {
        if (Flags.computerControlActivityPolicyStrict()) {
            // TODO(b/444600407): Remove this once the consent model is per-target app. While the
            // consent is general, the caller can extend the list of target packages dynamically.
            mVirtualDevice.addActivityPolicyExemption(
                    new ActivityPolicyExemption.Builder().setPackageName(packageName).build());
        }
        final UserHandle user = UserHandle.of(UserHandle.getUserId(Binder.getCallingUid()));
        Binder.withCleanCallingIdentity(() -> mInjector.launchApplicationOnDisplayAsUser(
+17 −4
Original line number Diff line number Diff line
@@ -271,7 +271,7 @@ public class ComputerControlSessionTest {
    }

    @Test
    public void launchApplication_launchesApplication() {
    public void launchApplication_launchesApplication() throws RemoteException {
        createComputerControlSession(mDefaultParams);
        mSession.launchApplication(TARGET_PACKAGE_1);
        verify(mInjector).launchApplicationOnDisplayAsUser(
@@ -279,10 +279,23 @@ public class ComputerControlSessionTest {
    }

    @Test
    public void launchApplication_undeclaredPackage_throws() {
    @DisableFlags(Flags.FLAG_COMPUTER_CONTROL_ACTIVITY_POLICY_STRICT)
    public void launchApplication_noActivityPolicy_launchesApplication() throws RemoteException {
        createComputerControlSession(mDefaultParams);
        assertThrows(IllegalArgumentException.class,
                () -> mSession.launchApplication(UNDECLARED_TARGET_PACKAGE));
        mSession.launchApplication(UNDECLARED_TARGET_PACKAGE);
        verify(mInjector).launchApplicationOnDisplayAsUser(
                eq(UNDECLARED_TARGET_PACKAGE), eq(VIRTUAL_DISPLAY_ID), any());
    }

    @Test
    @EnableFlags(Flags.FLAG_COMPUTER_CONTROL_ACTIVITY_POLICY_STRICT)
    public void launchApplication_strictActivityPolicy_addsExemption() throws RemoteException {
        createComputerControlSession(mDefaultParams);
        mSession.launchApplication(UNDECLARED_TARGET_PACKAGE);
        verify(mVirtualDevice).addActivityPolicyExemption(
                argThat(new MatchesActivityPolicyExcemption(UNDECLARED_TARGET_PACKAGE)));
        verify(mInjector).launchApplicationOnDisplayAsUser(
                eq(UNDECLARED_TARGET_PACKAGE), eq(VIRTUAL_DISPLAY_ID), any());
    }

    @Test