Skip to content

Commit

Permalink
[fix] Fix provider bitmap reuse bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Apr 10, 2024
1 parent 000d4b5 commit 960611a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
minSdk = 24
targetSdk = 34
versionCode = 62
versionName = "2.2-beta01"
versionName = "2.2-beta02"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
24 changes: 11 additions & 13 deletions app/src/main/java/com/skyd/rays/model/provider/StickerProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,23 @@ class StickerProvider : DocumentsProvider() {
inSampleSize = inSampleSize shl 1
}
inJustDecodeBounds = false
if (reuseBitmap?.get() != null) {
inBitmap = reuseBitmap?.get()
}
// Only mutable bitmap can be reused.
inMutable = true
val bitmap = BitmapFactory.decodeFile(stickerFile.path, this)
val oldBitmap = reuseBitmap?.get()
if (oldBitmap != null) {
if (oldBitmap.width >= outWidth / inSampleSize &&
oldBitmap.height >= outHeight / inSampleSize
) {
inBitmap = reuseBitmap?.get()
}
}
val bitmap = BitmapFactory.decodeFile(stickerFile.path, this) ?: inBitmap
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos)

reuseBitmap.let { reuseBitmap ->
if (reuseBitmap == null) {
if (reuseBitmap?.get() != bitmap) {
reuseBitmap?.clear()
this@StickerProvider.reuseBitmap = WeakReference(bitmap)
} else {
if (reuseBitmap.get() == null ||
reuseBitmap.get()!!.width < bitmap.width ||
reuseBitmap.get()!!.height < bitmap.height
) {
reuseBitmap.clear()
this@StickerProvider.reuseBitmap = WeakReference(bitmap)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ fun DataScreen(viewModel: DataViewModel = hiltViewModel()) {
.nestedScroll(scrollBehavior.nestedScrollConnection),
contentPadding = paddingValues,
) {
item {
CategorySettingsItem(text = stringResource(id = R.string.data_screen_source_category))
}
item {
BaseSettingsItem(
icon = rememberVectorPainter(Icons.Default.Source),
Expand All @@ -80,9 +77,6 @@ fun DataScreen(viewModel: DataViewModel = hiltViewModel()) {
onClick = { navController.navigate(IMAGE_SOURCE_SCREEN_ROUTE) }
)
}
item {
CategorySettingsItem(text = stringResource(id = R.string.data_screen_sync_category))
}
item {
BaseSettingsItem(
icon = rememberVectorPainter(image = Icons.Default.ImportExport),
Expand All @@ -92,7 +86,15 @@ fun DataScreen(viewModel: DataViewModel = hiltViewModel()) {
)
}
item {
CategorySettingsItem(text = stringResource(id = R.string.data_screen_delete_category))
BaseSettingsItem(
icon = rememberVectorPainter(image = Icons.Default.Cached),
text = stringResource(id = R.string.cache_screen_name),
descriptionText = stringResource(id = R.string.cache_screen_description),
onClick = { navController.navigate(CACHE_SCREEN_ROUTE) }
)
}
item {
CategorySettingsItem(text = stringResource(id = R.string.data_screen_danger_category))
}
item {
BaseSettingsItem(
Expand All @@ -102,14 +104,6 @@ fun DataScreen(viewModel: DataViewModel = hiltViewModel()) {
onClick = { openDeleteWarningDialog = true }
)
}
item {
BaseSettingsItem(
icon = rememberVectorPainter(image = Icons.Default.Cached),
text = stringResource(id = R.string.cache_screen_name),
descriptionText = stringResource(id = R.string.cache_screen_description),
onClick = { navController.navigate(CACHE_SCREEN_ROUTE) }
)
}
}

when (val event = uiEvent) {
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,5 @@
<string name="cache_screen_delete_provider_thumbnails">删除缩略图缓存</string>
<string name="cache_screen_delete_provider_thumbnails_description">删除表情包提供者缩略图缓存</string>
<string name="cache_screen_delete_provider_thumbnails_success">已删除缩略图缓存, 花费 %.2f 秒</string>
<string name="data_screen_sync_category">同步</string>
<string name="data_screen_delete_category">删除</string>
<string name="data_screen_source_category">来源</string>
<string name="data_screen_danger_category">危险区域</string>
</resources>
4 changes: 1 addition & 3 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,5 @@
<string name="cache_screen_delete_provider_thumbnails">刪除縮圖快取</string>
<string name="cache_screen_delete_provider_thumbnails_description">刪除貼紙提供者縮圖快取</string>
<string name="cache_screen_delete_provider_thumbnails_success">所有縮圖均已清除,已花費 %.2f 秒</string>
<string name="data_screen_sync_category">同步</string>
<string name="data_screen_delete_category">刪除</string>
<string name="data_screen_source_category">來源</string>
<string name="data_screen_danger_category">危險區域</string>
</resources>
4 changes: 1 addition & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,5 @@
<string name="cache_screen_delete_provider_thumbnails">Delete thumbnails</string>
<string name="cache_screen_delete_provider_thumbnails_description">Delete sticker provider thumbnail caches</string>
<string name="cache_screen_delete_provider_thumbnails_success">All thumbnails cleared, spent %.2fs</string>
<string name="data_screen_sync_category">Sync</string>
<string name="data_screen_delete_category">Delete</string>
<string name="data_screen_source_category">Source</string>
<string name="data_screen_danger_category">Danger area</string>
</resources>

0 comments on commit 960611a

Please sign in to comment.