diff --git a/app/build.gradle b/app/build.gradle index bf412703..f6285c33 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/com/kabouzeid/gramophone/App.java b/app/src/main/java/com/kabouzeid/gramophone/App.java index 9ba9ad1c..fdd0a3b7 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/App.java +++ b/app/src/main/java/com/kabouzeid/gramophone/App.java @@ -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); diff --git a/app/src/main/java/com/kabouzeid/gramophone/imageloader/PhonographExecutor.java b/app/src/main/java/com/kabouzeid/gramophone/imageloader/PhonographExecutor.java new file mode 100644 index 00000000..cbeb4e1f --- /dev/null +++ b/app/src/main/java/com/kabouzeid/gramophone/imageloader/PhonographExecutor.java @@ -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. + *
+ * 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); + } +} diff --git a/app/src/main/java/com/kabouzeid/gramophone/imageloader/PhonographImageDownloader.java b/app/src/main/java/com/kabouzeid/gramophone/imageloader/PhonographImageDownloader.java index 8585c293..d1c2bf7a 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/imageloader/PhonographImageDownloader.java +++ b/app/src/main/java/com/kabouzeid/gramophone/imageloader/PhonographImageDownloader.java @@ -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];