Skip to content

Commit

Permalink
[feature] Support delete mime type cache; update classification model…
Browse files Browse the repository at this point in the history
…; support check APNG format
  • Loading branch information
SkyD666 committed Apr 14, 2024
1 parent daf5b50 commit 7ec71f5
Show file tree
Hide file tree
Showing 18 changed files with 166 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ android {
applicationId = "com.skyd.rays"
minSdk = 24
targetSdk = 34
versionCode = 64
versionName = "2.2-rc01"
versionCode = 65
versionName = "2.2-rc02"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/assets/stickerclassification/lang/en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"丁真": "Tenzing Tsondu",
"东雪莲": "Doraemon",
"干物妹小埋": "Umaru Doma",
"孤独摇滚": "Bocchi the Rock!",
"古明地觉": "Satori Komeiji",
"乖宝宝": "Good baby",
"和泉纱雾": "Sagiri Izumi",
Expand Down Expand Up @@ -50,5 +49,13 @@
"熊猫人": "Chinese Panda Meme",
"永雏塔菲": "Ace Taffy",
"原神": "Genshin",
"中二病也要谈恋爱": "Love, Chunibyo & Other Delusions"
"中二病也要谈恋爱": "Love, Chunibyo & Other Delusions",
"什么猫": "Shenmemao",
"咖波": "Capoo",
"铸币大头": "Pig head",
"惠惠": "Megumin",
"山田凉": "Yamada Ryō",
"波奇酱": "Bocchi",
"伊地知虹夏": "Ijichi Nijika",
"喜多": "Kita Ikuyo"
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/skyd/rays/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const val GITHUB_REPO = "https://github.com/SkyD666/Rays-Android"

const val WEBLATE_URL = "https://hosted.weblate.org/engage/rays/"

const val ANIVU_URL = "https://github.com/SkyD666/AniVu"

const val RACA_ANDROID_URL = "https://github.com/SkyD666/Raca-Android"

const val NIGHT_SCREEN_URL = "https://github.com/SkyD666/NightScreen"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ interface MimeTypeDao {
)
fun delete(stickerUuid: String)

@Transaction
@Query("DELETE FROM $MIME_TYPE_TABLE_NAME")
fun deleteAll()

@Transaction
fun getMimeTypeOrNull(
stickerUuid: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.skyd.rays.appContext
import com.skyd.rays.base.BaseRepository
import com.skyd.rays.config.PROVIDER_THUMBNAIL_DIR
import com.skyd.rays.model.db.dao.cache.StickerShareTimeDao
import com.skyd.rays.model.db.dao.sticker.MimeTypeDao
import com.skyd.rays.model.db.dao.sticker.StickerDao
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
Expand All @@ -12,6 +13,7 @@ import kotlin.system.measureTimeMillis
class DataRepository @Inject constructor(
private val stickerDao: StickerDao,
private val stickerShareTimeDao: StickerShareTimeDao,
private val mimeTypeDao: MimeTypeDao,
) : BaseRepository() {
suspend fun requestDeleteAllData(): Flow<Long> {
return flowOnIo {
Expand All @@ -30,4 +32,10 @@ class DataRepository @Inject constructor(
emit(measureTimeMillis { appContext.PROVIDER_THUMBNAIL_DIR.deleteRecursively() })
}
}

suspend fun requestDeleteAllMimetypes(): Flow<Long> {
return flowOnIo {
emit(measureTimeMillis { mimeTypeDao.deleteAll() })
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.skyd.rays.R
import com.skyd.rays.config.ANIVU_URL
import com.skyd.rays.config.GITHUB_REPO
import com.skyd.rays.config.NIGHT_SCREEN_URL
import com.skyd.rays.config.RACA_ANDROID_URL
Expand Down Expand Up @@ -424,6 +425,12 @@ private fun rememberOtherWorksList(): List<OtherWorksBean> {
val context = LocalContext.current
return remember {
listOf(
OtherWorksBean(
name = context.getString(R.string.about_screen_other_works_anivu_name),
icon = R.drawable.ic_anivu,
description = context.getString(R.string.about_screen_other_works_anivu_description),
url = ANIVU_URL,
),
OtherWorksBean(
name = context.getString(R.string.about_screen_other_works_raca_name),
icon = R.drawable.ic_raca,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ sealed interface CacheEvent : MviSingleEvent {
sealed interface DeleteDocumentsProviderThumbnailsResultEvent : CacheEvent {
data class Success(val time: Long) : DeleteDocumentsProviderThumbnailsResultEvent
}

sealed interface DeleteAllMimetypesResultEvent : CacheEvent {
data class Success(val time: Long) : DeleteAllMimetypesResultEvent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import com.skyd.rays.base.mvi.MviIntent
sealed interface CacheIntent : MviIntent {
data object Init : CacheIntent
data object DeleteDocumentsProviderThumbnails : CacheIntent
data object DeleteAllMimetypes : CacheIntent
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ internal sealed interface CachePartialStateChange {
oldState.copy(loadingDialog = false)
}
}

sealed interface DeleteAllMimetypes : CachePartialStateChange {
data class Success(val time: Long) : DeleteAllMimetypes {
override fun reduce(oldState: CacheState): CacheState =
oldState.copy(loadingDialog = false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.skyd.rays.ui.screen.settings.data.cache
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DeleteOutline
import androidx.compose.material.icons.filled.Gif
import androidx.compose.material.icons.filled.Image
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
Expand Down Expand Up @@ -57,12 +58,20 @@ fun CacheScreen(viewModel: CacheViewModel = hiltViewModel()) {
) {
item {
BaseSettingsItem(
icon = rememberVectorPainter(image = Icons.Default.DeleteOutline),
icon = rememberVectorPainter(image = Icons.Default.Image),
text = stringResource(id = R.string.cache_screen_delete_provider_thumbnails),
descriptionText = stringResource(id = R.string.cache_screen_delete_provider_thumbnails_description),
onClick = { dispatch(CacheIntent.DeleteDocumentsProviderThumbnails) }
)
}
item {
BaseSettingsItem(
icon = rememberVectorPainter(image = Icons.Default.Gif),
text = stringResource(id = R.string.cache_screen_delete_all_mimetypes),
descriptionText = stringResource(id = R.string.cache_screen_delete_all_mimetypes_description),
onClick = { dispatch(CacheIntent.DeleteAllMimetypes) }
)
}
}

when (val event = uiEvent) {
Expand All @@ -76,6 +85,16 @@ fun CacheScreen(viewModel: CacheViewModel = hiltViewModel()) {
)
}

is CacheEvent.DeleteAllMimetypesResultEvent.Success -> {
snackbarHostState.showSnackbarWithLaunchedEffect(
message = context.getString(
R.string.cache_screen_delete_all_mimetypes_success,
event.time / 1000.0f
),
key2 = event,
)
}

null -> Unit
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class CacheViewModel @Inject constructor(private var dataRepo: DataRepository) :
is CachePartialStateChange.DeleteDocumentsProviderThumbnails.Success ->
CacheEvent.DeleteDocumentsProviderThumbnailsResultEvent.Success(change.time)

is CachePartialStateChange.DeleteAllMimetypes.Success ->
CacheEvent.DeleteAllMimetypesResultEvent.Success(change.time)

else -> return@onEach
}
sendEvent(event)
Expand All @@ -68,6 +71,12 @@ class CacheViewModel @Inject constructor(private var dataRepo: DataRepository) :
.map { CachePartialStateChange.DeleteDocumentsProviderThumbnails.Success(it) }
.startWith(CachePartialStateChange.LoadingDialog)
},

filterIsInstance<CacheIntent.DeleteAllMimetypes>().flatMapConcat {
dataRepo.requestDeleteAllMimetypes()
.map { CachePartialStateChange.DeleteAllMimetypes.Success(it) }
.startWith(CachePartialStateChange.LoadingDialog)
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ sealed class FormatStandard(
) {
companion object {
val formatStandards by lazy {
arrayOf(PngFormat, JpgFormat, GifFormat, BmpFormat, WebpFormat, HeifFormat, HeicFormat)
arrayOf(
ApngFormat, // Should be before PngFormat
PngFormat,
JpgFormat,
GifFormat,
BmpFormat,
WebpFormat,
HeifFormat,
HeicFormat
)
}
}

Expand All @@ -34,6 +43,37 @@ sealed class FormatStandard(
return check(buffer) to buffer
}

data object ApngFormat : FormatStandard(
format = ImageFormat.APNG,
requiredByteArraySize = 41,
) {
override fun check(tested: ByteArray): Boolean {
// Return false if the image is not png
if (tested.size < 12 || !PngFormat.check(tested)) {
return false
}
// Get IHDR length, in fact it should be decimal 13
var ihdrLength = 0
for (i in 8..11) {
ihdrLength = ihdrLength shl 8 or tested[i].toInt()
}

/**
* 8: PNG format
* 4: 4 bytes to store the length of the next part (IHDR)
* 4: Chunk Type (IHDR)
* 13: IHDR length (ihdrLength)
* 4: CRC32
* 4: 4 bytes to store the length of the next part (acTL)
*/
val startIndex = 8 + 4 + 4 + /*13*/ ihdrLength + 4 + 4
return baseCheck(
byteArrayOf(0x61, 0x63, 0x54, 0x4C),
tested.copyOfRange(startIndex, startIndex + 4)
)
}
}

data object PngFormat : FormatStandard(
format = ImageFormat.PNG,
requiredByteArraySize = 8,
Expand All @@ -48,6 +88,7 @@ sealed class FormatStandard(
0x1A.toByte(),
0x0A.toByte(),
)

override fun check(tested: ByteArray): Boolean = baseCheck(
standard = PNG_FORMAT_DATA,
tested = tested,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.skyd.rays.util.image.format
enum class ImageFormat {
JPG,
PNG,
APNG,
GIF,
WEBP,
BMP,
Expand All @@ -14,6 +15,7 @@ enum class ImageFormat {
return when (this) {
JPG -> ".jpg"
PNG -> ".png"
APNG -> ".apng"
GIF -> ".gif"
WEBP -> ".webp"
BMP -> ".bmp"
Expand All @@ -27,6 +29,7 @@ enum class ImageFormat {
return when (this) {
JPG -> "image/jpg"
PNG -> "image/png"
APNG -> "image/apng"
GIF -> "image/gif"
WEBP -> "image/webp"
BMP -> "image/bmp"
Expand All @@ -40,6 +43,7 @@ enum class ImageFormat {
fun fromMimeType(mimeType: String): ImageFormat {
return when (mimeType) {
"image/jpg" -> JPG
"image/apng" -> APNG
"image/png" -> PNG
"image/gif" -> GIF
"image/webp" -> WEBP
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/drawable-night/ic_anivu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="256"
android:viewportHeight="256">
<path
android:fillColor="#000000"
android:pathData="M195,256H61c-33.69,0 -61,-27.31 -61,-61V61C0,27.31 27.31,0 61,0h134c33.69,0 61,27.31 61,61v134C256,228.69 228.69,256 195,256z" />
<path
android:fillColor="#FFFFFF"
android:pathData="M125.19,176.2c-3.94,0 -7.36,-5.05 -10.26,-15.14c-1.11,-3.85 -2.5,-10.58 -4.17,-20.21c-4.36,0.6 -10.07,1.67 -17.13,3.21l-17.06,3.53c-2.14,5.6 -5.8,13.79 -10.97,24.57c-1.37,2.39 -3.23,3.59 -5.58,3.59c-1.71,0 -3.24,-0.62 -4.59,-1.86c-1.35,-1.24 -2.02,-2.78 -2.02,-4.62c0,-2.05 3.23,-9.86 9.69,-23.42C62.37,144.74 62,143.46 62,142c0,-3.46 2.09,-5.69 6.29,-6.67c4.88,-9.15 11.03,-19.67 18.48,-31.56c10.14,-16.21 16.44,-24.31 18.92,-24.31c3.38,0 5.69,2.35 6.93,7.06l4.04,21.55l9.56,44.58l3.66,10.14c1.24,3.46 1.86,5.77 1.86,6.93c0,1.84 -0.66,3.38 -1.99,4.62C128.42,175.58 126.9,176.2 125.19,176.2zM102.55,102.87L84.2,132.45c5.17,-1.33 13.02,-2.93 23.54,-4.81L102.55,102.87z" />
<path
android:fillColor="#FFFFFF"
android:pathData="M179.59,170.94c-0.13,3.93 -2.07,5.9 -5.84,5.9c-3.12,0 -5.39,-1.84 -6.8,-5.52l-9.81,-26.04l-11.42,-29.45c-0.43,-1.11 -0.64,-2.09 -0.64,-2.95c0,-1.75 0.66,-3.25 1.99,-4.49c1.33,-1.24 2.87,-1.86 4.62,-1.86c2.57,0 4.4,1.31 5.52,3.91l16.29,44.84c3.21,-6.84 7.25,-16.76 12.12,-29.77c1.16,-3.42 2.97,-8.25 5.45,-14.5c1.33,-2.74 3.21,-4.11 5.65,-4.11c1.75,0 3.29,0.62 4.62,1.86c1.33,1.24 1.99,2.72 1.99,4.43c0,1.63 -3.53,10.78 -10.58,27.46L179.59,170.94z" />
</vector>
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/ic_anivu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="256"
android:viewportHeight="256">
<path
android:fillColor="#FFFFFF"
android:pathData="M195,256H61c-33.69,0 -61,-27.31 -61,-61V61C0,27.31 27.31,0 61,0h134c33.69,0 61,27.31 61,61v134C256,228.69 228.69,256 195,256z" />
<path
android:fillColor="#FF000000"
android:pathData="M125.19,176.2c-3.94,0 -7.36,-5.05 -10.26,-15.14c-1.11,-3.85 -2.5,-10.58 -4.17,-20.21c-4.36,0.6 -10.07,1.67 -17.13,3.21l-17.06,3.53c-2.14,5.6 -5.8,13.79 -10.97,24.57c-1.37,2.39 -3.23,3.59 -5.58,3.59c-1.71,0 -3.24,-0.62 -4.59,-1.86c-1.35,-1.24 -2.02,-2.78 -2.02,-4.62c0,-2.05 3.23,-9.86 9.69,-23.42C62.37,144.74 62,143.46 62,142c0,-3.46 2.09,-5.69 6.29,-6.67c4.88,-9.15 11.03,-19.67 18.48,-31.56c10.14,-16.21 16.44,-24.31 18.92,-24.31c3.38,0 5.69,2.35 6.93,7.06l4.04,21.55l9.56,44.58l3.66,10.14c1.24,3.46 1.86,5.77 1.86,6.93c0,1.84 -0.66,3.38 -1.99,4.62C128.42,175.58 126.9,176.2 125.19,176.2zM102.55,102.87L84.2,132.45c5.17,-1.33 13.02,-2.93 23.54,-4.81L102.55,102.87z" />
<path
android:fillColor="#FF000000"
android:pathData="M179.59,170.94c-0.13,3.93 -2.07,5.9 -5.84,5.9c-3.12,0 -5.39,-1.84 -6.8,-5.52l-9.81,-26.04l-11.42,-29.45c-0.43,-1.11 -0.64,-2.09 -0.64,-2.95c0,-1.75 0.66,-3.25 1.99,-4.49c1.33,-1.24 2.87,-1.86 4.62,-1.86c2.57,0 4.4,1.31 5.52,3.91l16.29,44.84c3.21,-6.84 7.25,-16.76 12.12,-29.77c1.16,-3.42 2.97,-8.25 5.45,-14.5c1.33,-2.74 3.21,-4.11 5.65,-4.11c1.75,0 3.29,0.62 4.62,1.86c1.33,1.24 1.99,2.72 1.99,4.43c0,1.63 -3.53,10.78 -10.58,27.46L179.59,170.94z" />
</vector>
6 changes: 5 additions & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,12 @@
<string name="cache_screen_description">删除缓存数据等</string>
<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="cache_screen_delete_provider_thumbnails_success">已删除缩略图缓存花费 %.2f 秒</string>
<string name="data_screen_danger_category">危险区域</string>
<string name="data_screen_delete_all_sticker_share_time_data">删除分享数据</string>
<string name="data_screen_delete_all_sticker_share_time_data_description">删除分享表情包操作的统计数据,例如每次分享的时间</string>
<string name="about_screen_other_works_anivu_description">一个集 RSS 番剧订阅与更新、比特洪流下载、视频播放为一体的工具。</string>
<string name="cache_screen_delete_all_mimetypes">删除 Mimetype</string>
<string name="cache_screen_delete_all_mimetypes_description">删除所有已缓存的表情包 Mimetype 数据</string>
<string name="cache_screen_delete_all_mimetypes_success">已删除 Mimetype 数据,花费 %.2f 秒</string>
</resources>
6 changes: 5 additions & 1 deletion app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,12 @@
<string name="cache_screen_description">刪除快取資料等</string>
<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="cache_screen_delete_provider_thumbnails_success">所有縮圖均已清除,花費了 %.2f 秒</string>
<string name="data_screen_danger_category">危險區域</string>
<string name="data_screen_delete_all_sticker_share_time_data">刪除分享數據</string>
<string name="data_screen_delete_all_sticker_share_time_data_description">刪除表情包分享統計訊息,例如每次分享的時間</string>
<string name="about_screen_other_works_anivu_description">一款用於 RSS 動漫訂閱和更新、Bit Torrent 下載和影片播放的一體化工具。</string>
<string name="cache_screen_delete_all_mimetypes">刪除 Mimetype</string>
<string name="cache_screen_delete_all_mimetypes_description">刪除快取的 Mimetype 數據</string>
<string name="cache_screen_delete_all_mimetypes_success">所有 Mimetype 已刪除,花費了 %.2f 秒</string>
</resources>
7 changes: 6 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
<string name="auto_share_screen_accessibility_granted">Accessibility permission granted now</string>
<string name="auto_share_screen_accessibility_not_granted">No accessibility permission granted now</string>
<string name="auto_share_screen_supported_app">Auto share allows you to share a sticker directly to the application corresponding to the previous page.\n\nThis feature requires accessibility permission. After killing the Rays process, upgrading or reinstalling Rays, Auto share may be disabled and needs to be enabled by manually granting accessibility permission. \n\nIt is recommended that Rays should be allowed to launch on startup, launch by other apps and keep running in background.\n\nSupported apps:\nTelegram\nDiscord\nWeChat\nInstagram\nQQ\nWhatsApp\nWeibo</string>
<string name="help_translate">Help translate</string>
<string name="help_translate">Translate</string>
<string name="sponsor">Sponsor</string>
<string name="sponsor_description">If you like the app, you can buy me a cup of coffee, thanks!</string>
<string name="sponsor_afadian">Afadian</string>
Expand Down Expand Up @@ -342,4 +342,9 @@
<string name="data_screen_danger_category">Danger area</string>
<string name="data_screen_delete_all_sticker_share_time_data">Delete share data</string>
<string name="data_screen_delete_all_sticker_share_time_data_description">Delete sticker share statistics, e.g. time of each share</string>
<string name="about_screen_other_works_anivu_name" translatable="false">AniVu</string>
<string name="about_screen_other_works_anivu_description">An all-in-one tool for RSS anime subscription and updates, bit torrent downloads, and video playback.</string>
<string name="cache_screen_delete_all_mimetypes">Delete mimetypes</string>
<string name="cache_screen_delete_all_mimetypes_description">Delete cached Mimetype data</string>
<string name="cache_screen_delete_all_mimetypes_success">All Mimetype deleted, spent %.2fs</string>
</resources>

0 comments on commit 7ec71f5

Please sign in to comment.