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

Commit b89d24b4 authored by Joe Antonetti's avatar Joe Antonetti
Browse files

Create HandoffRequestMessage

Flag: android.companion.enable_task_continuity
Test: Added unit tests
Bug: 400970610
Change-Id: I47eef0106fdd195794e4ae6978969e25d42d2831
parent 5de8f058
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ message TaskContinuityMessage {
        ContinuityDeviceConnected device_connected = 1;
        RemoteTaskAddedMessage remote_task_added_message = 2;
        RemoteTaskRemovedMessage remote_task_removed = 3;
        HandoffRequestMessage handoff_request = 4;
    }
}

@@ -38,6 +39,10 @@ message RemoteTaskAddedMessage {
    RemoteTaskInfo task = 1;
}

message HandoffRequestMessage {
    int32 task_id = 1;
}

message RemoteTaskInfo {
    int32 id = 1;
    string label = 2;
+46 −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 android.util.proto.ProtoInputStream;
import android.util.proto.ProtoOutputStream;

import java.io.IOException;

/**
 * Deserialized version of the HandoffRequestMessage proto.
 */
public record HandoffRequestMessage(int taskId) implements TaskContinuityMessageData {

    public static HandoffRequestMessage readFromProto(ProtoInputStream pis) throws IOException {
        int taskId = 0;
        while (pis.nextField() != ProtoInputStream.NO_MORE_FIELDS) {
            switch (pis.getFieldNumber()) {
                case (int) android.companion.HandoffRequestMessage.TASK_ID:
                    taskId = pis.readInt(android.companion.HandoffRequestMessage.TASK_ID);
                    break;
            }
        }

        return new HandoffRequestMessage(taskId);
    }

    @Override
    public void writeToProto(ProtoOutputStream pos) {
        pos.write(android.companion.HandoffRequestMessage.TASK_ID, taskId());
    }
}
 No newline at end of file
+77 −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 android.platform.test.annotations.Presubmit;
import android.testing.AndroidTestingRunner;
import android.util.proto.ProtoInputStream;
import android.util.proto.ProtoOutputStream;

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

import java.io.IOException;

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

    @Test
    public void testConstructor_fromTaskId() {
        int taskId = 1;
        HandoffRequestMessage handoffRequestMessage = new HandoffRequestMessage(taskId);
        assertThat(handoffRequestMessage.taskId()).isEqualTo(taskId);
    }

    @Test
    public void testConstructor_fromProto() throws IOException {
        int taskId = 1;
        final ProtoOutputStream pos = new ProtoOutputStream();
        pos.write(android.companion.HandoffRequestMessage.TASK_ID, taskId);
        pos.flush();

        ProtoInputStream pis = new ProtoInputStream(pos.getBytes());
        HandoffRequestMessage handoffRequestMessage = HandoffRequestMessage.readFromProto(pis);

        assertThat(handoffRequestMessage.taskId()).isEqualTo(taskId);
    }

    @Test
    public void testConstructor_fromProto_noTaskId_returnsZero() throws IOException {
        final ProtoOutputStream pos = new ProtoOutputStream();
        pos.flush();
        ProtoInputStream pis = new ProtoInputStream(pos.getBytes());
        HandoffRequestMessage handoffRequestMessage = HandoffRequestMessage.readFromProto(pis);
        assertThat(handoffRequestMessage.taskId()).isEqualTo(0);
    }

    @Test
    public void testWriteAndRead_roundTrip_works() throws IOException {
        HandoffRequestMessage expectedMessage = new HandoffRequestMessage(1);

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

        final ProtoInputStream pis = new ProtoInputStream(pos.getBytes());
        final HandoffRequestMessage actualMessage = HandoffRequestMessage.readFromProto(pis);

        assertThat(actualMessage).isEqualTo(expectedMessage);
    }
}
 No newline at end of file