@kerdly/expo 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +235 -2
- package/android/build.gradle +61 -0
- package/android/src/main/AndroidManifest.xml +19 -0
- package/android/src/main/java/com/kerdly/expo/KerdlySdkModule.kt +326 -0
- package/android/vendor/kerdly-sdk/.kerdly-sync.json +8 -0
- package/android/vendor/kerdly-sdk/consumer-rules.pro +15 -0
- package/android/vendor/kerdly-sdk/kerdly-sdk.properties +2 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/Kerdly.kt +343 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/KerdlyTypes.kt +89 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Attribution.kt +39 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Batch.kt +159 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Core.kt +991 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/DeepLinks.kt +114 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/DeferredLinks.kt +111 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/DeviceContext.kt +137 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/EventQueue.kt +135 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Events.kt +314 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Json.kt +116 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Logger.kt +49 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Options.kt +47 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Ports.kt +83 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/PublicIp.kt +120 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Referrers.kt +137 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/RemoteConfig.kt +100 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/Storage.kt +271 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/android/AndroidPlatform.kt +274 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/android/AndroidRuntime.kt +187 -0
- package/android/vendor/kerdly-sdk/kotlin/com/kerdly/sdk/internal/android/PlayReferrerReader.kt +97 -0
- package/app.plugin.js +2 -0
- package/build/Kerdly.d.ts +106 -0
- package/build/Kerdly.d.ts.map +1 -0
- package/build/Kerdly.js +494 -0
- package/build/Kerdly.js.map +1 -0
- package/build/index.d.ts +4 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +2 -0
- package/build/index.js.map +1 -0
- package/build/log.d.ts +13 -0
- package/build/log.d.ts.map +1 -0
- package/build/log.js +45 -0
- package/build/log.js.map +1 -0
- package/build/native.d.ts +74 -0
- package/build/native.d.ts.map +1 -0
- package/build/native.js +56 -0
- package/build/native.js.map +1 -0
- package/build/normalize.d.ts +8 -0
- package/build/normalize.d.ts.map +1 -0
- package/build/normalize.js +86 -0
- package/build/normalize.js.map +1 -0
- package/build/types.d.ts +134 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +3 -0
- package/build/types.js.map +1 -0
- package/build/validation.d.ts +70 -0
- package/build/validation.d.ts.map +1 -0
- package/build/validation.js +349 -0
- package/build/validation.js.map +1 -0
- package/build/version.d.ts +4 -0
- package/build/version.d.ts.map +1 -0
- package/build/version.js +5 -0
- package/build/version.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/KerdlySDK/.kerdly-sync.json +7 -0
- package/ios/KerdlySDK/Core/Dependencies.swift +163 -0
- package/ios/KerdlySDK/Core/KerdlyCore+API.swift +117 -0
- package/ios/KerdlySDK/Core/KerdlyCore+Collectors.swift +410 -0
- package/ios/KerdlySDK/Core/KerdlyCore+DeferredLinks.swift +220 -0
- package/ios/KerdlySDK/Core/KerdlyCore+Delivery.swift +424 -0
- package/ios/KerdlySDK/Core/KerdlyCore.swift +557 -0
- package/ios/KerdlySDK/Core/Support.swift +95 -0
- package/ios/KerdlySDK/Logic/Backoff.swift +22 -0
- package/ios/KerdlySDK/Logic/Batch.swift +180 -0
- package/ios/KerdlySDK/Logic/Context.swift +85 -0
- package/ios/KerdlySDK/Logic/DeepLinks.swift +93 -0
- package/ios/KerdlySDK/Logic/DeferredLinks.swift +239 -0
- package/ios/KerdlySDK/Logic/Environment.swift +39 -0
- package/ios/KerdlySDK/Logic/EventQueue.swift +85 -0
- package/ios/KerdlySDK/Logic/EventValidator.swift +84 -0
- package/ios/KerdlySDK/Logic/Events.swift +102 -0
- package/ios/KerdlySDK/Logic/IPv6Address.swift +26 -0
- package/ios/KerdlySDK/Logic/InstallIdentity.swift +106 -0
- package/ios/KerdlySDK/Logic/JSONValue.swift +198 -0
- package/ios/KerdlySDK/Logic/Purchases.swift +84 -0
- package/ios/KerdlySDK/Logic/RemoteConfig.swift +185 -0
- package/ios/KerdlySDK/Logic/Skan.swift +343 -0
- package/ios/KerdlySDK/Logic/StandardEvents.swift +126 -0
- package/ios/KerdlySDK/Platform/AdSignals.swift +19 -0
- package/ios/KerdlySDK/Platform/DeferredLinkBundle.swift +25 -0
- package/ios/KerdlySDK/Platform/DeviceInfo.swift +82 -0
- package/ios/KerdlySDK/Platform/Lifecycle.swift +74 -0
- package/ios/KerdlySDK/Platform/PublicIp.swift +33 -0
- package/ios/KerdlySDK/Platform/SkanWriter.swift +70 -0
- package/ios/KerdlySDK/Platform/StoreKitAdapter.swift +185 -0
- package/ios/KerdlySDK/Platform/SystemStores.swift +200 -0
- package/ios/KerdlySDK/Platform/Tracking.swift +124 -0
- package/ios/KerdlySDK/Public/Kerdly.swift +202 -0
- package/ios/KerdlySDK/Public/KerdlyTypes.swift +130 -0
- package/ios/KerdlySDK/Resources/PrivacyInfo.xcprivacy +95 -0
- package/ios/KerdlySDK/Storage/PersistedState.swift +175 -0
- package/ios/KerdlySDK/Storage/StateStore.swift +36 -0
- package/ios/KerdlySdk.podspec +45 -0
- package/ios/KerdlySdkModule.swift +332 -0
- package/package.json +92 -4
- package/plugin/withKerdly.js +163 -0
- package/src/Kerdly.ts +524 -0
- package/src/index.ts +23 -0
- package/src/log.ts +49 -0
- package/src/native.ts +150 -0
- package/src/normalize.ts +82 -0
- package/src/types.ts +183 -0
- package/src/validation.ts +343 -0
- package/src/version.ts +5 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
package com.kerdly.sdk.internal
|
|
2
|
+
|
|
3
|
+
import com.kerdly.sdk.KerdlyDeepLink
|
|
4
|
+
import java.io.ByteArrayOutputStream
|
|
5
|
+
|
|
6
|
+
/** Deep-link parsing: the attribution params come from the query string,
|
|
7
|
+
* whatever the scheme, host or path. `myapp://onboarding?cpp=x` and the App
|
|
8
|
+
* Link `https://example.com/onboarding?cpp=x` parse the same. */
|
|
9
|
+
internal object DeepLinks {
|
|
10
|
+
const val CPP = "cpp"
|
|
11
|
+
const val PPID = "ppid"
|
|
12
|
+
const val CLICK_ID = "kerdly_c"
|
|
13
|
+
const val VISIT_ID = "kerdly_v"
|
|
14
|
+
/** The same two as links made before 0.1.0 spell them: read when the current name is absent. */
|
|
15
|
+
const val LEGACY_CLICK_ID = "afr_c"
|
|
16
|
+
const val LEGACY_VISIT_ID = "afr_v"
|
|
17
|
+
const val CAMPAIGN_IDS = "campaign_ids"
|
|
18
|
+
private val PARAMS = setOf(CPP, PPID, CLICK_ID, VISIT_ID, LEGACY_CLICK_ID, LEGACY_VISIT_ID, CAMPAIGN_IDS)
|
|
19
|
+
|
|
20
|
+
/** The collector's limit on `$deeplink.url`; longer URLs are cut (the params are parsed first). */
|
|
21
|
+
const val MAX_URL_CHARS = 4096
|
|
22
|
+
|
|
23
|
+
/** Null only for a blank string. */
|
|
24
|
+
fun parse(url: String): KerdlyDeepLink? {
|
|
25
|
+
val trimmed = url.trim()
|
|
26
|
+
if (trimmed.isEmpty()) return null
|
|
27
|
+
val params = query(trimmed)?.let(::parseQuery) ?: emptyMap()
|
|
28
|
+
return KerdlyDeepLink(
|
|
29
|
+
url = trimmed,
|
|
30
|
+
cpp = params[CPP],
|
|
31
|
+
ppid = params[PPID],
|
|
32
|
+
clickId = params[CLICK_ID] ?: params[LEGACY_CLICK_ID],
|
|
33
|
+
visitId = params[VISIT_ID] ?: params[LEGACY_VISIT_ID],
|
|
34
|
+
campaignIds = params[CAMPAIGN_IDS],
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A link carrying one of these is kept pending until the collector accepts it. */
|
|
39
|
+
fun isAttributionBearing(link: KerdlyDeepLink): Boolean =
|
|
40
|
+
link.cpp != null || link.ppid != null || link.clickId != null || link.visitId != null
|
|
41
|
+
|
|
42
|
+
/** The text between the first `?` and the fragment, or null when there is none. */
|
|
43
|
+
fun query(url: String): String? {
|
|
44
|
+
val hash = url.indexOf('#')
|
|
45
|
+
val beforeFragment = if (hash >= 0) url.substring(0, hash) else url
|
|
46
|
+
val qm = beforeFragment.indexOf('?')
|
|
47
|
+
return if (qm >= 0) beforeFragment.substring(qm + 1) else null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The known params of a query string. The first non-empty occurrence of a name wins; empty
|
|
51
|
+
* values are absent. Values are percent-decoded, but `+` stays `+`: Meta's `campaign_ids`
|
|
52
|
+
* is base64-like and must arrive "as given". */
|
|
53
|
+
fun parseQuery(query: String): Map<String, String> {
|
|
54
|
+
val out = HashMap<String, String>()
|
|
55
|
+
for (part in query.split('&')) {
|
|
56
|
+
if (part.isEmpty()) continue
|
|
57
|
+
val eq = part.indexOf('=')
|
|
58
|
+
val key = percentDecode(if (eq >= 0) part.substring(0, eq) else part)
|
|
59
|
+
if (key !in PARAMS || out.containsKey(key)) continue
|
|
60
|
+
val value = if (eq >= 0) percentDecode(part.substring(eq + 1)) else ""
|
|
61
|
+
if (value.isNotEmpty()) out[key] = value
|
|
62
|
+
}
|
|
63
|
+
return out
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** RFC 3986 percent-decoding into UTF-8. A malformed escape is kept literally. */
|
|
67
|
+
fun percentDecode(s: String): String {
|
|
68
|
+
if (s.indexOf('%') < 0) return s
|
|
69
|
+
val bytes = ByteArrayOutputStream(s.length)
|
|
70
|
+
var i = 0
|
|
71
|
+
while (i < s.length) {
|
|
72
|
+
val c = s[i]
|
|
73
|
+
if (c == '%' && i + 2 < s.length) {
|
|
74
|
+
val hi = Character.digit(s[i + 1], 16)
|
|
75
|
+
val lo = Character.digit(s[i + 2], 16)
|
|
76
|
+
if (hi >= 0 && lo >= 0) {
|
|
77
|
+
bytes.write(hi * 16 + lo)
|
|
78
|
+
i += 3
|
|
79
|
+
continue
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
val cp = s.codePointAt(i)
|
|
83
|
+
val chunk = String(Character.toChars(cp)).toByteArray(Charsets.UTF_8)
|
|
84
|
+
bytes.write(chunk, 0, chunk.size)
|
|
85
|
+
i += Character.charCount(cp)
|
|
86
|
+
}
|
|
87
|
+
return String(bytes.toByteArray(), Charsets.UTF_8)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** `source` of a `$deeplink`. A caller that knows it (the Expo bridge: React Native's initial URL
|
|
92
|
+
* vs its `url` events) passes it. Otherwise the first `handleUrl` of the process, before the app
|
|
93
|
+
* first went to the background, is the link that opened it (`initial`); every later one is
|
|
94
|
+
* `runtime`. */
|
|
95
|
+
internal class DeepLinkSources {
|
|
96
|
+
private var initialTaken = false
|
|
97
|
+
private var wentBackground = false
|
|
98
|
+
|
|
99
|
+
@Synchronized
|
|
100
|
+
fun onBackground() {
|
|
101
|
+
wentBackground = true
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@Synchronized
|
|
105
|
+
fun next(explicit: String? = null): String {
|
|
106
|
+
if (explicit == "initial" || explicit == "runtime") {
|
|
107
|
+
if (explicit == "initial") initialTaken = true
|
|
108
|
+
return explicit
|
|
109
|
+
}
|
|
110
|
+
if (initialTaken || wentBackground) return "runtime"
|
|
111
|
+
initialTaken = true
|
|
112
|
+
return "initial"
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
package com.kerdly.sdk.internal
|
|
2
|
+
|
|
3
|
+
import org.json.JSONObject
|
|
4
|
+
import java.text.SimpleDateFormat
|
|
5
|
+
import java.util.Date
|
|
6
|
+
import java.util.Locale
|
|
7
|
+
import java.util.TimeZone
|
|
8
|
+
|
|
9
|
+
/** The config's `ddl` block. Android asks TikTok only: for Meta, the Play
|
|
10
|
+
* referrer and Meta's Install Referrer already name the ad. The host is the SDK's own. */
|
|
11
|
+
internal class DdlConfig(
|
|
12
|
+
val enabled: Boolean,
|
|
13
|
+
val maxAgeMs: Long,
|
|
14
|
+
/** TikTok Events Manager's App ID; null = TikTok is not asked. */
|
|
15
|
+
val tiktokAppId: String?,
|
|
16
|
+
/** What the Business SDK sends as `app.id`: the package. */
|
|
17
|
+
val storeAppId: String?,
|
|
18
|
+
) {
|
|
19
|
+
companion object {
|
|
20
|
+
const val DEFAULT_MAX_AGE_H = 47.0
|
|
21
|
+
|
|
22
|
+
/** Null when the block is absent; ages are clamped to 1–168 h. */
|
|
23
|
+
fun fromJson(o: JSONObject?): DdlConfig? {
|
|
24
|
+
if (o == null) return null
|
|
25
|
+
val hours = (o.opt("max_age_h") as? Number)?.toDouble()?.takeIf { it.isFinite() } ?: DEFAULT_MAX_AGE_H
|
|
26
|
+
val tiktok = o.optJSONObject("tiktok")
|
|
27
|
+
return DdlConfig(
|
|
28
|
+
enabled = o.boolOrNull("enabled") ?: false,
|
|
29
|
+
maxAgeMs = (hours.coerceIn(1.0, 168.0) * 3_600_000).toLong(),
|
|
30
|
+
tiktokAppId = tiktok?.stringOrNull("app_id")?.takeIf { it.isNotEmpty() },
|
|
31
|
+
storeAppId = tiktok?.stringOrNull("store_app_id")?.takeIf { it.isNotEmpty() },
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** One network's answer to a deferred-deep-link request. */
|
|
38
|
+
internal class DdlAnswer(
|
|
39
|
+
val network: String,
|
|
40
|
+
/** `link` | `none` | `error`. */
|
|
41
|
+
val status: String,
|
|
42
|
+
val url: String?,
|
|
43
|
+
val httpStatus: Int?,
|
|
44
|
+
val detail: String?,
|
|
45
|
+
) {
|
|
46
|
+
/** No answer, a 429 or a 5xx: worth asking again. Any other answer, a refusal included, is final. */
|
|
47
|
+
val transient: Boolean
|
|
48
|
+
get() = status == "error" && (httpStatus == null || httpStatus == 429 || httpStatus >= 500)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** TikTok's deferred-deep-link request, field for field the Business SDK 1.7.0's
|
|
52
|
+
* (`TTRequestBuilder.ddlJson`): unsigned JSON with the GAID and the raw Play referrer, which
|
|
53
|
+
* TikTok decodes on its side. */
|
|
54
|
+
internal object TikTokDdl {
|
|
55
|
+
const val NETWORK = "tiktok"
|
|
56
|
+
const val URL = "https://analytics.us.tiktok.com/api/v1/app_sdk/ddl"
|
|
57
|
+
|
|
58
|
+
fun request(
|
|
59
|
+
tiktokAppId: String,
|
|
60
|
+
storeAppId: String?,
|
|
61
|
+
app: AppInfo,
|
|
62
|
+
device: DeviceContext,
|
|
63
|
+
gaid: String?,
|
|
64
|
+
referrer: String?,
|
|
65
|
+
userAgent: String?,
|
|
66
|
+
nowMs: Long,
|
|
67
|
+
): HttpRequest {
|
|
68
|
+
val appJson = JSONObject()
|
|
69
|
+
.put("namespace", app.bundle)
|
|
70
|
+
.putNullable("version", app.version)
|
|
71
|
+
.putNullable("build", app.build)
|
|
72
|
+
.put("tiktok_app_id", tiktokAppId)
|
|
73
|
+
.put("app_session_id", "")
|
|
74
|
+
if (storeAppId != null) appJson.put("id", storeAppId)
|
|
75
|
+
val deviceJson = JSONObject()
|
|
76
|
+
.put("platform", "Android")
|
|
77
|
+
.putNullable("os_version", device.os)
|
|
78
|
+
.putNullable("locale", device.languages.firstOrNull())
|
|
79
|
+
.putNullable("screen_width", device.screenW)
|
|
80
|
+
.putNullable("screen_height", device.screenH)
|
|
81
|
+
if (gaid != null) deviceJson.put("gaid", gaid)
|
|
82
|
+
val context = JSONObject().put("app", appJson).put("device", deviceJson).put("user", JSONObject())
|
|
83
|
+
if (!referrer.isNullOrEmpty()) context.put("ad", JSONObject().put("gp_referrer", referrer))
|
|
84
|
+
val body = JSONObject()
|
|
85
|
+
.put("tiktok_app_id", tiktokAppId)
|
|
86
|
+
.put("context", context)
|
|
87
|
+
.put("timestamp", iso8601(nowMs))
|
|
88
|
+
.put("ip", "")
|
|
89
|
+
if (userAgent != null) body.put("user_agent", userAgent)
|
|
90
|
+
return HttpRequest("POST", URL, mapOf("Content-Type" to "application/json"), body.toString().toByteArray(Charsets.UTF_8))
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** `{"code": 0, "data": {"ddl": "<url>"}}`; any other code is an error. */
|
|
94
|
+
fun parse(response: HttpResponse): DdlAnswer {
|
|
95
|
+
val o = parseObject(response.body)
|
|
96
|
+
val code = o?.intOrNull("code")
|
|
97
|
+
if (response.status != 200 || code != 0) {
|
|
98
|
+
val message = o?.stringOrNull("message")?.let { "${code ?: "?"} $it" }
|
|
99
|
+
return DdlAnswer(NETWORK, "error", null, response.status, message ?: response.body.take(300))
|
|
100
|
+
}
|
|
101
|
+
val link = o.optJSONObject("data")?.stringOrNull("ddl")?.takeIf { it.isNotEmpty() }
|
|
102
|
+
return if (link != null) DdlAnswer(NETWORK, "link", link, response.status, null)
|
|
103
|
+
else DdlAnswer(NETWORK, "none", null, response.status, response.body.take(300))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private fun iso8601(ms: Long): String {
|
|
107
|
+
val f = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US)
|
|
108
|
+
f.timeZone = TimeZone.getTimeZone("UTC")
|
|
109
|
+
return f.format(Date(ms))
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
package com.kerdly.sdk.internal
|
|
2
|
+
|
|
3
|
+
import org.json.JSONArray
|
|
4
|
+
import org.json.JSONObject
|
|
5
|
+
import java.util.Locale
|
|
6
|
+
import kotlin.math.max
|
|
7
|
+
import kotlin.math.min
|
|
8
|
+
import kotlin.math.roundToLong
|
|
9
|
+
|
|
10
|
+
/** Device facts for the batch `context`, as the platform reports them. */
|
|
11
|
+
internal class DeviceContext(
|
|
12
|
+
val os: String?,
|
|
13
|
+
val model: String?,
|
|
14
|
+
val manufacturer: String?,
|
|
15
|
+
/** `en_US` style (see [LocaleFormat.identifier]). */
|
|
16
|
+
val locale: String?,
|
|
17
|
+
/** BCP-47 tags, at most 5. */
|
|
18
|
+
val languages: List<String>,
|
|
19
|
+
val tz: String?,
|
|
20
|
+
val screenW: Int?,
|
|
21
|
+
val screenH: Int?,
|
|
22
|
+
val scale: Double?,
|
|
23
|
+
/** A Firebase Test Lab or Play pre-launch report device ([TestLab]); sent only when true. */
|
|
24
|
+
val testLab: Boolean = false,
|
|
25
|
+
) {
|
|
26
|
+
companion object {
|
|
27
|
+
/** A failed read: the snapshot still says `android`, `not_applicable` and the app version. */
|
|
28
|
+
val UNKNOWN = DeviceContext(null, null, null, null, emptyList(), null, null, null, null)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Firebase Test Lab, and the Play pre-launch report that runs on it, set the system setting
|
|
33
|
+
* `firebase.test.lab` to `"true"` on its devices. */
|
|
34
|
+
internal object TestLab {
|
|
35
|
+
const val SETTING = "firebase.test.lab"
|
|
36
|
+
|
|
37
|
+
/** [read] returns the setting (`Settings.System.getString`); anything but `"true"`, or a
|
|
38
|
+
* failed read, is not a test device. */
|
|
39
|
+
fun detect(read: () -> String?): Boolean = try {
|
|
40
|
+
read() == "true"
|
|
41
|
+
} catch (t: Throwable) {
|
|
42
|
+
false
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
internal object ContextJson {
|
|
47
|
+
// The collector rejects the whole batch (400) when a context string is over its limit, so
|
|
48
|
+
// device strings are cut to it here.
|
|
49
|
+
private const val MAX_DEVICE = 64
|
|
50
|
+
private const val MAX_APP = 32
|
|
51
|
+
|
|
52
|
+
/** The batch `context`. */
|
|
53
|
+
fun build(
|
|
54
|
+
device: DeviceContext,
|
|
55
|
+
analytics: Boolean,
|
|
56
|
+
ads: Boolean,
|
|
57
|
+
lat: Boolean?,
|
|
58
|
+
appVersion: String?,
|
|
59
|
+
appBuild: String?,
|
|
60
|
+
): JSONObject = fields(device, appVersion, appBuild) { o ->
|
|
61
|
+
o.put("consent", JSONObject().put("analytics", analytics).put("ads", ads))
|
|
62
|
+
if (lat != null) o.put("lat", lat)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The `$install` `device` snapshot: the context's device and app fields,
|
|
66
|
+
* built by the same code, without `consent` and `lat`. */
|
|
67
|
+
fun device(device: DeviceContext, appVersion: String?, appBuild: String?): JSONObject =
|
|
68
|
+
fields(device, appVersion, appBuild) {}
|
|
69
|
+
|
|
70
|
+
private inline fun fields(
|
|
71
|
+
device: DeviceContext,
|
|
72
|
+
appVersion: String?,
|
|
73
|
+
appBuild: String?,
|
|
74
|
+
beforeApp: (JSONObject) -> Unit,
|
|
75
|
+
): JSONObject {
|
|
76
|
+
val o = JSONObject()
|
|
77
|
+
.put("platform", "android")
|
|
78
|
+
.putNullable("os", device.os?.take(MAX_DEVICE))
|
|
79
|
+
.putNullable("model", device.model?.take(MAX_DEVICE))
|
|
80
|
+
.putNullable("manufacturer", device.manufacturer?.take(MAX_DEVICE))
|
|
81
|
+
.putNullable("locale", device.locale?.take(MAX_DEVICE))
|
|
82
|
+
.put("languages", JSONArray(device.languages.map { it.take(MAX_DEVICE) }.take(5)))
|
|
83
|
+
.putNullable("tz", device.tz?.take(MAX_DEVICE))
|
|
84
|
+
if (device.screenW != null && device.screenH != null) {
|
|
85
|
+
val screen = JSONObject().put("w", device.screenW).put("h", device.screenH)
|
|
86
|
+
device.scale?.let { screen.put("scale", it) }
|
|
87
|
+
o.put("screen", screen)
|
|
88
|
+
}
|
|
89
|
+
o.put("att", "not_applicable")
|
|
90
|
+
if (device.testLab) o.put("test_lab", true)
|
|
91
|
+
beforeApp(o)
|
|
92
|
+
o.putNullable("app_version", appVersion?.take(MAX_APP))
|
|
93
|
+
o.putNullable("app_build", appBuild?.take(MAX_APP))
|
|
94
|
+
return o
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Portrait pixels (rotation must not look like a context change) and the density, rounded. */
|
|
98
|
+
fun screen(widthPx: Int, heightPx: Int, density: Float): Triple<Int, Int, Double>? {
|
|
99
|
+
if (widthPx <= 0 || heightPx <= 0) return null
|
|
100
|
+
val scale = if (density > 0f && density.isFinite()) (density.toDouble() * 10_000).roundToLong() / 10_000.0 else 1.0
|
|
101
|
+
return Triple(min(widthPx, heightPx), max(widthPx, heightPx), scale)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
internal object LocaleFormat {
|
|
106
|
+
/** `Locale.current.identifier` style, as iOS reports it: `language[-Script][_REGION]`, e.g.
|
|
107
|
+
* `es-US` → `es_US`, `zh-Hant-TW` → `zh-Hant_TW`. Null for an undetermined locale. */
|
|
108
|
+
fun identifier(languageTag: String?): String? {
|
|
109
|
+
if (languageTag.isNullOrBlank()) return null
|
|
110
|
+
val parts = languageTag.trim().replace('_', '-').split('-').filter { it.isNotEmpty() }
|
|
111
|
+
if (parts.isEmpty()) return null
|
|
112
|
+
val language = parts[0].lowercase(Locale.ROOT)
|
|
113
|
+
if (language == "und" || !language.all { it in 'a'..'z' }) return null
|
|
114
|
+
var i = 1
|
|
115
|
+
var script: String? = null
|
|
116
|
+
var region: String? = null
|
|
117
|
+
if (i < parts.size && parts[i].length == 4 && parts[i].all { it.isLetter() }) {
|
|
118
|
+
script = parts[i].lowercase(Locale.ROOT).replaceFirstChar { it.titlecase(Locale.ROOT) }
|
|
119
|
+
i++
|
|
120
|
+
}
|
|
121
|
+
if (i < parts.size) {
|
|
122
|
+
val p = parts[i]
|
|
123
|
+
if ((p.length == 2 && p.all { it.isLetter() }) || (p.length == 3 && p.all { it.isDigit() })) {
|
|
124
|
+
region = p.uppercase(Locale.ROOT)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return buildString {
|
|
128
|
+
append(language)
|
|
129
|
+
if (script != null) append('-').append(script)
|
|
130
|
+
if (region != null) append('_').append(region)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Preferred languages as BCP-47 tags, deduplicated, at most 5. */
|
|
135
|
+
fun languages(tags: List<String>): List<String> =
|
|
136
|
+
tags.map { it.trim() }.filter { it.isNotEmpty() && it != "und" }.distinct().take(5)
|
|
137
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
package com.kerdly.sdk.internal
|
|
2
|
+
|
|
3
|
+
import org.json.JSONObject
|
|
4
|
+
|
|
5
|
+
/** One event as it goes on the wire. [json] is the serialized envelope, built
|
|
6
|
+
* once at enqueue and reused for every retry, so the id and payload never change. */
|
|
7
|
+
internal class QueuedEvent(val id: String, val name: String, val seq: Long, val json: String) {
|
|
8
|
+
val bytes: Int = utf8Length(json)
|
|
9
|
+
val reserved: Boolean get() = EventNames.isReserved(name)
|
|
10
|
+
|
|
11
|
+
companion object {
|
|
12
|
+
fun fromJson(o: JSONObject): QueuedEvent? {
|
|
13
|
+
val id = o.stringOrNull("id") ?: return null
|
|
14
|
+
val name = o.stringOrNull("name") ?: return null
|
|
15
|
+
val seq = o.longOrNull("seq") ?: return null
|
|
16
|
+
return QueuedEvent(id, name, seq, o.toString())
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The persistent queue, kept in `seq` order.
|
|
22
|
+
*
|
|
23
|
+
* Capacity: at [softMaxEvents] events or [softMaxBytes] the oldest custom event goes; reserved
|
|
24
|
+
* events are never dropped for it. [hardMaxEvents] / [hardMaxBytes] only guard against an
|
|
25
|
+
* endless pile of reserved events (a collector unreachable for months): past them the oldest
|
|
26
|
+
* `$open` goes first, then the oldest event other than `$install`. */
|
|
27
|
+
internal class EventQueue(
|
|
28
|
+
private val softMaxEvents: Int = 1_000,
|
|
29
|
+
private val softMaxBytes: Long = 1_000_000,
|
|
30
|
+
private val hardMaxEvents: Int = 5_000,
|
|
31
|
+
private val hardMaxBytes: Long = 5_000_000,
|
|
32
|
+
) {
|
|
33
|
+
private val list = ArrayList<QueuedEvent>()
|
|
34
|
+
private val ids = HashSet<String>()
|
|
35
|
+
|
|
36
|
+
/** The next `seq` to hand out; never reused within an install. */
|
|
37
|
+
var nextSeq: Long = 1
|
|
38
|
+
|
|
39
|
+
var bytes: Long = 0
|
|
40
|
+
private set
|
|
41
|
+
|
|
42
|
+
val size: Int get() = list.size
|
|
43
|
+
|
|
44
|
+
fun isEmpty(): Boolean = list.isEmpty()
|
|
45
|
+
|
|
46
|
+
fun events(): List<QueuedEvent> = list
|
|
47
|
+
|
|
48
|
+
fun contains(id: String): Boolean = id in ids
|
|
49
|
+
|
|
50
|
+
fun maxSeq(): Long = list.maxOfOrNull { it.seq } ?: 0
|
|
51
|
+
|
|
52
|
+
/** Inserts in `seq` order and returns what the capacity rules dropped (possibly [event]). */
|
|
53
|
+
fun add(event: QueuedEvent): List<QueuedEvent> {
|
|
54
|
+
if (!ids.add(event.id)) return emptyList()
|
|
55
|
+
var i = list.size
|
|
56
|
+
while (i > 0 && list[i - 1].seq > event.seq) i--
|
|
57
|
+
list.add(i, event)
|
|
58
|
+
bytes += event.bytes
|
|
59
|
+
if (event.seq >= nextSeq) nextSeq = event.seq + 1
|
|
60
|
+
return trim()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Removes these ids; returns how many were queued. */
|
|
64
|
+
fun remove(toRemove: Collection<String>): Int {
|
|
65
|
+
if (toRemove.isEmpty()) return 0
|
|
66
|
+
val set = toRemove as? Set<String> ?: toRemove.toHashSet()
|
|
67
|
+
var removed = 0
|
|
68
|
+
val it = list.iterator()
|
|
69
|
+
while (it.hasNext()) {
|
|
70
|
+
val e = it.next()
|
|
71
|
+
if (e.id in set) {
|
|
72
|
+
it.remove()
|
|
73
|
+
ids.remove(e.id)
|
|
74
|
+
bytes -= e.bytes
|
|
75
|
+
removed++
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return removed
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private fun removeAt(index: Int): QueuedEvent {
|
|
82
|
+
val e = list.removeAt(index)
|
|
83
|
+
ids.remove(e.id)
|
|
84
|
+
bytes -= e.bytes
|
|
85
|
+
return e
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private fun trim(): List<QueuedEvent> {
|
|
89
|
+
var dropped: MutableList<QueuedEvent>? = null
|
|
90
|
+
while (list.size > softMaxEvents || bytes > softMaxBytes) {
|
|
91
|
+
val i = list.indexOfFirst { !it.reserved }
|
|
92
|
+
if (i < 0) break
|
|
93
|
+
(dropped ?: ArrayList<QueuedEvent>().also { dropped = it }).add(removeAt(i))
|
|
94
|
+
}
|
|
95
|
+
while (list.size > hardMaxEvents || bytes > hardMaxBytes) {
|
|
96
|
+
var i = list.indexOfFirst { it.name == EventNames.OPEN }
|
|
97
|
+
if (i < 0) i = list.indexOfFirst { it.name != EventNames.INSTALL }
|
|
98
|
+
if (i < 0) break
|
|
99
|
+
(dropped ?: ArrayList<QueuedEvent>().also { dropped = it }).add(removeAt(i))
|
|
100
|
+
}
|
|
101
|
+
return dropped ?: emptyList()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** `{"v":1,"install_id":…,"next_seq":…,"events":[…]}`; the event JSON is spliced in as built. */
|
|
105
|
+
fun serialize(installId: String): String {
|
|
106
|
+
val sb = StringBuilder(bytes.toInt().coerceAtMost(Int.MAX_VALUE - 256) + 128)
|
|
107
|
+
sb.append("{\"v\":1,\"install_id\":").append(JSONObject.quote(installId))
|
|
108
|
+
sb.append(",\"next_seq\":").append(nextSeq)
|
|
109
|
+
sb.append(",\"events\":[")
|
|
110
|
+
list.forEachIndexed { i, e ->
|
|
111
|
+
if (i > 0) sb.append(',')
|
|
112
|
+
sb.append(e.json)
|
|
113
|
+
}
|
|
114
|
+
sb.append("]}")
|
|
115
|
+
return sb.toString()
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
companion object {
|
|
119
|
+
/** Null when [text] is not a queue of [installId] (absent, unreadable, or another install's). */
|
|
120
|
+
fun parse(text: String?, installId: String): EventQueue? {
|
|
121
|
+
val o = parseObject(text) ?: return null
|
|
122
|
+
if (o.stringOrNull("install_id") != installId) return null
|
|
123
|
+
val queue = EventQueue()
|
|
124
|
+
val events = o.optJSONArray("events")
|
|
125
|
+
if (events != null) {
|
|
126
|
+
for (i in 0 until events.length()) {
|
|
127
|
+
val e = events.optJSONObject(i) ?: continue
|
|
128
|
+
QueuedEvent.fromJson(e)?.let { queue.add(it) }
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
queue.nextSeq = maxOf(o.longOrNull("next_seq") ?: 1, queue.maxSeq() + 1, 1)
|
|
132
|
+
return queue
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|