Skip to content

Commit

Permalink
Handle the bitmap loading result with applicationHandler
Browse files Browse the repository at this point in the history
Before this change, the bitmap loading result with mainHandler, in which we set the metadata to `MediaSessionCompat`. However, the `MediaSessionCompat` is not thread safe, all calls should be made from the same thread. In the other calls to `MediaSessionCompat`, we ensure that they are on the application thread (which may be or may not be main thread), so we should do the same for `setMetadata` when bitmap arrives.

Also removes a comment in `DefaultMediaNotificationProvider` as bitmap request caching is already moved to CacheBitmapLoader.

PiperOrigin-RevId: 490524209
  • Loading branch information
tianyif authored and icbaker committed Nov 24, 2022
1 parent 2693ca1 commit 8092726
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ public interface NotificationIdProvider {
private final String channelId;
@StringRes private final int channelNameResourceId;
private final NotificationManager notificationManager;
// Cache the last bitmap load request to avoid reloading the bitmap again, particularly useful
// when showing a notification for the same item (e.g. when switching from playing to paused).
private final Handler mainHandler;

private @MonotonicNonNull OnBitmapLoadedFutureCallback pendingOnBitmapLoadedFutureCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
private final MediaPlayPauseKeyHandler mediaPlayPauseKeyHandler;
private final MediaSessionCompat sessionCompat;
@Nullable private VolumeProviderCompat volumeProviderCompat;
private final Handler mainHandler;

private volatile long connectionTimeoutMs;
@Nullable private FutureCallback<Bitmap> pendingBitmapLoadCallback;
Expand Down Expand Up @@ -162,7 +161,6 @@ public MediaSessionLegacyStub(
@Initialized
MediaSessionLegacyStub thisRef = this;
sessionCompat.setCallback(thisRef, handler);
mainHandler = new Handler(Looper.getMainLooper());
}

/** Starts to receive commands. */
Expand Down Expand Up @@ -1205,7 +1203,9 @@ public void onFailure(Throwable t) {
}
};
Futures.addCallback(
bitmapFuture, pendingBitmapLoadCallback, /* executor= */ mainHandler::post);
bitmapFuture,
pendingBitmapLoadCallback,
/* executor= */ sessionImpl.getApplicationHandler()::post);
}
}
setMetadata(
Expand Down

0 comments on commit 8092726

Please sign in to comment.