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

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

[Handoff][6/N] Properly launch activity as current user

Because `OutboundHandoffRequestController` is running in `system_server`, `startActivity()` will not start as the current user. This change fixes that by manually starting the activity as the logged-in user.

Test: Validated manually, and updated unit tests with new calls
Bug: 400970610
Flag: android.companion.enable_task_continuity
Change-Id: Ic5a0241c9e764e905b2ae9231186c8bcab5b7fd7
parent 4c59b078
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import com.android.server.companion.datatransfer.continuity.messages.HandoffRequ
import com.android.server.companion.datatransfer.continuity.messages.HandoffRequestResultMessage;
import com.android.server.companion.datatransfer.continuity.messages.TaskContinuityMessage;

import android.app.ActivityOptions;
import android.app.HandoffActivityData;
import android.content.Context;
import android.content.Intent;
@@ -34,6 +35,7 @@ import android.companion.datatransfer.continuity.IHandoffRequestCallback;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Slog;
import android.os.UserHandle;

import java.io.IOException;
import java.util.ArrayList;
@@ -146,7 +148,10 @@ public class OutboundHandoffRequestController {
        intent.setComponent(topActivity.getComponentName());
        intent.putExtras(new Bundle(topActivity.getExtras()));
        // TODO (joeantonetti): Handle failures here and fall back to a web URL.
        mContext.startActivity(intent);
        mContext.startActivityAsUser(
            intent,
            ActivityOptions.makeBasic().toBundle(),
            UserHandle.CURRENT);
        finishHandoffRequest(associationId, taskId, HANDOFF_REQUEST_RESULT_SUCCESS);
    }

+4 −4
Original line number Diff line number Diff line
@@ -112,7 +112,7 @@ public class OutboundHandoffRequestControllerTest {
            = new HandoffActivityData.Builder(expectedComponentName)
                .setExtras(expectedExtras)
                .build();
        doNothing().when(mContext).startActivity(any());
        doNothing().when(mContext).startActivityAsUser(any(), any(), any());

        HandoffRequestResultMessage handoffRequestResultMessage = new HandoffRequestResultMessage(
            taskId,
@@ -124,7 +124,7 @@ public class OutboundHandoffRequestControllerTest {

        // Verify the intent was launched.
        ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
        verify(mContext, times(1)).startActivity(intentCaptor.capture());
        verify(mContext, times(1)).startActivityAsUser(intentCaptor.capture(), any(), any());
        Intent actualIntent = intentCaptor.getValue();
        assertThat(actualIntent.getComponent()).isEqualTo(expectedComponentName);
        assertThat(actualIntent.getExtras().size()).isEqualTo(1);
@@ -215,7 +215,7 @@ public class OutboundHandoffRequestControllerTest {
            failureStatusCode);

        // Verify no intent was launched.
        verify(mContext, never()).startActivity(any());
        verify(mContext, never()).startActivityAsUser(any(), any(), any());
    }

    @Test
@@ -247,7 +247,7 @@ public class OutboundHandoffRequestControllerTest {
            TaskContinuityManager.HANDOFF_REQUEST_RESULT_FAILURE_NO_DATA_PROVIDED_BY_TASK);

        // Verify no intent was launched.
        verify(mContext, never()).startActivity(any());
        verify(mContext, never()).startActivityAsUser(any(), any(), any());
    }

    private final class HandoffRequestCallbackHolder {