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:
@@ -602,9 +602,8 @@ class MediaRouter2ServiceImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO(b/136703681): Review this is handling multi-user properly.
|
// TODO(b/136703681): Review this is handling multi-user properly.
|
||||||
void switchUser() {
|
void switchUser(int userId) {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
int userId = ActivityManager.getCurrentUser();
|
|
||||||
if (mCurrentUserId != userId) {
|
if (mCurrentUserId != userId) {
|
||||||
final int oldUserId = mCurrentUserId;
|
final int oldUserId = mCurrentUserId;
|
||||||
mCurrentUserId = userId; // do this first
|
mCurrentUserId = userId; // do this first
|
||||||
|
|||||||
@@ -17,7 +17,9 @@
|
|||||||
package com.android.server.media;
|
package com.android.server.media;
|
||||||
|
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
|
import android.annotation.RequiresPermission;
|
||||||
import android.app.ActivityManager;
|
import android.app.ActivityManager;
|
||||||
|
import android.app.UserSwitchObserver;
|
||||||
import android.bluetooth.BluetoothA2dp;
|
import android.bluetooth.BluetoothA2dp;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
@@ -217,18 +219,27 @@ public final class MediaRouterService extends IMediaRouterService.Stub
|
|||||||
context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null);
|
context.registerReceiverAsUser(mReceiver, UserHandle.ALL, intentFilter, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void systemRunning() {
|
/**
|
||||||
IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
|
* Initializes the MediaRouter service.
|
||||||
mContext.registerReceiver(new BroadcastReceiver() {
|
*
|
||||||
|
* @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
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onUserSwitchComplete(int newUserId) {
|
||||||
if (intent.getAction().equals(Intent.ACTION_USER_SWITCHED)) {
|
switchUser(newUserId);
|
||||||
switchUser();
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}, filter);
|
TAG);
|
||||||
|
switchUser(ActivityManager.getCurrentUser());
|
||||||
switchUser();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -702,9 +713,8 @@ public final class MediaRouterService extends IMediaRouterService.Stub
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void switchUser() {
|
void switchUser(int userId) {
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
int userId = ActivityManager.getCurrentUser();
|
|
||||||
if (mCurrentUserId != userId) {
|
if (mCurrentUserId != userId) {
|
||||||
final int oldUserId = mCurrentUserId;
|
final int oldUserId = mCurrentUserId;
|
||||||
mCurrentUserId = userId; // do this first
|
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) {
|
void clientDied(ClientRecord clientRecord) {
|
||||||
|
|||||||
Reference in New Issue
Block a user