@h1r-ai/croche-react-native 0.5.5 → 0.7.0
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/LICENSE +1 -1
- package/README.md +90 -451
- package/THIRD-PARTY-NOTICES +906 -14
- package/android/build.gradle +6 -1
- package/android/src/androidTest/java/dev/croche/reactnative/WikiFileBridgeTest.kt +126 -0
- package/android/src/main/AndroidManifest.xml +0 -5
- package/android/src/main/java/dev/croche/reactnative/CoreDispatch.kt +106 -0
- package/android/src/main/java/dev/croche/reactnative/CoreWire.kt +217 -0
- package/android/src/main/java/dev/croche/reactnative/CrocheRuntimeModule.kt +48 -824
- package/android/src/main/java/dev/croche/reactnative/NativePal.kt +68 -0
- package/android/src/main/java/dev/croche/reactnative/RNHostPal.kt +9 -0
- package/android/src/test/java/dev/croche/reactnative/AgentMemoryTest.kt +64 -41
- package/android/src/test/java/dev/croche/reactnative/BackgroundDelegationTest.kt +64 -0
- package/android/src/test/java/dev/croche/reactnative/BlobFilesTest.kt +107 -0
- package/android/src/test/java/dev/croche/reactnative/ImageAdapterWireTest.kt +70 -0
- package/android/src/test/java/dev/croche/reactnative/OpenForwardingFixture.kt +44 -0
- package/android/src/test/java/dev/croche/reactnative/ProjectAttestationTest.kt +19 -32
- package/android/src/test/java/dev/croche/reactnative/ProjectHandlerStagesTest.kt +32 -0
- package/android/src/test/java/dev/croche/reactnative/ProjectRecoveryTest.kt +12 -0
- package/android/src/test/java/dev/croche/reactnative/ProjectTimeZoneTest.kt +11 -15
- package/android/src/test/java/dev/croche/reactnative/StorageVersionWireTest.kt +37 -0
- package/android/src/test/java/dev/croche/reactnative/VerificationDispatchTest.kt +24 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidEventSink.kt +27 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/AndroidPlatform.kt +10 -8
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundRegistration.kt +16 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BackgroundWakeWorker.kt +55 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapDecodedImageCodec.kt +162 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageCodec.kt +1 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapImageInput.kt +15 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapOrientation.kt +40 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/BitmapPalCodec.kt +66 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidAssetPal.kt +30 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreFactory.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidCoreHost.kt +116 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidMainDispatcher.kt +17 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt +17 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidS2Pal.kt +14 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStateStore.kt +216 -0
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidStoragePal.kt +108 -0
- package/android/vendor/CrocheRuntimeSDK/consumer-rules.pro +8 -2
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Adapters.kt +75 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/BindingFacts.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ContextConfig.kt +31 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Core.kt +25 -1
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Croche.kt +121 -103
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheAgent.kt +89 -103
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheRuntime.kt +20 -360
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/CrocheTools.kt +136 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Decision.kt +170 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/DecodedImageCodec.kt +23 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Effects.kt +26 -21
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/EncodedImageCodec.kt +51 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/GeneratedAppStream.kt +715 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Host.kt +9 -322
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ImagePal.kt +183 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Images.kt +16 -28
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeEvent.kt +59 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeHandleOwner.kt +118 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Platform.kt +10 -74
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectTypes.kt +294 -37
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/PublicResults.kt +127 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ReaderValues.kt +73 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/RunTool.kt +73 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SendEncoding.kt +23 -21
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StateStore.kt +90 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/SubscriptionTypes.kt +88 -24
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ffi/croche_ffi.kt +5723 -3088
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppEventSink.kt +34 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/AppStoragePal.kt +96 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundOsRegistration.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/BackgroundSink.kt +37 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreDefinitionCalls.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreFactoryCalls.kt +30 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreOptionsEncoding.kt +41 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/CoreStreamCalls.kt +35 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ForeignCallbacks.kt +127 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ImageAdapterHost.kt +28 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmCoreHost.kt +70 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStateStore.kt +125 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/JvmStoragePal.kt +64 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/LifecycleBinding.kt +18 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativeCoreInitialization.kt +21 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/NativePlatform.kt +60 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PalCallbacks.kt +27 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PlainToolJson.kt +56 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/PublicCalls.kt +43 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ScopeJson.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StateStoreContract.kt +103 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/StorageVersionContract.kt +40 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/ToolCompletionEncoding.kt +29 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/AtomicKeyFiles.kt +100 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/BlobFiles.kt +157 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/DirectHttpPal.kt +223 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/InstancePayloadPurge.kt +79 -0
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/s7/pal/VersionedKeyFiles.kt +71 -0
- package/android/vendor/jniLibs/arm64-v8a/libcroche_ffi.so +0 -0
- package/android/vendor/jniLibs/armeabi-v7a/libcroche_ffi.so +0 -0
- package/android/vendor/jniLibs/x86_64/libcroche_ffi.so +0 -0
- package/croche-react-native.podspec +1 -0
- package/dist/package.json +1 -0
- package/dist/react-native/src/NativeCrocheRuntime.d.ts +16 -0
- package/dist/react-native/src/NativeCrocheRuntime.js +2 -0
- package/dist/react-native/src/core-native.d.ts +6 -0
- package/dist/react-native/src/core-native.js +316 -0
- package/dist/react-native/src/core-wire.d.ts +55 -0
- package/dist/react-native/src/core-wire.js +1 -0
- package/dist/react-native/src/native-module.d.ts +8 -0
- package/dist/react-native/src/native-module.js +53 -0
- package/dist/rn-shared-build.json +16 -0
- package/dist/runtime/src/adapters.d.ts +8 -0
- package/dist/runtime/src/adapters.js +29 -0
- package/dist/runtime/src/agent.d.ts +91 -0
- package/dist/runtime/src/agent.js +137 -0
- package/dist/runtime/src/app-api.d.ts +572 -0
- package/dist/runtime/src/app-api.js +1 -0
- package/dist/runtime/src/cancel.d.ts +9 -0
- package/dist/runtime/src/cancel.js +27 -0
- package/dist/runtime/src/core/foreground.d.ts +3 -0
- package/dist/runtime/src/core/foreground.js +40 -0
- package/dist/runtime/src/core/port.d.ts +247 -0
- package/dist/runtime/src/core/port.js +1 -0
- package/dist/runtime/src/core/wasm-owner.d.ts +32 -0
- package/dist/runtime/src/core/wasm-owner.js +237 -0
- package/dist/runtime/src/core/wasm.d.ts +1 -0
- package/dist/runtime/src/core/wasm.js +2 -0
- package/dist/runtime/src/croche.d.ts +97 -0
- package/dist/runtime/src/croche.js +172 -0
- package/dist/runtime/src/errors.d.ts +33 -0
- package/dist/runtime/src/errors.js +63 -0
- package/dist/runtime/src/events.d.ts +5 -0
- package/dist/runtime/src/events.js +31 -0
- package/dist/runtime/src/generated/app-stream.d.ts +328 -0
- package/dist/runtime/src/generated/app-stream.js +127 -0
- package/dist/runtime/src/image-input.d.ts +5 -0
- package/dist/runtime/src/image-input.js +60 -0
- package/dist/runtime/src/index.d.ts +6 -0
- package/dist/runtime/src/index.js +5 -0
- package/dist/runtime/src/index.native.d.ts +1 -0
- package/dist/runtime/src/index.native.js +2 -0
- package/dist/runtime/src/json.d.ts +13 -0
- package/dist/runtime/src/json.js +120 -0
- package/dist/runtime/src/native-adapter.d.ts +3 -0
- package/dist/runtime/src/native-adapter.js +30 -0
- package/dist/runtime/src/pal/host.d.ts +5 -0
- package/dist/runtime/src/pal/host.js +60 -0
- package/dist/runtime/src/pal/platform.d.ts +21 -0
- package/dist/runtime/src/pal/platform.js +7 -0
- package/dist/runtime/src/pal/state-store-lock.d.ts +8 -0
- package/dist/runtime/src/pal/state-store-lock.js +2 -0
- package/dist/runtime/src/pal/state-store.d.ts +66 -0
- package/dist/runtime/src/pal/state-store.js +1 -0
- package/dist/runtime/src/pal/storage-version-contract.d.ts +19 -0
- package/dist/runtime/src/pal/storage-version-contract.js +68 -0
- package/dist/runtime/src/pal/transactional-storage.d.ts +3 -0
- package/dist/runtime/src/pal/transactional-storage.js +124 -0
- package/dist/runtime/src/rn-entry.d.ts +1 -0
- package/dist/runtime/src/rn-entry.js +4 -0
- package/dist/runtime/src/run-tool.d.ts +33 -0
- package/dist/runtime/src/run-tool.js +7 -0
- package/dist/runtime/src/tools.d.ts +35 -0
- package/dist/runtime/src/tools.js +94 -0
- package/ios/CoreAgentTransport.swift +14 -0
- package/ios/CoreDeliveryTransport.swift +58 -0
- package/ios/CoreNativePal.swift +98 -0
- package/ios/CorePlatformTransport.swift +44 -0
- package/ios/CoreWireDispatch.swift +122 -0
- package/ios/CoreWireSupport.swift +286 -0
- package/ios/CrocheRuntimeBridge.swift +34 -994
- package/ios/ProjectPal.swift +49 -0
- package/ios/RNCrocheRuntime.mm +18 -201
- package/ios/vendor/CrocheRuntimeSDK/AppBuildFacts.swift +112 -0
- package/ios/vendor/CrocheRuntimeSDK/BlobFiles.swift +124 -0
- package/ios/vendor/CrocheRuntimeSDK/ContextConfig.swift +60 -0
- package/ios/vendor/CrocheRuntimeSDK/DirectHTTP.swift +258 -0
- package/ios/vendor/CrocheRuntimeSDK/Effects.swift +17 -17
- package/ios/vendor/CrocheRuntimeSDK/EncodedImages.swift +85 -0
- package/ios/vendor/CrocheRuntimeSDK/FFIStreamPort.swift +26 -157
- package/ios/vendor/CrocheRuntimeSDK/GeneratedAppStream.swift +672 -0
- package/ios/vendor/CrocheRuntimeSDK/Host.swift +34 -371
- package/ios/vendor/CrocheRuntimeSDK/ImagePal.swift +410 -0
- package/ios/vendor/CrocheRuntimeSDK/Images.swift +10 -9
- package/ios/vendor/CrocheRuntimeSDK/InstancePayloadPurge.swift +88 -0
- package/ios/vendor/CrocheRuntimeSDK/JSON.swift +30 -14
- package/ios/vendor/CrocheRuntimeSDK/Offload.swift +24 -0
- package/ios/vendor/CrocheRuntimeSDK/OpenOptions.swift +1 -155
- package/ios/vendor/CrocheRuntimeSDK/Platform.swift +33 -59
- package/ios/vendor/CrocheRuntimeSDK/PlatformBackground.swift +289 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformPal.swift +302 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformStateStore.swift +253 -0
- package/ios/vendor/CrocheRuntimeSDK/PlatformStorage.swift +281 -0
- package/ios/vendor/CrocheRuntimeSDK/Project.swift +4 -269
- package/ios/vendor/CrocheRuntimeSDK/Runtime.swift +24 -461
- package/ios/vendor/CrocheRuntimeSDK/RuntimeHTTPAdapter.swift +42 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Adapters.swift +110 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Agent.swift +248 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Allowance.swift +20 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1BindingFacts.swift +33 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Core.swift +170 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Croche.swift +313 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Decision.swift +146 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Events.swift +37 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1HandleOwner.swift +58 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Options.swift +346 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1RunTool.swift +89 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Storage.swift +165 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1TestHooks.swift +100 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Tools.swift +278 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/S1Verification.swift +27 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/StateStoreContract.swift +189 -0
- package/ios/vendor/CrocheRuntimeSDK/S1/StorageVersionContract.swift +68 -0
- package/ios/vendor/CrocheRuntimeSDK/Storage.swift +23 -269
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionTypes.swift +163 -116
- package/ios/vendor/CrocheRuntimeSDK/Truncate.swift +12 -0
- package/ios/vendor/CrocheRuntimeSDK/croche_ffi.swift +4712 -2469
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/Headers/croche_ffiFFI.h +899 -608
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64/libcroche_ffi.a +0 -0
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/Headers/croche_ffiFFI.h +899 -608
- package/ios/vendor/croche_ffiFFI.xcframework/ios-arm64_x86_64-simulator/libcroche_ffi.a +0 -0
- package/package.json +18 -8
- package/scripts/README.md +11 -0
- package/scripts/build-shared.mjs +46 -0
- package/scripts/build.mjs +11 -0
- package/scripts/check-swift-vendor-modules.sh +15 -0
- package/scripts/vendor-native.sh +8 -6
- package/src/NativeCrocheRuntime.ts +12 -159
- package/android/src/main/java/dev/croche/reactnative/AgentMemory.kt +0 -25
- package/android/src/main/java/dev/croche/reactnative/ProjectAttestation.kt +0 -17
- package/android/src/main/java/dev/croche/reactnative/ProjectTimeZone.kt +0 -14
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/CrocheProjectRecordProvider.kt +0 -32
- package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/WorkManagerScheduler.kt +0 -146
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/AppStreamBranch.kt +0 -110
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Control.kt +0 -178
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/NativeCoreStreamPort.kt +0 -173
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectBinding.kt +0 -79
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPalRouting.kt +0 -25
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectPlatform.kt +0 -46
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/ProjectRecords.kt +0 -87
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/Storage.kt +0 -181
- package/android/vendor/CrocheRuntimeSDK/core/dev/croche/StreamHub.kt +0 -227
- package/ios/vendor/CrocheRuntimeSDK/Background.swift +0 -59
- package/ios/vendor/CrocheRuntimeSDK/Control.swift +0 -192
- package/ios/vendor/CrocheRuntimeSDK/StreamHub.swift +0 -453
- package/ios/vendor/CrocheRuntimeSDK/SubscriptionRuntime.swift +0 -38
- package/src/index.ts +0 -664
- package/src/project-types.ts +0 -35
- package/src/project.ts +0 -434
- package/src/protocol.ts +0 -28
- package/src/types.ts +0 -9
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
package dev.croche.reactnative
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonParser
|
|
4
|
+
import dev.croche.ffi.*
|
|
5
|
+
import org.junit.Assert.*
|
|
6
|
+
import org.junit.Test
|
|
7
|
+
|
|
8
|
+
class StorageVersionWireTest {
|
|
9
|
+
@Test fun requiredNullFieldsSurviveRealWireSerialization() {
|
|
10
|
+
val value = JsonParser.parseString(json(mapOf("value" to mapOf("value" to null, "version" to null)))).asJsonObject.getAsJsonObject("value")
|
|
11
|
+
assertTrue(value.has("value")); assertTrue(value.get("value").isJsonNull)
|
|
12
|
+
assertTrue(value.has("version")); assertTrue(value.get("version").isJsonNull)
|
|
13
|
+
}
|
|
14
|
+
@Test fun callbackDecodingPairsWithTheCapturedRequestAndRejectsMissingFields() {
|
|
15
|
+
var reply = "{\"type\":\"written\",\"version\":null}"
|
|
16
|
+
lateinit var callbacks: CoreCallbacks
|
|
17
|
+
callbacks = CoreCallbacks { packet ->
|
|
18
|
+
val call = JsonParser.parseString(packet).asJsonObject.get("call").asString
|
|
19
|
+
callbacks.reply(call, "{\"value\":$reply}")
|
|
20
|
+
}
|
|
21
|
+
val storage = CallbackStorage("storage", "host", callbacks)
|
|
22
|
+
fun refused(body: () -> Unit) {
|
|
23
|
+
try { body(); fail("P1_WIRE_MISMATCH: invalid reply accepted") }
|
|
24
|
+
catch (_: CoreStorageFailure.Failure) { }
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
assertEquals(CoreWriteOutcome.Written(null), storage.writeIf("key", null, null))
|
|
28
|
+
refused { storage.writeIf("key", "write", null) }
|
|
29
|
+
reply = "{\"type\":\"written\",\"version\":\"token\"}"
|
|
30
|
+
assertEquals(CoreWriteOutcome.Written("token"), storage.writeIf("key", "write", null))
|
|
31
|
+
refused { storage.writeIf("key", null, "token") }
|
|
32
|
+
for (malformed in listOf("{}", "null", "{\"value\":\"raw\"}", "{\"value\":null,\"version\":\"token\"}")) {
|
|
33
|
+
reply = malformed; refused { storage.readVersioned("key") }
|
|
34
|
+
}
|
|
35
|
+
} finally { callbacks.invalidate() }
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
package dev.croche.reactnative
|
|
2
|
+
|
|
3
|
+
import dev.croche.ffi.*
|
|
4
|
+
import org.junit.Assert.*
|
|
5
|
+
import org.junit.Test
|
|
6
|
+
|
|
7
|
+
class VerificationDispatchTest {
|
|
8
|
+
/** RN croche.verification() is one read on the root Core handle; its JSON and errors pass through. */
|
|
9
|
+
@Test fun verificationForwardsToTheRootAndErrorsPassThrough() {
|
|
10
|
+
var closed = false
|
|
11
|
+
val root = object : CoreCroche(NoHandle) {
|
|
12
|
+
override fun verification(): String {
|
|
13
|
+
if (closed) throw CoreApiException.Failure("closed", "Croche is closed", "closed", null, null)
|
|
14
|
+
return """{"status":"unverified","lastAttestation":"attestation_timeout"}"""
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
val bridge = CoreDispatch({ error("No new host") }, { error("No platform storage") }) { error("No event") }
|
|
18
|
+
val id = bridge.handles.hold(root)
|
|
19
|
+
assertEquals("""{"status":"unverified","lastAttestation":"attestation_timeout"}""", bridge.call(id, "verification", com.google.gson.JsonArray()))
|
|
20
|
+
closed = true
|
|
21
|
+
try { bridge.call(id, "verification", com.google.gson.JsonArray()); fail("closed root must refuse") }
|
|
22
|
+
catch (error: CoreApiException.Failure) { assertEquals("closed", error.code) }
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
package dev.croche.android
|
|
2
|
+
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import dev.croche.ffi.CoreDelivery
|
|
6
|
+
import dev.croche.ffi.CoreEventSink
|
|
7
|
+
|
|
8
|
+
/** Platform dispatch only. Core owns serialization, buffering, entry fences and completion.
|
|
9
|
+
* No SDK queue; the platform's UI dispatcher runs the one delivery supplied by Core. */
|
|
10
|
+
internal class AndroidEventSink(private val onEvent: (String) -> Unit) : CoreEventSink {
|
|
11
|
+
private val ui = Handler(Looper.getMainLooper())
|
|
12
|
+
override fun deliver(deliveryId: String, eventJson: String, delivery: CoreDelivery) {
|
|
13
|
+
val invoke = Runnable {
|
|
14
|
+
if (!delivery.tryEnter()) return@Runnable
|
|
15
|
+
val outcome = try {
|
|
16
|
+
onEvent(eventJson)
|
|
17
|
+
"{\"outcome\":\"returned\"}"
|
|
18
|
+
} catch (_: Throwable) {
|
|
19
|
+
// This is the invocation fact, not an SDK error code/category decision.
|
|
20
|
+
"{\"outcome\":\"threw\"}"
|
|
21
|
+
}
|
|
22
|
+
delivery.complete(outcome)
|
|
23
|
+
}
|
|
24
|
+
if (Looper.myLooper() == ui.looper) invoke.run()
|
|
25
|
+
else if (!ui.post(invoke)) delivery.complete("{\"outcome\":\"unavailable\"}")
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -9,7 +9,7 @@ import com.google.android.play.core.integrity.IntegrityServiceException
|
|
|
9
9
|
import com.google.android.play.core.integrity.IntegrityTokenRequest
|
|
10
10
|
import com.google.gson.JsonObject
|
|
11
11
|
import com.google.gson.JsonParser
|
|
12
|
-
import dev.croche.
|
|
12
|
+
import dev.croche.CrocheError
|
|
13
13
|
import dev.croche.RuntimeAttestation
|
|
14
14
|
import dev.croche.RuntimeAttestor
|
|
15
15
|
import dev.croche.RuntimeCancellation
|
|
@@ -24,7 +24,7 @@ import javax.crypto.KeyGenerator
|
|
|
24
24
|
import javax.crypto.SecretKey
|
|
25
25
|
import javax.crypto.spec.GCMParameterSpec
|
|
26
26
|
|
|
27
|
-
// Android implementations of the Platform Abstraction Layer. See docs/
|
|
27
|
+
// Android implementations of the Platform Abstraction Layer. See docs/hld/surface-and-hosts.md.
|
|
28
28
|
// Not compiled on the Linux host (no Android SDK): build and run on the Mac before relying on it.
|
|
29
29
|
|
|
30
30
|
/**
|
|
@@ -34,7 +34,7 @@ import javax.crypto.spec.GCMParameterSpec
|
|
|
34
34
|
* instance (the local conversation stays). Unlike the iOS Keychain, it does not survive
|
|
35
35
|
* uninstall. That asymmetry is by platform design; see the design document.
|
|
36
36
|
*/
|
|
37
|
-
class KeystoreSecretStore(context: Context, name: String) : RuntimeSecretStore {
|
|
37
|
+
internal class KeystoreSecretStore(context: Context, name: String) : RuntimeSecretStore {
|
|
38
38
|
private val file = File(context.noBackupFilesDir, "croche/${name.replace(Regex("[^A-Za-z0-9._-]"), "_")}.secrets")
|
|
39
39
|
|
|
40
40
|
private fun key(): SecretKey {
|
|
@@ -64,8 +64,8 @@ class KeystoreSecretStore(context: Context, name: String) : RuntimeSecretStore {
|
|
|
64
64
|
file.delete(); return JsonObject()
|
|
65
65
|
}
|
|
66
66
|
val saved = try { JsonParser.parseString(String(plain, Charsets.UTF_8)) }
|
|
67
|
-
catch (_: Exception) { throw
|
|
68
|
-
if (!saved.isJsonObject) throw
|
|
67
|
+
catch (_: Exception) { throw CrocheError("corrupt_store", "Runtime credentials are unreadable") }
|
|
68
|
+
if (!saved.isJsonObject) throw CrocheError("corrupt_store", "Runtime credentials must be an object")
|
|
69
69
|
return saved.asJsonObject
|
|
70
70
|
}
|
|
71
71
|
|
|
@@ -79,7 +79,7 @@ class KeystoreSecretStore(context: Context, name: String) : RuntimeSecretStore {
|
|
|
79
79
|
val temporary = File(file.parentFile, file.name + ".tmp")
|
|
80
80
|
try {
|
|
81
81
|
FileOutputStream(temporary).use { output -> output.write(sealed); output.fd.sync() }
|
|
82
|
-
if (!temporary.renameTo(file)) throw
|
|
82
|
+
if (!temporary.renameTo(file)) throw CrocheError("storage_error", "Could not replace Runtime credentials")
|
|
83
83
|
} finally { temporary.delete() }
|
|
84
84
|
}
|
|
85
85
|
|
|
@@ -102,8 +102,10 @@ class KeystoreSecretStore(context: Context, name: String) : RuntimeSecretStore {
|
|
|
102
102
|
*/
|
|
103
103
|
class PlayIntegrityAttestor(context: Context, private val cloudProjectNumber: Long) : RuntimeAttestor {
|
|
104
104
|
private val manager = IntegrityManagerFactory.create(context.applicationContext)
|
|
105
|
+
/** Platform identifier sent to Core for this attestor. */
|
|
105
106
|
override val platform = "android-play-integrity"
|
|
106
107
|
|
|
108
|
+
/** Request a Play Integrity proof using the supplied client-data hash as the nonce. */
|
|
107
109
|
override fun attest(clientData: String, clientDataHash: String, cancellation: RuntimeCancellation): RuntimeAttestation {
|
|
108
110
|
cancellation.check()
|
|
109
111
|
// The nonce must be URL-safe, no-wrap Base64 of 16-500 characters; the 43-character
|
|
@@ -117,7 +119,7 @@ class PlayIntegrityAttestor(context: Context, private val cloudProjectNumber: Lo
|
|
|
117
119
|
// -1 API_NOT_AVAILABLE) is what a release setup is debugged with, and it carries no
|
|
118
120
|
// secret. The Core shows the host's code, not its message, so the number goes there.
|
|
119
121
|
val play = generateSequence<Throwable>(e) { it.cause }.filterIsInstance<IntegrityServiceException>().firstOrNull()
|
|
120
|
-
throw
|
|
122
|
+
throw CrocheError(play?.let { "play_integrity_${it.errorCode}" } ?: "attestation_failed", "Play Integrity could not attest this app")
|
|
121
123
|
}
|
|
122
124
|
cancellation.check()
|
|
123
125
|
return RuntimeAttestation(response.token())
|
|
@@ -125,7 +127,7 @@ class PlayIntegrityAttestor(context: Context, private val cloudProjectNumber: Lo
|
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
/** The wiki documents shipped in the app's assets under `folder` (`src/main/assets/wiki/`),
|
|
128
|
-
* paths relative to it: `
|
|
130
|
+
* paths relative to it: `wikiFromAssets(context.assets)`. */
|
|
129
131
|
fun wikiFromAssets(assets: android.content.res.AssetManager, folder: String = "wiki"): List<dev.croche.WikiDocument> =
|
|
130
132
|
dev.croche.WikiDocument.fromTree(
|
|
131
133
|
list = { path -> assets.list(if (path.isEmpty()) folder else "$folder/$path") },
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
package dev.croche.android
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import dev.croche.ffi.CoreStoragePal
|
|
5
|
+
import dev.croche.ffi.CoreEventSink
|
|
6
|
+
import dev.croche.ffi.HostPal
|
|
7
|
+
import dev.croche.s7.BackgroundCalls
|
|
8
|
+
|
|
9
|
+
/** Explicit Context resolves the native PAL; the whole options JSON reaches Core unchanged. */
|
|
10
|
+
internal class BackgroundRegistration(
|
|
11
|
+
private val hostForContext: (Context) -> HostPal,
|
|
12
|
+
private val storageForContext: (Context) -> CoreStoragePal,
|
|
13
|
+
) {
|
|
14
|
+
fun registerBackground(context: Context, optionsJson: String, sink: CoreEventSink? = null) =
|
|
15
|
+
BackgroundCalls.registerBackground(optionsJson, storageForContext(context), hostForContext(context), sink)
|
|
16
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
package dev.croche.android
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import androidx.work.ExistingWorkPolicy
|
|
5
|
+
import androidx.work.OneTimeWorkRequestBuilder
|
|
6
|
+
import androidx.work.WorkManager
|
|
7
|
+
import androidx.work.Worker
|
|
8
|
+
import androidx.work.WorkerParameters
|
|
9
|
+
import dev.croche.ffi.CancelToken
|
|
10
|
+
import dev.croche.ffi.CoreBackgroundWake
|
|
11
|
+
import dev.croche.s7.BackgroundOsRegistration
|
|
12
|
+
import dev.croche.s7.ensureNativeCoreInitialized
|
|
13
|
+
import java.util.concurrent.TimeUnit
|
|
14
|
+
|
|
15
|
+
/** Process callback only. The Core wake object owns a weak service reference. */
|
|
16
|
+
internal object AndroidBackgroundWake {
|
|
17
|
+
@Volatile private var registration: BackgroundOsRegistration? = null
|
|
18
|
+
|
|
19
|
+
@Synchronized fun install(context: Context, identifier: String, wake: CoreBackgroundWake) {
|
|
20
|
+
val app = context.applicationContext
|
|
21
|
+
val entry = registration ?: BackgroundOsRegistration { name, keep, delay ->
|
|
22
|
+
val work = OneTimeWorkRequestBuilder<BackgroundWakeWorker>()
|
|
23
|
+
.setInitialDelay(delay, TimeUnit.MILLISECONDS).build()
|
|
24
|
+
// A returned Operation is not confirmation; wait for WorkManager's persisted result.
|
|
25
|
+
WorkManager.getInstance(app).enqueueUniqueWork(name,
|
|
26
|
+
if (keep) ExistingWorkPolicy.KEEP else ExistingWorkPolicy.REPLACE, work).result.get()
|
|
27
|
+
Unit
|
|
28
|
+
}
|
|
29
|
+
entry.install(identifier, wake)
|
|
30
|
+
registration = entry
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fun schedule(earliest: Long?, now: Long) { registration?.schedule(earliest, now) }
|
|
34
|
+
fun callback(): CoreBackgroundWake? = registration?.wake()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** No SDK retries, tools or turns. WorkManager stop cancels only this Core wake run. */
|
|
38
|
+
class BackgroundWakeWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
|
|
39
|
+
init { ensureNativeCoreInitialized() }
|
|
40
|
+
private val cancel = CancelToken()
|
|
41
|
+
private val stopLock = Any()
|
|
42
|
+
private var finished = false
|
|
43
|
+
/** Run the registered Core wake callback and report the worker outcome to WorkManager. */
|
|
44
|
+
override fun doWork(): Result {
|
|
45
|
+
if (isStopped) cancel.cancel()
|
|
46
|
+
try {
|
|
47
|
+
// WorkManager exposes no absolute execution deadline, so pass null, not a guess.
|
|
48
|
+
AndroidBackgroundWake.callback()?.run(null, cancel)
|
|
49
|
+
return Result.success()
|
|
50
|
+
} catch (_: Exception) { return Result.failure() }
|
|
51
|
+
finally { synchronized(stopLock) { finished = true; cancel.close() } }
|
|
52
|
+
}
|
|
53
|
+
/** Request cancellation of the wake run if the worker has not finished. */
|
|
54
|
+
override fun onStopped() { synchronized(stopLock) { if (!finished) cancel.cancel() } }
|
|
55
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
package dev.croche.android
|
|
2
|
+
|
|
3
|
+
import android.graphics.Bitmap
|
|
4
|
+
import android.graphics.BitmapFactory
|
|
5
|
+
import android.graphics.Matrix
|
|
6
|
+
import dev.croche.CrocheError
|
|
7
|
+
import dev.croche.DecodedImageCodec
|
|
8
|
+
import dev.croche.NativeImagePal
|
|
9
|
+
import dev.croche.ffi.CancelToken
|
|
10
|
+
import dev.croche.DecodedImage
|
|
11
|
+
import dev.croche.ImageDecodeOptions
|
|
12
|
+
import dev.croche.ImageFormat
|
|
13
|
+
import dev.croche.ImageRenderRequest
|
|
14
|
+
import java.io.ByteArrayOutputStream
|
|
15
|
+
import java.util.UUID
|
|
16
|
+
import kotlin.math.roundToInt
|
|
17
|
+
|
|
18
|
+
/** Android effects, not a preparation policy. Core chooses frame, orientation, dimensions,
|
|
19
|
+
* format and quality. Private immutable pixels are leased; release fences without waiting.
|
|
20
|
+
*/
|
|
21
|
+
internal class BitmapDecodedImageCodec(
|
|
22
|
+
private val decodedForTests: () -> Unit = {},
|
|
23
|
+
private val renderedForTests: () -> Unit = {},
|
|
24
|
+
) : DecodedImageCodec {
|
|
25
|
+
private class Entry(val bitmap: Bitmap) {
|
|
26
|
+
var leases = 0
|
|
27
|
+
var retired = false
|
|
28
|
+
}
|
|
29
|
+
private val lock = Any()
|
|
30
|
+
private val entries = mutableMapOf<String, Entry>()
|
|
31
|
+
private var closed = false
|
|
32
|
+
|
|
33
|
+
override fun decode(bytes: ByteArray, options: ImageDecodeOptions, cancel: CancelToken): DecodedImage {
|
|
34
|
+
NativeImagePal.check(cancel)
|
|
35
|
+
if (options.frame != 0u) invalid("Only frame zero can be decoded")
|
|
36
|
+
var owned: Bitmap? = null
|
|
37
|
+
try {
|
|
38
|
+
// BitmapFactory decodes the first frame; no animated drawable is retained.
|
|
39
|
+
val decoded = BitmapFactory.decodeByteArray(bytes, 0, bytes.size, BitmapFactory.Options().apply {
|
|
40
|
+
inPreferredConfig = Bitmap.Config.ARGB_8888
|
|
41
|
+
inScaled = false
|
|
42
|
+
}) ?: invalid("The platform could not decode the image bytes")
|
|
43
|
+
owned = decoded
|
|
44
|
+
decodedForTests()
|
|
45
|
+
NativeImagePal.check(cancel) // OS decode is uninterruptible; do not publish after cancel.
|
|
46
|
+
val bitmap = if (options.applyOrientation) orient(decoded, bitmapOrientation(bytes)) else decoded
|
|
47
|
+
if (bitmap !== decoded) decoded.recycle()
|
|
48
|
+
owned = bitmap
|
|
49
|
+
val alpha = transparentPixel(bitmap, cancel)
|
|
50
|
+
return synchronized(lock) {
|
|
51
|
+
NativeImagePal.check(cancel)
|
|
52
|
+
if (closed) throw CrocheError("closed", "The image codec is closed")
|
|
53
|
+
val handle = UUID.randomUUID().toString()
|
|
54
|
+
entries[handle] = Entry(bitmap)
|
|
55
|
+
owned = null // The private table owns the pixels from this point.
|
|
56
|
+
DecodedImage(handle, bitmap.width.toUInt(), bitmap.height.toUInt(), alpha)
|
|
57
|
+
}
|
|
58
|
+
} finally {
|
|
59
|
+
owned?.recycle()
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
override fun render(request: ImageRenderRequest, cancel: CancelToken): ByteArray {
|
|
64
|
+
NativeImagePal.check(cancel)
|
|
65
|
+
val entry = synchronized(lock) {
|
|
66
|
+
val entry = entries[request.handle] ?: unsupported("The decoded image handle is unavailable")
|
|
67
|
+
entry.leases++
|
|
68
|
+
entry
|
|
69
|
+
}
|
|
70
|
+
var scaled: Bitmap? = null
|
|
71
|
+
try {
|
|
72
|
+
val width = dimension(request.width)
|
|
73
|
+
val height = dimension(request.height)
|
|
74
|
+
val format = when (request.format) {
|
|
75
|
+
ImageFormat.PNG -> Bitmap.CompressFormat.PNG
|
|
76
|
+
ImageFormat.JPEG -> Bitmap.CompressFormat.JPEG
|
|
77
|
+
}
|
|
78
|
+
val quality = when (request.format) {
|
|
79
|
+
ImageFormat.PNG -> {
|
|
80
|
+
if (request.quality != null) unsupported("PNG does not take a quality argument")
|
|
81
|
+
100 // Android ignores this parameter for PNG.
|
|
82
|
+
}
|
|
83
|
+
ImageFormat.JPEG -> {
|
|
84
|
+
val value = request.quality ?: unsupported("JPEG requires a quality argument")
|
|
85
|
+
if (!value.isFinite() || value < 0 || value > 1) unsupported("JPEG quality is outside its range")
|
|
86
|
+
(value * 100).roundToInt()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
NativeImagePal.check(cancel)
|
|
90
|
+
scaled = Bitmap.createScaledBitmap(entry.bitmap, width, height, true)
|
|
91
|
+
NativeImagePal.check(cancel)
|
|
92
|
+
val output = ByteArrayOutputStream()
|
|
93
|
+
if (!scaled.compress(format, quality, output)) unsupported("The platform could not encode the image")
|
|
94
|
+
renderedForTests()
|
|
95
|
+
NativeImagePal.check(cancel)
|
|
96
|
+
return synchronized(lock) {
|
|
97
|
+
if (entry.retired) unsupported("The decoded image handle was released")
|
|
98
|
+
output.toByteArray() // An owned encoded buffer, never JSON/base64.
|
|
99
|
+
}
|
|
100
|
+
} finally {
|
|
101
|
+
if (scaled !== entry.bitmap) scaled?.recycle()
|
|
102
|
+
synchronized(lock) {
|
|
103
|
+
entry.leases--
|
|
104
|
+
if (entry.retired && entry.leases == 0) entry.bitmap.recycle()
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
override fun release(handle: String) {
|
|
110
|
+
synchronized(lock) {
|
|
111
|
+
val entry = entries.remove(handle) ?: return
|
|
112
|
+
entry.retired = true
|
|
113
|
+
if (entry.leases == 0) entry.bitmap.recycle()
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
override fun close() {
|
|
118
|
+
synchronized(lock) {
|
|
119
|
+
closed = true
|
|
120
|
+
entries.values.forEach { entry ->
|
|
121
|
+
entry.retired = true
|
|
122
|
+
if (entry.leases == 0) entry.bitmap.recycle()
|
|
123
|
+
}
|
|
124
|
+
entries.clear()
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private fun transparentPixel(bitmap: Bitmap, cancel: CancelToken): Boolean {
|
|
129
|
+
val row = IntArray(bitmap.width)
|
|
130
|
+
for (y in 0 until bitmap.height) {
|
|
131
|
+
NativeImagePal.check(cancel)
|
|
132
|
+
bitmap.getPixels(row, 0, bitmap.width, 0, y, bitmap.width, 1)
|
|
133
|
+
if (row.any { (it ushr 24) != 255 }) return true
|
|
134
|
+
}
|
|
135
|
+
return false
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private fun orient(bitmap: Bitmap, orientation: Int): Bitmap {
|
|
139
|
+
val matrix = Matrix().apply {
|
|
140
|
+
when (orientation) {
|
|
141
|
+
2 -> postScale(-1f, 1f)
|
|
142
|
+
3 -> postRotate(180f)
|
|
143
|
+
4 -> postScale(1f, -1f)
|
|
144
|
+
5 -> { postRotate(90f); postScale(-1f, 1f) }
|
|
145
|
+
6 -> postRotate(90f)
|
|
146
|
+
7 -> { postRotate(270f); postScale(-1f, 1f) }
|
|
147
|
+
8 -> postRotate(270f)
|
|
148
|
+
else -> return bitmap
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private fun dimension(value: UInt): Int {
|
|
155
|
+
if (value == 0u || value > Int.MAX_VALUE.toUInt()) unsupported("Image dimension cannot be represented by the platform")
|
|
156
|
+
return value.toInt()
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private fun unsupported(detail: String): Nothing = throw CrocheError("image_unsupported", detail)
|
|
160
|
+
|
|
161
|
+
private fun invalid(detail: String): Nothing = throw CrocheError("invalid_input", detail)
|
|
162
|
+
}
|
|
@@ -17,6 +17,7 @@ import kotlin.math.roundToInt
|
|
|
17
17
|
* (`CrocheImages.codec`): an Android app needs no setup.
|
|
18
18
|
*/
|
|
19
19
|
class BitmapImageCodec : ImageCodec {
|
|
20
|
+
/** Decode and orient the first frame, fitting its longest side to the requested size, and encode the resulting image. */
|
|
20
21
|
override fun render(bytes: ByteArray, maxSide: Int): ImageCodec.Rendered? {
|
|
21
22
|
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
|
22
23
|
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
@file:JvmName("BitmapImageInput")
|
|
2
|
+
package dev.croche
|
|
3
|
+
|
|
4
|
+
import android.graphics.Bitmap
|
|
5
|
+
import java.io.ByteArrayOutputStream
|
|
6
|
+
|
|
7
|
+
/** An Android Bitmap as a send input: snapshotted now as lossless PNG bytes (the Bitmap is already
|
|
8
|
+
* upright and may be recycled after this returns). Core and the native PAL size and encode it. */
|
|
9
|
+
fun ImageInput(bitmap: Bitmap, alt: String? = null): ImageInput {
|
|
10
|
+
if (bitmap.isRecycled) throw CrocheError("image_unsupported", "The image could not be prepared.")
|
|
11
|
+
val bytes = ByteArrayOutputStream().also {
|
|
12
|
+
if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, it)) throw CrocheError("image_unsupported", "The image could not be prepared.")
|
|
13
|
+
}.toByteArray()
|
|
14
|
+
return ImageInput(bytes, alt)
|
|
15
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
package dev.croche.android
|
|
2
|
+
|
|
3
|
+
import android.media.ExifInterface
|
|
4
|
+
import java.io.ByteArrayInputStream
|
|
5
|
+
|
|
6
|
+
/** Shared platform EXIF read: WebP needs the existing RIFF reader on older Android releases. */
|
|
7
|
+
internal fun bitmapOrientation(bytes: ByteArray): Int = webpOrientation(bytes) ?: try {
|
|
8
|
+
ExifInterface(ByteArrayInputStream(bytes)).getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
|
|
9
|
+
} catch (_: Exception) { ExifInterface.ORIENTATION_NORMAL }
|
|
10
|
+
|
|
11
|
+
/** A WebP's EXIF orientation (RIFF `EXIF` chunk, TIFF IFD0 tag 0x0112), or null when it is not a
|
|
12
|
+
* WebP or has none. */
|
|
13
|
+
private fun webpOrientation(bytes: ByteArray): Int? {
|
|
14
|
+
fun u8(i: Int) = bytes[i].toInt() and 0xFF
|
|
15
|
+
fun fourcc(i: Int) = String(bytes, i, 4, Charsets.US_ASCII)
|
|
16
|
+
if (bytes.size < 12 || fourcc(0) != "RIFF" || fourcc(8) != "WEBP") return null
|
|
17
|
+
var at = 12
|
|
18
|
+
while (at + 8 <= bytes.size) {
|
|
19
|
+
val size = u8(at + 4) or (u8(at + 5) shl 8) or (u8(at + 6) shl 16) or (u8(at + 7) shl 24)
|
|
20
|
+
if (size < 0 || at + 8 + size > bytes.size) return null
|
|
21
|
+
if (fourcc(at) == "EXIF") {
|
|
22
|
+
var tiff = at + 8
|
|
23
|
+
if (size >= 6 && fourcc(tiff) == "Exif" && u8(tiff + 4) == 0 && u8(tiff + 5) == 0) tiff += 6
|
|
24
|
+
val end = at + 8 + size
|
|
25
|
+
if (tiff + 8 > end) return null
|
|
26
|
+
val little = u8(tiff) == 0x49
|
|
27
|
+
fun u16(i: Int) = if (i + 2 > end) -1 else if (little) u8(i) or (u8(i + 1) shl 8) else (u8(i) shl 8) or u8(i + 1)
|
|
28
|
+
fun u32(i: Int) = if (i + 4 > end) -1L else if (little) (u16(i).toLong() or (u16(i + 2).toLong() shl 16)) else ((u16(i).toLong() shl 16) or u16(i + 2).toLong())
|
|
29
|
+
val ifd = tiff + u32(tiff + 4).toInt()
|
|
30
|
+
val count = u16(ifd)
|
|
31
|
+
for (entry in 0 until maxOf(0, count)) {
|
|
32
|
+
val e = ifd + 2 + entry * 12
|
|
33
|
+
if (u16(e) == 0x0112) return u16(e + 8).takeIf { it in 1..8 } ?: 1
|
|
34
|
+
}
|
|
35
|
+
return 1
|
|
36
|
+
}
|
|
37
|
+
at += 8 + size + (size and 1)
|
|
38
|
+
}
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
package dev.croche.android
|
|
2
|
+
|
|
3
|
+
import android.graphics.Bitmap
|
|
4
|
+
import android.graphics.BitmapFactory
|
|
5
|
+
import android.graphics.Matrix
|
|
6
|
+
import android.media.ExifInterface
|
|
7
|
+
import dev.croche.PalImageCodec
|
|
8
|
+
import java.io.ByteArrayOutputStream
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The native image PAL's Android decoder (found by name by `NativeImagePal`; no app setup). The
|
|
12
|
+
* header is read with inJustDecodeBounds, pixels are decoded at the largest power-of-two sample
|
|
13
|
+
* that stays at or above the target (inSampleSize), turned upright by EXIF and scaled to the exact
|
|
14
|
+
* size, then encoded as PNG and, without an alpha channel, JPEG at quality 70 (as TypeScript). GIF decodes
|
|
15
|
+
* its first frame.
|
|
16
|
+
*/
|
|
17
|
+
internal class BitmapPalCodec : PalImageCodec {
|
|
18
|
+
override fun open(bytes: ByteArray, maxPixels: Long): PalImageCodec.Opened? {
|
|
19
|
+
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
|
20
|
+
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds)
|
|
21
|
+
val storedWidth = bounds.outWidth; val storedHeight = bounds.outHeight
|
|
22
|
+
if (storedWidth <= 0 || storedHeight <= 0 || storedWidth.toLong() * storedHeight > maxPixels) return null
|
|
23
|
+
// android.media.ExifInterface misses a WebP EXIF chunk on some API levels (API 30), so WebP is
|
|
24
|
+
// read here first; JPEG, PNG and the rest go through ExifInterface.
|
|
25
|
+
val orientation = bitmapOrientation(bytes)
|
|
26
|
+
val swaps = orientation in 5..8
|
|
27
|
+
return object : PalImageCodec.Opened {
|
|
28
|
+
override val width = if (swaps) storedHeight else storedWidth
|
|
29
|
+
override val height = if (swaps) storedWidth else storedHeight
|
|
30
|
+
override val reoriented = orientation in 2..8
|
|
31
|
+
override fun render(width: Int, height: Int): PalImageCodec.Rendered {
|
|
32
|
+
val targetWidth = if (swaps) height else width; val targetHeight = if (swaps) width else height
|
|
33
|
+
var sample = 1
|
|
34
|
+
while (storedWidth / (sample * 2) >= targetWidth && storedHeight / (sample * 2) >= targetHeight) sample *= 2
|
|
35
|
+
val decoded = BitmapFactory.decodeByteArray(bytes, 0, bytes.size, BitmapFactory.Options().apply {
|
|
36
|
+
inSampleSize = sample; inPreferredConfig = Bitmap.Config.ARGB_8888
|
|
37
|
+
}) ?: throw IllegalStateException("decode")
|
|
38
|
+
val matrix = Matrix().apply {
|
|
39
|
+
postScale(targetWidth.toFloat() / decoded.width, targetHeight.toFloat() / decoded.height)
|
|
40
|
+
when (orientation) {
|
|
41
|
+
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> postScale(-1f, 1f)
|
|
42
|
+
ExifInterface.ORIENTATION_ROTATE_180 -> postRotate(180f)
|
|
43
|
+
ExifInterface.ORIENTATION_FLIP_VERTICAL -> postScale(1f, -1f)
|
|
44
|
+
ExifInterface.ORIENTATION_TRANSPOSE -> { postRotate(90f); postScale(-1f, 1f) }
|
|
45
|
+
ExifInterface.ORIENTATION_ROTATE_90 -> postRotate(90f)
|
|
46
|
+
ExifInterface.ORIENTATION_TRANSVERSE -> { postRotate(270f); postScale(-1f, 1f) }
|
|
47
|
+
ExifInterface.ORIENTATION_ROTATE_270 -> postRotate(270f)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
var bitmap = Bitmap.createBitmap(decoded, 0, 0, decoded.width, decoded.height, matrix, true)
|
|
51
|
+
if (bitmap !== decoded) decoded.recycle()
|
|
52
|
+
if (bitmap.width != width || bitmap.height != height) {
|
|
53
|
+
// Float rounding in the matrix can land one pixel off; Core's plan size is exact.
|
|
54
|
+
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true)
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
fun encode(format: Bitmap.CompressFormat, quality: Int) =
|
|
58
|
+
ByteArrayOutputStream().also { if (!bitmap.compress(format, quality, it)) throw IllegalStateException("encode") }.toByteArray()
|
|
59
|
+
val png = encode(Bitmap.CompressFormat.PNG, 100)
|
|
60
|
+
return PalImageCodec.Rendered(png, if (bitmap.hasAlpha()) null else encode(Bitmap.CompressFormat.JPEG, 70))
|
|
61
|
+
} finally { bitmap.recycle() }
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package dev.croche.android.pal
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.content.pm.ApplicationInfo
|
|
5
|
+
import dev.croche.CrocheError
|
|
6
|
+
import java.io.File
|
|
7
|
+
import java.io.FileNotFoundException
|
|
8
|
+
import java.io.IOException
|
|
9
|
+
import java.net.URI
|
|
10
|
+
|
|
11
|
+
internal class AndroidAssetPal(context: Context) {
|
|
12
|
+
private val assets = context.applicationContext.assets
|
|
13
|
+
fun readAsset(path: String): ByteArray? = try {
|
|
14
|
+
if (path.startsWith("file:")) File(URI(path)).inputStream().use { it.readBytes() }
|
|
15
|
+
else assets.open(path).use { it.readBytes() }
|
|
16
|
+
} catch (e: FileNotFoundException) {
|
|
17
|
+
// FileInputStream also uses FileNotFoundException for denied access: only absence is null.
|
|
18
|
+
if (path.startsWith("file:") && File(URI(path)).exists())
|
|
19
|
+
throw CrocheError("asset_read_failed", "App asset could not be read")
|
|
20
|
+
null
|
|
21
|
+
} catch (_: IOException) { throw CrocheError("asset_read_failed", "App asset could not be read") }
|
|
22
|
+
catch (_: SecurityException) { throw CrocheError("asset_read_failed", "App asset could not be read") }
|
|
23
|
+
catch (_: Exception) { throw CrocheError("internal", "Unexpected app asset failure") }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The application, not this library's BuildConfig, supplies Android's debuggable fact. */
|
|
27
|
+
internal data class AndroidPlatformFacts(val debuggable: Boolean)
|
|
28
|
+
internal fun androidPlatformFacts(context: Context) = AndroidPlatformFacts(
|
|
29
|
+
(context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0,
|
|
30
|
+
)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package dev.croche.android.pal
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import com.google.gson.JsonObject
|
|
5
|
+
import dev.croche.ffi.CoreAgent
|
|
6
|
+
import dev.croche.ffi.CoreCroche
|
|
7
|
+
import dev.croche.ffi.CoreStoragePal
|
|
8
|
+
import dev.croche.ffi.CoreToolRegistration
|
|
9
|
+
import dev.croche.ffi.HostStreamPlatform
|
|
10
|
+
import dev.croche.ffi.HostPal
|
|
11
|
+
import dev.croche.s7.CoreFactoryCalls
|
|
12
|
+
import dev.croche.s7.CoreOptionsEncoding
|
|
13
|
+
|
|
14
|
+
/** Internal platform entry for Kotlin/RN: real PALs, one Core owner, no successful fallback.
|
|
15
|
+
* This binds the ABI. The pinned Core factory still fails closed until its owner is complete.
|
|
16
|
+
*/
|
|
17
|
+
internal class AndroidCoreFactory(context: Context, private val reactNative: Boolean = false) {
|
|
18
|
+
private val platformStorage = AndroidStoragePal(context.applicationContext)
|
|
19
|
+
private val context = context.applicationContext
|
|
20
|
+
|
|
21
|
+
suspend fun open(
|
|
22
|
+
options: JsonObject,
|
|
23
|
+
registrations: List<CoreToolRegistration>? = null,
|
|
24
|
+
customStorage: CoreStoragePal? = null,
|
|
25
|
+
): CoreCroche {
|
|
26
|
+
val encoded = CoreOptionsEncoding.encode(options, registrations, platformStorage, customStorage)
|
|
27
|
+
val android = AndroidCoreHost(context, encoded.storage)
|
|
28
|
+
val host = if (!reactNative) android else object : HostPal by android {
|
|
29
|
+
override fun streamPlatform() = HostStreamPlatform.REACT_NATIVE
|
|
30
|
+
}
|
|
31
|
+
return CoreFactoryCalls.open(encoded.optionsJson, encoded.registrations, encoded.storage, host)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fun agent(owner: CoreCroche, id: String, scopeJson: String?): CoreAgent = owner.agent(id, scopeJson)
|
|
35
|
+
}
|