Handle the bitmap loading result with applicationHandler

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
(cherry picked from commit 80927260fd46413b7d1efafed72360b10049af2a)
This commit is contained in:
tianyifeng 2022-11-23 17:40:16 +00:00 committed by christosts
parent b495d21f04
commit d58b4fd6a6
2 changed files with 3 additions and 5 deletions

View File

@ -244,8 +244,6 @@ public class DefaultMediaNotificationProvider implements MediaNotification.Provi
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;

View File

@ -118,7 +118,6 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
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;
@ -162,7 +161,6 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
@Initialized
MediaSessionLegacyStub thisRef = this;
sessionCompat.setCallback(thisRef, handler);
mainHandler = new Handler(Looper.getMainLooper());
}
/** Starts to receive commands. */
@ -1205,7 +1203,9 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
}
};
Futures.addCallback(
bitmapFuture, pendingBitmapLoadCallback, /* executor= */ mainHandler::post);
bitmapFuture,
pendingBitmapLoadCallback,
/* executor= */ sessionImpl.getApplicationHandler()::post);
}
}
setMetadata(