@onekeyfe/react-native-image 6.7.0-5 → 6.7.0-51
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 +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/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 +545 -0
- package/ios/OneKeyAvatarImageLoader.swift +1 -1
- package/ios/OneKeyIcon.swift +96 -0
- package/ios/OneKeyImage.swift +83 -2
- package/ios/OneKeyImageCache.swift +25 -5
- package/ios/OneKeyImageCoderBridge.h +4 -0
- package/ios/OneKeyImageCoderBridge.m +402 -1
- package/ios/OneKeyImageRequestContext.swift +8 -3
- package/ios/OneKeyImageStartupPrewarm.swift +564 -0
- package/ios/OneKeyMainThreadDecode.swift +338 -0
- package/ios/OneKeyMediaPreviewCache.swift +209 -0
- package/ios/OneKeyNamedSVG.swift +687 -0
- package/ios/tests/OneKeyIconTests.swift +665 -0
- package/ios/tests/OneKeyImageInfrastructureTests.swift +288 -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,199 @@
|
|
|
1
|
+
package com.margelo.nitro.onekeyimage
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.graphics.Bitmap
|
|
5
|
+
import android.graphics.BitmapFactory
|
|
6
|
+
import android.os.Handler
|
|
7
|
+
import android.os.Looper
|
|
8
|
+
import android.util.LruCache
|
|
9
|
+
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper
|
|
10
|
+
import com.bumptech.glide.signature.ObjectKey
|
|
11
|
+
import java.io.ByteArrayOutputStream
|
|
12
|
+
import java.io.File
|
|
13
|
+
import java.security.MessageDigest
|
|
14
|
+
import java.util.UUID
|
|
15
|
+
import java.util.concurrent.Future
|
|
16
|
+
import java.util.concurrent.LinkedBlockingQueue
|
|
17
|
+
import java.util.concurrent.ThreadPoolExecutor
|
|
18
|
+
import java.util.concurrent.TimeUnit
|
|
19
|
+
|
|
20
|
+
/** Native-only process cache. View slots retain their players and surfaces. */
|
|
21
|
+
class OneKeyMediaPreviewCache private constructor(context: Context, directory: File) {
|
|
22
|
+
companion object {
|
|
23
|
+
@Volatile private var instance: OneKeyMediaPreviewCache? = null
|
|
24
|
+
fun get(context: Context): OneKeyMediaPreviewCache = instance ?: synchronized(this) {
|
|
25
|
+
instance ?: OneKeyMediaPreviewCache(context.applicationContext,
|
|
26
|
+
File(context.cacheDir, "onekey-media-preview-v1")).also { instance = it }
|
|
27
|
+
}
|
|
28
|
+
fun targetEdge(pixels: Int): Int = listOf(128, 256, 512, 1024).firstOrNull { it >= pixels } ?: 1024
|
|
29
|
+
internal fun storageKey(uri: String, headers: Map<String, String>, edge: Int, fit: String): String {
|
|
30
|
+
val fields = headers.entries.map { it.key.lowercase(java.util.Locale.ROOT) to it.value }
|
|
31
|
+
.sortedWith(compareBy({ it.first }, { it.second }))
|
|
32
|
+
val values = listOf("onekey-media-preview-v1", uri, edge.toString(), fit) + fields.flatMap { listOf(it.first, it.second) }
|
|
33
|
+
val canonical = values.joinToString("") { "${it.toByteArray(Charsets.UTF_8).size}:$it" }
|
|
34
|
+
return MessageDigest.getInstance("SHA-256").digest(canonical.toByteArray(Charsets.UTF_8))
|
|
35
|
+
.joinToString("") { "%02x".format(it.toInt() and 255) }
|
|
36
|
+
}
|
|
37
|
+
internal fun isolated(context: Context, directory: File) = OneKeyMediaPreviewCache(context, directory)
|
|
38
|
+
}
|
|
39
|
+
class Subscription internal constructor(internal val key: String,
|
|
40
|
+
internal val image: (Bitmap) -> Unit, internal val extract: (UUID) -> Unit,
|
|
41
|
+
internal val revoked: () -> Unit, internal val failure: () -> Unit) {
|
|
42
|
+
internal var cancelled = false
|
|
43
|
+
}
|
|
44
|
+
private class Flight(val key: String, val storageKey: String, val policy: String,
|
|
45
|
+
val memoryGeneration: Int, val diskGeneration: Int) {
|
|
46
|
+
val subscribers = mutableListOf<Subscription>()
|
|
47
|
+
var lease: UUID? = null
|
|
48
|
+
var timeout: Runnable? = null
|
|
49
|
+
var ready = false
|
|
50
|
+
var lookupTask: Future<*>? = null
|
|
51
|
+
}
|
|
52
|
+
private val main = Handler(Looper.getMainLooper())
|
|
53
|
+
private val io = ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, LinkedBlockingQueue()) { task ->
|
|
54
|
+
Thread(task, "onekey-media-preview-io").apply { isDaemon = true }
|
|
55
|
+
}
|
|
56
|
+
private val disk = DiskLruCacheWrapper.create(directory, 128L * 1024 * 1024)
|
|
57
|
+
private val memory = object : LruCache<String, Bitmap>(16 * 1024 * 1024) {
|
|
58
|
+
override fun sizeOf(key: String, value: Bitmap): Int = value.allocationByteCount
|
|
59
|
+
}
|
|
60
|
+
private val flights = linkedMapOf<String, Flight>()
|
|
61
|
+
private val queue = ArrayDeque<String>()
|
|
62
|
+
private var active = 0
|
|
63
|
+
private var pendingPersistenceBytes = 0
|
|
64
|
+
private var memoryGeneration = 0
|
|
65
|
+
private var diskGeneration = 0
|
|
66
|
+
private fun checkMain() = check(Looper.myLooper() == Looper.getMainLooper())
|
|
67
|
+
private fun memoryPolicy(policy: String) = policy == "memory" || policy == "memory-disk"
|
|
68
|
+
private fun diskPolicy(policy: String) = policy == "disk" || policy == "memory-disk"
|
|
69
|
+
|
|
70
|
+
fun request(uri: String, headers: Map<String, String>, edge: Int, fit: String, policy: String,
|
|
71
|
+
onImage: (Bitmap) -> Unit, onExtract: (UUID) -> Unit, onRevoked: () -> Unit,
|
|
72
|
+
onFailure: () -> Unit): Subscription {
|
|
73
|
+
checkMain()
|
|
74
|
+
val storageKey = storageKey(uri, headers, targetEdge(edge.coerceAtLeast(1)), fit)
|
|
75
|
+
val key = "$storageKey|$policy"
|
|
76
|
+
val subscriber = Subscription(key, onImage, onExtract, onRevoked, onFailure)
|
|
77
|
+
flights[key]?.let { it.subscribers.add(subscriber); return subscriber }
|
|
78
|
+
if (flights.size >= 130) { main.post { if (!subscriber.cancelled) subscriber.failure() }; return subscriber }
|
|
79
|
+
val flight = Flight(key, storageKey, policy, memoryGeneration, diskGeneration)
|
|
80
|
+
flight.subscribers.add(subscriber); flights[key] = flight
|
|
81
|
+
if (memoryPolicy(policy)) memory.get(storageKey)?.let { bitmap ->
|
|
82
|
+
main.post { finish(flight, bitmap) }; return subscriber
|
|
83
|
+
}
|
|
84
|
+
flight.lookupTask = io.submit {
|
|
85
|
+
val bitmap = if (diskPolicy(policy)) runCatching {
|
|
86
|
+
disk.get(ObjectKey(storageKey))?.let { file ->
|
|
87
|
+
if (file.length() > 8L * 1024 * 1024) null else {
|
|
88
|
+
val data = file.inputStream().use { input ->
|
|
89
|
+
val output = ByteArrayOutputStream()
|
|
90
|
+
val buffer = ByteArray(8192)
|
|
91
|
+
while (output.size() <= 8 * 1024 * 1024) {
|
|
92
|
+
val count = input.read(buffer, 0, minOf(buffer.size, 8 * 1024 * 1024 + 1 - output.size()))
|
|
93
|
+
if (count < 0) break
|
|
94
|
+
output.write(buffer, 0, count)
|
|
95
|
+
}
|
|
96
|
+
output.toByteArray()
|
|
97
|
+
}
|
|
98
|
+
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
|
99
|
+
if (data.size > 8 * 1024 * 1024) null else {
|
|
100
|
+
BitmapFactory.decodeByteArray(data, 0, data.size, bounds)
|
|
101
|
+
if (bounds.outWidth in 1..1024 && bounds.outHeight in 1..1024)
|
|
102
|
+
BitmapFactory.decodeByteArray(data, 0, data.size) else null
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}.also { if (it == null) disk.delete(ObjectKey(storageKey)) }
|
|
106
|
+
}.getOrNull() else null
|
|
107
|
+
main.post {
|
|
108
|
+
if (flights[key] !== flight) return@post
|
|
109
|
+
if (bitmap != null && flight.diskGeneration == diskGeneration) {
|
|
110
|
+
if (memoryPolicy(policy) && flight.memoryGeneration == memoryGeneration) storeMemory(storageKey, bitmap)
|
|
111
|
+
finish(flight, bitmap)
|
|
112
|
+
} else { flight.ready = true; queue.addLast(key); pump() }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return subscriber
|
|
116
|
+
}
|
|
117
|
+
fun cancel(subscription: Subscription?) {
|
|
118
|
+
checkMain()
|
|
119
|
+
if (subscription == null) return
|
|
120
|
+
subscription.cancelled = true
|
|
121
|
+
val flight = flights[subscription.key] ?: return
|
|
122
|
+
val index = flight.subscribers.indexOf(subscription)
|
|
123
|
+
if (index < 0) return
|
|
124
|
+
val owned = index == 0 && flight.lease != null
|
|
125
|
+
flight.subscribers.removeAt(index)
|
|
126
|
+
if (owned) {
|
|
127
|
+
flight.timeout?.let(main::removeCallbacks); flight.lease = null; active--
|
|
128
|
+
subscription.revoked()
|
|
129
|
+
}
|
|
130
|
+
if (flight.subscribers.isEmpty()) {
|
|
131
|
+
cancelLookup(flight)
|
|
132
|
+
flights.remove(flight.key); queue.removeAll { it == flight.key }
|
|
133
|
+
} else if (owned) queue.addFirst(flight.key)
|
|
134
|
+
pump()
|
|
135
|
+
}
|
|
136
|
+
fun fail(lease: UUID) {
|
|
137
|
+
checkMain(); flights.values.firstOrNull { it.lease == lease }?.let { finish(it, null) }
|
|
138
|
+
}
|
|
139
|
+
fun complete(lease: UUID, bitmap: Bitmap) {
|
|
140
|
+
checkMain()
|
|
141
|
+
val flight = flights.values.firstOrNull { it.lease == lease } ?: return
|
|
142
|
+
if (bitmap.width !in 1..1024 || bitmap.height !in 1..1024) { finish(flight, null); return }
|
|
143
|
+
if (memoryPolicy(flight.policy) && flight.memoryGeneration == memoryGeneration) storeMemory(flight.storageKey, bitmap)
|
|
144
|
+
finish(flight, bitmap)
|
|
145
|
+
val cost = bitmap.allocationByteCount
|
|
146
|
+
if (!diskPolicy(flight.policy) || pendingPersistenceBytes + cost > 16 * 1024 * 1024) return
|
|
147
|
+
pendingPersistenceBytes += cost
|
|
148
|
+
io.execute {
|
|
149
|
+
val data = runCatching { ByteArrayOutputStream().use {
|
|
150
|
+
bitmap.compress(Bitmap.CompressFormat.PNG, 100, it); it.toByteArray()
|
|
151
|
+
} }.getOrNull()
|
|
152
|
+
main.post {
|
|
153
|
+
if (flight.diskGeneration != diskGeneration || data == null || data.size > 8 * 1024 * 1024) {
|
|
154
|
+
pendingPersistenceBytes -= cost; return@post
|
|
155
|
+
}
|
|
156
|
+
// Serial writes and clears seal the generation-check/write window.
|
|
157
|
+
io.execute {
|
|
158
|
+
runCatching { disk.put(ObjectKey(flight.storageKey)) { file ->
|
|
159
|
+
file.outputStream().use { it.write(data) }; true
|
|
160
|
+
} }
|
|
161
|
+
main.post { pendingPersistenceBytes -= cost }
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
fun clearMemory() { checkMain(); memoryGeneration++; memory.evictAll() }
|
|
167
|
+
fun clearDisk(completion: () -> Unit) {
|
|
168
|
+
checkMain(); diskGeneration++
|
|
169
|
+
io.execute { runCatching { disk.clear() }; completion() }
|
|
170
|
+
}
|
|
171
|
+
private fun storeMemory(key: String, bitmap: Bitmap) {
|
|
172
|
+
if (bitmap.allocationByteCount <= 16 * 1024 * 1024) memory.put(key, bitmap)
|
|
173
|
+
while (memory.snapshot().size > 64) memory.remove(memory.snapshot().keys.first())
|
|
174
|
+
}
|
|
175
|
+
private fun pump() {
|
|
176
|
+
while (active < 2 && queue.isNotEmpty()) {
|
|
177
|
+
val flight = flights[queue.removeFirst()] ?: continue
|
|
178
|
+
if (!flight.ready || flight.lease != null || flight.subscribers.isEmpty()) continue
|
|
179
|
+
val lease = UUID.randomUUID(); flight.lease = lease; active++
|
|
180
|
+
val timeout = Runnable { if (flight.lease == lease) finish(flight, null) }
|
|
181
|
+
flight.timeout = timeout; main.postDelayed(timeout, 10_000)
|
|
182
|
+
flight.subscribers.first().extract(lease)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
private fun cancelLookup(flight: Flight) {
|
|
186
|
+
flight.lookupTask?.let { task -> task.cancel(false); (task as? Runnable)?.let(io::remove) }
|
|
187
|
+
flight.lookupTask = null
|
|
188
|
+
}
|
|
189
|
+
private fun finish(flight: Flight, bitmap: Bitmap?) {
|
|
190
|
+
if (flights[flight.key] !== flight) return
|
|
191
|
+
cancelLookup(flight)
|
|
192
|
+
flights.remove(flight.key); queue.removeAll { it == flight.key }
|
|
193
|
+
if (flight.lease != null) { active--; flight.subscribers.firstOrNull()?.revoked?.invoke() }
|
|
194
|
+
flight.timeout?.let(main::removeCallbacks); flight.lease = null
|
|
195
|
+
val subscribers = flight.subscribers.toList(); flight.subscribers.clear()
|
|
196
|
+
subscribers.filterNot { it.cancelled }.forEach { if (bitmap != null) it.image(bitmap) else it.failure() }
|
|
197
|
+
pump()
|
|
198
|
+
}
|
|
199
|
+
}
|