Fetch app assets in the background while in the app view

This commit is contained in:
Cameron Gutman 2015-02-25 21:57:54 -05:00
parent 90209f2ca2
commit 833b7c3916
2 changed files with 29 additions and 4 deletions

View File

@ -43,6 +43,9 @@ public class AppGridAdapter extends GenericGridAdapter<AppView.AppObject> {
}
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<AppView.AppObject> {
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<AppView.AppObject> {
}
};
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<AppView.AppObject> {
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);

View File

@ -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<Runnable>());
private final ThreadPoolExecutor foregroundExecutor = new ThreadPoolExecutor(8, 8, Long.MAX_VALUE, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>());
private final ThreadPoolExecutor backgroundExecutor = new ThreadPoolExecutor(2, 2, Long.MAX_VALUE, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>());
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;
}