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

Commit dda8cdd5 authored by Hyundo Moon's avatar Hyundo Moon Committed by Android (Google) Code Review
Browse files

Merge "MediaRouter2: Add OnCreateSessionListener"

parents 42b4cba1 84e027d6
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -18,13 +18,15 @@ package android.media;

import android.content.Intent;
import android.media.IMediaRoute2ProviderClient;
import android.os.Bundle;

/**
 * {@hide}
 */
oneway interface IMediaRoute2Provider {
    void setClient(IMediaRoute2ProviderClient client);
    void requestCreateSession(String packageName, String routeId, long requestId);
    void requestCreateSession(String packageName, String routeId, long requestId,
            in @nullable Bundle sessionHints);
    void releaseSession(String sessionId);

    void selectRoute(String sessionId, String routeId);
+3 −1
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.media.MediaRoute2Info;
import android.media.MediaRouterClientState;
import android.media.RouteDiscoveryPreference;
import android.media.RoutingSessionInfo;
import android.os.Bundle;

/**
 * {@hide}
@@ -52,7 +53,8 @@ interface IMediaRouterService {
    void requestSetVolume2(IMediaRouter2Client client, in MediaRoute2Info route, int volume);
    void requestUpdateVolume2(IMediaRouter2Client client, in MediaRoute2Info route, int direction);

    void requestCreateSession(IMediaRouter2Client client, in MediaRoute2Info route, int requestId);
    void requestCreateSession(IMediaRouter2Client client, in MediaRoute2Info route, int requestId,
            in @nullable Bundle sessionHints);
    void setDiscoveryRequest2(IMediaRouter2Client client, in RouteDiscoveryPreference preference);
    void selectRoute(IMediaRouter2Client client, String sessionId, in MediaRoute2Info route);
    void deselectRoute(IMediaRouter2Client client, String sessionId, in MediaRoute2Info route);
+12 −7
Original line number Diff line number Diff line
@@ -161,8 +161,8 @@ public abstract class MediaRoute2ProviderService extends Service {
     * @param sessionInfo information of the new session.
     *                    The {@link RoutingSessionInfo#getId() id} of the session must be unique.
     * @param requestId id of the previous request to create this session provided in
     *                  {@link #onCreateSession(String, String, long)}
     * @see #onCreateSession(String, String, long)
     *                  {@link #onCreateSession(String, String, long, Bundle)}
     * @see #onCreateSession(String, String, long, Bundle)
     * @hide
     */
    public final void notifySessionCreated(@NonNull RoutingSessionInfo sessionInfo,
@@ -196,8 +196,8 @@ public abstract class MediaRoute2ProviderService extends Service {
     * Notifies clients of that the session could not be created.
     *
     * @param requestId id of the previous request to create the session provided in
     *                  {@link #onCreateSession(String, String, long)}.
     * @see #onCreateSession(String, String, long)
     *                  {@link #onCreateSession(String, String, long, Bundle)}.
     * @see #onCreateSession(String, String, long, Bundle)
     * @hide
     */
    public final void notifySessionCreationFailed(long requestId) {
@@ -290,6 +290,9 @@ public abstract class MediaRoute2ProviderService extends Service {
     * @param packageName the package name of the application that selected the route
     * @param routeId the id of the route initially being connected
     * @param requestId the id of this session creation request
     * @param sessionHints an optional bundle of app-specific arguments sent by
     *                     {@link MediaRouter2}, or null if none. The contents of this bundle
     *                     may affect the result of session creation.
     *
     * @see RoutingSessionInfo.Builder#Builder(String, String)
     * @see RoutingSessionInfo.Builder#addSelectedRoute(String)
@@ -297,7 +300,7 @@ public abstract class MediaRoute2ProviderService extends Service {
     * @hide
     */
    public abstract void onCreateSession(@NonNull String packageName, @NonNull String routeId,
            long requestId);
            long requestId, @Nullable Bundle sessionHints);

    /**
     * Called when the session should be released. A client of the session or system can request
@@ -432,12 +435,14 @@ public abstract class MediaRoute2ProviderService extends Service {
        }

        @Override
        public void requestCreateSession(String packageName, String routeId, long requestId) {
        public void requestCreateSession(String packageName, String routeId, long requestId,
                @Nullable Bundle requestCreateSession) {
            if (!checkCallerisSystem()) {
                return;
            }
            mHandler.sendMessage(obtainMessage(MediaRoute2ProviderService::onCreateSession,
                    MediaRoute2ProviderService.this, packageName, routeId, requestId));
                    MediaRoute2ProviderService.this, packageName, routeId, requestId,
                    requestCreateSession));
        }

        @Override
+54 −1
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ public class MediaRouter2 {
    @GuardedBy("sRouterLock")
    private boolean mShouldUpdateRoutes;
    private volatile List<MediaRoute2Info> mFilteredRoutes = Collections.emptyList();
    private volatile OnCreateSessionListener mOnCreateSessionListener;

    /**
     * Gets an instance of the media router associated with the context.
@@ -280,6 +281,19 @@ public class MediaRouter2 {
        }
    }

    /**
     * Sets an {@link OnCreateSessionListener} to send hints when creating a session.
     * To send the hints, listener should be set <em>BEFORE</em> calling
     * {@link #requestCreateSession(MediaRoute2Info)}.
     *
     * @param listener A listener to send optional app-specific hints when creating a session.
     *                 {@code null} for unset.
     * @hide
     */
    public void setOnCreateSessionListener(@Nullable OnCreateSessionListener listener) {
        mOnCreateSessionListener = listener;
    }

    /**
     * Requests the media route provider service to create a session with the given route.
     *
@@ -300,13 +314,24 @@ public class MediaRouter2 {
        SessionCreationRequest request = new SessionCreationRequest(requestId, route);
        mSessionCreationRequests.add(request);


        OnCreateSessionListener listener = mOnCreateSessionListener;
        Bundle sessionHints = null;
        if (listener != null) {
            sessionHints = listener.onCreateSession(route);
            if (sessionHints != null) {
                sessionHints = new Bundle(sessionHints);
            }
        }

        Client2 client;
        synchronized (sRouterLock) {
            client = mClient;
        }
        if (client != null) {
            try {
                mMediaRouterService.requestCreateSession(client, route, requestId);
                mMediaRouterService.requestCreateSession(client, route, requestId,
                        sessionHints);
            } catch (RemoteException ex) {
                Log.e(TAG, "Unable to request to create session.", ex);
                mHandler.sendMessage(obtainMessage(MediaRouter2::createControllerOnHandler,
@@ -706,6 +731,34 @@ public class MediaRouter2 {
        public void onSessionReleased(@NonNull RoutingController controller) {}
    }

    /**
     * A listener interface to send an optional app-specific hints when creating a session.
     *
     * @hide
     */
    public interface OnCreateSessionListener {
        /**
         * Called when the {@link MediaRouter2} is about to request
         * the media route provider service to create a session with the given route.
         * The {@link Bundle} returned here will be sent to media route provider service as a hint
         * for creating a session.
         * <p>
         * To send hints when creating the session, set this listener before calling
         * {@link #requestCreateSession(MediaRoute2Info)}.
         * <p>
         * This will be called on the same thread which calls
         * {@link #requestCreateSession(MediaRoute2Info)}.
         *
         * @param route The route to create session with
         * @return An optional bundle of app-specific arguments to send to the provider,
         *         or null if none. The contents of this bundle may affect the result of
         *         session creation.
         * @see MediaRoute2ProviderService#onCreateSession(String, String, long, Bundle)
         */
        @Nullable
        Bundle onCreateSession(@NonNull MediaRoute2Info route);
    }

    /**
     * A class to control media routing session in media route provider.
     * For example, selecting/deselcting/transferring routes to session can be done through this
+6 −1
Original line number Diff line number Diff line
@@ -19,10 +19,12 @@ package com.android.mediarouteprovider.example;
import static android.media.MediaRoute2Info.DEVICE_TYPE_REMOTE_SPEAKER;
import static android.media.MediaRoute2Info.DEVICE_TYPE_REMOTE_TV;

import android.annotation.Nullable;
import android.content.Intent;
import android.media.MediaRoute2Info;
import android.media.MediaRoute2ProviderService;
import android.media.RoutingSessionInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;

@@ -167,7 +169,8 @@ public class SampleMediaRoute2ProviderService extends MediaRoute2ProviderService
    }

    @Override
    public void onCreateSession(String packageName, String routeId, long requestId) {
    public void onCreateSession(String packageName, String routeId, long requestId,
            @Nullable Bundle sessionHints) {
        MediaRoute2Info route = mRoutes.get(routeId);
        if (route == null || TextUtils.equals(ROUTE_ID3_SESSION_CREATION_FAILED, routeId)) {
            // Tell the router that session cannot be created by passing null as sessionInfo.
@@ -188,6 +191,8 @@ public class SampleMediaRoute2ProviderService extends MediaRoute2ProviderService
                .addSelectedRoute(routeId)
                .addSelectableRoute(ROUTE_ID4_TO_SELECT_AND_DESELECT)
                .addTransferrableRoute(ROUTE_ID5_TO_TRANSFER_TO)
                // Set control hints with given sessionHints
                .setControlHints(sessionHints)
                .build();
        notifySessionCreated(sessionInfo, requestId);
        publishRoutes();
Loading