4545import okhttp3 .ResponseBody ;
4646
4747public class Assets {
48+ private static LruCache <String , Bitmap > memoryCache ;
49+
50+ static {
51+ final int maxMemory = (int ) (Runtime .getRuntime ().maxMemory () / 1024 );
52+ final int cacheSize = maxMemory / 8 ;
53+ Logger .d ("setup lru cache " + (cacheSize / 1024 ) + " MB" );
54+ memoryCache = new LruCache <String , Bitmap >(cacheSize ) {
55+ @ Override
56+ protected int sizeOf (String key , Bitmap bitmap ) {
57+ return bitmap .getByteCount () / 1024 ;
58+ }
59+ };
60+ }
61+
4862 private enum Variant {
4963 @ SerializedName ("1x" )
5064 MDPI ("1x" , 1.0f ),
@@ -109,7 +123,6 @@ public interface Callback {
109123 private Project project ;
110124 private File manifestFile ;
111125 private Manifest manifest ;
112- private LruCache <String , Bitmap > memoryCache ;
113126
114127 Assets (Project project ) {
115128 this .app = Snabble .getInstance ().getApplication ();
@@ -118,16 +131,6 @@ public interface Callback {
118131 this .assetDir = new File (project .getInternalStorageDirectory (), "assets/" );
119132 this .assetDir .mkdirs ();
120133
121- final int maxMemory = (int ) (Runtime .getRuntime ().maxMemory () / 1024 );
122- final int cacheSize = maxMemory / 16 ;
123- Logger .d ("setup lru cache " + (cacheSize / 1024 ) + " MB" );
124- memoryCache = new LruCache <String , Bitmap >(cacheSize ) {
125- @ Override
126- protected int sizeOf (String key , Bitmap bitmap ) {
127- return bitmap .getByteCount () / 1024 ;
128- }
129- };
130-
131134 loadManifest ();
132135 }
133136
@@ -354,19 +357,26 @@ private static boolean isNightModeActive(Context context) {
354357 }
355358
356359 public Bitmap getBitmap (String name ) {
357- return getBitmapSVG (name );
360+ return getBitmap (name , Type . SVG );
358361 }
359362
360363 public Bitmap getBitmap (String name , Type type ) {
364+ Bitmap bitmap ;
365+
361366 switch (type ) {
362367 case SVG :
363- return getBitmapSVG (name );
368+ bitmap = getBitmapSVG (name );
369+ break ;
364370 case JPG :
365371 case WEBP :
366- return getBitmapByType (name , type );
372+ bitmap = getBitmapByType (name , type );
373+ break ;
367374 default :
368- return null ;
375+ bitmap = null ;
376+ break ;
369377 }
378+
379+ return bitmap ;
370380 }
371381
372382 private Asset getAsset (String name , Type type ) {
@@ -390,31 +400,21 @@ private Asset getAsset(String name, Type type) {
390400 break ;
391401 }
392402
393- synchronized (this ) {
394- Asset asset = manifest .assets .get (fileName );
395- if (asset == null ) {
396- // try non night mode version
397- asset = manifest .assets .get (name );
398- }
399-
400- return asset ;
403+ Asset asset = manifest .assets .get (fileName );
404+ if (asset == null ) {
405+ // try non night mode version
406+ asset = manifest .assets .get (name );
401407 }
408+
409+ return asset ;
402410 }
403411
404412 private Bitmap getBitmapByType (String name , Type type ) {
405413 Asset asset = getAsset (name , type );
406414 if (asset != null ) {
407415 try {
408416 Logger .d ("render %s %s/%s" , type .name (), project .getId (), name );
409- String cacheKey = asset .hash ;
410- Bitmap cachedBitmap = getBitmapFromMemCache (cacheKey );
411- if (cachedBitmap != null ) {
412- return cachedBitmap ;
413- }
414-
415- Bitmap bitmap = BitmapFactory .decodeStream (new FileInputStream (asset .file ));
416- addBitmapToMemoryCache (cacheKey , bitmap );
417- return bitmap ;
417+ return BitmapFactory .decodeStream (new FileInputStream (asset .file ));
418418 } catch (Exception e ) {
419419 Logger .d ("could not decode " + name + ": " + e .toString ());
420420 return null ;
@@ -440,16 +440,9 @@ private Bitmap getBitmapSVG(String name) {
440440 svg .setDocumentWidth (width );
441441 svg .setDocumentHeight (height );
442442
443- String cacheKey = asset .hash ;
444- Bitmap cachedBitmap = getBitmapFromMemCache (cacheKey );
445- if (cachedBitmap != null ) {
446- return cachedBitmap ;
447- }
448-
449443 Bitmap bitmap = Bitmap .createBitmap (width , height , Bitmap .Config .ARGB_8888 );
450444 Canvas canvas = new Canvas (bitmap );
451445 svg .renderToCanvas (canvas );
452- addBitmapToMemoryCache (cacheKey , bitmap );
453446 return bitmap ;
454447 } catch (Exception e ) {
455448 Logger .d ("could not decode " + name + ": " + e .toString ());
@@ -483,17 +476,35 @@ public void get(String name, Type type, boolean async, Callback callback) {
483476 }
484477 final String finalFileName = fileName ;
485478
479+ Asset asset = getAsset (name , type );
480+ if (asset != null ) {
481+ String cacheKey = asset .hash ;
482+ Bitmap cachedBitmap = getBitmapFromMemCache (cacheKey );
483+ if (cachedBitmap != null ) {
484+ callback .onReceive (cachedBitmap );
485+ return ;
486+ }
487+ }
488+
486489 if (async ) {
487- Dispatch .background (() -> get (finalFileName , type , callback ));
490+ Dispatch .background (() -> {
491+ get (finalFileName , type , callback );
492+ });
488493 } else {
489494 get (finalFileName , type , callback );
490495 }
491496 }
492497
493- private void get (final String name , Type type , Callback callback ) {
498+ private void get (String name , Type type , Callback callback ) {
494499 Bitmap bitmap = getBitmap (name , type );
495500 if (bitmap != null ) {
496501 Logger .d ("cache hit " + name );
502+ Asset asset = getAsset (name , type );
503+ if (asset != null ) {
504+ String cacheKey = asset .hash ;
505+ addBitmapToMemoryCache (cacheKey , bitmap );
506+ }
507+
497508 Dispatch .mainThread (() -> callback .onReceive (bitmap ));
498509 } else {
499510 Logger .d ("cache miss " + name );
@@ -502,6 +513,12 @@ private void get(final String name, Type type, Callback callback) {
502513 @ Override
503514 public void success () {
504515 Bitmap b = getBitmap (name , type );
516+ Asset asset = getAsset (name , type );
517+ if (asset != null ) {
518+ String cacheKey = asset .hash ;
519+ addBitmapToMemoryCache (cacheKey , b );
520+ }
521+
505522 Dispatch .mainThread (() -> callback .onReceive (b ));
506523 }
507524
@@ -514,14 +531,14 @@ public void failure() {
514531 }
515532 }
516533
517- public void addBitmapToMemoryCache (String key , Bitmap bitmap ) {
534+ public static void addBitmapToMemoryCache (String key , Bitmap bitmap ) {
518535 if (getBitmapFromMemCache (key ) == null ) {
519- Logger .d ("put lru cache " + key );
536+ Logger .d ("put lru cache %d / %d MB %s" , memoryCache . size () / 1024 , memoryCache . maxSize () / 1024 , key );
520537 memoryCache .put (key , bitmap );
521538 }
522539 }
523540
524- public Bitmap getBitmapFromMemCache (String key ) {
541+ public static Bitmap getBitmapFromMemCache (String key ) {
525542 Bitmap bitmap = memoryCache .get (key );
526543 Logger .d ("get lru cache %s %s" , bitmap != null ? "HIT" : "MISS" , key );
527544 return bitmap ;
0 commit comments