Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c95f926f authored by Iván Budnik's avatar Iván Budnik Committed by Android (Google) Code Review
Browse files

Merge "Non functional cleanup and refactor of InfoMediaManager"

parents 6fd1d8b8 82057cef
Loading
Loading
Loading
Loading
+93 −95
Original line number Diff line number Diff line
@@ -142,13 +142,12 @@ public class InfoMediaManager extends MediaManager {
     * Transfer MediaDevice for media without package name.
     */
    boolean connectDeviceWithoutPackageName(MediaDevice device) {
        boolean isConnected = false;
        final RoutingSessionInfo info = mRouterManager.getSystemRoutingSession(null);
        if (info != null) {
            mRouterManager.transfer(info, device.mRouteInfo);
            isConnected = true;
            return true;
        }
        return isConnected;
        return false;
    }

    /**
@@ -164,17 +163,16 @@ public class InfoMediaManager extends MediaManager {
        }

        final RoutingSessionInfo info = getRoutingSessionInfo();
        if (info != null && info.getSelectableRoutes().contains(device.mRouteInfo.getId())) {
            mRouterManager.selectRoute(info, device.mRouteInfo);
            return true;
        }

        if (info == null || !info.getSelectableRoutes().contains(device.mRouteInfo.getId())) {
            Log.w(TAG, "addDeviceToPlayMedia() Ignoring selecting a non-selectable device : "
                    + device.getName());

            return false;
        }

        mRouterManager.selectRoute(info, device.mRouteInfo);
        return true;
    }

    private RoutingSessionInfo getRoutingSessionInfo() {
        return getRoutingSessionInfo(mPackageName);
    }
@@ -190,8 +188,7 @@ public class InfoMediaManager extends MediaManager {
    }

    boolean isRoutingSessionAvailableForVolumeControl() {
        List<RoutingSessionInfo> sessions =
                mRouterManager.getRoutingSessions(mPackageName);
        List<RoutingSessionInfo> sessions = mRouterManager.getRoutingSessions(mPackageName);

        for (RoutingSessionInfo session : sessions) {
            if (!session.isSystemSession()
@@ -230,17 +227,16 @@ public class InfoMediaManager extends MediaManager {
        }

        final RoutingSessionInfo info = getRoutingSessionInfo();
        if (info != null && info.getSelectedRoutes().contains(device.mRouteInfo.getId())) {
            mRouterManager.deselectRoute(info, device.mRouteInfo);
            return true;
        }

        if (info == null || !info.getSelectedRoutes().contains(device.mRouteInfo.getId())) {
            Log.w(TAG, "removeDeviceFromMedia() Ignoring deselecting a non-deselectable device : "
                    + device.getName());

            return false;
        }

        mRouterManager.deselectRoute(info, device.mRouteInfo);
        return true;
    }

    /**
     * Release session to stop playing media on MediaDevice.
     */
@@ -251,31 +247,35 @@ public class InfoMediaManager extends MediaManager {
        }

        final RoutingSessionInfo sessionInfo = getRoutingSessionInfo();
        if (sessionInfo == null) {
            Log.w(TAG, "releaseSession() Ignoring release session : " + mPackageName);
            return false;
        }

        if (sessionInfo != null) {
        mRouterManager.releaseSession(sessionInfo);
        return true;
    }

        Log.w(TAG, "releaseSession() Ignoring release session : " + mPackageName);

        return false;
    }

    /**
     * Get the MediaDevice list that can be added to current media.
     *
     * @return list of MediaDevice
     * Returns the list of {@link MediaDevice media devices} that can be added to the current {@link
     * RoutingSessionInfo routing session}.
     */
    List<MediaDevice> getSelectableMediaDevice() {
        final List<MediaDevice> deviceList = new ArrayList<>();
    @NonNull
    List<MediaDevice> getSelectableMediaDevices() {
        if (TextUtils.isEmpty(mPackageName)) {
            Log.w(TAG, "getSelectableMediaDevice() package name is null or empty!");
            return deviceList;
            Log.w(TAG, "getSelectableMediaDevices() package name is null or empty!");
            return Collections.emptyList();
        }

        final RoutingSessionInfo info = getRoutingSessionInfo();
        if (info != null) {
        if (info == null) {
            Log.w(TAG, "getSelectableMediaDevices() cannot find selectable MediaDevice from : "
                    + mPackageName);
            return Collections.emptyList();
        }

        final List<MediaDevice> deviceList = new ArrayList<>();
        for (MediaRoute2Info route : mRouterManager.getSelectableRoutes(info)) {
            deviceList.add(new InfoMediaDevice(mContext, mRouterManager,
                    route, mPackageName, mPreferenceItemMap.get(route.getId())));
@@ -283,26 +283,25 @@ public class InfoMediaManager extends MediaManager {
        return deviceList;
    }

        Log.w(TAG, "getSelectableMediaDevice() cannot found selectable MediaDevice from : "
                + mPackageName);

        return deviceList;
    }

    /**
     * Get the MediaDevice list that can be removed from current media session.
     *
     * @return list of MediaDevice
     * Returns the list of {@link MediaDevice media devices} that can be deselected from the current
     * {@link RoutingSessionInfo routing session}.
     */
    List<MediaDevice> getDeselectableMediaDevice() {
        final List<MediaDevice> deviceList = new ArrayList<>();
    @NonNull
    List<MediaDevice> getDeselectableMediaDevices() {
        if (TextUtils.isEmpty(mPackageName)) {
            Log.d(TAG, "getDeselectableMediaDevice() package name is null or empty!");
            return deviceList;
            Log.d(TAG, "getDeselectableMediaDevices() package name is null or empty!");
            return Collections.emptyList();
        }

        final RoutingSessionInfo info = getRoutingSessionInfo();
        if (info != null) {
        if (info == null) {
            Log.d(TAG, "getDeselectableMediaDevices() cannot find deselectable MediaDevice from : "
                    + mPackageName);
            return Collections.emptyList();
        }

        final List<MediaDevice> deviceList = new ArrayList<>();
        for (MediaRoute2Info route : mRouterManager.getDeselectableRoutes(info)) {
            deviceList.add(new InfoMediaDevice(mContext, mRouterManager,
                    route, mPackageName, mPreferenceItemMap.get(route.getId())));
@@ -310,26 +309,26 @@ public class InfoMediaManager extends MediaManager {
        }
        return deviceList;
    }
        Log.d(TAG, "getDeselectableMediaDevice() cannot found deselectable MediaDevice from : "
                + mPackageName);

        return deviceList;
    }

    /**
     * Get the MediaDevice list that has been selected to current media.
     *
     * @return list of MediaDevice
     * Returns the list of {@link MediaDevice media devices} that are selected in the current {@link
     * RoutingSessionInfo routing session}.
     */
    List<MediaDevice> getSelectedMediaDevice() {
        final List<MediaDevice> deviceList = new ArrayList<>();
    @NonNull
    List<MediaDevice> getSelectedMediaDevices() {
        if (TextUtils.isEmpty(mPackageName)) {
            Log.w(TAG, "getSelectedMediaDevice() package name is null or empty!");
            return deviceList;
            Log.w(TAG, "getSelectedMediaDevices() package name is null or empty!");
            return Collections.emptyList();
        }

        final RoutingSessionInfo info = getRoutingSessionInfo();
        if (info != null) {
        if (info == null) {
            Log.w(TAG, "getSelectedMediaDevices() cannot find selectable MediaDevice from : "
                    + mPackageName);
            return Collections.emptyList();
        }

        final List<MediaDevice> deviceList = new ArrayList<>();
        for (MediaRoute2Info route : mRouterManager.getSelectedRoutes(info)) {
            deviceList.add(new InfoMediaDevice(mContext, mRouterManager,
                    route, mPackageName, mPreferenceItemMap.get(route.getId())));
@@ -337,12 +336,6 @@ public class InfoMediaManager extends MediaManager {
        return deviceList;
    }

        Log.w(TAG, "getSelectedMediaDevice() cannot found selectable MediaDevice from : "
                + mPackageName);

        return deviceList;
    }

    void adjustSessionVolume(RoutingSessionInfo info, int volume) {
        if (info == null) {
            Log.w(TAG, "Unable to adjust session volume. RoutingSessionInfo is empty");
@@ -364,15 +357,14 @@ public class InfoMediaManager extends MediaManager {
        }

        final RoutingSessionInfo info = getRoutingSessionInfo();
        if (info != null) {
            Log.d(TAG, "adjustSessionVolume() adjust volume : " + volume + ", with : "
        if (info == null) {
            Log.w(TAG, "adjustSessionVolume() can't found corresponding RoutingSession with : "
                    + mPackageName);
            mRouterManager.setSessionVolume(info, volume);
            return;
        }

        Log.w(TAG, "adjustSessionVolume() can't found corresponding RoutingSession with : "
                + mPackageName);
        Log.d(TAG, "adjustSessionVolume() adjust volume: " + volume + ", with : " + mPackageName);
        mRouterManager.setSessionVolume(info, volume);
    }

    /**
@@ -387,15 +379,15 @@ public class InfoMediaManager extends MediaManager {
        }

        final RoutingSessionInfo info = getRoutingSessionInfo();
        if (info != null) {
            return info.getVolumeMax();
        }

        Log.w(TAG, "getSessionVolumeMax() can't found corresponding RoutingSession with : "
        if (info == null) {
            Log.w(TAG, "getSessionVolumeMax() can't find corresponding RoutingSession with : "
                    + mPackageName);
            return -1;
        }

        return info.getVolumeMax();
    }

    /**
     * Gets the current volume of the {@link android.media.RoutingSessionInfo}.
     *
@@ -408,15 +400,15 @@ public class InfoMediaManager extends MediaManager {
        }

        final RoutingSessionInfo info = getRoutingSessionInfo();
        if (info != null) {
            return info.getVolume();
        }

        Log.w(TAG, "getSessionVolume() can't found corresponding RoutingSession with : "
        if (info == null) {
            Log.w(TAG, "getSessionVolume() can't find corresponding RoutingSession with : "
                    + mPackageName);
            return -1;
        }

        return info.getVolume();
    }

    CharSequence getSessionName() {
        if (TextUtils.isEmpty(mPackageName)) {
            Log.w(TAG, "Unable to get session name. The package name is null or empty!");
@@ -424,14 +416,14 @@ public class InfoMediaManager extends MediaManager {
        }

        final RoutingSessionInfo info = getRoutingSessionInfo();
        if (info != null) {
            return info.getName();
        }

        if (info == null) {
            Log.w(TAG, "Unable to get session name for package: " + mPackageName);
            return null;
        }

        return info.getName();
    }

    boolean shouldDisableMediaOutput(String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            Log.w(TAG, "shouldDisableMediaOutput() package name is null or empty!");
@@ -473,7 +465,12 @@ public class InfoMediaManager extends MediaManager {
        }
    }

    List<RoutingSessionInfo> getActiveMediaSession() {
    /**
     * Returns the list of currently active {@link RoutingSessionInfo routing sessions} known to the
     * system.
     */
    @NonNull
    List<RoutingSessionInfo> getActiveRoutingSessions() {
        List<RoutingSessionInfo> infos = new ArrayList<>();
        infos.add(mRouterManager.getSystemRoutingSession(null));
        infos.addAll(mRouterManager.getRemoteSessions());
@@ -631,7 +628,8 @@ public class InfoMediaManager extends MediaManager {
        }

        /**
         * Ignore callback here since we'll also receive {@link onRequestFailed} with reason code.
         * Ignore callback here since we'll also receive{@link
         * MediaRouter2Manager.Callback#onRequestFailed onRequestFailed} with reason code.
         */
        @Override
        public void onTransferFailed(RoutingSessionInfo session, MediaRoute2Info route) {
+4 −4
Original line number Diff line number Diff line
@@ -346,7 +346,7 @@ public class LocalMediaManager implements BluetoothCallback {
     * @return list of MediaDevice
     */
    public List<MediaDevice> getSelectableMediaDevice() {
        return mInfoMediaManager.getSelectableMediaDevice();
        return mInfoMediaManager.getSelectableMediaDevices();
    }

    /**
@@ -355,7 +355,7 @@ public class LocalMediaManager implements BluetoothCallback {
     * @return list of MediaDevice
     */
    public List<MediaDevice> getDeselectableMediaDevice() {
        return mInfoMediaManager.getDeselectableMediaDevice();
        return mInfoMediaManager.getDeselectableMediaDevices();
    }

    /**
@@ -371,7 +371,7 @@ public class LocalMediaManager implements BluetoothCallback {
     * @return list of MediaDevice
     */
    public List<MediaDevice> getSelectedMediaDevice() {
        return mInfoMediaManager.getSelectedMediaDevice();
        return mInfoMediaManager.getSelectedMediaDevices();
    }

    /**
@@ -433,7 +433,7 @@ public class LocalMediaManager implements BluetoothCallback {
     * @return current active session list{@link android.media.RoutingSessionInfo}
     */
    public List<RoutingSessionInfo> getActiveMediaSession() {
        return mInfoMediaManager.getActiveMediaSession();
        return mInfoMediaManager.getActiveRoutingSessions();
    }

    /**
+5 −5
Original line number Diff line number Diff line
@@ -668,7 +668,7 @@ public class InfoMediaManagerTest {
    public void getSelectableMediaDevice_packageNameIsNull_returnFalse() {
        mInfoMediaManager.mPackageName = null;

        assertThat(mInfoMediaManager.getSelectableMediaDevice()).isEmpty();
        assertThat(mInfoMediaManager.getSelectableMediaDevices()).isEmpty();
    }

    @Test
@@ -680,14 +680,14 @@ public class InfoMediaManagerTest {
        mShadowRouter2Manager.setRoutingSessions(routingSessionInfos);
        when(info.getClientPackageName()).thenReturn("com.fake.packagename");

        assertThat(mInfoMediaManager.getSelectableMediaDevice()).isEmpty();
        assertThat(mInfoMediaManager.getSelectableMediaDevices()).isEmpty();
    }

    @Test
    public void getDeselectableMediaDevice_packageNameIsNull_returnFalse() {
        mInfoMediaManager.mPackageName = null;

        assertThat(mInfoMediaManager.getDeselectableMediaDevice()).isEmpty();
        assertThat(mInfoMediaManager.getDeselectableMediaDevices()).isEmpty();
    }

    @Test
@@ -703,7 +703,7 @@ public class InfoMediaManagerTest {
        when(mediaRoute2Info.getName()).thenReturn(TEST_NAME);
        when(mediaRoute2Info.getId()).thenReturn(TEST_ID);

        final List<MediaDevice> mediaDevices = mInfoMediaManager.getDeselectableMediaDevice();
        final List<MediaDevice> mediaDevices = mInfoMediaManager.getDeselectableMediaDevices();

        assertThat(mediaDevices.size()).isEqualTo(1);
        assertThat(mediaDevices.get(0).getName()).isEqualTo(TEST_NAME);
@@ -797,7 +797,7 @@ public class InfoMediaManagerTest {
        mShadowRouter2Manager.setSystemRoutingSession(sysSessionInfo);
        mShadowRouter2Manager.setRemoteSessions(infos);

        assertThat(mInfoMediaManager.getActiveMediaSession())
        assertThat(mInfoMediaManager.getActiveRoutingSessions())
                .containsExactlyElementsIn(activeSessionInfos);
    }

+2 −2
Original line number Diff line number Diff line
@@ -435,7 +435,7 @@ public class LocalMediaManagerTest {
        final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
        when(info.getId()).thenReturn(TEST_SESSION_ID);
        routingSessionInfos.add(info);
        when(mInfoMediaManager.getActiveMediaSession()).thenReturn(routingSessionInfos);
        when(mInfoMediaManager.getActiveRoutingSessions()).thenReturn(routingSessionInfos);

        assertThat(mLocalMediaManager.getActiveMediaSession().get(0).getId())
                .matches(TEST_SESSION_ID);
@@ -546,7 +546,7 @@ public class LocalMediaManagerTest {
        final RoutingSessionInfo info = mock(RoutingSessionInfo.class);
        when(info.getId()).thenReturn(TEST_SESSION_ID);
        routingSessionInfos.add(info);
        when(mInfoMediaManager.getActiveMediaSession()).thenReturn(routingSessionInfos);
        when(mInfoMediaManager.getActiveRoutingSessions()).thenReturn(routingSessionInfos);

        mLocalMediaManager.adjustSessionVolume(TEST_SESSION_ID, 10);