Merge "Add new interface to get active local media session and to adjust volume" into rvc-dev
This commit is contained in:
@@ -249,6 +249,15 @@ public class InfoMediaManager extends MediaManager {
|
||||
return deviceList;
|
||||
}
|
||||
|
||||
void adjustSessionVolume(RoutingSessionInfo info, int volume) {
|
||||
if (info == null) {
|
||||
Log.w(TAG, "Unable to adjust session volume. RoutingSessionInfo is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
mRouterManager.setSessionVolume(info, volume);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust the volume of {@link android.media.RoutingSessionInfo}.
|
||||
*
|
||||
@@ -352,6 +361,10 @@ public class InfoMediaManager extends MediaManager {
|
||||
}
|
||||
}
|
||||
|
||||
List<RoutingSessionInfo> getActiveMediaSession() {
|
||||
return mRouterManager.getActiveSessions();
|
||||
}
|
||||
|
||||
private void buildAvailableRoutes() {
|
||||
for (MediaRoute2Info route : mRouterManager.getAvailableRoutes(mPackageName)) {
|
||||
if (DEBUG) {
|
||||
|
||||
@@ -20,6 +20,7 @@ import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.Context;
|
||||
import android.media.RoutingSessionInfo;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -255,22 +256,6 @@ public class LocalMediaManager implements BluetoothCallback {
|
||||
return mCurrentConnectedDevice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the active MediaDevice.
|
||||
*
|
||||
* @param type the media device type.
|
||||
* @return MediaDevice list
|
||||
*/
|
||||
public List<MediaDevice> getActiveMediaDevice(@MediaDevice.MediaDeviceType int type) {
|
||||
final List<MediaDevice> devices = new ArrayList<>();
|
||||
for (MediaDevice device : mMediaDevices) {
|
||||
if (type == device.mType && device.getClientPackageName() != null) {
|
||||
devices.add(device);
|
||||
}
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a MediaDevice to let it play current media.
|
||||
*
|
||||
@@ -316,6 +301,23 @@ public class LocalMediaManager implements BluetoothCallback {
|
||||
return mInfoMediaManager.getSelectedMediaDevice();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust the volume of session.
|
||||
*
|
||||
* @param sessionId the value of media session id
|
||||
* @param volume the value of volume
|
||||
*/
|
||||
public void adjustSessionVolume(String sessionId, int volume) {
|
||||
final List<RoutingSessionInfo> infos = getActiveMediaSession();
|
||||
for (RoutingSessionInfo info : infos) {
|
||||
if (TextUtils.equals(sessionId, info.getId())) {
|
||||
mInfoMediaManager.adjustSessionVolume(info, volume);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Log.w(TAG, "adjustSessionVolume: Unable to find session: " + sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust the volume of session.
|
||||
*
|
||||
@@ -352,6 +354,15 @@ public class LocalMediaManager implements BluetoothCallback {
|
||||
return mInfoMediaManager.getSessionName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current active session.
|
||||
*
|
||||
* @return current active session list{@link android.media.RoutingSessionInfo}
|
||||
*/
|
||||
public List<RoutingSessionInfo> getActiveMediaSession() {
|
||||
return mInfoMediaManager.getActiveMediaSession();
|
||||
}
|
||||
|
||||
private MediaDevice updateCurrentConnectedDevice() {
|
||||
MediaDevice phoneMediaDevice = null;
|
||||
for (MediaDevice device : mMediaDevices) {
|
||||
|
||||
@@ -415,6 +415,11 @@ public class InfoMediaManagerTest {
|
||||
assertThat(mInfoMediaManager.getSelectableMediaDevice()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adjustSessionVolume_routingSessionInfoIsNull_noCrash() {
|
||||
mInfoMediaManager.adjustSessionVolume(null, 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adjustSessionVolume_packageNameIsNull_noCrash() {
|
||||
mInfoMediaManager.mPackageName = null;
|
||||
@@ -486,6 +491,14 @@ public class InfoMediaManagerTest {
|
||||
assertThat(mInfoMediaManager.getSessionVolume()).isEqualTo(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getActiveMediaSession_returnActiveSession() {
|
||||
final List<RoutingSessionInfo> infos = new ArrayList<>();
|
||||
mShadowRouter2Manager.setActiveSessions(infos);
|
||||
|
||||
assertThat(mInfoMediaManager.getActiveMediaSession()).containsExactlyElementsIn(infos);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void releaseSession_packageNameIsNull_returnFalse() {
|
||||
mInfoMediaManager.mPackageName = null;
|
||||
|
||||
@@ -34,6 +34,7 @@ import android.bluetooth.BluetoothDevice;
|
||||
import android.content.Context;
|
||||
import android.media.MediaRoute2Info;
|
||||
import android.media.MediaRouter2Manager;
|
||||
import android.media.RoutingSessionInfo;
|
||||
|
||||
import com.android.settingslib.bluetooth.A2dpProfile;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
@@ -67,6 +68,7 @@ public class LocalMediaManagerTest {
|
||||
private static final String TEST_DEVICE_ID_3 = "device_id_3";
|
||||
private static final String TEST_CURRENT_DEVICE_ID = "currentDevice_id";
|
||||
private static final String TEST_PACKAGE_NAME = "com.test.playmusic";
|
||||
private static final String TEST_SESSION_ID = "session_id";
|
||||
|
||||
@Mock
|
||||
private InfoMediaManager mInfoMediaManager;
|
||||
@@ -525,40 +527,6 @@ public class LocalMediaManagerTest {
|
||||
.STATE_CONNECTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getActiveMediaDevice_checkList() {
|
||||
final List<MediaDevice> devices = new ArrayList<>();
|
||||
final MediaDevice device1 = mock(MediaDevice.class);
|
||||
final MediaDevice device2 = mock(MediaDevice.class);
|
||||
final MediaDevice device3 = mock(MediaDevice.class);
|
||||
device1.mType = MediaDevice.MediaDeviceType.TYPE_PHONE_DEVICE;
|
||||
device2.mType = MediaDevice.MediaDeviceType.TYPE_CAST_DEVICE;
|
||||
device3.mType = MediaDevice.MediaDeviceType.TYPE_BLUETOOTH_DEVICE;
|
||||
when(device1.getClientPackageName()).thenReturn(TEST_DEVICE_ID_1);
|
||||
when(device2.getClientPackageName()).thenReturn(TEST_DEVICE_ID_2);
|
||||
when(device3.getClientPackageName()).thenReturn(TEST_DEVICE_ID_3);
|
||||
when(device1.getId()).thenReturn(TEST_DEVICE_ID_1);
|
||||
when(device2.getId()).thenReturn(TEST_DEVICE_ID_2);
|
||||
when(device3.getId()).thenReturn(TEST_DEVICE_ID_3);
|
||||
devices.add(device1);
|
||||
devices.add(device2);
|
||||
devices.add(device3);
|
||||
mLocalMediaManager.registerCallback(mCallback);
|
||||
mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices);
|
||||
|
||||
List<MediaDevice> activeDevices = mLocalMediaManager.getActiveMediaDevice(
|
||||
MediaDevice.MediaDeviceType.TYPE_PHONE_DEVICE);
|
||||
assertThat(activeDevices).containsExactly(device1);
|
||||
|
||||
activeDevices = mLocalMediaManager.getActiveMediaDevice(
|
||||
MediaDevice.MediaDeviceType.TYPE_CAST_DEVICE);
|
||||
assertThat(activeDevices).containsExactly(device2);
|
||||
|
||||
activeDevices = mLocalMediaManager.getActiveMediaDevice(
|
||||
MediaDevice.MediaDeviceType.TYPE_BLUETOOTH_DEVICE);
|
||||
assertThat(activeDevices).containsExactly(device3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceAttributesChanged_shouldBeCalled() {
|
||||
mLocalMediaManager.registerCallback(mCallback);
|
||||
@@ -568,6 +536,18 @@ public class LocalMediaManagerTest {
|
||||
verify(mCallback).onDeviceAttributesChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getActiveMediaSession_verifyCorrectSession() {
|
||||
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
|
||||
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
|
||||
when(info.getId()).thenReturn(TEST_SESSION_ID);
|
||||
routingSessionInfos.add(info);
|
||||
when(mInfoMediaManager.getActiveMediaSession()).thenReturn(routingSessionInfos);
|
||||
|
||||
assertThat(mLocalMediaManager.getActiveMediaSession().get(0).getId())
|
||||
.matches(TEST_SESSION_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceListAdded_haveDisconnectedDevice_addDisconnectedDevice() {
|
||||
final List<MediaDevice> devices = new ArrayList<>();
|
||||
@@ -721,4 +701,17 @@ public class LocalMediaManagerTest {
|
||||
assertThat(mLocalMediaManager.mMediaDevices).hasSize(2);
|
||||
verify(mCallback).onDeviceListUpdate(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adjustSessionVolume_verifyCorrectSessionVolume() {
|
||||
final List<RoutingSessionInfo> routingSessionInfos = new ArrayList<>();
|
||||
final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
|
||||
when(info.getId()).thenReturn(TEST_SESSION_ID);
|
||||
routingSessionInfos.add(info);
|
||||
when(mInfoMediaManager.getActiveMediaSession()).thenReturn(routingSessionInfos);
|
||||
|
||||
mLocalMediaManager.adjustSessionVolume(TEST_SESSION_ID, 10);
|
||||
|
||||
verify(mInfoMediaManager).adjustSessionVolume(info, 10);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user