@onekeyfe/react-native-image 6.7.0-alpha.475 → 6.8.0-2

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.
Files changed (31) hide show
  1. package/README.md +144 -86
  2. package/android/build.gradle +1 -0
  3. package/android/src/main/AndroidManifest.xml +12 -1
  4. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyIcon.kt +5 -1
  5. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImage.kt +240 -6
  6. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageCache.kt +4 -0
  7. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideModule.kt +17 -0
  8. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageGlideRegistry.kt +1 -0
  9. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageSafety.kt +41 -0
  10. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupPrewarm.kt +1106 -0
  11. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyImageStartupProvider.kt +54 -0
  12. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMainThreadDecode.kt +541 -0
  13. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyMediaPreviewCache.kt +1 -1
  14. package/android/src/main/java/com/margelo/nitro/onekeyimage/OneKeyNamedSVG.kt +758 -61
  15. package/android/src/test/java/com/margelo/nitro/onekeyimage/OneKeyStartupPrewarmTest.kt +600 -0
  16. package/ios/OneKeyAvatarImageLoader.swift +1 -1
  17. package/ios/OneKeyIcon.swift +6 -0
  18. package/ios/OneKeyImage.swift +85 -2
  19. package/ios/OneKeyImageCache.swift +2 -0
  20. package/ios/OneKeyImageCoderBridge.m +42 -1
  21. package/ios/OneKeyImageRequestContext.swift +16 -2
  22. package/ios/OneKeyImageStartupPrewarm.swift +602 -0
  23. package/ios/OneKeyMainThreadDecode.swift +338 -0
  24. package/ios/OneKeyMediaPreviewCache.swift +20 -12
  25. package/ios/OneKeyNamedSVG.swift +437 -9
  26. package/ios/tests/OneKeyIconTests.swift +241 -0
  27. package/ios/tests/OneKeyImageInfrastructureTests.swift +611 -0
  28. package/ios/tests/OneKeyMediaPreviewCacheTests.swift +49 -0
  29. package/lib/module/index.js +5 -1
  30. package/package.json +1 -1
  31. package/src/index.tsx +23 -2
@@ -0,0 +1,54 @@
1
+ package com.margelo.nitro.onekeyimage
2
+
3
+ import android.content.ContentProvider
4
+ import android.content.ContentValues
5
+ import android.content.Context
6
+ import android.database.Cursor
7
+ import android.net.Uri
8
+ import java.util.concurrent.atomic.AtomicBoolean
9
+
10
+ internal object OneKeyImageStartupTrigger {
11
+ private val started = AtomicBoolean(false)
12
+
13
+ fun startIfNeeded(context: Context) {
14
+ val appContext = context.applicationContext ?: context
15
+ OneKeyNamedSVG.setCacheDirIfNeeded(appContext.cacheDir)
16
+ OneKeyImageStartupPrewarm.setCacheDirIfNeeded(appContext.cacheDir)
17
+ if (!started.compareAndSet(false, true)) return
18
+ OneKeyNamedSVG.startPrewarmAsync(appContext)
19
+ OneKeyImageStartupPrewarm.startPrewarmAsync(appContext)
20
+ }
21
+ }
22
+
23
+ /**
24
+ * Triggers asynchronous icon and image startup prewarming during process
25
+ * initialization before `Application.onCreate` or React Native startup.
26
+ */
27
+ class OneKeyImageStartupProvider : ContentProvider() {
28
+ override fun onCreate(): Boolean {
29
+ val appContext = context?.applicationContext ?: return false
30
+ OneKeyImageStartupTrigger.startIfNeeded(appContext)
31
+ return true
32
+ }
33
+
34
+ override fun query(
35
+ uri: Uri,
36
+ projection: Array<out String>?,
37
+ selection: String?,
38
+ selectionArgs: Array<out String>?,
39
+ sortOrder: String?,
40
+ ): Cursor? = null
41
+
42
+ override fun getType(uri: Uri): String? = null
43
+
44
+ override fun insert(uri: Uri, values: ContentValues?): Uri? = null
45
+
46
+ override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
47
+
48
+ override fun update(
49
+ uri: Uri,
50
+ values: ContentValues?,
51
+ selection: String?,
52
+ selectionArgs: Array<out String>?,
53
+ ): Int = 0
54
+ }
@@ -0,0 +1,541 @@
1
+ package com.margelo.nitro.onekeyimage
2
+
3
+ import android.graphics.BitmapFactory
4
+ import android.os.Handler
5
+ import android.os.Looper
6
+ import android.os.Trace
7
+ import android.util.Log
8
+ import java.io.ByteArrayInputStream
9
+ import java.io.File
10
+ import java.util.Locale
11
+
12
+ internal object OneKeyMainThreadDecode {
13
+ const val MAX_ENCODED_BYTES = 32 * 1024
14
+ const val MAX_SOURCE_PIXEL_SIZE = 512
15
+ const val MAX_THUMBNAIL_PIXEL_SIZE = 256
16
+ const val MAX_ICON_PIXEL_SIZE = 192
17
+ const val FRAME_BUDGET_NS = 4_000_000L
18
+ const val FRAME_WINDOW_NS = 16_666_667L
19
+ private const val METRICS_REPORT_DELAY_MS = 3_500L
20
+
21
+ data class Metrics(
22
+ var attempts: Int = 0,
23
+ var hits: Int = 0,
24
+ var misses: Int = 0,
25
+ var budgetRejections: Int = 0,
26
+ var lockContentionMisses: Int = 0,
27
+ var diskMisses: Int = 0,
28
+ var limitRejections: Int = 0,
29
+ var decodeFailures: Int = 0,
30
+ var windows: Int = 0,
31
+ var maxWindowAllowed: Int = 0,
32
+ var maxWindowRejected: Int = 0,
33
+ var totalDurationMs: Double = 0.0,
34
+ var p50DurationMs: Double = 0.0,
35
+ var p90DurationMs: Double = 0.0,
36
+ var maxDurationMs: Double = 0.0,
37
+ ) {
38
+ val hitRate: Double
39
+ get() = if (attempts > 0) hits.toDouble() / attempts.toDouble() else 0.0
40
+ }
41
+
42
+ private val metricsLock = Any()
43
+ private var windowStartNs = 0L
44
+ private var spentNs = 0L
45
+ private var currentWindowAllowed = 0
46
+ private var currentWindowRejected = 0
47
+ private var frameBudgetNs = FRAME_BUDGET_NS
48
+ private var frameWindowNs = FRAME_WINDOW_NS
49
+ private var totalDurationNs = 0L
50
+ private val durationsNs = ArrayList<Long>(64)
51
+ private var metrics = Metrics()
52
+ private var metricsReportScheduled = false
53
+
54
+ @Volatile
55
+ var isEnabled: Boolean =
56
+ System.getProperty("OneKeyImageSyncDecodeDisabled")?.equals("true", ignoreCase = true) != true
57
+
58
+ @Volatile
59
+ internal var mainThreadCheckOverrideForTesting: (() -> Boolean)? = null
60
+
61
+ internal fun isOnMainThread(): Boolean {
62
+ mainThreadCheckOverrideForTesting?.let { return it() }
63
+ return try {
64
+ val mainLooper = Looper.getMainLooper()
65
+ mainLooper != null && Looper.myLooper() === mainLooper
66
+ } catch (_: Throwable) {
67
+ false
68
+ }
69
+ }
70
+
71
+ fun metricsSnapshot(): Metrics = synchronized(metricsLock) {
72
+ val snapshot = metrics.copy()
73
+ if (durationsNs.isNotEmpty()) {
74
+ val sorted = durationsNs.sorted()
75
+ snapshot.totalDurationMs = totalDurationNs / 1_000_000.0
76
+ snapshot.p50DurationMs = percentile(sorted, 0.50) / 1_000_000.0
77
+ snapshot.p90DurationMs = percentile(sorted, 0.90) / 1_000_000.0
78
+ snapshot.maxDurationMs = (sorted.lastOrNull() ?: 0L) / 1_000_000.0
79
+ }
80
+ snapshot
81
+ }
82
+
83
+ fun resetForTesting() {
84
+ synchronized(metricsLock) {
85
+ windowStartNs = 0L
86
+ spentNs = 0L
87
+ currentWindowAllowed = 0
88
+ currentWindowRejected = 0
89
+ frameBudgetNs = FRAME_BUDGET_NS
90
+ frameWindowNs = FRAME_WINDOW_NS
91
+ totalDurationNs = 0L
92
+ durationsNs.clear()
93
+ metrics = Metrics()
94
+ metricsReportScheduled = false
95
+ }
96
+ }
97
+
98
+ fun setBudgetForTesting(budgetNs: Long, windowNs: Long = FRAME_WINDOW_NS) {
99
+ synchronized(metricsLock) {
100
+ frameBudgetNs = maxOf(0L, budgetNs)
101
+ frameWindowNs = maxOf(1L, windowNs)
102
+ windowStartNs = 0L
103
+ spentNs = 0L
104
+ currentWindowAllowed = 0
105
+ currentWindowRejected = 0
106
+ }
107
+ }
108
+
109
+ internal fun noteLockContention() {
110
+ synchronized(metricsLock) { metrics.lockContentionMisses += 1 }
111
+ }
112
+
113
+ internal fun noteDiskMiss() {
114
+ synchronized(metricsLock) { metrics.diskMisses += 1 }
115
+ }
116
+
117
+ internal fun noteLimitRejection() {
118
+ synchronized(metricsLock) { metrics.limitRejections += 1 }
119
+ }
120
+
121
+ internal fun noteDecodeFailure() {
122
+ synchronized(metricsLock) { metrics.decodeFailures += 1 }
123
+ }
124
+
125
+ fun <T> run(work: () -> T?): T? {
126
+ if (!isEnabled || !isOnMainThread()) return null
127
+ val start = System.nanoTime()
128
+ val allowed = synchronized(metricsLock) {
129
+ if (start - windowStartNs >= frameWindowNs) {
130
+ windowStartNs = start
131
+ spentNs = 0L
132
+ currentWindowAllowed = 0
133
+ currentWindowRejected = 0
134
+ metrics.windows += 1
135
+ }
136
+ if (spentNs >= frameBudgetNs) {
137
+ metrics.budgetRejections += 1
138
+ currentWindowRejected += 1
139
+ if (currentWindowRejected > metrics.maxWindowRejected) {
140
+ metrics.maxWindowRejected = currentWindowRejected
141
+ }
142
+ false
143
+ } else {
144
+ metrics.attempts += 1
145
+ currentWindowAllowed += 1
146
+ if (currentWindowAllowed > metrics.maxWindowAllowed) {
147
+ metrics.maxWindowAllowed = currentWindowAllowed
148
+ }
149
+ true
150
+ }
151
+ }
152
+ scheduleMetricsReportIfNeeded()
153
+ if (!allowed) return null
154
+
155
+ try {
156
+ Trace.beginSection("OneKeyMainThreadDecode")
157
+ } catch (_: Throwable) {
158
+ }
159
+ val result = try {
160
+ work()
161
+ } finally {
162
+ try {
163
+ Trace.endSection()
164
+ } catch (_: Throwable) {
165
+ }
166
+ }
167
+ val elapsed = (System.nanoTime() - start).coerceAtLeast(0L)
168
+ synchronized(metricsLock) {
169
+ spentNs += elapsed
170
+ totalDurationNs += elapsed
171
+ if (durationsNs.size < 512) {
172
+ durationsNs.add(elapsed)
173
+ }
174
+ if (result != null) {
175
+ metrics.hits += 1
176
+ } else {
177
+ metrics.misses += 1
178
+ }
179
+ }
180
+ return result
181
+ }
182
+
183
+ private fun scheduleMetricsReportIfNeeded() {
184
+ val shouldSchedule = synchronized(metricsLock) {
185
+ if (metricsReportScheduled) false
186
+ else {
187
+ metricsReportScheduled = true
188
+ true
189
+ }
190
+ }
191
+ if (!shouldSchedule) return
192
+ try {
193
+ val mainLooper = Looper.getMainLooper() ?: return
194
+ Handler(mainLooper).postDelayed({
195
+ val s = metricsSnapshot()
196
+ try {
197
+ Log.i(
198
+ "OneKeyImage",
199
+ String.format(
200
+ Locale.ROOT,
201
+ "[OneKeyImageStartupMetrics] syncDecode attempts=%d hits=%d misses=%d hitRate=%.2f budgetRejected=%d lockContended=%d diskMiss=%d limitRejected=%d decodeFailed=%d windows=%d maxWindowAllowed=%d maxWindowRejected=%d totalMs=%.3f p50Ms=%.3f p90Ms=%.3f maxMs=%.3f",
202
+ s.attempts,
203
+ s.hits,
204
+ s.misses,
205
+ s.hitRate,
206
+ s.budgetRejections,
207
+ s.lockContentionMisses,
208
+ s.diskMisses,
209
+ s.limitRejections,
210
+ s.decodeFailures,
211
+ s.windows,
212
+ s.maxWindowAllowed,
213
+ s.maxWindowRejected,
214
+ s.totalDurationMs,
215
+ s.p50DurationMs,
216
+ s.p90DurationMs,
217
+ s.maxDurationMs,
218
+ ),
219
+ )
220
+ } catch (_: Throwable) {
221
+ }
222
+ }, METRICS_REPORT_DELAY_MS)
223
+ } catch (_: Throwable) {
224
+ }
225
+ }
226
+
227
+ private fun percentile(sorted: List<Long>, p: Double): Long {
228
+ if (sorted.isEmpty()) return 0L
229
+ val index = minOf(sorted.size - 1, maxOf(0, ((sorted.size - 1) * p).toInt()))
230
+ return sorted[index]
231
+ }
232
+
233
+ /**
234
+ * Decodes a small local or disk-cached static image into the shared startup memory
235
+ * cache on the UI thread when all size and safety bounds are satisfied.
236
+ */
237
+ fun promoteToMemory(
238
+ cacheDir: File?,
239
+ url: String,
240
+ rawUrl: String,
241
+ width: Int,
242
+ height: Int,
243
+ round: Boolean,
244
+ ): Boolean {
245
+ if (width <= 0 || height <= 0 || maxOf(width, height) > MAX_THUMBNAIL_PIXEL_SIZE) {
246
+ noteLimitRejection()
247
+ return false
248
+ }
249
+ val keyHash = OneKeyImageStartupPrewarm.keyHash(url, width, height, round)
250
+ if (OneKeyImageStartupPrewarm.cachedBitmap(keyHash) != null) {
251
+ return true
252
+ }
253
+ val bytes = encodedBytes(cacheDir, url) ?: return false
254
+ val bounds = inspectSmallStaticImage(bytes) ?: return false
255
+ val bitmap = OneKeyImageStartupPrewarm.decodeStaticBitmap(
256
+ bytes = bytes,
257
+ sourceWidth = bounds.first,
258
+ sourceHeight = bounds.second,
259
+ targetWidth = width,
260
+ targetHeight = height,
261
+ round = round,
262
+ )
263
+ if (bitmap == null) {
264
+ noteDecodeFailure()
265
+ return false
266
+ }
267
+ OneKeyImageStartupPrewarm.putMemoryBitmap(keyHash, bitmap)
268
+ if (cacheDir != null) {
269
+ OneKeyImageStartupPrewarm.ensureFallbackDiskBytesAsync(cacheDir, url, bytes)
270
+ }
271
+ return true
272
+ }
273
+
274
+ internal fun encodedBytes(cacheDir: File?, url: String): ByteArray? {
275
+ if (url.startsWith("file://")) {
276
+ val rawPath = url.removePrefix("file://").substringBefore('?').substringBefore('#')
277
+ if (rawPath.isEmpty() || rawPath.startsWith("/android_asset/")) {
278
+ noteDiskMiss()
279
+ return null
280
+ }
281
+ val file = File(rawPath)
282
+ if (!file.isFile) {
283
+ noteDiskMiss()
284
+ return null
285
+ }
286
+ val length = file.length()
287
+ if (length <= 0L) {
288
+ noteDiskMiss()
289
+ return null
290
+ }
291
+ if (length > MAX_ENCODED_BYTES) {
292
+ noteLimitRejection()
293
+ return null
294
+ }
295
+ val bytes = try {
296
+ file.readBytes()
297
+ } catch (_: Exception) {
298
+ noteDiskMiss()
299
+ return null
300
+ }
301
+ if (bytes.isEmpty() || bytes.size > MAX_ENCODED_BYTES) {
302
+ noteLimitRejection()
303
+ return null
304
+ }
305
+ return bytes
306
+ }
307
+
308
+ if (url.startsWith("data:")) {
309
+ if (url.length > 65_536) {
310
+ noteLimitRejection()
311
+ return null
312
+ }
313
+ val comma = url.indexOf(',')
314
+ if (comma < 0 || !url.substring(0, comma).lowercase(Locale.ROOT).contains(";base64")) {
315
+ noteDecodeFailure()
316
+ return null
317
+ }
318
+ val payloadStart = comma + 1
319
+ val estimated = try {
320
+ OneKeyImageSafety.dataUriDecodedByteCount(url, payloadStart)
321
+ } catch (_: Exception) {
322
+ noteLimitRejection()
323
+ return null
324
+ }
325
+ if (estimated <= 0L || estimated > MAX_ENCODED_BYTES) {
326
+ noteLimitRejection()
327
+ return null
328
+ }
329
+ val decoded = try {
330
+ OneKeyImageSafety.decodeDataUriBase64(url.substring(payloadStart))
331
+ } catch (_: Exception) {
332
+ noteDecodeFailure()
333
+ return null
334
+ }
335
+ if (decoded.isEmpty() || decoded.size > MAX_ENCODED_BYTES) {
336
+ noteLimitRejection()
337
+ return null
338
+ }
339
+ return decoded
340
+ }
341
+
342
+ if (
343
+ url.startsWith("http://") ||
344
+ url.startsWith("https://") ||
345
+ OneKeyBlockieAvatar.isAvatarUri(url)
346
+ ) {
347
+ if (cacheDir == null) {
348
+ noteDiskMiss()
349
+ return null
350
+ }
351
+ val safeKey = OneKeyImageStartupPrewarm.glideDataSafeKey(url)
352
+ if (safeKey == null) {
353
+ noteDiskMiss()
354
+ return null
355
+ }
356
+ val diskFile = OneKeyImageStartupPrewarm.findDiskCacheFile(cacheDir, safeKey)
357
+ if (diskFile == null) {
358
+ noteDiskMiss()
359
+ return null
360
+ }
361
+ val length = diskFile.length()
362
+ if (length <= 0L) {
363
+ noteDiskMiss()
364
+ return null
365
+ }
366
+ if (length > MAX_ENCODED_BYTES) {
367
+ noteLimitRejection()
368
+ return null
369
+ }
370
+ val bytes = try {
371
+ diskFile.readBytes()
372
+ } catch (_: Exception) {
373
+ noteDiskMiss()
374
+ return null
375
+ }
376
+ if (bytes.isEmpty() || bytes.size > MAX_ENCODED_BYTES) {
377
+ noteLimitRejection()
378
+ return null
379
+ }
380
+ if (OneKeyBlockieAvatar.isAvatarUri(url) && !OneKeyBlockieAvatar.isValidPng(bytes)) {
381
+ noteDecodeFailure()
382
+ return null
383
+ }
384
+ return bytes
385
+ }
386
+
387
+ noteDiskMiss()
388
+ return null
389
+ }
390
+
391
+ internal fun inspectSmallStaticImage(bytes: ByteArray): Pair<Int, Int>? {
392
+ val metadata = try {
393
+ OneKeyEncodedImageInspector.inspect(ByteArrayInputStream(bytes))
394
+ } catch (_: Exception) {
395
+ noteDecodeFailure()
396
+ return null
397
+ }
398
+ if (metadata.animated) {
399
+ noteLimitRejection()
400
+ return null
401
+ }
402
+ val bounds = if (metadata.width > 0 && metadata.height > 0) {
403
+ metadata.width to metadata.height
404
+ } else {
405
+ extractStaticImageBounds(bytes)
406
+ }
407
+ if (bounds == null) {
408
+ noteDecodeFailure()
409
+ return null
410
+ }
411
+ if (bounds.first <= 0 || bounds.second <= 0) {
412
+ noteDecodeFailure()
413
+ return null
414
+ }
415
+ try {
416
+ OneKeyImageSafety.requireStaticDimensions(bounds.first, bounds.second)
417
+ } catch (_: Exception) {
418
+ noteLimitRejection()
419
+ return null
420
+ }
421
+ if (maxOf(bounds.first, bounds.second) > MAX_SOURCE_PIXEL_SIZE) {
422
+ noteLimitRejection()
423
+ return null
424
+ }
425
+ return bounds
426
+ }
427
+
428
+ internal fun extractStaticImageBounds(bytes: ByteArray): Pair<Int, Int>? {
429
+ // 1. PNG IHDR
430
+ if (
431
+ bytes.size >= 24 &&
432
+ bytes[0] == 0x89.toByte() &&
433
+ bytes[1] == 0x50.toByte() &&
434
+ bytes[2] == 0x4e.toByte() &&
435
+ bytes[3] == 0x47.toByte()
436
+ ) {
437
+ val width = u32Be(bytes, 16)
438
+ val height = u32Be(bytes, 20)
439
+ if (width > 0 && height > 0) return width to height
440
+ }
441
+ // 2. WebP (VP8 / VP8L / VP8X)
442
+ if (
443
+ bytes.size >= 25 &&
444
+ bytes[0] == 'R'.code.toByte() &&
445
+ bytes[1] == 'I'.code.toByte() &&
446
+ bytes[2] == 'F'.code.toByte() &&
447
+ bytes[3] == 'F'.code.toByte() &&
448
+ bytes[8] == 'W'.code.toByte() &&
449
+ bytes[9] == 'E'.code.toByte() &&
450
+ bytes[10] == 'B'.code.toByte() &&
451
+ bytes[11] == 'P'.code.toByte()
452
+ ) {
453
+ val chunk = String(bytes, 12, 4, Charsets.US_ASCII)
454
+ when (chunk) {
455
+ "VP8X" -> if (bytes.size >= 30) {
456
+ val w = u24Le(bytes, 24) + 1
457
+ val h = u24Le(bytes, 27) + 1
458
+ return w to h
459
+ }
460
+ "VP8 " -> if (
461
+ bytes.size >= 30 &&
462
+ (bytes[23].toInt() and 0xff) == 0x9d &&
463
+ (bytes[24].toInt() and 0xff) == 0x01 &&
464
+ (bytes[25].toInt() and 0xff) == 0x2a
465
+ ) {
466
+ val w = u16Le(bytes, 26) and 0x3fff
467
+ val h = u16Le(bytes, 28) and 0x3fff
468
+ if (w > 0 && h > 0) return w to h
469
+ }
470
+ "VP8L" -> if (bytes.size >= 25 && (bytes[20].toInt() and 0xff) == 0x2f) {
471
+ val b0 = bytes[21].toInt() and 0xff
472
+ val b1 = bytes[22].toInt() and 0xff
473
+ val b2 = bytes[23].toInt() and 0xff
474
+ val b3 = bytes[24].toInt() and 0xff
475
+ val bits = b0 or (b1 shl 8) or (b2 shl 16) or (b3 shl 24)
476
+ val w = (bits and 0x3fff) + 1
477
+ val h = ((bits ushr 14) and 0x3fff) + 1
478
+ return w to h
479
+ }
480
+ }
481
+ }
482
+ // 3. JPEG SOF markers
483
+ if (bytes.size >= 4 && (bytes[0].toInt() and 0xff) == 0xff && (bytes[1].toInt() and 0xff) == 0xd8) {
484
+ var pos = 2
485
+ while (pos + 4 <= bytes.size) {
486
+ if ((bytes[pos].toInt() and 0xff) != 0xff) {
487
+ pos += 1
488
+ continue
489
+ }
490
+ var marker = bytes[pos + 1].toInt() and 0xff
491
+ pos += 2
492
+ while (marker == 0xff && pos < bytes.size) {
493
+ marker = bytes[pos].toInt() and 0xff
494
+ pos += 1
495
+ }
496
+ if (marker == 0xd8 || marker == 0xd9 || marker in 0xd0..0xd7 || marker == 0x01) {
497
+ continue
498
+ }
499
+ if (pos + 2 > bytes.size) break
500
+ val segmentLength = u16Be(bytes, pos)
501
+ if (segmentLength < 2 || pos + segmentLength > bytes.size) break
502
+ if (marker in 0xc0..0xc3 || marker in 0xc5..0xc7 || marker in 0xc9..0xcb || marker in 0xcd..0xcf) {
503
+ if (segmentLength >= 7) {
504
+ val h = u16Be(bytes, pos + 3)
505
+ val w = u16Be(bytes, pos + 5)
506
+ if (w > 0 && h > 0) return w to h
507
+ }
508
+ break
509
+ }
510
+ pos += segmentLength
511
+ }
512
+ }
513
+ // 4. Platform BitmapFactory bounds fallback
514
+ return try {
515
+ val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
516
+ BitmapFactory.decodeByteArray(bytes, 0, bytes.size, options)
517
+ if (options.outWidth > 0 && options.outHeight > 0) {
518
+ options.outWidth to options.outHeight
519
+ } else null
520
+ } catch (_: Throwable) {
521
+ null
522
+ }
523
+ }
524
+
525
+ private fun u16Le(bytes: ByteArray, offset: Int): Int =
526
+ (bytes[offset].toInt() and 0xff) or ((bytes[offset + 1].toInt() and 0xff) shl 8)
527
+
528
+ private fun u16Be(bytes: ByteArray, offset: Int): Int =
529
+ ((bytes[offset].toInt() and 0xff) shl 8) or (bytes[offset + 1].toInt() and 0xff)
530
+
531
+ private fun u24Le(bytes: ByteArray, offset: Int): Int =
532
+ (bytes[offset].toInt() and 0xff) or
533
+ ((bytes[offset + 1].toInt() and 0xff) shl 8) or
534
+ ((bytes[offset + 2].toInt() and 0xff) shl 16)
535
+
536
+ private fun u32Be(bytes: ByteArray, offset: Int): Int =
537
+ ((bytes[offset].toInt() and 0xff) shl 24) or
538
+ ((bytes[offset + 1].toInt() and 0xff) shl 16) or
539
+ ((bytes[offset + 2].toInt() and 0xff) shl 8) or
540
+ (bytes[offset + 3].toInt() and 0xff)
541
+ }
@@ -53,7 +53,7 @@ class OneKeyMediaPreviewCache private constructor(context: Context, directory: F
53
53
  private val io = ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, LinkedBlockingQueue()) { task ->
54
54
  Thread(task, "onekey-media-preview-io").apply { isDaemon = true }
55
55
  }
56
- private val disk = DiskLruCacheWrapper.create(directory, 64L * 1024 * 1024)
56
+ private val disk = DiskLruCacheWrapper.create(directory, 128L * 1024 * 1024)
57
57
  private val memory = object : LruCache<String, Bitmap>(16 * 1024 * 1024) {
58
58
  override fun sizeOf(key: String, value: Bitmap): Int = value.allocationByteCount
59
59
  }