Skip to content

Commit 707c2d8

Browse files
christostsmicrokatz
authored andcommitted
MediaController: Add missing event flags (2/2)
This is the follow-up commit where the onEvents callback raised by MediaController contains the missing events, for the case where MediaController is connected to a legacy MediaSession. #minor-release PiperOrigin-RevId: 487231996 (cherry picked from commit c403b4c)
1 parent c89ceb8 commit 707c2d8

File tree

2 files changed

+170
-8
lines changed

2 files changed

+170
-8
lines changed

libraries/session/src/main/java/androidx/media3/session/MediaControllerImplLegacy.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,9 +1435,8 @@ private void updateControllerInfo(
14351435
Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED));
14361436
}
14371437
if (!Util.areEqual(oldLegacyPlayerInfo.queueTitle, newLegacyPlayerInfo.queueTitle)) {
1438-
// TODO(b/187152483): Set proper event code when available.
14391438
listeners.queueEvent(
1440-
C.INDEX_UNSET,
1439+
Player.EVENT_PLAYLIST_METADATA_CHANGED,
14411440
(listener) ->
14421441
listener.onPlaylistMetadataChanged(newControllerInfo.playerInfo.playlistMetadata));
14431442
}
@@ -1515,23 +1514,20 @@ private void updateControllerInfo(
15151514
}
15161515
if (!oldControllerInfo.playerInfo.audioAttributes.equals(
15171516
newControllerInfo.playerInfo.audioAttributes)) {
1518-
// TODO(b/187152483): Set proper event code when available.
15191517
listeners.queueEvent(
1520-
C.INDEX_UNSET,
1518+
Player.EVENT_AUDIO_ATTRIBUTES_CHANGED,
15211519
(listener) ->
15221520
listener.onAudioAttributesChanged(newControllerInfo.playerInfo.audioAttributes));
15231521
}
15241522
if (!oldControllerInfo.playerInfo.deviceInfo.equals(newControllerInfo.playerInfo.deviceInfo)) {
1525-
// TODO(b/187152483): Set proper event code when available.
15261523
listeners.queueEvent(
1527-
C.INDEX_UNSET,
1524+
Player.EVENT_DEVICE_INFO_CHANGED,
15281525
(listener) -> listener.onDeviceInfoChanged(newControllerInfo.playerInfo.deviceInfo));
15291526
}
15301527
if (oldControllerInfo.playerInfo.deviceVolume != newControllerInfo.playerInfo.deviceVolume
15311528
|| oldControllerInfo.playerInfo.deviceMuted != newControllerInfo.playerInfo.deviceMuted) {
1532-
// TODO(b/187152483): Set proper event code when available.
15331529
listeners.queueEvent(
1534-
C.INDEX_UNSET,
1530+
Player.EVENT_DEVICE_VOLUME_CHANGED,
15351531
(listener) ->
15361532
listener.onDeviceVolumeChanged(
15371533
newControllerInfo.playerInfo.deviceVolume,

libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaControllerListenerWithMediaSessionCompatTest.java

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,25 @@
1616
package androidx.media3.session;
1717

1818
import static androidx.media3.test.session.common.TestUtils.TIMEOUT_MS;
19+
import static androidx.media3.test.session.common.TestUtils.getEventsAsList;
1920
import static com.google.common.truth.Truth.assertThat;
2021
import static java.util.concurrent.TimeUnit.MILLISECONDS;
22+
import static org.junit.Assume.assumeTrue;
2123

2224
import android.content.Context;
25+
import android.media.AudioManager;
2326
import android.os.Bundle;
2427
import android.os.RemoteException;
2528
import android.support.v4.media.session.MediaSessionCompat;
2629
import android.support.v4.media.session.PlaybackStateCompat;
30+
import androidx.media.VolumeProviderCompat;
31+
import androidx.media3.common.AudioAttributes;
2732
import androidx.media3.common.C;
33+
import androidx.media3.common.DeviceInfo;
2834
import androidx.media3.common.FlagSet;
35+
import androidx.media3.common.MediaMetadata;
2936
import androidx.media3.common.Player;
37+
import androidx.media3.common.util.Util;
3038
import androidx.media3.test.session.common.CommonConstants;
3139
import androidx.media3.test.session.common.HandlerThreadTestRule;
3240
import androidx.media3.test.session.common.MainLooperTestRule;
@@ -40,6 +48,7 @@
4048
import java.util.List;
4149
import java.util.concurrent.CopyOnWriteArrayList;
4250
import java.util.concurrent.CountDownLatch;
51+
import java.util.concurrent.atomic.AtomicInteger;
4352
import java.util.concurrent.atomic.AtomicReference;
4453
import org.junit.After;
4554
import org.junit.Before;
@@ -190,4 +199,161 @@ public void onExtrasChanged(MediaController controller, Bundle extras) {
190199
assertThat(countDownLatch.await(1_000, MILLISECONDS)).isTrue();
191200
assertThat(TestUtils.equals(receivedSessionExtras.get(0), sessionExtras)).isTrue();
192201
}
202+
203+
@Test
204+
public void onPlaylistMetadataChanged() throws Exception {
205+
MediaController controller = controllerTestRule.createController(session.getSessionToken());
206+
CountDownLatch latch = new CountDownLatch(2);
207+
AtomicReference<MediaMetadata> playlistMetadataParamRef = new AtomicReference<>();
208+
AtomicReference<MediaMetadata> playlistMetadataGetterRef = new AtomicReference<>();
209+
AtomicReference<MediaMetadata> playlistMetadataOnEventsRef = new AtomicReference<>();
210+
AtomicReference<Player.Events> onEvents = new AtomicReference<>();
211+
Player.Listener listener =
212+
new Player.Listener() {
213+
@Override
214+
public void onPlaylistMetadataChanged(MediaMetadata mediaMetadata) {
215+
playlistMetadataParamRef.set(mediaMetadata);
216+
playlistMetadataGetterRef.set(controller.getPlaylistMetadata());
217+
latch.countDown();
218+
}
219+
220+
@Override
221+
public void onEvents(Player player, Player.Events events) {
222+
onEvents.set(events);
223+
playlistMetadataOnEventsRef.set(player.getPlaylistMetadata());
224+
latch.countDown();
225+
}
226+
};
227+
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
228+
229+
session.setQueueTitle("queue-title");
230+
231+
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
232+
assertThat(playlistMetadataParamRef.get().title).isEqualTo("queue-title");
233+
assertThat(playlistMetadataGetterRef.get()).isEqualTo(playlistMetadataParamRef.get());
234+
assertThat(playlistMetadataOnEventsRef.get()).isEqualTo(playlistMetadataParamRef.get());
235+
assertThat(getEventsAsList(onEvents.get()))
236+
.containsExactly(Player.EVENT_PLAYLIST_METADATA_CHANGED);
237+
}
238+
239+
@Test
240+
public void onAudioAttributesChanged() throws Exception {
241+
// We need to trigger MediaControllerCompat.Callback.onAudioInfoChanged in order to raise the
242+
// onAudioAttributesChanged() callback. In API 21 and 22, onAudioInfoChanged is not called when
243+
// playback is changed to local.
244+
assumeTrue(Util.SDK_INT != 21 && Util.SDK_INT != 22);
245+
246+
session.setPlaybackToRemote(
247+
/* volumeControl= */ VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE,
248+
/* maxVolume= */ 100,
249+
/* currentVolume= */ 50);
250+
MediaController controller = controllerTestRule.createController(session.getSessionToken());
251+
CountDownLatch latch = new CountDownLatch(2);
252+
AtomicReference<AudioAttributes> audioAttributesParamRef = new AtomicReference<>();
253+
AtomicReference<AudioAttributes> audioAttributesGetterRef = new AtomicReference<>();
254+
AtomicReference<AudioAttributes> audioAttributesOnEventsRef = new AtomicReference<>();
255+
AtomicReference<Player.Events> onEvents = new AtomicReference<>();
256+
Player.Listener listener =
257+
new Player.Listener() {
258+
@Override
259+
public void onAudioAttributesChanged(AudioAttributes audioAttributes) {
260+
audioAttributesParamRef.set(audioAttributes);
261+
audioAttributesGetterRef.set(controller.getAudioAttributes());
262+
latch.countDown();
263+
}
264+
265+
@Override
266+
public void onEvents(Player player, Player.Events events) {
267+
onEvents.set(events);
268+
audioAttributesOnEventsRef.set(player.getAudioAttributes());
269+
latch.countDown();
270+
}
271+
};
272+
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
273+
274+
session.setPlaybackToLocal(AudioManager.STREAM_ALARM);
275+
276+
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
277+
assertThat(audioAttributesGetterRef.get().contentType).isEqualTo(AudioManager.STREAM_ALARM);
278+
assertThat(audioAttributesGetterRef.get()).isEqualTo(audioAttributesParamRef.get());
279+
assertThat(audioAttributesOnEventsRef.get()).isEqualTo(audioAttributesParamRef.get());
280+
assertThat(getEventsAsList(onEvents.get())).contains(Player.EVENT_AUDIO_ATTRIBUTES_CHANGED);
281+
}
282+
283+
@Test
284+
public void onDeviceInfoChanged() throws Exception {
285+
MediaController controller = controllerTestRule.createController(session.getSessionToken());
286+
CountDownLatch latch = new CountDownLatch(2);
287+
AtomicReference<DeviceInfo> deviceInfoParamRef = new AtomicReference<>();
288+
AtomicReference<DeviceInfo> deviceInfoGetterRef = new AtomicReference<>();
289+
AtomicReference<DeviceInfo> deviceInfoOnEventsRef = new AtomicReference<>();
290+
AtomicReference<Player.Events> onEvents = new AtomicReference<>();
291+
Player.Listener listener =
292+
new Player.Listener() {
293+
@Override
294+
public void onDeviceInfoChanged(DeviceInfo deviceInfo) {
295+
deviceInfoParamRef.set(deviceInfo);
296+
deviceInfoGetterRef.set(controller.getDeviceInfo());
297+
latch.countDown();
298+
}
299+
300+
@Override
301+
public void onEvents(Player player, Player.Events events) {
302+
deviceInfoOnEventsRef.set(player.getDeviceInfo());
303+
onEvents.set(events);
304+
latch.countDown();
305+
}
306+
};
307+
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
308+
309+
session.setPlaybackToRemote(
310+
/* volumeControl= */ VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE,
311+
/* maxVolume= */ 100,
312+
/* currentVolume= */ 50);
313+
314+
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
315+
assertThat(deviceInfoParamRef.get().playbackType).isEqualTo(DeviceInfo.PLAYBACK_TYPE_REMOTE);
316+
assertThat(deviceInfoParamRef.get().maxVolume).isEqualTo(100);
317+
assertThat(deviceInfoGetterRef.get()).isEqualTo(deviceInfoParamRef.get());
318+
assertThat(deviceInfoOnEventsRef.get()).isEqualTo(deviceInfoGetterRef.get());
319+
assertThat(getEventsAsList(onEvents.get())).contains(Player.EVENT_DEVICE_VOLUME_CHANGED);
320+
}
321+
322+
@Test
323+
public void onDeviceVolumeChanged() throws Exception {
324+
MediaController controller = controllerTestRule.createController(session.getSessionToken());
325+
CountDownLatch latch = new CountDownLatch(2);
326+
AtomicInteger deviceVolumeParam = new AtomicInteger();
327+
AtomicInteger deviceVolumeGetter = new AtomicInteger();
328+
AtomicInteger deviceVolumeOnEvents = new AtomicInteger();
329+
AtomicReference<Player.Events> onEvents = new AtomicReference<>();
330+
Player.Listener listener =
331+
new Player.Listener() {
332+
@Override
333+
public void onDeviceVolumeChanged(int volume, boolean muted) {
334+
deviceVolumeParam.set(volume);
335+
deviceVolumeGetter.set(controller.getDeviceVolume());
336+
latch.countDown();
337+
}
338+
339+
@Override
340+
public void onEvents(Player player, Player.Events events) {
341+
deviceVolumeOnEvents.set(player.getDeviceVolume());
342+
onEvents.set(events);
343+
latch.countDown();
344+
}
345+
};
346+
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
347+
348+
session.setPlaybackToRemote(
349+
/* volumeControl= */ VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE,
350+
/* maxVolume= */ 100,
351+
/* currentVolume= */ 50);
352+
353+
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
354+
assertThat(deviceVolumeParam.get()).isEqualTo(50);
355+
assertThat(deviceVolumeGetter.get()).isEqualTo(50);
356+
assertThat(deviceVolumeOnEvents.get()).isEqualTo(50);
357+
assertThat(getEventsAsList(onEvents.get())).contains(Player.EVENT_DEVICE_VOLUME_CHANGED);
358+
}
193359
}

0 commit comments

Comments
 (0)