@adshift/react-native-plugin 3.0.0 → 3.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.
Files changed (176) hide show
  1. package/AdShiftReactNativePlugin.podspec +20 -4
  2. package/CHANGELOG.md +30 -17
  3. package/README.md +55 -47
  4. package/android/build.gradle +2 -3
  5. package/android/consumer-rules.pro +2 -21
  6. package/android/src/main/AndroidManifest.xml +1 -1
  7. package/android/src/main/java/com/adshift/reactnativeplugin/ActivityIntents.kt +307 -0
  8. package/android/src/main/java/com/adshift/reactnativeplugin/AdShiftReactNativePluginModule.kt +83 -157
  9. package/android/src/test/java/com/adshift/reactnativeplugin/ConsentSnapshotTest.kt +0 -20
  10. package/android/src/test/java/com/adshift/reactnativeplugin/DeepLinkDeliveryTest.kt +0 -4
  11. package/android/src/test/java/com/adshift/reactnativeplugin/InitializeOrderingTest.kt +37 -5
  12. package/android/src/test/java/com/adshift/reactnativeplugin/IntentLinkDedupTest.kt +0 -5
  13. package/android/src/test/java/com/adshift/reactnativeplugin/LaunchIntentForwardingTest.kt +7 -26
  14. package/android/src/test/java/com/adshift/reactnativeplugin/LaunchLinkRecordTest.kt +420 -0
  15. package/android/src/test/java/com/adshift/reactnativeplugin/ModuleTestSupport.kt +93 -34
  16. package/android/src/test/java/com/adshift/reactnativeplugin/StartVerdictTest.kt +0 -4
  17. package/ios/AdShiftReactNativePlugin.h +0 -12
  18. package/ios/AdShiftReactNativePlugin.mm +2 -14
  19. package/ios/AdShiftReactNativePluginHelper.swift +14 -73
  20. package/ios/Core/BridgeRules.swift +24 -29
  21. package/lib/commonjs/Adshift.js +78 -285
  22. package/lib/commonjs/Adshift.js.map +1 -1
  23. package/lib/commonjs/NativeAdShiftReactNativePlugin.js +2 -11
  24. package/lib/commonjs/NativeAdShiftReactNativePlugin.js.map +1 -1
  25. package/lib/commonjs/index.js.map +1 -1
  26. package/lib/commonjs/jest/index.js +4 -18
  27. package/lib/commonjs/jest/index.js.map +1 -1
  28. package/lib/commonjs/nativeSdkVersions.js +4 -6
  29. package/lib/commonjs/nativeSdkVersions.js.map +1 -1
  30. package/lib/commonjs/types/AdshiftAdRevenue.js +0 -24
  31. package/lib/commonjs/types/AdshiftAdRevenue.js.map +1 -1
  32. package/lib/commonjs/types/AdshiftConfig.js +3 -24
  33. package/lib/commonjs/types/AdshiftConfig.js.map +1 -1
  34. package/lib/commonjs/types/AdshiftConsent.js +9 -45
  35. package/lib/commonjs/types/AdshiftConsent.js.map +1 -1
  36. package/lib/commonjs/types/AdshiftConsentSnapshot.js +5 -24
  37. package/lib/commonjs/types/AdshiftConsentSnapshot.js.map +1 -1
  38. package/lib/commonjs/types/AdshiftDeepLink.js +3 -30
  39. package/lib/commonjs/types/AdshiftDeepLink.js.map +1 -1
  40. package/lib/commonjs/types/AdshiftError.js +7 -27
  41. package/lib/commonjs/types/AdshiftError.js.map +1 -1
  42. package/lib/commonjs/types/AdshiftEventParam.js +4 -38
  43. package/lib/commonjs/types/AdshiftEventParam.js.map +1 -1
  44. package/lib/commonjs/types/AdshiftEventType.js +1 -39
  45. package/lib/commonjs/types/AdshiftEventType.js.map +1 -1
  46. package/lib/commonjs/types/AdshiftThirdPartySharing.js +7 -38
  47. package/lib/commonjs/types/AdshiftThirdPartySharing.js.map +1 -1
  48. package/lib/commonjs/version.js +1 -1
  49. package/lib/module/Adshift.js +78 -285
  50. package/lib/module/Adshift.js.map +1 -1
  51. package/lib/module/NativeAdShiftReactNativePlugin.js +2 -12
  52. package/lib/module/NativeAdShiftReactNativePlugin.js.map +1 -1
  53. package/lib/module/index.js +1 -46
  54. package/lib/module/index.js.map +1 -1
  55. package/lib/module/jest/index.js +4 -18
  56. package/lib/module/jest/index.js.map +1 -1
  57. package/lib/module/nativeSdkVersions.js +4 -6
  58. package/lib/module/nativeSdkVersions.js.map +1 -1
  59. package/lib/module/types/AdshiftAdRevenue.js +0 -24
  60. package/lib/module/types/AdshiftAdRevenue.js.map +1 -1
  61. package/lib/module/types/AdshiftConfig.js +3 -24
  62. package/lib/module/types/AdshiftConfig.js.map +1 -1
  63. package/lib/module/types/AdshiftConsent.js +9 -45
  64. package/lib/module/types/AdshiftConsent.js.map +1 -1
  65. package/lib/module/types/AdshiftConsentSnapshot.js +5 -24
  66. package/lib/module/types/AdshiftConsentSnapshot.js.map +1 -1
  67. package/lib/module/types/AdshiftDeepLink.js +3 -31
  68. package/lib/module/types/AdshiftDeepLink.js.map +1 -1
  69. package/lib/module/types/AdshiftError.js +7 -28
  70. package/lib/module/types/AdshiftError.js.map +1 -1
  71. package/lib/module/types/AdshiftEventParam.js +4 -38
  72. package/lib/module/types/AdshiftEventParam.js.map +1 -1
  73. package/lib/module/types/AdshiftEventType.js +1 -39
  74. package/lib/module/types/AdshiftEventType.js.map +1 -1
  75. package/lib/module/types/AdshiftThirdPartySharing.js +7 -38
  76. package/lib/module/types/AdshiftThirdPartySharing.js.map +1 -1
  77. package/lib/module/version.js +1 -1
  78. package/lib/typescript/commonjs/src/Adshift.d.ts +61 -185
  79. package/lib/typescript/commonjs/src/Adshift.d.ts.map +1 -1
  80. package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts +10 -36
  81. package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -1
  82. package/lib/typescript/commonjs/src/index.d.ts +0 -32
  83. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  84. package/lib/typescript/commonjs/src/jest/index.d.ts +6 -20
  85. package/lib/typescript/commonjs/src/jest/index.d.ts.map +1 -1
  86. package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts +4 -6
  87. package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts.map +1 -1
  88. package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts +0 -24
  89. package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts.map +1 -1
  90. package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts +10 -37
  91. package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts.map +1 -1
  92. package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts +9 -49
  93. package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts.map +1 -1
  94. package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts +10 -28
  95. package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts.map +1 -1
  96. package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts +6 -34
  97. package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts.map +1 -1
  98. package/lib/typescript/commonjs/src/types/AdshiftError.d.ts +7 -26
  99. package/lib/typescript/commonjs/src/types/AdshiftError.d.ts.map +1 -1
  100. package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts +3 -19
  101. package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts.map +1 -1
  102. package/lib/typescript/commonjs/src/types/AdshiftEventType.d.ts +1 -23
  103. package/lib/typescript/commonjs/src/types/AdshiftEventType.d.ts.map +1 -1
  104. package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts +7 -38
  105. package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts.map +1 -1
  106. package/lib/typescript/commonjs/src/version.d.ts +1 -1
  107. package/lib/typescript/module/src/Adshift.d.ts +61 -185
  108. package/lib/typescript/module/src/Adshift.d.ts.map +1 -1
  109. package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts +10 -36
  110. package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -1
  111. package/lib/typescript/module/src/index.d.ts +0 -32
  112. package/lib/typescript/module/src/index.d.ts.map +1 -1
  113. package/lib/typescript/module/src/jest/index.d.ts +6 -20
  114. package/lib/typescript/module/src/jest/index.d.ts.map +1 -1
  115. package/lib/typescript/module/src/nativeSdkVersions.d.ts +4 -6
  116. package/lib/typescript/module/src/nativeSdkVersions.d.ts.map +1 -1
  117. package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts +0 -24
  118. package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts.map +1 -1
  119. package/lib/typescript/module/src/types/AdshiftConfig.d.ts +10 -37
  120. package/lib/typescript/module/src/types/AdshiftConfig.d.ts.map +1 -1
  121. package/lib/typescript/module/src/types/AdshiftConsent.d.ts +9 -49
  122. package/lib/typescript/module/src/types/AdshiftConsent.d.ts.map +1 -1
  123. package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts +10 -28
  124. package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts.map +1 -1
  125. package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts +6 -34
  126. package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts.map +1 -1
  127. package/lib/typescript/module/src/types/AdshiftError.d.ts +7 -26
  128. package/lib/typescript/module/src/types/AdshiftError.d.ts.map +1 -1
  129. package/lib/typescript/module/src/types/AdshiftEventParam.d.ts +3 -19
  130. package/lib/typescript/module/src/types/AdshiftEventParam.d.ts.map +1 -1
  131. package/lib/typescript/module/src/types/AdshiftEventType.d.ts +1 -23
  132. package/lib/typescript/module/src/types/AdshiftEventType.d.ts.map +1 -1
  133. package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts +7 -38
  134. package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts.map +1 -1
  135. package/lib/typescript/module/src/version.d.ts +1 -1
  136. package/package.json +11 -1
  137. package/src/Adshift.ts +82 -286
  138. package/src/NativeAdShiftReactNativePlugin.ts +10 -48
  139. package/src/index.tsx +1 -44
  140. package/src/jest/index.ts +6 -20
  141. package/src/nativeSdkVersions.ts +4 -6
  142. package/src/types/AdshiftAdRevenue.ts +0 -24
  143. package/src/types/AdshiftConfig.ts +11 -54
  144. package/src/types/AdshiftConsent.ts +9 -49
  145. package/src/types/AdshiftConsentSnapshot.ts +11 -38
  146. package/src/types/AdshiftDeepLink.ts +6 -34
  147. package/src/types/AdshiftError.ts +7 -26
  148. package/src/types/AdshiftEventParam.ts +4 -38
  149. package/src/types/AdshiftEventType.ts +1 -39
  150. package/src/types/AdshiftThirdPartySharing.ts +7 -38
  151. package/src/version.ts +1 -1
  152. package/vendor/ios/AdshiftSDK.files.json +30 -0
  153. package/vendor/ios/AdshiftSDK.xcframework/Info.plist +44 -0
  154. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/AdshiftSDK +0 -0
  155. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Headers/AdshiftSDK-Swift.h +315 -0
  156. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Info.plist +0 -0
  157. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.abi.json +18729 -0
  158. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.private.swiftinterface +414 -0
  159. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
  160. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.swiftinterface +414 -0
  161. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/module.modulemap +4 -0
  162. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/PrivacyInfo.xcprivacy +132 -0
  163. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/AdshiftSDK +0 -0
  164. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Headers/AdshiftSDK-Swift.h +626 -0
  165. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Info.plist +0 -0
  166. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.abi.json +18729 -0
  167. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +414 -0
  168. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
  169. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.swiftinterface +414 -0
  170. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.abi.json +18729 -0
  171. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +414 -0
  172. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
  173. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +414 -0
  174. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/module.modulemap +4 -0
  175. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/PrivacyInfo.xcprivacy +132 -0
  176. package/android/src/main/java/com/adshift/reactnativeplugin/RestoredIntents.kt +0 -64
@@ -0,0 +1,307 @@
1
+ package com.adshift.reactnativeplugin
2
+
3
+ import android.app.Activity
4
+ import android.app.ActivityManager
5
+ import android.app.Application
6
+ import android.content.Context
7
+ import android.content.Intent
8
+ import android.os.Build
9
+ import android.os.Bundle
10
+ import android.os.SystemClock
11
+ import android.provider.Settings
12
+ import android.util.AtomicFile
13
+ import android.util.Log
14
+ import androidx.startup.Initializer
15
+ import java.io.File
16
+ import java.security.MessageDigest
17
+ import java.util.Collections
18
+ import java.util.WeakHashMap
19
+ import kotlin.math.abs
20
+ import org.json.JSONArray
21
+ import org.json.JSONObject
22
+
23
+ /**
24
+ * Only the Intent an Activity was created with comes back when the system re-creates it; one
25
+ * handed over later through `onNewIntent` or `setIntent` is always a new tap.
26
+ */
27
+ internal object ActivityIntents : Application.ActivityLifecycleCallbacks {
28
+
29
+ enum class Restored { YES, NO, UNKNOWN }
30
+
31
+ /** The keys are taken at creation, so later additions to the Intent do not change them. */
32
+ class Creation(
33
+ val intent: Intent,
34
+ val restored: Restored,
35
+ val taskId: Int,
36
+ val taskStamp: Long? = null,
37
+ val intentKey: String? = null,
38
+ val urlKey: String? = null,
39
+ ) {
40
+ /** False only when the system is known to have started the Activity afresh. */
41
+ val mayBeRestored: Boolean get() = restored != Restored.NO
42
+ }
43
+
44
+ private val lock = Any()
45
+
46
+ // Guarded by [lock]. Weak keys, so finished Activities and their Intents can go.
47
+ private val byActivity = WeakHashMap<Activity, Creation>()
48
+ private val byIntent = WeakHashMap<Intent, Creation>()
49
+ private val newIntentReceived: MutableSet<Activity> =
50
+ Collections.newSetFromMap(WeakHashMap<Activity, Boolean>())
51
+
52
+ private var application: Application? = null
53
+
54
+ @Synchronized
55
+ fun register(context: Context) {
56
+ val app = context.applicationContext as? Application ?: return
57
+ if (application === app) return
58
+ application = app
59
+ LaunchRecords.open(app)
60
+ app.registerActivityLifecycleCallbacks(this)
61
+ }
62
+
63
+ /** The creation [intent] opened, or null for an Intent the Activity was handed later. */
64
+ fun creationOf(intent: Intent): Creation? = synchronized(lock) { byIntent[intent] }
65
+
66
+ /** For an Activity created before registration: it counts as possibly re-created. */
67
+ fun adopt(activity: Activity) {
68
+ synchronized(lock) {
69
+ if (activity in byActivity) return
70
+ }
71
+ record(activity, Restored.UNKNOWN)
72
+ }
73
+
74
+ fun noteNewIntent(activity: Activity) {
75
+ synchronized(lock) { newIntentReceived += activity }
76
+ }
77
+
78
+ fun hasReceivedNewIntent(activity: Activity): Boolean =
79
+ synchronized(lock) { activity in newIntentReceived }
80
+
81
+ /** Runs inside the app's Activity creation, so nothing here may throw. */
82
+ private fun record(activity: Activity, restored: Restored) {
83
+ try {
84
+ val intent = activity.intent ?: return
85
+ val taskId = runCatching { activity.taskId }.getOrDefault(-1)
86
+ val creation = Creation(
87
+ intent = intent,
88
+ restored = restored,
89
+ taskId = taskId,
90
+ taskStamp = taskStampOf(taskId),
91
+ intentKey = LaunchRecords.intentKeyOf(intent),
92
+ urlKey = runCatching { intent.dataString }.getOrNull()?.let(LaunchRecords::urlKey),
93
+ )
94
+ synchronized(lock) {
95
+ byActivity[activity] = creation
96
+ byIntent[intent] = creation
97
+ }
98
+ } catch (e: Exception) {
99
+ Log.d("AdShiftRN", "activity not recorded: ${e.javaClass.simpleName}")
100
+ }
101
+ }
102
+
103
+ /** Below API 26 only: Android 7 reuses a removed task's number for the next new task. */
104
+ private fun taskStampOf(taskId: Int): Long? {
105
+ if (Build.VERSION.SDK_INT >= 26 || taskId < 0) return null
106
+ return runCatching {
107
+ val manager = application?.getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager
108
+ ?: return null
109
+ @Suppress("DEPRECATION")
110
+ val info = manager.appTasks.map { it.taskInfo }.firstOrNull { it.persistentId == taskId }
111
+ ?: return null
112
+ ActivityManager.RecentTaskInfo::class.java.getDeclaredField("firstActiveTime")
113
+ .apply { isAccessible = true }
114
+ .getLong(info)
115
+ .takeIf { it > 0 }
116
+ }.getOrNull()
117
+ }
118
+
119
+ // API 29+: the state the system kept, whatever the app passes to super.onCreate.
120
+ override fun onActivityPreCreated(activity: Activity, savedInstanceState: Bundle?) =
121
+ record(activity, if (savedInstanceState != null) Restored.YES else Restored.NO)
122
+
123
+ // Below API 29: a null state may also be an app that calls super.onCreate(null).
124
+ override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
125
+ synchronized(lock) {
126
+ if (activity in byActivity) return
127
+ }
128
+ record(activity, if (savedInstanceState != null) Restored.YES else Restored.UNKNOWN)
129
+ }
130
+
131
+ override fun onActivityDestroyed(activity: Activity) {
132
+ val creation = synchronized(lock) {
133
+ newIntentReceived -= activity
134
+ byActivity.remove(activity)
135
+ } ?: return
136
+ // Only finishing is for good: a rotation or a reclaimed Activity comes back re-created.
137
+ if (activity.isFinishing) LaunchRecords.forget(creation)
138
+ }
139
+
140
+ override fun onActivityStarted(activity: Activity) = Unit
141
+ override fun onActivityResumed(activity: Activity) = Unit
142
+ override fun onActivityPaused(activity: Activity) = Unit
143
+ override fun onActivityStopped(activity: Activity) = Unit
144
+ override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit
145
+ }
146
+
147
+ /**
148
+ * Digests of links already passed to the SDK, per task number; no link is stored. Valid only for
149
+ * the boot that wrote them, because task numbers start over when the phone restarts.
150
+ */
151
+ internal object LaunchRecords {
152
+
153
+ private const val TAG = "AdShiftRN"
154
+ private const val FILE_NAME = "adshift_rn_launch_links.json"
155
+ private const val LIMIT = 16
156
+ private const val BOOT_TIME_TOLERANCE_MS = 60_000L
157
+ private const val META_APP_LINK_EXTRA = "al_applink_data"
158
+
159
+ private class Entry(val taskId: Int, val key: String, val taskStamp: Long?) {
160
+ fun matches(creation: ActivityIntents.Creation, key: String): Boolean =
161
+ taskId == creation.taskId && this.key == key &&
162
+ (taskStamp == null || creation.taskStamp == null || taskStamp == creation.taskStamp)
163
+ }
164
+
165
+ private var file: AtomicFile? = null
166
+ private var bootCount = -1
167
+ private var bootTimeMs = 0L
168
+
169
+ private var entries: ArrayDeque<Entry>? = null
170
+
171
+ @Synchronized
172
+ fun open(context: Context) {
173
+ if (file != null) return
174
+ file = AtomicFile(File(context.noBackupFilesDir, FILE_NAME))
175
+ bootCount = runCatching {
176
+ Settings.Global.getInt(context.contentResolver, Settings.Global.BOOT_COUNT, -1)
177
+ }.getOrDefault(-1)
178
+ bootTimeMs = System.currentTimeMillis() - SystemClock.elapsedRealtime()
179
+ }
180
+
181
+ @Synchronized
182
+ fun contains(creation: ActivityIntents.Creation, key: String): Boolean =
183
+ load().any { it.matches(creation, key) }
184
+
185
+ /** Writes only when the record changes: the write is synchronous, on the main thread. */
186
+ @Synchronized
187
+ fun add(creation: ActivityIntents.Creation, key: String) {
188
+ val list = load()
189
+ if (list.any { it.taskId == creation.taskId && it.key == key && it.taskStamp == creation.taskStamp }) {
190
+ return
191
+ }
192
+ list.removeAll { it.taskId == creation.taskId && it.key == key }
193
+ list.addLast(Entry(creation.taskId, key, creation.taskStamp))
194
+ while (list.size > LIMIT) list.removeFirst()
195
+ save(list)
196
+ }
197
+
198
+ @Synchronized
199
+ fun forget(creation: ActivityIntents.Creation) {
200
+ val keys = setOfNotNull(creation.intentKey, creation.urlKey)
201
+ if (keys.isEmpty()) return
202
+ val list = load()
203
+ if (list.removeAll { it.taskId == creation.taskId && it.key in keys }) save(list)
204
+ }
205
+
206
+ /** Null also when the extras cannot be read: below Android 13 an unknown class throws. */
207
+ fun intentKeyOf(intent: Intent): String? = runCatching {
208
+ if (intent.data == null && intent.extras?.isEmpty != false) null else intentKey(intent)
209
+ }.getOrNull()
210
+
211
+ private fun intentKey(intent: Intent): String = "i:" + sha256(
212
+ "${intent.dataString ?: ""}|${appLinkTarget(intent)}|${extrasFingerprint(intent)}"
213
+ )
214
+
215
+ fun urlKey(url: String): String = "u:" + sha256(url)
216
+
217
+ private fun appLinkTarget(intent: Intent): String = runCatching {
218
+ @Suppress("DEPRECATION")
219
+ when (val extra = intent.extras?.get(META_APP_LINK_EXTRA)) {
220
+ is String -> extra
221
+ is Bundle -> extra.getString("target_url") ?: ""
222
+ else -> ""
223
+ }
224
+ }.getOrDefault("")
225
+
226
+ /** Only values whose text survives re-parcelling, so a re-created Activity's Intent matches. */
227
+ private fun extrasFingerprint(intent: Intent): String = runCatching {
228
+ val extras = intent.extras ?: return@runCatching ""
229
+ extras.keySet()
230
+ .filter { it != META_APP_LINK_EXTRA }
231
+ .sorted()
232
+ .mapNotNull { key ->
233
+ @Suppress("DEPRECATION")
234
+ when (val value = extras.get(key)) {
235
+ is CharSequence, is Boolean, is Int, is Long, is Float, is Double -> "$key=$value"
236
+ else -> null
237
+ }
238
+ }
239
+ .joinToString("&")
240
+ }.getOrDefault("")
241
+
242
+ private fun sha256(text: String): String =
243
+ MessageDigest.getInstance("SHA-256").digest(text.toByteArray())
244
+ .joinToString("") { "%02x".format(it) }
245
+
246
+ private fun load(): ArrayDeque<Entry> {
247
+ entries?.let { return it }
248
+ val loaded = ArrayDeque<Entry>()
249
+ runCatching {
250
+ val stored = file?.takeIf { it.baseFile.exists() }?.readFully() ?: return@runCatching
251
+ val json = JSONObject(String(stored))
252
+ if (!sameBoot(json.optInt("boot_count", -1), json.optLong("boot_time_ms", 0L))) {
253
+ return@runCatching
254
+ }
255
+ val list = json.optJSONArray("entries") ?: return@runCatching
256
+ for (i in 0 until list.length()) {
257
+ val item = list.optJSONObject(i) ?: continue
258
+ val key = item.optString("key").takeIf { it.isNotEmpty() } ?: continue
259
+ val stamp = if (item.has("task_stamp")) item.optLong("task_stamp") else null
260
+ loaded.addLast(Entry(item.optInt("task_id", -1), key, stamp))
261
+ }
262
+ }.onFailure { Log.d(TAG, "launch links unreadable: ${it.javaClass.simpleName}") }
263
+ entries = loaded
264
+ return loaded
265
+ }
266
+
267
+ private fun sameBoot(storedCount: Int, storedTimeMs: Long): Boolean =
268
+ if (bootCount >= 0 && storedCount >= 0) {
269
+ bootCount == storedCount
270
+ } else {
271
+ abs(bootTimeMs - storedTimeMs) < BOOT_TIME_TOLERANCE_MS
272
+ }
273
+
274
+ private fun save(list: ArrayDeque<Entry>) {
275
+ val target = file ?: return
276
+ val json = JSONObject()
277
+ .put("boot_count", bootCount)
278
+ .put("boot_time_ms", bootTimeMs)
279
+ .put("entries", JSONArray().apply {
280
+ list.forEach { entry ->
281
+ put(
282
+ JSONObject().put("task_id", entry.taskId).put("key", entry.key)
283
+ .apply { entry.taskStamp?.let { put("task_stamp", it) } }
284
+ )
285
+ }
286
+ })
287
+ var stream: java.io.FileOutputStream? = null
288
+ try {
289
+ stream = target.startWrite()
290
+ stream.write(json.toString().toByteArray())
291
+ target.finishWrite(stream)
292
+ } catch (e: Exception) {
293
+ stream?.let(target::failWrite)
294
+ Log.d(TAG, "launch links not saved: ${e.javaClass.simpleName}")
295
+ }
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Registers [ActivityIntents] before the first Activity is created. An app that removes this
301
+ * startup entry is covered from when the module loads, less precisely.
302
+ */
303
+ internal class ActivityIntentsInitializer : Initializer<Unit> {
304
+ override fun create(context: Context) = ActivityIntents.register(context)
305
+
306
+ override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
307
+ }