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

Commit 24762bff authored by RoboErik's avatar RoboErik
Browse files

Add an onSessionDestroyed callback to MediaController.Callback

Callbacks on the controller need to know when the session ceases
to exist.

bug:17009030
Change-Id: I0f1c1cfc51f7dd2203955287cc269667d3765ee4
parent b3cca876
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -16718,6 +16718,7 @@ package android.media.session {
    method public void onPlaybackStateChanged(android.media.session.PlaybackState);
    method public void onQueueChanged(java.util.List<android.media.session.MediaSession.Track>);
    method public void onQueueTitleChanged(java.lang.CharSequence);
    method public void onSessionDestroyed();
    method public void onSessionEvent(java.lang.String, android.os.Bundle);
    method public void onVolumeInfoChanged(android.media.session.MediaController.VolumeInfo);
  }
+7 −0
Original line number Diff line number Diff line
@@ -178,6 +178,13 @@ public class Media extends BaseCommand {
        public ControllerMonitor(ISessionController controller) {
            mController = controller;
        }

        @Override
        public void onSessionDestroyed() {
            System.out.println("onSessionDestroyed. Enter q to quit.");

        }

        @Override
        public void onEvent(String event, Bundle extras) {
            System.out.println("onSessionEvent event=" + event + ", extras=" + extras);
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.os.Bundle;
 */
oneway interface ISessionControllerCallback {
    void onEvent(String event, in Bundle extras);
    void onSessionDestroyed();

    // These callbacks are for the TransportController
    void onPlaybackStateChanged(in PlaybackState state);
+19 −0
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ public final class MediaController {
    private static final int MSG_UPDATE_QUEUE = 5;
    private static final int MSG_UPDATE_QUEUE_TITLE = 6;
    private static final int MSG_UPDATE_EXTRAS = 7;
    private static final int MSG_DESTROYED = 8;

    private final ISessionController mSessionBinder;

@@ -507,6 +508,13 @@ public final class MediaController {
     * registered using {@link #addCallback}
     */
    public static abstract class Callback {
        /**
         * Override to handle the session being destroyed. The session is no
         * longer valid after this call and calls to it will be ignored.
         */
        public void onSessionDestroyed() {
        }

        /**
         * Override to handle custom events sent by the session owner without a
         * specified interface. Controllers should only handle these for
@@ -862,6 +870,14 @@ public final class MediaController {
            mController = new WeakReference<MediaController>(controller);
        }

        @Override
        public void onSessionDestroyed() {
            MediaController controller = mController.get();
            if (controller != null) {
                controller.postMessage(MSG_DESTROYED, null, null);
            }
        }

        @Override
        public void onEvent(String event, Bundle extras) {
            MediaController controller = mController.get();
@@ -955,6 +971,9 @@ public final class MediaController {
                case MSG_UPDATE_VOLUME:
                    mCallback.onVolumeInfoChanged((VolumeInfo) msg.obj);
                    break;
                case MSG_DESTROYED:
                    mCallback.onSessionDestroyed();
                    break;
            }
        }

+37 −0
Original line number Diff line number Diff line
@@ -397,6 +397,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
                return;
            }
            mDestroyed = true;
            mHandler.post(MessageHandler.MSG_DESTROYED);
        }
    }

@@ -575,6 +576,29 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
        }
    }

    private void pushSessionDestroyed() {
        synchronized (mLock) {
            // This is the only method that may be (and can only be) called
            // after the session is destroyed.
            if (!mDestroyed) {
                return;
            }
            for (int i = mControllerCallbacks.size() - 1; i >= 0; i--) {
                ISessionControllerCallback cb = mControllerCallbacks.get(i);
                try {
                    cb.onSessionDestroyed();
                } catch (DeadObjectException e) {
                    Log.w(TAG, "Removing dead callback in pushEvent.", e);
                    mControllerCallbacks.remove(i);
                } catch (RemoteException e) {
                    Log.w(TAG, "unexpected exception in pushEvent.", e);
                }
            }
            // After notifying clear all listeners
            mControllerCallbacks.clear();
        }
    }

    private PlaybackState getStateWithUpdatedPosition() {
        PlaybackState state = mPlaybackState;
        long duration = -1;
@@ -919,6 +943,16 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
        @Override
        public void registerCallbackListener(ISessionControllerCallback cb) {
            synchronized (mLock) {
                // If this session is already destroyed tell the caller and
                // don't add them.
                if (mDestroyed) {
                    try {
                        cb.onSessionDestroyed();
                    } catch (Exception e) {
                        // ignored
                    }
                    return;
                }
                if (getControllerCbIndexForCb(cb) < 0) {
                    mControllerCallbacks.add(cb);
                    if (DEBUG) {
@@ -1123,6 +1157,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
        private static final int MSG_SEND_EVENT = 6;
        private static final int MSG_UPDATE_SESSION_STATE = 7;
        private static final int MSG_UPDATE_VOLUME = 8;
        private static final int MSG_DESTROYED = 9;

        public MessageHandler(Looper looper) {
            super(looper);
@@ -1154,6 +1189,8 @@ public class MediaSessionRecord implements IBinder.DeathRecipient {
                case MSG_UPDATE_VOLUME:
                    pushVolumeUpdate();
                    break;
                case MSG_DESTROYED:
                    pushSessionDestroyed();
            }
        }