Add custom artist images
This commit is contained in:
parent
9e9baf9723
commit
9755e877f9
11 changed files with 350 additions and 105 deletions
|
|
@ -0,0 +1,132 @@
|
|||
package com.kabouzeid.gramophone.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.support.annotation.NonNull;
|
||||
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 com.kabouzeid.gramophone.App;
|
||||
import com.kabouzeid.gramophone.model.Artist;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void setCustomArtistImage(final Artist artist, Uri uri) {
|
||||
Glide.with(App.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.getInstance(), e.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@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.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.getInstance(), e.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
if (succesful) {
|
||||
mPreferences.edit().putBoolean(getFileName(artist), true).commit();
|
||||
ArtistSignatureUtil.getInstance(App.getInstance()).updateArtistSignature(artist.getName());
|
||||
App.getInstance().getContentResolver().notifyChange(Uri.parse("content://media"), null); // trigger media store changed to force artist image reload
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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.getInstance()).updateArtistSignature(artist.getName());
|
||||
App.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);
|
||||
}
|
||||
|
||||
private static String getFileName(Artist artist) {
|
||||
return String.format(Locale.US, "#%d#%s.jpeg", artist.getId(), artist.getName());
|
||||
}
|
||||
|
||||
public static File getFile(Artist artist) {
|
||||
File dir = new File(App.getInstance().getFilesDir(), FOLDER_NAME);
|
||||
return new File(dir, getFileName(artist));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,8 @@
|
|||
package com.kabouzeid.gramophone.util;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
|
|
@ -29,4 +32,30 @@ public class ImageUtil {
|
|||
|
||||
return inSampleSize;
|
||||
}
|
||||
|
||||
public static Bitmap resizeBitmap(@NonNull Bitmap src, int maxForSmallerSize) {
|
||||
int width = src.getWidth();
|
||||
int height = src.getHeight();
|
||||
|
||||
final int dstWidth;
|
||||
final int dstHeight;
|
||||
|
||||
if (width < height) {
|
||||
if (maxForSmallerSize >= width) {
|
||||
return src;
|
||||
}
|
||||
float ratio = (float) height / width;
|
||||
dstWidth = maxForSmallerSize;
|
||||
dstHeight = Math.round(maxForSmallerSize * ratio);
|
||||
} else {
|
||||
if (maxForSmallerSize >= height) {
|
||||
return src;
|
||||
}
|
||||
float ratio = (float) width / height;
|
||||
dstWidth = Math.round(maxForSmallerSize * ratio);
|
||||
dstHeight = maxForSmallerSize;
|
||||
}
|
||||
|
||||
return Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue