mirror of
https://github.com/kalarina-bit/Sparkledns.git
synced 2026-11-22 07:36:09 +00:00
Render app icons at their on-screen size
Icons were always drawn at 96 px: blurry on screens where 40 dp is larger, wasted memory where it is smaller. The cache is now limited to 10 MB instead of 256 entries.
This commit is contained in:
@@ -53,6 +53,7 @@ import androidx.compose.ui.graphics.ImageBitmap
|
|||||||
import androidx.compose.ui.graphics.asImageBitmap
|
import androidx.compose.ui.graphics.asImageBitmap
|
||||||
import androidx.compose.ui.graphics.graphicsLayer
|
import androidx.compose.ui.graphics.graphicsLayer
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.platform.LocalDensity
|
||||||
import androidx.compose.ui.res.painterResource
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.semantics.Role
|
import androidx.compose.ui.semantics.Role
|
||||||
@@ -78,8 +79,11 @@ private enum class AppGroup(@StringRes val label: Int, @DrawableRes val icon: In
|
|||||||
|
|
||||||
private class AppItem(val label: String, val packageName: String, val group: AppGroup)
|
private class AppItem(val label: String, val packageName: String, val group: AppGroup)
|
||||||
|
|
||||||
// Decoded icons survive scrolling and tab switches instead of being rendered again every time
|
// Decoded icons survive scrolling and tab switches instead of being rendered again every time.
|
||||||
private val IconCache = LruCache<String, ImageBitmap>(256)
|
// Limited by memory rather than count, since icon size now follows the screen density.
|
||||||
|
private val IconCache = object : LruCache<String, ImageBitmap>(10 * 1024 * 1024) {
|
||||||
|
override fun sizeOf(key: String, value: ImageBitmap) = value.width * value.height * 4
|
||||||
|
}
|
||||||
|
|
||||||
internal fun clearAppIconCache() = IconCache.evictAll()
|
internal fun clearAppIconCache() = IconCache.evictAll()
|
||||||
|
|
||||||
@@ -283,12 +287,15 @@ private fun GroupHeader(
|
|||||||
@Composable
|
@Composable
|
||||||
private fun AppRow(modifier: Modifier, app: AppItem, enabled: Boolean, last: Boolean, onToggle: (Boolean) -> Unit) {
|
private fun AppRow(modifier: Modifier, app: AppItem, enabled: Boolean, last: Boolean, onToggle: (Boolean) -> Unit) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val icon by produceState<ImageBitmap?>(IconCache.get(app.packageName), app.packageName) {
|
// Rendered at the size it is shown (40 dp), so icons stay sharp on dense screens
|
||||||
|
val iconPx = with(LocalDensity.current) { 40.dp.roundToPx() }
|
||||||
|
val cacheKey = "${app.packageName}@$iconPx"
|
||||||
|
val icon by produceState<ImageBitmap?>(IconCache.get(cacheKey), cacheKey) {
|
||||||
if (value != null) return@produceState
|
if (value != null) return@produceState
|
||||||
value = withContext(Dispatchers.IO) {
|
value = withContext(Dispatchers.IO) {
|
||||||
runCatching { context.packageManager.getApplicationIcon(app.packageName).toImageBitmap(96) }
|
runCatching { context.packageManager.getApplicationIcon(app.packageName).toImageBitmap(iconPx) }
|
||||||
.getOrNull()
|
.getOrNull()
|
||||||
?.also { IconCache.put(app.packageName, it) }
|
?.also { IconCache.put(cacheKey, it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class SparkleApp : Application() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The VPN keeps the process running long after the screens are gone; decoded app icons
|
// The VPN keeps the process running long after the screens are gone; decoded app icons
|
||||||
// (up to about 9 MB) can be rendered again the next time the Apps tab opens
|
// (up to 10 MB) can be rendered again the next time the Apps tab opens
|
||||||
override fun onTrimMemory(level: Int) {
|
override fun onTrimMemory(level: Int) {
|
||||||
super.onTrimMemory(level)
|
super.onTrimMemory(level)
|
||||||
if (level >= TRIM_MEMORY_UI_HIDDEN) clearAppIconCache()
|
if (level >= TRIM_MEMORY_UI_HIDDEN) clearAppIconCache()
|
||||||
|
|||||||
Reference in New Issue
Block a user