use of androidx.media3.common.StarRating in project media by androidx.
the class MediaSessionCallbackTest method onSetRatingWithoutMediaId.
@Test
public void onSetRatingWithoutMediaId() throws Exception {
float ratingValue = 3.5f;
Rating testRating = new StarRating(5, ratingValue);
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Rating> ratingRef = new AtomicReference<>();
MediaSession.SessionCallback callback = new MediaSession.SessionCallback() {
@Override
public ListenableFuture<SessionResult> onSetRating(MediaSession session, ControllerInfo controller, Rating rating) {
if (!TextUtils.equals(controller.getPackageName(), SUPPORT_APP_PACKAGE_NAME)) {
return Futures.immediateFuture(new SessionResult(RESULT_INFO_SKIPPED));
}
ratingRef.set(rating);
latch.countDown();
return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
}
};
MediaSession session = sessionTestRule.ensureReleaseAfterTest(new MediaSession.Builder(context, player).setSessionCallback(callback).setId("testOnSetRating").build());
RemoteMediaController controller = controllerTestRule.createRemoteController(session.getToken());
SessionResult result = controller.setRating(testRating);
assertThat(result.resultCode).isEqualTo(RESULT_SUCCESS);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(ratingRef.get()).isEqualTo(testRating);
}
use of androidx.media3.common.StarRating in project media by androidx.
the class MediaSessionCompatCallbackWithMediaControllerTest method setRatingWithMediaId.
@Test
public void setRatingWithMediaId() throws Exception {
float ratingValue = 3.5f;
Rating rating = new StarRating(5, ratingValue);
String mediaId = "media_id";
MediaMetadataCompat metadata = new MediaMetadataCompat.Builder().putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, mediaId).build();
session.setMetadata(metadata);
RemoteMediaController controller = createControllerAndWaitConnection();
sessionCallback.reset(1);
controller.setRating(mediaId, rating);
assertThat(sessionCallback.await(TIMEOUT_MS)).isTrue();
assertThat(sessionCallback.onSetRatingCalled).isTrue();
assertThat(MediaUtils.convertToRating(sessionCallback.rating)).isEqualTo(rating);
}
use of androidx.media3.common.StarRating in project media by androidx.
the class MediaSessionCallbackTest method onSetRatingWithMediaId.
@Test
public void onSetRatingWithMediaId() throws Exception {
float ratingValue = 3.5f;
Rating testRating = new StarRating(5, ratingValue);
String testMediaId = "media_id";
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<String> mediaIdRef = new AtomicReference<>();
AtomicReference<Rating> ratingRef = new AtomicReference<>();
MediaSession.SessionCallback callback = new MediaSession.SessionCallback() {
@Override
public ListenableFuture<SessionResult> onSetRating(MediaSession session, ControllerInfo controller, String mediaId, Rating rating) {
if (!TextUtils.equals(controller.getPackageName(), SUPPORT_APP_PACKAGE_NAME)) {
return Futures.immediateFuture(new SessionResult(RESULT_INFO_SKIPPED));
}
mediaIdRef.set(mediaId);
ratingRef.set(rating);
latch.countDown();
return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
}
};
MediaSession session = sessionTestRule.ensureReleaseAfterTest(new MediaSession.Builder(context, player).setSessionCallback(callback).setId("testOnSetRating").build());
RemoteMediaController controller = controllerTestRule.createRemoteController(session.getToken());
SessionResult result = controller.setRating(testMediaId, testRating);
assertThat(result.resultCode).isEqualTo(RESULT_SUCCESS);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(mediaIdRef.get()).isEqualTo(testMediaId);
assertThat(ratingRef.get()).isEqualTo(testRating);
}
use of androidx.media3.common.StarRating in project media by androidx.
the class MediaSessionPermissionTest method changingPermissionForSessionCommandWithSetAvailableCommands.
@Test
public void changingPermissionForSessionCommandWithSetAvailableCommands() throws Exception {
String mediaId = "testSetRating";
Rating rating = new StarRating(5, 3.5f);
createSessionWithAvailableCommands(createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)), Player.Commands.EMPTY);
RemoteMediaController controller = controllerTestRule.createRemoteController(session.getToken());
controller.setRating(mediaId, rating);
assertThat(callback.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(callback.onSetRatingCalled).isTrue();
callback.reset();
// Change allowed commands.
session.setAvailableCommands(getTestControllerInfo(), createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_MEDIA_URI)), Player.Commands.EMPTY);
controller.setRating(mediaId, rating);
assertThat(callback.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
}
use of androidx.media3.common.StarRating in project media by androidx.
the class StarRating method toBundle.
@UnstableApi
@Override
public Bundle toBundle() {
Bundle bundle = new Bundle();
bundle.putInt(keyForField(FIELD_RATING_TYPE), TYPE);
bundle.putInt(keyForField(FIELD_MAX_STARS), maxStars);
bundle.putFloat(keyForField(FIELD_STAR_RATING), starRating);
return bundle;
}
Aggregations