@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
|
@@ -11,6 +11,7 @@ import android.graphics.Outline
|
|
|
11
11
|
import android.graphics.Paint
|
|
12
12
|
import android.graphics.Path
|
|
13
13
|
import android.graphics.drawable.Animatable
|
|
14
|
+
import android.graphics.drawable.BitmapDrawable
|
|
14
15
|
import android.graphics.drawable.Drawable
|
|
15
16
|
import android.os.Build
|
|
16
17
|
import android.os.Looper
|
|
@@ -572,7 +573,41 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
572
573
|
addAll(OneKeyImageMemoryVariantRegistry.candidates(family, exactVariant))
|
|
573
574
|
}.distinct()
|
|
574
575
|
|
|
576
|
+
val allowPrewarmedCache = OneKeyImageModel.headers(sourceHeadersJson).isEmpty()
|
|
575
577
|
for (candidate in candidates) {
|
|
578
|
+
val prewarmedBitmap = if (allowPrewarmedCache) {
|
|
579
|
+
OneKeyImageStartupPrewarm.cachedBitmap(
|
|
580
|
+
requestUrl = candidate.requestUrl,
|
|
581
|
+
width = candidate.width,
|
|
582
|
+
height = candidate.height,
|
|
583
|
+
round = candidate.round,
|
|
584
|
+
headersJson = sourceHeadersJson,
|
|
585
|
+
)
|
|
586
|
+
} else {
|
|
587
|
+
null
|
|
588
|
+
}
|
|
589
|
+
if (prewarmedBitmap != null) {
|
|
590
|
+
clearMemoryPreviewTarget()
|
|
591
|
+
memoryPreviewTarget = null
|
|
592
|
+
memoryPreviewFamily = family
|
|
593
|
+
scalesCenterMemoryPreview = shouldScaleCenterMemoryPreview(
|
|
594
|
+
contentFit = contentFit ?: OneKeyImageContentFit.COVER,
|
|
595
|
+
candidate = candidate,
|
|
596
|
+
target = exactVariant,
|
|
597
|
+
)
|
|
598
|
+
applyContentFit()
|
|
599
|
+
displayState = DisplayState.IMAGE
|
|
600
|
+
requestActive = false
|
|
601
|
+
isPreservingDisplayedImage = false
|
|
602
|
+
hostView.skeletonRequested = false
|
|
603
|
+
hostView.drawStateSymbol = false
|
|
604
|
+
hostView.setBackgroundColor(Color.TRANSPARENT)
|
|
605
|
+
hostView.setImageDrawable(BitmapDrawable(hostView.resources, prewarmedBitmap))
|
|
606
|
+
applyAutoplay()
|
|
607
|
+
OneKeyImageMemoryVariantRegistry.record(family, candidate)
|
|
608
|
+
return true
|
|
609
|
+
}
|
|
610
|
+
|
|
576
611
|
var synchronousResource: Drawable? = null
|
|
577
612
|
val target = object : CustomTarget<Drawable>(candidate.width, candidate.height) {
|
|
578
613
|
override fun onResourceReady(
|
|
@@ -631,12 +666,163 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
631
666
|
return false
|
|
632
667
|
}
|
|
633
668
|
|
|
669
|
+
private fun showDirectPrewarmedOrSyncImageIfAvailable(
|
|
670
|
+
rawUrl: String,
|
|
671
|
+
optimizedUrl: String,
|
|
672
|
+
decodeDimensions: OneKeyImageDecodeDimensions,
|
|
673
|
+
memoryFamily: OneKeyImageMemoryFamilyKey,
|
|
674
|
+
policy: OneKeyImageCachePolicy,
|
|
675
|
+
requestGeneration: Long,
|
|
676
|
+
): Boolean {
|
|
677
|
+
if (
|
|
678
|
+
requestGeneration != generation ||
|
|
679
|
+
disposed ||
|
|
680
|
+
(policy != OneKeyImageCachePolicy.MEMORY && policy != OneKeyImageCachePolicy.MEMORY_DISK) ||
|
|
681
|
+
OneKeyImageModel.headers(sourceHeadersJson).isNotEmpty()
|
|
682
|
+
) {
|
|
683
|
+
return false
|
|
684
|
+
}
|
|
685
|
+
val urls = if (optimizedUrl == rawUrl) listOf(optimizedUrl) else listOf(optimizedUrl, rawUrl)
|
|
686
|
+
val isRound = round == true
|
|
687
|
+
for (url in urls) {
|
|
688
|
+
val bitmap = OneKeyImageStartupPrewarm.cachedBitmap(
|
|
689
|
+
requestUrl = url,
|
|
690
|
+
width = decodeDimensions.width,
|
|
691
|
+
height = decodeDimensions.height,
|
|
692
|
+
round = isRound,
|
|
693
|
+
headersJson = sourceHeadersJson,
|
|
694
|
+
) ?: continue
|
|
695
|
+
val hitVariant = OneKeyImageMemoryVariant(
|
|
696
|
+
requestUrl = url,
|
|
697
|
+
width = decodeDimensions.width,
|
|
698
|
+
height = decodeDimensions.height,
|
|
699
|
+
round = isRound,
|
|
700
|
+
)
|
|
701
|
+
clearMemoryPreviewTarget()
|
|
702
|
+
requestActive = false
|
|
703
|
+
displayState = DisplayState.IMAGE
|
|
704
|
+
isPreservingDisplayedImage = false
|
|
705
|
+
hostView.skeletonRequested = false
|
|
706
|
+
hostView.drawStateSymbol = false
|
|
707
|
+
hostView.setBackgroundColor(Color.TRANSPARENT)
|
|
708
|
+
resetImageTransition()
|
|
709
|
+
hostView.setImageDrawable(BitmapDrawable(hostView.resources, bitmap))
|
|
710
|
+
applyContentFit()
|
|
711
|
+
applyAutoplay()
|
|
712
|
+
OneKeyImageMemoryVariantRegistry.record(memoryFamily, hitVariant)
|
|
713
|
+
OneKeyImageStartupPrewarm.note(
|
|
714
|
+
cacheDir = context.applicationContext?.cacheDir ?: context.cacheDir,
|
|
715
|
+
requestUrl = url,
|
|
716
|
+
rawUrl = rawUrl,
|
|
717
|
+
headersJson = sourceHeadersJson,
|
|
718
|
+
cachePolicy = policy,
|
|
719
|
+
width = decodeDimensions.width,
|
|
720
|
+
height = decodeDimensions.height,
|
|
721
|
+
round = isRound,
|
|
722
|
+
)
|
|
723
|
+
val loadCallback = onLoad
|
|
724
|
+
val loadEndCallback = onLoadEnd
|
|
725
|
+
OneKeyTerminalEventPair.dispatch(
|
|
726
|
+
primary = loadCallback?.let { callback ->
|
|
727
|
+
{
|
|
728
|
+
callback(
|
|
729
|
+
bitmap.width.coerceAtLeast(0).toDouble(),
|
|
730
|
+
bitmap.height.coerceAtLeast(0).toDouble(),
|
|
731
|
+
OneKeyImageCacheType.MEMORY,
|
|
732
|
+
)
|
|
733
|
+
}
|
|
734
|
+
},
|
|
735
|
+
onLoadEnd = loadEndCallback,
|
|
736
|
+
)
|
|
737
|
+
if (requestGeneration == generation && !disposed) {
|
|
738
|
+
scheduleOnDisplay(requestGeneration)
|
|
739
|
+
}
|
|
740
|
+
return true
|
|
741
|
+
}
|
|
742
|
+
return false
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
private fun hasGlideExactMemoryCacheHit(
|
|
746
|
+
rawUrl: String,
|
|
747
|
+
optimizedUrl: String,
|
|
748
|
+
decodeDimensions: OneKeyImageDecodeDimensions,
|
|
749
|
+
policy: OneKeyImageCachePolicy,
|
|
750
|
+
requestGeneration: Long,
|
|
751
|
+
): Boolean {
|
|
752
|
+
if (
|
|
753
|
+
Looper.myLooper() != Looper.getMainLooper() ||
|
|
754
|
+
disposed ||
|
|
755
|
+
requestGeneration != generation ||
|
|
756
|
+
(policy != OneKeyImageCachePolicy.MEMORY && policy != OneKeyImageCachePolicy.MEMORY_DISK)
|
|
757
|
+
) {
|
|
758
|
+
return false
|
|
759
|
+
}
|
|
760
|
+
val urls = if (optimizedUrl == rawUrl) listOf(optimizedUrl) else listOf(optimizedUrl, rawUrl)
|
|
761
|
+
val isRound = round == true
|
|
762
|
+
for (url in urls) {
|
|
763
|
+
var hit = false
|
|
764
|
+
val probeTarget = object : CustomTarget<Drawable>(decodeDimensions.width, decodeDimensions.height) {
|
|
765
|
+
override fun onResourceReady(
|
|
766
|
+
resource: Drawable,
|
|
767
|
+
transition: Transition<in Drawable>?,
|
|
768
|
+
) {
|
|
769
|
+
hit = true
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
override fun onLoadCleared(placeholder: Drawable?) = Unit
|
|
773
|
+
}
|
|
774
|
+
requestManager()
|
|
775
|
+
.asDrawable()
|
|
776
|
+
.load(OneKeyImageModel.build(url, sourceHeadersJson))
|
|
777
|
+
.apply(memoryProbeRequestOptions(requestOptions(policy, url, isRound)))
|
|
778
|
+
.override(decodeDimensions.width, decodeDimensions.height)
|
|
779
|
+
.into(probeTarget)
|
|
780
|
+
requestManager().clear(probeTarget)
|
|
781
|
+
if (hit) return true
|
|
782
|
+
}
|
|
783
|
+
return false
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
private fun promoteSmallCachedImageToMemory(
|
|
787
|
+
rawUrl: String,
|
|
788
|
+
optimizedUrl: String,
|
|
789
|
+
decodeDimensions: OneKeyImageDecodeDimensions,
|
|
790
|
+
policy: OneKeyImageCachePolicy,
|
|
791
|
+
): Boolean {
|
|
792
|
+
if (policy != OneKeyImageCachePolicy.MEMORY_DISK) return false
|
|
793
|
+
if (OneKeyImageModel.headers(sourceHeadersJson).isNotEmpty()) return false
|
|
794
|
+
if (maxOf(decodeDimensions.width, decodeDimensions.height) > OneKeyMainThreadDecode.MAX_THUMBNAIL_PIXEL_SIZE) {
|
|
795
|
+
return false
|
|
796
|
+
}
|
|
797
|
+
val appCacheDir = context.applicationContext?.cacheDir ?: context.cacheDir
|
|
798
|
+
val urls = if (optimizedUrl == rawUrl) listOf(optimizedUrl) else listOf(optimizedUrl, rawUrl)
|
|
799
|
+
val isRound = round == true
|
|
800
|
+
val promoted = OneKeyMainThreadDecode.run {
|
|
801
|
+
for (url in urls) {
|
|
802
|
+
if (
|
|
803
|
+
OneKeyMainThreadDecode.promoteToMemory(
|
|
804
|
+
cacheDir = appCacheDir,
|
|
805
|
+
url = url,
|
|
806
|
+
rawUrl = rawUrl,
|
|
807
|
+
width = decodeDimensions.width,
|
|
808
|
+
height = decodeDimensions.height,
|
|
809
|
+
round = isRound,
|
|
810
|
+
)
|
|
811
|
+
) {
|
|
812
|
+
return@run true
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
null
|
|
816
|
+
}
|
|
817
|
+
return promoted == true
|
|
818
|
+
}
|
|
819
|
+
|
|
634
820
|
private fun startLoad(force: Boolean) {
|
|
635
821
|
if (disposed) return
|
|
636
822
|
if (hostView.width <= 0 || hostView.height <= 0) {
|
|
637
823
|
cancelCurrent(invalidateGeneration = true, clearMemoryPreview = false)
|
|
638
824
|
lastSignature = null
|
|
639
|
-
if (
|
|
825
|
+
if (memoryPreviewFamily == null || hostView.drawable == null) {
|
|
640
826
|
showLoading(requestIsActive = false)
|
|
641
827
|
}
|
|
642
828
|
return
|
|
@@ -692,15 +878,53 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
692
878
|
} else rawUrl
|
|
693
879
|
val decodeDimensions = OneKeyImageDecodeDimensions.forRender(hostView.width, hostView.height)
|
|
694
880
|
val memoryFamily = OneKeyImageMemoryFamilyKey.from(rawUrl, sourceHeadersJson)
|
|
881
|
+
val effectivePolicy = cachePolicy ?: OneKeyImageCachePolicy.MEMORY_DISK
|
|
695
882
|
val exactVariant = OneKeyImageMemoryVariant(
|
|
696
883
|
requestUrl = optimizedUrl,
|
|
697
884
|
width = decodeDimensions.width,
|
|
698
885
|
height = decodeDimensions.height,
|
|
699
886
|
round = round == true,
|
|
700
887
|
)
|
|
888
|
+
if (
|
|
889
|
+
showDirectPrewarmedOrSyncImageIfAvailable(
|
|
890
|
+
rawUrl = rawUrl,
|
|
891
|
+
optimizedUrl = optimizedUrl,
|
|
892
|
+
decodeDimensions = decodeDimensions,
|
|
893
|
+
memoryFamily = memoryFamily,
|
|
894
|
+
policy = effectivePolicy,
|
|
895
|
+
requestGeneration = requestGeneration,
|
|
896
|
+
)
|
|
897
|
+
) {
|
|
898
|
+
return
|
|
899
|
+
}
|
|
900
|
+
if (
|
|
901
|
+
!hasGlideExactMemoryCacheHit(
|
|
902
|
+
rawUrl = rawUrl,
|
|
903
|
+
optimizedUrl = optimizedUrl,
|
|
904
|
+
decodeDimensions = decodeDimensions,
|
|
905
|
+
policy = effectivePolicy,
|
|
906
|
+
requestGeneration = requestGeneration,
|
|
907
|
+
) &&
|
|
908
|
+
promoteSmallCachedImageToMemory(
|
|
909
|
+
rawUrl = rawUrl,
|
|
910
|
+
optimizedUrl = optimizedUrl,
|
|
911
|
+
decodeDimensions = decodeDimensions,
|
|
912
|
+
policy = effectivePolicy,
|
|
913
|
+
) &&
|
|
914
|
+
showDirectPrewarmedOrSyncImageIfAvailable(
|
|
915
|
+
rawUrl = rawUrl,
|
|
916
|
+
optimizedUrl = optimizedUrl,
|
|
917
|
+
decodeDimensions = decodeDimensions,
|
|
918
|
+
memoryFamily = memoryFamily,
|
|
919
|
+
policy = effectivePolicy,
|
|
920
|
+
requestGeneration = requestGeneration,
|
|
921
|
+
)
|
|
922
|
+
) {
|
|
923
|
+
return
|
|
924
|
+
}
|
|
701
925
|
if (
|
|
702
926
|
shouldProbeMemoryPreview(
|
|
703
|
-
hasTarget =
|
|
927
|
+
hasTarget = memoryPreviewFamily != null,
|
|
704
928
|
sameFamily = memoryPreviewFamily == memoryFamily,
|
|
705
929
|
hasDrawable = hostView.drawable != null,
|
|
706
930
|
)
|
|
@@ -709,7 +933,7 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
709
933
|
family = memoryFamily,
|
|
710
934
|
exactVariant = exactVariant,
|
|
711
935
|
includeExact = false,
|
|
712
|
-
policy =
|
|
936
|
+
policy = effectivePolicy,
|
|
713
937
|
requestGeneration = requestGeneration,
|
|
714
938
|
)
|
|
715
939
|
}
|
|
@@ -719,7 +943,7 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
719
943
|
requestGeneration = requestGeneration,
|
|
720
944
|
mayFallbackToRaw = optimizedUrl != rawUrl,
|
|
721
945
|
headersJson = sourceHeadersJson,
|
|
722
|
-
policy =
|
|
946
|
+
policy = effectivePolicy,
|
|
723
947
|
decodeDimensions = decodeDimensions,
|
|
724
948
|
memoryFamily = memoryFamily,
|
|
725
949
|
memoryVariant = exactVariant,
|
|
@@ -747,13 +971,13 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
747
971
|
if (requestGeneration != generation || disposed) return
|
|
748
972
|
showLoading(
|
|
749
973
|
requestIsActive = true,
|
|
750
|
-
preserveDisplayedImage =
|
|
974
|
+
preserveDisplayedImage = memoryPreviewFamily != null && hostView.drawable != null,
|
|
751
975
|
)
|
|
752
976
|
}
|
|
753
977
|
|
|
754
978
|
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
|
|
755
979
|
if (requestGeneration != generation || disposed) return
|
|
756
|
-
val replacesMemoryPreview =
|
|
980
|
+
val replacesMemoryPreview = memoryPreviewFamily != null && hostView.drawable != null
|
|
757
981
|
requestActive = false
|
|
758
982
|
displayState = DisplayState.IMAGE
|
|
759
983
|
hostView.skeletonRequested = false
|
|
@@ -767,6 +991,16 @@ class HybridOneKeyImage(private val context: ThemedReactContext) :
|
|
|
767
991
|
policy == OneKeyImageCachePolicy.MEMORY_DISK)
|
|
768
992
|
) {
|
|
769
993
|
OneKeyImageMemoryVariantRegistry.record(memoryFamily, memoryVariant)
|
|
994
|
+
OneKeyImageStartupPrewarm.note(
|
|
995
|
+
cacheDir = context.applicationContext?.cacheDir ?: context.cacheDir,
|
|
996
|
+
requestUrl = requestUrl,
|
|
997
|
+
rawUrl = rawUrl,
|
|
998
|
+
headersJson = headersJson,
|
|
999
|
+
cachePolicy = policy,
|
|
1000
|
+
width = decodeDimensions.width,
|
|
1001
|
+
height = decodeDimensions.height,
|
|
1002
|
+
round = memoryVariant.round,
|
|
1003
|
+
)
|
|
770
1004
|
}
|
|
771
1005
|
clearMemoryPreviewTarget()
|
|
772
1006
|
applyLoadedImageTransition(
|
|
@@ -95,14 +95,22 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
|
|
|
95
95
|
val context = NitroModules.applicationContext
|
|
96
96
|
?: throw IllegalStateException("React application context is unavailable")
|
|
97
97
|
withContext(Dispatchers.Main) {
|
|
98
|
+
OneKeyMediaPreviewCache.get(context).clearMemory()
|
|
98
99
|
Glide.get(context).clearMemory()
|
|
99
100
|
OneKeyImageMemoryVariantRegistry.clear()
|
|
101
|
+
OneKeyImageStartupPrewarm.clearMemoryCache()
|
|
100
102
|
}
|
|
101
103
|
}
|
|
102
104
|
|
|
103
105
|
override fun clearDisk(): Promise<Unit> = Promise.async {
|
|
104
106
|
val context = NitroModules.applicationContext
|
|
105
107
|
?: throw IllegalStateException("React application context is unavailable")
|
|
108
|
+
OneKeyImageStartupPrewarm.clearDiskCache(context.cacheDir)
|
|
109
|
+
kotlinx.coroutines.suspendCancellableCoroutine<Unit> { continuation ->
|
|
110
|
+
android.os.Handler(android.os.Looper.getMainLooper()).post {
|
|
111
|
+
OneKeyMediaPreviewCache.get(context).clearDisk { continuation.resumeWith(Result.success(Unit)) }
|
|
112
|
+
}
|
|
113
|
+
}
|
|
106
114
|
Glide.get(context).clearDiskCache()
|
|
107
115
|
}
|
|
108
116
|
|
|
@@ -110,8 +118,16 @@ class HybridOneKeyImageCache : HybridOneKeyImageCacheSpec() {
|
|
|
110
118
|
val context = NitroModules.applicationContext
|
|
111
119
|
?: throw IllegalStateException("React application context is unavailable")
|
|
112
120
|
withContext(Dispatchers.Main) {
|
|
121
|
+
OneKeyMediaPreviewCache.get(context).clearMemory()
|
|
113
122
|
Glide.get(context).clearMemory()
|
|
114
123
|
OneKeyImageMemoryVariantRegistry.clear()
|
|
124
|
+
OneKeyImageStartupPrewarm.clearMemoryCache()
|
|
125
|
+
}
|
|
126
|
+
OneKeyImageStartupPrewarm.clearDiskCache(context.cacheDir)
|
|
127
|
+
kotlinx.coroutines.suspendCancellableCoroutine<Unit> { continuation ->
|
|
128
|
+
android.os.Handler(android.os.Looper.getMainLooper()).post {
|
|
129
|
+
OneKeyMediaPreviewCache.get(context).clearDisk { continuation.resumeWith(Result.success(Unit)) }
|
|
130
|
+
}
|
|
115
131
|
}
|
|
116
132
|
Glide.get(context).clearDiskCache()
|
|
117
133
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
package com.margelo.nitro.onekeyimage
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import com.bumptech.glide.Glide
|
|
5
|
+
import com.bumptech.glide.GlideBuilder
|
|
6
|
+
import com.bumptech.glide.Registry
|
|
7
|
+
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
|
|
8
|
+
import com.bumptech.glide.module.GlideModule
|
|
9
|
+
|
|
10
|
+
@Suppress("DEPRECATION")
|
|
11
|
+
class OneKeyImageGlideModule : GlideModule {
|
|
12
|
+
override fun applyOptions(context: Context, builder: GlideBuilder) {
|
|
13
|
+
builder.setDiskCache(InternalCacheDiskCacheFactory(context, 320L * 1024 * 1024))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {}
|
|
17
|
+
}
|
|
@@ -44,6 +44,7 @@ internal object OneKeyImageGlideRegistry {
|
|
|
44
44
|
private var registered = false
|
|
45
45
|
|
|
46
46
|
fun ensureRegistered(context: Context) {
|
|
47
|
+
OneKeyImageStartupTrigger.startIfNeeded(context)
|
|
47
48
|
if (registered) return
|
|
48
49
|
synchronized(this) {
|
|
49
50
|
if (registered) return
|
|
@@ -80,6 +81,11 @@ internal object OneKeyImageGlideRegistry {
|
|
|
80
81
|
InputStream::class.java,
|
|
81
82
|
OneKeyImageRemoteModelLoaderFactory(imageHttpClient),
|
|
82
83
|
)
|
|
84
|
+
registry.append(
|
|
85
|
+
ByteBuffer::class.java,
|
|
86
|
+
SVG::class.java,
|
|
87
|
+
OneKeyByteBufferSvgDecoder(),
|
|
88
|
+
)
|
|
83
89
|
registry.append(
|
|
84
90
|
InputStream::class.java,
|
|
85
91
|
SVG::class.java,
|
|
@@ -255,6 +261,19 @@ private class OneKeyDataUriFetcher(private val dataUri: String) : DataFetcher<By
|
|
|
255
261
|
}
|
|
256
262
|
}
|
|
257
263
|
|
|
264
|
+
private class OneKeyByteBufferSvgDecoder : ResourceDecoder<ByteBuffer, SVG> {
|
|
265
|
+
private val delegate = OneKeySvgDecoder()
|
|
266
|
+
|
|
267
|
+
override fun handles(source: ByteBuffer, options: Options): Boolean = true
|
|
268
|
+
|
|
269
|
+
override fun decode(source: ByteBuffer, width: Int, height: Int, options: Options): Resource<SVG> {
|
|
270
|
+
val copy = source.asReadOnlyBuffer()
|
|
271
|
+
copy.position(0)
|
|
272
|
+
OneKeyImageSafety.requireEncodedLength(copy.remaining().toLong(), OneKeyImageSafety.MAX_ENCODED_BYTES)
|
|
273
|
+
return delegate.decode(com.bumptech.glide.util.ByteBufferUtil.toStream(copy), width, height, options)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
258
277
|
private class OneKeySvgDecoder : ResourceDecoder<InputStream, SVG> {
|
|
259
278
|
override fun handles(source: InputStream, options: Options): Boolean = true
|
|
260
279
|
|
|
@@ -266,7 +285,7 @@ private class OneKeySvgDecoder : ResourceDecoder<InputStream, SVG> {
|
|
|
266
285
|
options: Options,
|
|
267
286
|
): Resource<SVG> {
|
|
268
287
|
val svg = try {
|
|
269
|
-
|
|
288
|
+
OneKeySVG.parse(source)
|
|
270
289
|
} catch (error: SVGParseException) {
|
|
271
290
|
throw IOException("Cannot decode SVG", error)
|
|
272
291
|
}
|
|
@@ -5,6 +5,7 @@ import com.facebook.react.bridge.NativeModule
|
|
|
5
5
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
6
|
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
7
7
|
import com.facebook.react.uimanager.ViewManager
|
|
8
|
+
import com.margelo.nitro.onekeyimage.views.HybridOneKeyIconManager
|
|
8
9
|
import com.margelo.nitro.onekeyimage.views.HybridOneKeyImageManager
|
|
9
10
|
|
|
10
11
|
class OneKeyImagePackage : BaseReactPackage() {
|
|
@@ -17,7 +18,7 @@ class OneKeyImagePackage : BaseReactPackage() {
|
|
|
17
18
|
reactContext: ReactApplicationContext,
|
|
18
19
|
): List<ViewManager<*, *>> {
|
|
19
20
|
OneKeyImageGlideRegistry.ensureRegistered(reactContext)
|
|
20
|
-
return listOf(HybridOneKeyImageManager())
|
|
21
|
+
return listOf(HybridOneKeyImageManager(), HybridOneKeyIconManager())
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
companion object {
|
|
@@ -129,6 +129,47 @@ internal object OneKeyImageSafety {
|
|
|
129
129
|
return decodedBytes.coerceAtLeast(0L)
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Decodes Base64 payloads using `android.util.Base64` (available since API 8,
|
|
134
|
+
* compatible with minSdkVersion 24). Falls back to a pure-Kotlin MIME Base64
|
|
135
|
+
* decoder only when running in JVM unit tests where `android.util.Base64.decode`
|
|
136
|
+
* is stubbed to return `null`.
|
|
137
|
+
*/
|
|
138
|
+
fun decodeDataUriBase64(payload: String): ByteArray {
|
|
139
|
+
try {
|
|
140
|
+
android.util.Base64.decode(payload, android.util.Base64.DEFAULT)?.let { return it }
|
|
141
|
+
} catch (error: RuntimeException) {
|
|
142
|
+
if (error is IllegalArgumentException) throw error
|
|
143
|
+
}
|
|
144
|
+
return decodeBase64Fallback(payload)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private fun decodeBase64Fallback(payload: String): ByteArray {
|
|
148
|
+
val out = ByteArrayOutputStream((payload.length * 3) / 4)
|
|
149
|
+
var buffer = 0
|
|
150
|
+
var bitsCollected = 0
|
|
151
|
+
for (i in payload.indices) {
|
|
152
|
+
val c = payload[i]
|
|
153
|
+
if (c == '=') break
|
|
154
|
+
val value = when (c) {
|
|
155
|
+
in 'A'..'Z' -> c.code - 'A'.code
|
|
156
|
+
in 'a'..'z' -> c.code - 'a'.code + 26
|
|
157
|
+
in '0'..'9' -> c.code - '0'.code + 52
|
|
158
|
+
'+', '-' -> 62
|
|
159
|
+
'/', '_' -> 63
|
|
160
|
+
' ', '\t', '\r', '\n' -> continue
|
|
161
|
+
else -> throw IllegalArgumentException("Invalid Base64 character")
|
|
162
|
+
}
|
|
163
|
+
buffer = (buffer shl 6) or value
|
|
164
|
+
bitsCollected += 6
|
|
165
|
+
if (bitsCollected >= 8) {
|
|
166
|
+
bitsCollected -= 8
|
|
167
|
+
out.write((buffer ushr bitsCollected) and 0xff)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return out.toByteArray()
|
|
171
|
+
}
|
|
172
|
+
|
|
132
173
|
fun isSafetyFailure(error: Throwable?): Boolean {
|
|
133
174
|
if (error == null) return false
|
|
134
175
|
val seen = Collections.newSetFromMap(IdentityHashMap<Throwable, Boolean>())
|
|
@@ -802,7 +843,7 @@ internal class OneKeyImageSafetyVersionedKey(
|
|
|
802
843
|
override fun hashCode(): Int = 31 * delegate.hashCode() + CACHE_VERSION.hashCode()
|
|
803
844
|
|
|
804
845
|
companion object {
|
|
805
|
-
internal const val CACHE_VERSION = "onekey-image-safety-
|
|
846
|
+
internal const val CACHE_VERSION = "onekey-image-safety-v4"
|
|
806
847
|
private val VERSION_BYTES = CACHE_VERSION.toByteArray(Key.CHARSET)
|
|
807
848
|
}
|
|
808
849
|
}
|