2020-11-26 16:05:58 +01:00
|
|
|
package com.cappielloantonio.play.glide;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2023-03-14 10:48:13 +01:00
|
|
|
import android.graphics.drawable.ColorDrawable;
|
2020-11-26 16:05:58 +01:00
|
|
|
import android.graphics.drawable.Drawable;
|
2021-08-31 10:12:11 +02:00
|
|
|
import android.util.Log;
|
2020-11-26 16:05:58 +01:00
|
|
|
|
|
|
|
|
import com.bumptech.glide.Glide;
|
|
|
|
|
import com.bumptech.glide.RequestBuilder;
|
|
|
|
|
import com.bumptech.glide.RequestManager;
|
|
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
2023-03-16 19:47:39 +01:00
|
|
|
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
|
|
|
|
|
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
|
|
|
|
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
|
2020-11-26 16:05:58 +01:00
|
|
|
import com.bumptech.glide.request.RequestOptions;
|
|
|
|
|
import com.bumptech.glide.signature.ObjectKey;
|
|
|
|
|
import com.cappielloantonio.play.App;
|
2023-03-06 21:59:10 +01:00
|
|
|
import com.cappielloantonio.play.util.Preferences;
|
2023-03-14 10:48:13 +01:00
|
|
|
import com.google.android.material.elevation.SurfaceColors;
|
2020-11-26 16:05:58 +01:00
|
|
|
|
2021-07-27 12:10:28 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
2020-11-26 16:05:58 +01:00
|
|
|
public class CustomGlideRequest {
|
2021-07-27 17:23:14 +02:00
|
|
|
private static final String TAG = "CustomGlideRequest";
|
|
|
|
|
|
2023-03-16 19:47:39 +01:00
|
|
|
public static final int CORNER_RADIUS = Preferences.isCornerRoundingEnabled() ? Preferences.getRoundedCornerSize() : 1;
|
|
|
|
|
//public static final int CORNER_RADIUS = 12;
|
2021-08-09 10:48:52 +02:00
|
|
|
|
2020-11-26 16:05:58 +01:00
|
|
|
public static final DiskCacheStrategy DEFAULT_DISK_CACHE_STRATEGY = DiskCacheStrategy.ALL;
|
|
|
|
|
|
2023-03-14 10:48:13 +01:00
|
|
|
public static RequestOptions createRequestOptions(Context context, String item) {
|
2021-08-09 10:48:52 +02:00
|
|
|
return new RequestOptions()
|
2023-03-14 10:48:13 +01:00
|
|
|
.placeholder(new ColorDrawable(SurfaceColors.SURFACE_5.getColor(context)))
|
|
|
|
|
.fallback(new ColorDrawable(SurfaceColors.SURFACE_5.getColor(context)))
|
|
|
|
|
.error(new ColorDrawable(SurfaceColors.SURFACE_5.getColor(context)))
|
2020-11-26 16:05:58 +01:00
|
|
|
.diskCacheStrategy(DEFAULT_DISK_CACHE_STRATEGY)
|
|
|
|
|
.signature(new ObjectKey(item != null ? item : 0))
|
2023-03-16 19:47:39 +01:00
|
|
|
.transform(new CenterCrop(), new RoundedCorners(CustomGlideRequest.CORNER_RADIUS));
|
2020-11-26 16:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-31 09:58:14 +02:00
|
|
|
public static String createUrl(String item, int size) {
|
2023-03-10 15:21:02 +01:00
|
|
|
Map<String, String> params = App.getSubsonicClientInstance(false).getParams();
|
2020-11-27 09:06:50 +01:00
|
|
|
|
2023-03-11 19:05:25 +01:00
|
|
|
StringBuilder uri = new StringBuilder();
|
2021-08-31 09:58:14 +02:00
|
|
|
|
2023-03-11 19:05:25 +01:00
|
|
|
uri.append(App.getSubsonicClientInstance(false).getUrl());
|
|
|
|
|
uri.append("getCoverArt");
|
2021-08-31 10:12:11 +02:00
|
|
|
|
2023-03-11 19:05:25 +01:00
|
|
|
if (params.containsKey("u") && params.get("u") != null)
|
|
|
|
|
uri.append("?u=").append(params.get("u"));
|
|
|
|
|
if (params.containsKey("p") && params.get("p") != null)
|
|
|
|
|
uri.append("&p=").append(params.get("p"));
|
|
|
|
|
if (params.containsKey("s") && params.get("s") != null)
|
|
|
|
|
uri.append("&s=").append(params.get("s"));
|
|
|
|
|
if (params.containsKey("t") && params.get("t") != null)
|
|
|
|
|
uri.append("&t=").append(params.get("t"));
|
|
|
|
|
if (params.containsKey("v") && params.get("v") != null)
|
|
|
|
|
uri.append("&v=").append(params.get("v"));
|
|
|
|
|
if (params.containsKey("c") && params.get("c") != null)
|
|
|
|
|
uri.append("&c=").append(params.get("c"));
|
|
|
|
|
if (size != -1)
|
|
|
|
|
uri.append("&size=").append(size);
|
2022-01-11 16:25:11 +01:00
|
|
|
|
2023-03-11 19:05:25 +01:00
|
|
|
uri.append("&id=").append(item);
|
2021-08-31 09:58:14 +02:00
|
|
|
|
2023-03-11 19:05:25 +01:00
|
|
|
Log.d(TAG, "createUrl() " + uri);
|
|
|
|
|
|
|
|
|
|
return uri.toString();
|
2020-11-26 16:05:58 +01:00
|
|
|
}
|
2021-04-27 11:01:02 +02:00
|
|
|
|
|
|
|
|
public static class Builder {
|
|
|
|
|
private final RequestManager requestManager;
|
2023-03-14 11:23:35 +01:00
|
|
|
private Object item;
|
2021-04-27 11:01:02 +02:00
|
|
|
|
2023-03-11 19:05:25 +01:00
|
|
|
private Builder(Context context, String item) {
|
2021-04-27 11:01:02 +02:00
|
|
|
this.requestManager = Glide.with(context);
|
2021-08-11 13:05:10 +02:00
|
|
|
|
2023-03-14 11:23:35 +01:00
|
|
|
if (item != null && !Preferences.isDataSavingMode()) {
|
2023-03-06 21:59:10 +01:00
|
|
|
this.item = createUrl(item, Preferences.getImageSize());
|
2021-08-11 13:05:10 +02:00
|
|
|
}
|
2021-04-27 11:01:02 +02:00
|
|
|
|
2023-03-14 10:48:13 +01:00
|
|
|
requestManager.applyDefaultRequestOptions(createRequestOptions(context, item));
|
2021-04-27 11:01:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-11 19:05:25 +01:00
|
|
|
public static Builder from(Context context, String item) {
|
|
|
|
|
return new Builder(context, item);
|
2021-04-27 11:01:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RequestBuilder<Drawable> build() {
|
2023-03-16 19:47:39 +01:00
|
|
|
return requestManager
|
|
|
|
|
.load(item)
|
|
|
|
|
.transition(DrawableTransitionOptions.withCrossFade());
|
2021-04-27 11:01:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-11-26 16:05:58 +01:00
|
|
|
}
|