converted to Glide 4
This commit is contained in:
parent
96aa205405
commit
f4c56c8484
45 changed files with 853 additions and 945 deletions
|
@ -3,9 +3,11 @@ package code.name.monkey.retromusic.util;
|
|||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.bumptech.glide.signature.StringSignature;
|
||||
import com.bumptech.glide.signature.ObjectKey;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import code.name.monkey.retromusic.App;
|
||||
|
||||
|
||||
public class ArtistSignatureUtil {
|
||||
|
@ -19,9 +21,9 @@ public class ArtistSignatureUtil {
|
|||
mPreferences = context.getSharedPreferences(ARTIST_SIGNATURE_PREFS, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
public static ArtistSignatureUtil getInstance(@NonNull final Context context) {
|
||||
public static ArtistSignatureUtil getInstance() {
|
||||
if (sInstance == null) {
|
||||
sInstance = new ArtistSignatureUtil(context.getApplicationContext());
|
||||
sInstance = new ArtistSignatureUtil(App.Companion.getContext());
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
@ -35,7 +37,7 @@ public class ArtistSignatureUtil {
|
|||
return mPreferences.getLong(artistName, 0);
|
||||
}
|
||||
|
||||
public StringSignature getArtistSignature(String artistName) {
|
||||
return new StringSignature(String.valueOf(getArtistSignatureRaw(artistName)));
|
||||
public ObjectKey getArtistSignature(String artistName) {
|
||||
return new ObjectKey(String.valueOf(getArtistSignatureRaw(artistName)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,137 +0,0 @@
|
|||
package code.name.monkey.retromusic.util;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||
import com.bumptech.glide.request.animation.GlideAnimation;
|
||||
import com.bumptech.glide.request.target.SimpleTarget;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Locale;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import code.name.monkey.retromusic.App;
|
||||
import code.name.monkey.retromusic.model.Artist;
|
||||
|
||||
|
||||
public class CustomArtistImageUtil {
|
||||
private static final String CUSTOM_ARTIST_IMAGE_PREFS = "custom_artist_image";
|
||||
private static final String FOLDER_NAME = "/custom_artist_images/";
|
||||
|
||||
private static CustomArtistImageUtil sInstance;
|
||||
|
||||
private final SharedPreferences mPreferences;
|
||||
|
||||
private CustomArtistImageUtil(@NonNull final Context context) {
|
||||
mPreferences = context.getApplicationContext().getSharedPreferences(CUSTOM_ARTIST_IMAGE_PREFS, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
public static CustomArtistImageUtil getInstance(@NonNull final Context context) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new CustomArtistImageUtil(context.getApplicationContext());
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private static String getFileName(Artist artist) {
|
||||
String artistName = artist.getName();
|
||||
if (artistName == null)
|
||||
artistName = "";
|
||||
// replace everything that is not a letter or a number with _
|
||||
artistName = artistName.replaceAll("[^a-zA-Z0-9]", "_");
|
||||
return String.format(Locale.US, "#%d#%s.jpeg", artist.getId(), artistName);
|
||||
}
|
||||
|
||||
public static File getFile(Artist artist) {
|
||||
File dir = new File(App.Companion.getInstance().getFilesDir(), FOLDER_NAME);
|
||||
return new File(dir, getFileName(artist));
|
||||
}
|
||||
|
||||
public void setCustomArtistImage(final Artist artist, Uri uri) {
|
||||
Glide.with(App.Companion.getInstance())
|
||||
.load(uri)
|
||||
.asBitmap()
|
||||
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
||||
.skipMemoryCache(true)
|
||||
.into(new SimpleTarget<Bitmap>() {
|
||||
@Override
|
||||
public void onLoadFailed(Exception e, Drawable errorDrawable) {
|
||||
super.onLoadFailed(e, errorDrawable);
|
||||
e.printStackTrace();
|
||||
Toast.makeText(App.Companion.getInstance(), e.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
@Override
|
||||
public void onResourceReady(final Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
|
||||
new AsyncTask<Void, Void, Void>() {
|
||||
@SuppressLint("ApplySharedPref")
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
File dir = new File(App.Companion.getInstance().getFilesDir(), FOLDER_NAME);
|
||||
if (!dir.exists()) {
|
||||
if (!dir.mkdirs()) { // create the folder
|
||||
return null;
|
||||
}
|
||||
}
|
||||
File file = new File(dir, getFileName(artist));
|
||||
|
||||
boolean succesful = false;
|
||||
try {
|
||||
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
|
||||
succesful = ImageUtil.resizeBitmap(resource, 2048).compress(Bitmap.CompressFormat.JPEG, 100, os);
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
Toast.makeText(App.Companion.getInstance(), e.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
if (succesful) {
|
||||
mPreferences.edit().putBoolean(getFileName(artist), true).commit();
|
||||
ArtistSignatureUtil.getInstance(App.Companion.getInstance()).updateArtistSignature(artist.getName());
|
||||
App.Companion.getInstance().getContentResolver().notifyChange(Uri.parse("content://media"), null); // trigger media store changed to force artist image reload
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
public void resetCustomArtistImage(final Artist artist) {
|
||||
new AsyncTask<Void, Void, Void>() {
|
||||
@SuppressLint("ApplySharedPref")
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
mPreferences.edit().putBoolean(getFileName(artist), false).commit();
|
||||
ArtistSignatureUtil.getInstance(App.Companion.getInstance()).updateArtistSignature(artist.getName());
|
||||
App.Companion.getInstance().getContentResolver().notifyChange(Uri.parse("content://media"), null); // trigger media store changed to force artist image reload
|
||||
|
||||
File file = getFile(artist);
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
} else {
|
||||
file.delete();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
|
||||
// shared prefs saves us many IO operations
|
||||
public boolean hasCustomArtistImage(Artist artist) {
|
||||
return mPreferences.getBoolean(getFileName(artist), false);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
package code.name.monkey.retromusic.util
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.net.Uri
|
||||
import android.os.AsyncTask
|
||||
import android.widget.Toast
|
||||
import code.name.monkey.retromusic.App
|
||||
import code.name.monkey.retromusic.glide.GlideApp
|
||||
import code.name.monkey.retromusic.glide.RetroSimpleTarget
|
||||
import code.name.monkey.retromusic.model.Artist
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
||||
import com.bumptech.glide.request.RequestOptions
|
||||
import com.bumptech.glide.request.transition.Transition
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
|
||||
class CustomArtistImageUtil private constructor(context: Context) {
|
||||
|
||||
private val mPreferences: SharedPreferences
|
||||
|
||||
init {
|
||||
mPreferences = context.applicationContext.getSharedPreferences(CUSTOM_ARTIST_IMAGE_PREFS, Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
fun setCustomArtistImage(artist: Artist, uri: Uri) {
|
||||
GlideApp.with(App.context)
|
||||
.asBitmap()
|
||||
.load(uri)
|
||||
.apply(RequestOptions()
|
||||
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
||||
.skipMemoryCache(true)
|
||||
)
|
||||
.into(object : RetroSimpleTarget<Bitmap>() {
|
||||
|
||||
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
|
||||
object : AsyncTask<Void, Void, Void>() {
|
||||
@SuppressLint("ApplySharedPref")
|
||||
override fun doInBackground(vararg params: Void): Void? {
|
||||
val dir = File(App.context.getFilesDir(), FOLDER_NAME)
|
||||
if (!dir.exists()) {
|
||||
if (!dir.mkdirs()) { // create the folder
|
||||
return null
|
||||
}
|
||||
}
|
||||
val file = File(dir, getFileName(artist))
|
||||
|
||||
var succesful = false
|
||||
try {
|
||||
val os = BufferedOutputStream(FileOutputStream(file))
|
||||
succesful = ImageUtil.resizeBitmap(resource, 2048).compress(Bitmap.CompressFormat.JPEG, 100, os)
|
||||
os.close()
|
||||
} catch (e: IOException) {
|
||||
Toast.makeText(App.context, e.toString(), Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
if (succesful) {
|
||||
mPreferences.edit().putBoolean(getFileName(artist), true).commit()
|
||||
ArtistSignatureUtil.getInstance().updateArtistSignature(artist.name)
|
||||
App.context.getContentResolver().notifyChange(Uri.parse("content://media"), null) // trigger media store changed to force artist image reload
|
||||
}
|
||||
return null
|
||||
}
|
||||
}.execute()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun resetCustomArtistImage(artist: Artist) {
|
||||
object : AsyncTask<Void, Void, Void>() {
|
||||
@SuppressLint("ApplySharedPref")
|
||||
override fun doInBackground(vararg params: Void): Void? {
|
||||
mPreferences.edit().putBoolean(getFileName(artist), false).commit()
|
||||
ArtistSignatureUtil.getInstance().updateArtistSignature(artist.name)
|
||||
App.context.contentResolver.notifyChange(Uri.parse("content://media"), null) // trigger media store changed to force artist image reload
|
||||
|
||||
val file = getFile(artist)
|
||||
if (!file.exists()) {
|
||||
return null
|
||||
} else {
|
||||
file.delete()
|
||||
}
|
||||
return null
|
||||
}
|
||||
}.execute()
|
||||
}
|
||||
|
||||
// shared prefs saves us many IO operations
|
||||
fun hasCustomArtistImage(artist: Artist): Boolean {
|
||||
return mPreferences.getBoolean(getFileName(artist), false)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val CUSTOM_ARTIST_IMAGE_PREFS = "custom_artist_image"
|
||||
private const val FOLDER_NAME = "/custom_artist_images/"
|
||||
|
||||
private var sInstance: CustomArtistImageUtil? = null
|
||||
|
||||
fun getInstance(context: Context): CustomArtistImageUtil {
|
||||
if (sInstance == null) {
|
||||
sInstance = CustomArtistImageUtil(context.applicationContext)
|
||||
}
|
||||
return sInstance!!
|
||||
}
|
||||
|
||||
fun getFileName(artist: Artist): String {
|
||||
var artistName = artist.name
|
||||
// replace everything that is not a letter or a number with _
|
||||
artistName = artistName.replace("[^a-zA-Z0-9]".toRegex(), "_")
|
||||
return String.format(Locale.US, "#%d#%s.jpeg", artist.id, artistName)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getFile(artist: Artist): File {
|
||||
val dir = File(App.context.getFilesDir(), FOLDER_NAME)
|
||||
return File(dir, getFileName(artist))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue