@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,116 @@
|
|
|
1
|
+
package dev.croche.android.pal
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.net.Uri
|
|
5
|
+
import com.google.gson.JsonParser
|
|
6
|
+
import dev.croche.android.AndroidBackgroundWake
|
|
7
|
+
import dev.croche.CrocheError
|
|
8
|
+
import dev.croche.NativeImagePal
|
|
9
|
+
import dev.croche.ResponseSink
|
|
10
|
+
import dev.croche.RuntimeCancellation
|
|
11
|
+
import dev.croche.android.BitmapDecodedImageCodec
|
|
12
|
+
import dev.croche.ffi.*
|
|
13
|
+
import dev.croche.s7.attached
|
|
14
|
+
import dev.croche.s7.effectCallback
|
|
15
|
+
import dev.croche.s7.pal.BlobFiles
|
|
16
|
+
import dev.croche.s7.pal.DirectHttpPal
|
|
17
|
+
import java.io.ByteArrayOutputStream
|
|
18
|
+
import java.io.File
|
|
19
|
+
import java.util.TimeZone
|
|
20
|
+
import java.util.UUID
|
|
21
|
+
|
|
22
|
+
/** Platform effects for the one S1 factory. No legacy project open, record routing or registry. */
|
|
23
|
+
internal class AndroidCoreHost(
|
|
24
|
+
context: Context,
|
|
25
|
+
private val storage: CoreStoragePal,
|
|
26
|
+
) : HostPal {
|
|
27
|
+
private val context = context.applicationContext
|
|
28
|
+
private val assets = AndroidAssetPal(this.context)
|
|
29
|
+
private val http = DirectHttpPal()
|
|
30
|
+
private val codec = BitmapDecodedImageCodec()
|
|
31
|
+
private val captures = NativeImagePal()
|
|
32
|
+
private val blobs = BlobFiles(File(this.context.noBackupFilesDir, "croche-pal-v1"), syncDirectory = ::syncDirectory)
|
|
33
|
+
override fun blobOperation(operation: String, key: String, cursor: String?, bytes: ByteArray?) =
|
|
34
|
+
blobs.operation(operation, key, cursor, bytes)
|
|
35
|
+
override fun streamPlatform() = HostStreamPlatform.OTHER
|
|
36
|
+
override fun platformFacts() = PalPlatformFacts(PalTargetKind.ANDROID, androidPlatformFacts(context).debuggable,
|
|
37
|
+
context.resources.configuration.locales[0]?.toLanguageTag(), "android-play-integrity")
|
|
38
|
+
private val encodedImages = dev.croche.EncodedImageCodec(codec)
|
|
39
|
+
override fun imageSupport() = com.google.gson.Gson().toJson(encodedImages.support)
|
|
40
|
+
override fun imageOperation(operation: String, bytes: ByteArray, optionsJson: String, cancel: CancelToken) =
|
|
41
|
+
effectCallback { encodedImages.operation(operation, bytes, optionsJson, cancel) }
|
|
42
|
+
override fun releaseImage(capture: String) = captures.release(capture)
|
|
43
|
+
|
|
44
|
+
override fun readImage(request: PalImageReadRequest, cancel: CancelToken): ByteArray = effectCallback {
|
|
45
|
+
if (request.source is PalImageReadSource.Capture) return@effectCallback captures.readBytes(request, cancel)
|
|
46
|
+
NativeImagePal.check(cancel)
|
|
47
|
+
val uri = (request.source as PalImageReadSource.Local).uri
|
|
48
|
+
try {
|
|
49
|
+
val input = if (Uri.parse(uri).scheme == null) context.assets.open(uri)
|
|
50
|
+
else context.contentResolver.openInputStream(Uri.parse(uri))
|
|
51
|
+
val bytes = input?.use { source ->
|
|
52
|
+
val output = ByteArrayOutputStream()
|
|
53
|
+
val buffer = ByteArray(8192)
|
|
54
|
+
while (true) {
|
|
55
|
+
NativeImagePal.check(cancel)
|
|
56
|
+
val count = source.read(buffer)
|
|
57
|
+
if (count < 0) break
|
|
58
|
+
if (output.size().toULong() + count.toULong() > request.maxBytes)
|
|
59
|
+
throw CrocheError("image_fetch_failed", "Image source exceeds the requested byte bound")
|
|
60
|
+
output.write(buffer, 0, count)
|
|
61
|
+
}
|
|
62
|
+
NativeImagePal.check(cancel)
|
|
63
|
+
output.toByteArray()
|
|
64
|
+
} ?: throw CrocheError("image_fetch_failed", "The image source is missing")
|
|
65
|
+
// Core sniffs and hashes these owned bytes; no JS/base64 or host metadata.
|
|
66
|
+
bytes
|
|
67
|
+
} catch (error: CrocheError) { throw error }
|
|
68
|
+
catch (_: java.io.IOException) { throw CrocheError("image_fetch_failed", "The image source could not be read") }
|
|
69
|
+
catch (_: SecurityException) { throw CrocheError("image_fetch_failed", "The image source could not be read") }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
override fun http(request: PalHttpRequest, sink: HttpSink, cancel: CancelToken) = effectCallback {
|
|
73
|
+
http.http(request, object : ResponseSink {
|
|
74
|
+
override fun head(status: Int, headers: List<PalHeader>) = sink.head(status.toUShort(), headers)
|
|
75
|
+
override fun chunk(bytes: ByteArray) = sink.chunk(bytes)
|
|
76
|
+
}, signal(cancel))
|
|
77
|
+
}
|
|
78
|
+
override fun resolve(host: String, cancel: CancelToken) = effectCallback { http.resolve(host, signal(cancel)) }
|
|
79
|
+
override fun readAsset(path: String) = effectCallback { assets.readAsset(path) }
|
|
80
|
+
|
|
81
|
+
// Transitional HostPal storage members are the SAME physical PAL, not a snapshot adapter.
|
|
82
|
+
// S1 composes the separately supplied CoreStoragePal for its actual storage operations.
|
|
83
|
+
override fun read(key: String) = storage.read(key)
|
|
84
|
+
override fun write(key: String, value: String) = storage.write(key, value)
|
|
85
|
+
override fun remove(key: String) = storage.remove(key)
|
|
86
|
+
override fun purgeInstance(prefix: String) = storage.purgeInstance(prefix, null)
|
|
87
|
+
override fun list(prefix: String) = storage.list(prefix)
|
|
88
|
+
override fun readSecret(key: String) = storage.readSecret(key)
|
|
89
|
+
override fun writeSecret(key: String, value: String) = storage.writeSecret(key, value)
|
|
90
|
+
override fun removeSecret(key: String) = storage.removeSecret(key)
|
|
91
|
+
override fun lock(key: String) = storage.lock(key)
|
|
92
|
+
override fun unlock(key: String) = storage.unlock(key)
|
|
93
|
+
override fun storeIdentity() = context.noBackupFilesDir.canonicalPath
|
|
94
|
+
override fun nowMs() = System.currentTimeMillis().toDouble()
|
|
95
|
+
override fun utcOffsetMinutes(atMs: Double) = TimeZone.getDefault().getOffset(atMs.toLong()).toLong() / 60_000
|
|
96
|
+
override fun random() = UUID.randomUUID().toString()
|
|
97
|
+
|
|
98
|
+
// CORE-GAP: factory Runtime.tick must be driven by Core's owned loop. There is no
|
|
99
|
+
// factory tick export at this pin; deliverDue is NOT a substitute for maintenance.
|
|
100
|
+
override fun wake(atMs: Double?) = Unit
|
|
101
|
+
override fun registerBackground(identifier: String, wake: CoreBackgroundWake) =
|
|
102
|
+
AndroidBackgroundWake.install(context, identifier, wake)
|
|
103
|
+
override fun schedule(entries: String) {
|
|
104
|
+
val earliest = JsonParser.parseString(entries).asJsonArray.map { java.time.Instant.parse(it.asJsonObject.get("at").asString).toEpochMilli() }.minOrNull()
|
|
105
|
+
AndroidBackgroundWake.schedule(earliest, System.currentTimeMillis())
|
|
106
|
+
}
|
|
107
|
+
override fun tool(invocation: String, cancel: CancelToken): ToolOutput =
|
|
108
|
+
throw CorePalException.Failure("capability_unsupported", "S1 uses registered Core tool definitions")
|
|
109
|
+
override fun attest(request: String, timeoutMs: ULong): String =
|
|
110
|
+
throw CorePalException.Failure("attestation_unsupported", "No attestation provider is attached")
|
|
111
|
+
|
|
112
|
+
private fun signal(cancel: CancelToken) = RuntimeCancellation().also { signal ->
|
|
113
|
+
if (cancel.isCancelled()) signal.cancel()
|
|
114
|
+
cancel.onCancel(attached(object : CancelCallback { override fun cancelled() = signal.cancel() }))
|
|
115
|
+
}
|
|
116
|
+
}
|
package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidMainDispatcher.kt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
package dev.croche.android.pal
|
|
2
|
+
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import kotlinx.coroutines.CoroutineDispatcher
|
|
6
|
+
import java.util.concurrent.RejectedExecutionException
|
|
7
|
+
import kotlin.coroutines.CoroutineContext
|
|
8
|
+
|
|
9
|
+
/** The platform UI executor, not another event queue. Core owns event order and delivery gates.
|
|
10
|
+
* Uses Android itself; the common SDK does not require a coroutines-android dependency. */
|
|
11
|
+
internal object AndroidMainDispatcher : CoroutineDispatcher() {
|
|
12
|
+
private val handler by lazy { Handler(Looper.getMainLooper()) }
|
|
13
|
+
override fun isDispatchNeeded(context: CoroutineContext) = Looper.myLooper() != Looper.getMainLooper()
|
|
14
|
+
override fun dispatch(context: CoroutineContext, block: Runnable) {
|
|
15
|
+
if (!handler.post(block)) throw RejectedExecutionException("The Android main looper is unavailable")
|
|
16
|
+
}
|
|
17
|
+
}
|
package/android/vendor/CrocheRuntimeSDK/android/dev/croche/android/pal/AndroidPublicPlatform.kt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
package dev.croche.android.pal
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import dev.croche.SelectedAdapters
|
|
5
|
+
import dev.croche.s7.NativePlatformPals
|
|
6
|
+
import dev.croche.s7.NativePlatformProvider
|
|
7
|
+
import dev.croche.s7.backgroundStorage
|
|
8
|
+
|
|
9
|
+
/** Loaded by the common facade; the app's Context remains an opaque platform handle. */
|
|
10
|
+
internal class AndroidPublicPlatform : NativePlatformProvider {
|
|
11
|
+
override fun create(platform: Any?, adapters: SelectedAdapters): NativePlatformPals {
|
|
12
|
+
val context = (platform as Context).applicationContext
|
|
13
|
+
val (pal, platformStorage) = backgroundStorage(adapters, AndroidStoragePal(context))
|
|
14
|
+
val host = AndroidCoreHost(context, pal)
|
|
15
|
+
return NativePlatformPals(pal, host, AndroidMainDispatcher, platformStorage)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
package dev.croche.android.pal
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import dev.croche.s7.pal.DirectHttpPal
|
|
5
|
+
|
|
6
|
+
/** Android effects for S2. No admission policy, record layout or wiki cache lives here.
|
|
7
|
+
* AndroidCoreHost composes these same implementations through the generated callbacks.
|
|
8
|
+
* The old public host is deliberately not used as a fallback for the partial S1 factory. */
|
|
9
|
+
internal class AndroidS2Pal(context: Context) {
|
|
10
|
+
val storage = AndroidStoragePal(context)
|
|
11
|
+
val assets = AndroidAssetPal(context)
|
|
12
|
+
val http = DirectHttpPal()
|
|
13
|
+
val platformFacts = androidPlatformFacts(context)
|
|
14
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
package dev.croche.android.pal
|
|
2
|
+
|
|
3
|
+
import android.database.AbstractWindowedCursor
|
|
4
|
+
import android.database.Cursor
|
|
5
|
+
import android.database.sqlite.SQLiteBlobTooBigException
|
|
6
|
+
import android.database.sqlite.SQLiteDatabase
|
|
7
|
+
import android.os.ParcelFileDescriptor
|
|
8
|
+
import dev.croche.StateCommitResult
|
|
9
|
+
import dev.croche.StateCondition
|
|
10
|
+
import dev.croche.StateEntry
|
|
11
|
+
import dev.croche.StateListOptions
|
|
12
|
+
import dev.croche.StateOp
|
|
13
|
+
import dev.croche.StatePage
|
|
14
|
+
import dev.croche.VersionedValue
|
|
15
|
+
import dev.croche.WriteOutcome
|
|
16
|
+
import dev.croche.s7.StateRange
|
|
17
|
+
import dev.croche.s7.stateKey
|
|
18
|
+
import dev.croche.s7.storageVersion
|
|
19
|
+
import dev.croche.s7.validateStateBatch
|
|
20
|
+
import java.io.File
|
|
21
|
+
import java.io.IOException
|
|
22
|
+
import java.nio.file.Files
|
|
23
|
+
import java.nio.file.LinkOption.NOFOLLOW_LINKS
|
|
24
|
+
import java.util.UUID
|
|
25
|
+
|
|
26
|
+
/** The rows of one CursorWindow, and how many rows the statement produced (at most its LIMIT). */
|
|
27
|
+
internal class WindowRows(val entries: List<StateEntry>, val total: Int)
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The most entries one Android page returns (HLD storage, "Range reads"). With the look-ahead row
|
|
31
|
+
* that is 33 rows, and 33 rows of Core's largest size (key plus value 60 KiB) fit one default
|
|
32
|
+
* 2 MiB CursorWindow. The statement's LIMIT has to be this small from the outset: Android's first
|
|
33
|
+
* window fill steps and counts every row the statement produces, whether or not it fits.
|
|
34
|
+
*/
|
|
35
|
+
internal const val WINDOW_PAGE_ROWS = 32
|
|
36
|
+
|
|
37
|
+
/** The range with its page held to [WINDOW_PAGE_ROWS]. A cursor does not encode the limit. */
|
|
38
|
+
internal fun windowRange(prefix: String, options: StateListOptions) =
|
|
39
|
+
StateRange(prefix, options.copy(limit = minOf(options.limit ?: WINDOW_PAGE_ROWS.toUInt(), WINDOW_PAGE_ROWS.toUInt())))
|
|
40
|
+
|
|
41
|
+
/** Runs the page statement of [range]: the page plus one look-ahead row, through one window. */
|
|
42
|
+
internal fun readPage(db: SQLiteDatabase, range: StateRange): WindowRows =
|
|
43
|
+
db.rawQuery("SELECT key, value ${range.bounds}", arrayOf(range.from, range.to, (range.limit + 1).toString())).use(::firstWindow)
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Reads a `(key, value)` query through ONE CursorWindow fill.
|
|
47
|
+
*
|
|
48
|
+
* `count` fills the first window and steps every row of the statement to count it, so the caller's
|
|
49
|
+
* LIMIT is the bound on rows visited. A refill would run the query again from its first row, so
|
|
50
|
+
* rows that did not fit the window are not read here: the caller returns a short page with a
|
|
51
|
+
* cursor, and the next call seeks to it. That only happens for rows above Core's row size.
|
|
52
|
+
* A first row too large for an empty window is reported as [SQLiteBlobTooBigException].
|
|
53
|
+
*/
|
|
54
|
+
internal fun firstWindow(cursor: Cursor): WindowRows {
|
|
55
|
+
val total = cursor.count
|
|
56
|
+
val window = (cursor as? AbstractWindowedCursor)?.window
|
|
57
|
+
// No window to inspect (not a platform SQLite cursor): read the rows as they come.
|
|
58
|
+
val held = if (window == null) total else {
|
|
59
|
+
check(window.startPosition == 0) { "StateStore page did not start at its first row" }
|
|
60
|
+
minOf(window.numRows, total)
|
|
61
|
+
}
|
|
62
|
+
if (total > 0 && held == 0) throw SQLiteBlobTooBigException("StateStore row does not fit one CursorWindow")
|
|
63
|
+
val entries = buildList(held) {
|
|
64
|
+
for (position in 0 until held) {
|
|
65
|
+
check(cursor.moveToPosition(position)) { "StateStore page row is missing" }
|
|
66
|
+
add(StateEntry(cursor.getString(0), cursor.getBlob(1).decodeToString(throwOnInvalidSequence = true)))
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return WindowRows(entries, total)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** One database per platform storage root. Each batch stays on the calling effect thread. */
|
|
73
|
+
internal class AndroidStateStore(private val path: File) : AutoCloseable {
|
|
74
|
+
private var connection: SQLiteDatabase? = null
|
|
75
|
+
|
|
76
|
+
@Synchronized private fun database(create: Boolean): SQLiteDatabase? {
|
|
77
|
+
connection?.let { return it }
|
|
78
|
+
val parent = requireNotNull(path.parentFile)
|
|
79
|
+
if (create) Files.createDirectories(parent.toPath())
|
|
80
|
+
if (!Files.exists(path.toPath(), NOFOLLOW_LINKS) && !create) return null
|
|
81
|
+
if (!Files.isDirectory(parent.toPath(), NOFOLLOW_LINKS) ||
|
|
82
|
+
(Files.exists(path.toPath(), NOFOLLOW_LINKS) && !Files.isRegularFile(path.toPath(), NOFOLLOW_LINKS))) {
|
|
83
|
+
throw IOException("Invalid StateStore path")
|
|
84
|
+
}
|
|
85
|
+
val flags = SQLiteDatabase.OPEN_READWRITE or SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING or
|
|
86
|
+
(if (create) SQLiteDatabase.CREATE_IF_NECESSARY else 0)
|
|
87
|
+
val db = SQLiteDatabase.openDatabase(path.path, null, flags)
|
|
88
|
+
try {
|
|
89
|
+
db.rawQuery("PRAGMA busy_timeout = 5000", null).use { check(it.moveToFirst()) }
|
|
90
|
+
db.execSQL("PRAGMA synchronous = NORMAL")
|
|
91
|
+
db.execSQL("CREATE TABLE IF NOT EXISTS state (key TEXT PRIMARY KEY NOT NULL, value BLOB NOT NULL, version TEXT NOT NULL)")
|
|
92
|
+
connection = db
|
|
93
|
+
return db
|
|
94
|
+
} catch (error: Throwable) {
|
|
95
|
+
db.close()
|
|
96
|
+
throw error
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@Synchronized override fun close() {
|
|
101
|
+
connection?.close()
|
|
102
|
+
connection = null
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private fun version(db: SQLiteDatabase, key: String): String? =
|
|
106
|
+
db.rawQuery("SELECT version FROM state WHERE key = ?", arrayOf(key)).use { row ->
|
|
107
|
+
if (row.moveToFirst()) row.getString(0) else null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// CursorWindow has a platform-dependent row size. A large value must not become
|
|
111
|
+
// unreadable after a successful commit. SQLite can return that value through a descriptor.
|
|
112
|
+
private fun largeValue(db: SQLiteDatabase, key: String): String =
|
|
113
|
+
db.compileStatement("SELECT value FROM state WHERE key = ?").use { statement ->
|
|
114
|
+
statement.bindString(1, key)
|
|
115
|
+
val descriptor = statement.simpleQueryForBlobFileDescriptor()
|
|
116
|
+
?: throw IOException("Could not read StateStore value")
|
|
117
|
+
ParcelFileDescriptor.AutoCloseInputStream(descriptor).use {
|
|
118
|
+
it.readBytes().decodeToString(throwOnInvalidSequence = true)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fun get(key: String): VersionedValue {
|
|
123
|
+
stateKey(key)
|
|
124
|
+
val db = database(false) ?: return VersionedValue(null, null)
|
|
125
|
+
return try {
|
|
126
|
+
db.rawQuery("SELECT value, version FROM state WHERE key = ?", arrayOf(key)).use { row ->
|
|
127
|
+
if (row.moveToFirst()) VersionedValue(row.getBlob(0).decodeToString(throwOnInvalidSequence = true), row.getString(1))
|
|
128
|
+
else VersionedValue(null, null)
|
|
129
|
+
}
|
|
130
|
+
} catch (_: SQLiteBlobTooBigException) {
|
|
131
|
+
transaction(db) {
|
|
132
|
+
val version = version(db, key)
|
|
133
|
+
VersionedValue(version?.let { largeValue(db, key) }, version)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
fun list(prefix: String, options: StateListOptions): StatePage {
|
|
139
|
+
val range = windowRange(prefix, options)
|
|
140
|
+
val db = database(false) ?: return StatePage(emptyList())
|
|
141
|
+
val bounds = range.bounds
|
|
142
|
+
return try {
|
|
143
|
+
val rows = readPage(db, range)
|
|
144
|
+
range.page(rows.entries, more = rows.entries.size < rows.total)
|
|
145
|
+
} catch (_: SQLiteBlobTooBigException) {
|
|
146
|
+
// The first row is larger than a whole window (far above Core's 60 KiB row size). It is
|
|
147
|
+
// returned alone, read as a stream; the two-key query only tells whether more rows follow.
|
|
148
|
+
transaction(db) {
|
|
149
|
+
val keys = db.rawQuery("SELECT key $bounds", arrayOf(range.from, range.to, "2")).use { cursor ->
|
|
150
|
+
buildList { while (cursor.moveToNext()) add(cursor.getString(0)) }
|
|
151
|
+
}
|
|
152
|
+
range.page(keys.take(1).map { StateEntry(it, largeValue(db, it)) }, more = keys.size > 1)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private fun apply(db: SQLiteDatabase, op: StateOp): String? = when (op) {
|
|
158
|
+
is StateOp.Put -> UUID.randomUUID().toString().also { version ->
|
|
159
|
+
db.execSQL("INSERT OR REPLACE INTO state(key, value, version) VALUES (?, ?, ?)", arrayOf(op.key, op.value.encodeToByteArray(), version))
|
|
160
|
+
}
|
|
161
|
+
is StateOp.Delete -> {
|
|
162
|
+
db.execSQL("DELETE FROM state WHERE key = ?", arrayOf(op.key))
|
|
163
|
+
null
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private inline fun <T> transaction(db: SQLiteDatabase, block: () -> T): T {
|
|
168
|
+
db.beginTransactionNonExclusive()
|
|
169
|
+
try {
|
|
170
|
+
val result = block()
|
|
171
|
+
db.setTransactionSuccessful()
|
|
172
|
+
return result
|
|
173
|
+
} finally { db.endTransaction() }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
fun commit(ops: List<StateOp>, condition: StateCondition): StateCommitResult {
|
|
177
|
+
validateStateBatch(ops, condition)
|
|
178
|
+
val db = requireNotNull(database(true))
|
|
179
|
+
return transaction(db) {
|
|
180
|
+
var version = version(db, condition.key)
|
|
181
|
+
if (version != condition.version) return@transaction StateCommitResult.Conflict
|
|
182
|
+
for (op in ops) {
|
|
183
|
+
val written = apply(db, op)
|
|
184
|
+
if (op.key == condition.key) version = written
|
|
185
|
+
}
|
|
186
|
+
StateCommitResult.Committed(version)
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
fun write(key: String, value: String) {
|
|
191
|
+
val validKey = stateKey(key)
|
|
192
|
+
apply(requireNotNull(database(true)), StateOp.Put(validKey, value))
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
fun remove(key: String) {
|
|
196
|
+
stateKey(key)
|
|
197
|
+
database(false)?.let { apply(it, StateOp.Delete(key)) }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
fun writeIf(key: String, value: String?, expect: String?): WriteOutcome {
|
|
201
|
+
stateKey(key)
|
|
202
|
+
storageVersion(expect)
|
|
203
|
+
val db = requireNotNull(database(true))
|
|
204
|
+
return transaction(db) {
|
|
205
|
+
val current = version(db, key)
|
|
206
|
+
if (current != expect) return@transaction WriteOutcome.Conflict(current)
|
|
207
|
+
WriteOutcome.Written(apply(db, if (value != null) StateOp.Put(key, value) else StateOp.Delete(key)))
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
fun purge(prefix: String) {
|
|
212
|
+
require(prefix.length > 1 && prefix.endsWith('/'))
|
|
213
|
+
val range = StateRange(prefix, StateListOptions())
|
|
214
|
+
database(false)?.execSQL("DELETE FROM state WHERE key >= ? AND key < ?", arrayOf(range.lower, range.upper))
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
package dev.croche.android.pal
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.security.keystore.KeyGenParameterSpec
|
|
5
|
+
import android.security.keystore.KeyProperties
|
|
6
|
+
import android.system.Os
|
|
7
|
+
import android.system.OsConstants
|
|
8
|
+
import dev.croche.StateListOptions
|
|
9
|
+
import dev.croche.ffi.CoreStateCondition
|
|
10
|
+
import dev.croche.ffi.CoreStateListOptions
|
|
11
|
+
import dev.croche.ffi.CoreStateOp
|
|
12
|
+
import dev.croche.ffi.PalLockOutcome
|
|
13
|
+
import dev.croche.s7.LocalStoragePal
|
|
14
|
+
import dev.croche.s7.logicalStorageKey
|
|
15
|
+
import dev.croche.s7.versionedStorageKey
|
|
16
|
+
import dev.croche.s7.storageCallback
|
|
17
|
+
import dev.croche.s7.toApp
|
|
18
|
+
import dev.croche.s7.toCore
|
|
19
|
+
import dev.croche.s7.pal.AtomicKeyFiles
|
|
20
|
+
import dev.croche.s7.pal.purgeInstanceFiles
|
|
21
|
+
import java.io.File
|
|
22
|
+
import java.security.KeyStore
|
|
23
|
+
import java.util.Base64
|
|
24
|
+
import javax.crypto.Cipher
|
|
25
|
+
import javax.crypto.KeyGenerator
|
|
26
|
+
import javax.crypto.SecretKey
|
|
27
|
+
import javax.crypto.spec.GCMParameterSpec
|
|
28
|
+
|
|
29
|
+
/** S2 ordinary values and secrets have separate no-backup roots. No JSON/snapshot fallback. */
|
|
30
|
+
internal class AndroidStoragePal(context: Context) : LocalStoragePal {
|
|
31
|
+
private val root = File(context.noBackupFilesDir, "croche-pal-v1")
|
|
32
|
+
private val state = AndroidStateStore(File(root, "state.db"))
|
|
33
|
+
private val values = AtomicKeyFiles(File(root, "state"), ::syncDirectory)
|
|
34
|
+
private val secrets = AtomicKeyFiles(File(root, "secrets"), ::syncDirectory)
|
|
35
|
+
override fun read(key: String) = storageCallback { state.get(logicalStorageKey(key)).value }
|
|
36
|
+
override fun readVersioned(key: String) = storageCallback { state.get(versionedStorageKey(key)).toCore() }
|
|
37
|
+
override fun writeIf(key: String, value: String?, expect: String?) = storageCallback {
|
|
38
|
+
state.writeIf(versionedStorageKey(key), value, expect).toCore(value)
|
|
39
|
+
}
|
|
40
|
+
override fun commitState(ops: List<CoreStateOp>, condition: CoreStateCondition) = storageCallback {
|
|
41
|
+
state.commit(ops.map { it.toApp() }, condition.toApp()).toCore()
|
|
42
|
+
}
|
|
43
|
+
override fun listState(prefix: String, options: CoreStateListOptions) = storageCallback {
|
|
44
|
+
state.list(prefix, options.toApp()).toCore()
|
|
45
|
+
}
|
|
46
|
+
override fun write(key: String, value: String) = storageCallback { state.write(logicalStorageKey(key), value) }
|
|
47
|
+
override fun remove(key: String) = storageCallback { state.remove(logicalStorageKey(key)) }
|
|
48
|
+
override fun purgeInstance(prefix: String, secretKey: String?) = purgeStorage(prefix, true, true)
|
|
49
|
+
override fun purgeStorage(
|
|
50
|
+
prefix: String, localState: Boolean, localSecret: Boolean, deleteSecret: () -> Unit, deleteState: () -> Unit,
|
|
51
|
+
) = storageCallback {
|
|
52
|
+
purgeInstanceFiles(values.takeIf { localState }, secrets.takeIf { localSecret }, prefix, deleteSecret, deleteState)
|
|
53
|
+
if (localState) state.purge(prefix)
|
|
54
|
+
}
|
|
55
|
+
override fun list(prefix: String): List<String> = storageCallback {
|
|
56
|
+
logicalStorageKey(prefix)
|
|
57
|
+
buildList {
|
|
58
|
+
var cursor: String? = null
|
|
59
|
+
do {
|
|
60
|
+
val page = state.list(prefix, StateListOptions(cursor = cursor))
|
|
61
|
+
addAll(page.entries.map { it.key })
|
|
62
|
+
cursor = page.nextCursor
|
|
63
|
+
} while (cursor != null)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
override fun lock(name: String): PalLockOutcome = storageCallback {
|
|
67
|
+
if (values.lock(name)) PalLockOutcome.ACQUIRED else PalLockOutcome.HELD
|
|
68
|
+
}
|
|
69
|
+
override fun unlock(name: String) = storageCallback { values.unlock(name) }
|
|
70
|
+
override fun removeSecret(key: String) = storageCallback { secrets.remove(key) }
|
|
71
|
+
override fun readSecret(key: String): String? = secretOperation {
|
|
72
|
+
val encoded = secrets.read(key) ?: return@secretOperation null
|
|
73
|
+
val bytes = Base64.getDecoder().decode(encoded)
|
|
74
|
+
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
|
|
75
|
+
cipher.init(Cipher.DECRYPT_MODE, credentialKey(), GCMParameterSpec(128, bytes, 0, 12))
|
|
76
|
+
cipher.updateAAD(key.toByteArray(Charsets.UTF_8))
|
|
77
|
+
String(cipher.doFinal(bytes, 12, bytes.size - 12), Charsets.UTF_8)
|
|
78
|
+
}
|
|
79
|
+
override fun writeSecret(key: String, value: String) = secretOperation {
|
|
80
|
+
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
|
|
81
|
+
cipher.init(Cipher.ENCRYPT_MODE, credentialKey())
|
|
82
|
+
cipher.updateAAD(key.toByteArray(Charsets.UTF_8))
|
|
83
|
+
secrets.write(key, Base64.getEncoder().encodeToString(cipher.iv + cipher.doFinal(value.toByteArray(Charsets.UTF_8))))
|
|
84
|
+
}
|
|
85
|
+
@Synchronized private fun credentialKey(): SecretKey {
|
|
86
|
+
val store = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
|
|
87
|
+
(store.getKey(ALIAS, null) as? SecretKey)?.let { return it }
|
|
88
|
+
if (!secrets.lock("keystore-key")) throw java.io.IOException("Keystore key creation is held by another process")
|
|
89
|
+
try {
|
|
90
|
+
// Another process may have created the alias while we acquired the OS lock.
|
|
91
|
+
(store.getKey(ALIAS, null) as? SecretKey)?.let { return it }
|
|
92
|
+
return KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore").run {
|
|
93
|
+
init(KeyGenParameterSpec.Builder(ALIAS, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
|
|
94
|
+
.setBlockModes(KeyProperties.BLOCK_MODE_GCM).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
|
|
95
|
+
.setKeySize(256).build())
|
|
96
|
+
generateKey()
|
|
97
|
+
}
|
|
98
|
+
} finally { secrets.unlock("keystore-key") }
|
|
99
|
+
}
|
|
100
|
+
private inline fun <T> secretOperation(block: () -> T): T = storageCallback(block)
|
|
101
|
+
private companion object { const val ALIAS = "croche.pal.v1.secrets" }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
internal fun syncDirectory(directory: File) {
|
|
105
|
+
// O_RDONLY is public Android API; fsync still checkpoints the directory entry changes.
|
|
106
|
+
val fd = Os.open(directory.path, OsConstants.O_RDONLY, 0)
|
|
107
|
+
try { Os.fsync(fd) } finally { Os.close(fd) }
|
|
108
|
+
}
|
|
@@ -5,5 +5,11 @@
|
|
|
5
5
|
# The UniFFI bindings: JNA reads their Structure fields and callback interfaces reflectively.
|
|
6
6
|
-keep class dev.croche.ffi.** { *; }
|
|
7
7
|
|
|
8
|
-
# The
|
|
9
|
-
-
|
|
8
|
+
# The SDK finds Android's image codecs by name (Class.forName), so R8 must keep them and their
|
|
9
|
+
# no-argument constructors.
|
|
10
|
+
-keep class dev.croche.android.BitmapPalCodec { public <init>(); }
|
|
11
|
+
-keep class dev.croche.android.BitmapDecodedImageCodec { public <init>(); }
|
|
12
|
+
-keep class dev.croche.android.BitmapImageCodec { public <init>(); }
|
|
13
|
+
|
|
14
|
+
# Common Kotlin facade loads the Android-only PAL provider by name.
|
|
15
|
+
-keep class dev.croche.android.pal.AndroidPublicPlatform { public <init>(); *; }
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
package dev.croche
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonArray
|
|
4
|
+
import com.google.gson.JsonObject
|
|
5
|
+
|
|
6
|
+
/** Each entry is the implementation of one capability, never a provider wrapper. */
|
|
7
|
+
sealed interface Adapter {
|
|
8
|
+
/** Capability tag used to choose which adapter implements a host operation. */
|
|
9
|
+
val kind: String
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Format and input capabilities supplied by an image adapter.
|
|
14
|
+
*
|
|
15
|
+
* @property inputMediaTypes Source media types the adapter can read.
|
|
16
|
+
* @property maxInputPixels Upper pixel count advertised for input images, when specified.
|
|
17
|
+
* @property outputMediaTypes Media types the adapter can produce.
|
|
18
|
+
*/
|
|
19
|
+
data class ImageSupport(val inputMediaTypes: List<String>, val outputMediaTypes: List<String>, val maxInputPixels: Long? = null)
|
|
20
|
+
/**
|
|
21
|
+
* Dimensions of the upright, EXIF-applied first frame.
|
|
22
|
+
*
|
|
23
|
+
* @property hasAlpha Whether the image has an alpha channel, when known.
|
|
24
|
+
* @property height Upright first-frame height in pixels.
|
|
25
|
+
* @property mediaType Detected media type of the source image.
|
|
26
|
+
* @property width Upright first-frame width in pixels.
|
|
27
|
+
*/
|
|
28
|
+
data class ImageInfo(val mediaType: String, val width: Int, val height: Int, val hasAlpha: Boolean? = null)
|
|
29
|
+
/**
|
|
30
|
+
* Bounds and output formats for a resized model view.
|
|
31
|
+
*
|
|
32
|
+
* @property maxBytes Maximum encoded byte length of the resized image.
|
|
33
|
+
* @property maxSide Maximum length of the longest side in pixels, when set.
|
|
34
|
+
* @property outputMediaTypes Accepted media types for the resized output.
|
|
35
|
+
*/
|
|
36
|
+
data class ImageResizeLimits(val maxBytes: Long, val outputMediaTypes: List<String>, val maxSide: Int? = null)
|
|
37
|
+
|
|
38
|
+
/** Owned encoded files; coroutine cancellation is the operation's cancellation signal. */
|
|
39
|
+
interface ImageAdapter : Adapter {
|
|
40
|
+
/** Host adapter tag for image inspection and resizing. */
|
|
41
|
+
override val kind: String get() = "image"
|
|
42
|
+
/** Formats and input bounds advertised to Core by this adapter. */
|
|
43
|
+
val support: ImageSupport
|
|
44
|
+
/** Find out whether a photo fits before preparing it for the selected model. */
|
|
45
|
+
suspend fun info(bytes: ByteArray): ImageInfo
|
|
46
|
+
/** Fit a large photo within the model’s limits while keeping the user’s original. */
|
|
47
|
+
suspend fun resize(bytes: ByteArray, limits: ImageResizeLimits): ByteArray
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
internal data class SelectedAdapters(
|
|
51
|
+
val state: StateStore? = null,
|
|
52
|
+
val image: ImageAdapter? = null,
|
|
53
|
+
)
|
|
54
|
+
internal fun adaptersFor(adapters: List<Adapter>?): SelectedAdapters {
|
|
55
|
+
var state: StateStore? = null
|
|
56
|
+
var image: ImageAdapter? = null
|
|
57
|
+
val seen = mutableSetOf<String>()
|
|
58
|
+
adapters.orEmpty().forEach { adapter ->
|
|
59
|
+
if (adapter.kind !in listOf("image", "state"))
|
|
60
|
+
throw CrocheError("invalid_configuration", "Unknown adapter kind.", AppStreamContract.ErrorCategory.SETUP)
|
|
61
|
+
if (!seen.add(adapter.kind))
|
|
62
|
+
throw CrocheError("invalid_configuration", "Duplicate adapter kind: ${adapter.kind}.", AppStreamContract.ErrorCategory.SETUP)
|
|
63
|
+
when (adapter.kind) {
|
|
64
|
+
"state" -> state = adapter as? StateStore
|
|
65
|
+
?: throw CrocheError("invalid_configuration", "Adapter does not implement its declared kind.", AppStreamContract.ErrorCategory.SETUP)
|
|
66
|
+
"image" -> image = adapter as? ImageAdapter
|
|
67
|
+
?: throw CrocheError("invalid_configuration", "Adapter does not implement its declared kind.", AppStreamContract.ErrorCategory.SETUP)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return SelectedAdapters(state, image)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
internal fun List<Adapter>.markers() = JsonArray().also { rows ->
|
|
74
|
+
forEach { adapter -> rows.add(JsonObject().apply { addProperty("kind", adapter.kind) }) }
|
|
75
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package dev.croche
|
|
2
|
+
|
|
3
|
+
/** The binding facts Core reports to Cloud with each Runtime (docs/RUNTIME-CLOUD-PROTOCOL.md):
|
|
4
|
+
* `{sdk, sdk_version, platform}`, passed beside the open JSON, never an OpenOptions member. They are
|
|
5
|
+
* diagnostic and carry no logic; Core adds its own runtime_version. */
|
|
6
|
+
internal object BindingFacts {
|
|
7
|
+
/** This package's version. scripts/release-version.mjs sets it with every other SDK version, and
|
|
8
|
+
* its --check refuses a value that differs from sdk/kotlin/build.gradle.kts. */
|
|
9
|
+
const val SDK_VERSION = "0.7.0"
|
|
10
|
+
val platform: String get() =
|
|
11
|
+
if (System.getProperty("java.vm.name") == "Dalvik") "android" else platformOf(System.getProperty("os.name"))
|
|
12
|
+
|
|
13
|
+
/** A JVM OS name as Core's label (1-40 of `[a-z0-9-]`, starting with a letter): the three known
|
|
14
|
+
* names, otherwise the lowercased name itself ("FreeBSD" -> "freebsd", "SunOS" -> "sunos"), and
|
|
15
|
+
* "unknown" when no label can be made. Never "server": an unlisted OS is not guessed. */
|
|
16
|
+
internal fun platformOf(osName: String?): String {
|
|
17
|
+
val os = osName.orEmpty().lowercase()
|
|
18
|
+
return when {
|
|
19
|
+
os.startsWith("mac") -> "macos"
|
|
20
|
+
os.startsWith("windows") -> "windows"
|
|
21
|
+
os.startsWith("linux") -> "linux"
|
|
22
|
+
else -> os.replace(Regex("[^a-z0-9]+"), "-").trim('-').take(40).trimEnd('-')
|
|
23
|
+
.takeIf { it.isNotEmpty() && it[0] in 'a'..'z' } ?: "unknown"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** `sdk` is "kotlin", or "react-native" from the RN Android bridge, which builds these sources. */
|
|
27
|
+
fun json(sdk: String = "kotlin"): String =
|
|
28
|
+
obj("sdk" to str(sdk), "sdk_version" to str(SDK_VERSION), "platform" to str(platform)).toString()
|
|
29
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
package dev.croche
|
|
2
|
+
|
|
3
|
+
import com.google.gson.JsonObject
|
|
4
|
+
|
|
5
|
+
/** Context settings. Core owns defaults and validation. */
|
|
6
|
+
data class ContextConfig(
|
|
7
|
+
/** Target average input tokens per model call, 1..16_777_216. */
|
|
8
|
+
val turnTokens: Int? = null,
|
|
9
|
+
/** 0..0.5, default 0.25; controls memory inclusion; zero disables it. Ignored when memory is disabled. */
|
|
10
|
+
val memoryShare: Double? = null,
|
|
11
|
+
) {
|
|
12
|
+
/** Serialize the supplied settings for the Core configuration, omitting null values. */
|
|
13
|
+
fun toJson(): JsonObject = JsonObject().apply {
|
|
14
|
+
turnTokens?.let { addProperty("turnTokens", it) }
|
|
15
|
+
memoryShare?.let { addProperty("memoryShare", it) }
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Application memory settings. Core owns defaults and validation.
|
|
21
|
+
*
|
|
22
|
+
* @property enabled Sets whether the agent uses memory. Null leaves the field omitted.
|
|
23
|
+
* @property policy Optional memory policy text passed to Core.
|
|
24
|
+
*/
|
|
25
|
+
data class MemoryConfig(val enabled: Boolean? = null, val policy: String? = null) {
|
|
26
|
+
/** Serialize the supplied memory settings, omitting null values. */
|
|
27
|
+
fun toJson(): JsonObject = JsonObject().apply {
|
|
28
|
+
enabled?.let { addProperty("enabled", it) }
|
|
29
|
+
policy?.let { addProperty("policy", it) }
|
|
30
|
+
}
|
|
31
|
+
}
|