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

Commit 3877928f authored by Joe Antonetti's avatar Joe Antonetti
Browse files

Create RemoteTaskAddedMessage

Flag: android.companion.enable_task_continuity
Test: Added unit tests
Bug: 400970610
Change-Id: Icc7073829882fe4ad8303497315933c40cfc0fb1
parent bfbdf101
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ option java_multiple_files = true;
message TaskContinuityMessage {
    oneof data {
        ContinuityDeviceConnected device_connected = 1;
        RemoteTaskAddedMessage remote_task_added_message = 2;
    }
}

@@ -32,6 +33,10 @@ message ContinuityDeviceConnected {
    repeated RemoteTaskInfo remoteTasks = 2;
}

message RemoteTaskAddedMessage {
    RemoteTaskInfo task = 1;
}

message RemoteTaskInfo {
    int32 id = 1;
    string label = 2;
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.companion.datatransfer.continuity.messages;

import com.android.server.companion.datatransfer.continuity.messages.RemoteTaskInfo;

import android.util.proto.ProtoInputStream;
import android.util.proto.ProtoOutputStream;

import java.io.IOException;

/**
 * Deserialized version of the {@link RemoteTaskAdded} proto.
 */
public class RemoteTaskAddedMessage implements TaskContinuityMessageData {

    private RemoteTaskInfo mTask;

    public RemoteTaskAddedMessage(RemoteTaskInfo task) {
        mTask = task;
    }

    RemoteTaskAddedMessage(ProtoInputStream pis) throws IOException {
        while (pis.nextField() != ProtoInputStream.NO_MORE_FIELDS) {
            switch (pis.getFieldNumber()) {
                case (int) android.companion.RemoteTaskAddedMessage.TASK:
                    final long taskToken = pis.start(
                        android.companion.RemoteTaskAddedMessage.TASK);
                    mTask = new RemoteTaskInfo(pis);
                    pis.end(taskToken);
                    break;
            }
        }
    }

    @Override
    public void writeToProto(ProtoOutputStream pos) {
        long taskToken = pos.start(
            android.companion.RemoteTaskAddedMessage.TASK);

        mTask.writeToProto(pos);
        pos.end(taskToken);
    }

    public RemoteTaskInfo getTask() {
        return mTask;
    }
}
 No newline at end of file
+18 −2
Original line number Diff line number Diff line
@@ -48,6 +48,14 @@ public final class TaskContinuityMessage {
                    mData = new ContinuityDeviceConnected(pis);
                    pis.end(deviceConnectedToken);
                    break;
                case (int) android.companion.TaskContinuityMessage.REMOTE_TASK_ADDED_MESSAGE:
                    final long remoteTaskAddedMessageToken = pis.start(
                        android.companion.TaskContinuityMessage.REMOTE_TASK_ADDED_MESSAGE
                    );

                    mData = new RemoteTaskAddedMessage(pis);
                    pis.end(remoteTaskAddedMessageToken);
                    break;
            }
        }
    }
@@ -66,12 +74,20 @@ public final class TaskContinuityMessage {
        ProtoOutputStream pos = new ProtoOutputStream();
        switch (mData) {
            case ContinuityDeviceConnected continuityDeviceConnected:
                long token = pos.start(
                long continutyDeviceConnectedToken = pos.start(
                    android.companion.TaskContinuityMessage.DEVICE_CONNECTED
                );

                continuityDeviceConnected.writeToProto(pos);
                pos.end(token);
                pos.end(continutyDeviceConnectedToken);
                break;
            case RemoteTaskAddedMessage remoteTaskAddedMessage:
                long remoteTaskAddedMessageToken = pos.start(
                    android.companion.TaskContinuityMessage.REMOTE_TASK_ADDED_MESSAGE
                );

                remoteTaskAddedMessage.writeToProto(pos);
                pos.end(remoteTaskAddedMessageToken);
                break;
            default:
                break;
+96 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.companion.datatransfer.continuity.messages;


import static com.google.common.truth.Truth.assertThat;
import static org.testng.Assert.expectThrows;

import android.app.ActivityManager;
import android.platform.test.annotations.Presubmit;
import android.testing.AndroidTestingRunner;
import android.util.proto.ProtoInputStream;
import android.util.proto.ProtoOutputStream;
import android.util.proto.ProtoParseException;

import com.android.server.companion.datatransfer.continuity.messages.RemoteTaskAddedMessage;
import com.android.server.companion.datatransfer.continuity.messages.RemoteTaskInfo;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;

@Presubmit
@RunWith(AndroidTestingRunner.class)
public class RemoteTaskAddedMessageTest {

    @Test
    public void testConstructor_fromObjects() {
        RemoteTaskInfo expected = createNewRemoteTaskInfo("label", 0);

        RemoteTaskAddedMessage remoteTaskAddedMessage
            = new RemoteTaskAddedMessage(expected);

        assertRemoteTaskInfoEqual(expected, remoteTaskAddedMessage.getTask());
    }

    @Test
    public void testConstructor_fromProto_hasTask() throws IOException {
        final RemoteTaskInfo expected = createNewRemoteTaskInfo("label", 0);
        final ProtoOutputStream pos = new ProtoOutputStream();
        final long taskToken = pos.start(android.companion.RemoteTaskAddedMessage.TASK);
        expected.writeToProto(pos);
        pos.end(taskToken);
        pos.flush();

        ProtoInputStream pis = new ProtoInputStream(pos.getBytes());
        RemoteTaskAddedMessage remoteTaskAddedMessage
            = new RemoteTaskAddedMessage(pis);

        assertRemoteTaskInfoEqual(expected, remoteTaskAddedMessage.getTask());
    }

    @Test
    public void testWriteAndRead_roundTrip_works() throws IOException {
        RemoteTaskAddedMessage expected
            = new RemoteTaskAddedMessage(createNewRemoteTaskInfo("label", 0));

        final ProtoOutputStream pos = new ProtoOutputStream();
        expected.writeToProto(pos);
        pos.flush();

        final ProtoInputStream pis = new ProtoInputStream(pos.getBytes());
        final RemoteTaskAddedMessage actual = new RemoteTaskAddedMessage(pis);

        assertRemoteTaskInfoEqual(expected.getTask(), actual.getTask());
    }

    private RemoteTaskInfo createNewRemoteTaskInfo(String label, long lastUsedTimeMillis) {
        ActivityManager.RunningTaskInfo runningTaskInfo = new ActivityManager.RunningTaskInfo();
        runningTaskInfo.taskId = 1;
        runningTaskInfo.taskDescription = new ActivityManager.TaskDescription(label);
        runningTaskInfo.lastActiveTime = lastUsedTimeMillis;
        return new RemoteTaskInfo(runningTaskInfo);
    }

    private void assertRemoteTaskInfoEqual(RemoteTaskInfo expected, RemoteTaskInfo actual) {
        assertThat(actual.getId()).isEqualTo(expected.getId());
        assertThat(actual.getLabel()).isEqualTo(expected.getLabel());
        assertThat(actual.getLastUsedTimeMillis()).isEqualTo(expected.getLastUsedTimeMillis());
    }
}
 No newline at end of file