Skip to content

Commit

Permalink
Apply review: move thumbnail loading out of Player
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Jun 18, 2021
1 parent e9d6c7a commit 6fe3182
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 79 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/schabi/newpipe/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void onCreate() {
PicassoHelper.init(this);
PicassoHelper.setShouldLoadImages(
prefs.getBoolean(getString(R.string.download_thumbnail_key), true));
PicassoHelper.setIndicatorsEnabled(BuildConfig.DEBUG
PicassoHelper.setIndicatorsEnabled(MainActivity.DEBUG
&& prefs.getBoolean(getString(R.string.show_image_indicators_key), false));

configureRxJavaErrorHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ private void initThumbnailViews(@NonNull final StreamInfo info) {
.into(binding.detailThumbnailImageView, new Callback() {
@Override
public void onSuccess() {
// nothing to do, the image was loaded correctly into the thumbnail
}

@Override
Expand Down
108 changes: 31 additions & 77 deletions app/src/main/java/org/schabi/newpipe/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.squareup.picasso.Transformation;

import org.schabi.newpipe.DownloaderImpl;
import org.schabi.newpipe.MainActivity;
Expand Down Expand Up @@ -155,6 +154,7 @@
import static com.google.android.exoplayer2.Player.RepeatMode;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import static org.schabi.newpipe.ktx.ViewUtils.animate;
import static org.schabi.newpipe.ktx.ViewUtils.animateRotation;
import static org.schabi.newpipe.player.MainPlayer.ACTION_CLOSE;
Expand Down Expand Up @@ -244,9 +244,6 @@ public final class Player implements

private static final int RENDERER_UNAVAILABLE = -1;

private static final String PICASSO_PLAYER_TAG = "PICASSO_PLAYER_TAG";
private static final String PICASSO_TRANSFORMATION_KEY = "PICASSO_TRANSFORMATION_KEY";

/*//////////////////////////////////////////////////////////////////////////
// Playback
//////////////////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -811,7 +808,7 @@ public void destroy() {

databaseUpdateDisposable.clear();
progressUpdateDisposable.set(null);
PicassoHelper.cancelTag(PICASSO_PLAYER_TAG);
PicassoHelper.cancelTag(PicassoHelper.PLAYER_THUMBNAIL_TAG); // cancel thumbnail loading

if (binding != null) {
binding.endScreen.setImageBitmap(null);
Expand Down Expand Up @@ -1209,85 +1206,42 @@ private void initThumbnail(final String url) {
Log.d(TAG, "Thumbnail - initThumbnail() called with url = ["
+ (url == null ? "null" : url) + "]");
}
if (url == null || url.isEmpty()) {
if (isNullOrEmpty(url)) {
return;
}

// scale down the notification thumbnail for performance
PicassoHelper.loadThumbnail(url)
.tag(PICASSO_PLAYER_TAG)
.transform(new Transformation() {
@Override
public Bitmap transform(final Bitmap source) {
final float notificationThumbnailWidth = Math.min(
context.getResources()
.getDimension(R.dimen.player_notification_thumbnail_width),
source.getWidth());

final Bitmap result = Bitmap.createScaledBitmap(
source,
(int) notificationThumbnailWidth,
(int) (source.getHeight()
/ (source.getWidth() / notificationThumbnailWidth)),
true);

if (result == source) {
// create a new mutable bitmap to prevent strange crashes on some
// devices (see #4638)
final Bitmap copied = Bitmap.createScaledBitmap(
source,
(int) notificationThumbnailWidth - 1,
(int) (source.getHeight() / (source.getWidth()
/ (notificationThumbnailWidth - 1))),
true);
source.recycle();
return copied;
} else {
source.recycle();
return result;
}
}

@Override
public String key() {
return PICASSO_TRANSFORMATION_KEY;
}
})
.into(new Target() {
@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" + bitmap.getHeight()
+ "], from = [" + from + "]");
}
PicassoHelper.loadScaledDownThumbnail(context, url).into(new Target() {
@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"
+ bitmap.getHeight() + "], from = [" + from + "]");
}

currentThumbnail = bitmap;
NotificationUtil.getInstance()
.createNotificationIfNeededAndUpdate(Player.this, false);
// there is a new thumbnail, so changed the end screen thumbnail, too.
updateEndScreenThumbnail();
}
currentThumbnail = bitmap;
NotificationUtil.getInstance()
.createNotificationIfNeededAndUpdate(Player.this, false);
// there is a new thumbnail, so changed the end screen thumbnail, too.
updateEndScreenThumbnail();
}

@Override
public void onBitmapFailed(final Exception e, final Drawable errorDrawable) {
Log.e(TAG, "Thumbnail - onBitmapFailed() called with: url = ["
+ url + "]", e);
currentThumbnail = null;
NotificationUtil.getInstance()
.createNotificationIfNeededAndUpdate(Player.this, false);
}
@Override
public void onBitmapFailed(final Exception e, final Drawable errorDrawable) {
Log.e(TAG, "Thumbnail - onBitmapFailed() called with: url = [" + url + "]", e);
currentThumbnail = null;
NotificationUtil.getInstance()
.createNotificationIfNeededAndUpdate(Player.this, false);
}

@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
if (DEBUG) {
Log.d(TAG, "Thumbnail - onLoadingStarted() called with: url = ["
+ url + "]");
}
}
});
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
if (DEBUG) {
Log.d(TAG, "Thumbnail - onLoadingStarted() called with: url = [" + url + "]");
}
}
});
}

/**
Expand Down
53 changes: 52 additions & 1 deletion app/src/main/java/org/schabi/newpipe/util/PicassoHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.squareup.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Transformation;

import org.schabi.newpipe.R;

Expand All @@ -20,6 +21,9 @@
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;

public final class PicassoHelper {
public static final String PLAYER_THUMBNAIL_TAG = "PICASSO_PLAYER_THUMBNAIL_TAG";
private static final String PLAYER_THUMBNAIL_TRANSFORMATION_KEY
= "PICASSO_PLAYER_THUMBNAIL_TRANSFORMATION_KEY";

private PicassoHelper() {
}
Expand Down Expand Up @@ -60,7 +64,10 @@ public static void terminate() {
public static void clearCache(final Context context) throws IOException {
picassoInstance.shutdown();
picassoCache.clear(); // clear memory cache
picassoDownloaderClient.cache().delete(); // clear disk cache
final okhttp3.Cache diskCache = picassoDownloaderClient.cache();
if (diskCache != null) {
diskCache.delete(); // clear disk cache
}
init(context);
}

Expand Down Expand Up @@ -98,6 +105,50 @@ public static RequestCreator loadPlaylistThumbnail(final String url) {
}


public static RequestCreator loadScaledDownThumbnail(final Context context, final String url) {
// scale down the notification thumbnail for performance
return PicassoHelper.loadThumbnail(url)
.tag(PLAYER_THUMBNAIL_TAG)
.transform(new Transformation() {
@Override
public Bitmap transform(final Bitmap source) {
final float notificationThumbnailWidth = Math.min(
context.getResources()
.getDimension(R.dimen.player_notification_thumbnail_width),
source.getWidth());

final Bitmap result = Bitmap.createScaledBitmap(
source,
(int) notificationThumbnailWidth,
(int) (source.getHeight()
/ (source.getWidth() / notificationThumbnailWidth)),
true);

if (result == source) {
// create a new mutable bitmap to prevent strange crashes on some
// devices (see #4638)
final Bitmap copied = Bitmap.createScaledBitmap(
source,
(int) notificationThumbnailWidth - 1,
(int) (source.getHeight() / (source.getWidth()
/ (notificationThumbnailWidth - 1))),
true);
source.recycle();
return copied;
} else {
source.recycle();
return result;
}
}

@Override
public String key() {
return PLAYER_THUMBNAIL_TRANSFORMATION_KEY;
}
});
}


private static RequestCreator loadImageDefault(final String url, final int placeholderResId) {
return picassoInstance
.load((!shouldLoadImages || isBlank(url)) ? null : url)
Expand Down

0 comments on commit 6fe3182

Please sign in to comment.