Skip to content

Commit 6de67eb

Browse files
committed
Debounce calls to onNotificationRefreshRequired()
1 parent 9da4d35 commit 6de67eb

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,18 @@
141141
private final boolean playIfSuppressed;
142142
private final HandlerThread compatSessionInteractionThread;
143143
private final Handler compatSessionInteractionHandler;
144+
private final Runnable callOnNotificationRefreshRequiredRunnable =
145+
this::callOnNotificationRefreshRequiredIfNeeded;
146+
private final Runnable requestNotificationRefreshRunnable =
147+
() -> notificationRefreshRequiredPending = true;
144148

145149
private volatile long connectionTimeoutMs;
146150
@Nullable private FutureCallback<Bitmap> pendingBitmapLoadCallback;
147151
@Nullable private VolumeProviderCompat volumeProviderCompat;
148152
private int sessionFlags;
149153
@Nullable private LegacyError legacyError;
150154
private Bundle legacyExtras;
155+
private boolean notificationRefreshRequiredPending;
151156
private ImmutableList<CommandButton> customLayout;
152157
private ImmutableList<CommandButton> mediaButtonPreferences;
153158
private SessionCommands availableSessionCommands;
@@ -1083,9 +1088,7 @@ public void updateLegacySessionPlaybackState(PlayerWrapper playerWrapper, boolea
10831088
postOrRunForCompatSession(
10841089
() -> {
10851090
sessionCompat.setPlaybackState(playbackStateCompat);
1086-
if (notify) {
1087-
sessionImpl.onNotificationRefreshRequired();
1088-
}
1091+
requestNotificationRefresh(notify);
10891092
});
10901093
});
10911094
}
@@ -1222,6 +1225,29 @@ private static void sendCustomCommandResultWhenReady(
12221225
MoreExecutors.directExecutor());
12231226
}
12241227

1228+
private void callOnNotificationRefreshRequiredIfNeeded() {
1229+
if (notificationRefreshRequiredPending) {
1230+
sessionImpl.onNotificationRefreshRequired();
1231+
notificationRefreshRequiredPending = false;
1232+
}
1233+
}
1234+
1235+
private void requestNotificationRefresh(boolean postRunnable) {
1236+
if (compatSessionInteractionHandler.getLooper().isCurrentThread()) {
1237+
notificationRefreshRequiredPending = true;
1238+
} else {
1239+
compatSessionInteractionHandler.post(requestNotificationRefreshRunnable);
1240+
}
1241+
if (postRunnable) {
1242+
postOnNotificationRefreshRequiredRunnable();
1243+
}
1244+
}
1245+
1246+
private void postOnNotificationRefreshRequiredRunnable() {
1247+
compatSessionInteractionHandler.removeCallbacks(callOnNotificationRefreshRequiredRunnable);
1248+
compatSessionInteractionHandler.post(callOnNotificationRefreshRequiredRunnable);
1249+
}
1250+
12251251
private static <T> void ignoreFuture(Future<T> unused) {
12261252
// no-op
12271253
}
@@ -1547,7 +1573,7 @@ public void onMediaItemTransition(
15471573
LegacyConversions.getRatingCompatStyle(mediaItem.mediaMetadata.userRating));
15481574
}
15491575
sessionCompat.setPlaybackState(playbackStateCompat);
1550-
sessionImpl.onNotificationRefreshRequired();
1576+
requestNotificationRefresh(true);
15511577
});
15521578
}
15531579

@@ -1575,9 +1601,7 @@ private void updateQueue(Timeline timeline, boolean notify) {
15751601
postOrRunForCompatSession(
15761602
() -> {
15771603
setQueue(/* queue= */ null);
1578-
if (notify) {
1579-
sessionImpl.onNotificationRefreshRequired();
1580-
}
1604+
requestNotificationRefresh(notify);
15811605
});
15821606
return;
15831607
}
@@ -1629,9 +1653,7 @@ private void handleBitmapFuturesAllCompletedAndSetQueue(
16291653
// Framework MediaSession#setQueue() uses ParceledListSlice,
16301654
// which means we can safely send long lists.
16311655
setQueue(queueItemList);
1632-
if (notify) {
1633-
sessionImpl.onNotificationRefreshRequired();
1634-
}
1656+
requestNotificationRefresh(notify);
16351657
}
16361658

16371659
@Override
@@ -1680,7 +1702,7 @@ public void onAudioAttributesChanged(int seq, AudioAttributes audioAttributes) {
16801702
return;
16811703
}
16821704
sessionCompat.setPlaybackToLocal(audioAttributes.getStreamType());
1683-
sessionImpl.onNotificationRefreshRequired();
1705+
requestNotificationRefresh(true);
16841706
});
16851707
}
16861708
}
@@ -1697,7 +1719,7 @@ public void onDeviceInfoChanged(int seq, DeviceInfo deviceInfo) {
16971719
return;
16981720
}
16991721
sessionCompat.setPlaybackToLocal(streamType);
1700-
sessionImpl.onNotificationRefreshRequired();
1722+
requestNotificationRefresh(true);
17011723
});
17021724
} else {
17031725
postOrRunForCompatSession(() -> {
@@ -1706,7 +1728,7 @@ public void onDeviceInfoChanged(int seq, DeviceInfo deviceInfo) {
17061728
return;
17071729
}
17081730
sessionCompat.setPlaybackToRemote(volumeProviderCompat);
1709-
sessionImpl.onNotificationRefreshRequired();
1731+
requestNotificationRefresh(true);
17101732
});
17111733
}
17121734
}
@@ -1751,7 +1773,7 @@ private void updateMetadataIfChanged(boolean notify) {
17511773
&& Objects.equals(lastMediaUri, newMediaUri)
17521774
&& lastDurationMs == newDurationMs) {
17531775
if (notify) {
1754-
sessionImpl.onNotificationRefreshRequired();
1776+
postOnNotificationRefreshRequiredRunnable();
17551777
}
17561778
return;
17571779
}
@@ -1789,7 +1811,7 @@ public void onSuccess(Bitmap result) {
17891811
newMediaUri,
17901812
newDurationMs,
17911813
/* artworkBitmap= */ result));
1792-
sessionImpl.onNotificationRefreshRequired();
1814+
requestNotificationRefresh(true);
17931815
});
17941816
}
17951817

@@ -1813,9 +1835,7 @@ public void onFailure(Throwable t) {
18131835
setMetadata(
18141836
LegacyConversions.convertToMediaMetadataCompat(
18151837
newMediaMetadata, newMediaId, newMediaUri, newDurationMs, artworkBitmapFinal));
1816-
if (notify) {
1817-
sessionImpl.onNotificationRefreshRequired();
1818-
}
1838+
requestNotificationRefresh(notify);
18191839
});
18201840
}
18211841
}

0 commit comments

Comments
 (0)