Skip to content

Commit

Permalink
Keep strong references to Picasso notification icon loading targets
Browse files Browse the repository at this point in the history
Before the Target would sometimes be garbage collected before being called with the loaded channel icon, since Picasso holds weak references to targets. This meant that sometimes a new streams notification  would not be shown, because the lambda that should have shown it had already been garbage collected.
  • Loading branch information
Stypox committed Jul 22, 2022
1 parent cacce6d commit 1ec9e4a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
import com.squareup.picasso.Picasso
import com.squareup.picasso.Target
import org.schabi.newpipe.R
import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.local.feed.service.FeedUpdateInfo
Expand All @@ -27,6 +31,8 @@ class NotificationHelper(val context: Context) {
Context.NOTIFICATION_SERVICE
) as NotificationManager

private val iconLoadingTargets = ArrayList<Target>()

/**
* Show a notification about new streams from a single channel.
* Opening the notification will open the corresponding channel page.
Expand Down Expand Up @@ -77,10 +83,29 @@ class NotificationHelper(val context: Context) {
)
)

PicassoHelper.loadNotificationIcon(data.avatarUrl) { bitmap ->
bitmap?.let { builder.setLargeIcon(it) } // set only if != null
manager.notify(data.pseudoId, builder.build())
// a Target is like a listener for image loading events
val target = object : Target {
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
builder.setLargeIcon(bitmap) // set only if there is actually one
manager.notify(data.pseudoId, builder.build())
iconLoadingTargets.remove(this) // allow it to be garbage-collected
}

override fun onBitmapFailed(e: Exception, errorDrawable: Drawable) {
manager.notify(data.pseudoId, builder.build())
iconLoadingTargets.remove(this) // allow it to be garbage-collected
}

override fun onPrepareLoad(placeHolderDrawable: Drawable) {
// Nothing to do
}
}

// add the target to the list to hold a strong reference and prevent it from being garbage
// collected, since Picasso only holds weak references to targets
iconLoadingTargets.add(target)

PicassoHelper.loadNotificationIcon(data.avatarUrl).into(target)
}

companion object {
Expand Down
28 changes: 4 additions & 24 deletions app/src/main/java/org/schabi/newpipe/util/PicassoHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import androidx.annotation.Nullable;

Expand All @@ -14,15 +13,13 @@
import com.squareup.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Target;
import com.squareup.picasso.Transformation;

import org.schabi.newpipe.R;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import okhttp3.OkHttpClient;

Expand Down Expand Up @@ -120,6 +117,10 @@ public static RequestCreator loadSeekbarThumbnailPreview(final String url) {
return picassoInstance.load(url);
}

public static RequestCreator loadNotificationIcon(final String url) {
return loadImageDefault(url, R.drawable.ic_newpipe_triangle_white);
}


public static RequestCreator loadScaledDownThumbnail(final Context context, final String url) {
// scale down the notification thumbnail for performance
Expand Down Expand Up @@ -170,27 +171,6 @@ public static Bitmap getImageFromCacheIfPresent(final String imageUrl) {
return picassoCache.get(imageUrl + "\n");
}

public static void loadNotificationIcon(final String url,
final Consumer<Bitmap> bitmapConsumer) {
loadImageDefault(url, R.drawable.ic_newpipe_triangle_white)
.into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
bitmapConsumer.accept(bitmap);
}

@Override
public void onBitmapFailed(final Exception e, final Drawable errorDrawable) {
bitmapConsumer.accept(null);
}

@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
// Nothing to do
}
});
}


private static RequestCreator loadImageDefault(final String url, final int placeholderResId) {
return loadImageDefault(url, placeholderResId, true);
Expand Down

0 comments on commit 1ec9e4a

Please sign in to comment.