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

Commit dec0c2e1 authored by Jaewan Kim's avatar Jaewan Kim Committed by Android (Google) Code Review
Browse files

Merge "MediaSession2: Stop overriding MediaPlayerBase for session/controller"

parents 2f39adce 113a561d
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -106,7 +106,7 @@ public class MediaController2Impl implements MediaController2Provider {
        mCallback = callback;
        mCallbackExecutor = executor;
        mDeathRecipient = () -> {
            mInstance.release();
            mInstance.close();
        };

        mSessionBinder = null;
@@ -159,9 +159,9 @@ public class MediaController2Impl implements MediaController2Provider {
    }

    @Override
    public void release_impl() {
    public void close_impl() {
        if (DEBUG) {
            Log.d(TAG, "release from " + mToken);
            Log.d(TAG, "relese from " + mToken);
        }
        final IMediaSession2 binder;
        synchronized (mLock) {
@@ -397,7 +397,7 @@ public class MediaController2Impl implements MediaController2Provider {
            if (release) {
                // Trick to call release() without holding the lock, to prevent potential deadlock
                // with the developer's custom lock within the ControllerCallback.onDisconnected().
                mInstance.release();
                mInstance.close();
            }
        }
    }
@@ -530,7 +530,7 @@ public class MediaController2Impl implements MediaController2Provider {
            // Permanent lose of the binding because of the service package update or removed.
            // This SessionServiceRecord will be removed accordingly, but forget session binder here
            // for sure.
            mInstance.release();
            mInstance.close();
        }
    }
}
+22 −26
Original line number Diff line number Diff line
@@ -104,45 +104,41 @@ public class MediaSession2Impl implements MediaSession2Provider {
    @Override
    public void setPlayer_impl(MediaPlayerBase player) throws IllegalArgumentException {
        ensureCallingThread();
        // TODO(jaewan): Remove this when we don't inherits MediaPlayerBase.
        if (player instanceof MediaSession2 || player instanceof MediaController2) {
            throw new IllegalArgumentException("player doesn't accept MediaSession2 nor"
                    + " MediaController2");
        }
        if (player != null && mPlayer == player) {
            // Player didn't changed. No-op.
            return;
        if (player == null) {
            throw new IllegalArgumentException("player shouldn't be null");
        }
        setPlayerInternal(player);
    }

    private void setPlayerInternal(MediaPlayerBase player) {
        mHandler.removeCallbacksAndMessages(null);
        if (mPlayer == null && player != null) {
            if (DEBUG) {
                Log.d(TAG, "session is ready to use, id=" + mId);
            }
        } else if (mPlayer != null && player == null) {
            if (DEBUG) {
                Log.d(TAG, "session is now unavailable, id=" + mId);
            }
            if (mSessionStub != null) {
                // Invalidate previously published session stub.
                mSessionStub.destroyNotLocked();
            }
        if (mPlayer == player) {
            // Player didn't changed. No-op.
            return;
        }
        mHandler.removeCallbacksAndMessages(null);
        if (mPlayer != null && mListener != null) {
            // This might not work for a poorly implemented player.
            mPlayer.removePlaybackListener(mListener);
        }
        if (player != null) {
        mListener = new MyPlaybackListener(this, player);
        player.addPlaybackListener(mListener, mHandler);
        notifyPlaybackStateChanged(player.getPlaybackState());
        }
        mPlayer = player;
    }

    @Override
    public void close_impl() {
        // Flush any pending messages.
        mHandler.removeCallbacksAndMessages(null);
        if (mSessionStub != null) {
            if (DEBUG) {
                Log.d(TAG, "session is now unavailable, id=" + mId);
            }
            // Invalidate previously published session stub.
            mSessionStub.destroyNotLocked();
        }
    }

    @Override
    public MediaPlayerBase getPlayer_impl() {
        return getPlayer();
+27 −27
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ public class MediaController2Test extends MediaSession2TestBase {
        super.cleanUp();
        sHandler.postAndSync(() -> {
            if (mSession != null) {
                mSession.setPlayer(null);
                mSession.close();
            }
        });
        TestServiceRegistry.getInstance().cleanUp();
@@ -210,7 +210,7 @@ public class MediaController2Test extends MediaSession2TestBase {
            }
        };
        sHandler.postAndSync(() -> {
            mSession.setPlayer(null);
            mSession.close();
            mSession = new MediaSession2.Builder(mContext, mPlayer)
                    .setSessionCallback(sessionCallback).build();
        });
@@ -224,14 +224,14 @@ public class MediaController2Test extends MediaSession2TestBase {
    @Test
    public void testControllerCallback_releaseSession() throws InterruptedException {
        sHandler.postAndSync(() -> {
            mSession.setPlayer(null);
            mSession.close();
        });
        waitForDisconnect(mController, true);
    }

    @Test
    public void testControllerCallback_release() throws InterruptedException {
        mController.release();
        mController.close();
        waitForDisconnect(mController, true);
    }

@@ -239,7 +239,7 @@ public class MediaController2Test extends MediaSession2TestBase {
    public void testIsConnected() throws InterruptedException {
        assertTrue(mController.isConnected());
        sHandler.postAndSync(()->{
            mSession.setPlayer(null);
            mSession.close();
        });
        // postAndSync() to wait until the disconnection is propagated.
        sHandler.postAndSync(()->{
@@ -253,7 +253,7 @@ public class MediaController2Test extends MediaSession2TestBase {
    @Test
    public void testDeadlock() throws InterruptedException {
        sHandler.postAndSync(() -> {
            mSession.setPlayer(null);
            mSession.close();
            mSession = null;
        });

@@ -301,7 +301,7 @@ public class MediaController2Test extends MediaSession2TestBase {
            if (mSession != null) {
                sessionHandler.postAndSync(() -> {
                    // Clean up here because sessionHandler will be removed afterwards.
                    mSession.setPlayer(null);
                    mSession.close();
                    mSession = null;
                });
            }
@@ -384,26 +384,26 @@ public class MediaController2Test extends MediaSession2TestBase {
    }

    @Test
    public void testRelease_beforeConnected() throws InterruptedException {
    public void testClose_beforeConnected() throws InterruptedException {
        MediaController2 controller =
                createController(mSession.getToken(), false, null);
        controller.release();
        controller.close();
    }

    @Test
    public void testRelease_twice() throws InterruptedException {
        mController.release();
        mController.release();
    public void testClose_twice() throws InterruptedException {
        mController.close();
        mController.close();
    }

    @Test
    public void testRelease_session() throws InterruptedException {
    public void testClose_session() throws InterruptedException {
        final String id = mSession.getToken().getId();
        mController.release();
        // Release is done immediately for session.
        mController.close();
        // close is done immediately for session.
        testNoInteraction();

        // Test whether the controller is notified about later release of the session or
        // Test whether the controller is notified about later close of the session or
        // re-creation.
        testControllerAfterSessionIsGone(id);
    }
@@ -411,20 +411,20 @@ public class MediaController2Test extends MediaSession2TestBase {
    // TODO(jaewan): Reenable when session manager detects app installs
    @Ignore
    @Test
    public void testRelease_sessionService() throws InterruptedException {
    public void testClose_sessionService() throws InterruptedException {
        connectToService(TestUtils.getServiceToken(mContext, MockMediaSessionService2.ID));
        testReleaseFromService();
        testCloseFromService();
    }

    // TODO(jaewan): Reenable when session manager detects app installs
    @Ignore
    @Test
    public void testRelease_libraryService() throws InterruptedException {
    public void testClose_libraryService() throws InterruptedException {
        connectToService(TestUtils.getServiceToken(mContext, MockMediaSessionService2.ID));
        testReleaseFromService();
        testCloseFromService();
    }

    private void testReleaseFromService() throws InterruptedException {
    private void testCloseFromService() throws InterruptedException {
        final String id = mController.getSessionToken().getId();
        final CountDownLatch latch = new CountDownLatch(1);
        TestServiceRegistry.getInstance().setServiceInstanceChangedCallback((service) -> {
@@ -433,21 +433,21 @@ public class MediaController2Test extends MediaSession2TestBase {
                latch.countDown();
            }
        });
        mController.release();
        // Wait until release triggers onDestroy() of the session service.
        mController.close();
        // Wait until close triggers onDestroy() of the session service.
        assertTrue(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS));
        assertNull(TestServiceRegistry.getInstance().getServiceInstance());
        testNoInteraction();

        // Test whether the controller is notified about later release of the session or
        // Test whether the controller is notified about later close of the session or
        // re-creation.
        testControllerAfterSessionIsGone(id);
    }

    private void testControllerAfterSessionIsGone(final String id) throws InterruptedException {
        sHandler.postAndSync(() -> {
            // TODO(jaewan): Use Session.release later when we add the API.
            mSession.setPlayer(null);
            // TODO(jaewan): Use Session.close later when we add the API.
            mSession.close();
        });
        waitForDisconnect(mController, true);
        testNoInteraction();
@@ -464,7 +464,7 @@ public class MediaController2Test extends MediaSession2TestBase {
    private void testNoInteraction() throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        final PlaybackListener playbackListener = (state) -> {
            fail("Controller shouldn't be notified about change in session after the release.");
            fail("Controller shouldn't be notified about change in session after the close.");
            latch.countDown();
        };
        mController.addPlaybackListener(playbackListener, sHandler);
+4 −4
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ public class MediaSession2Test extends MediaSession2TestBase {
    public void cleanUp() throws Exception {
        super.cleanUp();
        sHandler.postAndSync(() -> {
            mSession.setPlayer(null);
            mSession.close();
        });
    }

@@ -93,7 +93,7 @@ public class MediaSession2Test extends MediaSession2TestBase {
            // Test if setPlayer doesn't crash with various situations.
            mSession.setPlayer(mPlayer);
            mSession.setPlayer(player);
            mSession.setPlayer(null);
            mSession.close();
        });
    }

@@ -199,7 +199,7 @@ public class MediaSession2Test extends MediaSession2TestBase {
    public void testOnCommandCallback() throws InterruptedException {
        final MockOnCommandCallback callback = new MockOnCommandCallback();
        sHandler.postAndSync(() -> {
            mSession.setPlayer(null);
            mSession.close();
            mPlayer = new MockPlayer(1);
            mSession = new MediaSession2.Builder(mContext, mPlayer)
                    .setSessionCallback(callback).build();
@@ -224,7 +224,7 @@ public class MediaSession2Test extends MediaSession2TestBase {
    public void testOnConnectCallback() throws InterruptedException {
        final MockOnConnectCallback sessionCallback = new MockOnConnectCallback();
        sHandler.postAndSync(() -> {
            mSession.setPlayer(null);
            mSession.close();
            mSession = new MediaSession2.Builder(mContext, mPlayer)
                    .setSessionCallback(sessionCallback).build();
        });
+1 −1
Original line number Diff line number Diff line
@@ -97,7 +97,7 @@ abstract class MediaSession2TestBase {
    @CallSuper
    public void cleanUp() throws Exception {
        for (int i = 0; i < mControllers.size(); i++) {
            mControllers.get(i).release();
            mControllers.get(i).close();
        }
    }

Loading