Select correct system audio route in updateAudioRoutes

While update audio routes, MediaRouter had an access to the variable
of MediaRouterService.mGlobalBluetoothA2dpOn, which was updated in
the callback of AudioRoutesObserver.dispatchAudioRoutesChanged().
However, since updateAudioRoutes() was also called by the same
callback, mGlobalBluetoothA2dpOn could be used in updateAudioRoutes()
before its value was updated.

Bug: 65629167
Test: passed MediaRouterTest
Test: Manually confirmed that the issue is fixed with this CL
Change-Id: Id0034996a51a6d8d1e8bd9d2c6ca386aabbb8baf
Merged-In: Id0034996a51a6d8d1e8bd9d2c6ca386aabbb8baf
This commit is contained in:
Sungsoo Lim
2017-09-18 14:28:38 +09:00
parent 7a59a62276
commit 0e43c503ab
3 changed files with 24 additions and 35 deletions

View File

@@ -28,7 +28,6 @@ interface IMediaRouterService {
MediaRouterClientState getState(IMediaRouterClient client);
boolean isPlaybackActive(IMediaRouterClient client);
boolean isGlobalBluetoothA2doOn();
void setDiscoveryRequest(IMediaRouterClient client, int routeTypes, boolean activeScan);
void setSelectedRoute(IMediaRouterClient client, String routeId, boolean explicit);

View File

@@ -184,25 +184,33 @@ public class MediaRouter {
void updateAudioRoutes(AudioRoutesInfo newRoutes) {
boolean audioRoutesChanged = false;
boolean forceUseDefaultRoute = false;
if (newRoutes.mainType != mCurAudioRoutesInfo.mainType) {
mCurAudioRoutesInfo.mainType = newRoutes.mainType;
int name;
if ((newRoutes.mainType&AudioRoutesInfo.MAIN_HEADPHONES) != 0
|| (newRoutes.mainType&AudioRoutesInfo.MAIN_HEADSET) != 0) {
if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HEADPHONES) != 0
|| (newRoutes.mainType & AudioRoutesInfo.MAIN_HEADSET) != 0) {
name = com.android.internal.R.string.default_audio_route_name_headphones;
} else if ((newRoutes.mainType&AudioRoutesInfo.MAIN_DOCK_SPEAKERS) != 0) {
} else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_DOCK_SPEAKERS) != 0) {
name = com.android.internal.R.string.default_audio_route_name_dock_speakers;
} else if ((newRoutes.mainType&AudioRoutesInfo.MAIN_HDMI) != 0) {
} else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HDMI) != 0) {
name = com.android.internal.R.string.default_media_route_name_hdmi;
} else {
name = com.android.internal.R.string.default_audio_route_name;
}
mDefaultAudioVideo.mNameResId = name;
dispatchRouteChanged(mDefaultAudioVideo);
if ((newRoutes.mainType & (AudioRoutesInfo.MAIN_HEADSET
| AudioRoutesInfo.MAIN_HEADPHONES | AudioRoutesInfo.MAIN_USB)) != 0) {
forceUseDefaultRoute = true;
}
audioRoutesChanged = true;
}
if (!TextUtils.equals(newRoutes.bluetoothName, mCurAudioRoutesInfo.bluetoothName)) {
forceUseDefaultRoute = false;
mCurAudioRoutesInfo.bluetoothName = newRoutes.bluetoothName;
if (mCurAudioRoutesInfo.bluetoothName != null) {
if (mBluetoothA2dpRoute == null) {
@@ -231,30 +239,18 @@ public class MediaRouter {
Log.v(TAG, "Audio routes updated: " + newRoutes + ", a2dp=" + isBluetoothA2dpOn());
if (mSelectedRoute == null || mSelectedRoute == mDefaultAudioVideo
|| mSelectedRoute == mBluetoothA2dpRoute) {
selectRouteStatic(ROUTE_TYPE_LIVE_AUDIO, getDefaultSystemAudioRoute(), false);
if (forceUseDefaultRoute || mBluetoothA2dpRoute == null) {
selectRouteStatic(ROUTE_TYPE_LIVE_AUDIO, mDefaultAudioVideo, false);
} else {
selectRouteStatic(ROUTE_TYPE_LIVE_AUDIO, mBluetoothA2dpRoute, false);
}
}
}
}
RouteInfo getDefaultSystemAudioRoute() {
boolean globalBluetoothA2doOn = false;
try {
globalBluetoothA2doOn = mMediaRouterService.isGlobalBluetoothA2doOn();
} catch (RemoteException ex) {
Log.e(TAG, "Unable to call isSystemBluetoothA2doOn.", ex);
}
return (globalBluetoothA2doOn && mBluetoothA2dpRoute != null)
? mBluetoothA2dpRoute : mDefaultAudioVideo;
}
RouteInfo getCurrentSystemAudioRoute() {
return (isBluetoothA2dpOn() && mBluetoothA2dpRoute != null)
? mBluetoothA2dpRoute : mDefaultAudioVideo;
}
boolean isBluetoothA2dpOn() {
try {
return mAudioService.isBluetoothA2dpOn();
return mBluetoothA2dpRoute != null && mAudioService.isBluetoothA2dpOn();
} catch (RemoteException e) {
Log.e(TAG, "Error querying Bluetooth A2DP state", e);
return false;
@@ -608,6 +604,7 @@ public class MediaRouter {
|| mSelectedRoute == null) {
return;
}
Log.v(TAG, "onRestoreRoute() : a2dp=" + isBluetoothA2dpOn());
mSelectedRoute.select();
}
}
@@ -940,10 +937,12 @@ public class MediaRouter {
Log.v(TAG, "Selecting route: " + route);
assert(route != null);
final RouteInfo oldRoute = sStatic.mSelectedRoute;
final RouteInfo currentSystemRoute = sStatic.isBluetoothA2dpOn()
? sStatic.mBluetoothA2dpRoute : sStatic.mDefaultAudioVideo;
boolean wasDefaultOrBluetoothRoute = (oldRoute == sStatic.mDefaultAudioVideo
|| oldRoute == sStatic.mBluetoothA2dpRoute);
if (oldRoute == route
&& (!wasDefaultOrBluetoothRoute || route == sStatic.getCurrentSystemAudioRoute())) {
&& (!wasDefaultOrBluetoothRoute || route == currentSystemRoute)) {
return;
}
if (!route.matchesTypes(types)) {
@@ -1014,8 +1013,7 @@ public class MediaRouter {
static void selectDefaultRouteStatic() {
// TODO: Be smarter about the route types here; this selects for all valid.
if (sStatic.mSelectedRoute != sStatic.mBluetoothA2dpRoute
&& sStatic.mBluetoothA2dpRoute != null && sStatic.isBluetoothA2dpOn()) {
if (sStatic.mSelectedRoute != sStatic.mBluetoothA2dpRoute && sStatic.isBluetoothA2dpOn()) {
selectRouteStatic(ROUTE_TYPE_ANY, sStatic.mBluetoothA2dpRoute, false);
} else {
selectRouteStatic(ROUTE_TYPE_ANY, sStatic.mDefaultAudioVideo, false);

View File

@@ -269,14 +269,6 @@ public final class MediaRouterService extends IMediaRouterService.Stub
}
}
// Binder call
@Override
public boolean isGlobalBluetoothA2doOn() {
synchronized (mLock) {
return mGlobalBluetoothA2dpOn;
}
}
// Binder call
@Override
public void setDiscoveryRequest(IMediaRouterClient client,
@@ -383,7 +375,7 @@ public final class MediaRouterService extends IMediaRouterService.Stub
synchronized (mLock) {
a2dpOn = mGlobalBluetoothA2dpOn;
}
Slog.v(TAG, "restoreBluetoothA2dp( " + a2dpOn + ")");
Slog.v(TAG, "restoreBluetoothA2dp(" + a2dpOn + ")");
mAudioService.setBluetoothA2dpOn(a2dpOn);
} catch (RemoteException e) {
Slog.w(TAG, "RemoteException while calling setBluetoothA2dpOn.");