2013-02-08 10:09:55 +01:00
|
|
|
/*
|
|
|
|
This file is part of Subsonic.
|
|
|
|
|
|
|
|
Subsonic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Subsonic is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2009 (C) Sindre Mehus
|
|
|
|
*/
|
2013-04-06 21:47:24 +02:00
|
|
|
package com.thejoshwa.ultrasonic.androidapp.util;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
import android.content.Context;
|
2013-06-07 04:27:45 +02:00
|
|
|
import android.content.res.Resources;
|
2013-02-08 10:09:55 +01:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.graphics.drawable.TransitionDrawable;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.util.DisplayMetrics;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2013-04-06 21:47:24 +02:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.R;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.MusicDirectory;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.MusicService;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.service.MusicServiceFactory;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronous loading of images, with caching.
|
|
|
|
* <p/>
|
|
|
|
* There should normally be only one instance of this class.
|
|
|
|
*
|
|
|
|
* @author Sindre Mehus
|
|
|
|
*/
|
|
|
|
public class ImageLoader implements Runnable {
|
|
|
|
|
|
|
|
private static final String TAG = ImageLoader.class.getSimpleName();
|
|
|
|
private static final int CONCURRENCY = 5;
|
|
|
|
|
2013-05-20 08:33:40 +02:00
|
|
|
private final LRUCache<String, Bitmap> cache = new LRUCache<String, Bitmap>(100);
|
2013-02-08 10:09:55 +01:00
|
|
|
private final BlockingQueue<Task> queue;
|
|
|
|
private final int imageSizeDefault;
|
|
|
|
private final int imageSizeLarge;
|
2013-05-20 08:33:40 +02:00
|
|
|
private Bitmap largeUnknownImage;
|
|
|
|
private Context context;
|
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
public ImageLoader(Context context) {
|
2013-05-20 08:33:40 +02:00
|
|
|
this.context = context;
|
|
|
|
queue = new LinkedBlockingQueue<Task>(500);
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-06-07 04:27:45 +02:00
|
|
|
Resources resources = context.getResources();
|
|
|
|
Drawable drawable = resources.getDrawable(R.drawable.unknown_album);
|
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
// Determine the density-dependent image sizes.
|
2013-06-07 04:27:45 +02:00
|
|
|
imageSizeDefault = drawable.getIntrinsicHeight();
|
2013-02-08 10:09:55 +01:00
|
|
|
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
|
2013-06-07 04:27:45 +02:00
|
|
|
imageSizeLarge = Math.round(Math.min(metrics.widthPixels, metrics.heightPixels));
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < CONCURRENCY; i++) {
|
|
|
|
new Thread(this, "ImageLoader").start();
|
|
|
|
}
|
|
|
|
|
|
|
|
createLargeUnknownImage(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createLargeUnknownImage(Context context) {
|
|
|
|
BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.unknown_album_large);
|
|
|
|
Log.i(TAG, "createLargeUnknownImage");
|
2013-06-07 04:27:45 +02:00
|
|
|
|
|
|
|
if (drawable != null) {
|
|
|
|
largeUnknownImage = Util.scaleBitmap(drawable.getBitmap(), imageSizeLarge);
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|
|
|
|
|
2013-06-07 04:27:45 +02:00
|
|
|
public void loadImage(View view, MusicDirectory.Entry entry, boolean large, int size, boolean crossFade, boolean highQuality) {
|
2013-04-02 03:16:45 +02:00
|
|
|
if (entry == null) {
|
|
|
|
setUnknownImage(view, large);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String coverArt = entry.getCoverArt();
|
|
|
|
|
|
|
|
if (coverArt == null) {
|
2013-02-08 10:09:55 +01:00
|
|
|
setUnknownImage(view, large);
|
|
|
|
return;
|
|
|
|
}
|
2013-04-02 03:16:45 +02:00
|
|
|
|
2013-05-20 08:33:40 +02:00
|
|
|
if (size <= 0) {
|
|
|
|
size = large ? imageSizeLarge : imageSizeDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap bitmap = cache.get(getKey(coverArt, size));
|
2013-04-02 03:16:45 +02:00
|
|
|
|
2013-05-20 08:33:40 +02:00
|
|
|
if (bitmap != null) {
|
|
|
|
setImageBitmap(view, bitmap, false);
|
2013-02-08 10:09:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!large) {
|
|
|
|
setUnknownImage(view, large);
|
|
|
|
}
|
2013-04-02 03:16:45 +02:00
|
|
|
|
2013-06-07 04:27:45 +02:00
|
|
|
queue.offer(new Task(view, entry, size, large, crossFade, highQuality));
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private String getKey(String coverArtId, int size) {
|
|
|
|
return coverArtId + size;
|
|
|
|
}
|
|
|
|
|
2013-06-07 04:27:45 +02:00
|
|
|
private void setImageBitmap(View view, Bitmap bitmap, boolean crossFade) {
|
2013-05-20 08:33:40 +02:00
|
|
|
if (view instanceof ImageView) {
|
2013-02-08 10:09:55 +01:00
|
|
|
ImageView imageView = (ImageView) view;
|
|
|
|
|
2013-06-07 04:27:45 +02:00
|
|
|
if (crossFade) {
|
2013-02-08 10:09:55 +01:00
|
|
|
Drawable existingDrawable = imageView.getDrawable();
|
2013-05-20 08:33:40 +02:00
|
|
|
Drawable newDrawable = Util.createDrawableFromBitmap(this.context, bitmap);
|
|
|
|
|
2013-02-08 10:09:55 +01:00
|
|
|
if (existingDrawable == null) {
|
2013-05-20 08:33:40 +02:00
|
|
|
Bitmap emptyImage = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
2013-02-08 10:09:55 +01:00
|
|
|
existingDrawable = new BitmapDrawable(emptyImage);
|
|
|
|
}
|
|
|
|
|
2013-05-20 08:33:40 +02:00
|
|
|
Drawable[] layers = new Drawable[]{existingDrawable, newDrawable};
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
|
|
|
|
imageView.setImageDrawable(transitionDrawable);
|
|
|
|
transitionDrawable.startTransition(250);
|
|
|
|
} else {
|
2013-05-20 08:33:40 +02:00
|
|
|
imageView.setImageBitmap(bitmap);
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setUnknownImage(View view, boolean large) {
|
|
|
|
if (large) {
|
2013-05-20 08:33:40 +02:00
|
|
|
setImageBitmap(view, largeUnknownImage, false);
|
2013-02-08 10:09:55 +01:00
|
|
|
} else {
|
|
|
|
if (view instanceof TextView) {
|
|
|
|
((TextView) view).setCompoundDrawablesWithIntrinsicBounds(R.drawable.unknown_album, 0, 0, 0);
|
|
|
|
} else if (view instanceof ImageView) {
|
|
|
|
((ImageView) view).setImageResource(R.drawable.unknown_album);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clear() {
|
|
|
|
queue.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
Task task = queue.take();
|
|
|
|
task.execute();
|
|
|
|
} catch (Throwable x) {
|
|
|
|
Log.e(TAG, "Unexpected exception in ImageLoader.", x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class Task {
|
|
|
|
private final View view;
|
|
|
|
private final MusicDirectory.Entry entry;
|
|
|
|
private final Handler handler;
|
|
|
|
private final int size;
|
|
|
|
private final boolean saveToFile;
|
|
|
|
private final boolean crossfade;
|
2013-05-20 08:33:40 +02:00
|
|
|
private final boolean highQuality;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-06-07 04:27:45 +02:00
|
|
|
public Task(View view, MusicDirectory.Entry entry, int size, boolean saveToFile, boolean crossFade, boolean highQuality) {
|
2013-02-08 10:09:55 +01:00
|
|
|
this.view = view;
|
|
|
|
this.entry = entry;
|
|
|
|
this.size = size;
|
|
|
|
this.saveToFile = saveToFile;
|
2013-06-07 04:27:45 +02:00
|
|
|
this.crossfade = crossFade;
|
2013-05-20 08:33:40 +02:00
|
|
|
this.highQuality = highQuality;
|
2013-02-08 10:09:55 +01:00
|
|
|
handler = new Handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void execute() {
|
|
|
|
try {
|
|
|
|
MusicService musicService = MusicServiceFactory.getMusicService(view.getContext());
|
2013-05-20 08:33:40 +02:00
|
|
|
final Bitmap bitmap = musicService.getCoverArt(view.getContext(), entry, size, saveToFile, highQuality, null);
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-05-20 08:33:40 +02:00
|
|
|
cache.put(getKey(entry.getCoverArt(), size), bitmap);
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-05-20 08:33:40 +02:00
|
|
|
setImageBitmap(view, bitmap, crossfade);
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (Throwable x) {
|
|
|
|
Log.e(TAG, "Failed to download album art.", x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|