@app-swiss-knife/react-native-sdk 0.1.1 → 0.2.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/README.md +51 -0
- package/android/build.gradle +7 -0
- package/android/consumer-rules.pro +64 -0
- package/android/src/main/java/com/swissknife/purchases/Conversions.kt +85 -0
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +37 -30
- package/android/vendor/java/com/appswissknife/SwissKnife.kt +219 -51
- package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +177 -25
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/identity/Identity.kt +15 -0
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +1 -1
- package/ios/SwissKnifePurchases.m +8 -0
- package/ios/SwissKnifePurchases.swift +35 -0
- package/ios/vendor/SwissKnifeiOS/Attribution/Attribution.swift +25 -7
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
- package/ios/vendor/SwissKnifeiOS/Identity/Identity.swift +8 -2
- package/ios/vendor/SwissKnifeiOS/Net/BackendHttpClient.swift +32 -1
- package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +35 -5
- package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +39 -22
- package/ios/vendor/SwissKnifeiOS/SwissKnife+DeviceID.swift +12 -2
- package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +270 -32
- package/lib/commonjs/identity.js +59 -4
- package/lib/commonjs/identity.js.map +1 -1
- package/lib/commonjs/native/spec.js.map +1 -1
- package/lib/commonjs/purchases.js +95 -3
- package/lib/commonjs/purchases.js.map +1 -1
- package/lib/commonjs/types.js.map +1 -1
- package/lib/module/identity.js +59 -4
- package/lib/module/identity.js.map +1 -1
- package/lib/module/native/spec.js.map +1 -1
- package/lib/module/purchases.js +95 -3
- package/lib/module/purchases.js.map +1 -1
- package/lib/module/types.js.map +1 -1
- package/lib/typescript/commonjs/identity.d.ts +30 -4
- package/lib/typescript/commonjs/identity.d.ts.map +1 -1
- package/lib/typescript/commonjs/native/spec.d.ts +27 -0
- package/lib/typescript/commonjs/native/spec.d.ts.map +1 -1
- package/lib/typescript/commonjs/purchases.d.ts +46 -1
- package/lib/typescript/commonjs/purchases.d.ts.map +1 -1
- package/lib/typescript/commonjs/types.d.ts +11 -2
- package/lib/typescript/commonjs/types.d.ts.map +1 -1
- package/lib/typescript/module/identity.d.ts +30 -4
- package/lib/typescript/module/identity.d.ts.map +1 -1
- package/lib/typescript/module/native/spec.d.ts +27 -0
- package/lib/typescript/module/native/spec.d.ts.map +1 -1
- package/lib/typescript/module/purchases.d.ts +46 -1
- package/lib/typescript/module/purchases.d.ts.map +1 -1
- package/lib/typescript/module/types.d.ts +11 -2
- package/lib/typescript/module/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/identity.ts +63 -4
- package/src/native/spec.ts +30 -0
- package/src/purchases.ts +103 -4
- package/src/types.ts +11 -2
|
@@ -64,6 +64,41 @@ object SwissKnife {
|
|
|
64
64
|
@JvmStatic
|
|
65
65
|
fun getGateway(): PurchasesGateway = gateway
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Test seam — drop the process-global configuration flags so a test can
|
|
69
|
+
* exercise the "nothing configured yet" branch. Mirror of iOS
|
|
70
|
+
* `SwissKnife._dangerouslyResetConfigForTesting()`. Production code MUST NOT
|
|
71
|
+
* call this.
|
|
72
|
+
*
|
|
73
|
+
* Clearing [observabilityWired] is the point: it is a once-per-process guard,
|
|
74
|
+
* so without a reset any test running after the first `configure(...)` sees
|
|
75
|
+
* [ensureObservabilityBackend] early-return and can never observe the
|
|
76
|
+
* analytics-only wiring path.
|
|
77
|
+
*/
|
|
78
|
+
@VisibleForTesting
|
|
79
|
+
@JvmStatic
|
|
80
|
+
internal fun _resetConfigurationStateForTesting() {
|
|
81
|
+
observabilityWired = false
|
|
82
|
+
coreConfigured = false
|
|
83
|
+
offeringsRepository = null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Test seam — restore the once-per-process "already wired" steady state
|
|
88
|
+
* without re-running any wiring.
|
|
89
|
+
*
|
|
90
|
+
* A test class that called [_resetConfigurationStateForTesting] must call this
|
|
91
|
+
* in its teardown. Otherwise it hands the NEXT test class a process in which
|
|
92
|
+
* [ensureObservabilityBackend] re-installs the ingest pipeline mid-suite,
|
|
93
|
+
* whose extra HTTP traffic then consumes MockWebServer responses that class
|
|
94
|
+
* had queued for its own assertions.
|
|
95
|
+
*/
|
|
96
|
+
@VisibleForTesting
|
|
97
|
+
@JvmStatic
|
|
98
|
+
internal fun _markObservabilityWiredForTesting() {
|
|
99
|
+
observabilityWired = true
|
|
100
|
+
}
|
|
101
|
+
|
|
67
102
|
/**
|
|
68
103
|
* Production seam for optional store add-ons (e.g. swiss-knife-android-rustore)
|
|
69
104
|
* to install their [PurchasesGateway] before [configure]. The base SDK never
|
|
@@ -78,10 +113,18 @@ object SwissKnife {
|
|
|
78
113
|
}
|
|
79
114
|
|
|
80
115
|
/**
|
|
81
|
-
* Configure the SDK
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
116
|
+
* Configure the WHOLE SDK — core (ingest / identity / attribution /
|
|
117
|
+
* attributes) **and** the purchase engine (Google Play Billing).
|
|
118
|
+
*
|
|
119
|
+
* Exactly equivalent to [configureCore] followed by [configurePurchases], and
|
|
120
|
+
* kept behaviour-compatible with the pre-split entry point, so every existing
|
|
121
|
+
* integration keeps working unchanged.
|
|
122
|
+
*
|
|
123
|
+
* **If your app already has another IAP library** (RevenueCat, a hand-rolled
|
|
124
|
+
* BillingClient, …), call [configureCore] instead: this method builds a
|
|
125
|
+
* [com.appswissknife.purchases.BillingClientGateway]'s `BillingClient` and
|
|
126
|
+
* registers its `PurchasesUpdatedListener`, which will contend with the other
|
|
127
|
+
* library's client. [configureCore] never touches Play Billing.
|
|
85
128
|
*/
|
|
86
129
|
@JvmStatic
|
|
87
130
|
@JvmOverloads
|
|
@@ -95,75 +138,107 @@ object SwissKnife {
|
|
|
95
138
|
attribution: Attribution.Configuration = Attribution.Configuration(),
|
|
96
139
|
) {
|
|
97
140
|
require(apiKey.isNotEmpty()) { "configure requires a non-empty apiKey" }
|
|
98
|
-
|
|
141
|
+
configureCore(
|
|
142
|
+
context = context,
|
|
143
|
+
apiKey = apiKey,
|
|
144
|
+
appUserID = appUserID,
|
|
145
|
+
proxyURL = proxyURL,
|
|
146
|
+
logLevel = logLevel,
|
|
147
|
+
attribution = attribution,
|
|
148
|
+
)
|
|
149
|
+
configurePurchases(
|
|
150
|
+
context = context,
|
|
151
|
+
apiKey = apiKey,
|
|
152
|
+
appUserID = appUserID,
|
|
153
|
+
proxyURL = proxyURL,
|
|
154
|
+
useAmazon = useAmazon,
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Configure everything the SDK can do **without owning the store**:
|
|
160
|
+
* analytics / crash / performance ingest, [com.appswissknife.identity.Identity],
|
|
161
|
+
* [com.appswissknife.attribution.Attribution], and the subscriber-attributes
|
|
162
|
+
* client.
|
|
163
|
+
*
|
|
164
|
+
* Guarantees — asserted by `StoreOwnershipSeparationTest`:
|
|
165
|
+
* - **No `BillingClient` is instantiated.** The [PurchasesGateway] is never
|
|
166
|
+
* configured, so `BillingClient.newBuilder(...)` is never reached and no
|
|
167
|
+
* `PurchasesUpdatedListener` is registered. An app that keeps RevenueCat
|
|
168
|
+
* can adopt identity / attribution / analytics with no Play Billing
|
|
169
|
+
* contention.
|
|
170
|
+
* - **No install is registered as a side effect.** Unlike [configure], this
|
|
171
|
+
* does NOT honour [Attribution.Configuration.sendInstallOnFirstLaunch] —
|
|
172
|
+
* see [configurePurchases] for where that moved and why. Hosts that want
|
|
173
|
+
* the install POST from a core-only app call
|
|
174
|
+
* `Attribution.registerInstall(...)` / `collectAndRegister(...)`
|
|
175
|
+
* explicitly.
|
|
176
|
+
*
|
|
177
|
+
* `attribution` is still accepted because the *configuration* (which
|
|
178
|
+
* collectors to consult) is a core concern governing those explicit calls.
|
|
179
|
+
*
|
|
180
|
+
* Mirror of `SwissKnife.configureCore` on iOS — keep the two in lockstep.
|
|
181
|
+
*/
|
|
182
|
+
@JvmStatic
|
|
183
|
+
@JvmOverloads
|
|
184
|
+
fun configureCore(
|
|
185
|
+
context: Context,
|
|
186
|
+
apiKey: String,
|
|
187
|
+
appUserID: String? = null,
|
|
188
|
+
proxyURL: String? = null,
|
|
189
|
+
logLevel: String? = null,
|
|
190
|
+
attribution: Attribution.Configuration = Attribution.Configuration(),
|
|
191
|
+
) {
|
|
192
|
+
require(apiKey.isNotEmpty()) { "configureCore requires a non-empty apiKey" }
|
|
193
|
+
val serverUrl = proxyURL?.trimEnd('/').orEmpty()
|
|
99
194
|
// Phase 3 of the RC rip-and-replace — wire AttributesClient so the
|
|
100
195
|
// setEmail/setPhoneNumber/etc. methods below have somewhere to send to.
|
|
101
196
|
// Anonymous-id / user-id come from the gateway lazily (per-call), not
|
|
102
197
|
// captured at configure time, so logIn() between configure() and a
|
|
103
|
-
// setEmail() resolves to the right user_id.
|
|
198
|
+
// setEmail() resolves to the right user_id. When the purchase engine is
|
|
199
|
+
// NOT configured (core-only app) we fall back to the Identity snapshot,
|
|
200
|
+
// which is the same source of truth iOS uses — without the fallback,
|
|
201
|
+
// attributes from an analytics-only app would carry no ids at all.
|
|
104
202
|
AttributesClient.getInstance(context).configure(
|
|
105
203
|
context = context,
|
|
106
204
|
serverURL = proxyURL,
|
|
107
205
|
apiKey = apiKey,
|
|
108
206
|
anonymousIdProvider = {
|
|
109
|
-
if (gateway.isConfigured()) gateway.getAppUserID()
|
|
207
|
+
if (gateway.isConfigured()) gateway.getAppUserID()
|
|
208
|
+
else Identity.current().let { it.userId ?: it.anonymousId }.takeIf { it.isNotEmpty() }
|
|
110
209
|
},
|
|
111
210
|
userIdProvider = {
|
|
112
|
-
if (gateway.isConfigured()
|
|
211
|
+
if (gateway.isConfigured()) {
|
|
212
|
+
if (!gateway.isAnonymous()) gateway.getAppUserID() else null
|
|
213
|
+
} else {
|
|
214
|
+
Identity.current().userId
|
|
215
|
+
}
|
|
113
216
|
},
|
|
114
217
|
)
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
//
|
|
118
|
-
//
|
|
119
|
-
val gw = gateway
|
|
120
|
-
val priceProvider = gw.priceEnrichmentProvider()
|
|
121
|
-
if (priceProvider != null && !proxyURL.isNullOrEmpty()) {
|
|
122
|
-
offeringsRepository = com.appswissknife.offerings.OfferingsRepository(
|
|
123
|
-
httpClient = com.appswissknife.net.BackendHttpClientAdapter(),
|
|
124
|
-
serverUrl = proxyURL.trimEnd('/'),
|
|
125
|
-
apiKey = apiKey,
|
|
126
|
-
platformTags = priceProvider.platformTags(),
|
|
127
|
-
priceProvider = priceProvider,
|
|
128
|
-
)
|
|
129
|
-
}
|
|
130
|
-
// Wave 1 — wire the unified [Identity] namespace so its anonymous-id /
|
|
131
|
-
// identified-id snapshot stays in lockstep with the gateway. Identity
|
|
132
|
-
// owns the `/api/sdk/users/identify` POST that previously was
|
|
133
|
-
// missing on Android (the audit's real-bug-closed item).
|
|
134
|
-
// Same store-agnostic gate as the offerings wiring: real store gateways
|
|
135
|
-
// (Play, RuStore) supply a price provider; test fakes don't, so identity's
|
|
136
|
-
// entitlements repo stays null for them exactly as before.
|
|
137
|
-
val entitlementsRepoForIdentity = if (priceProvider != null) {
|
|
138
|
-
com.appswissknife.entitlements.EntitlementsRepository.get(
|
|
139
|
-
context = context.applicationContext,
|
|
140
|
-
serverUrl = proxyURL?.trimEnd('/').orEmpty(),
|
|
141
|
-
apiKey = apiKey,
|
|
142
|
-
userIdProvider = { gateway.getAppUserID() },
|
|
143
|
-
)
|
|
144
|
-
} else null
|
|
218
|
+
// Wave 1 — wire the unified [Identity] namespace. Identity owns the
|
|
219
|
+
// `/api/sdk/users/identify` POST that previously was missing on Android
|
|
220
|
+
// (the audit's real-bug-closed item). The entitlements repository is a
|
|
221
|
+
// purchases concern and gets attached later by [configurePurchases].
|
|
145
222
|
Identity.configure(
|
|
146
223
|
context = context,
|
|
147
|
-
serverUrl =
|
|
224
|
+
serverUrl = serverUrl,
|
|
148
225
|
apiKey = apiKey,
|
|
149
226
|
seedUserId = appUserID,
|
|
150
|
-
entitlementsRepository =
|
|
227
|
+
entitlementsRepository = null,
|
|
151
228
|
)
|
|
152
|
-
// Wave 2 — Attribution
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
// disk without re-POSTing.
|
|
156
|
-
Attribution.configure(
|
|
229
|
+
// Wave 2 — Attribution. Snapshot ONLY: the auto-install POST is
|
|
230
|
+
// deliberately not a side effect of enabling core.
|
|
231
|
+
Attribution.configureCore(
|
|
157
232
|
context = context,
|
|
158
|
-
serverUrl =
|
|
233
|
+
serverUrl = serverUrl,
|
|
159
234
|
apiKey = apiKey,
|
|
160
235
|
configuration = attribution,
|
|
161
236
|
)
|
|
162
237
|
// Round C — wire the unified Net + Context primitives (BackendHttpClient +
|
|
163
238
|
// IngestClient + context suppliers). Extracted into ensureObservabilityBackend
|
|
164
|
-
// so observability can also be wired WITHOUT
|
|
165
|
-
ensureObservabilityBackend(context, apiKey,
|
|
166
|
-
// ...and then make
|
|
239
|
+
// so observability can also be wired WITHOUT the purchase engine.
|
|
240
|
+
ensureObservabilityBackend(context, apiKey, serverUrl)
|
|
241
|
+
// ...and then make configureCore() AUTHORITATIVE for the backend config.
|
|
167
242
|
// ensureObservabilityBackend is once-only, so if the analytics-only path
|
|
168
243
|
// ran first (Analytics.init, possibly with no serverURL at all) the
|
|
169
244
|
// ConfigBridge would otherwise stay pinned to ITS apiKey/baseUrl and every
|
|
@@ -174,17 +249,78 @@ object SwissKnife {
|
|
|
174
249
|
// wired flag). Mirrors `SwissKnife.ensureObservabilityBackend` on iOS,
|
|
175
250
|
// where `configure` likewise overwrites the seeded config.
|
|
176
251
|
runCatching {
|
|
177
|
-
val baseUrl = proxyURL?.trimEnd('/')?.ifBlank { null }
|
|
178
252
|
com.appswissknife.net.ConfigBridge.register {
|
|
179
|
-
com.appswissknife.net.BackendConfig(apiKey = apiKey, baseUrl =
|
|
253
|
+
com.appswissknife.net.BackendConfig(apiKey = apiKey, baseUrl = serverUrl.ifBlank { null })
|
|
180
254
|
}
|
|
181
255
|
}
|
|
182
256
|
logLevel?.let { applyLogLevel(it) }
|
|
257
|
+
coreConfigured = true
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Configure the purchase engine: the [PurchasesGateway] (production =
|
|
262
|
+
* [com.appswissknife.purchases.BillingClientGateway], which builds the
|
|
263
|
+
* `BillingClient` and registers its `PurchasesUpdatedListener`) plus the
|
|
264
|
+
* offerings repository and the entitlements cache Identity re-keys.
|
|
265
|
+
*
|
|
266
|
+
* Also the home of [Attribution.Configuration.sendInstallOnFirstLaunch].
|
|
267
|
+
* Rationale: install→campaign resolution exists to attribute *revenue*, so it
|
|
268
|
+
* belongs with the revenue engine; and keeping it here makes [configure] an
|
|
269
|
+
* exact composition of the two halves, so no existing integration loses the
|
|
270
|
+
* install event. A core-only app registers installs explicitly.
|
|
271
|
+
*
|
|
272
|
+
* Mirror of `SwissKnife.configurePurchases` on iOS.
|
|
273
|
+
*/
|
|
274
|
+
@JvmStatic
|
|
275
|
+
@JvmOverloads
|
|
276
|
+
fun configurePurchases(
|
|
277
|
+
context: Context,
|
|
278
|
+
apiKey: String,
|
|
279
|
+
appUserID: String? = null,
|
|
280
|
+
proxyURL: String? = null,
|
|
281
|
+
useAmazon: Boolean = false,
|
|
282
|
+
) {
|
|
283
|
+
require(apiKey.isNotEmpty()) { "configurePurchases requires a non-empty apiKey" }
|
|
284
|
+
gateway.configure(context, apiKey, appUserID, proxyURL, useAmazon)
|
|
285
|
+
// Wire OfferingsRepository for any store that supplies a price-enrichment
|
|
286
|
+
// provider (Play via BillingClientGateway, RuStore via the add-on). Test
|
|
287
|
+
// fakes return null → no repo, preserving the prior "production gateway
|
|
288
|
+
// only" guard so a [FakePurchasesGateway] never hits a real store query.
|
|
289
|
+
val priceProvider = gateway.priceEnrichmentProvider()
|
|
290
|
+
if (priceProvider != null && !proxyURL.isNullOrEmpty()) {
|
|
291
|
+
offeringsRepository = com.appswissknife.offerings.OfferingsRepository(
|
|
292
|
+
httpClient = com.appswissknife.net.BackendHttpClientAdapter(),
|
|
293
|
+
serverUrl = proxyURL.trimEnd('/'),
|
|
294
|
+
apiKey = apiKey,
|
|
295
|
+
platformTags = priceProvider.platformTags(),
|
|
296
|
+
priceProvider = priceProvider,
|
|
297
|
+
)
|
|
298
|
+
}
|
|
299
|
+
// Same store-agnostic gate as the offerings wiring: real store gateways
|
|
300
|
+
// (Play, RuStore) supply a price provider; test fakes don't, so identity's
|
|
301
|
+
// entitlements repo stays null for them exactly as before. Attached rather
|
|
302
|
+
// than re-`configure`d so a logIn() that landed between the two halves is
|
|
303
|
+
// not rolled back to the seed user.
|
|
304
|
+
if (priceProvider != null) {
|
|
305
|
+
Identity._attachEntitlementsRepository(
|
|
306
|
+
com.appswissknife.entitlements.EntitlementsRepository.get(
|
|
307
|
+
context = context.applicationContext,
|
|
308
|
+
serverUrl = proxyURL?.trimEnd('/').orEmpty(),
|
|
309
|
+
apiKey = apiKey,
|
|
310
|
+
userIdProvider = { gateway.getAppUserID() },
|
|
311
|
+
)
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
// Wave 2 — first launch auto-registers the install. Fire-and-forget.
|
|
315
|
+
Attribution.sendInstallOnFirstLaunchIfEnabled(context)
|
|
183
316
|
}
|
|
184
317
|
|
|
185
318
|
@Volatile
|
|
186
319
|
private var observabilityWired = false
|
|
187
320
|
|
|
321
|
+
@Volatile
|
|
322
|
+
private var coreConfigured = false
|
|
323
|
+
|
|
188
324
|
/**
|
|
189
325
|
* Wire the observability backend (analytics / crash / performance ingest)
|
|
190
326
|
* WITHOUT requiring [configure] or the purchase engine. Idempotent — safe to
|
|
@@ -230,6 +366,23 @@ object SwissKnife {
|
|
|
230
366
|
)
|
|
231
367
|
}
|
|
232
368
|
}
|
|
369
|
+
// Same for Attribution: without an appContext + apiKey its `postInstall`
|
|
370
|
+
// returns null, so `Attribution.registerInstall(...)` was a silent no-op in
|
|
371
|
+
// an analytics-only app while iOS worked. Snapshot only — this never fires
|
|
372
|
+
// the install POST (that is `configurePurchases`' job, or an explicit call).
|
|
373
|
+
runCatching {
|
|
374
|
+
if (!Attribution.isConfigured()) {
|
|
375
|
+
Attribution.configureCore(
|
|
376
|
+
context = context,
|
|
377
|
+
serverUrl = serverUrl,
|
|
378
|
+
apiKey = apiKey,
|
|
379
|
+
configuration = Attribution.Configuration(),
|
|
380
|
+
)
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
// Core is live: identity + attribution + attributes all have what they
|
|
384
|
+
// need. The purchase engine is untouched — no BillingClient anywhere.
|
|
385
|
+
coreConfigured = true
|
|
233
386
|
// Each step is wrapped in runCatching so a transient init failure
|
|
234
387
|
// (e.g. Robolectric not exposing ProcessLifecycleOwner cleanly,
|
|
235
388
|
// sandboxed ConnectivityManager) doesn't break the rest of the flow.
|
|
@@ -274,10 +427,25 @@ object SwissKnife {
|
|
|
274
427
|
}
|
|
275
428
|
}
|
|
276
429
|
|
|
430
|
+
/**
|
|
431
|
+
* Whether the PURCHASE engine is configured. Deliberately stays `false` for a
|
|
432
|
+
* core-only app — those APIs genuinely cannot work without Play Billing, and
|
|
433
|
+
* a host that keeps its own IAP layer must be able to tell the two apart.
|
|
434
|
+
*/
|
|
277
435
|
@JvmStatic
|
|
278
436
|
val isConfigured: Boolean
|
|
279
437
|
get() = gateway.isConfigured()
|
|
280
438
|
|
|
439
|
+
/**
|
|
440
|
+
* Whether the CORE is configured — ingest, [com.appswissknife.identity.Identity],
|
|
441
|
+
* [com.appswissknife.attribution.Attribution], subscriber attributes. `true`
|
|
442
|
+
* after ANY of [configure], [configureCore], or [ensureObservabilityBackend].
|
|
443
|
+
* Mirror of `SwissKnife.isCoreConfigured` on iOS.
|
|
444
|
+
*/
|
|
445
|
+
@JvmStatic
|
|
446
|
+
val isCoreConfigured: Boolean
|
|
447
|
+
get() = coreConfigured
|
|
448
|
+
|
|
281
449
|
@JvmStatic
|
|
282
450
|
val appUserID: String
|
|
283
451
|
get() = gateway.getAppUserID()
|
|
@@ -114,10 +114,105 @@ object Attribution {
|
|
|
114
114
|
@VisibleForTesting
|
|
115
115
|
internal var installReferrerCollector: InstallReferrerCollector = NoopInstallReferrerCollector()
|
|
116
116
|
|
|
117
|
-
|
|
117
|
+
@Volatile
|
|
118
|
+
private var executor: ExecutorService = newExecutor()
|
|
119
|
+
|
|
120
|
+
private fun newExecutor(): ExecutorService = Executors.newSingleThreadExecutor { r ->
|
|
118
121
|
Thread(r, "swissknife-attribution").apply { isDaemon = true }
|
|
119
122
|
}
|
|
120
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Configuration generation. Bumped by [configureCore] and
|
|
126
|
+
* [_resetForTesting]; captured into [InstallTask] when an install POST is
|
|
127
|
+
* scheduled and re-checked when it runs.
|
|
128
|
+
*
|
|
129
|
+
* **This is an intent guard, not the correctness mechanism.** Correctness
|
|
130
|
+
* comes from [InstallTask]: a scheduled task carries its own immutable copy
|
|
131
|
+
* of every input, so it can no longer be re-pointed by a later
|
|
132
|
+
* configuration no matter when it runs. The generation check exists on top
|
|
133
|
+
* of that so a task whose configuration has been *superseded* also declines
|
|
134
|
+
* to fire at all — posting an install for a configuration the host has
|
|
135
|
+
* abandoned is semantically wrong even when it is aimed at the right host.
|
|
136
|
+
*
|
|
137
|
+
* Mirrors the counter [com.appswissknife.identity.Identity] keeps for its
|
|
138
|
+
* in-flight identify POST — keep the two consistent.
|
|
139
|
+
*/
|
|
140
|
+
@Volatile
|
|
141
|
+
private var generation: Long = 0L
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Immutable snapshot of everything an install POST needs, taken when the
|
|
145
|
+
* task is SCHEDULED. Execution reads only from here and touches no
|
|
146
|
+
* process-global mutable state.
|
|
147
|
+
*
|
|
148
|
+
* Why this exists: the install POST is fire-and-forget on a shared
|
|
149
|
+
* single-thread executor, and `postInstall` used to resolve its inputs at
|
|
150
|
+
* EXECUTION time — `appContext`, `apiKey`, `serverUrl`, the `httpClient`
|
|
151
|
+
* seam, and, worst of all, `BackendHttpClient.shared`, whose `configResolver`
|
|
152
|
+
* is `{ ConfigBridge.snapshot() }` and so is re-read on every request
|
|
153
|
+
* ("`configure` mid-flight picks up the new apiKey / baseURL" — its own
|
|
154
|
+
* doc). A task still queued when the SDK was (re)configured therefore fired
|
|
155
|
+
* at the NEW backend with the NEW api key on behalf of a configuration that
|
|
156
|
+
* no longer existed. `SwissKnife.configurePurchases` schedules exactly such
|
|
157
|
+
* a task, so `configurePurchases` followed by a later `configureCore` could
|
|
158
|
+
* turn an install POST that should have been a no-op into a real one aimed
|
|
159
|
+
* at the new host.
|
|
160
|
+
*
|
|
161
|
+
* A generation check alone could not close this: it is a single gate at the
|
|
162
|
+
* top of the task, and advertising-id / install-referrer collection runs
|
|
163
|
+
* between it and the field reads. A `configureCore` landing in that window
|
|
164
|
+
* still re-pointed the POST. Capturing the inputs removes the window
|
|
165
|
+
* entirely — there is nothing left to re-point.
|
|
166
|
+
*
|
|
167
|
+
* [backend] is pinned via `BackendHttpClient.create(apiKey, baseUrl)`,
|
|
168
|
+
* which takes a FIXED `configResolver`, rather than `shared`, which takes a
|
|
169
|
+
* live one. That distinction is the whole fix on the shared-client path.
|
|
170
|
+
*/
|
|
171
|
+
private data class InstallTask(
|
|
172
|
+
val generation: Long,
|
|
173
|
+
val appContext: Context?,
|
|
174
|
+
val serverUrl: String,
|
|
175
|
+
val apiKey: String,
|
|
176
|
+
val configuration: Configuration,
|
|
177
|
+
val httpClient: BillingHttpClient,
|
|
178
|
+
val backend: com.appswissknife.net.BackendHttpClient?,
|
|
179
|
+
val advertisingIdCollector: AdvertisingIdCollector,
|
|
180
|
+
val installReferrerCollector: InstallReferrerCollector,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Capture the current configuration for a task about to be scheduled. Taken
|
|
185
|
+
* under [lock] so it cannot interleave with [configureCore].
|
|
186
|
+
*/
|
|
187
|
+
private fun captureInstallTask(): InstallTask = synchronized(lock) {
|
|
188
|
+
// Pin the shared client's CURRENT coordinates into a fixed-config
|
|
189
|
+
// instance. Reading the snapshot here — not at execution time — is what
|
|
190
|
+
// stops a later `configure` from redirecting this POST.
|
|
191
|
+
val pinnedBackend = runCatching {
|
|
192
|
+
val snapshot = com.appswissknife.net.ConfigBridge.snapshot()
|
|
193
|
+
if (snapshot.apiKey.isEmpty()) null
|
|
194
|
+
else com.appswissknife.net.BackendHttpClient.create(
|
|
195
|
+
apiKey = snapshot.apiKey,
|
|
196
|
+
baseUrl = snapshot.baseUrl,
|
|
197
|
+
)
|
|
198
|
+
}.getOrNull()
|
|
199
|
+
InstallTask(
|
|
200
|
+
generation = generation,
|
|
201
|
+
appContext = appContext,
|
|
202
|
+
serverUrl = serverUrl,
|
|
203
|
+
apiKey = apiKey,
|
|
204
|
+
configuration = configuration,
|
|
205
|
+
httpClient = httpClient,
|
|
206
|
+
backend = pinnedBackend,
|
|
207
|
+
advertisingIdCollector = advertisingIdCollector,
|
|
208
|
+
installReferrerCollector = installReferrerCollector,
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** True when a later [configureCore] / [_resetForTesting] superseded [task]. */
|
|
213
|
+
private fun isSuperseded(task: InstallTask): Boolean =
|
|
214
|
+
synchronized(lock) { generation != task.generation }
|
|
215
|
+
|
|
121
216
|
// ---- Public surface ----------------------------------------------------
|
|
122
217
|
|
|
123
218
|
/** Latest resolved attribution. Null if no install POST has succeeded. */
|
|
@@ -142,15 +237,20 @@ object Attribution {
|
|
|
142
237
|
loadFromDiskIfNeeded()
|
|
143
238
|
cached?.let { onSuccess(it); return }
|
|
144
239
|
val ctx = context.applicationContext
|
|
240
|
+
// Capture BEFORE queueing. Everything below reads from `task`.
|
|
241
|
+
val task = captureInstallTask()
|
|
145
242
|
executor.execute {
|
|
146
|
-
|
|
147
|
-
|
|
243
|
+
// The task's coordinates are still its own, so this is an intent
|
|
244
|
+
// check, not a safety one: the host has moved on. See [generation].
|
|
245
|
+
if (isSuperseded(task)) { onSuccess(null); return@execute }
|
|
246
|
+
val advId = if (task.configuration.collectAdvertisingId) {
|
|
247
|
+
runCatching { task.advertisingIdCollector.get(ctx) }.getOrNull()
|
|
148
248
|
} else null
|
|
149
|
-
val referrer = if (configuration.collectInstallReferrer) {
|
|
150
|
-
runCatching { installReferrerCollector.get(ctx) }.getOrNull()
|
|
249
|
+
val referrer = if (task.configuration.collectInstallReferrer) {
|
|
250
|
+
runCatching { task.installReferrerCollector.get(ctx) }.getOrNull()
|
|
151
251
|
} else null
|
|
152
252
|
try {
|
|
153
|
-
val result = postInstall(advertisingId = advId, referrer = referrer)
|
|
253
|
+
val result = postInstall(task, advertisingId = advId, referrer = referrer)
|
|
154
254
|
onSuccess(result)
|
|
155
255
|
} catch (e: Throwable) {
|
|
156
256
|
onError(toPurchasesError(e))
|
|
@@ -171,9 +271,14 @@ object Attribution {
|
|
|
171
271
|
) {
|
|
172
272
|
loadFromDiskIfNeeded()
|
|
173
273
|
cached?.let { onSuccess(it); return }
|
|
274
|
+
// Capture BEFORE queueing — see [InstallTask].
|
|
275
|
+
val task = captureInstallTask()
|
|
174
276
|
executor.execute {
|
|
277
|
+
// See [collectAndRegister] / [generation] — an install POST belongs
|
|
278
|
+
// to the configuration that scheduled it, never to a later one.
|
|
279
|
+
if (isSuperseded(task)) { onSuccess(null); return@execute }
|
|
175
280
|
try {
|
|
176
|
-
val result = postInstall(advertisingId = advertisingId, referrer = referrer)
|
|
281
|
+
val result = postInstall(task, advertisingId = advertisingId, referrer = referrer)
|
|
177
282
|
onSuccess(result)
|
|
178
283
|
} catch (e: Throwable) {
|
|
179
284
|
onError(toPurchasesError(e))
|
|
@@ -184,39 +289,73 @@ object Attribution {
|
|
|
184
289
|
// ---- Internal: SwissKnife wiring ---------------------------------------
|
|
185
290
|
|
|
186
291
|
/**
|
|
187
|
-
* Called by `SwissKnife.
|
|
188
|
-
*
|
|
189
|
-
*
|
|
292
|
+
* Called by `SwissKnife.configureCore(...)` (and by
|
|
293
|
+
* `SwissKnife.ensureObservabilityBackend(...)` for the analytics-only path)
|
|
294
|
+
* to snapshot the context / backend coordinates / configuration.
|
|
295
|
+
*
|
|
296
|
+
* **Deliberately has no side effects** — it does NOT register an install.
|
|
297
|
+
* Enabling core must not silently emit an install event: a host that owns
|
|
298
|
+
* attribution elsewhere would get its install data corrupted by an SDK it
|
|
299
|
+
* only adopted for analytics. `sendInstallOnFirstLaunch` therefore fires
|
|
300
|
+
* from `SwissKnife.configurePurchases(...)` — see
|
|
301
|
+
* [sendInstallOnFirstLaunchIfEnabled]. Mirror of iOS
|
|
302
|
+
* `SwissKnife.Attribution._configureCore`.
|
|
190
303
|
*/
|
|
191
|
-
internal fun
|
|
304
|
+
internal fun configureCore(
|
|
192
305
|
context: Context,
|
|
193
306
|
serverUrl: String,
|
|
194
307
|
apiKey: String,
|
|
195
308
|
configuration: Configuration,
|
|
196
309
|
) {
|
|
197
310
|
synchronized(lock) {
|
|
311
|
+
// Bump FIRST: any install POST queued under the previous
|
|
312
|
+
// coordinates is now stale and must not fire at these new ones.
|
|
313
|
+
this.generation += 1
|
|
198
314
|
this.appContext = context.applicationContext
|
|
199
315
|
this.serverUrl = serverUrl.trimEnd('/')
|
|
200
316
|
this.apiKey = apiKey
|
|
201
317
|
this.configuration = configuration
|
|
202
318
|
this.prefs = openPrefs(context.applicationContext)
|
|
203
319
|
}
|
|
204
|
-
if (configuration.sendInstallOnFirstLaunch) {
|
|
205
|
-
collectAndRegister(
|
|
206
|
-
context = context.applicationContext,
|
|
207
|
-
onSuccess = { /* cached, log-less */ },
|
|
208
|
-
onError = { /* best-effort */ },
|
|
209
|
-
)
|
|
210
|
-
}
|
|
211
320
|
}
|
|
212
321
|
|
|
322
|
+
/**
|
|
323
|
+
* Called by `SwissKnife.configurePurchases(...)`. Honours
|
|
324
|
+
* [Configuration.sendInstallOnFirstLaunch] (default `true`), so the legacy
|
|
325
|
+
* `SwissKnife.configure(...)` — which is core + purchases — keeps
|
|
326
|
+
* registering the install exactly as it always did. Fire-and-forget.
|
|
327
|
+
*/
|
|
328
|
+
internal fun sendInstallOnFirstLaunchIfEnabled(context: Context) {
|
|
329
|
+
if (!configuration.sendInstallOnFirstLaunch) return
|
|
330
|
+
collectAndRegister(
|
|
331
|
+
context = context.applicationContext,
|
|
332
|
+
onSuccess = { /* cached, log-less */ },
|
|
333
|
+
onError = { /* best-effort */ },
|
|
334
|
+
)
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Whether [configureCore] has run (an app context is wired). Used by the
|
|
339
|
+
* observability-only init path so it doesn't clobber a real configuration
|
|
340
|
+
* the core/purchase path already installed.
|
|
341
|
+
*/
|
|
342
|
+
internal fun isConfigured(): Boolean = appContext != null
|
|
343
|
+
|
|
213
344
|
// ---- Internal: HTTP + persistence --------------------------------------
|
|
214
345
|
|
|
346
|
+
/**
|
|
347
|
+
* POST the install. Every input comes from [task] — the snapshot taken when
|
|
348
|
+
* the task was scheduled — so this function reads NO process-global mutable
|
|
349
|
+
* state and cannot be re-pointed by a concurrent `configure`. See
|
|
350
|
+
* [InstallTask].
|
|
351
|
+
*/
|
|
215
352
|
private fun postInstall(
|
|
353
|
+
task: InstallTask,
|
|
216
354
|
advertisingId: String?,
|
|
217
355
|
referrer: String?,
|
|
218
356
|
): ResolvedAttribution? {
|
|
219
|
-
val ctx = appContext ?: return null
|
|
357
|
+
val ctx = task.appContext ?: return null
|
|
358
|
+
val apiKey = task.apiKey
|
|
220
359
|
if (apiKey.isEmpty()) return null
|
|
221
360
|
val identity = Identity.current()
|
|
222
361
|
val anonymousId = identity.anonymousId
|
|
@@ -242,12 +381,16 @@ object Attribution {
|
|
|
242
381
|
"X-API-Key" to apiKey,
|
|
243
382
|
)
|
|
244
383
|
|
|
245
|
-
// Round-D: when
|
|
384
|
+
// Round-D: when the backend client was wired (SwissKnife.configure
|
|
246
385
|
// path), use it so attribution shares the unified retry policy.
|
|
247
386
|
// The test seam (`httpClient`) takes precedence so AttributionTest's
|
|
248
387
|
// MockWebServer-injected client still works.
|
|
249
|
-
|
|
250
|
-
|
|
388
|
+
//
|
|
389
|
+
// `task.backend` is PINNED to the coordinates captured at schedule time
|
|
390
|
+
// — deliberately not `BackendHttpClient.shared`, whose configResolver is
|
|
391
|
+
// live and would resolve whatever `ConfigBridge` holds right now.
|
|
392
|
+
val backend = task.backend
|
|
393
|
+
if (backend != null && task.httpClient is BackendHttpClientAdapter) {
|
|
251
394
|
try {
|
|
252
395
|
val responseBody = backend.postRaw(
|
|
253
396
|
path = "/api/sdk/attribution/install",
|
|
@@ -273,8 +416,8 @@ object Attribution {
|
|
|
273
416
|
var lastError: Throwable? = null
|
|
274
417
|
while (attempt < 2) {
|
|
275
418
|
try {
|
|
276
|
-
val responseBody = httpClient.post(
|
|
277
|
-
url = "$serverUrl/api/sdk/attribution/install",
|
|
419
|
+
val responseBody = task.httpClient.post(
|
|
420
|
+
url = "${task.serverUrl}/api/sdk/attribution/install",
|
|
278
421
|
body = body,
|
|
279
422
|
headers = headers,
|
|
280
423
|
)
|
|
@@ -349,6 +492,14 @@ object Attribution {
|
|
|
349
492
|
@VisibleForTesting
|
|
350
493
|
internal fun _resetForTesting() {
|
|
351
494
|
synchronized(lock) {
|
|
495
|
+
// Same reasoning as [configureCore]: a reset invalidates every
|
|
496
|
+
// queued install POST. Bumping the generation makes those tasks
|
|
497
|
+
// no-op, and swapping in a fresh executor means a later
|
|
498
|
+
// [_awaitForTesting] drains only work the *current* configuration
|
|
499
|
+
// scheduled — it never blocks behind a stale task's retry backoff.
|
|
500
|
+
// Mirrors Identity._resetForTesting.
|
|
501
|
+
generation += 1
|
|
502
|
+
executor = newExecutor()
|
|
352
503
|
cached = null
|
|
353
504
|
appContext = null
|
|
354
505
|
serverUrl = ""
|
|
@@ -377,7 +528,8 @@ object Attribution {
|
|
|
377
528
|
|
|
378
529
|
@VisibleForTesting
|
|
379
530
|
internal fun _awaitForTesting() {
|
|
380
|
-
executor
|
|
531
|
+
val drainExecutor = executor
|
|
532
|
+
drainExecutor.submit {}.get()
|
|
381
533
|
}
|
|
382
534
|
|
|
383
535
|
// ---- Helpers -----------------------------------------------------------
|
|
@@ -42,7 +42,7 @@ internal object DeviceContextProvider {
|
|
|
42
42
|
* eventually inject this via `BuildConfig`, but for now keep it in
|
|
43
43
|
* source so we don't have to thread build settings through every test.
|
|
44
44
|
*/
|
|
45
|
-
const val SDK_VERSION: String = "0.
|
|
45
|
+
const val SDK_VERSION: String = "0.2.0"
|
|
46
46
|
|
|
47
47
|
private val cacheLock = Any()
|
|
48
48
|
|
|
@@ -378,6 +378,21 @@ object Identity {
|
|
|
378
378
|
}
|
|
379
379
|
}
|
|
380
380
|
|
|
381
|
+
/**
|
|
382
|
+
* Attach the entitlements repository after the fact.
|
|
383
|
+
*
|
|
384
|
+
* `SwissKnife.configureCore(...)` wires Identity with `null` (entitlements
|
|
385
|
+
* are a purchases concern); `SwissKnife.configurePurchases(...)` then calls
|
|
386
|
+
* this. Deliberately does NOT touch [explicitUserId] — re-running the full
|
|
387
|
+
* [configure] here would reset the identity back to the seed user and
|
|
388
|
+
* silently undo a `logIn(...)` that landed between the two halves.
|
|
389
|
+
*/
|
|
390
|
+
internal fun _attachEntitlementsRepository(repository: EntitlementsRepository?) {
|
|
391
|
+
synchronized(lock) {
|
|
392
|
+
this.entitlementsRepo = repository
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
381
396
|
/**
|
|
382
397
|
* Whether [configure] has run (an app context is wired). Used by the
|
|
383
398
|
* observability-only init path to avoid clobbering a real configuration
|