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

Commit f0979eff authored by Bishoy Gendy's avatar Bishoy Gendy Committed by Android (Google) Code Review
Browse files

Merge "Fix volume adjustment for foreground media apps without media session" into main

parents acc4fac2 27687c21
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
package: "com.android.media.flags"

flag {
     namespace: "media_solutions"
    name: "enable_rlp_callbacks_in_media_router2"
    namespace: "media_solutions"
    description: "Make RouteListingPreference getter and callbacks public in MediaRouter2."
    bug: "281067101"
}

flag {
    name: "adjust_volume_for_foreground_app_playing_audio_without_media_session"
    namespace: "media_solutions"
    description: "Gates whether to adjust local stream volume when the app in the foreground is the last app to play audio or adjust the volume of the last active media session that the user interacted with."
    bug: "275185436"
}
+18 −1
Original line number Diff line number Diff line
@@ -153,6 +153,23 @@ class AudioPlayerStateMonitor {
        return sortedAudioPlaybackClientUids;
    }

    /**
     * Returns whether the given uid corresponds to the last process to audio or not.
     *
     * <p> This is useful for handling the cases where the foreground app is playing media without
     * a media session. Then, volume events should affect the local music stream rather than other
     * media sessions.
     *
     * @return {@code true} if the given uid corresponds to the last process to audio or
     * {@code false} otherwise.
     */
    public boolean hasUidPlayedAudioLast(int uid) {
        synchronized (mLock) {
            return !mSortedAudioPlaybackClientUids.isEmpty()
                    && uid == mSortedAudioPlaybackClientUids.get(0);
        }
    }

    /**
     * Returns if the audio playback is active for the uid.
     */
@@ -234,7 +251,7 @@ class AudioPlayerStateMonitor {
                    }
                }

                // Update mSortedAuioPlaybackClientUids.
                // Update mSortedAudioPlaybackClientUids.
                for (int i = 0; i < activeAudioPlaybackConfigs.size(); ++i) {
                    AudioPlaybackConfiguration config = activeAudioPlaybackConfigs.valueAt(i);
                    final int uid = config.getClientUid();
+16 −1
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.server.media;
import static android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
import static android.os.UserHandle.ALL;
import static android.os.UserHandle.CURRENT;

import static com.android.server.media.MediaKeyDispatcher.KEY_EVENT_LONG_PRESS;
import static com.android.server.media.MediaKeyDispatcher.isDoubleTapOverridden;
import static com.android.server.media.MediaKeyDispatcher.isLongPressOverridden;
@@ -85,6 +84,7 @@ import android.view.ViewConfiguration;

import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.media.flags.Flags;
import com.android.server.LocalManagerRegistry;
import com.android.server.LocalServices;
import com.android.server.SystemService;
@@ -2192,6 +2192,21 @@ public class MediaSessionService extends SystemService implements Monitor {
                    isValidLocalStreamType(suggestedStream)
                            && AudioSystem.isStreamActive(suggestedStream, 0);

            if (session != null && session.getUid() != uid
                    && mAudioPlayerStateMonitor.hasUidPlayedAudioLast(uid)) {
                if (Flags.adjustVolumeForForegroundAppPlayingAudioWithoutMediaSession()) {
                    // The app in the foreground has been the last app to play media locally.
                    // Therefore, We ignore the chosen session so that volume events affect the
                    // local music stream instead. See b/275185436 for details.
                    Log.d(TAG, "Ignoring session=" + session + " and adjusting suggestedStream="
                            + suggestedStream + " instead");
                    session = null;
                } else {
                    Log.d(TAG, "Session=" + session + " will not be not ignored and will receive"
                            + " the volume adjustment event");
                }
            }

            if (session == null || preferSuggestedStream) {
                if (DEBUG_KEY_EVENT) {
                    Log.d(TAG, "Adjusting suggestedStream=" + suggestedStream + " by " + direction