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:
parent
783332c3af
commit
638ab4547c
4 changed files with 65 additions and 1 deletions
|
|
@ -17,6 +17,7 @@ apply plugin: 'com.github.triplet.play'
|
|||
|
||||
repositories {
|
||||
maven { url 'https://maven.fabric.io/public' }
|
||||
maven { url "https://jitpack.io" }
|
||||
}
|
||||
|
||||
// For pushing APKs directly to Google Play. Won't work without the .p12 key.
|
||||
|
|
@ -102,7 +103,7 @@ dependencies {
|
|||
compile 'com.squareup.retrofit:retrofit:1.9.0'
|
||||
compile 'com.squareup.okhttp:okhttp:2.4.0'
|
||||
|
||||
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
|
||||
compile 'com.github.kabouzeid:Android-Universal-Image-Loader:8ffb5d4afa'
|
||||
|
||||
compile 'com.afollestad:material-dialogs:0.7.8.1'
|
||||
compile 'com.afollestad:material-cab:0.1.4'
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue