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
|
|
|
|
*/
|
2015-07-26 18:15:07 +02:00
|
|
|
package org.moire.ultrasonic.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;
|
2018-02-10 20:11:17 +01:00
|
|
|
import android.text.TextUtils;
|
2013-02-08 10:09:55 +01:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2015-07-26 18:15:07 +02:00
|
|
|
import org.moire.ultrasonic.R;
|
|
|
|
import org.moire.ultrasonic.domain.MusicDirectory;
|
|
|
|
import org.moire.ultrasonic.service.MusicService;
|
|
|
|
import org.moire.ultrasonic.service.MusicServiceFactory;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2014-01-22 09:58:38 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2014-01-27 00:47:13 +01:00
|
|
|
import java.util.Collections;
|
2013-02-08 10:09:55 +01:00
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
2014-01-25 10:41:45 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2013-02-08 10:09:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronous loading of images, with caching.
|
|
|
|
* <p/>
|
|
|
|
* There should normally be only one instance of this class.
|
|
|
|
*
|
|
|
|
* @author Sindre Mehus
|
|
|
|
*/
|
2013-12-04 07:36:02 +01:00
|
|
|
public class ImageLoader implements Runnable
|
|
|
|
{
|
|
|
|
private static final String TAG = ImageLoader.class.getSimpleName();
|
2013-02-08 10:09:55 +01:00
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
private final LRUCache<String, Bitmap> cache = new LRUCache<String, Bitmap>(150);
|
|
|
|
private final BlockingQueue<Task> queue;
|
|
|
|
private int imageSizeDefault;
|
|
|
|
private final int imageSizeLarge;
|
|
|
|
private Bitmap largeUnknownImage;
|
2014-01-29 08:47:56 +01:00
|
|
|
private Bitmap unknownAvatarImage;
|
2013-05-20 08:33:40 +02:00
|
|
|
private Context context;
|
2014-01-25 10:41:45 +01:00
|
|
|
private Collection<Thread> threads;
|
|
|
|
private AtomicBoolean running = new AtomicBoolean();
|
2014-01-22 09:58:38 +01:00
|
|
|
private int concurrency;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2014-01-22 09:58:38 +01:00
|
|
|
public ImageLoader(Context context, int concurrency)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
|
|
|
this.context = context;
|
2014-01-22 09:58:38 +01:00
|
|
|
this.concurrency = concurrency;
|
2013-12-04 07:36:02 +01:00
|
|
|
queue = new LinkedBlockingQueue<Task>(1000);
|
|
|
|
|
|
|
|
Resources resources = context.getResources();
|
|
|
|
Drawable drawable = resources.getDrawable(R.drawable.unknown_album);
|
|
|
|
|
|
|
|
// Determine the density-dependent image sizes.
|
|
|
|
if (drawable != null)
|
|
|
|
{
|
|
|
|
imageSizeDefault = drawable.getIntrinsicHeight();
|
|
|
|
}
|
|
|
|
|
2014-01-27 07:45:21 +01:00
|
|
|
imageSizeLarge = Util.getMaxDisplayMetric(context);
|
2014-01-22 09:58:38 +01:00
|
|
|
createLargeUnknownImage(context);
|
2014-01-29 08:47:56 +01:00
|
|
|
createUnknownAvatarImage(context);
|
2014-01-22 09:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized boolean isRunning()
|
|
|
|
{
|
2014-01-25 10:41:45 +01:00
|
|
|
return running.get() && !threads.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setConcurrency(int concurrency)
|
|
|
|
{
|
|
|
|
this.concurrency = concurrency;
|
2014-01-22 09:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void startImageLoader()
|
|
|
|
{
|
2014-01-25 10:41:45 +01:00
|
|
|
running.set(true);
|
|
|
|
|
2014-01-27 00:47:13 +01:00
|
|
|
threads = Collections.synchronizedCollection(new ArrayList<Thread>(this.concurrency));
|
2014-01-22 09:58:38 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < this.concurrency; i++)
|
|
|
|
{
|
2014-02-10 07:42:52 +01:00
|
|
|
Thread thread = new Thread(this, String.format("ImageLoader_%d", i));
|
2014-01-22 09:58:38 +01:00
|
|
|
threads.add(thread);
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void stopImageLoader()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
|
|
|
for (Thread thread : threads)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
2014-01-22 09:58:38 +01:00
|
|
|
thread.interrupt();
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
2014-01-25 10:41:45 +01:00
|
|
|
running.set(false);
|
|
|
|
threads.clear();
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void createLargeUnknownImage(Context context)
|
|
|
|
{
|
|
|
|
BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.unknown_album_large);
|
|
|
|
Log.i(TAG, "createLargeUnknownImage");
|
|
|
|
|
|
|
|
if (drawable != null)
|
|
|
|
{
|
|
|
|
largeUnknownImage = Util.scaleBitmap(drawable.getBitmap(), imageSizeLarge);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 08:47:56 +01:00
|
|
|
private void createUnknownAvatarImage(Context context)
|
|
|
|
{
|
|
|
|
Resources res = context.getResources();
|
|
|
|
Drawable contact = res.getDrawable(R.drawable.ic_contact_picture);
|
|
|
|
unknownAvatarImage = Util.createBitmapFromDrawable(contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadAvatarImage(View view, String username, boolean large, int size, boolean crossFade, boolean highQuality)
|
|
|
|
{
|
|
|
|
view.invalidate();
|
|
|
|
|
|
|
|
if (username == null)
|
|
|
|
{
|
|
|
|
setUnknownAvatarImage(view);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size <= 0)
|
|
|
|
{
|
|
|
|
size = large ? imageSizeLarge : imageSizeDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap bitmap = cache.get(getKey(username, size));
|
|
|
|
|
|
|
|
if (bitmap != null)
|
|
|
|
{
|
|
|
|
setAvatarImageBitmap(view, username, bitmap, crossFade);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setUnknownAvatarImage(view);
|
|
|
|
|
|
|
|
queue.offer(new Task(view, username, size, large, crossFade, highQuality));
|
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
public void loadImage(View view, MusicDirectory.Entry entry, boolean large, int size, boolean crossFade, boolean highQuality)
|
|
|
|
{
|
2014-01-27 04:43:34 +01:00
|
|
|
view.invalidate();
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
if (entry == null)
|
|
|
|
{
|
|
|
|
setUnknownImage(view, large);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String coverArt = entry.getCoverArt();
|
|
|
|
|
2018-02-10 20:11:17 +01:00
|
|
|
if (TextUtils.isEmpty(coverArt)) {
|
2013-12-04 07:36:02 +01:00
|
|
|
setUnknownImage(view, large);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size <= 0)
|
|
|
|
{
|
|
|
|
size = large ? imageSizeLarge : imageSizeDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap bitmap = cache.get(getKey(coverArt, size));
|
|
|
|
|
|
|
|
if (bitmap != null)
|
|
|
|
{
|
2014-01-24 05:18:33 +01:00
|
|
|
setImageBitmap(view, entry, bitmap, crossFade);
|
2013-12-04 07:36:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setUnknownImage(view, large);
|
|
|
|
|
|
|
|
queue.offer(new Task(view, entry, size, large, crossFade, highQuality));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getKey(String coverArtId, int size)
|
|
|
|
{
|
2014-01-22 09:58:38 +01:00
|
|
|
return String.format("%s:%d", coverArtId, size);
|
2014-01-21 07:16:24 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 08:47:56 +01:00
|
|
|
public Bitmap getImageBitmap(String username, int size)
|
|
|
|
{
|
|
|
|
Bitmap bitmap = cache.get(getKey(username, size));
|
|
|
|
|
|
|
|
if (bitmap != null && !bitmap.isRecycled())
|
|
|
|
{
|
|
|
|
Bitmap.Config config = bitmap.getConfig();
|
|
|
|
return bitmap.copy(config, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-01-21 07:16:24 +01:00
|
|
|
public Bitmap getImageBitmap(MusicDirectory.Entry entry, boolean large, int size)
|
|
|
|
{
|
|
|
|
if (entry == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
String coverArt = entry.getCoverArt();
|
|
|
|
|
2018-02-10 20:11:17 +01:00
|
|
|
if (TextUtils.isEmpty(coverArt)) {
|
2014-01-21 07:16:24 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size <= 0)
|
|
|
|
{
|
|
|
|
size = large ? imageSizeLarge : imageSizeDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap bitmap = cache.get(getKey(coverArt, size));
|
|
|
|
|
2014-01-27 07:45:21 +01:00
|
|
|
if (bitmap != null && !bitmap.isRecycled())
|
2014-01-21 07:16:24 +01:00
|
|
|
{
|
|
|
|
Bitmap.Config config = bitmap.getConfig();
|
|
|
|
return bitmap.copy(config, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
2014-01-24 05:18:33 +01:00
|
|
|
private void setImageBitmap(View view, MusicDirectory.Entry entry, Bitmap bitmap, boolean crossFade)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
|
|
|
if (view instanceof ImageView)
|
|
|
|
{
|
|
|
|
ImageView imageView = (ImageView) view;
|
|
|
|
|
2014-01-24 05:18:33 +01:00
|
|
|
MusicDirectory.Entry tagEntry = (MusicDirectory.Entry) view.getTag();
|
|
|
|
|
2014-01-27 00:47:13 +01:00
|
|
|
// Only apply image to the view if the view is intended for this entry
|
2014-01-24 05:18:33 +01:00
|
|
|
if (entry != null && tagEntry != null && !entry.equals(tagEntry))
|
|
|
|
{
|
2014-01-27 00:47:13 +01:00
|
|
|
Log.i(TAG, "View is no longer valid, not setting ImageBitmap");
|
2014-01-24 05:18:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
if (crossFade)
|
|
|
|
{
|
|
|
|
Drawable existingDrawable = imageView.getDrawable();
|
|
|
|
Drawable newDrawable = Util.createDrawableFromBitmap(this.context, bitmap);
|
|
|
|
|
|
|
|
if (existingDrawable == null)
|
|
|
|
{
|
|
|
|
Bitmap emptyImage = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
2014-01-29 08:47:56 +01:00
|
|
|
existingDrawable = new BitmapDrawable(context.getResources(), emptyImage);
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Drawable[] layers = new Drawable[]{existingDrawable, newDrawable};
|
|
|
|
|
|
|
|
TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
|
|
|
|
imageView.setImageDrawable(transitionDrawable);
|
|
|
|
transitionDrawable.startTransition(250);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
imageView.setImageBitmap(bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 08:47:56 +01:00
|
|
|
private void setAvatarImageBitmap(View view, String username, Bitmap bitmap, boolean crossFade)
|
|
|
|
{
|
|
|
|
if (view instanceof ImageView)
|
|
|
|
{
|
|
|
|
ImageView imageView = (ImageView) view;
|
|
|
|
|
|
|
|
String tagEntry = (String) view.getTag();
|
|
|
|
|
|
|
|
// Only apply image to the view if the view is intended for this entry
|
|
|
|
if (username != null && tagEntry != null && !username.equals(tagEntry))
|
|
|
|
{
|
|
|
|
Log.i(TAG, "View is no longer valid, not setting ImageBitmap");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crossFade)
|
|
|
|
{
|
|
|
|
Drawable existingDrawable = imageView.getDrawable();
|
|
|
|
Drawable newDrawable = Util.createDrawableFromBitmap(this.context, bitmap);
|
|
|
|
|
|
|
|
if (existingDrawable == null)
|
|
|
|
{
|
|
|
|
Bitmap emptyImage = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
existingDrawable = new BitmapDrawable(context.getResources(), emptyImage);
|
|
|
|
}
|
|
|
|
|
|
|
|
Drawable[] layers = new Drawable[]{existingDrawable, newDrawable};
|
|
|
|
|
|
|
|
TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
|
|
|
|
imageView.setImageDrawable(transitionDrawable);
|
|
|
|
transitionDrawable.startTransition(250);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
imageView.setImageBitmap(bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUnknownAvatarImage(View view)
|
|
|
|
{
|
|
|
|
setAvatarImageBitmap(view, null, unknownAvatarImage, false);
|
|
|
|
}
|
|
|
|
|
2014-01-23 09:50:49 +01:00
|
|
|
public void setUnknownImage(View view, boolean large)
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
|
|
|
if (large)
|
|
|
|
{
|
2014-01-24 05:18:33 +01:00
|
|
|
setImageBitmap(view, null, largeUnknownImage, false);
|
2013-12-04 07:36:02 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-21 07:16:24 +01:00
|
|
|
public void addImageToCache(Bitmap bitmap, MusicDirectory.Entry entry, int size)
|
|
|
|
{
|
|
|
|
cache.put(getKey(entry.getCoverArt(), size), bitmap);
|
|
|
|
}
|
|
|
|
|
2014-01-29 08:47:56 +01:00
|
|
|
public void addImageToCache(Bitmap bitmap, String username, int size)
|
|
|
|
{
|
|
|
|
cache.put(getKey(username, size), bitmap);
|
|
|
|
}
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
public void clear()
|
|
|
|
{
|
|
|
|
queue.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2014-01-25 10:41:45 +01:00
|
|
|
while (running.get())
|
2013-12-04 07:36:02 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Task task = queue.take();
|
|
|
|
task.execute();
|
2014-01-22 09:58:38 +01:00
|
|
|
}
|
|
|
|
catch (InterruptedException ignored)
|
|
|
|
{
|
2014-01-25 10:41:45 +01:00
|
|
|
running.set(false);
|
|
|
|
break;
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
catch (Throwable x)
|
|
|
|
{
|
|
|
|
Log.e(TAG, "Unexpected exception in ImageLoader.", x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class Task
|
|
|
|
{
|
|
|
|
private final View view;
|
|
|
|
private final MusicDirectory.Entry entry;
|
2014-01-29 08:47:56 +01:00
|
|
|
private final String username;
|
2013-12-04 07:36:02 +01:00
|
|
|
private final Handler handler;
|
|
|
|
private final int size;
|
|
|
|
private final boolean saveToFile;
|
|
|
|
private final boolean crossFade;
|
|
|
|
private final boolean highQuality;
|
|
|
|
|
|
|
|
public Task(View view, MusicDirectory.Entry entry, int size, boolean saveToFile, boolean crossFade, boolean highQuality)
|
|
|
|
{
|
|
|
|
this.view = view;
|
|
|
|
this.entry = entry;
|
2014-01-29 08:47:56 +01:00
|
|
|
this.username = null;
|
|
|
|
this.size = size;
|
|
|
|
this.saveToFile = saveToFile;
|
|
|
|
this.crossFade = crossFade;
|
|
|
|
this.highQuality = highQuality;
|
|
|
|
handler = new Handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task(View view, String username, int size, boolean saveToFile, boolean crossFade, boolean highQuality)
|
|
|
|
{
|
|
|
|
this.view = view;
|
|
|
|
this.entry = null;
|
|
|
|
this.username = username;
|
2013-12-04 07:36:02 +01:00
|
|
|
this.size = size;
|
|
|
|
this.saveToFile = saveToFile;
|
|
|
|
this.crossFade = crossFade;
|
|
|
|
this.highQuality = highQuality;
|
|
|
|
handler = new Handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void execute()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MusicService musicService = MusicServiceFactory.getMusicService(view.getContext());
|
2014-01-29 08:47:56 +01:00
|
|
|
final boolean isAvatar = this.username != null && this.entry == null;
|
|
|
|
final Bitmap bitmap = this.entry != null ? musicService.getCoverArt(view.getContext(), entry, size, saveToFile, highQuality, null) : musicService.getAvatar(view.getContext(), username, size, saveToFile, highQuality, null);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2014-01-29 08:47:56 +01:00
|
|
|
if (isAvatar)
|
|
|
|
addImageToCache(bitmap, username, size);
|
|
|
|
else
|
|
|
|
addImageToCache(bitmap, entry, size);
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
handler.post(new Runnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2014-01-29 08:47:56 +01:00
|
|
|
if (isAvatar)
|
|
|
|
{
|
|
|
|
setAvatarImageBitmap(view, username, bitmap, crossFade);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setImageBitmap(view, entry, bitmap, crossFade);
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Throwable x)
|
|
|
|
{
|
|
|
|
Log.e(TAG, "Failed to download album art.", x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 10:09:55 +01:00
|
|
|
}
|