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

Commit 43d75385 authored by Santiago Seifert's avatar Santiago Seifert
Browse files

Use UserSwitchObserver to detect switches in the running user

The current implementation relies on listening for
Intent.ACTION_USER_SWITCHED, which can take more than 30
seconds to propagate, making UserSwitchObserver a more
reliable option.

Bug: 242188673
Test: atest mediaroutertest CtsMediaBetterTogetherTestCases
Test: Manually tested casting on secondary users.
Test: Pending CL to verify route availability on secondary users as part of the linked bug.
Change-Id: I67765a671bce590495b73ef7df6bf72ba3c9f03a
parent 7c34e2fb
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -622,9 +622,8 @@ class MediaRouter2ServiceImpl {
    }

    // TODO(b/136703681): Review this is handling multi-user properly.
    void switchUser() {
    void switchUser(int userId) {
        synchronized (mLock) {
            int userId = ActivityManager.getCurrentUser();
            if (mCurrentUserId != userId) {
                final int oldUserId = mCurrentUserId;
                mCurrentUserId = userId; // do this first
+25 −15
Original line number Diff line number Diff line
@@ -17,7 +17,9 @@
package com.android.server.media;

import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.app.ActivityManager;
import android.app.UserSwitchObserver;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
@@ -146,18 +148,27 @@ public final class MediaRouterService extends IMediaRouterService.Stub
        context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null);
    }

    public void systemRunning() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
        mContext.registerReceiver(new BroadcastReceiver() {
    /**
     * Initializes the MediaRouter service.
     *
     * @throws RemoteException If an error occurs while registering the {@link UserSwitchObserver}.
     */
    @RequiresPermission(
            anyOf = {
                "android.permission.INTERACT_ACROSS_USERS",
                "android.permission.INTERACT_ACROSS_USERS_FULL"
            })
    public void systemRunning() throws RemoteException {
        ActivityManager.getService()
                .registerUserSwitchObserver(
                        new UserSwitchObserver() {
                            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Intent.ACTION_USER_SWITCHED)) {
                    switchUser();
                }
                            public void onUserSwitchComplete(int newUserId) {
                                switchUser(newUserId);
                            }
        }, filter);

        switchUser();
                        },
                        TAG);
        switchUser(ActivityManager.getCurrentUser());
    }

    @Override
@@ -634,9 +645,8 @@ public final class MediaRouterService extends IMediaRouterService.Stub
        }
    }

    void switchUser() {
    void switchUser(int userId) {
        synchronized (mLock) {
            int userId = ActivityManager.getCurrentUser();
            if (mCurrentUserId != userId) {
                final int oldUserId = mCurrentUserId;
                mCurrentUserId = userId; // do this first
@@ -653,7 +663,7 @@ public final class MediaRouterService extends IMediaRouterService.Stub
                }
            }
        }
        mService2.switchUser();
        mService2.switchUser(userId);
    }

    void clientDied(ClientRecord clientRecord) {