From d59e5ae9cf76ffd09433debf9ef489f2952d1042 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 11 Jun 2020 19:08:25 -0700 Subject: [PATCH] Store the original bitmap dimensions for the box art --- .../grid/assets/CachedAppAssetLoader.java | 20 +++++++++---------- .../grid/assets/DiskAssetLoader.java | 12 ++++++++--- .../grid/assets/MemoryAssetLoader.java | 19 +++++++++--------- .../limelight/grid/assets/ScaledBitmap.java | 18 +++++++++++++++++ 4 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/com/limelight/grid/assets/ScaledBitmap.java 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 3362fa35..53fbbe39 100644 --- a/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java +++ b/app/src/main/java/com/limelight/grid/assets/CachedAppAssetLoader.java @@ -92,7 +92,7 @@ public class CachedAppAssetLoader { memoryLoader.clearCache(); } - private Bitmap doNetworkAssetLoad(LoaderTuple tuple, LoaderTask task) { + private ScaledBitmap doNetworkAssetLoad(LoaderTuple tuple, LoaderTask task) { // Try 3 times for (int i = 0; i < 3; i++) { // Check again whether we've been cancelled or the image view is gone @@ -113,7 +113,7 @@ public class CachedAppAssetLoader { // If there's a task associated with this load, we should return the bitmap if (task != null) { // If the cached bitmap is valid, return it. Otherwise, we'll try the load again - Bitmap bmp = diskLoader.loadBitmapFromCache(tuple, (int) scalingDivider); + ScaledBitmap bmp = diskLoader.loadBitmapFromCache(tuple, (int) scalingDivider); if (bmp != null) { return bmp; } @@ -135,7 +135,7 @@ public class CachedAppAssetLoader { return null; } - private class LoaderTask extends AsyncTask { + private class LoaderTask extends AsyncTask { private final WeakReference imageViewRef; private final WeakReference progressViewRef; private final boolean diskOnly; @@ -149,7 +149,7 @@ public class CachedAppAssetLoader { } @Override - protected Bitmap doInBackground(LoaderTuple... params) { + protected ScaledBitmap doInBackground(LoaderTuple... params) { tuple = params[0]; // Check whether it has been cancelled or the views are gone @@ -157,7 +157,7 @@ public class CachedAppAssetLoader { return null; } - Bitmap bmp = diskLoader.loadBitmapFromCache(tuple, (int) scalingDivider); + ScaledBitmap bmp = diskLoader.loadBitmapFromCache(tuple, (int) scalingDivider); if (bmp == null) { if (!diskOnly) { // Try to load the asset from the network @@ -205,7 +205,7 @@ public class CachedAppAssetLoader { } @Override - protected void onPostExecute(final Bitmap bitmap) { + protected void onPostExecute(final ScaledBitmap bitmap) { // Do nothing if cancelled if (isCancelled()) { return; @@ -231,7 +231,7 @@ public class CachedAppAssetLoader { @Override public void onAnimationEnd(Animation animation) { // Fade in the new box art - imageView.setImageBitmap(bitmap); + imageView.setImageBitmap(bitmap.bitmap); imageView.setAnimation(AnimationUtils.loadAnimation(imageView.getContext(), R.anim.boxart_fadein)); } @@ -242,7 +242,7 @@ public class CachedAppAssetLoader { } else { // View is invisible already, so just fade in the new art - imageView.setImageBitmap(bitmap); + imageView.setImageBitmap(bitmap.bitmap); imageView.setAnimation(AnimationUtils.loadAnimation(imageView.getContext(), R.anim.boxart_fadein)); imageView.setVisibility(View.VISIBLE); } @@ -338,11 +338,11 @@ public class CachedAppAssetLoader { prgView.setVisibility(View.INVISIBLE); // First, try the memory cache in the current context - Bitmap bmp = memoryLoader.loadBitmapFromCache(tuple); + ScaledBitmap bmp = memoryLoader.loadBitmapFromCache(tuple); if (bmp != null) { // Show the bitmap immediately imgView.setVisibility(View.VISIBLE); - imgView.setImageBitmap(bmp); + imgView.setImageBitmap(bmp.bitmap); return true; } diff --git a/app/src/main/java/com/limelight/grid/assets/DiskAssetLoader.java b/app/src/main/java/com/limelight/grid/assets/DiskAssetLoader.java index 38a9f3f1..2e0c73c3 100644 --- a/app/src/main/java/com/limelight/grid/assets/DiskAssetLoader.java +++ b/app/src/main/java/com/limelight/grid/assets/DiskAssetLoader.java @@ -64,7 +64,7 @@ public class DiskAssetLoader { return inSampleSize; } - public Bitmap loadBitmapFromCache(CachedAppAssetLoader.LoaderTuple tuple, int sampleSize) { + public ScaledBitmap loadBitmapFromCache(CachedAppAssetLoader.LoaderTuple tuple, int sampleSize) { File file = getFile(tuple.computer.uuid, tuple.app.getAppId()); // Don't bother with anything if it doesn't exist @@ -110,27 +110,33 @@ public class DiskAssetLoader { bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), options); if (bmp != null) { LimeLog.info("Tuple "+tuple+" decoded from disk cache with sample size: "+options.inSampleSize); + return new ScaledBitmap(decodeOnlyOptions.outWidth, decodeOnlyOptions.outHeight, bmp); } } else { // On P, we can get a bitmap back in one step with ImageDecoder + final ScaledBitmap scaledBitmap = new ScaledBitmap(); try { - bmp = ImageDecoder.decodeBitmap(ImageDecoder.createSource(file), new ImageDecoder.OnHeaderDecodedListener() { + scaledBitmap.bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(file), new ImageDecoder.OnHeaderDecodedListener() { @Override public void onHeaderDecoded(ImageDecoder imageDecoder, ImageDecoder.ImageInfo imageInfo, ImageDecoder.Source source) { + scaledBitmap.originalWidth = imageInfo.getSize().getWidth(); + scaledBitmap.originalHeight = imageInfo.getSize().getHeight(); + imageDecoder.setTargetSize(STANDARD_ASSET_WIDTH, STANDARD_ASSET_HEIGHT); if (isLowRamDevice) { imageDecoder.setMemorySizePolicy(ImageDecoder.MEMORY_POLICY_LOW_RAM); } } }); + return scaledBitmap; } catch (IOException e) { e.printStackTrace(); return null; } } - return bmp; + return null; } public File getFile(String computerUuid, int appId) { diff --git a/app/src/main/java/com/limelight/grid/assets/MemoryAssetLoader.java b/app/src/main/java/com/limelight/grid/assets/MemoryAssetLoader.java index 6728798d..7feea570 100644 --- a/app/src/main/java/com/limelight/grid/assets/MemoryAssetLoader.java +++ b/app/src/main/java/com/limelight/grid/assets/MemoryAssetLoader.java @@ -1,6 +1,5 @@ package com.limelight.grid.assets; -import android.graphics.Bitmap; import android.util.LruCache; import com.limelight.LimeLog; @@ -10,15 +9,15 @@ import java.util.HashMap; public class MemoryAssetLoader { private static final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); - private static final LruCache memoryCache = new LruCache(maxMemory / 16) { + private static final LruCache memoryCache = new LruCache(maxMemory / 16) { @Override - protected int sizeOf(String key, Bitmap bitmap) { + protected int sizeOf(String key, ScaledBitmap bitmap) { // Sizeof returns kilobytes - return bitmap.getByteCount() / 1024; + return bitmap.bitmap.getByteCount() / 1024; } @Override - protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { + protected void entryRemoved(boolean evicted, String key, ScaledBitmap oldValue, ScaledBitmap newValue) { super.entryRemoved(evicted, key, oldValue, newValue); if (evicted) { @@ -27,22 +26,22 @@ public class MemoryAssetLoader { } } }; - private static final HashMap> evictionCache = new HashMap<>(); + private static final HashMap> evictionCache = new HashMap<>(); private static String constructKey(CachedAppAssetLoader.LoaderTuple tuple) { return tuple.computer.uuid+"-"+tuple.app.getAppId(); } - public Bitmap loadBitmapFromCache(CachedAppAssetLoader.LoaderTuple tuple) { + public ScaledBitmap loadBitmapFromCache(CachedAppAssetLoader.LoaderTuple tuple) { final String key = constructKey(tuple); - Bitmap bmp = memoryCache.get(key); + ScaledBitmap bmp = memoryCache.get(key); if (bmp != null) { LimeLog.info("LRU cache hit for tuple: "+tuple); return bmp; } - WeakReference bmpRef = evictionCache.get(key); + WeakReference bmpRef = evictionCache.get(key); if (bmpRef != null) { bmp = bmpRef.get(); if (bmp != null) { @@ -63,7 +62,7 @@ public class MemoryAssetLoader { return null; } - public void populateCache(CachedAppAssetLoader.LoaderTuple tuple, Bitmap bitmap) { + public void populateCache(CachedAppAssetLoader.LoaderTuple tuple, ScaledBitmap bitmap) { memoryCache.put(constructKey(tuple), bitmap); } diff --git a/app/src/main/java/com/limelight/grid/assets/ScaledBitmap.java b/app/src/main/java/com/limelight/grid/assets/ScaledBitmap.java new file mode 100644 index 00000000..dbf9f0f4 --- /dev/null +++ b/app/src/main/java/com/limelight/grid/assets/ScaledBitmap.java @@ -0,0 +1,18 @@ +package com.limelight.grid.assets; + +import android.graphics.Bitmap; + +public class ScaledBitmap { + public int originalWidth; + public int originalHeight; + + public Bitmap bitmap; + + public ScaledBitmap() {} + + public ScaledBitmap(int originalWidth, int originalHeight, Bitmap bitmap) { + this.originalWidth = originalWidth; + this.originalHeight = originalHeight; + this.bitmap = bitmap; + } +}