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

Commit bd1ccc97 authored by Santiago Seifert's avatar Santiago Seifert
Browse files

Route volume key presses to mirroring sessions while mirroring

This change doesn't implement the system ui changes, which will come in
a follow up CL.

Bug: b/362507305
Bug: b/396394220
Test: atest CtsMediaBetterTogetherTestCases:android.media.router.cts.SystemMediaRoutingTest
Flag: com.android.media.flags.enable_mirroring_in_media_router_2
Change-Id: Icd9b1fa299093dded1ebcd52d31ada6d764be022
parent 1418b9e4
Loading
Loading
Loading
Loading
+63 −0
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.PermissionChecker;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.IMediaRouter2;
import android.media.IMediaRouter2Manager;
import android.media.MediaRoute2Info;
@@ -102,6 +103,7 @@ import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
@@ -129,6 +131,9 @@ class MediaRouter2ServiceImpl {
                Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN
            };

    private static final AtomicReference<MediaRouter2ServiceImpl> sInstance =
            new AtomicReference<>();

    private final Context mContext;
    private final Looper mLooper;
    private final UserManagerInternal mUserManagerInternal;
@@ -241,6 +246,41 @@ class MediaRouter2ServiceImpl {
        }
    }

    /**
     * Conditionally handles a volume key press, and returns true if handled.
     *
     * <p>A volume key press will only be handled if a routing session subject to volume key presses
     * exists.
     *
     * @param callerLogTag A tag to include in the log line indicating that the event is handled.
     * @param direction One of {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE},
     *     or {@link AudioManager#ADJUST_SAME}. If the direction is not one of these, the key press
     *     will not be handled.
     * @param suggestedStream The suggested stream to adjust. If the stream is not {@link
     *     AudioManager#USE_DEFAULT_STREAM_TYPE} or {@link AudioManager#STREAM_MUSIC}, the key press
     *     will not be handled.
     * @return Whether the key press was handled.
     * @see SystemMediaRoute2Provider2#maybeHandleVolumeKeyEventForSystemMediaSession
     */

    /* package */ static boolean maybeHandleVolumeKeyEvent(
            String callerLogTag, int direction, int suggestedStream) {
        var service = Flags.enableMirroringInMediaRouter2() ? sInstance.get() : null;
        var isRelevantStream =
                suggestedStream == AudioManager.USE_DEFAULT_STREAM_TYPE
                        || suggestedStream == AudioManager.STREAM_MUSIC;
        if (service == null || !isRelevantStream) {
            return false;
        }
        var handled = service.maybeHandleVolumeKeyEventInternal(direction);
        if (handled) {
            Log.i(
                    TAG,
                    "Volume key press handled by the routing framework. Caller: " + callerLogTag);
        }
        return handled;
    }

    // Start of methods that implement MediaRouter2 operations.

    @NonNull
@@ -1095,6 +1135,24 @@ class MediaRouter2ServiceImpl {
        }
    }

    private boolean maybeHandleVolumeKeyEventInternal(int direction) {
        synchronized (mLock) {
            // TODO: b/396399175 - Replace this for-loop with a singleton system provider when
            // multi-user support is properly implemented.
            for (int i = 0; i < mUserRecords.size(); i++) {
                var userRecord = mUserRecords.valueAt(i);
                var systemProvider = userRecord.mHandler.getSystemProvider();
                var handled =
                        systemProvider.maybeHandleVolumeKeyEventForSystemMediaSession(
                                DUMMY_REQUEST_ID, direction);
                if (handled) {
                    return true;
                }
            }
            return false;
        }
    }

    /* package */ void updateRunningUserAndProfiles(int newActiveUserId) {
        synchronized (mLock) {
            if (mCurrentActiveUserId != newActiveUserId) {
@@ -2264,6 +2322,11 @@ class MediaRouter2ServiceImpl {
        }
    }

    /** Invoked when {@link MediaRouterService#systemRunning()} is invoked. */
    /* package */ void systemRunning() {
        sInstance.set(this);
    }

    final class UserRecord {
        public final int mUserId;
        //TODO: make records private for thread-safety
+1 −0
Original line number Diff line number Diff line
@@ -199,6 +199,7 @@ public final class MediaRouterService extends IMediaRouterService.Stub
                        },
                        TAG);
        updateRunningUserAndProfiles(ActivityManager.getCurrentUser());
        mService2.systemRunning();
    }

    @Override
+5 −0
Original line number Diff line number Diff line
@@ -782,6 +782,11 @@ public class MediaSessionRecord extends MediaSessionRecordImpl implements IBinde
            String opPackageName,
            int uid,
            int pid) {

        if (MediaRouter2ServiceImpl.maybeHandleVolumeKeyEvent(TAG, direction, stream)) {
            return;
        }

        try {
            if (useSuggested) {
                if (AudioSystem.isStreamActive(stream, 0)) {
+5 −0
Original line number Diff line number Diff line
@@ -2574,6 +2574,11 @@ public class MediaSessionService extends SystemService implements Monitor {
                    return;
                }

                if (MediaRouter2ServiceImpl.maybeHandleVolumeKeyEvent(
                        TAG, direction, suggestedStream)) {
                    return;
                }

                // Execute mAudioService.adjustSuggestedStreamVolume() on
                // handler thread of MediaSessionService.
                // This will release the MediaSessionService.mLock sooner and avoid
+17 −0
Original line number Diff line number Diff line
@@ -318,6 +318,23 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
        // Do nothing since the system session persists.
    }

    /**
     * Adjusts the volume of {@link MediaRoute2ProviderService service-provided} system media
     * routing sessions.
     *
     * <p>This method does not affect the volume of the system, which is managed by {@link
     * AudioManager}.
     *
     * @param requestId The id of the request, or {@link MediaRoute2ProviderService#REQUEST_ID_NONE}
     *     if there's no associated id.
     * @param direction One of {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE},
     *     or {@link AudioManager#ADJUST_SAME}.
     * @return Whether the volume key pressed was handled.
     */
    public boolean maybeHandleVolumeKeyEventForSystemMediaSession(long requestId, int direction) {
        return false;
    }

    public MediaRoute2Info getDefaultRoute() {
        return mDefaultRoute;
    }
Loading