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

Commit ed543683 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "MediaSession2: Fix bug in MediaSession2.setCustomLayout()"

parents eddaba90 9b67177e
Loading
Loading
Loading
Loading
+0 −6
Original line number Original line Diff line number Diff line
@@ -91,10 +91,4 @@ public class MediaBrowser2Impl extends MediaController2Impl implements MediaBrow
            mCallback.onGetRootResult(rootHints, rootMediaId, rootExtra);
            mCallback.onGetRootResult(rootHints, rootMediaId, rootExtra);
        });
        });
    }
    }

    public void onCustomLayoutChanged(final List<CommandButton> layout) {
        getCallbackExecutor().execute(() -> {
            mCallback.onCustomLayoutChanged(layout);
        });
    }
}
}
+7 −0
Original line number Original line Diff line number Diff line
@@ -26,6 +26,7 @@ import android.media.MediaController2.PlaybackInfo;
import android.media.MediaItem2;
import android.media.MediaItem2;
import android.media.MediaSession2;
import android.media.MediaSession2;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaController2;
import android.media.MediaController2;
import android.media.MediaController2.ControllerCallback;
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.
    // This will be called on the main thread.
    private class SessionServiceConnection implements ServiceConnection {
    private class SessionServiceConnection implements ServiceConnection {
        @Override
        @Override
+25 −24
Original line number Original line Diff line number Diff line
@@ -53,7 +53,6 @@ public class MediaSession2CallbackStub extends IMediaSession2Callback.Stub {
        return controller;
        return controller;
    }
    }


    // TODO(jaewan): Refactor code to get rid of these pattern.
    private MediaBrowser2Impl getBrowser() throws IllegalStateException {
    private MediaBrowser2Impl getBrowser() throws IllegalStateException {
        final MediaController2Impl controller = getController();
        final MediaController2Impl controller = getController();
        if (controller instanceof MediaBrowser2Impl) {
        if (controller instanceof MediaBrowser2Impl) {
@@ -162,50 +161,32 @@ public class MediaSession2CallbackStub extends IMediaSession2Callback.Stub {
        controller.getInstance().close();
        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
    @Override
    public void onCustomLayoutChanged(List<Bundle> commandButtonlist) {
    public void onCustomLayoutChanged(List<Bundle> commandButtonlist) {
        if (commandButtonlist == null) {
        if (commandButtonlist == null) {
            // Illegal call. Ignore
            // Illegal call. Ignore
            return;
            return;
        }
        }
        // TODO(jaewan): Fix here. It's controller feature so shouldn't use browser
        final MediaController2Impl controller;
        final MediaBrowser2Impl browser;
        try {
        try {
            browser = getBrowser();
            controller = getController();
        } catch (IllegalStateException e) {
        } catch (IllegalStateException e) {
            Log.w(TAG, "Don't fail silently here. Highly likely a bug");
            Log.w(TAG, "Don't fail silently here. Highly likely a bug");
            return;
            return;
        }
        }
        if (browser == null) {
        if (controller == null) {
            // TODO(jaewan): Revisit here. Could be a bug
            // TODO(jaewan): Revisit here. Could be a bug
            return;
            return;
        }
        }
        List<CommandButton> layout = new ArrayList<>();
        List<CommandButton> layout = new ArrayList<>();
        for (int i = 0; i < commandButtonlist.size(); i++) {
        for (int i = 0; i < commandButtonlist.size(); i++) {
            CommandButton button = CommandButtonImpl.fromBundle(
            CommandButton button = CommandButtonImpl.fromBundle(
                    browser.getContext(), commandButtonlist.get(i));
                    controller.getContext(), commandButtonlist.get(i));
            if (button != null) {
            if (button != null) {
                layout.add(button);
                layout.add(button);
            }
            }
        }
        }
        browser.onCustomLayoutChanged(layout);
        controller.onCustomLayoutChanged(layout);
    }
    }


    @Override
    @Override
@@ -223,4 +204,24 @@ public class MediaSession2CallbackStub extends IMediaSession2Callback.Stub {
        }
        }
        controller.onCustomCommand(command, args, receiver);
        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 Original line Diff line number Diff line
@@ -24,6 +24,7 @@ import android.annotation.Nullable;
import android.content.Context;
import android.content.Context;
import android.media.MediaBrowser2.BrowserCallback;
import android.media.MediaBrowser2.BrowserCallback;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.PlaylistParams;
import android.media.MediaSession2.PlaylistParams;
import android.os.Bundle;
import android.os.Bundle;
@@ -36,6 +37,7 @@ import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;


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


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


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


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


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


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


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



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

        @Override
        @Override
        public void onGetRootResult(Bundle rootHints, String rootMediaId, Bundle rootExtra) {
        public void onGetRootResult(Bundle rootHints, String rootMediaId, Bundle rootExtra) {
            super.onGetRootResult(rootHints, rootMediaId, rootExtra);
            super.onGetRootResult(rootHints, rootMediaId, rootExtra);
+41 −0
Original line number Original line Diff line number Diff line
@@ -33,6 +33,8 @@ import android.media.MediaController2.PlaybackInfo;
import android.media.MediaPlayerInterface.PlaybackListener;
import android.media.MediaPlayerInterface.PlaybackListener;
import android.media.MediaSession2.Builder;
import android.media.MediaSession2.Builder;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.Command;
import android.media.MediaSession2.CommandButton;
import android.media.MediaSession2.CommandGroup;
import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.ControllerInfo;
import android.media.MediaSession2.PlaylistParams;
import android.media.MediaSession2.PlaylistParams;
import android.media.MediaSession2.SessionCallback;
import android.media.MediaSession2.SessionCallback;
@@ -352,6 +354,45 @@ public class MediaSession2Test extends MediaSession2TestBase {
        waitForDisconnect(controller, true);
        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
    @Test
    public void testSendCustomAction() throws InterruptedException {
    public void testSendCustomAction() throws InterruptedException {
        final Command testCommand =
        final Command testCommand =
Loading