Merge "Reset app routes when global a2dp state changed" into rvc-dev

This commit is contained in:
TreeHugger Robot
2020-06-12 06:37:46 +00:00
committed by Android (Google) Code Review
3 changed files with 44 additions and 20 deletions

View File

@@ -23,4 +23,5 @@ oneway interface IMediaRouterClient {
void onStateChanged(); void onStateChanged();
void onRestoreRoute(); void onRestoreRoute();
void onSelectedRouteChanged(String routeId); void onSelectedRouteChanged(String routeId);
void onGlobalA2dpChanged(boolean a2dpOn);
} }

View File

@@ -621,33 +621,27 @@ public class MediaRouter {
final class Client extends IMediaRouterClient.Stub { final class Client extends IMediaRouterClient.Stub {
@Override @Override
public void onStateChanged() { public void onStateChanged() {
mHandler.post(new Runnable() { mHandler.post(() -> {
@Override if (Client.this == mClient) {
public void run() { updateClientState();
if (Client.this == mClient) {
updateClientState();
}
} }
}); });
} }
@Override @Override
public void onRestoreRoute() { public void onRestoreRoute() {
mHandler.post(new Runnable() { mHandler.post(() -> {
@Override // Skip restoring route if the selected route is not a system audio route,
public void run() { // MediaRouter is initializing, or mClient was changed.
// Skip restoring route if the selected route is not a system audio route, if (Client.this != mClient || mSelectedRoute == null
// MediaRouter is initializing, or mClient was changed. || (mSelectedRoute != mDefaultAudioVideo
if (Client.this != mClient || mSelectedRoute == null && mSelectedRoute != mBluetoothA2dpRoute)) {
|| (mSelectedRoute != mDefaultAudioVideo return;
&& mSelectedRoute != mBluetoothA2dpRoute)) {
return;
}
if (DEBUG) {
Log.d(TAG, "onRestoreRoute() : route=" + mSelectedRoute);
}
mSelectedRoute.select();
} }
if (DEBUG) {
Log.d(TAG, "onRestoreRoute() : route=" + mSelectedRoute);
}
mSelectedRoute.select();
}); });
} }
@@ -659,6 +653,19 @@ public class MediaRouter {
} }
}); });
} }
// Called when the selection of a connected device (phone speaker or BT devices)
// is changed.
@Override
public void onGlobalA2dpChanged(boolean a2dpOn) {
mHandler.post(() -> {
if (mSelectedRoute == mDefaultAudioVideo && a2dpOn) {
setSelectedRoute(mBluetoothA2dpRoute, false);
} else if (mSelectedRoute == mBluetoothA2dpRoute && !a2dpOn) {
setSelectedRoute(mDefaultAudioVideo, false);
}
});
}
} }
} }

View File

@@ -891,8 +891,24 @@ public final class MediaRouterService extends IMediaRouterService.Stub
if (intent.getAction().equals(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED)) { if (intent.getAction().equals(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED)) {
BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
synchronized (mLock) { synchronized (mLock) {
boolean wasA2dpOn = mGlobalBluetoothA2dpOn;
mActiveBluetoothDevice = btDevice; mActiveBluetoothDevice = btDevice;
mGlobalBluetoothA2dpOn = btDevice != null; mGlobalBluetoothA2dpOn = btDevice != null;
if (wasA2dpOn != mGlobalBluetoothA2dpOn) {
UserRecord userRecord = mUserRecords.get(mCurrentUserId);
if (userRecord != null) {
for (ClientRecord cr : userRecord.mClientRecords) {
// mSelectedRouteId will be null for BT and phone speaker.
if (cr.mSelectedRouteId == null) {
try {
cr.mClient.onGlobalA2dpChanged(mGlobalBluetoothA2dpOn);
} catch (RemoteException e) {
// Ignore exception
}
}
}
}
}
} }
} }
} }