diff --git a/media/java/android/media/IMediaRouterClient.aidl b/media/java/android/media/IMediaRouterClient.aidl index 240ae796f9574..53122bb990d64 100644 --- a/media/java/android/media/IMediaRouterClient.aidl +++ b/media/java/android/media/IMediaRouterClient.aidl @@ -23,4 +23,5 @@ oneway interface IMediaRouterClient { void onStateChanged(); void onRestoreRoute(); void onSelectedRouteChanged(String routeId); + void onGlobalA2dpChanged(boolean a2dpOn); } diff --git a/media/java/android/media/MediaRouter.java b/media/java/android/media/MediaRouter.java index 12fc3a60cf283..7ae2949e60749 100644 --- a/media/java/android/media/MediaRouter.java +++ b/media/java/android/media/MediaRouter.java @@ -621,33 +621,27 @@ public class MediaRouter { final class Client extends IMediaRouterClient.Stub { @Override public void onStateChanged() { - mHandler.post(new Runnable() { - @Override - public void run() { - if (Client.this == mClient) { - updateClientState(); - } + mHandler.post(() -> { + if (Client.this == mClient) { + updateClientState(); } }); } @Override public void onRestoreRoute() { - mHandler.post(new Runnable() { - @Override - public void run() { - // Skip restoring route if the selected route is not a system audio route, - // MediaRouter is initializing, or mClient was changed. - if (Client.this != mClient || mSelectedRoute == null - || (mSelectedRoute != mDefaultAudioVideo - && mSelectedRoute != mBluetoothA2dpRoute)) { - return; - } - if (DEBUG) { - Log.d(TAG, "onRestoreRoute() : route=" + mSelectedRoute); - } - mSelectedRoute.select(); + mHandler.post(() -> { + // Skip restoring route if the selected route is not a system audio route, + // MediaRouter is initializing, or mClient was changed. + if (Client.this != mClient || mSelectedRoute == null + || (mSelectedRoute != mDefaultAudioVideo + && mSelectedRoute != mBluetoothA2dpRoute)) { + return; } + 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); + } + }); + } } } diff --git a/services/core/java/com/android/server/media/MediaRouterService.java b/services/core/java/com/android/server/media/MediaRouterService.java index bf2cc5e68facc..3337b480d6a8f 100644 --- a/services/core/java/com/android/server/media/MediaRouterService.java +++ b/services/core/java/com/android/server/media/MediaRouterService.java @@ -891,8 +891,24 @@ public final class MediaRouterService extends IMediaRouterService.Stub if (intent.getAction().equals(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED)) { BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); synchronized (mLock) { + boolean wasA2dpOn = mGlobalBluetoothA2dpOn; mActiveBluetoothDevice = btDevice; 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 + } + } + } + } + } } } }