@onekeyfe/react-native-image 6.7.0-50 → 6.7.0-52
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/android/build.gradle +1 -0
- package/android/src/main/AndroidManifest.xml +5 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyIcon.kt +5 -1
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImage.kt +240 -6
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageCache.kt +4 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideRegistry.kt +1 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageSafety.kt +41 -0
- package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupPrewarm.kt +990 -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/OneKeyNamedSVG.kt +758 -61
- package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyStartupPrewarmTest.kt +545 -0
- package/ios/OneKeyIcon.swift +6 -0
- package/ios/OneKeyImage.swift +75 -1
- package/ios/OneKeyImageCache.swift +2 -0
- package/ios/OneKeyImageCoderBridge.m +42 -1
- package/ios/OneKeyImageStartupPrewarm.swift +564 -0
- package/ios/OneKeyMainThreadDecode.swift +338 -0
- package/ios/OneKeyNamedSVG.swift +380 -7
- package/ios/tests/OneKeyIconTests.swift +147 -0
- package/ios/tests/OneKeyImageInfrastructureTests.swift +263 -0
- package/lib/module/index.js +5 -1
- package/package.json +1 -1
- package/src/index.tsx +23 -2
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
package com.margelo.nitro.onekeyimage
|
|
2
2
|
|
|
3
|
+
import android.content.Context
|
|
3
4
|
import android.content.res.AssetManager
|
|
4
5
|
import android.graphics.Bitmap
|
|
6
|
+
import android.graphics.BitmapFactory
|
|
5
7
|
import android.graphics.Canvas
|
|
6
8
|
import android.graphics.Color
|
|
7
9
|
import android.graphics.Paint
|
|
@@ -10,8 +12,16 @@ import android.graphics.PorterDuffColorFilter
|
|
|
10
12
|
import android.os.Handler
|
|
11
13
|
import android.os.Looper
|
|
12
14
|
import android.util.Log
|
|
15
|
+
import org.json.JSONArray
|
|
13
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
|
|
14
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
|
|
15
25
|
import kotlin.math.roundToInt
|
|
16
26
|
|
|
17
27
|
/** Process-owned named resources; no React runtime or Activity is retained. */
|
|
@@ -22,16 +32,20 @@ object OneKeyNamedSVG {
|
|
|
22
32
|
val height: Float,
|
|
23
33
|
val mode: RecolorMode,
|
|
24
34
|
)
|
|
25
|
-
data class RenderResult(
|
|
26
|
-
val
|
|
35
|
+
data class RenderResult(
|
|
36
|
+
val bitmap: Bitmap,
|
|
37
|
+
val mode: RecolorMode,
|
|
38
|
+
val sourceIdentity: String,
|
|
39
|
+
val maskAlpha: Int,
|
|
40
|
+
) {
|
|
27
41
|
fun canTint(color: String): Boolean {
|
|
28
42
|
val value = parseColor(color) ?: return false
|
|
29
43
|
return mode == RecolorMode.FIXED || mode == RecolorMode.SINGLE ||
|
|
30
|
-
(mode == RecolorMode.MULTIPLE &&
|
|
44
|
+
(mode == RecolorMode.MULTIPLE && ((value ushr 24) and 0xff) == maskAlpha)
|
|
31
45
|
}
|
|
32
46
|
fun tintColor(color: String): Int? {
|
|
33
47
|
val value = parseColor(color) ?: return null
|
|
34
|
-
return when(mode) {
|
|
48
|
+
return when (mode) {
|
|
35
49
|
RecolorMode.SINGLE -> value
|
|
36
50
|
RecolorMode.MULTIPLE -> value or (0xff shl 24)
|
|
37
51
|
else -> null
|
|
@@ -40,58 +54,183 @@ object OneKeyNamedSVG {
|
|
|
40
54
|
internal fun finalBitmap(color: String): Bitmap {
|
|
41
55
|
val tint = tintColor(color) ?: return bitmap
|
|
42
56
|
if (tint == Color.BLACK) return bitmap
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
}
|
|
47
65
|
}
|
|
48
66
|
}
|
|
49
67
|
|
|
50
|
-
|
|
51
|
-
val
|
|
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
|
+
|
|
52
107
|
private val lock = Any()
|
|
53
108
|
private val pending = LinkedHashMap<Any, Request>()
|
|
54
109
|
private val cache = LinkedHashMap<String, RenderResult>(128, 0.75f, true)
|
|
55
110
|
private var cacheBytes = 0L
|
|
56
111
|
private var activeWorkers = 0
|
|
112
|
+
@Volatile
|
|
57
113
|
private var manifest: JSONObject? = null
|
|
114
|
+
@Volatile
|
|
115
|
+
private var manifestFallbackMap: Map<String, ManifestEntry>? = null
|
|
58
116
|
private val manifestLock = Any()
|
|
117
|
+
private val manifestAccessLock = ReentrantLock()
|
|
59
118
|
private val workers = Executors.newFixedThreadPool(2)
|
|
60
|
-
private val
|
|
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
|
+
}
|
|
61
127
|
private val colorPattern = Regex("rgba\\(\\d{1,3},\\d{1,3},\\d{1,3},(?:0(?:\\.\\d+)?|1(?:\\.0+)?)\\)")
|
|
62
128
|
|
|
63
129
|
private var manifestMissingLogged = false
|
|
64
130
|
|
|
65
|
-
|
|
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
|
+
}
|
|
66
168
|
|
|
67
169
|
/** Caller holds manifestLock. Hosts own the resources, so a miss is logged once. */
|
|
68
|
-
private fun loadManifestLocked(assets: AssetManager): JSONObject? {
|
|
170
|
+
private fun loadManifestLocked(assets: AssetManager?): JSONObject? {
|
|
69
171
|
manifest?.let { return it }
|
|
172
|
+
if (assets == null) return null
|
|
173
|
+
manifestAccessLock.lock()
|
|
70
174
|
return try {
|
|
71
|
-
|
|
72
|
-
|
|
175
|
+
manifest?.let { return it }
|
|
176
|
+
val raw = assets.open("icons/manifest.json").bufferedReader().use { it.readText() }
|
|
177
|
+
JSONObject(raw).also { manifest = it }
|
|
73
178
|
} catch (error: Exception) {
|
|
74
179
|
if (!manifestMissingLogged) {
|
|
75
180
|
manifestMissingLogged = true
|
|
76
|
-
|
|
181
|
+
try {
|
|
182
|
+
Log.w("OneKeyIcon", "assets/icons/manifest.json is missing or invalid; named icons will not render", error)
|
|
183
|
+
} catch (_: Throwable) {
|
|
184
|
+
}
|
|
77
185
|
}
|
|
78
186
|
null
|
|
187
|
+
} finally {
|
|
188
|
+
manifestAccessLock.unlock()
|
|
79
189
|
}
|
|
80
190
|
}
|
|
81
191
|
|
|
82
192
|
private fun modeOf(metadata: JSONObject): RecolorMode = try {
|
|
83
|
-
RecolorMode.valueOf(metadata.optString("recolorMode", "raster").uppercase())
|
|
193
|
+
RecolorMode.valueOf(metadata.optString("recolorMode", "raster").uppercase(Locale.ROOT))
|
|
84
194
|
} catch (_: IllegalArgumentException) {
|
|
85
195
|
RecolorMode.RASTER
|
|
86
196
|
}
|
|
87
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
|
+
|
|
88
219
|
private data class RenderKey(val identity: String, val key: String, val maskAlpha: Int)
|
|
89
220
|
|
|
90
|
-
private fun renderKey(
|
|
91
|
-
|
|
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 {
|
|
92
231
|
val identity = "$name|$digest|$width|$height|$scale"
|
|
93
|
-
val maskAlpha = if (mode == RecolorMode.MULTIPLE)
|
|
94
|
-
val colorKey = when(mode) {
|
|
232
|
+
val maskAlpha = if (mode == RecolorMode.MULTIPLE) ((color ushr 24) and 0xff) else 255
|
|
233
|
+
val colorKey = when (mode) {
|
|
95
234
|
RecolorMode.RASTER -> requestColor
|
|
96
235
|
RecolorMode.MULTIPLE -> maskAlpha.toString()
|
|
97
236
|
else -> ""
|
|
@@ -103,17 +242,70 @@ object OneKeyNamedSVG {
|
|
|
103
242
|
* Main-thread fast path: returns an already-decoded result without asset I/O or
|
|
104
243
|
* manifest loading, so remounted and recycled views paint in the same frame.
|
|
105
244
|
*/
|
|
106
|
-
fun cachedRender(name: String, color: String, width: Int, height: Int, scale: Float): RenderResult?
|
|
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
|
+
}
|
|
107
259
|
val value = parseColor(color) ?: return null
|
|
108
|
-
val
|
|
109
|
-
val
|
|
110
|
-
val key = renderKey(name, digest, width, height, scale, modeOf(metadata), color, value).key
|
|
260
|
+
val entry = lookupManifestEntryNonBlocking(name) ?: return null
|
|
261
|
+
val key = renderKey(name, entry.digest, width, height, scale, entry.mode, color, value).key
|
|
111
262
|
return synchronized(lock) { cache[key] }
|
|
112
263
|
}
|
|
113
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
|
+
|
|
114
301
|
fun iconMetadata(assets: AssetManager, name: String): IconMetadata? {
|
|
115
302
|
val trimmed = name.trim()
|
|
116
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
|
+
}
|
|
117
309
|
val metadata = try {
|
|
118
310
|
synchronized(manifestLock) {
|
|
119
311
|
loadManifestLocked(assets)?.optJSONObject(trimmed)
|
|
@@ -130,18 +322,63 @@ object OneKeyNamedSVG {
|
|
|
130
322
|
return IconMetadata(width = width, height = height, mode = mode)
|
|
131
323
|
}
|
|
132
324
|
|
|
133
|
-
fun request(
|
|
134
|
-
|
|
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
|
+
}
|
|
135
345
|
enqueue(owner, assets, name, color, width, height, scale, true) { complete(it?.bitmap) }
|
|
136
346
|
}
|
|
137
347
|
|
|
138
|
-
fun requestRender(
|
|
139
|
-
|
|
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
|
+
}
|
|
140
368
|
enqueue(owner, assets, name, color, width, height, scale, false, complete)
|
|
141
369
|
}
|
|
142
370
|
|
|
143
|
-
private fun enqueue(
|
|
144
|
-
|
|
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
|
+
) {
|
|
145
382
|
synchronized(lock) {
|
|
146
383
|
pending[owner] = Request(assets, name, color, width, height, scale, finalColor, complete)
|
|
147
384
|
while (activeWorkers < 2 && activeWorkers < pending.size) {
|
|
@@ -156,62 +393,522 @@ object OneKeyNamedSVG {
|
|
|
156
393
|
val channels = color.removePrefix("rgba(").removeSuffix(")").split(',')
|
|
157
394
|
val rgb = channels.take(3).map { it.toInt() }
|
|
158
395
|
if (rgb.any { it !in 0..255 }) return null
|
|
159
|
-
val alpha = (channels[3].toDouble() * 255).roundToInt()
|
|
160
|
-
return
|
|
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)
|
|
161
398
|
}
|
|
399
|
+
|
|
162
400
|
fun isValidColor(color: String): Boolean = parseColor(color) != null
|
|
163
401
|
|
|
164
402
|
private fun drain() {
|
|
165
403
|
while (true) {
|
|
166
404
|
val request = synchronized(lock) {
|
|
167
405
|
val first = pending.entries.firstOrNull()
|
|
168
|
-
if (first == null) {
|
|
169
|
-
|
|
406
|
+
if (first == null) {
|
|
407
|
+
activeWorkers--
|
|
408
|
+
null
|
|
409
|
+
} else {
|
|
410
|
+
pending.remove(first.key)
|
|
411
|
+
first.value
|
|
412
|
+
}
|
|
170
413
|
} ?: return
|
|
171
414
|
val bitmap = try {
|
|
172
415
|
decode(request)?.let { result ->
|
|
173
|
-
if (request.finalColor)
|
|
174
|
-
|
|
416
|
+
if (request.finalColor) {
|
|
417
|
+
result.copy(bitmap = result.finalBitmap(request.color), mode = RecolorMode.RASTER)
|
|
418
|
+
} else {
|
|
419
|
+
result
|
|
420
|
+
}
|
|
175
421
|
}
|
|
176
|
-
} catch (_: Throwable) {
|
|
177
|
-
|
|
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
|
+
}
|
|
178
431
|
}
|
|
179
432
|
}
|
|
180
433
|
|
|
181
|
-
private fun decode(request: Request): RenderResult? {
|
|
182
|
-
if (
|
|
183
|
-
request.width
|
|
184
|
-
request.
|
|
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
|
+
}
|
|
185
444
|
val color = parseColor(request.color) ?: return null
|
|
186
|
-
val
|
|
187
|
-
|
|
445
|
+
val manifestEntry = if (diskOnly) {
|
|
446
|
+
lookupManifestEntryNonBlocking(request.name)
|
|
447
|
+
} else {
|
|
448
|
+
lookupManifestEntryBlocking(request.assets, request.name)
|
|
188
449
|
} ?: return null
|
|
189
|
-
val mode =
|
|
190
|
-
val (sourceIdentity, key, maskAlpha) = renderKey(
|
|
191
|
-
request.
|
|
192
|
-
|
|
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) {
|
|
193
491
|
RecolorMode.FIXED, RecolorMode.SINGLE -> Color.BLACK
|
|
194
492
|
RecolorMode.MULTIPLE -> Color.argb(maskAlpha, 0, 0, 0)
|
|
195
493
|
RecolorMode.RASTER -> color
|
|
196
494
|
}
|
|
197
|
-
synchronized(lock) { cache[key]?.let { return it } }
|
|
198
495
|
// AndroidSVG rgba quantizes with 256; SVG hex alpha preserves the exact CSS 8-bit channel.
|
|
199
|
-
val svgColor = listOf(
|
|
200
|
-
.
|
|
201
|
-
|
|
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() }
|
|
202
503
|
.replace(Regex("((?:fill|stroke)\\s*=\\s*[\"'])currentColor([\"'])"), "$1${svgColor}$2")
|
|
203
504
|
val bitmap = OneKeySVG.decode(xml.byteInputStream()).render(request.width, request.height)
|
|
204
505
|
val result = RenderResult(bitmap, mode, sourceIdentity, maskAlpha)
|
|
205
|
-
|
|
206
|
-
if (
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
+
}
|
|
213
524
|
}
|
|
214
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
|
+
}
|
|
215
842
|
return result
|
|
216
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
|
+
}
|
|
217
914
|
}
|