From f3a9b81b670c0e999a9d2fcc8b657d3e85e182f0 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 21 Jul 2022 23:03:42 +0200 Subject: [PATCH] Fix sometimes seeing outdated thumbnail in notification Before the thumbnail finishes loading for the new video the player is now playing, the old thumbnail was being used, leading to wrong thumbnails set in the media session and the notification. --- .../java/org/schabi/newpipe/player/Player.java | 14 +++++++++----- .../org/schabi/newpipe/util/PicassoHelper.java | 7 +++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.java b/app/src/main/java/org/schabi/newpipe/player/Player.java index e1bc2f061..13dd1d938 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -748,11 +748,15 @@ public final class Player implements PlaybackListener, Listener { //////////////////////////////////////////////////////////////////////////*/ //region Thumbnail loading - private void initThumbnail(final String url) { + private void loadCurrentThumbnail(final String url) { if (DEBUG) { - Log.d(TAG, "Thumbnail - initThumbnail() called with url = [" + Log.d(TAG, "Thumbnail - loadCurrentThumbnail() called with url = [" + (url == null ? "null" : url) + "]"); } + + // Unset currentThumbnail, since it is now outdated. This ensures it is not used in media + // session metadata while the new thumbnail is being loaded by Picasso. + currentThumbnail = null; if (isNullOrEmpty(url)) { return; } @@ -762,8 +766,8 @@ public final class Player implements PlaybackListener, Listener { @Override public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) { if (DEBUG) { - Log.d(TAG, "Thumbnail - onLoadingComplete() called with: url = [" + url - + "], " + "loadedImage = [" + bitmap + " -> " + bitmap.getWidth() + "x" + Log.d(TAG, "Thumbnail - onBitmapLoaded() called with: url = [" + url + + "], " + "bitmap = [" + bitmap + " -> " + bitmap.getWidth() + "x" + bitmap.getHeight() + "], from = [" + from + "]"); } @@ -1727,7 +1731,7 @@ public final class Player implements PlaybackListener, Listener { maybeAutoQueueNextStream(info); - initThumbnail(info.getThumbnailUrl()); + loadCurrentThumbnail(info.getThumbnailUrl()); registerStreamViewed(); notifyMetadataUpdateToListeners(); diff --git a/app/src/main/java/org/schabi/newpipe/util/PicassoHelper.java b/app/src/main/java/org/schabi/newpipe/util/PicassoHelper.java index fc7600d4b..5739b930b 100644 --- a/app/src/main/java/org/schabi/newpipe/util/PicassoHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/PicassoHelper.java @@ -1,10 +1,12 @@ package org.schabi.newpipe.util; +import static org.schabi.newpipe.MainActivity.DEBUG; import static org.schabi.newpipe.extractor.utils.Utils.isBlank; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; +import android.util.Log; import androidx.annotation.Nullable; @@ -24,6 +26,7 @@ import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; public final class PicassoHelper { + private static final String TAG = PicassoHelper.class.getSimpleName(); public static final String PLAYER_THUMBNAIL_TAG = "PICASSO_PLAYER_THUMBNAIL_TAG"; private static final String PLAYER_THUMBNAIL_TRANSFORMATION_KEY = "PICASSO_PLAYER_THUMBNAIL_TRANSFORMATION_KEY"; @@ -129,6 +132,10 @@ public final class PicassoHelper { .transform(new Transformation() { @Override public Bitmap transform(final Bitmap source) { + if (DEBUG) { + Log.d(TAG, "Thumbnail - transform() called"); + } + final float notificationThumbnailWidth = Math.min( context.getResources() .getDimension(R.dimen.player_notification_thumbnail_width),