-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcd6805
commit bd8e3f1
Showing
10 changed files
with
326 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
.../src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/base64/BitmapPool.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.sessionreplay.internal.recorder.base64 | ||
|
||
import android.content.ComponentCallbacks2 | ||
import android.content.res.Configuration | ||
import android.graphics.Bitmap | ||
import android.graphics.Bitmap.Config | ||
import android.util.LruCache | ||
import com.datadog.android.sessionreplay.internal.utils.CacheUtils | ||
|
||
internal object BitmapPool : Cache<String, Bitmap>, ComponentCallbacks2 { | ||
@Suppress("MagicNumber") | ||
val MAX_CACHE_MEMORY_SIZE_BYTES = (Runtime.getRuntime().maxMemory() / 8).toInt() | ||
|
||
private var cache: LruCache<String, Bitmap> = object : | ||
LruCache<String, Bitmap>(MAX_CACHE_MEMORY_SIZE_BYTES) { | ||
override fun sizeOf(key: String?, value: Bitmap): Int { | ||
return value.allocationByteCount | ||
} | ||
} | ||
|
||
override fun put(value: Bitmap) { | ||
val key = generateKey(value) | ||
cache.put(key, value) | ||
} | ||
|
||
override fun size(): Int = cache.size() | ||
|
||
override fun clear() = cache.evictAll() | ||
|
||
override fun get(element: String): Bitmap? = cache.get(element) | ||
|
||
internal fun getBitmapByProperties(width: Int, height: Int, config: Config): Bitmap? { | ||
val key = generateKey(width, height, config) | ||
return this.get(key) | ||
} | ||
|
||
private fun generateKey(bitmap: Bitmap) = | ||
generateKey(bitmap.width, bitmap.height, bitmap.config) | ||
|
||
private fun generateKey(width: Int, height: Int, config: Config) = | ||
"$width-$height-$config" | ||
|
||
override fun onConfigurationChanged(newConfig: Configuration) {} | ||
|
||
override fun onLowMemory() { | ||
cache.evictAll() | ||
} | ||
|
||
override fun onTrimMemory(level: Int) { | ||
val cacheUtils = CacheUtils<String, Bitmap>() | ||
cacheUtils.handleTrimMemory(level, cache) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...ion-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/CacheUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.sessionreplay.internal.utils | ||
|
||
import android.content.ComponentCallbacks2 | ||
import android.util.LruCache | ||
|
||
internal class CacheUtils<K : Any, V : Any> { | ||
internal fun handleTrimMemory(level: Int, cache: LruCache<K, V>) { | ||
@Suppress("MagicNumber") | ||
val onLowMemorySizeBytes = cache.maxSize() / 2 | ||
|
||
@Suppress("MagicNumber") | ||
val onModerateMemorySizeBytes = cache.maxSize() / 4 | ||
|
||
when (level) { | ||
ComponentCallbacks2.TRIM_MEMORY_BACKGROUND -> { | ||
cache.evictAll() | ||
} | ||
|
||
ComponentCallbacks2.TRIM_MEMORY_COMPLETE -> { | ||
cache.evictAll() | ||
} | ||
|
||
ComponentCallbacks2.TRIM_MEMORY_MODERATE -> { | ||
cache.trimToSize(onModerateMemorySizeBytes) | ||
} | ||
|
||
ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL -> { | ||
cache.evictAll() | ||
} | ||
|
||
ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW -> { | ||
cache.trimToSize(onLowMemorySizeBytes) | ||
} | ||
|
||
ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE -> { | ||
cache.trimToSize(onModerateMemorySizeBytes) | ||
} | ||
|
||
ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN -> { | ||
cache.evictAll() | ||
} | ||
|
||
else -> { | ||
cache.evictAll() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.