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
Merged-In: I67765a671bce590495b73ef7df6bf72ba3c9f03a
This commit is contained in:
Santiago Seifert
2022-09-27 11:22:53 +00:00
parent f509069081
commit 2be074c097
2 changed files with 27 additions and 18 deletions

View File

@@ -601,10 +601,9 @@ class MediaRouter2ServiceImpl {
}
}
//TODO(b/136703681): Review this is handling multi-user properly.
void switchUser() {
// TODO(b/136703681): Review this is handling multi-user properly.
void switchUser(int userId) {
synchronized (mLock) {
int userId = ActivityManager.getCurrentUser();
if (mCurrentUserId != userId) {
final int oldUserId = mCurrentUserId;
mCurrentUserId = userId; // do this first

View File

@@ -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;
@@ -217,18 +219,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() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_SWITCHED)) {
switchUser();
}
}
}, filter);
switchUser();
/**
* 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 onUserSwitchComplete(int newUserId) {
switchUser(newUserId);
}
},
TAG);
switchUser(ActivityManager.getCurrentUser());
}
@Override
@@ -702,9 +713,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
@@ -721,7 +731,7 @@ public final class MediaRouterService extends IMediaRouterService.Stub
}
}
}
mService2.switchUser();
mService2.switchUser(userId);
}
void clientDied(ClientRecord clientRecord) {