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

Commit 2dc180ad authored by Jin Seok Park's avatar Jin Seok Park
Browse files

Rename and improve remote volume controller related methods

1. Renamed "RemoteVolumeController" to "RemoteSession" since it
better encapsulates the callback methods.
2. Added Javadoc for new parameters in
registerRemoteSessionCallback.
3. Renamed onSessionChanged to onDefaultRemoteSessionChanged and
revised documentation,
4. Added documentation for what the flags in
RemoteSessionCallback#onVolumeChanged are for.

Bug: 173657280
Test: N/A
Change-Id: I8f0d58f732df8117888bc66a58f47fc33db2339e
parent 9e832828
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -116,14 +116,14 @@ package android.media.session {
    method public void dispatchVolumeKeyEventAsSystemService(@NonNull android.view.KeyEvent, int);
    method public void dispatchVolumeKeyEventToSessionAsSystemService(@NonNull android.view.KeyEvent, @NonNull android.media.session.MediaSession.Token);
    method @NonNull public java.util.List<android.media.session.MediaController> getActiveSessionsForUser(@Nullable android.content.ComponentName, int);
    method public void registerRemoteVolumeControllerCallback(@NonNull java.util.concurrent.Executor, @NonNull android.media.session.MediaSessionManager.RemoteVolumeControllerCallback);
    method public void unregisterRemoteVolumeControllerCallback(@NonNull android.media.session.MediaSessionManager.RemoteVolumeControllerCallback);
    method public void registerRemoteSessionCallback(@NonNull java.util.concurrent.Executor, @NonNull android.media.session.MediaSessionManager.RemoteSessionCallback);
    method public void unregisterRemoteSessionCallback(@NonNull android.media.session.MediaSessionManager.RemoteSessionCallback);
    field public static final int RESULT_MEDIA_KEY_HANDLED = 1; // 0x1
    field public static final int RESULT_MEDIA_KEY_NOT_HANDLED = 0; // 0x0
  }

  public static interface MediaSessionManager.RemoteVolumeControllerCallback {
    method public void onSessionChanged(@Nullable android.media.session.MediaSession.Token);
  public static interface MediaSessionManager.RemoteSessionCallback {
    method public void onDefaultRemoteSessionChanged(@Nullable android.media.session.MediaSession.Token);
    method public void onVolumeChanged(@NonNull android.media.session.MediaSession.Token, int);
  }

+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import android.media.session.MediaSession;
 * TODO add in better support for multiple remote sessions.
 * @hide
 */
oneway interface IRemoteVolumeControllerCallback {
oneway interface IRemoteSessionCallback {
    void onVolumeChanged(in MediaSession.Token sessionToken, int flags);
    // sets the default session to use with the slider, replaces remoteSliderVisibility
    // on IVolumeController
+3 −3
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ package android.media.session;

import android.content.ComponentName;
import android.content.pm.ParceledListSlice;
import android.media.IRemoteVolumeControllerCallback;
import android.media.IRemoteSessionCallback;
import android.media.Session2Token;
import android.media.session.IActiveSessionsListener;
import android.media.session.IOnMediaKeyEventDispatchedListener;
@@ -57,8 +57,8 @@ interface ISessionManager {
    void addSession2TokensListener(in ISession2TokensListener listener, int userId);
    void removeSession2TokensListener(in ISession2TokensListener listener);

    void registerRemoteVolumeControllerCallback(in IRemoteVolumeControllerCallback rvc);
    void unregisterRemoteVolumeControllerCallback(in IRemoteVolumeControllerCallback rvc);
    void registerRemoteSessionCallback(in IRemoteSessionCallback rvc);
    void unregisterRemoteSessionCallback(in IRemoteSessionCallback rvc);

    // For PhoneWindowManager to precheck media keys
    boolean isGlobalPriorityActive();
+46 −35
Original line number Diff line number Diff line
@@ -27,10 +27,11 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ParceledListSlice;
import android.media.AudioManager;
import android.media.IRemoteVolumeControllerCallback;
import android.media.IRemoteSessionCallback;
import android.media.MediaFrameworkPlatformInitializer;
import android.media.MediaSession2;
import android.media.Session2Token;
import android.media.VolumeProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
@@ -87,8 +88,8 @@ public final class MediaSessionManager {
    private final OnMediaKeyEventSessionChangedListenerStub
            mOnMediaKeyEventSessionChangedListenerStub =
            new OnMediaKeyEventSessionChangedListenerStub();
    private final RemoteVolumeControllerCallbackStub mRemoteVolumeControllerCallbackStub =
            new RemoteVolumeControllerCallbackStub();
    private final RemoteSessionCallbackStub mRemoteSessionCallbackStub =
            new RemoteSessionCallbackStub();

    private final Object mLock = new Object();
    @GuardedBy("mLock")
@@ -108,8 +109,8 @@ public final class MediaSessionManager {
    @GuardedBy("mLock")
    private MediaSession.Token mCurMediaKeyEventSession;
    @GuardedBy("mLock")
    private final Map<RemoteVolumeControllerCallback, Executor>
            mRemoteVolumeControllerCallbacks = new ArrayMap<>();
    private final Map<RemoteSessionCallback, Executor>
            mRemoteSessionCallbacks = new ArrayMap<>();

    private Context mContext;
    private OnVolumeKeyLongPressListenerImpl mOnVolumeKeyLongPressListener;
@@ -482,27 +483,29 @@ public final class MediaSessionManager {
     * Set the remote volume controller callback to receive volume updates on.
     * Only for use by System UI and Settings application.
     *
     * @param executor The executor on which the callback should be invoked
     * @param callback The volume controller callback to receive updates on.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public void registerRemoteVolumeControllerCallback(
    public void registerRemoteSessionCallback(
            @NonNull @CallbackExecutor Executor executor,
            @NonNull RemoteVolumeControllerCallback callback) {
            @NonNull RemoteSessionCallback callback) {
        Objects.requireNonNull(executor, "executor shouldn't be null");
        Objects.requireNonNull(callback, "callback shouldn't be null");
        boolean shouldRegisterCallback = false;
        synchronized (mLock) {
            int prevCallbackCount = mRemoteVolumeControllerCallbacks.size();
            mRemoteVolumeControllerCallbacks.put(callback, executor);
            if (prevCallbackCount == 0 && mRemoteVolumeControllerCallbacks.size() == 1) {
            int prevCallbackCount = mRemoteSessionCallbacks.size();
            mRemoteSessionCallbacks.put(callback, executor);
            if (prevCallbackCount == 0 && mRemoteSessionCallbacks.size() == 1) {
                shouldRegisterCallback = true;
            }
        }
        if (shouldRegisterCallback) {
            try {
                mService.registerRemoteVolumeControllerCallback(
                        mRemoteVolumeControllerCallbackStub);
                mService.registerRemoteSessionCallback(
                        mRemoteSessionCallbackStub);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to register remote volume controller callback", e);
            }
@@ -511,27 +514,27 @@ public final class MediaSessionManager {

    /**
     * Unregisters the remote volume controller callback which was previously registered with
     * {@link #registerRemoteVolumeControllerCallback(Executor, RemoteVolumeControllerCallback)}.
     * {@link #registerRemoteSessionCallback(Executor, RemoteSessionCallback)}.
     * Only for use by System UI and Settings application.
     *
     * @param callback The volume controller callback to receive updates on.
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public void unregisterRemoteVolumeControllerCallback(
            @NonNull RemoteVolumeControllerCallback callback) {
    public void unregisterRemoteSessionCallback(
            @NonNull RemoteSessionCallback callback) {
        Objects.requireNonNull(callback, "callback shouldn't be null");
        boolean shouldUnregisterCallback = false;
        synchronized (mLock) {
            if (mRemoteVolumeControllerCallbacks.remove(callback) != null
                    && mRemoteVolumeControllerCallbacks.size() == 0) {
            if (mRemoteSessionCallbacks.remove(callback) != null
                    && mRemoteSessionCallbacks.size() == 0) {
                shouldUnregisterCallback = true;
            }
        }
        try {
            if (shouldUnregisterCallback) {
                mService.unregisterRemoteVolumeControllerCallback(
                        mRemoteVolumeControllerCallbackStub);
                mService.unregisterRemoteSessionCallback(
                        mRemoteSessionCallbackStub);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Failed to unregister remote volume controller callback", e);
@@ -1107,26 +1110,34 @@ public final class MediaSessionManager {
    }

    /**
     * Callback to receive changes in the remote volume controller.
     * Callback to receive changes in the existing remote sessions. A remote session is a
     * {@link MediaSession} that is connected to a remote player via
     * {@link MediaSession#setPlaybackToRemote(VolumeProvider)}
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public interface RemoteVolumeControllerCallback {
    public interface RemoteSessionCallback {
        /**
         * Called when the volume is changed.
         * Called when the volume is changed for the given session. Flags that are defined in
         * {@link AudioManager} will also be sent and will contain information about how to
         * handle the volume change. For example, {@link AudioManager#FLAG_SHOW_UI} indicates that a
         * toast showing the volume should be shown.
         *
         * @param sessionToken the remote media session token
         * @param flags any of the flags from {@link AudioManager}
         * @param flags extra information about how to handle the volume change
         */
        void onVolumeChanged(@NonNull MediaSession.Token sessionToken, int flags);

        /**
         * Called when the session for the default remote controller is changed.
         * Called when the default remote session is changed where the default remote session
         * denotes an active remote session that has the highest priority for receiving key events.
         * Null will be sent if there are currently no active remote sessions.
         *
         * @param sessionToken the remote media session token
         * @param sessionToken the token of the default remote session, a session with the highest
         *                     priority for receiving key events.
         */
        void onSessionChanged(@Nullable MediaSession.Token sessionToken);
        void onDefaultRemoteSessionChanged(@Nullable MediaSession.Token sessionToken);
    }

    /**
@@ -1362,27 +1373,27 @@ public final class MediaSessionManager {
        }
    }

    private final class RemoteVolumeControllerCallbackStub
            extends IRemoteVolumeControllerCallback.Stub {
    private final class RemoteSessionCallbackStub
            extends IRemoteSessionCallback.Stub {
        @Override
        public void onVolumeChanged(MediaSession.Token sessionToken, int flags) {
            Map<RemoteVolumeControllerCallback, Executor> callbacks = new ArrayMap<>();
            Map<RemoteSessionCallback, Executor> callbacks = new ArrayMap<>();
            synchronized (mLock) {
                callbacks.putAll(mRemoteVolumeControllerCallbacks);
                callbacks.putAll(mRemoteSessionCallbacks);
            }
            for (Map.Entry<RemoteVolumeControllerCallback, Executor> e : callbacks.entrySet()) {
            for (Map.Entry<RemoteSessionCallback, Executor> e : callbacks.entrySet()) {
                e.getValue().execute(() -> e.getKey().onVolumeChanged(sessionToken, flags));
            }
        }

        @Override
        public void onSessionChanged(MediaSession.Token sessionToken) {
            Map<RemoteVolumeControllerCallback, Executor> callbacks = new ArrayMap<>();
            Map<RemoteSessionCallback, Executor> callbacks = new ArrayMap<>();
            synchronized (mLock) {
                callbacks.putAll(mRemoteVolumeControllerCallbacks);
                callbacks.putAll(mRemoteSessionCallbacks);
            }
            for (Map.Entry<RemoteVolumeControllerCallback, Executor> e : callbacks.entrySet()) {
                e.getValue().execute(() -> e.getKey().onSessionChanged(sessionToken));
            for (Map.Entry<RemoteSessionCallback, Executor> e : callbacks.entrySet()) {
                e.getValue().execute(() -> e.getKey().onDefaultRemoteSessionChanged(sessionToken));
            }
        }
    }
+15 −13
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ import android.media.session.MediaSession.QueueItem;
import android.media.session.MediaSession.Token;
import android.media.session.MediaSessionManager;
import android.media.session.MediaSessionManager.OnActiveSessionsChangedListener;
import android.media.session.MediaSessionManager.RemoteVolumeControllerCallback;
import android.media.session.MediaSessionManager.RemoteSessionCallback;
import android.media.session.PlaybackState;
import android.os.Bundle;
import android.os.Handler;
@@ -100,8 +100,8 @@ public class MediaSessions {
        mMgr.addOnActiveSessionsChangedListener(mSessionsListener, null, mHandler);
        mInit = true;
        postUpdateSessions();
        mMgr.registerRemoteVolumeControllerCallback(mHandlerExecutor,
                mRemoteVolumeControllerCallback);
        mMgr.registerRemoteSessionCallback(mHandlerExecutor,
                mRemoteSessionCallback);
    }

    protected void postUpdateSessions() {
@@ -116,7 +116,7 @@ public class MediaSessions {
        if (D.BUG) Log.d(TAG, "destroy");
        mInit = false;
        mMgr.removeOnActiveSessionsChangedListener(mSessionsListener);
        mMgr.unregisterRemoteVolumeControllerCallback(mRemoteVolumeControllerCallback);
        mMgr.unregisterRemoteSessionCallback(mRemoteSessionCallback);
    }

    /**
@@ -142,11 +142,11 @@ public class MediaSessions {
        mCallbacks.onRemoteVolumeChanged(token, flags);
    }

    private void onUpdateRemoteControllerH(Token sessionToken) {
    private void onUpdateRemoteSessionListH(Token sessionToken) {
        final MediaController controller =
                sessionToken != null ? new MediaController(mContext, sessionToken) : null;
        final String pkg = controller != null ? controller.getPackageName() : null;
        if (D.BUG) Log.d(TAG, "updateRemoteControllerH " + pkg);
        if (D.BUG) Log.d(TAG, "onUpdateRemoteSessionListH " + pkg);
        // this may be our only indication that a remote session is changed, refresh
        postUpdateSessions();
    }
@@ -336,8 +336,8 @@ public class MediaSessions {
                }
            };

    private final RemoteVolumeControllerCallback mRemoteVolumeControllerCallback =
            new RemoteVolumeControllerCallback() {
    private final RemoteSessionCallback mRemoteSessionCallback =
            new RemoteSessionCallback() {
                @Override
                public void onVolumeChanged(@NonNull MediaSession.Token sessionToken,
                        int flags) {
@@ -346,15 +346,17 @@ public class MediaSessions {
                }

                @Override
                public void onSessionChanged(@Nullable MediaSession.Token sessionToken) {
                    mHandler.obtainMessage(H.UPDATE_REMOTE_CONTROLLER, sessionToken).sendToTarget();
                public void onDefaultRemoteSessionChanged(
                        @Nullable MediaSession.Token sessionToken) {
                    mHandler.obtainMessage(H.UPDATE_REMOTE_SESSION_LIST,
                            sessionToken).sendToTarget();
                }
    };

    private final class H extends Handler {
        private static final int UPDATE_SESSIONS = 1;
        private static final int REMOTE_VOLUME_CHANGED = 2;
        private static final int UPDATE_REMOTE_CONTROLLER = 3;
        private static final int UPDATE_REMOTE_SESSION_LIST = 3;

        private H(Looper looper) {
            super(looper);
@@ -369,8 +371,8 @@ public class MediaSessions {
                case REMOTE_VOLUME_CHANGED:
                    onRemoteVolumeChangedH((Token) msg.obj, msg.arg1);
                    break;
                case UPDATE_REMOTE_CONTROLLER:
                    onUpdateRemoteControllerH((Token) msg.obj);
                case UPDATE_REMOTE_SESSION_LIST:
                    onUpdateRemoteSessionListH((Token) msg.obj);
                    break;
            }
        }
Loading