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

Commit 1411fe55 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "API for app launch on computer control sessions" into main

parents 371de440 402f4f89
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -130,6 +130,22 @@ 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>
     *
     * @see ComputerControlSessionParams#getTargetPackageNames()
     */
    public void launchApplication(@NonNull String packageName) {
        try {
            mSession.launchApplication(packageName);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Screenshot the current display content.
     *
+28 −0
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.view.Surface;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
@@ -33,6 +35,7 @@ import java.util.Objects;
public final class ComputerControlSessionParams implements Parcelable {

    private final String mName;
    private final List<String> mTargetPackageNames;
    private final int mDisplayWidthPx;
    private final int mDisplayHeightPx;
    private final int mDisplayDpi;
@@ -41,12 +44,14 @@ public final class ComputerControlSessionParams implements Parcelable {

    private ComputerControlSessionParams(
            @NonNull String name,
            @Nullable List<String> targetPackageNames,  // TODO(b/437849228): Should be non-null
            int displayWidthPx,
            int displayHeightPx,
            int displayDpi,
            @Nullable Surface displaySurface,
            boolean isDisplayAlwaysUnlocked) {
        mName = name;
        mTargetPackageNames = targetPackageNames;
        mDisplayWidthPx = displayWidthPx;
        mDisplayHeightPx = displayHeightPx;
        mDisplayDpi = displayDpi;
@@ -56,6 +61,8 @@ public final class ComputerControlSessionParams implements Parcelable {

    private ComputerControlSessionParams(Parcel parcel) {
        mName = parcel.readString8();
        mTargetPackageNames = new ArrayList<>();
        parcel.readStringList(mTargetPackageNames);
        mDisplayWidthPx = parcel.readInt();
        mDisplayHeightPx = parcel.readInt();
        mDisplayDpi = parcel.readInt();
@@ -69,6 +76,12 @@ public final class ComputerControlSessionParams implements Parcelable {
        return mName;
    }

    /** Returns the package names of the applications that can be automated during this session. */
    @Nullable  // TODO(b/437849228): Should be non-null
    public List<String> getTargetPackageNames() {
        return mTargetPackageNames;
    }

    /** Returns the width of the display, in pixels. */
    public int getDisplayWidthPx() {
        return mDisplayWidthPx;
@@ -103,6 +116,7 @@ public final class ComputerControlSessionParams implements Parcelable {
    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeString8(mName);
        dest.writeStringList(mTargetPackageNames);
        dest.writeInt(mDisplayWidthPx);
        dest.writeInt(mDisplayHeightPx);
        dest.writeInt(mDisplayDpi);
@@ -128,6 +142,7 @@ public final class ComputerControlSessionParams implements Parcelable {
    /** Builder for {@link ComputerControlSessionParams}. */
    public static final class Builder {
        private String mName;
        private List<String> mTargetPackageNames;
        private int mDisplayWidthPx;
        private int mDisplayHeightPx;
        private int mDisplayDpi;
@@ -149,6 +164,17 @@ public final class ComputerControlSessionParams implements Parcelable {
            return this;
        }


        /**
         * Set the package names of all applications that may be automated during this session.
         */
        @Nullable  // TODO(b/437849228): Should be non-null
        public Builder setTargetPackageNames(@NonNull List<String> targetPackageNames) {
            // TODO(b/437849228): Check for null and non-empty
            mTargetPackageNames = targetPackageNames;
            return this;
        }

        /**
         * Sets the width of the display, in pixels.
         *
@@ -230,6 +256,7 @@ public final class ComputerControlSessionParams implements Parcelable {
            if (mName == null || mName.isEmpty()) {
                throw new IllegalArgumentException("Name must be set");
            }
            // TODO(b/437849228): Do not allow for unset targetPackageNames
            if (mDisplaySurface != null) {
                if (mDisplayWidthPx <= 0) {
                    throw new IllegalArgumentException(
@@ -246,6 +273,7 @@ public final class ComputerControlSessionParams implements Parcelable {
            }
            return new ComputerControlSessionParams(
                    mName,
                    mTargetPackageNames,
                    mDisplayWidthPx,
                    mDisplayHeightPx,
                    mDisplayDpi,
+3 −0
Original line number Diff line number Diff line
@@ -28,6 +28,9 @@ import android.view.Surface;
 */
interface IComputerControlSession {

    /** Launches an application on the trusted virtual display. */
    void launchApplication(in String packageName);

    /* Injects a tap event into the trusted virtual display. */
    void tap(float x, float y);

+10 −0
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

@RunWith(AndroidJUnit4.class)
public class ComputerControlSessionParamsTest {

@@ -37,6 +39,10 @@ public class ComputerControlSessionParamsTest {
    private static final int DISPLAY_WIDTH = 1920;
    private static final int DISPLAY_HEIGHT = 1080;
    private static final int DISPLAY_DPI = 480;
    private static final String TARGET_PACKAGE_1 = "com.android.foo";
    private static final String TARGET_PACKAGE_2 = "com.android.bar";
    private static final List<String> TARGET_PACKAGE_NAMES =
            List.of(TARGET_PACKAGE_1, TARGET_PACKAGE_2);

    private Surface mSurface;

@@ -49,6 +55,7 @@ public class ComputerControlSessionParamsTest {
    public void parcelable_shouldRecreateSuccessfully() {
        ComputerControlSessionParams originalParams = new ComputerControlSessionParams.Builder()
                .setName(COMPUTER_CONTROL_SESSION_NAME)
                .setTargetPackageNames(TARGET_PACKAGE_NAMES)
                .setDisplayWidthPx(DISPLAY_WIDTH)
                .setDisplayHeightPx(DISPLAY_HEIGHT)
                .setDisplayDpi(DISPLAY_DPI)
@@ -61,6 +68,7 @@ public class ComputerControlSessionParamsTest {
        ComputerControlSessionParams params =
                ComputerControlSessionParams.CREATOR.createFromParcel(parcel);
        assertThat(params.getName()).isEqualTo(COMPUTER_CONTROL_SESSION_NAME);
        assertThat(params.getTargetPackageNames()).containsExactlyElementsIn(TARGET_PACKAGE_NAMES);
        assertThat(params.getDisplayWidthPx()).isEqualTo(DISPLAY_WIDTH);
        assertThat(params.getDisplayHeightPx()).isEqualTo(DISPLAY_HEIGHT);
        assertThat(params.getDisplayDpi()).isEqualTo(DISPLAY_DPI);
@@ -77,6 +85,8 @@ public class ComputerControlSessionParamsTest {
                        .build());
    }

    // TODO(b/437849228): Test that targetPackageNames must be set.

    @Test
    public void build_setSurface_nonPositiveDisplayDimensions_throwsException() {
        assertThrows(IllegalArgumentException.class,
+7 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ public class ComputerControlSessionTest {
    private static final int DISPLAY_ID = 42;
    private static final int WIDTH = 1920;
    private static final int HEIGHT = 1080;
    private static final String TARGET_PACKAGE = "com.android.foo";

    @Mock
    private IComputerControlSession mMockSession;
@@ -128,6 +129,12 @@ public class ComputerControlSessionTest {
        verify(mMockSession).close();
    }

    @Test
    public void launchApplication_launchesApplication() throws RemoteException {
        mSession.launchApplication(TARGET_PACKAGE);
        verify(mMockSession).launchApplication(eq(TARGET_PACKAGE));
    }

    @Test
    public void tap_taps() throws RemoteException {
        mSession.tap(0.1f, 0.2f);
Loading