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

Commit 52190729 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

Fix RoutingSessionInfo route ID issue

In the previous CL, we relied on MediaRouterUtils.getOriginalId()
that returns a wrong ID when it has no provider ID but the separator.

Resolve the issue by resetting route ID in Builder class.

Bug: 150666870
Bug: 150751854
Test: cts test realted to MediaRouter2
Change-Id: Ib17cdc8385308ccadad79e117fcfede72b8af6b5
parent b979998e
Loading
Loading
Loading
Loading
+3 −7
Original line number Diff line number Diff line
@@ -304,13 +304,9 @@ public class MediaRouter2Manager {
     * @see Callback#onTransferFailed(RoutingSessionInfo, MediaRoute2Info)
     */
    public void transfer(@NonNull RoutingSessionInfo sessionInfo,
            @Nullable MediaRoute2Info route) {
            @NonNull MediaRoute2Info route) {
        Objects.requireNonNull(sessionInfo, "sessionInfo must not be null");

        if (route == null) {
            releaseSession(sessionInfo);
            return;
        }
        Objects.requireNonNull(route, "route must not be null");

        //TODO: Ignore unknown route.
        if (sessionInfo.getTransferableRoutes().contains(route.getId())) {
@@ -334,10 +330,10 @@ public class MediaRouter2Manager {
                int requestId = mNextRequestId.getAndIncrement();
                mMediaRouterService.requestCreateSessionWithManager(
                        client, sessionInfo.getClientPackageName(), route, requestId);
                releaseSession(sessionInfo);
            } catch (RemoteException ex) {
                Log.e(TAG, "Unable to select media route", ex);
            }
            releaseSession(sessionInfo);
        }
    }

+43 −30
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
@@ -73,10 +74,14 @@ public final class RoutingSessionInfo implements Parcelable {
        mClientPackageName = builder.mClientPackageName;
        mProviderId = builder.mProviderId;

        mSelectedRoutes = Collections.unmodifiableList(builder.mSelectedRoutes);
        mSelectableRoutes = Collections.unmodifiableList(builder.mSelectableRoutes);
        mDeselectableRoutes = Collections.unmodifiableList(builder.mDeselectableRoutes);
        mTransferableRoutes = Collections.unmodifiableList(builder.mTransferableRoutes);
        mSelectedRoutes = Collections.unmodifiableList(
                convertToUniqueRouteIds(builder.mSelectedRoutes));
        mSelectableRoutes = Collections.unmodifiableList(
                convertToUniqueRouteIds(builder.mSelectableRoutes));
        mDeselectableRoutes = Collections.unmodifiableList(
                convertToUniqueRouteIds(builder.mDeselectableRoutes));
        mTransferableRoutes = Collections.unmodifiableList(
                convertToUniqueRouteIds(builder.mTransferableRoutes));

        mVolumeHandling = builder.mVolumeHandling;
        mVolumeMax = builder.mVolumeMax;
@@ -326,6 +331,24 @@ public final class RoutingSessionInfo implements Parcelable {
        return result.toString();
    }

    private List<String> convertToUniqueRouteIds(@NonNull List<String> routeIds) {
        if (routeIds == null) {
            Log.w(TAG, "routeIds is null. Returning an empty list");
            return Collections.emptyList();
        }

        // mProviderId can be null if not set. Return the original list for this case.
        if (mProviderId == null) {
            return routeIds;
        }

        List<String> result = new ArrayList<>();
        for (String routeId : routeIds) {
            result.add(MediaRouter2Utils.toUniqueId(mProviderId, routeId));
        }
        return result;
    }

    /**
     * Builder class for {@link RoutingSessionInfo}.
     */
@@ -345,7 +368,6 @@ public final class RoutingSessionInfo implements Parcelable {
        Bundle mControlHints;
        boolean mIsSystemSession;

        //TODO: Remove this.
        /**
         * Constructor for builder to create {@link RoutingSessionInfo}.
         * <p>
@@ -392,6 +414,14 @@ public final class RoutingSessionInfo implements Parcelable {
            mDeselectableRoutes = new ArrayList<>(sessionInfo.mDeselectableRoutes);
            mTransferableRoutes = new ArrayList<>(sessionInfo.mTransferableRoutes);

            if (mProviderId != null) {
                // They must have unique IDs.
                mSelectedRoutes.replaceAll(MediaRouter2Utils::getOriginalId);
                mSelectableRoutes.replaceAll(MediaRouter2Utils::getOriginalId);
                mDeselectableRoutes.replaceAll(MediaRouter2Utils::getOriginalId);
                mTransferableRoutes.replaceAll(MediaRouter2Utils::getOriginalId);
            }

            mVolumeHandling = sessionInfo.mVolumeHandling;
            mVolumeMax = sessionInfo.mVolumeMax;
            mVolume = sessionInfo.mVolume;
@@ -431,12 +461,6 @@ public final class RoutingSessionInfo implements Parcelable {
                throw new IllegalArgumentException("providerId must not be empty");
            }
            mProviderId = providerId;

            mSelectedRoutes.replaceAll(routeId -> getUniqueId(mProviderId, routeId));
            mSelectableRoutes.replaceAll(routeId -> getUniqueId(mProviderId, routeId));
            mDeselectableRoutes.replaceAll(routeId -> getUniqueId(mProviderId, routeId));
            mTransferableRoutes.replaceAll(routeId -> getUniqueId(mProviderId, routeId));

            return this;
        }

@@ -457,7 +481,7 @@ public final class RoutingSessionInfo implements Parcelable {
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mSelectedRoutes.add(getUniqueId(mProviderId, routeId));
            mSelectedRoutes.add(routeId);
            return this;
        }

@@ -469,7 +493,7 @@ public final class RoutingSessionInfo implements Parcelable {
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mSelectedRoutes.remove(getUniqueId(mProviderId, routeId));
            mSelectedRoutes.remove(routeId);
            return this;
        }

@@ -490,7 +514,7 @@ public final class RoutingSessionInfo implements Parcelable {
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mSelectableRoutes.add(getUniqueId(mProviderId, routeId));
            mSelectableRoutes.add(routeId);
            return this;
        }

@@ -502,7 +526,7 @@ public final class RoutingSessionInfo implements Parcelable {
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mSelectableRoutes.remove(getUniqueId(mProviderId, routeId));
            mSelectableRoutes.remove(routeId);
            return this;
        }

@@ -523,7 +547,7 @@ public final class RoutingSessionInfo implements Parcelable {
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mDeselectableRoutes.add(getUniqueId(mProviderId, routeId));
            mDeselectableRoutes.add(routeId);
            return this;
        }

@@ -535,7 +559,7 @@ public final class RoutingSessionInfo implements Parcelable {
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mDeselectableRoutes.remove(getUniqueId(mProviderId, routeId));
            mDeselectableRoutes.remove(routeId);
            return this;
        }

@@ -556,7 +580,7 @@ public final class RoutingSessionInfo implements Parcelable {
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mTransferableRoutes.add(getUniqueId(mProviderId, routeId));
            mTransferableRoutes.add(routeId);
            return this;
        }

@@ -568,7 +592,7 @@ public final class RoutingSessionInfo implements Parcelable {
            if (TextUtils.isEmpty(routeId)) {
                throw new IllegalArgumentException("routeId must not be empty");
            }
            mTransferableRoutes.remove(getUniqueId(mProviderId, routeId));
            mTransferableRoutes.remove(routeId);
            return this;
        }

@@ -634,15 +658,4 @@ public final class RoutingSessionInfo implements Parcelable {
            return new RoutingSessionInfo(this);
        }
    }

    private static String getUniqueId(String providerId, String routeId) {
        if (TextUtils.isEmpty(providerId)) {
            return routeId;
        }
        String originalId = MediaRouter2Utils.getOriginalId(routeId);
        if (originalId == null) {
            originalId = routeId;
        }
        return MediaRouter2Utils.toUniqueId(providerId, originalId);
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -127,7 +127,8 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
    @Override
    public void requestCreateSession(String packageName, String routeId, long requestId,
            Bundle sessionHints) {
        // Do nothing
        // Handle it as an internal transfer.
        transferToRoute(SYSTEM_SESSION_ID, routeId, requestId);
    }

    @Override
+1 −1

File changed.

Contains only whitespace changes.