Finally fixed the issue where the artist image loading blocks the image loaders queue. This is done by using a custom Executor which is basically a wrapper for two independent ThreadPoolExecutors.

This commit is contained in:
Karim Abou Zeid 2015-08-29 19:22:50 +02:00
commit 638ab4547c
4 changed files with 65 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import android.app.Application;
import android.content.Context;
import com.crashlytics.android.Crashlytics;
import com.kabouzeid.gramophone.imageloader.PhonographExecutor;
import com.kabouzeid.gramophone.imageloader.PhonographImageDownloader;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
@ -38,6 +39,7 @@ public class App extends Application {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.imageDownloader(new PhonographImageDownloader(this))
.taskExecutor(new PhonographExecutor())
.memoryCacheSizePercentage(30)
.build();
ImageLoader.getInstance().init(config);

View file

@ -0,0 +1,60 @@
package com.kabouzeid.gramophone.imageloader;
import android.support.annotation.NonNull;
import com.nostra13.universalimageloader.core.DefaultConfigurationFactory;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.LoadAndDisplayImageTask;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import java.io.File;
import java.util.concurrent.Executor;
/**
* A custom {@link Executor} meant for kabouzeid's fork of nostra13's Android Universal Image Loader (https://github.com/kabouzeid/Android-Universal-Image-Loader).
* This {@link Executor} separates network and disk loading tasks into different executors so the network image loading doesn't block the disk image loading which is in most cases much faster.
* <p/>
* Maybe there is a better solution for this with a single (ThreadPool-)Executor, but I'm lacking experience here.
*
* @author Karim Abou Zeid (kabouzeid)
*/
public class PhonographExecutor implements Executor {
public static final String TAG = PhonographExecutor.class.getSimpleName();
private Executor localTaskExecutor;
private Executor networkTaskExecutor;
// The thread pool size here needs further testing. Maybe the 2 additional Threads of the networkTaskExecutor are to much for lower end devices.
public PhonographExecutor() {
localTaskExecutor = DefaultConfigurationFactory.createExecutor(
ImageLoaderConfiguration.Builder.DEFAULT_THREAD_POOL_SIZE,
ImageLoaderConfiguration.Builder.DEFAULT_THREAD_PRIORITY,
ImageLoaderConfiguration.Builder.DEFAULT_TASK_PROCESSING_TYPE
);
networkTaskExecutor = DefaultConfigurationFactory.createExecutor(
2,
ImageLoaderConfiguration.Builder.DEFAULT_THREAD_PRIORITY,
ImageLoaderConfiguration.Builder.DEFAULT_TASK_PROCESSING_TYPE
);
}
@Override
public void execute(@NonNull Runnable command) {
if (command instanceof LoadAndDisplayImageTask) {
String uri = ((LoadAndDisplayImageTask) command).getLoadingUri();
switch (ImageDownloader.Scheme.ofUri(uri)) {
case HTTP:
case HTTPS:
File imageFile = ImageLoader.getInstance().getDiskCache().get(uri);
if (imageFile == null || !imageFile.exists() || imageFile.length() <= 0) {
// the image is not yet in the disk cache
networkTaskExecutor.execute(command);
return;
}
}
}
localTaskExecutor.execute(command);
}
}

View file

@ -55,6 +55,7 @@ public class PhonographImageDownloader extends BaseImageDownloader {
return super.getStream(imageUri, extra);
}
@Nullable
protected InputStream getStreamFromArtist(@NonNull String imageUri, @NonNull Object extra) throws IOException {
String[] data = imageUri.substring(SCHEME_ARTIST.length()).split("#", 2);
String artistName = data[1];