@onekeyfe/react-native-image 6.7.0-6 → 6.7.0-60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/OneKeyImage.podspec +1 -1
- package/README.md +147 -77
- package/android/build.gradle +4 -1
- package/android/src/main/AndroidManifest.xml +12 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyBlockieAvatarLoader.kt +1 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyIcon.kt +97 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImage.kt +240 -6
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageCache.kt +16 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideModule.kt +17 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideRegistry.kt +20 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImagePackage.kt +2 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageSafety.kt +42 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupPrewarm.kt +1106 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupProvider.kt +54 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMainThreadDecode.kt +541 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMediaPreviewCache.kt +199 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyNamedSVG.kt +914 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeySVG.kt +264 -0
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyBlockieAvatarLoaderTest.kt +2 -1
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyImageSafetyTest.kt +1 -1
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeySVGForeignObjectTest.kt +130 -0
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyStartupPrewarmTest.kt +600 -0
- package/ios/OneKeyAvatarImageLoader.swift +1 -1
- package/ios/OneKeyIcon.swift +96 -0
- package/ios/OneKeyImage.swift +85 -2
- package/ios/OneKeyImageCache.swift +25 -5
- package/ios/OneKeyImageCoderBridge.h +4 -0
- package/ios/OneKeyImageCoderBridge.m +402 -1
- package/ios/OneKeyImageRequestContext.swift +17 -3
- package/ios/OneKeyImageStartupPrewarm.swift +602 -0
- package/ios/OneKeyMainThreadDecode.swift +338 -0
- package/ios/OneKeyMediaPreviewCache.swift +209 -0
- package/ios/OneKeyNamedSVG.swift +710 -0
- package/ios/tests/OneKeyIconTests.swift +725 -0
- package/ios/tests/OneKeyImageInfrastructureTests.swift +614 -3
- package/ios/tests/OneKeyMediaPreviewCacheTests.swift +139 -0
- package/ios/tests/OneKeySVGForeignObjectTests.swift +267 -0
- package/lib/module/OneKeyIcon.nitro.js +4 -0
- package/lib/module/icon/assetUrls.js +13 -0
- package/lib/module/icon/dom.js +21 -0
- package/lib/module/icon/index.js +29 -0
- package/lib/module/icon/index.web.js +32 -0
- package/lib/module/icon/props.js +22 -0
- package/lib/module/index.js +5 -1
- package/lib/module/package.json +6 -1
- package/lib/nitrogen/generated/android/c++/JHybridOneKeyIconSpec.cpp +67 -0
- package/lib/nitrogen/generated/android/c++/JHybridOneKeyIconSpec.hpp +66 -0
- package/lib/nitrogen/generated/android/c++/views/JHybridOneKeyIconStateUpdater.cpp +79 -0
- package/lib/nitrogen/generated/android/c++/views/JHybridOneKeyIconStateUpdater.hpp +54 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/HybridOneKeyIconSpec.kt +65 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/views/HybridOneKeyIconManager.kt +96 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/views/HybridOneKeyIconStateUpdater.kt +23 -0
- package/lib/nitrogen/generated/android/onekeyimage+autolinking.cmake +4 -0
- package/lib/nitrogen/generated/android/onekeyimageOnLoad.cpp +18 -0
- package/lib/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Bridge.cpp +17 -0
- package/lib/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Bridge.hpp +32 -15
- package/lib/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Umbrella.hpp +5 -0
- package/lib/nitrogen/generated/ios/OneKeyImageAutolinking.mm +8 -0
- package/lib/nitrogen/generated/ios/OneKeyImageAutolinking.swift +12 -0
- package/lib/nitrogen/generated/ios/c++/HybridOneKeyIconSpecSwift.cpp +11 -0
- package/lib/nitrogen/generated/ios/c++/HybridOneKeyIconSpecSwift.hpp +89 -0
- package/lib/nitrogen/generated/ios/c++/views/HybridOneKeyIconComponent.mm +150 -0
- package/lib/nitrogen/generated/ios/swift/HybridOneKeyIconSpec.swift +56 -0
- package/lib/nitrogen/generated/ios/swift/HybridOneKeyIconSpec_cxx.swift +193 -0
- package/lib/nitrogen/generated/shared/c++/HybridOneKeyIconSpec.cpp +24 -0
- package/lib/nitrogen/generated/shared/c++/HybridOneKeyIconSpec.hpp +66 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridOneKeyIconComponent.cpp +36 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridOneKeyIconComponent.hpp +88 -0
- package/lib/nitrogen/generated/shared/json/OneKeyIconConfig.json +11 -0
- package/lib/typescript/src/OneKeyIcon.nitro.d.ts +7 -0
- package/lib/typescript/src/icon/assetUrls.d.ts +2 -0
- package/lib/typescript/src/icon/dom.d.ts +3 -0
- package/lib/typescript/src/icon/index.d.ts +12 -0
- package/lib/typescript/src/icon/index.web.d.ts +6 -0
- package/lib/typescript/src/icon/props.d.ts +8 -0
- package/lib/typescript/src/index.d.ts +1 -1
- package/nitro.json +10 -0
- package/nitrogen/generated/android/c++/JHybridOneKeyIconSpec.cpp +67 -0
- package/nitrogen/generated/android/c++/JHybridOneKeyIconSpec.hpp +66 -0
- package/nitrogen/generated/android/c++/views/JHybridOneKeyIconStateUpdater.cpp +79 -0
- package/nitrogen/generated/android/c++/views/JHybridOneKeyIconStateUpdater.hpp +54 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/HybridOneKeyIconSpec.kt +65 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/views/HybridOneKeyIconManager.kt +96 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/onekeyimage/views/HybridOneKeyIconStateUpdater.kt +23 -0
- package/nitrogen/generated/android/onekeyimage+autolinking.cmake +4 -0
- package/nitrogen/generated/android/onekeyimageOnLoad.cpp +18 -0
- package/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Bridge.cpp +17 -0
- package/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Bridge.hpp +32 -15
- package/nitrogen/generated/ios/OneKeyImage-Swift-Cxx-Umbrella.hpp +5 -0
- package/nitrogen/generated/ios/OneKeyImageAutolinking.mm +8 -0
- package/nitrogen/generated/ios/OneKeyImageAutolinking.swift +12 -0
- package/nitrogen/generated/ios/c++/HybridOneKeyIconSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridOneKeyIconSpecSwift.hpp +89 -0
- package/nitrogen/generated/ios/c++/views/HybridOneKeyIconComponent.mm +150 -0
- package/nitrogen/generated/ios/swift/HybridOneKeyIconSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridOneKeyIconSpec_cxx.swift +193 -0
- package/nitrogen/generated/shared/c++/HybridOneKeyIconSpec.cpp +24 -0
- package/nitrogen/generated/shared/c++/HybridOneKeyIconSpec.hpp +66 -0
- package/nitrogen/generated/shared/c++/views/HybridOneKeyIconComponent.cpp +36 -0
- package/nitrogen/generated/shared/c++/views/HybridOneKeyIconComponent.hpp +88 -0
- package/nitrogen/generated/shared/json/OneKeyIconConfig.json +11 -0
- package/package.json +48 -15
- package/scripts/configure-icon-imports.mjs +7 -0
- package/scripts/icon-fixtures.test.mjs +398 -0
- package/src/OneKeyIcon.nitro.ts +8 -0
- package/src/icon/assetUrls.ts +13 -0
- package/src/icon/dom.ts +21 -0
- package/src/icon/index.tsx +38 -0
- package/src/icon/index.web.tsx +36 -0
- package/src/icon/props.ts +38 -0
- package/src/index.tsx +23 -2
|
@@ -0,0 +1,914 @@
|
|
|
1
|
+
package com.margelo.nitro.onekeyimage
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.content.res.AssetManager
|
|
5
|
+
import android.graphics.Bitmap
|
|
6
|
+
import android.graphics.BitmapFactory
|
|
7
|
+
import android.graphics.Canvas
|
|
8
|
+
import android.graphics.Color
|
|
9
|
+
import android.graphics.Paint
|
|
10
|
+
import android.graphics.PorterDuff
|
|
11
|
+
import android.graphics.PorterDuffColorFilter
|
|
12
|
+
import android.os.Handler
|
|
13
|
+
import android.os.Looper
|
|
14
|
+
import android.util.Log
|
|
15
|
+
import org.json.JSONArray
|
|
16
|
+
import org.json.JSONObject
|
|
17
|
+
import java.io.File
|
|
18
|
+
import java.io.FileOutputStream
|
|
19
|
+
import java.security.MessageDigest
|
|
20
|
+
import java.util.Locale
|
|
21
|
+
import java.util.concurrent.Executors
|
|
22
|
+
import java.util.concurrent.ScheduledFuture
|
|
23
|
+
import java.util.concurrent.TimeUnit
|
|
24
|
+
import java.util.concurrent.locks.ReentrantLock
|
|
25
|
+
import kotlin.math.roundToInt
|
|
26
|
+
|
|
27
|
+
/** Process-owned named resources; no React runtime or Activity is retained. */
|
|
28
|
+
object OneKeyNamedSVG {
|
|
29
|
+
enum class RecolorMode { FIXED, SINGLE, MULTIPLE, RASTER }
|
|
30
|
+
data class IconMetadata(
|
|
31
|
+
val width: Float,
|
|
32
|
+
val height: Float,
|
|
33
|
+
val mode: RecolorMode,
|
|
34
|
+
)
|
|
35
|
+
data class RenderResult(
|
|
36
|
+
val bitmap: Bitmap,
|
|
37
|
+
val mode: RecolorMode,
|
|
38
|
+
val sourceIdentity: String,
|
|
39
|
+
val maskAlpha: Int,
|
|
40
|
+
) {
|
|
41
|
+
fun canTint(color: String): Boolean {
|
|
42
|
+
val value = parseColor(color) ?: return false
|
|
43
|
+
return mode == RecolorMode.FIXED || mode == RecolorMode.SINGLE ||
|
|
44
|
+
(mode == RecolorMode.MULTIPLE && ((value ushr 24) and 0xff) == maskAlpha)
|
|
45
|
+
}
|
|
46
|
+
fun tintColor(color: String): Int? {
|
|
47
|
+
val value = parseColor(color) ?: return null
|
|
48
|
+
return when (mode) {
|
|
49
|
+
RecolorMode.SINGLE -> value
|
|
50
|
+
RecolorMode.MULTIPLE -> value or (0xff shl 24)
|
|
51
|
+
else -> null
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
internal fun finalBitmap(color: String): Bitmap {
|
|
55
|
+
val tint = tintColor(color) ?: return bitmap
|
|
56
|
+
if (tint == Color.BLACK) return bitmap
|
|
57
|
+
return try {
|
|
58
|
+
val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
|
|
59
|
+
val paint = Paint().apply { colorFilter = PorterDuffColorFilter(tint, PorterDuff.Mode.SRC_IN) }
|
|
60
|
+
Canvas(output).drawBitmap(bitmap, 0f, 0f, paint)
|
|
61
|
+
output
|
|
62
|
+
} catch (_: Throwable) {
|
|
63
|
+
bitmap
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
internal data class StartupEntry(
|
|
69
|
+
val name: String,
|
|
70
|
+
val color: String,
|
|
71
|
+
val width: Int,
|
|
72
|
+
val height: Int,
|
|
73
|
+
val scale: Float,
|
|
74
|
+
val launch: Int = 0,
|
|
75
|
+
) {
|
|
76
|
+
val identity: String
|
|
77
|
+
get() = "$name|$color|$width|$height|$scale"
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
internal data class StartupPrewarmMetrics(
|
|
81
|
+
val entryCount: Int = 0,
|
|
82
|
+
val decodedCount: Int = 0,
|
|
83
|
+
val manifestMs: Double = 0.0,
|
|
84
|
+
val totalMs: Double = 0.0,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
private const val STARTUP_MAX_ENTRIES = 96
|
|
88
|
+
private const val STARTUP_RECORD_DURATION_MS = 6_000L
|
|
89
|
+
private const val STARTUP_MAX_DURATION_MS = 60_000L
|
|
90
|
+
private const val STARTUP_MIN_SETTLED_COUNT = 8
|
|
91
|
+
private const val STARTUP_MAX_LAUNCH_AGE = 3
|
|
92
|
+
private const val STARTUP_FILE_NAME = "onekey-named-svg-startup-v1.json"
|
|
93
|
+
private const val DISK_RASTER_DIR_NAME = "onekey-named-svg-v1"
|
|
94
|
+
private const val MAX_DISK_RASTER_BYTES = 67_108_864L
|
|
95
|
+
|
|
96
|
+
private data class Request(
|
|
97
|
+
val assets: AssetManager?,
|
|
98
|
+
val name: String,
|
|
99
|
+
val color: String,
|
|
100
|
+
val width: Int,
|
|
101
|
+
val height: Int,
|
|
102
|
+
val scale: Float,
|
|
103
|
+
val finalColor: Boolean,
|
|
104
|
+
val complete: (RenderResult?) -> Unit,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
private val lock = Any()
|
|
108
|
+
private val pending = LinkedHashMap<Any, Request>()
|
|
109
|
+
private val cache = LinkedHashMap<String, RenderResult>(128, 0.75f, true)
|
|
110
|
+
private var cacheBytes = 0L
|
|
111
|
+
private var activeWorkers = 0
|
|
112
|
+
@Volatile
|
|
113
|
+
private var manifest: JSONObject? = null
|
|
114
|
+
@Volatile
|
|
115
|
+
private var manifestFallbackMap: Map<String, ManifestEntry>? = null
|
|
116
|
+
private val manifestLock = Any()
|
|
117
|
+
private val manifestAccessLock = ReentrantLock()
|
|
118
|
+
private val workers = Executors.newFixedThreadPool(2)
|
|
119
|
+
private val diskExecutor = Executors.newSingleThreadScheduledExecutor { runnable ->
|
|
120
|
+
Thread(runnable, "onekey-named-svg-disk").apply { isDaemon = true }
|
|
121
|
+
}
|
|
122
|
+
private val main = try {
|
|
123
|
+
Looper.getMainLooper()?.let { Handler(it) }
|
|
124
|
+
} catch (_: Throwable) {
|
|
125
|
+
null
|
|
126
|
+
}
|
|
127
|
+
private val colorPattern = Regex("rgba\\(\\d{1,3},\\d{1,3},\\d{1,3},(?:0(?:\\.\\d+)?|1(?:\\.0+)?)\\)")
|
|
128
|
+
|
|
129
|
+
private var manifestMissingLogged = false
|
|
130
|
+
|
|
131
|
+
private val startupLock = Any()
|
|
132
|
+
private val startupRecorded = ArrayList<StartupEntry>()
|
|
133
|
+
private val startupRecordedKeys = HashSet<String>()
|
|
134
|
+
private var startupRecordStartTimeMs: Long? = null
|
|
135
|
+
private var startupRecordingClosed = false
|
|
136
|
+
private var startupFlushScheduled = false
|
|
137
|
+
private var startupDebounceFuture: ScheduledFuture<*>? = null
|
|
138
|
+
private var startupFileOverride: File? = null
|
|
139
|
+
@Volatile
|
|
140
|
+
private var baseCacheDir: File? = null
|
|
141
|
+
private var lastPrewarmMetrics = StartupPrewarmMetrics()
|
|
142
|
+
|
|
143
|
+
internal data class ManifestEntry(
|
|
144
|
+
val digest: String,
|
|
145
|
+
val mode: RecolorMode,
|
|
146
|
+
val width: Float = 24f,
|
|
147
|
+
val height: Float = 24f,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
@Volatile
|
|
151
|
+
internal var diskPngDecoderOverrideForTesting: ((File, Int, Int) -> Bitmap?)? = null
|
|
152
|
+
|
|
153
|
+
@Volatile
|
|
154
|
+
internal var bitmapByteCountOverrideForTesting: ((Bitmap) -> Long)? = null
|
|
155
|
+
|
|
156
|
+
internal fun setCacheDirIfNeeded(cacheDir: File?) {
|
|
157
|
+
if (cacheDir != null && baseCacheDir == null) {
|
|
158
|
+
baseCacheDir = cacheDir
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
internal fun startupMetricsSnapshot(): StartupPrewarmMetrics =
|
|
163
|
+
synchronized(startupLock) { lastPrewarmMetrics }
|
|
164
|
+
|
|
165
|
+
fun cancel(owner: Any) {
|
|
166
|
+
synchronized(lock) { pending.remove(owner) }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Caller holds manifestLock. Hosts own the resources, so a miss is logged once. */
|
|
170
|
+
private fun loadManifestLocked(assets: AssetManager?): JSONObject? {
|
|
171
|
+
manifest?.let { return it }
|
|
172
|
+
if (assets == null) return null
|
|
173
|
+
manifestAccessLock.lock()
|
|
174
|
+
return try {
|
|
175
|
+
manifest?.let { return it }
|
|
176
|
+
val raw = assets.open("icons/manifest.json").bufferedReader().use { it.readText() }
|
|
177
|
+
JSONObject(raw).also { manifest = it }
|
|
178
|
+
} catch (error: Exception) {
|
|
179
|
+
if (!manifestMissingLogged) {
|
|
180
|
+
manifestMissingLogged = true
|
|
181
|
+
try {
|
|
182
|
+
Log.w("OneKeyIcon", "assets/icons/manifest.json is missing or invalid; named icons will not render", error)
|
|
183
|
+
} catch (_: Throwable) {
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
null
|
|
187
|
+
} finally {
|
|
188
|
+
manifestAccessLock.unlock()
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private fun modeOf(metadata: JSONObject): RecolorMode = try {
|
|
193
|
+
RecolorMode.valueOf(metadata.optString("recolorMode", "raster").uppercase(Locale.ROOT))
|
|
194
|
+
} catch (_: IllegalArgumentException) {
|
|
195
|
+
RecolorMode.RASTER
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private fun lookupManifestEntryNonBlocking(name: String): ManifestEntry? {
|
|
199
|
+
if (!manifestAccessLock.tryLock()) return null
|
|
200
|
+
return try {
|
|
201
|
+
manifestFallbackMap?.get(name)?.let { return it }
|
|
202
|
+
val metadata = manifest?.optJSONObject(name) ?: return null
|
|
203
|
+
val digest = metadata.optString("digest").takeIf { it.isNotEmpty() } ?: return null
|
|
204
|
+
ManifestEntry(digest = digest, mode = modeOf(metadata))
|
|
205
|
+
} finally {
|
|
206
|
+
manifestAccessLock.unlock()
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private fun lookupManifestEntryBlocking(assets: AssetManager?, name: String): ManifestEntry? {
|
|
211
|
+
synchronized(manifestLock) {
|
|
212
|
+
manifestFallbackMap?.get(name)?.let { return it }
|
|
213
|
+
val metadata = loadManifestLocked(assets)?.optJSONObject(name) ?: return null
|
|
214
|
+
val digest = metadata.optString("digest").takeIf { it.isNotEmpty() } ?: return null
|
|
215
|
+
return ManifestEntry(digest = digest, mode = modeOf(metadata))
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private data class RenderKey(val identity: String, val key: String, val maskAlpha: Int)
|
|
220
|
+
|
|
221
|
+
private fun renderKey(
|
|
222
|
+
name: String,
|
|
223
|
+
digest: String,
|
|
224
|
+
width: Int,
|
|
225
|
+
height: Int,
|
|
226
|
+
scale: Float,
|
|
227
|
+
mode: RecolorMode,
|
|
228
|
+
requestColor: String,
|
|
229
|
+
color: Int,
|
|
230
|
+
): RenderKey {
|
|
231
|
+
val identity = "$name|$digest|$width|$height|$scale"
|
|
232
|
+
val maskAlpha = if (mode == RecolorMode.MULTIPLE) ((color ushr 24) and 0xff) else 255
|
|
233
|
+
val colorKey = when (mode) {
|
|
234
|
+
RecolorMode.RASTER -> requestColor
|
|
235
|
+
RecolorMode.MULTIPLE -> maskAlpha.toString()
|
|
236
|
+
else -> ""
|
|
237
|
+
}
|
|
238
|
+
return RenderKey(identity, "$identity|$mode|$colorKey", maskAlpha)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Main-thread fast path: returns an already-decoded result without asset I/O or
|
|
243
|
+
* manifest loading, so remounted and recycled views paint in the same frame.
|
|
244
|
+
*/
|
|
245
|
+
fun cachedRender(name: String, color: String, width: Int, height: Int, scale: Float): RenderResult? =
|
|
246
|
+
cachedRenderInternal(name, color, width, height, scale, recordStartup = true)
|
|
247
|
+
|
|
248
|
+
private fun cachedRenderInternal(
|
|
249
|
+
name: String,
|
|
250
|
+
color: String,
|
|
251
|
+
width: Int,
|
|
252
|
+
height: Int,
|
|
253
|
+
scale: Float,
|
|
254
|
+
recordStartup: Boolean,
|
|
255
|
+
): RenderResult? {
|
|
256
|
+
if (recordStartup) {
|
|
257
|
+
noteStartupRequest(name, color, width, height, scale)
|
|
258
|
+
}
|
|
259
|
+
val value = parseColor(color) ?: return null
|
|
260
|
+
val entry = lookupManifestEntryNonBlocking(name) ?: return null
|
|
261
|
+
val key = renderKey(name, entry.digest, width, height, scale, entry.mode, color, value).key
|
|
262
|
+
return synchronized(lock) { cache[key] }
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Synchronous fallback for memory cache misses: loads an already-rasterized icon
|
|
267
|
+
* from the disk PNG cache when within the main-thread decode limits and frame budget.
|
|
268
|
+
*/
|
|
269
|
+
fun diskRender(
|
|
270
|
+
assets: AssetManager?,
|
|
271
|
+
name: String,
|
|
272
|
+
color: String,
|
|
273
|
+
width: Int,
|
|
274
|
+
height: Int,
|
|
275
|
+
scale: Float,
|
|
276
|
+
): RenderResult? {
|
|
277
|
+
if (
|
|
278
|
+
width !in 1..OneKeyMainThreadDecode.MAX_ICON_PIXEL_SIZE ||
|
|
279
|
+
height !in 1..OneKeyMainThreadDecode.MAX_ICON_PIXEL_SIZE ||
|
|
280
|
+
!scale.isFinite() ||
|
|
281
|
+
scale <= 0f ||
|
|
282
|
+
!isValidColor(color)
|
|
283
|
+
) {
|
|
284
|
+
return null
|
|
285
|
+
}
|
|
286
|
+
return OneKeyMainThreadDecode.run {
|
|
287
|
+
if (!manifestAccessLock.tryLock()) {
|
|
288
|
+
OneKeyMainThreadDecode.noteLockContention()
|
|
289
|
+
return@run null
|
|
290
|
+
}
|
|
291
|
+
val hasManifest = try {
|
|
292
|
+
manifest != null || manifestFallbackMap != null
|
|
293
|
+
} finally {
|
|
294
|
+
manifestAccessLock.unlock()
|
|
295
|
+
}
|
|
296
|
+
if (!hasManifest) return@run null
|
|
297
|
+
decode(Request(assets, name, color, width, height, scale, false) {}, diskOnly = true)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
fun iconMetadata(assets: AssetManager, name: String): IconMetadata? {
|
|
302
|
+
val trimmed = name.trim()
|
|
303
|
+
if (trimmed.isEmpty()) return null
|
|
304
|
+
synchronized(manifestLock) {
|
|
305
|
+
manifestFallbackMap?.get(trimmed)?.let { entry ->
|
|
306
|
+
return IconMetadata(width = entry.width, height = entry.height, mode = entry.mode)
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
val metadata = try {
|
|
310
|
+
synchronized(manifestLock) {
|
|
311
|
+
loadManifestLocked(assets)?.optJSONObject(trimmed)
|
|
312
|
+
}
|
|
313
|
+
} catch (_: Exception) {
|
|
314
|
+
null
|
|
315
|
+
} ?: return null
|
|
316
|
+
val mode = modeOf(metadata)
|
|
317
|
+
val viewBox = metadata.optJSONArray("viewBox")
|
|
318
|
+
val vbWidth = if (viewBox != null && viewBox.length() >= 4) viewBox.optDouble(2, 24.0).toFloat() else 24f
|
|
319
|
+
val vbHeight = if (viewBox != null && viewBox.length() >= 4) viewBox.optDouble(3, 24.0).toFloat() else 24f
|
|
320
|
+
val width = if (vbWidth > 0f) vbWidth else 24f
|
|
321
|
+
val height = if (vbHeight > 0f) vbHeight else 24f
|
|
322
|
+
return IconMetadata(width = width, height = height, mode = mode)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
fun request(
|
|
326
|
+
owner: Any,
|
|
327
|
+
assets: AssetManager,
|
|
328
|
+
name: String,
|
|
329
|
+
color: String,
|
|
330
|
+
width: Int,
|
|
331
|
+
height: Int,
|
|
332
|
+
scale: Float,
|
|
333
|
+
complete: (Bitmap?) -> Unit,
|
|
334
|
+
) {
|
|
335
|
+
noteStartupRequest(name, color, width, height, scale)
|
|
336
|
+
if (OneKeyMainThreadDecode.isOnMainThread()) {
|
|
337
|
+
val hit = cachedRenderInternal(name, color, width, height, scale, recordStartup = false)
|
|
338
|
+
?: diskRender(assets, name, color, width, height, scale)
|
|
339
|
+
if (hit != null) {
|
|
340
|
+
cancel(owner)
|
|
341
|
+
complete(hit.finalBitmap(color))
|
|
342
|
+
return
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
enqueue(owner, assets, name, color, width, height, scale, true) { complete(it?.bitmap) }
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
fun requestRender(
|
|
349
|
+
owner: Any,
|
|
350
|
+
assets: AssetManager,
|
|
351
|
+
name: String,
|
|
352
|
+
color: String,
|
|
353
|
+
width: Int,
|
|
354
|
+
height: Int,
|
|
355
|
+
scale: Float,
|
|
356
|
+
complete: (RenderResult?) -> Unit,
|
|
357
|
+
) {
|
|
358
|
+
noteStartupRequest(name, color, width, height, scale)
|
|
359
|
+
if (OneKeyMainThreadDecode.isOnMainThread()) {
|
|
360
|
+
val hit = cachedRenderInternal(name, color, width, height, scale, recordStartup = false)
|
|
361
|
+
?: diskRender(assets, name, color, width, height, scale)
|
|
362
|
+
if (hit != null) {
|
|
363
|
+
cancel(owner)
|
|
364
|
+
complete(hit)
|
|
365
|
+
return
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
enqueue(owner, assets, name, color, width, height, scale, false, complete)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
private fun enqueue(
|
|
372
|
+
owner: Any,
|
|
373
|
+
assets: AssetManager,
|
|
374
|
+
name: String,
|
|
375
|
+
color: String,
|
|
376
|
+
width: Int,
|
|
377
|
+
height: Int,
|
|
378
|
+
scale: Float,
|
|
379
|
+
finalColor: Boolean,
|
|
380
|
+
complete: (RenderResult?) -> Unit,
|
|
381
|
+
) {
|
|
382
|
+
synchronized(lock) {
|
|
383
|
+
pending[owner] = Request(assets, name, color, width, height, scale, finalColor, complete)
|
|
384
|
+
while (activeWorkers < 2 && activeWorkers < pending.size) {
|
|
385
|
+
activeWorkers++
|
|
386
|
+
workers.execute { drain() }
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private fun parseColor(color: String): Int? {
|
|
392
|
+
if (!colorPattern.matches(color)) return null
|
|
393
|
+
val channels = color.removePrefix("rgba(").removeSuffix(")").split(',')
|
|
394
|
+
val rgb = channels.take(3).map { it.toInt() }
|
|
395
|
+
if (rgb.any { it !in 0..255 }) return null
|
|
396
|
+
val alpha = (channels[3].toDouble() * 255).roundToInt().coerceIn(0, 255)
|
|
397
|
+
return (alpha shl 24) or ((rgb[0] and 0xff) shl 16) or ((rgb[1] and 0xff) shl 8) or (rgb[2] and 0xff)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
fun isValidColor(color: String): Boolean = parseColor(color) != null
|
|
401
|
+
|
|
402
|
+
private fun drain() {
|
|
403
|
+
while (true) {
|
|
404
|
+
val request = synchronized(lock) {
|
|
405
|
+
val first = pending.entries.firstOrNull()
|
|
406
|
+
if (first == null) {
|
|
407
|
+
activeWorkers--
|
|
408
|
+
null
|
|
409
|
+
} else {
|
|
410
|
+
pending.remove(first.key)
|
|
411
|
+
first.value
|
|
412
|
+
}
|
|
413
|
+
} ?: return
|
|
414
|
+
val bitmap = try {
|
|
415
|
+
decode(request)?.let { result ->
|
|
416
|
+
if (request.finalColor) {
|
|
417
|
+
result.copy(bitmap = result.finalBitmap(request.color), mode = RecolorMode.RASTER)
|
|
418
|
+
} else {
|
|
419
|
+
result
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
} catch (_: Throwable) {
|
|
423
|
+
null
|
|
424
|
+
} // OOM must not strand activeWorkers.
|
|
425
|
+
val handler = main
|
|
426
|
+
if (handler != null) {
|
|
427
|
+
handler.post { request.complete(bitmap) }
|
|
428
|
+
} else {
|
|
429
|
+
request.complete(bitmap)
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
private fun decode(request: Request, diskOnly: Boolean = false): RenderResult? {
|
|
435
|
+
if (
|
|
436
|
+
request.width !in 1..4096 ||
|
|
437
|
+
request.height !in 1..4096 ||
|
|
438
|
+
request.width.toLong() * request.height > 4_194_304L ||
|
|
439
|
+
!request.scale.isFinite() ||
|
|
440
|
+
request.scale <= 0f
|
|
441
|
+
) {
|
|
442
|
+
return null
|
|
443
|
+
}
|
|
444
|
+
val color = parseColor(request.color) ?: return null
|
|
445
|
+
val manifestEntry = if (diskOnly) {
|
|
446
|
+
lookupManifestEntryNonBlocking(request.name)
|
|
447
|
+
} else {
|
|
448
|
+
lookupManifestEntryBlocking(request.assets, request.name)
|
|
449
|
+
} ?: return null
|
|
450
|
+
val mode = manifestEntry.mode
|
|
451
|
+
val (sourceIdentity, key, maskAlpha) = renderKey(
|
|
452
|
+
request.name,
|
|
453
|
+
manifestEntry.digest,
|
|
454
|
+
request.width,
|
|
455
|
+
request.height,
|
|
456
|
+
request.scale,
|
|
457
|
+
mode,
|
|
458
|
+
request.color,
|
|
459
|
+
color,
|
|
460
|
+
)
|
|
461
|
+
synchronized(lock) { cache[key]?.let { return it } }
|
|
462
|
+
|
|
463
|
+
val diskFile = diskRasterFile(key)
|
|
464
|
+
if (diskFile != null) {
|
|
465
|
+
val fileExists = diskFile.isFile
|
|
466
|
+
if (diskOnly && !fileExists) {
|
|
467
|
+
OneKeyMainThreadDecode.noteDiskMiss()
|
|
468
|
+
return null
|
|
469
|
+
}
|
|
470
|
+
if (fileExists) {
|
|
471
|
+
val length = diskFile.length()
|
|
472
|
+
if (diskOnly && (length <= 0L || length > OneKeyMainThreadDecode.MAX_ENCODED_BYTES)) {
|
|
473
|
+
OneKeyMainThreadDecode.noteLimitRejection()
|
|
474
|
+
return null
|
|
475
|
+
}
|
|
476
|
+
val decodedBitmap = decodeDiskPng(diskFile, request.width, request.height)
|
|
477
|
+
if (decodedBitmap != null) {
|
|
478
|
+
val result = RenderResult(decodedBitmap, mode, sourceIdentity, maskAlpha)
|
|
479
|
+
insertMemoryCache(key, result)
|
|
480
|
+
return result
|
|
481
|
+
} else if (diskOnly) {
|
|
482
|
+
OneKeyMainThreadDecode.noteDecodeFailure()
|
|
483
|
+
return null
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
if (diskOnly) return null
|
|
488
|
+
|
|
489
|
+
val assets = request.assets ?: return null
|
|
490
|
+
val paintColor = when (mode) {
|
|
491
|
+
RecolorMode.FIXED, RecolorMode.SINGLE -> Color.BLACK
|
|
492
|
+
RecolorMode.MULTIPLE -> Color.argb(maskAlpha, 0, 0, 0)
|
|
493
|
+
RecolorMode.RASTER -> color
|
|
494
|
+
}
|
|
495
|
+
// AndroidSVG rgba quantizes with 256; SVG hex alpha preserves the exact CSS 8-bit channel.
|
|
496
|
+
val svgColor = listOf(
|
|
497
|
+
Color.red(paintColor),
|
|
498
|
+
Color.green(paintColor),
|
|
499
|
+
Color.blue(paintColor),
|
|
500
|
+
Color.alpha(paintColor),
|
|
501
|
+
).joinToString(separator = "", prefix = "#") { it.toString(16).padStart(2, '0') }
|
|
502
|
+
val xml = assets.open("icons/${request.name}.svg").bufferedReader().use { it.readText() }
|
|
503
|
+
.replace(Regex("((?:fill|stroke)\\s*=\\s*[\"'])currentColor([\"'])"), "$1${svgColor}$2")
|
|
504
|
+
val bitmap = OneKeySVG.decode(xml.byteInputStream()).render(request.width, request.height)
|
|
505
|
+
val result = RenderResult(bitmap, mode, sourceIdentity, maskAlpha)
|
|
506
|
+
insertMemoryCache(key, result)
|
|
507
|
+
if (diskFile != null) {
|
|
508
|
+
writeDiskRasterAsync(diskFile, bitmap)
|
|
509
|
+
}
|
|
510
|
+
return result
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
private fun insertMemoryCache(key: String, result: RenderResult) {
|
|
514
|
+
val bytes = estimateBitmapBytes(result.bitmap)
|
|
515
|
+
if (bytes <= 8_388_608L) {
|
|
516
|
+
synchronized(lock) {
|
|
517
|
+
cache.put(key, result)?.let { cacheBytes -= estimateBitmapBytes(it.bitmap) }
|
|
518
|
+
cacheBytes += bytes
|
|
519
|
+
while (cache.size > 128 || cacheBytes > 8_388_608L) {
|
|
520
|
+
val first = cache.entries.first()
|
|
521
|
+
cacheBytes -= estimateBitmapBytes(first.value.bitmap)
|
|
522
|
+
cache.remove(first.key)
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
private fun estimateBitmapBytes(bitmap: Bitmap): Long {
|
|
529
|
+
bitmapByteCountOverrideForTesting?.let { return it(bitmap) }
|
|
530
|
+
return try {
|
|
531
|
+
bitmap.allocationByteCount.toLong()
|
|
532
|
+
} catch (_: Throwable) {
|
|
533
|
+
4096L
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
private fun diskRasterDir(): File? {
|
|
538
|
+
val root = startupFileOverride?.parentFile ?: baseCacheDir ?: return null
|
|
539
|
+
return File(root, DISK_RASTER_DIR_NAME)
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
internal fun diskRasterFile(key: String): File? {
|
|
543
|
+
val dir = diskRasterDir() ?: return null
|
|
544
|
+
return File(dir, "${sha256Hex(key.toByteArray(Charsets.UTF_8))}.png")
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
private fun decodeDiskPng(file: File, expectedWidth: Int, expectedHeight: Int): Bitmap? {
|
|
548
|
+
diskPngDecoderOverrideForTesting?.let { return it(file, expectedWidth, expectedHeight) }
|
|
549
|
+
return try {
|
|
550
|
+
val bitmap = BitmapFactory.decodeFile(
|
|
551
|
+
file.absolutePath,
|
|
552
|
+
BitmapFactory.Options().apply { inPreferredConfig = Bitmap.Config.ARGB_8888 },
|
|
553
|
+
) ?: return null
|
|
554
|
+
if (bitmap.width == expectedWidth && bitmap.height == expectedHeight) bitmap else null
|
|
555
|
+
} catch (_: Throwable) {
|
|
556
|
+
null
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
private fun writeDiskRasterAsync(file: File, bitmap: Bitmap) {
|
|
561
|
+
diskExecutor.execute {
|
|
562
|
+
try {
|
|
563
|
+
val dir = file.parentFile ?: return@execute
|
|
564
|
+
if (!dir.exists()) dir.mkdirs()
|
|
565
|
+
val tmp = File(dir, "${file.name}.tmp")
|
|
566
|
+
FileOutputStream(tmp).use { out ->
|
|
567
|
+
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
|
|
568
|
+
}
|
|
569
|
+
if (!tmp.renameTo(file)) {
|
|
570
|
+
tmp.copyTo(file, overwrite = true)
|
|
571
|
+
tmp.delete()
|
|
572
|
+
}
|
|
573
|
+
pruneDiskRasterDir(dir)
|
|
574
|
+
} catch (_: Throwable) {
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
private fun pruneDiskRasterDir(dir: File) {
|
|
580
|
+
val files = dir.listFiles()?.filter { it.isFile && it.name.endsWith(".png") } ?: return
|
|
581
|
+
var total = files.sumOf { it.length() }
|
|
582
|
+
if (total <= MAX_DISK_RASTER_BYTES) return
|
|
583
|
+
val sorted = files.sortedBy { it.lastModified() }
|
|
584
|
+
for (f in sorted) {
|
|
585
|
+
val len = f.length()
|
|
586
|
+
if (f.delete()) {
|
|
587
|
+
total -= len
|
|
588
|
+
if (total <= MAX_DISK_RASTER_BYTES) break
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
private fun noteStartupRequest(name: String, color: String, width: Int, height: Int, scale: Float) {
|
|
594
|
+
if (
|
|
595
|
+
name.isEmpty() ||
|
|
596
|
+
width !in 1..4096 ||
|
|
597
|
+
height !in 1..4096 ||
|
|
598
|
+
!scale.isFinite() ||
|
|
599
|
+
scale <= 0f ||
|
|
600
|
+
!isValidColor(color)
|
|
601
|
+
) {
|
|
602
|
+
return
|
|
603
|
+
}
|
|
604
|
+
val entry = StartupEntry(
|
|
605
|
+
name = name,
|
|
606
|
+
color = color,
|
|
607
|
+
width = width,
|
|
608
|
+
height = height,
|
|
609
|
+
scale = scale,
|
|
610
|
+
launch = 0,
|
|
611
|
+
)
|
|
612
|
+
val now = System.currentTimeMillis()
|
|
613
|
+
synchronized(startupLock) {
|
|
614
|
+
if (startupRecordingClosed) return
|
|
615
|
+
val start = startupRecordStartTimeMs ?: now.also { startupRecordStartTimeMs = it }
|
|
616
|
+
if (now - start >= STARTUP_MAX_DURATION_MS && startupRecorded.isNotEmpty()) {
|
|
617
|
+
startupRecordingClosed = true
|
|
618
|
+
scheduleStartupPersistLocked(delayMs = 0L)
|
|
619
|
+
return
|
|
620
|
+
}
|
|
621
|
+
if (startupRecorded.size < STARTUP_MAX_ENTRIES && startupRecordedKeys.add(entry.identity)) {
|
|
622
|
+
startupRecorded.add(entry)
|
|
623
|
+
}
|
|
624
|
+
scheduleStartupPersistLocked(delayMs = STARTUP_RECORD_DURATION_MS)
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
private fun scheduleStartupPersistLocked(delayMs: Long) {
|
|
629
|
+
startupDebounceFuture?.cancel(false)
|
|
630
|
+
startupFlushScheduled = true
|
|
631
|
+
startupDebounceFuture = diskExecutor.schedule({
|
|
632
|
+
startupDebounceFired()
|
|
633
|
+
}, delayMs, TimeUnit.MILLISECONDS)
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
private fun startupDebounceFired() {
|
|
637
|
+
val entries: List<StartupEntry>
|
|
638
|
+
val file: File?
|
|
639
|
+
val now = System.currentTimeMillis()
|
|
640
|
+
synchronized(startupLock) {
|
|
641
|
+
if (startupRecordingClosed && !startupFlushScheduled) return
|
|
642
|
+
val start = startupRecordStartTimeMs ?: now
|
|
643
|
+
if (
|
|
644
|
+
!startupRecordingClosed &&
|
|
645
|
+
startupRecorded.size < STARTUP_MIN_SETTLED_COUNT &&
|
|
646
|
+
now - start < STARTUP_MAX_DURATION_MS
|
|
647
|
+
) {
|
|
648
|
+
scheduleStartupPersistLocked(delayMs = STARTUP_RECORD_DURATION_MS)
|
|
649
|
+
return
|
|
650
|
+
}
|
|
651
|
+
startupRecordingClosed = true
|
|
652
|
+
startupFlushScheduled = false
|
|
653
|
+
startupDebounceFuture = null
|
|
654
|
+
entries = ArrayList(startupRecorded)
|
|
655
|
+
file = startupFile()
|
|
656
|
+
}
|
|
657
|
+
if (file != null) {
|
|
658
|
+
persistStartupEntries(entries, file)
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
private fun startupFile(): File? {
|
|
663
|
+
startupFileOverride?.let { return it }
|
|
664
|
+
val dir = baseCacheDir ?: return null
|
|
665
|
+
return File(dir, STARTUP_FILE_NAME)
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
private fun persistStartupEntries(entries: List<StartupEntry>, file: File) {
|
|
669
|
+
if (entries.isEmpty()) return
|
|
670
|
+
val existing = readStartupEntries(file)
|
|
671
|
+
val maxExistingLaunch = existing.maxOfOrNull { it.launch } ?: 0
|
|
672
|
+
val currentLaunch = maxExistingLaunch + 1
|
|
673
|
+
|
|
674
|
+
val stampedCurrent = entries.map {
|
|
675
|
+
StartupEntry(
|
|
676
|
+
name = it.name,
|
|
677
|
+
color = it.color,
|
|
678
|
+
width = it.width,
|
|
679
|
+
height = it.height,
|
|
680
|
+
scale = it.scale,
|
|
681
|
+
launch = currentLaunch,
|
|
682
|
+
)
|
|
683
|
+
}
|
|
684
|
+
val seen = stampedCurrent.mapTo(HashSet()) { it.identity }
|
|
685
|
+
val merged = ArrayList<StartupEntry>(STARTUP_MAX_ENTRIES)
|
|
686
|
+
merged.addAll(stampedCurrent.take(STARTUP_MAX_ENTRIES))
|
|
687
|
+
|
|
688
|
+
for (entry in existing) {
|
|
689
|
+
if (merged.size >= STARTUP_MAX_ENTRIES) break
|
|
690
|
+
if (
|
|
691
|
+
entry.name.isEmpty() ||
|
|
692
|
+
entry.width !in 1..4096 ||
|
|
693
|
+
entry.height !in 1..4096 ||
|
|
694
|
+
!entry.scale.isFinite() ||
|
|
695
|
+
entry.scale <= 0f ||
|
|
696
|
+
!isValidColor(entry.color)
|
|
697
|
+
) {
|
|
698
|
+
continue
|
|
699
|
+
}
|
|
700
|
+
val age = currentLaunch - entry.launch
|
|
701
|
+
if (age >= STARTUP_MAX_LAUNCH_AGE) continue
|
|
702
|
+
if (seen.add(entry.identity)) {
|
|
703
|
+
merged.add(entry)
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
if (
|
|
708
|
+
existing.size == merged.size &&
|
|
709
|
+
existing.indices.all { i ->
|
|
710
|
+
existing[i].identity == merged[i].identity &&
|
|
711
|
+
(currentLaunch - existing[i].launch) < 1
|
|
712
|
+
}
|
|
713
|
+
) {
|
|
714
|
+
return
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
try {
|
|
718
|
+
file.parentFile?.mkdirs()
|
|
719
|
+
val json = encodeStartupEntriesJson(merged)
|
|
720
|
+
val tmp = File(file.parentFile, "${file.name}.tmp")
|
|
721
|
+
tmp.writeText(json, Charsets.UTF_8)
|
|
722
|
+
if (!tmp.renameTo(file)) {
|
|
723
|
+
tmp.copyTo(file, overwrite = true)
|
|
724
|
+
tmp.delete()
|
|
725
|
+
}
|
|
726
|
+
} catch (_: Exception) {
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
fun startPrewarmAsync(context: Context) {
|
|
731
|
+
val appContext = context.applicationContext ?: context
|
|
732
|
+
setCacheDirIfNeeded(appContext.cacheDir)
|
|
733
|
+
diskExecutor.execute {
|
|
734
|
+
prewarmStartupSet(appContext.assets, appContext.cacheDir)
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
fun prewarmStartupSet(assets: AssetManager? = null, cacheDir: File? = baseCacheDir) {
|
|
739
|
+
val startNs = System.nanoTime()
|
|
740
|
+
if (cacheDir != null) setCacheDirIfNeeded(cacheDir)
|
|
741
|
+
val manifestStartNs = System.nanoTime()
|
|
742
|
+
if (assets != null) {
|
|
743
|
+
synchronized(manifestLock) {
|
|
744
|
+
loadManifestLocked(assets)
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
val manifestMs = (System.nanoTime() - manifestStartNs) / 1_000_000.0
|
|
748
|
+
val file = synchronized(startupLock) { startupFile() } ?: return
|
|
749
|
+
val entries = readStartupEntries(file).take(STARTUP_MAX_ENTRIES)
|
|
750
|
+
if (entries.isEmpty()) {
|
|
751
|
+
val totalMs = (System.nanoTime() - startNs) / 1_000_000.0
|
|
752
|
+
synchronized(startupLock) {
|
|
753
|
+
lastPrewarmMetrics = StartupPrewarmMetrics(0, 0, manifestMs, totalMs)
|
|
754
|
+
}
|
|
755
|
+
return
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
var decodedCount = 0
|
|
759
|
+
for (entry in entries) {
|
|
760
|
+
val result = decode(
|
|
761
|
+
Request(
|
|
762
|
+
assets = assets,
|
|
763
|
+
name = entry.name,
|
|
764
|
+
color = entry.color,
|
|
765
|
+
width = entry.width,
|
|
766
|
+
height = entry.height,
|
|
767
|
+
scale = entry.scale,
|
|
768
|
+
finalColor = false,
|
|
769
|
+
complete = {},
|
|
770
|
+
),
|
|
771
|
+
diskOnly = assets == null,
|
|
772
|
+
)
|
|
773
|
+
if (result != null) decodedCount += 1
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
val totalMs = (System.nanoTime() - startNs) / 1_000_000.0
|
|
777
|
+
synchronized(startupLock) {
|
|
778
|
+
lastPrewarmMetrics = StartupPrewarmMetrics(
|
|
779
|
+
entryCount = entries.size,
|
|
780
|
+
decodedCount = decodedCount,
|
|
781
|
+
manifestMs = manifestMs,
|
|
782
|
+
totalMs = totalMs,
|
|
783
|
+
)
|
|
784
|
+
}
|
|
785
|
+
try {
|
|
786
|
+
Log.i(
|
|
787
|
+
"OneKeyImage",
|
|
788
|
+
String.format(
|
|
789
|
+
Locale.ROOT,
|
|
790
|
+
"[OneKeyImageStartupMetrics] iconPrewarm entries=%d decoded=%d manifestMs=%.3f totalMs=%.3f",
|
|
791
|
+
entries.size,
|
|
792
|
+
decodedCount,
|
|
793
|
+
manifestMs,
|
|
794
|
+
totalMs,
|
|
795
|
+
),
|
|
796
|
+
)
|
|
797
|
+
} catch (_: Throwable) {
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
internal fun encodeStartupEntriesJson(entries: List<StartupEntry>): String {
|
|
802
|
+
val array = JSONArray()
|
|
803
|
+
for (entry in entries) {
|
|
804
|
+
array.put(
|
|
805
|
+
JSONObject()
|
|
806
|
+
.put("name", entry.name)
|
|
807
|
+
.put("color", entry.color)
|
|
808
|
+
.put("width", entry.width)
|
|
809
|
+
.put("height", entry.height)
|
|
810
|
+
.put("scale", entry.scale.toDouble())
|
|
811
|
+
.put("launch", entry.launch),
|
|
812
|
+
)
|
|
813
|
+
}
|
|
814
|
+
return array.toString()
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
internal fun decodeStartupEntriesJson(text: String): List<StartupEntry> {
|
|
818
|
+
val trimmed = text.trim()
|
|
819
|
+
if (trimmed.isEmpty()) return emptyList()
|
|
820
|
+
val array = JSONArray(trimmed)
|
|
821
|
+
val result = ArrayList<StartupEntry>(array.length())
|
|
822
|
+
for (i in 0 until array.length()) {
|
|
823
|
+
val obj = array.optJSONObject(i) ?: continue
|
|
824
|
+
val name = obj.optString("name", "").takeIf { it.isNotEmpty() } ?: continue
|
|
825
|
+
val color = obj.optString("color", "").takeIf { it.isNotEmpty() } ?: continue
|
|
826
|
+
val width = obj.optInt("width", 0)
|
|
827
|
+
val height = obj.optInt("height", 0)
|
|
828
|
+
val scale = obj.optDouble("scale", Double.NaN).toFloat()
|
|
829
|
+
if (width <= 0 || height <= 0 || !scale.isFinite() || scale <= 0f) continue
|
|
830
|
+
val launch = obj.optInt("launch", 0)
|
|
831
|
+
result.add(
|
|
832
|
+
StartupEntry(
|
|
833
|
+
name = name,
|
|
834
|
+
color = color,
|
|
835
|
+
width = width,
|
|
836
|
+
height = height,
|
|
837
|
+
scale = scale,
|
|
838
|
+
launch = launch,
|
|
839
|
+
),
|
|
840
|
+
)
|
|
841
|
+
}
|
|
842
|
+
return result
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
internal fun readStartupEntries(file: File): List<StartupEntry> {
|
|
846
|
+
if (!file.isFile) return emptyList()
|
|
847
|
+
return try {
|
|
848
|
+
decodeStartupEntriesJson(file.readText(Charsets.UTF_8))
|
|
849
|
+
} catch (_: Exception) {
|
|
850
|
+
emptyList()
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
private fun sha256Hex(bytes: ByteArray): String =
|
|
855
|
+
MessageDigest.getInstance("SHA-256")
|
|
856
|
+
.digest(bytes)
|
|
857
|
+
.joinToString("") { "%02x".format(Locale.ROOT, it.toInt() and 0xff) }
|
|
858
|
+
|
|
859
|
+
internal fun clearMemoryCacheForTesting() {
|
|
860
|
+
synchronized(lock) {
|
|
861
|
+
cache.clear()
|
|
862
|
+
cacheBytes = 0L
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
internal fun setManifestFallbackForTesting(entries: Map<String, ManifestEntry>?) {
|
|
867
|
+
manifestAccessLock.lock()
|
|
868
|
+
try {
|
|
869
|
+
manifestFallbackMap = entries
|
|
870
|
+
} finally {
|
|
871
|
+
manifestAccessLock.unlock()
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
internal fun tryAcquireManifestAccessLockForTesting(): Boolean = manifestAccessLock.tryLock()
|
|
876
|
+
|
|
877
|
+
internal fun unlockManifestAccessLockForTesting() {
|
|
878
|
+
manifestAccessLock.unlock()
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
internal fun resetStartupForTesting(file: File? = null, cacheDir: File? = null) {
|
|
882
|
+
synchronized(startupLock) {
|
|
883
|
+
startupDebounceFuture?.cancel(false)
|
|
884
|
+
startupDebounceFuture = null
|
|
885
|
+
startupRecorded.clear()
|
|
886
|
+
startupRecordedKeys.clear()
|
|
887
|
+
startupRecordStartTimeMs = null
|
|
888
|
+
startupRecordingClosed = false
|
|
889
|
+
startupFlushScheduled = false
|
|
890
|
+
startupFileOverride = file
|
|
891
|
+
baseCacheDir = cacheDir ?: file?.parentFile
|
|
892
|
+
lastPrewarmMetrics = StartupPrewarmMetrics()
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
internal fun flushStartupForTesting(forceClose: Boolean = true) {
|
|
897
|
+
val entries: List<StartupEntry>
|
|
898
|
+
val file: File?
|
|
899
|
+
synchronized(startupLock) {
|
|
900
|
+
startupDebounceFuture?.cancel(false)
|
|
901
|
+
startupDebounceFuture = null
|
|
902
|
+
if (!forceClose && startupRecorded.size < STARTUP_MIN_SETTLED_COUNT) {
|
|
903
|
+
return
|
|
904
|
+
}
|
|
905
|
+
startupRecordingClosed = true
|
|
906
|
+
startupFlushScheduled = false
|
|
907
|
+
entries = ArrayList(startupRecorded)
|
|
908
|
+
file = startupFile()
|
|
909
|
+
}
|
|
910
|
+
if (file != null) {
|
|
911
|
+
persistStartupEntries(entries, file)
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
}
|