Fix output switcher will show wrong active device

- The output switcher will show wrong active device because
  LocalMediaManager didn't go through all list. It will return
  PhoneMediaDevice immediately if found PhoneMediaDevice in the
  list, actually the active device is end of the list.

  This CL will cache PhoneMediaDevice if all BluetoothMediaDevice
  in the list are not active device then return PhoneMediaDevice
  as active device.

- Add log and test case.

Bug: 156689249
Test: make -j42 RunSettingsLibRoboTests
Change-Id: I8174168fa0578aa101efc3dd4f89e4bacb31082f
This commit is contained in:
hughchen
2020-05-20 14:19:11 +08:00
parent e39c2e7e61
commit b85a16cd08
4 changed files with 53 additions and 9 deletions

View File

@@ -56,7 +56,7 @@ import java.util.concurrent.Executors;
public class InfoMediaManager extends MediaManager {
private static final String TAG = "InfoMediaManager";
private static final boolean DEBUG = false;
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);;
@VisibleForTesting
final RouterManagerCallback mMediaRouterCallback = new RouterManagerCallback();
@VisibleForTesting
@@ -364,8 +364,8 @@ public class InfoMediaManager extends MediaManager {
private void buildAvailableRoutes() {
for (MediaRoute2Info route : mRouterManager.getAvailableRoutes(mPackageName)) {
if (DEBUG) {
Log.d(TAG, "buildAvailableRoutes() route : " + route.getName()
+ ", type : " + route.getType());
Log.d(TAG, "buildAvailableRoutes() route : " + route.getName() + ", volume : "
+ route.getVolume() + ", type : " + route.getType());
}
addMediaDevice(route);
}

View File

@@ -390,7 +390,9 @@ public class LocalMediaManager implements BluetoothCallback {
return mPackageName;
}
private MediaDevice updateCurrentConnectedDevice() {
@VisibleForTesting
MediaDevice updateCurrentConnectedDevice() {
MediaDevice connectedDevice = null;
synchronized (mMediaDevicesLock) {
for (MediaDevice device : mMediaDevices) {
if (device instanceof BluetoothMediaDevice) {
@@ -398,12 +400,12 @@ public class LocalMediaManager implements BluetoothCallback {
return device;
}
} else if (device instanceof PhoneMediaDevice) {
return device;
connectedDevice = device;
}
}
}
Log.w(TAG, "updateCurrentConnectedDevice() can't found current connected device");
return null;
return connectedDevice;
}
private boolean isActiveDevice(CachedBluetoothDevice device) {

View File

@@ -590,7 +590,7 @@ public class InfoMediaManagerTest {
final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID);
assertThat(mediaDevice).isNull();
mInfoMediaManager.mMediaRouterCallback.onTransferred(null, null);
mInfoMediaManager.mMediaRouterCallback.onTransferred(sessionInfo, sessionInfo);
final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice.getId()).isEqualTo(TEST_ID);
@@ -602,6 +602,7 @@ public class InfoMediaManagerTest {
@Test
public void onTransferred_buildAllRoutes_shouldAddMediaDevice() {
final MediaRoute2Info info = mock(MediaRoute2Info.class);
final RoutingSessionInfo sessionInfo = mock(RoutingSessionInfo.class);
mInfoMediaManager.registerCallback(mCallback);
when(info.getId()).thenReturn(TEST_ID);
@@ -616,7 +617,7 @@ public class InfoMediaManagerTest {
assertThat(mediaDevice).isNull();
mInfoMediaManager.mPackageName = "";
mInfoMediaManager.mMediaRouterCallback.onTransferred(null, null);
mInfoMediaManager.mMediaRouterCallback.onTransferred(sessionInfo, sessionInfo);
final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0);
assertThat(infoDevice.getId()).isEqualTo(TEST_ID);

View File

@@ -31,6 +31,7 @@ import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.media.MediaRoute2Info;
import android.media.MediaRouter2Manager;
@@ -725,4 +726,44 @@ public class LocalMediaManagerTest {
verify(mInfoMediaManager).adjustSessionVolume(info, 10);
}
@Test
public void updateCurrentConnectedDevice_bluetoothDeviceIsActive_returnBluetoothDevice() {
final BluetoothMediaDevice device1 = mock(BluetoothMediaDevice.class);
final BluetoothMediaDevice device2 = mock(BluetoothMediaDevice.class);
final PhoneMediaDevice phoneDevice = mock(PhoneMediaDevice.class);
final CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
when(device1.getCachedDevice()).thenReturn(cachedDevice1);
when(device2.getCachedDevice()).thenReturn(cachedDevice2);
when(cachedDevice1.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(false);
when(cachedDevice2.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(true);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(phoneDevice);
mLocalMediaManager.mMediaDevices.add(device2);
assertThat(mLocalMediaManager.updateCurrentConnectedDevice()).isEqualTo(device2);
}
@Test
public void updateCurrentConnectedDevice_phoneDeviceIsActive_returnPhoneDevice() {
final BluetoothMediaDevice device1 = mock(BluetoothMediaDevice.class);
final BluetoothMediaDevice device2 = mock(BluetoothMediaDevice.class);
final PhoneMediaDevice phoneDevice = mock(PhoneMediaDevice.class);
final CachedBluetoothDevice cachedDevice1 = mock(CachedBluetoothDevice.class);
final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
when(device1.getCachedDevice()).thenReturn(cachedDevice1);
when(device2.getCachedDevice()).thenReturn(cachedDevice2);
when(cachedDevice1.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(false);
when(cachedDevice2.isActiveDevice(BluetoothProfile.A2DP)).thenReturn(false);
mLocalMediaManager.mMediaDevices.add(device1);
mLocalMediaManager.mMediaDevices.add(phoneDevice);
mLocalMediaManager.mMediaDevices.add(device2);
assertThat(mLocalMediaManager.updateCurrentConnectedDevice()).isEqualTo(phoneDevice);
}
}