From 2da4f7909836f6688f6c9059bd68edc9df3370f1 Mon Sep 17 00:00:00 2001 From: tzugen Date: Wed, 23 Jun 2021 17:30:16 +0200 Subject: [PATCH] Increase memory cache size --- detekt-config.yml | 1 + .../moire/ultrasonic/imageloader/ImageLoader.kt | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/detekt-config.yml b/detekt-config.yml index 7fff31c3..eb76d7f7 100644 --- a/detekt-config.yml +++ b/detekt-config.yml @@ -45,6 +45,7 @@ complexity: thresholdInFiles: 20 thresholdInClasses: 20 thresholdInInterfaces: 20 + thresholdInObjects: 30 LabeledExpression: active: false diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/ImageLoader.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/ImageLoader.kt index 9d40ad28..73862c20 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/ImageLoader.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/imageloader/ImageLoader.kt @@ -1,9 +1,13 @@ package org.moire.ultrasonic.imageloader +import android.app.ActivityManager import android.content.Context +import android.content.pm.ApplicationInfo import android.text.TextUtils import android.view.View import android.widget.ImageView +import androidx.core.content.ContextCompat +import com.squareup.picasso.LruCache import com.squareup.picasso.Picasso import com.squareup.picasso.RequestCreator import java.io.File @@ -35,6 +39,7 @@ class ImageLoader( private val picasso = Picasso.Builder(context) .addRequestHandler(CoverArtRequestHandler(apiClient)) .addRequestHandler(AvatarRequestHandler(apiClient)) + .memoryCache(LruCache(calculateMemoryCacheSize(context))) .build().apply { setIndicatorsEnabled(BuildConfig.DEBUG) } @@ -179,6 +184,18 @@ class ImageLoader( return requested } } + + private fun calculateMemoryCacheSize(context: Context): Int { + val am = ContextCompat.getSystemService( + context, + ActivityManager::class.java + ) + val largeHeap = context.applicationInfo.flags and ApplicationInfo.FLAG_LARGE_HEAP != 0 + val memoryClass = if (largeHeap) am!!.largeMemoryClass else am!!.memoryClass + // Target 25% of the available heap. + @Suppress("MagicNumber") + return (1024L * 1024L * memoryClass / 4).toInt() + } } /**