@@ -13,7 +13,6 @@ import kotlinx.serialization.encodeToString
13
13
import kotlinx.serialization.json.Json
14
14
import org.fcitx.fcitx5.android.BuildConfig
15
15
import org.fcitx.fcitx5.android.core.data.DataManager.dataDir
16
- import org.fcitx.fcitx5.android.utils.Const
17
16
import org.fcitx.fcitx5.android.utils.FileUtil
18
17
import org.fcitx.fcitx5.android.utils.appContext
19
18
import org.fcitx.fcitx5.android.utils.javaIdRegex
@@ -39,13 +38,13 @@ object DataManager {
39
38
var synced = false
40
39
private set
41
40
42
- // should be consistent with the deserialization in build.gradle.kts (:app )
43
- private fun deserializeDataDescriptor (raw : String ) = runCatching {
44
- json.decodeFromString<DataDescriptor >(raw)
41
+ // should be consistent with the deserialization in DataDescriptorPlugin (:build-logic )
42
+ private fun deserializeDataDescriptor (raw : String ): DataDescriptor {
43
+ return json.decodeFromString<DataDescriptor >(raw)
45
44
}
46
45
47
- private fun serializeDataDescriptor (descriptor : DataDescriptor ) = runCatching {
48
- json.encodeToString(descriptor)
46
+ private fun serializeDataDescriptor (descriptor : DataDescriptor ): String {
47
+ return json.encodeToString(descriptor)
49
48
}
50
49
51
50
// If Android version supports direct boot, we put the hierarchy in device encrypted storage
@@ -57,14 +56,12 @@ object DataManager {
57
56
File (appContext.applicationInfo.dataDir)
58
57
}
59
58
60
- private val destDescriptorFile = File (dataDir, Const .dataDescriptorName)
61
-
62
- private fun AssetManager.getDataDescriptor () =
63
- open(Const .dataDescriptorName)
59
+ private fun AssetManager.getDataDescriptor (): DataDescriptor {
60
+ return open(BuildConfig .DATA_DESCRIPTOR_NAME )
64
61
.bufferedReader()
65
62
.use { it.readText() }
66
63
.let { deserializeDataDescriptor(it) }
67
- .getOrThrow()
64
+ }
68
65
69
66
private val loadedPlugins = mutableSetOf<PluginDescriptor >()
70
67
private val failedPlugins = mutableMapOf<String , PluginLoadFailed >()
@@ -102,6 +99,7 @@ object DataManager {
102
99
// Parse plugin.xml
103
100
for (packageName in pluginPackages) {
104
101
val res = pm.getResourcesForApplication(packageName)
102
+
105
103
@SuppressLint(" DiscouragedApi" )
106
104
val resId = res.getIdentifier(" plugin" , " xml" , packageName)
107
105
if (resId == 0 ) {
@@ -201,24 +199,15 @@ object DataManager {
201
199
loadedPlugins.clear()
202
200
failedPlugins.clear()
203
201
202
+ val destDescriptorFile = File (dataDir, BuildConfig .DATA_DESCRIPTOR_NAME )
203
+
204
204
// load last run's data descriptor
205
- val oldDescriptor =
206
- destDescriptorFile
207
- .takeIf { it.exists() && it.isFile }
208
- ?.runCatching { readText() }
209
- ?.getOrNull()
210
- ?.let { deserializeDataDescriptor(it) }
211
- ?.getOrNull()
212
- ? : DataDescriptor (" " , mapOf (), mapOf ())
205
+ val oldDescriptor = destDescriptorFile
206
+ .runCatching { deserializeDataDescriptor(bufferedReader().use { it.readText() }) }
207
+ .getOrElse { DataDescriptor (" " , emptyMap(), emptyMap()) }
213
208
214
209
// load app's data descriptor
215
- val mainDescriptor =
216
- appContext.assets
217
- .open(Const .dataDescriptorName)
218
- .bufferedReader()
219
- .use { it.readText() }
220
- .let { deserializeDataDescriptor(it) }
221
- .getOrThrow()
210
+ val mainDescriptor = appContext.assets.getDataDescriptor()
222
211
223
212
val (parsedDescriptors, failed) = detectPlugins()
224
213
failedPlugins.putAll(failed)
@@ -290,13 +279,15 @@ object DataManager {
290
279
}
291
280
}
292
281
// save the new hierarchy as the data descriptor to be used in the next run
293
- destDescriptorFile.writeText(serializeDataDescriptor(newHierarchy.downToDataDescriptor()).getOrThrow())
282
+ destDescriptorFile.bufferedWriter().use {
283
+ it.write(serializeDataDescriptor(newHierarchy.downToDataDescriptor()))
284
+ }
294
285
callbacks.forEach { it() }
295
286
callbacks.clear()
296
287
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
297
288
// remove old assets from credential encrypted storage
298
289
val oldDataDir = appContext.dataDir
299
- val oldDataDescriptor = oldDataDir.resolve(Const .dataDescriptorName )
290
+ val oldDataDescriptor = oldDataDir.resolve(BuildConfig . DATA_DESCRIPTOR_NAME )
300
291
if (oldDataDescriptor.exists()) {
301
292
oldDataDescriptor.delete()
302
293
oldDataDir.resolve(" README.md" ).delete()
@@ -324,7 +315,7 @@ object DataManager {
324
315
325
316
fun deleteAndSync () {
326
317
lock.withLock {
327
- dataDir.resolve(Const .dataDescriptorName ).delete()
318
+ dataDir.resolve(BuildConfig . DATA_DESCRIPTOR_NAME ).delete()
328
319
dataDir.resolve(" README.md" ).delete()
329
320
dataDir.resolve(" usr" ).deleteRecursively()
330
321
}
0 commit comments