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

Commit 990e3950 authored by Joe Antonetti's avatar Joe Antonetti
Browse files

Process Incoming RemoteTaskRemovedMessages

Test: Added unit tests
Bug: 400970610
Flag: android.companion.enable_task_continuity
Change-Id: I9efc0f5777041b7c151037bbf4828c2c2fc32d7f
parent 9bb8be17
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.content.Context;
import android.util.Slog;

import com.android.server.companion.datatransfer.continuity.messages.ContinuityDeviceConnected;
import com.android.server.companion.datatransfer.continuity.messages.RemoteTaskRemovedMessage;
import com.android.server.companion.datatransfer.continuity.messages.TaskContinuityMessage;
import com.android.server.companion.datatransfer.continuity.tasks.RemoteTaskStore;

@@ -107,6 +108,11 @@ public final class TaskContinuityManagerService extends SystemService {
                    associationId,
                    continuityDeviceConnected.getRemoteTasks());
                break;
            case RemoteTaskRemovedMessage remoteTaskRemovedMessage:
                mRemoteTaskStore.removeTask(
                    associationId,
                    remoteTaskRemovedMessage.taskId());
                break;
            default:
                Slog.w(TAG, "Received unknown message from device: " + associationId);
                break;
+7 −0
Original line number Diff line number Diff line
@@ -69,6 +69,13 @@ class RemoteDeviceTaskList {
        mTasks.addAll(tasks);
    }

    /**
     * Removes a task from the list of tasks currently available on the remote device.
     */
    void removeTask(int taskId) {
        mTasks.removeIf(task -> task.getId() == taskId);
    }

    /**
     * Gets the most recently used task on this device, or null if there are no
     * tasks.
+10 −0
Original line number Diff line number Diff line
@@ -66,6 +66,16 @@ public class RemoteTaskStore implements ConnectedAssociationStore.Observer {
        }
    }

   public void removeTask(int associationId, int taskId) {
        synchronized (mRemoteDeviceTaskLists) {
            if (!mRemoteDeviceTaskLists.containsKey(associationId)) {
                return;
            }

            mRemoteDeviceTaskLists.get(associationId).removeTask(taskId);
        }
    }

    /**
     * Returns the most recent tasks from all devices in the task store.
     *
+18 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import static com.android.server.companion.datatransfer.continuity.TaskContinuityTestUtils.createRunningTaskInfo;

import android.app.ActivityManager;
import android.companion.datatransfer.continuity.RemoteTask;
import android.platform.test.annotations.Presubmit;
import android.testing.AndroidTestingRunner;

@@ -97,6 +98,23 @@ public class RemoteDeviceTaskListTest {
            .isEqualTo(expectedTask.toRemoteTask(ASSOCIATION_ID, DEVICE_NAME));
    }

    @Test
    public void testRemoveTask_removesTask() {
        RemoteTaskInfo mostRecentTaskInfo = createNewRemoteTaskInfo(1, "task2", 200);
        RemoteTask mostRecentTask = mostRecentTaskInfo.toRemoteTask(ASSOCIATION_ID, DEVICE_NAME);
        RemoteTaskInfo secondMostRecentTaskInfo = createNewRemoteTaskInfo(2, "task1", 100);
        RemoteTask secondMostRecentTask
            = secondMostRecentTaskInfo.toRemoteTask(ASSOCIATION_ID, DEVICE_NAME);

        taskList.setTasks(Arrays.asList(mostRecentTaskInfo, secondMostRecentTaskInfo));
        assertThat(taskList.getMostRecentTask())
            .isEqualTo(mostRecentTask);

        taskList.removeTask(mostRecentTask.getId());
        assertThat(taskList.getMostRecentTask())
            .isEqualTo(secondMostRecentTask);
    }

    @Test
    public void testSetTasks_updatesMostRecentTask() {

+30 −4
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import static com.android.server.companion.datatransfer.continuity.TaskContinuit

import android.app.ActivityManager;
import android.companion.AssociationInfo;
import android.companion.datatransfer.continuity.RemoteTask;
import android.platform.test.annotations.Presubmit;
import android.testing.AndroidTestingRunner;

@@ -73,7 +74,7 @@ public class RemoteTaskStoreTest {
        taskStore.onTransportConnected(associationInfo);

        // Add tasks to the new association.
        RemoteTaskInfo remoteTaskInfo = createNewRemoteTaskInfo("task1", 100L);
        RemoteTaskInfo remoteTaskInfo = createNewRemoteTaskInfo(1, "task1", 100L);
        taskStore.setTasks(
            associationInfo.getId(),
            Collections.singletonList(remoteTaskInfo));
@@ -88,7 +89,7 @@ public class RemoteTaskStoreTest {
        when(mMockConnectedAssociationStore.getConnectedAssociationById(0))
            .thenReturn(null);

        RemoteTaskInfo remoteTaskInfo = createNewRemoteTaskInfo("task1", 100L);
        RemoteTaskInfo remoteTaskInfo = createNewRemoteTaskInfo(1, "task1", 100L);

        // Add the task. Since ConnectedAssociationStore does not have this
        // association, this should be ignored.
@@ -97,6 +98,30 @@ public class RemoteTaskStoreTest {
        assertThat(taskStore.getMostRecentTasks()).isEmpty();
    }

    @Test
    public void removeTask_removesTask() {
        // Setup an association.
        AssociationInfo associationInfo = createAssociationInfo(1, "name");
        taskStore.onTransportConnected(associationInfo);

        // Add two tasks
        RemoteTaskInfo mostRecentTaskInfo = createNewRemoteTaskInfo(1, "task1", 200);
        RemoteTask mostRecentTask
            = mostRecentTaskInfo.toRemoteTask(associationInfo.getId(), "name");
        RemoteTaskInfo secondMostRecentTaskInfo = createNewRemoteTaskInfo(2, "task2", 100);
        RemoteTask secondMostRecentTask
            = secondMostRecentTaskInfo.toRemoteTask(associationInfo.getId(), "name");
        taskStore.setTasks(
            associationInfo.getId(),
            Arrays.asList(mostRecentTaskInfo, secondMostRecentTaskInfo));

        assertThat(taskStore.getMostRecentTasks())
            .containsExactly(mostRecentTask);

        taskStore.removeTask(associationInfo.getId(), mostRecentTaskInfo.getId());
        assertThat(taskStore.getMostRecentTasks()).containsExactly(secondMostRecentTask);
    }

    @Test
    public void onTransportDisconnected_removesAssociation() {
        // Create a fake association info, and have connected association store
@@ -106,7 +131,7 @@ public class RemoteTaskStoreTest {
                .thenReturn(associationInfo);

        // Set tasks for the association.
        RemoteTaskInfo remoteTaskInfo = createNewRemoteTaskInfo("task1", 100L);
        RemoteTaskInfo remoteTaskInfo = createNewRemoteTaskInfo(1, "task1", 100L);
        taskStore.setTasks(0, Collections.singletonList(remoteTaskInfo));

        // Simulate the association being disconnected.
@@ -117,11 +142,12 @@ public class RemoteTaskStoreTest {
    }

    private RemoteTaskInfo createNewRemoteTaskInfo(
        int id,
        String label,
        long lastUsedTimeMillis) {

        ActivityManager.RunningTaskInfo runningTaskInfo
            = createRunningTaskInfo(1, label, lastUsedTimeMillis);
            = createRunningTaskInfo(id, label, lastUsedTimeMillis);

        return new RemoteTaskInfo(runningTaskInfo);
    }