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

Commit 9a189d15 authored by Winson Chung's avatar Winson Chung
Browse files

Ensure that we register the media session listener for the current user.

- Use the per-user calls when fetching/responding to media session events
  otherwise it will fallback to using the process user.

Bug: 64315017
Test: Launch secondary user, enter PiP with media session and no custom
      actions.  Ensure the media session buttons show.

Change-Id: I52152223e91f0256ac4135616694e2a293947e3e
parent 8a4f26cc
Loading
Loading
Loading
Loading
+29 −5
Original line number Diff line number Diff line
@@ -26,14 +26,18 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.media.session.MediaSessionManager;
import android.media.session.MediaSessionManager.OnActiveSessionsChangedListener;
import android.media.session.PlaybackState;
import android.os.UserHandle;

import com.android.systemui.Dependency;
import com.android.systemui.R;
import com.android.systemui.statusbar.policy.UserInfoController;

import java.util.ArrayList;
import java.util.Collections;
@@ -88,13 +92,21 @@ public class PipMediaController {
        }
    };

    private MediaController.Callback mPlaybackChangedListener = new MediaController.Callback() {
    private final MediaController.Callback mPlaybackChangedListener = new MediaController.Callback() {
        @Override
        public void onPlaybackStateChanged(PlaybackState state) {
            notifyActionsChanged();
        }
    };

    private final MediaSessionManager.OnActiveSessionsChangedListener mSessionsChangedListener =
            new OnActiveSessionsChangedListener() {
        @Override
        public void onActiveSessionsChanged(List<MediaController> controllers) {
            resolveActiveMediaController(controllers);
        }
    };

    private ArrayList<ActionListener> mListeners = new ArrayList<>();

    public PipMediaController(Context context, IActivityManager activityManager) {
@@ -110,9 +122,11 @@ public class PipMediaController {
        createMediaActions();
        mMediaSessionManager =
                (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
        mMediaSessionManager.addOnActiveSessionsChangedListener(controllers -> {
            resolveActiveMediaController(controllers);
        }, null);

        // The media session listener needs to be re-registered when switching users
        UserInfoController userInfoController = Dependency.get(UserInfoController.class);
        userInfoController.addCallback((String name, Drawable picture, String userAccount) ->
                registerSessionListenerForCurrentUser());
    }

    /**
@@ -120,7 +134,8 @@ public class PipMediaController {
     */
    public void onActivityPinned() {
        // Once we enter PiP, try to find the active media controller for the top most activity
        resolveActiveMediaController(mMediaSessionManager.getActiveSessions(null));
        resolveActiveMediaController(mMediaSessionManager.getActiveSessionsForUser(null,
                UserHandle.USER_CURRENT));
    }

    /**
@@ -200,6 +215,15 @@ public class PipMediaController {
                                FLAG_UPDATE_CURRENT));
    }

    /**
     * Re-registers the session listener for the current user.
     */
    private void registerSessionListenerForCurrentUser() {
        mMediaSessionManager.removeOnActiveSessionsChangedListener(mSessionsChangedListener);
        mMediaSessionManager.addOnActiveSessionsChangedListener(mSessionsChangedListener, null,
                UserHandle.USER_CURRENT, null);
    }

    /**
     * Tries to find and set the active media controller for the top PiP activity.
     */