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

Commit e171e5e6 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun Committed by Automerger Merge Worker
Browse files

Merge "Introduce APIs to deal with RCN case" into rvc-dev am: 0de25056 am:...

Merge "Introduce APIs to deal with RCN case" into rvc-dev am: 0de25056 am: d9536603 am: d95a5578

Change-Id: I7df91f483fb321b55fa8e3e28f7e5d34d3c1dd17
parents 96df88ba d95a5578
Loading
Loading
Loading
Loading
+80 −2
Original line number Original line Diff line number Diff line
@@ -170,8 +170,7 @@ public final class MediaRouter2Manager {
    public MediaController getMediaControllerForRoutingSession(
    public MediaController getMediaControllerForRoutingSession(
            @NonNull RoutingSessionInfo sessionInfo) {
            @NonNull RoutingSessionInfo sessionInfo) {
        for (MediaController controller : mMediaSessionManager.getActiveSessions(null)) {
        for (MediaController controller : mMediaSessionManager.getActiveSessions(null)) {
            String volumeControlId = controller.getPlaybackInfo().getVolumeControlId();
            if (areSessionsMatched(controller, sessionInfo)) {
            if (TextUtils.equals(sessionInfo.getId(), volumeControlId)) {
                return controller;
                return controller;
            }
            }
        }
        }
@@ -205,6 +204,37 @@ public final class MediaRouter2Manager {
        return routes;
        return routes;
    }
    }


    /**
     * Gets available routes for the given routing session.
     * The returned routes can be passed to
     * {@link #transfer(RoutingSessionInfo, MediaRoute2Info)} for transferring the routing session.
     *
     * @param sessionInfo the routing session that would be transferred
     */
    @NonNull
    public List<MediaRoute2Info> getAvailableRoutesForRoutingSession(
            @NonNull RoutingSessionInfo sessionInfo) {
        Objects.requireNonNull(sessionInfo, "sessionInfo must not be null");

        List<MediaRoute2Info> routes = new ArrayList<>();

        String packageName = sessionInfo.getClientPackageName();
        List<String> preferredFeatures = mPreferredFeaturesMap.get(packageName);
        if (preferredFeatures == null) {
            preferredFeatures = Collections.emptyList();
        }
        synchronized (mRoutesLock) {
            for (MediaRoute2Info route : mRoutes.values()) {
                if (route.isSystemRoute() || route.hasAnyFeatures(preferredFeatures)
                        || sessionInfo.getSelectedRoutes().contains(route.getId())
                        || sessionInfo.getTransferableRoutes().contains(route.getId())) {
                    routes.add(route);
                }
            }
        }
        return routes;
    }

    /**
    /**
     * Gets the system routing session associated with no specific application.
     * Gets the system routing session associated with no specific application.
     */
     */
@@ -218,6 +248,33 @@ public final class MediaRouter2Manager {
        throw new IllegalStateException("No system routing session");
        throw new IllegalStateException("No system routing session");
    }
    }


    /**
     * Gets the routing session of a media session.
     * If the session is using {#link PlaybackInfo#PLAYBACK_TYPE_LOCAL local playback},
     * the system routing session is returned.
     * If the session is using {#link PlaybackInfo#PLAYBACK_TYPE_REMOTE remote playback},
     * it returns the corresponding routing session or {@code null} if it's unavailable.
     */
    @Nullable
    public RoutingSessionInfo getRoutingSessionForMediaController(MediaController mediaController) {
        MediaController.PlaybackInfo playbackInfo = mediaController.getPlaybackInfo();
        if (playbackInfo == null) {
            return null;
        }
        if (playbackInfo.getPlaybackType() == MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL) {
            return new RoutingSessionInfo.Builder(getSystemRoutingSession())
                    .setClientPackageName(mediaController.getPackageName())
                    .build();
        }
        for (RoutingSessionInfo sessionInfo : getActiveSessions()) {
            if (!sessionInfo.isSystemSession()
                    && areSessionsMatched(mediaController, sessionInfo)) {
                return sessionInfo;
            }
        }
        return null;
    }

    /**
    /**
     * Gets routing sessions of an application with the given package name.
     * Gets routing sessions of an application with the given package name.
     * The first element of the returned list is the system routing session.
     * The first element of the returned list is the system routing session.
@@ -762,6 +819,27 @@ public final class MediaRouter2Manager {
        }
        }
    }
    }


    private boolean areSessionsMatched(MediaController mediaController,
            RoutingSessionInfo sessionInfo) {
        MediaController.PlaybackInfo playbackInfo = mediaController.getPlaybackInfo();
        if (playbackInfo == null) {
            return false;
        }

        String volumeControlId = playbackInfo.getVolumeControlId();
        if (volumeControlId == null) {
            return false;
        }

        if (TextUtils.equals(volumeControlId, sessionInfo.getId())) {
            return true;
        }
        // Workaround for provider not being able to know the unique session ID.
        return TextUtils.equals(volumeControlId, sessionInfo.getOriginalId())
                && TextUtils.equals(mediaController.getPackageName(),
                sessionInfo.getOwnerPackageName());
    }

    private List<MediaRoute2Info> getRoutesWithIds(List<String> routeIds) {
    private List<MediaRoute2Info> getRoutesWithIds(List<String> routeIds) {
        synchronized (sLock) {
        synchronized (sLock) {
            return routeIds.stream().map(mRoutes::get)
            return routeIds.stream().map(mRoutes::get)
+27 −1
Original line number Original line Diff line number Diff line
@@ -50,6 +50,7 @@ public final class RoutingSessionInfo implements Parcelable {


    final String mId;
    final String mId;
    final CharSequence mName;
    final CharSequence mName;
    final String mOwnerPackageName;
    final String mClientPackageName;
    final String mClientPackageName;
    @Nullable
    @Nullable
    final String mProviderId;
    final String mProviderId;
@@ -71,6 +72,7 @@ public final class RoutingSessionInfo implements Parcelable {


        mId = builder.mId;
        mId = builder.mId;
        mName = builder.mName;
        mName = builder.mName;
        mOwnerPackageName = builder.mOwnerPackageName;
        mClientPackageName = builder.mClientPackageName;
        mClientPackageName = builder.mClientPackageName;
        mProviderId = builder.mProviderId;
        mProviderId = builder.mProviderId;


@@ -96,6 +98,7 @@ public final class RoutingSessionInfo implements Parcelable {


        mId = ensureString(src.readString());
        mId = ensureString(src.readString());
        mName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(src);
        mName = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(src);
        mOwnerPackageName = src.readString();
        mClientPackageName = ensureString(src.readString());
        mClientPackageName = ensureString(src.readString());
        mProviderId = src.readString();
        mProviderId = src.readString();


@@ -158,6 +161,15 @@ public final class RoutingSessionInfo implements Parcelable {
        return mId;
        return mId;
    }
    }


    /**
     * Gets the package name of the session owner.
     * @hide
     */
    @Nullable
    public String getOwnerPackageName() {
        return mOwnerPackageName;
    }

    /**
    /**
     * Gets the client package name of the session
     * Gets the client package name of the session
     */
     */
@@ -263,6 +275,7 @@ public final class RoutingSessionInfo implements Parcelable {
    public void writeToParcel(@NonNull Parcel dest, int flags) {
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeString(mId);
        dest.writeString(mId);
        dest.writeCharSequence(mName);
        dest.writeCharSequence(mName);
        dest.writeString(mOwnerPackageName);
        dest.writeString(mClientPackageName);
        dest.writeString(mClientPackageName);
        dest.writeString(mProviderId);
        dest.writeString(mProviderId);
        dest.writeStringList(mSelectedRoutes);
        dest.writeStringList(mSelectedRoutes);
@@ -288,6 +301,7 @@ public final class RoutingSessionInfo implements Parcelable {
        RoutingSessionInfo other = (RoutingSessionInfo) obj;
        RoutingSessionInfo other = (RoutingSessionInfo) obj;
        return Objects.equals(mId, other.mId)
        return Objects.equals(mId, other.mId)
                && Objects.equals(mName, other.mName)
                && Objects.equals(mName, other.mName)
                && Objects.equals(mOwnerPackageName, other.mOwnerPackageName)
                && Objects.equals(mClientPackageName, other.mClientPackageName)
                && Objects.equals(mClientPackageName, other.mClientPackageName)
                && Objects.equals(mProviderId, other.mProviderId)
                && Objects.equals(mProviderId, other.mProviderId)
                && Objects.equals(mSelectedRoutes, other.mSelectedRoutes)
                && Objects.equals(mSelectedRoutes, other.mSelectedRoutes)
@@ -301,7 +315,7 @@ public final class RoutingSessionInfo implements Parcelable {


    @Override
    @Override
    public int hashCode() {
    public int hashCode() {
        return Objects.hash(mId, mName, mClientPackageName, mProviderId,
        return Objects.hash(mId, mName, mOwnerPackageName, mClientPackageName, mProviderId,
                mSelectedRoutes, mSelectableRoutes, mDeselectableRoutes, mTransferableRoutes,
                mSelectedRoutes, mSelectableRoutes, mDeselectableRoutes, mTransferableRoutes,
                mVolumeMax, mVolumeHandling, mVolume);
                mVolumeMax, mVolumeHandling, mVolume);
    }
    }
@@ -356,6 +370,7 @@ public final class RoutingSessionInfo implements Parcelable {
        // TODO: Reorder these (important ones first)
        // TODO: Reorder these (important ones first)
        final String mId;
        final String mId;
        CharSequence mName;
        CharSequence mName;
        String mOwnerPackageName;
        String mClientPackageName;
        String mClientPackageName;
        String mProviderId;
        String mProviderId;
        final List<String> mSelectedRoutes;
        final List<String> mSelectedRoutes;
@@ -439,6 +454,17 @@ public final class RoutingSessionInfo implements Parcelable {
            return this;
            return this;
        }
        }


        /**
         * Sets the package name of the session owner. It is expected to be called by the system.
         *
         * @hide
         */
        @NonNull
        public Builder setOwnerPackageName(@Nullable String packageName) {
            mOwnerPackageName = packageName;
            return this;
        }

        /**
        /**
         * Sets the client package name of the session.
         * Sets the client package name of the session.
         *
         *
+10 −9
Original line number Original line Diff line number Diff line
@@ -315,9 +315,7 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider
            return;
            return;
        }
        }


        sessionInfo = new RoutingSessionInfo.Builder(sessionInfo)
        sessionInfo = updateSessionInfo(sessionInfo);
                .setProviderId(getUniqueId())
                .build();


        boolean duplicateSessionAlreadyExists = false;
        boolean duplicateSessionAlreadyExists = false;
        synchronized (mLock) {
        synchronized (mLock) {
@@ -348,9 +346,7 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider
            return;
            return;
        }
        }


        sessionInfo = new RoutingSessionInfo.Builder(sessionInfo)
        sessionInfo = updateSessionInfo(sessionInfo);
                .setProviderId(getUniqueId())
                .build();


        boolean found = false;
        boolean found = false;
        synchronized (mLock) {
        synchronized (mLock) {
@@ -380,9 +376,7 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider
            return;
            return;
        }
        }


        sessionInfo = new RoutingSessionInfo.Builder(sessionInfo)
        sessionInfo = updateSessionInfo(sessionInfo);
                .setProviderId(getUniqueId())
                .build();


        boolean found = false;
        boolean found = false;
        synchronized (mLock) {
        synchronized (mLock) {
@@ -403,6 +397,13 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider
        mCallback.onSessionReleased(this, sessionInfo);
        mCallback.onSessionReleased(this, sessionInfo);
    }
    }


    private RoutingSessionInfo updateSessionInfo(RoutingSessionInfo sessionInfo) {
        return new RoutingSessionInfo.Builder(sessionInfo)
                .setOwnerPackageName(mComponentName.getPackageName())
                .setProviderId(getUniqueId())
                .build();
    }

    private void onRequestFailed(Connection connection, long requestId, int reason) {
    private void onRequestFailed(Connection connection, long requestId, int reason) {
        if (mActiveConnection != connection) {
        if (mActiveConnection != connection) {
            return;
            return;