Fix loading bugs with uncached images

This commit is contained in:
Cameron Gutman 2015-01-31 22:14:12 -05:00
parent b01f7c796e
commit 336f85a31c

View File

@ -172,12 +172,7 @@ public class AppGridAdapter extends GenericGridAdapter<AppView.AppObject> {
@Override @Override
public boolean populateImageView(final ImageView imgView, final AppView.AppObject obj) { public boolean populateImageView(final ImageView imgView, final AppView.AppObject obj) {
// Hide the image view while we're loading the image from disk cache
// Set SSL contexts correctly to allow us to authenticate
Ion.getDefault(imgView.getContext()).getHttpClient().getSSLSocketMiddleware().setTrustManagers(trustAllCerts);
Ion.getDefault(imgView.getContext()).getHttpClient().getSSLSocketMiddleware().setSSLContext(sslContext);
// Hide the image view while we're loading the image from cache
imgView.setVisibility(View.INVISIBLE); imgView.setVisibility(View.INVISIBLE);
// Check the on-disk cache // Check the on-disk cache
@ -221,10 +216,7 @@ public class AppGridAdapter extends GenericGridAdapter<AppView.AppObject> {
InputStream in = null; InputStream in = null;
try { try {
in = CacheHelper.openCacheFileForInput(context.getCacheDir(), "boxart", computer.uuid.toString(), appId + ".png"); in = CacheHelper.openCacheFileForInput(context.getCacheDir(), "boxart", computer.uuid.toString(), appId + ".png");
Bitmap bm = BitmapFactory.decodeStream(in); return BitmapFactory.decodeStream(in);
in.close();
return bm;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
@ -240,38 +232,47 @@ public class AppGridAdapter extends GenericGridAdapter<AppView.AppObject> {
@Override @Override
protected void onPostExecute(Bitmap result) { protected void onPostExecute(Bitmap result) {
if (result != null) { if (result != null) {
// Cache was read successfully // Disk cache was read successfully
LimeLog.info("Image cache hit for ("+computer.uuid+", "+appId+")"); LimeLog.info("Image disk cache hit for ("+computer.uuid+", "+appId+")");
view.setImageBitmap(result); view.setImageBitmap(result);
view.setVisibility(View.VISIBLE); view.setVisibility(View.VISIBLE);
} }
else { else {
LimeLog.info("Image cache miss for ("+computer.uuid+", "+appId+")"); LimeLog.info("Image disk cache miss for ("+computer.uuid+", "+appId+")");
LimeLog.info("Requesting: "+"https://" + getCurrentAddress().getHostAddress() + ":47984/appasset?uniqueid=" + uniqueId + "&appid=" +
appId + "&AssetType=2&AssetIdx=0");
// Load the placeholder image
view.setImageResource(defaultImageRes);
view.setVisibility(View.VISIBLE);
// Set SSL contexts correctly to allow us to authenticate
Ion.getDefault(context).getHttpClient().getSSLSocketMiddleware().setTrustManagers(trustAllCerts);
Ion.getDefault(context).getHttpClient().getSSLSocketMiddleware().setSSLContext(sslContext);
// Kick off the deferred image load // Kick off the deferred image load
synchronized (pendingRequests) { synchronized (pendingRequests) {
Future<ImageViewBitmapInfo> f = Ion.with(view) Future<Bitmap> f = Ion.with(context)
.placeholder(defaultImageRes)
.error(defaultImageRes)
.load("https://" + getCurrentAddress().getHostAddress() + ":47984/appasset?uniqueid=" + uniqueId + "&appid=" + .load("https://" + getCurrentAddress().getHostAddress() + ":47984/appasset?uniqueid=" + uniqueId + "&appid=" +
appId + "&AssetType=2&AssetIdx=0") appId + "&AssetType=2&AssetIdx=0")
.withBitmapInfo() .asBitmap()
.setCallback( .setCallback(new FutureCallback<Bitmap>() {
new FutureCallback<ImageViewBitmapInfo>() {
@Override @Override
public void onCompleted(Exception e, ImageViewBitmapInfo result) { public void onCompleted(Exception e, Bitmap result) {
view.setVisibility(View.VISIBLE);
synchronized (pendingRequests) { synchronized (pendingRequests) {
pendingRequests.remove(view); pendingRequests.remove(view);
} }
// Populate the cache if we got an image back if (result != null) {
if (result != null && // Make the view visible now
result.getBitmapInfo() != null && view.setImageBitmap(result);
result.getBitmapInfo().bitmap != null) { view.setVisibility(View.VISIBLE);
populateBitmapCache(computer.uuid, appId,
result.getBitmapInfo().bitmap); // Populate the disk cache if we got an image back
populateBitmapCache(computer.uuid, appId, result);
}
else {
// Leave the loading icon as is (probably should change this eventually...)
} }
} }
}); });