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

Commit 1648937d authored by dakinola's avatar dakinola Committed by Daniel Akinola
Browse files

Fix Launching Foreground Recording App with MediaProjection App Selector

Partner found a bug relating to when we attempt to partial screenshare the app that is requesting MediaProjection. We always attempt to find the selected app by launch cookie provided, but if we select the currently open app that is requesting a MediaProjection, the launch cookie won't match. It only works now by accident because the launch cookie is attributed to the app selector activity instead of the base recording activity, but if the app selector activity is closed by the time virtual display set up starts then it won't find an activity with the new launch cookie. To remedy this, for the case of starting a mediaprojection for the active activity, then we'll provide the task id as a backup

Bug: 365498349
Test: atest ShellRecentTaskListProviderTest
Flag: EXEMPT bugfix
Change-Id: I7f5e4139dd72f7f7795adab00a52dc82b14e4ac9
parent e8c0fd8f
Loading
Loading
Loading
Loading
+22 −13
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import com.android.systemui.mediaprojection.MediaProjectionServiceHelper
import com.android.systemui.mediaprojection.appselector.data.RecentTask
import com.android.systemui.mediaprojection.appselector.view.MediaProjectionRecentsViewController
import com.android.systemui.res.R
import com.android.systemui.shared.system.ActivityManagerWrapper
import com.android.systemui.statusbar.policy.ConfigurationController
import com.android.systemui.util.AsyncActivityLauncher
import java.lang.IllegalArgumentException
@@ -62,9 +63,10 @@ import javax.inject.Inject
class MediaProjectionAppSelectorActivity(
    private val componentFactory: MediaProjectionAppSelectorComponent.Factory,
    private val activityLauncher: AsyncActivityLauncher,
    private val activityManager: ActivityManagerWrapper,
    /** This is used to override the dependency in a screenshot test */
    @VisibleForTesting
    private val listControllerFactory: ((userHandle: UserHandle) -> ResolverListController)?
    private val listControllerFactory: ((userHandle: UserHandle) -> ResolverListController)?,
) :
    ChooserActivity(),
    MediaProjectionAppSelectorView,
@@ -74,8 +76,9 @@ class MediaProjectionAppSelectorActivity(
    @Inject
    constructor(
        componentFactory: MediaProjectionAppSelectorComponent.Factory,
        activityLauncher: AsyncActivityLauncher
    ) : this(componentFactory, activityLauncher, listControllerFactory = null)
        activityLauncher: AsyncActivityLauncher,
        activityManager: ActivityManagerWrapper,
    ) : this(componentFactory, activityLauncher, activityManager, listControllerFactory = null)

    private val lifecycleRegistry = LifecycleRegistry(this)
    override val lifecycle = lifecycleRegistry
@@ -100,7 +103,7 @@ class MediaProjectionAppSelectorActivity(
                callingPackage = callingPackage,
                view = this,
                resultHandler = this,
                isFirstStart = savedInstanceState == null
                isFirstStart = savedInstanceState == null,
            )
        component.lifecycleObservers.forEach { lifecycle.addObserver(it) }

@@ -113,7 +116,7 @@ class MediaProjectionAppSelectorActivity(
        intent.configureChooserIntent(
            resources,
            component.hostUserHandle,
            component.personalProfileUserHandle
            component.personalProfileUserHandle,
        )

        reviewGrantedConsentRequired =
@@ -180,8 +183,14 @@ class MediaProjectionAppSelectorActivity(
        // is created and ready to be captured.
        val activityStarted =
            activityLauncher.startActivityAsUser(intent, userHandle, activityOptions.toBundle()) {
                if (targetInfo.resolvedComponentName == callingActivity) {
                    // If attempting to launch the app used to launch the MediaProjection, then
                    // provide the task id since the launch cookie won't match the existing task
                    returnSelectedApp(launchCookie, taskId = activityManager.runningTask.taskId)
                } else {
                    returnSelectedApp(launchCookie, taskId = -1)
                }
            }

        // Rely on the ActivityManager to pop up a dialog regarding app suspension
        // and return false if suspended
@@ -213,7 +222,7 @@ class MediaProjectionAppSelectorActivity(
            MediaProjectionServiceHelper.setReviewedConsentIfNeeded(
                RECORD_CANCEL,
                reviewGrantedConsentRequired,
                /* projection= */ null
                /* projection= */ null,
            )
            if (isFinishing) {
                // Only log dismissed when actually finishing, and not when changing configuration.
@@ -246,7 +255,7 @@ class MediaProjectionAppSelectorActivity(
            val resultReceiver =
                intent.getParcelableExtra(
                    EXTRA_CAPTURE_REGION_RESULT_RECEIVER,
                    ResultReceiver::class.java
                    ResultReceiver::class.java,
                ) as ResultReceiver
            val captureRegion = MediaProjectionCaptureTarget(launchCookie, taskId)
            val data = Bundle().apply { putParcelable(KEY_CAPTURE_TARGET, captureRegion) }
@@ -260,8 +269,8 @@ class MediaProjectionAppSelectorActivity(
            val mediaProjectionBinder = intent.getIBinderExtra(EXTRA_MEDIA_PROJECTION)
            val projection = IMediaProjection.Stub.asInterface(mediaProjectionBinder)

            projection.setLaunchCookie(launchCookie)
            projection.setTaskId(taskId)
            projection.launchCookie = launchCookie
            projection.taskId = taskId

            val intent = Intent()
            intent.putExtra(EXTRA_MEDIA_PROJECTION, projection.asBinder())
@@ -270,7 +279,7 @@ class MediaProjectionAppSelectorActivity(
            MediaProjectionServiceHelper.setReviewedConsentIfNeeded(
                RECORD_CONTENT_TASK,
                reviewGrantedConsentRequired,
                projection
                projection,
            )
        }

@@ -457,7 +466,7 @@ class MediaProjectionAppSelectorActivity(
     */
    private class RecyclerViewExpandingAccessibilityDelegate(
        rdl: ResolverDrawerLayout,
        view: RecyclerView
        view: RecyclerView,
    ) : RecyclerViewAccessibilityDelegate(view) {

        private val delegate = AppListAccessibilityDelegate(rdl)
@@ -465,7 +474,7 @@ class MediaProjectionAppSelectorActivity(
        override fun onRequestSendAccessibilityEvent(
            host: ViewGroup,
            child: View,
            event: AccessibilityEvent
            event: AccessibilityEvent,
        ): Boolean {
            super.onRequestSendAccessibilityEvent(host, child, event)
            return delegate.onRequestSendAccessibilityEvent(host, child, event)