Skip to content

Commit

Permalink
[optimize|fix] Optimize AIDL arguments; fix sticker color theme bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Apr 13, 2024
1 parent b505bc0 commit daf5b50
Show file tree
Hide file tree
Showing 5 changed files with 46 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 = 63
versionName = "2.2-beta04"
versionCode = 64
versionName = "2.2-rc01"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/aidl/com/skyd/rays/api/IApiService.aidl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.skyd.rays.api;


interface IApiService {
String searchStickers(String requestPackage, String keyword);
String searchStickers(String requestPackage, String keyword, int startIndex, int size);
}
46 changes: 42 additions & 4 deletions app/src/main/java/com/skyd/rays/api/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.skyd.rays.api
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.os.Parcelable
import com.skyd.rays.api.strategy.SearchStickersStrategy
import com.skyd.rays.appContext
import com.skyd.rays.ext.dataStore
Expand All @@ -12,6 +13,8 @@ import com.skyd.rays.model.preference.ApiGrantPreference
import com.skyd.rays.model.respository.SearchRepository
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.runBlocking
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import javax.inject.Inject
Expand All @@ -25,7 +28,12 @@ class ApiService : Service() {
lateinit var json: Json

inner class ApiServiceBinder : IApiService.Stub() {
override fun searchStickers(requestPackage: String?, keyword: String?) = runBlocking {
override fun searchStickers(
requestPackage: String?,
keyword: String?,
startIndex: Int,
size: Int,
) = runBlocking {
with(AppDatabase.getInstance(appContext)) {
if (requestPackage.isNullOrBlank() || !appContext.dataStore.getOrDefault(
ApiGrantPreference
Expand All @@ -35,11 +43,41 @@ class ApiService : Service() {
}
}

json.encodeToString(
SearchStickersStrategy.execute(searchRepository, keyword, requestPackage!!)
)
var dataList: List<ApiStickerWithTags> = if (startIndex < 0 || size <= 0) emptyList()
else SearchStickersStrategy.execute(searchRepository, keyword, requestPackage!!)
val safeStartIndex =
if (dataList.isEmpty()) 0 else startIndex.coerceIn(0, dataList.size - 1)
val safeEndIndex =
if (dataList.isEmpty()) 0 else (startIndex + size).coerceIn(1, dataList.size)
if (dataList.isNotEmpty()) {
dataList = dataList.subList(safeStartIndex, safeEndIndex)
}

// The Binder transaction buffer has a limited fixed size, currently 1MB
var jsonString: String
var dataListSize = dataList.size
do {
jsonString = json.encodeToString(
ResultWrapper(
data = if (dataList.isEmpty()) dataList
else dataList.subList(0, dataListSize),
startIndex = safeStartIndex,
size = dataListSize,
)
)
dataListSize /= 2
} while (jsonString.encodeToByteArray().size > 1024 * 1024)
jsonString
}
}

override fun onBind(intent: Intent): IBinder = ApiServiceBinder()

@Parcelize
@Serializable
data class ResultWrapper(
val startIndex: Int,
val size: Int,
val data: List<ApiStickerWithTags>,
) : Parcelable
}
2 changes: 0 additions & 2 deletions app/src/main/java/com/skyd/rays/ui/activity/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.skyd.rays.model.preference.theme.StickerColorThemePreference
import com.skyd.rays.model.preference.theme.ThemeNamePreference
import com.skyd.rays.util.stickerUuidToFile
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
Expand Down Expand Up @@ -91,7 +90,6 @@ class MainViewModel @Inject constructor() :
if (swatch == null) {
return
}
delay(500)
appContext.dataStore.edit { pref ->
pref[ThemeNamePreference.key] = ThemeNamePreference.CUSTOM_THEME_NAME
pref[CustomPrimaryColorPreference.key] = Integer.toHexString(swatch.rgb)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/skyd/rays/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fun RaysTheme(
val themeName = LocalThemeName.current

MaterialTheme(
colorScheme = remember(themeName) {
colorScheme = remember(themeName, LocalCustomPrimaryColor.current) {
wallpaperColors.getOrElse(themeName) {
dynamicColorScheme(
seedColor = ThemeNamePreference.values[0].keyColor,
Expand Down

0 comments on commit daf5b50

Please sign in to comment.