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

Commit 7eb3aae8 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "MediaRouter: add volume control functionality into MR2"

parents b44570bc 5a8dedcf
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -27,4 +27,6 @@ oneway interface IMediaRoute2Provider {
    void selectRoute(String packageName, String id);
    void unselectRoute(String packageName, String id);
    void notifyControlRequestSent(String id, in Intent request);
    void requestSetVolume(String id, int volume);
    void requestUpdateVolume(String id, int delta);
}
+7 −0
Original line number Diff line number Diff line
@@ -46,6 +46,8 @@ interface IMediaRouterService {
    void registerClient2(IMediaRouter2Client client, String packageName);
    void unregisterClient2(IMediaRouter2Client client);
    void sendControlRequest(IMediaRouter2Client client, in MediaRoute2Info route, in Intent request);
    void requestSetVolume2(IMediaRouter2Client client, in MediaRoute2Info route, int volume);
    void requestUpdateVolume2(IMediaRouter2Client client, in MediaRoute2Info route, int direction);
    /**
     * Changes the selected route of the client.
     *
@@ -66,4 +68,9 @@ interface IMediaRouterService {
     */
    void selectClientRoute2(IMediaRouter2Manager manager, String packageName,
            in @nullable MediaRoute2Info route);

    void requestSetVolume2Manager(IMediaRouter2Manager manager,
            in MediaRoute2Info route, int volume);
    void requestUpdateVolume2Manager(IMediaRouter2Manager manager,
            in MediaRoute2Info route, int direction);
}
+26 −0
Original line number Diff line number Diff line
@@ -80,6 +80,20 @@ public abstract class MediaRoute2ProviderService extends Service {
    //TODO: Discuss what to use for request (e.g., Intent? Request class?)
    public abstract void onControlRequest(String routeId, Intent request);

    /**
     * Called when requestSetVolume is called on a route of the provider
     * @param routeId the id of the route
     * @param volume the target volume
     */
    public abstract void onSetVolume(String routeId, int volume);

    /**
     * Called when requestUpdateVolume is called on a route of the provider
     * @param routeId id of the route
     * @param delta the delta to add to the current volume
     */
    public abstract void onUpdateVolume(String routeId, int delta);

    /**
     * Updates provider info and publishes routes
     */
@@ -130,5 +144,17 @@ public abstract class MediaRoute2ProviderService extends Service {
            mHandler.sendMessage(obtainMessage(MediaRoute2ProviderService::onControlRequest,
                    MediaRoute2ProviderService.this, id, request));
        }

        @Override
        public void requestSetVolume(String id, int volume) {
            mHandler.sendMessage(obtainMessage(MediaRoute2ProviderService::onSetVolume,
                    MediaRoute2ProviderService.this, id, volume));
        }

        @Override
        public void requestUpdateVolume(String id, int delta) {
            mHandler.sendMessage(obtainMessage(MediaRoute2ProviderService::onUpdateVolume,
                    MediaRoute2ProviderService.this, id, delta));
        }
    }
}
+67 −0
Original line number Diff line number Diff line
@@ -265,6 +265,54 @@ public class MediaRouter2 {
        }
    }

    /**
     * Requests a volume change for the route asynchronously.
     * <p>
     * It may have no effect if the route is currently not selected.
     * </p>
     *
     * @param volume The new volume value between 0 and {@link MediaRoute2Info#getVolumeMax}.
     */
    public void requestSetVolume(@NonNull MediaRoute2Info route, int volume) {
        Objects.requireNonNull(route, "route must not be null");

        Client client;
        synchronized (sLock) {
            client = mClient;
        }
        if (client != null) {
            try {
                mMediaRouterService.requestSetVolume2(client, route, volume);
            } catch (RemoteException ex) {
                Log.e(TAG, "Unable to send control request.", ex);
            }
        }
    }

    /**
     * Requests an incremental volume update  for the route asynchronously.
     * <p>
     * It may have no effect if the route is currently not selected.
     * </p>
     *
     * @param delta The delta to add to the current volume.
     */
    public void requestUpdateVolume(@NonNull MediaRoute2Info route, int delta) {
        Objects.requireNonNull(route, "route must not be null");

        Client client;
        synchronized (sLock) {
            client = mClient;
        }
        if (client != null) {
            try {
                mMediaRouterService.requestUpdateVolume2(client, route, delta);
            } catch (RemoteException ex) {
                Log.e(TAG, "Unable to send control request.", ex);
            }
        }
    }

    @GuardedBy("mCallbackRecords")
    private int findCallbackRecordIndexLocked(Callback callback) {
        final int count = mCallbackRecords.size();
@@ -321,10 +369,21 @@ public class MediaRouter2 {
            if (!route.supportsControlCategory(controlCategories)) {
                continue;
            }
            MediaRoute2Info preRoute = findRouteById(route.getId());
            if (!route.equals(preRoute)) {
                notifyRouteChanged(route);
            }
            outRoutes.add(route);
        }
    }

    MediaRoute2Info findRouteById(String id) {
        for (MediaRoute2Info route : mRoutes) {
            if (route.getId().equals(id)) return route;
        }
        return null;
    }

    void notifyRouteListChanged(List<MediaRoute2Info> routes) {
        for (CallbackRecord record: mCallbackRecords) {
            record.mExecutor.execute(
@@ -332,10 +391,18 @@ public class MediaRouter2 {
        }
    }

    void notifyRouteChanged(MediaRoute2Info route) {
        for (CallbackRecord record: mCallbackRecords) {
            record.mExecutor.execute(
                    () -> record.mCallback.onRouteChanged(route));
        }
    }

    /**
     * Interface for receiving events about media routing changes.
     */
    public static class Callback {
        //TODO: clean up these callbacks
        /**
         * Called when a route is added.
         */
+48 −0
Original line number Diff line number Diff line
@@ -234,6 +234,54 @@ public class MediaRouter2Manager {
        }
    }

    /**
     * Requests a volume change for the route asynchronously.
     * <p>
     * It may have no effect if the route is currently not selected.
     * </p>
     *
     * @param volume The new volume value between 0 and {@link MediaRoute2Info#getVolumeMax}.
     */
    public void requestSetVolume(@NonNull MediaRoute2Info route, int volume) {
        Objects.requireNonNull(route, "route must not be null");

        Client client;
        synchronized (sLock) {
            client = mClient;
        }
        if (client != null) {
            try {
                mMediaRouterService.requestSetVolume2Manager(client, route, volume);
            } catch (RemoteException ex) {
                Log.e(TAG, "Unable to send control request.", ex);
            }
        }
    }

    /**
     * Requests an incremental volume update  for the route asynchronously.
     * <p>
     * It may have no effect if the route is currently not selected.
     * </p>
     *
     * @param delta The delta to add to the current volume.
     */
    public void requestUpdateVolume(@NonNull MediaRoute2Info route, int delta) {
        Objects.requireNonNull(route, "route must not be null");

        Client client;
        synchronized (sLock) {
            client = mClient;
        }
        if (client != null) {
            try {
                mMediaRouterService.requestUpdateVolume2Manager(client, route, delta);
            } catch (RemoteException ex) {
                Log.e(TAG, "Unable to send control request.", ex);
            }
        }
    }

    int findProviderIndex(MediaRoute2ProviderInfo provider) {
        final int count = mProviders.size();
        for (int i = 0; i < count; i++) {
Loading