Add documentation and fix SonarCloud issue

This commit is contained in:
Stypox 2023-05-02 21:06:12 +02:00
parent 8d463b9577
commit bf908f0b7d
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
5 changed files with 43 additions and 21 deletions

View File

@ -70,7 +70,9 @@ public class PlaylistRemoteEntity implements PlaylistLocalItem {
@Ignore @Ignore
public PlaylistRemoteEntity(final PlaylistInfo info) { public PlaylistRemoteEntity(final PlaylistInfo info) {
this(info.getServiceId(), info.getName(), info.getUrl(), this(info.getServiceId(), info.getName(), info.getUrl(),
ImageStrategy.choosePreferredImage(info.getThumbnails()), // use uploader avatar when no thumbnail is available
ImageStrategy.choosePreferredImage(info.getThumbnails().isEmpty()
? info.getUploaderAvatars() : info.getThumbnails()),
info.getUploaderName(), info.getStreamCount()); info.getUploaderName(), info.getStreamCount());
} }
@ -84,6 +86,8 @@ public class PlaylistRemoteEntity implements PlaylistLocalItem {
&& getStreamCount() == info.getStreamCount() && getStreamCount() == info.getStreamCount()
&& TextUtils.equals(getName(), info.getName()) && TextUtils.equals(getName(), info.getName())
&& TextUtils.equals(getUrl(), info.getUrl()) && TextUtils.equals(getUrl(), info.getUrl())
// we want to update the local playlist data even when either the remote thumbnail
// URL changes, or the preferred image quality setting is changed by the user
&& TextUtils.equals(getThumbnailUrl(), && TextUtils.equals(getThumbnailUrl(),
ImageStrategy.choosePreferredImage(info.getThumbnails())) ImageStrategy.choosePreferredImage(info.getThumbnails()))
&& TextUtils.equals(getUploader(), info.getUploaderName()); && TextUtils.equals(getUploader(), info.getUploaderName());

View File

@ -224,6 +224,7 @@ public abstract class BaseDescriptionFragment extends BaseFragment {
case LOW: case LOW:
urls.append(getString(R.string.image_quality_low)); urls.append(getString(R.string.image_quality_low));
break; break;
default: // unreachable, Image.ResolutionLevel.UNKNOWN is already filtered out
case MEDIUM: case MEDIUM:
urls.append(getString(R.string.image_quality_medium)); urls.append(getString(R.string.image_quality_medium));
break; break;

View File

@ -269,8 +269,8 @@ public final class ShareUtils {
* *
* <p> * <p>
* For Android 10+ users, a content preview is shown, which includes the title of the shared * For Android 10+ users, a content preview is shown, which includes the title of the shared
* content and an image preview the content, if its URL is not null or empty and its * content and an image preview the content, if the preferred image chosen by {@link
* corresponding image is in the image cache. * ImageStrategy#choosePreferredImage(List)} is in the image cache.
* </p> * </p>
* *
* @param context the context to use * @param context the context to use

View File

@ -38,19 +38,35 @@ public final class ImageStrategy {
// images whose size is completely unknown will be in their own subgroups, so // images whose size is completely unknown will be in their own subgroups, so
// any one of them will do, hence returning the same value for all of them // any one of them will do, hence returning the same value for all of them
return 0; return 0;
} else { } else {
return image.getWidth() * image.getWidth() / widthOverHeight; return image.getWidth() * image.getWidth() / widthOverHeight;
} }
} else if (image.getWidth() == WIDTH_UNKNOWN) { } else if (image.getWidth() == WIDTH_UNKNOWN) {
return image.getHeight() * image.getHeight() * widthOverHeight; return image.getHeight() * image.getHeight() * widthOverHeight;
} else { } else {
return image.getHeight() * image.getWidth(); return image.getHeight() * image.getWidth();
} }
} }
/**
* Chooses an image amongst the provided list based on the user preference previously set with
* {@link #setPreferredImageQuality(PreferredImageQuality)}. {@code null} will be returned in
* case the list is empty or the user preference is to not show images.
* <br>
* These properties will be preferred, from most to least important:
* <ol>
* <li>The image's {@link Image#getEstimatedResolutionLevel()} is not unknown and is close
* to {@link #preferredImageQuality}</li>
* <li>At least one of the image's width or height are known</li>
* <li>The highest resolution image is finally chosen if the user's preference is {@link
* PreferredImageQuality#HIGH}, otherwise the chosen image is the one that has the closest
* height to {@link #BEST_LOW_H} or {@link #BEST_MEDIUM_H}</li>
* </ol>
*
* @param images the images from which to choose
* @return the chosen preferred image, or {@link null} if the list is empty or the user disabled
* images
*/
@Nullable @Nullable
public static String choosePreferredImage(@NonNull final List<Image> images) { public static String choosePreferredImage(@NonNull final List<Image> images) {
if (preferredImageQuality == PreferredImageQuality.NONE) { if (preferredImageQuality == PreferredImageQuality.NONE) {

View File

@ -9,6 +9,7 @@ import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.graphics.BitmapCompat; import androidx.core.graphics.BitmapCompat;
@ -49,7 +50,7 @@ public final class PicassoHelper {
picassoCache = new LruCache(10 * 1024 * 1024); picassoCache = new LruCache(10 * 1024 * 1024);
picassoDownloaderClient = new OkHttpClient.Builder() picassoDownloaderClient = new OkHttpClient.Builder()
.cache(new okhttp3.Cache(new File(context.getExternalCacheDir(), "picasso"), .cache(new okhttp3.Cache(new File(context.getExternalCacheDir(), "picasso"),
50 * 1024 * 1024)) 50L * 1024L * 1024L))
// this should already be the default timeout in OkHttp3, but just to be sure... // this should already be the default timeout in OkHttp3, but just to be sure...
.callTimeout(15, TimeUnit.SECONDS) .callTimeout(15, TimeUnit.SECONDS)
.build(); .build();
@ -90,50 +91,50 @@ public final class PicassoHelper {
} }
public static RequestCreator loadAvatar(final List<Image> images) { public static RequestCreator loadAvatar(@NonNull final List<Image> images) {
return loadImageDefault(images, R.drawable.placeholder_person); return loadImageDefault(images, R.drawable.placeholder_person);
} }
public static RequestCreator loadAvatar(final String url) { public static RequestCreator loadAvatar(@Nullable final String url) {
return loadImageDefault(url, R.drawable.placeholder_person); return loadImageDefault(url, R.drawable.placeholder_person);
} }
public static RequestCreator loadThumbnail(final List<Image> images) { public static RequestCreator loadThumbnail(@NonNull final List<Image> images) {
return loadImageDefault(images, R.drawable.placeholder_thumbnail_video); return loadImageDefault(images, R.drawable.placeholder_thumbnail_video);
} }
public static RequestCreator loadThumbnail(final String url) { public static RequestCreator loadThumbnail(@Nullable final String url) {
return loadImageDefault(url, R.drawable.placeholder_thumbnail_video); return loadImageDefault(url, R.drawable.placeholder_thumbnail_video);
} }
public static RequestCreator loadDetailsThumbnail(final List<Image> images) { public static RequestCreator loadDetailsThumbnail(@NonNull final List<Image> images) {
return loadImageDefault(choosePreferredImage(images), return loadImageDefault(choosePreferredImage(images),
R.drawable.placeholder_thumbnail_video, false); R.drawable.placeholder_thumbnail_video, false);
} }
public static RequestCreator loadBanner(final List<Image> images) { public static RequestCreator loadBanner(@NonNull final List<Image> images) {
return loadImageDefault(images, R.drawable.placeholder_channel_banner); return loadImageDefault(images, R.drawable.placeholder_channel_banner);
} }
public static RequestCreator loadPlaylistThumbnail(final List<Image> images) { public static RequestCreator loadPlaylistThumbnail(@NonNull final List<Image> images) {
return loadImageDefault(images, R.drawable.placeholder_thumbnail_playlist); return loadImageDefault(images, R.drawable.placeholder_thumbnail_playlist);
} }
public static RequestCreator loadPlaylistThumbnail(final String url) { public static RequestCreator loadPlaylistThumbnail(@Nullable final String url) {
return loadImageDefault(url, R.drawable.placeholder_thumbnail_playlist); return loadImageDefault(url, R.drawable.placeholder_thumbnail_playlist);
} }
public static RequestCreator loadSeekbarThumbnailPreview(final String url) { public static RequestCreator loadSeekbarThumbnailPreview(@Nullable final String url) {
return picassoInstance.load(url); return picassoInstance.load(url);
} }
public static RequestCreator loadNotificationIcon(final String url) { public static RequestCreator loadNotificationIcon(@Nullable final String url) {
return loadImageDefault(url, R.drawable.ic_newpipe_triangle_white); return loadImageDefault(url, R.drawable.ic_newpipe_triangle_white);
} }
public static RequestCreator loadScaledDownThumbnail(final Context context, public static RequestCreator loadScaledDownThumbnail(final Context context,
final List<Image> images) { @NonNull final List<Image> images) {
// scale down the notification thumbnail for performance // scale down the notification thumbnail for performance
return PicassoHelper.loadThumbnail(images) return PicassoHelper.loadThumbnail(images)
.transform(new Transformation() { .transform(new Transformation() {
@ -182,18 +183,18 @@ public final class PicassoHelper {
} }
@Nullable @Nullable
public static Bitmap getImageFromCacheIfPresent(final String imageUrl) { public static Bitmap getImageFromCacheIfPresent(@NonNull final String imageUrl) {
// URLs in the internal cache finish with \n so we need to add \n to image URLs // URLs in the internal cache finish with \n so we need to add \n to image URLs
return picassoCache.get(imageUrl + "\n"); return picassoCache.get(imageUrl + "\n");
} }
private static RequestCreator loadImageDefault(final List<Image> images, private static RequestCreator loadImageDefault(@NonNull final List<Image> images,
final int placeholderResId) { final int placeholderResId) {
return loadImageDefault(choosePreferredImage(images), placeholderResId); return loadImageDefault(choosePreferredImage(images), placeholderResId);
} }
private static RequestCreator loadImageDefault(final String url, private static RequestCreator loadImageDefault(@Nullable final String url,
final int placeholderResId) { final int placeholderResId) {
return loadImageDefault(url, placeholderResId, true); return loadImageDefault(url, placeholderResId, true);
} }