Skip to content
Snippets Groups Projects
Commit 94b25696 authored by Etienne Ruffieux's avatar Etienne Ruffieux
Browse files

Fix null playstate update from players.

Some players will send a null object as parameter for
playstate change callback, without updating the
MediaSession. These events should still trigger an update
by using the Mediasession metadata if it is different.

Bug: 297191838
Tag: #feature
Test: atest BluetoothInstrumentationTests
Change-Id: Ic5a5b6a68e616af939a4e42d7d64476932dc38d3
parent 7e0b6227
No related branches found
No related tags found
No related merge requests found
...@@ -461,8 +461,11 @@ public class MediaPlayerWrapper { ...@@ -461,8 +461,11 @@ public class MediaPlayerWrapper {
@Override @Override
public void onMetadataChanged(@Nullable MediaMetadata mediaMetadata) { public void onMetadataChanged(@Nullable MediaMetadata mediaMetadata) {
if (!isMetadataReady()) { if (!isMetadataReady()) {
Log.v(TAG, "onMetadataChanged(): " + mPackageName Log.v(
+ " tried to update with no queue"); TAG,
"onMetadataChanged(): "
+ mPackageName
+ " tried to update with no metadata");
return; return;
} }
...@@ -495,13 +498,16 @@ public class MediaPlayerWrapper { ...@@ -495,13 +498,16 @@ public class MediaPlayerWrapper {
@Override @Override
public void onPlaybackStateChanged(@Nullable PlaybackState state) { public void onPlaybackStateChanged(@Nullable PlaybackState state) {
if (!isPlaybackStateReady()) { if (!isPlaybackStateReady()) {
Log.v(TAG, "onPlaybackStateChanged(): " + mPackageName Log.v(
+ " tried to update with no queue"); TAG,
"onPlaybackStateChanged(): "
+ mPackageName
+ " tried to update with no state");
return; return;
} }
mPlaybackStateChangeEventLogger.logv(TAG, "onPlaybackStateChanged(): " mPlaybackStateChangeEventLogger.logv(
+ mPackageName + " : " + state.toString()); TAG, "onPlaybackStateChanged(): " + mPackageName + " : " + state);
if (!playstateEquals(state, getPlaybackState())) { if (!playstateEquals(state, getPlaybackState())) {
e("The callback playback state doesn't match the current state"); e("The callback playback state doesn't match the current state");
...@@ -513,8 +519,8 @@ public class MediaPlayerWrapper { ...@@ -513,8 +519,8 @@ public class MediaPlayerWrapper {
return; return;
} }
// If there is no playstate, ignore the update. // If state isn't null and there is no playstate, ignore the update.
if (state.getState() == PlaybackState.STATE_NONE) { if (state != null && state.getState() == PlaybackState.STATE_NONE) {
Log.v(TAG, "Waiting to send update as controller has no playback state"); Log.v(TAG, "Waiting to send update as controller has no playback state");
return; return;
} }
......
...@@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat; ...@@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
...@@ -377,6 +376,38 @@ public class MediaPlayerWrapperTest { ...@@ -377,6 +376,38 @@ public class MediaPlayerWrapperTest {
Util.toMetadata(mMockContext, mTestMetadata.build())); Util.toMetadata(mMockContext, mTestMetadata.build()));
} }
@Test
public void testNullPlayback() {
// Create the wrapper object and register the looper with the timeout handler
TestLooperManager looperManager = new TestLooperManager(mThread.getLooper());
MediaPlayerWrapper wrapper =
MediaPlayerWrapperFactory.wrap(mMockContext, mMockController, mThread.getLooper());
wrapper.registerCallback(mTestCbs);
// Return null when getting the queue
doReturn(null).when(mMockController).getQueue();
// Grab the callbacks the wrapper registered with the controller
verify(mMockController).registerCallback(mControllerCbs.capture(), any());
MediaController.Callback controllerCallbacks = mControllerCbs.getValue();
// Update Metadata returned by controller
mTestState.setState(PlaybackState.STATE_PLAYING, 1000, 1.0f);
doReturn(mTestState.build()).when(mMockController).getPlaybackState();
// Call the callback
controllerCallbacks.onPlaybackStateChanged(null);
// Assert that the metadata returned by getPlaybackState() is used instead of null
verify(mTestCbs, times(1)).mediaUpdatedCallback(mMediaUpdateData.capture());
MediaData data = mMediaUpdateData.getValue();
Assert.assertEquals(
"Returned PlaybackState is incorrect",
data.state.toString(),
mTestState.build().toString());
}
@Test @Test
public void testNullQueue() { public void testNullQueue() {
// Create the wrapper object and register the looper with the timeout handler // Create the wrapper object and register the looper with the timeout handler
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment