diff --git a/app/src/main/java/com/limelight/grid/AppGridAdapter.java b/app/src/main/java/com/limelight/grid/AppGridAdapter.java index bf6b9144..31ca74fb 100644 --- a/app/src/main/java/com/limelight/grid/AppGridAdapter.java +++ b/app/src/main/java/com/limelight/grid/AppGridAdapter.java @@ -43,6 +43,9 @@ public class AppGridAdapter extends GenericGridAdapter { } public void addApp(AppView.AppObject app) { + // Queue a request to fetch this bitmap in the background + loader.loadBitmapWithContextInBackground(app.app, null, backgroundLoadListener); + itemList.add(app); sortList(); } @@ -51,7 +54,7 @@ public class AppGridAdapter extends GenericGridAdapter { itemList.remove(app); } - private final CachedAppAssetLoader.LoadListener loadListener = new CachedAppAssetLoader.LoadListener() { + private final CachedAppAssetLoader.LoadListener imageViewLoadListener = new CachedAppAssetLoader.LoadListener() { @Override public void notifyLongLoad(Object object) { final ImageView view = (ImageView) object; @@ -86,6 +89,14 @@ public class AppGridAdapter extends GenericGridAdapter { } }; + private final CachedAppAssetLoader.LoadListener backgroundLoadListener = new CachedAppAssetLoader.LoadListener() { + @Override + public void notifyLongLoad(Object object) {} + + @Override + public void notifyLoadComplete(Object object, final Bitmap bitmap) {} + }; + public boolean populateImageView(final ImageView imgView, final AppView.AppObject obj) { // Cancel pending loads on this image view CachedAppAssetLoader.LoaderTuple tuple = loadingTuples.remove(imgView); @@ -99,7 +110,7 @@ public class AppGridAdapter extends GenericGridAdapter { imgView.setAlpha(0.0f); // Start loading the bitmap - tuple = loader.loadBitmapWithContext(obj.app, imgView, loadListener); + tuple = loader.loadBitmapWithContext(obj.app, imgView, imageViewLoadListener); if (tuple != null) { // The load was issued asynchronously loadingTuples.put(imgView, tuple); diff --git a/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java b/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java index 898ce908..172b05c2 100644 --- a/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java +++ b/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java @@ -12,7 +12,8 @@ import java.util.concurrent.TimeUnit; public class CachedAppAssetLoader { private final ComputerDetails computer; private final String uniqueId; - private final ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 8, Long.MAX_VALUE, TimeUnit.DAYS, new LinkedBlockingQueue()); + private final ThreadPoolExecutor foregroundExecutor = new ThreadPoolExecutor(8, 8, Long.MAX_VALUE, TimeUnit.DAYS, new LinkedBlockingQueue()); + private final ThreadPoolExecutor backgroundExecutor = new ThreadPoolExecutor(2, 2, Long.MAX_VALUE, TimeUnit.DAYS, new LinkedBlockingQueue()); private final NetworkLoader networkLoader; private final CachedLoader memoryLoader; private final CachedLoader diskLoader; @@ -86,6 +87,14 @@ public class CachedAppAssetLoader { } public LoaderTuple loadBitmapWithContext(NvApp app, Object context, LoadListener listener) { + return loadBitmapWithContext(app, context, listener, false); + } + + public LoaderTuple loadBitmapWithContextInBackground(NvApp app, Object context, LoadListener listener) { + return loadBitmapWithContext(app, context, listener, true); + } + + private LoaderTuple loadBitmapWithContext(NvApp app, Object context, LoadListener listener, boolean background) { LoaderTuple tuple = new LoaderTuple(computer, uniqueId, app); // First, try the memory cache in the current context @@ -105,7 +114,12 @@ public class CachedAppAssetLoader { } // If it's not in memory, throw this in our executor - executor.execute(createLoaderRunnable(tuple, context, listener)); + if (background) { + backgroundExecutor.execute(createLoaderRunnable(tuple, context, listener)); + } + else { + foregroundExecutor.execute(createLoaderRunnable(tuple, context, listener)); + } return tuple; }