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

Commit 9b67177e authored by Jaewan Kim's avatar Jaewan Kim
Browse files

MediaSession2: Fix bug in MediaSession2.setCustomLayout()

There had been a bug that only the MediaBrowser2 can receive the change
in the custom layout.

This CL fixes the issue by moving code related with getting custom
layout change from MediaBrowser2Impl to the MediaController2Impl.
Related tests are also added.

Test: Run all MediaComponent tests once
Change-Id: If5ce1be19057af306f46f1b1ab64f18e3f3fb7bd
parent b41bfb59
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -91,10 +91,4 @@ public class MediaBrowser2Impl extends MediaController2Impl implements MediaBrow
            mCallback.onGetRootResult(rootHints, rootMediaId, rootExtra);
        });
    }

    public void onCustomLayoutChanged(final List<CommandButton> layout) {
        getCallbackExecutor().execute(() -> {
            mCallback.onCustomLayoutChanged(layout);
        });
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.media.MediaController2.PlaybackInfo;
import android.media.MediaItem2;
import android.media.MediaSession2;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaController2;
import android.media.MediaController2.ControllerCallback;
@@ -632,6 +633,12 @@ public class MediaController2Impl implements MediaController2Provider {
        });
    }

    void onCustomLayoutChanged(final List<CommandButton> layout) {
        mCallbackExecutor.execute(() -> {
            mCallback.onCustomLayoutChanged(layout);
        });
    }

    // This will be called on the main thread.
    private class SessionServiceConnection implements ServiceConnection {
        @Override
+25 −24
Original line number Diff line number Diff line
@@ -53,7 +53,6 @@ public class MediaSession2CallbackStub extends IMediaSession2Callback.Stub {
        return controller;
    }

    // TODO(jaewan): Refactor code to get rid of these pattern.
    private MediaBrowser2Impl getBrowser() throws IllegalStateException {
        final MediaController2Impl controller = getController();
        if (controller instanceof MediaBrowser2Impl) {
@@ -162,50 +161,32 @@ public class MediaSession2CallbackStub extends IMediaSession2Callback.Stub {
        controller.getInstance().close();
    }

    @Override
    public void onGetRootResult(Bundle rootHints, String rootMediaId, Bundle rootExtra)
            throws RuntimeException {
        final MediaBrowser2Impl browser;
        try {
            browser = getBrowser();
        } catch (IllegalStateException e) {
            Log.w(TAG, "Don't fail silently here. Highly likely a bug");
            return;
        }
        if (browser == null) {
            // TODO(jaewan): Revisit here. Could be a bug
            return;
        }
        browser.onGetRootResult(rootHints, rootMediaId, rootExtra);
    }

    @Override
    public void onCustomLayoutChanged(List<Bundle> commandButtonlist) {
        if (commandButtonlist == null) {
            // Illegal call. Ignore
            return;
        }
        // TODO(jaewan): Fix here. It's controller feature so shouldn't use browser
        final MediaBrowser2Impl browser;
        final MediaController2Impl controller;
        try {
            browser = getBrowser();
            controller = getController();
        } catch (IllegalStateException e) {
            Log.w(TAG, "Don't fail silently here. Highly likely a bug");
            return;
        }
        if (browser == null) {
        if (controller == null) {
            // TODO(jaewan): Revisit here. Could be a bug
            return;
        }
        List<CommandButton> layout = new ArrayList<>();
        for (int i = 0; i < commandButtonlist.size(); i++) {
            CommandButton button = CommandButtonImpl.fromBundle(
                    browser.getContext(), commandButtonlist.get(i));
                    controller.getContext(), commandButtonlist.get(i));
            if (button != null) {
                layout.add(button);
            }
        }
        browser.onCustomLayoutChanged(layout);
        controller.onCustomLayoutChanged(layout);
    }

    @Override
@@ -223,4 +204,24 @@ public class MediaSession2CallbackStub extends IMediaSession2Callback.Stub {
        }
        controller.onCustomCommand(command, args, receiver);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////
    // MediaBrowser specific
    ////////////////////////////////////////////////////////////////////////////////////////////
    @Override
    public void onGetRootResult(Bundle rootHints, String rootMediaId, Bundle rootExtra)
            throws RuntimeException {
        final MediaBrowser2Impl browser;
        try {
            browser = getBrowser();
        } catch (IllegalStateException e) {
            Log.w(TAG, "Don't fail silently here. Highly likely a bug");
            return;
        }
        if (browser == null) {
            // TODO(jaewan): Revisit here. Could be a bug
            return;
        }
        browser.onGetRootResult(rootHints, rootMediaId, rootExtra);
    }
}
+9 −8
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.annotation.Nullable;
import android.content.Context;
import android.media.MediaBrowser2.BrowserCallback;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.PlaylistParams;
import android.os.Bundle;
@@ -36,6 +37,7 @@ import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@@ -104,42 +106,41 @@ public class MediaBrowser2Test extends MediaController2Test {
        @CallSuper
        @Override
        public void onConnected(CommandGroup commands) {
            super.onConnected(commands);
            connectLatch.countDown();
        }

        @CallSuper
        @Override
        public void onDisconnected() {
            super.onDisconnected();
            disconnectLatch.countDown();
        }

        @Override
        public void onPlaybackStateChanged(PlaybackState2 state) {
            super.onPlaybackStateChanged(state);
            mCallbackProxy.onPlaybackStateChanged(state);
        }

        @Override
        public void onPlaylistParamsChanged(PlaylistParams params) {
            super.onPlaylistParamsChanged(params);
            mCallbackProxy.onPlaylistParamsChanged(params);
        }

        @Override
        public void onPlaybackInfoChanged(MediaController2.PlaybackInfo info) {
            if (mCallbackProxy != null) {
            mCallbackProxy.onPlaybackInfoChanged(info);
        }
        }

        @Override
        public void onCustomCommand(Command command, Bundle args, ResultReceiver receiver) {
            super.onCustomCommand(command, args, receiver);
            mCallbackProxy.onCustomCommand(command, args, receiver);
        }


        @Override
        public void onCustomLayoutChanged(List<CommandButton> layout) {
            mCallbackProxy.onCustomLayoutChanged(layout);
        }

        @Override
        public void onGetRootResult(Bundle rootHints, String rootMediaId, Bundle rootExtra) {
            super.onGetRootResult(rootHints, rootMediaId, rootExtra);
+41 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ import android.media.MediaController2.PlaybackInfo;
import android.media.MediaPlayerInterface.PlaybackListener;
import android.media.MediaSession2.Builder;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.PlaylistParams;
import android.media.MediaSession2.SessionCallback;
@@ -352,6 +354,45 @@ public class MediaSession2Test extends MediaSession2TestBase {
        waitForDisconnect(controller, true);
    }

    @Test
    public void testSetCustomLayout() throws InterruptedException {
        final List<CommandButton> buttons = new ArrayList<>();
        buttons.add(new CommandButton.Builder(mContext)
                .setCommand(new Command(mContext, MediaSession2.COMMAND_CODE_PLAYBACK_PLAY))
                .setDisplayName("button").build());
        final CountDownLatch latch = new CountDownLatch(1);
        final SessionCallback sessionCallback = new SessionCallback(mContext) {
            @Override
            public CommandGroup onConnect(ControllerInfo controller) {
                if (mContext.getPackageName().equals(controller.getPackageName())) {
                    mSession.setCustomLayout(controller, buttons);
                }
                return super.onConnect(controller);
            }
        };

        try (final MediaSession2 session = new MediaSession2.Builder(mContext, mPlayer)
                .setId("testSetCustomLayout")
                .setSessionCallback(sHandlerExecutor, sessionCallback)
                .build()) {
            final TestControllerCallbackInterface callback = new TestControllerCallbackInterface() {
                @Override
                public void onCustomLayoutChanged(List<CommandButton> layout) {
                    assertEquals(layout.size(), buttons.size());
                    for (int i = 0; i < layout.size(); i++) {
                        assertEquals(layout.get(i).getCommand(), buttons.get(i).getCommand());
                        assertEquals(layout.get(i).getDisplayName(),
                                buttons.get(i).getDisplayName());
                    }
                    latch.countDown();
                }
            };
            final MediaController2 controller =
                    createController(session.getToken(), true, callback);
            assertTrue(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS));
        }
    }

    @Test
    public void testSendCustomAction() throws InterruptedException {
        final Command testCommand =
Loading