@adshift/react-native-plugin 2.1.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.
- package/AdShiftReactNativePlugin.podspec +20 -4
- package/CHANGELOG.md +57 -8
- package/README.md +131 -74
- package/android/build.gradle +3 -2
- package/android/consumer-rules.pro +2 -21
- package/android/src/main/AndroidManifest.xml +14 -1
- package/android/src/main/java/com/adshift/reactnativeplugin/ActivityIntents.kt +307 -0
- package/android/src/main/java/com/adshift/reactnativeplugin/AdShiftReactNativePluginModule.kt +90 -153
- package/android/src/test/java/com/adshift/reactnativeplugin/ConsentSnapshotTest.kt +0 -20
- package/android/src/test/java/com/adshift/reactnativeplugin/DeepLinkDeliveryTest.kt +40 -7
- package/android/src/test/java/com/adshift/reactnativeplugin/InitializeOrderingTest.kt +51 -5
- package/android/src/test/java/com/adshift/reactnativeplugin/IntentLinkDedupTest.kt +0 -5
- package/android/src/test/java/com/adshift/reactnativeplugin/LaunchIntentForwardingTest.kt +21 -11
- package/android/src/test/java/com/adshift/reactnativeplugin/LaunchLinkRecordTest.kt +420 -0
- package/android/src/test/java/com/adshift/reactnativeplugin/ModuleTestSupport.kt +97 -25
- package/android/src/test/java/com/adshift/reactnativeplugin/StartVerdictTest.kt +0 -4
- package/ios/AdShiftReactNativePlugin.h +0 -12
- package/ios/AdShiftReactNativePlugin.mm +7 -14
- package/ios/AdShiftReactNativePluginHelper.swift +38 -73
- package/ios/Core/BridgeRules.swift +42 -30
- package/lib/commonjs/Adshift.js +100 -284
- package/lib/commonjs/Adshift.js.map +1 -1
- package/lib/commonjs/NativeAdShiftReactNativePlugin.js +2 -11
- package/lib/commonjs/NativeAdShiftReactNativePlugin.js.map +1 -1
- package/lib/commonjs/index.js +6 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/jest/index.js +6 -18
- package/lib/commonjs/jest/index.js.map +1 -1
- package/lib/commonjs/nativeSdkVersions.js +4 -6
- package/lib/commonjs/nativeSdkVersions.js.map +1 -1
- package/lib/commonjs/types/AdshiftAdRevenue.js +0 -24
- package/lib/commonjs/types/AdshiftAdRevenue.js.map +1 -1
- package/lib/commonjs/types/AdshiftConfig.js +11 -23
- package/lib/commonjs/types/AdshiftConfig.js.map +1 -1
- package/lib/commonjs/types/AdshiftConsent.js +9 -45
- package/lib/commonjs/types/AdshiftConsent.js.map +1 -1
- package/lib/commonjs/types/AdshiftConsentSnapshot.js +5 -24
- package/lib/commonjs/types/AdshiftConsentSnapshot.js.map +1 -1
- package/lib/commonjs/types/AdshiftDeepLink.js +50 -33
- package/lib/commonjs/types/AdshiftDeepLink.js.map +1 -1
- package/lib/commonjs/types/AdshiftError.js +7 -27
- package/lib/commonjs/types/AdshiftError.js.map +1 -1
- package/lib/commonjs/types/AdshiftEventParam.js +4 -38
- package/lib/commonjs/types/AdshiftEventParam.js.map +1 -1
- package/lib/commonjs/types/AdshiftEventType.js +1 -39
- package/lib/commonjs/types/AdshiftEventType.js.map +1 -1
- package/lib/commonjs/types/AdshiftThirdPartySharing.js +7 -38
- package/lib/commonjs/types/AdshiftThirdPartySharing.js.map +1 -1
- package/lib/commonjs/types/index.js +12 -0
- package/lib/commonjs/types/index.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/module/Adshift.js +101 -285
- package/lib/module/Adshift.js.map +1 -1
- package/lib/module/NativeAdShiftReactNativePlugin.js +2 -12
- package/lib/module/NativeAdShiftReactNativePlugin.js.map +1 -1
- package/lib/module/index.js +2 -47
- package/lib/module/index.js.map +1 -1
- package/lib/module/jest/index.js +7 -19
- package/lib/module/jest/index.js.map +1 -1
- package/lib/module/nativeSdkVersions.js +4 -6
- package/lib/module/nativeSdkVersions.js.map +1 -1
- package/lib/module/types/AdshiftAdRevenue.js +0 -24
- package/lib/module/types/AdshiftAdRevenue.js.map +1 -1
- package/lib/module/types/AdshiftConfig.js +10 -23
- package/lib/module/types/AdshiftConfig.js.map +1 -1
- package/lib/module/types/AdshiftConsent.js +9 -45
- package/lib/module/types/AdshiftConsent.js.map +1 -1
- package/lib/module/types/AdshiftConsentSnapshot.js +5 -24
- package/lib/module/types/AdshiftConsentSnapshot.js.map +1 -1
- package/lib/module/types/AdshiftDeepLink.js +49 -32
- package/lib/module/types/AdshiftDeepLink.js.map +1 -1
- package/lib/module/types/AdshiftError.js +7 -28
- package/lib/module/types/AdshiftError.js.map +1 -1
- package/lib/module/types/AdshiftEventParam.js +4 -38
- package/lib/module/types/AdshiftEventParam.js.map +1 -1
- package/lib/module/types/AdshiftEventType.js +1 -39
- package/lib/module/types/AdshiftEventType.js.map +1 -1
- package/lib/module/types/AdshiftThirdPartySharing.js +7 -38
- package/lib/module/types/AdshiftThirdPartySharing.js.map +1 -1
- package/lib/module/types/index.js +2 -2
- package/lib/module/types/index.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/typescript/commonjs/src/Adshift.d.ts +68 -184
- package/lib/typescript/commonjs/src/Adshift.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts +18 -35
- package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/index.d.ts +2 -34
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/jest/index.d.ts +9 -21
- package/lib/typescript/commonjs/src/jest/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts +4 -6
- package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts +0 -24
- package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts +35 -39
- package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts +9 -49
- package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts +10 -28
- package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts +55 -39
- package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/AdshiftError.d.ts +7 -26
- package/lib/typescript/commonjs/src/types/AdshiftError.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts +3 -19
- package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/AdshiftEventType.d.ts +1 -23
- package/lib/typescript/commonjs/src/types/AdshiftEventType.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts +7 -38
- package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/types/index.d.ts +3 -3
- package/lib/typescript/commonjs/src/types/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/version.d.ts +1 -1
- package/lib/typescript/module/src/Adshift.d.ts +68 -184
- package/lib/typescript/module/src/Adshift.d.ts.map +1 -1
- package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts +18 -35
- package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -1
- package/lib/typescript/module/src/index.d.ts +2 -34
- package/lib/typescript/module/src/index.d.ts.map +1 -1
- package/lib/typescript/module/src/jest/index.d.ts +9 -21
- package/lib/typescript/module/src/jest/index.d.ts.map +1 -1
- package/lib/typescript/module/src/nativeSdkVersions.d.ts +4 -6
- package/lib/typescript/module/src/nativeSdkVersions.d.ts.map +1 -1
- package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts +0 -24
- package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts.map +1 -1
- package/lib/typescript/module/src/types/AdshiftConfig.d.ts +35 -39
- package/lib/typescript/module/src/types/AdshiftConfig.d.ts.map +1 -1
- package/lib/typescript/module/src/types/AdshiftConsent.d.ts +9 -49
- package/lib/typescript/module/src/types/AdshiftConsent.d.ts.map +1 -1
- package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts +10 -28
- package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts.map +1 -1
- package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts +55 -39
- package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts.map +1 -1
- package/lib/typescript/module/src/types/AdshiftError.d.ts +7 -26
- package/lib/typescript/module/src/types/AdshiftError.d.ts.map +1 -1
- package/lib/typescript/module/src/types/AdshiftEventParam.d.ts +3 -19
- package/lib/typescript/module/src/types/AdshiftEventParam.d.ts.map +1 -1
- package/lib/typescript/module/src/types/AdshiftEventType.d.ts +1 -23
- package/lib/typescript/module/src/types/AdshiftEventType.d.ts.map +1 -1
- package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts +7 -38
- package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts.map +1 -1
- package/lib/typescript/module/src/types/index.d.ts +3 -3
- package/lib/typescript/module/src/types/index.d.ts.map +1 -1
- package/lib/typescript/module/src/version.d.ts +1 -1
- package/package.json +11 -1
- package/src/Adshift.ts +118 -285
- package/src/NativeAdShiftReactNativePlugin.ts +19 -47
- package/src/index.tsx +7 -46
- package/src/jest/index.ts +11 -20
- package/src/nativeSdkVersions.ts +4 -6
- package/src/types/AdshiftAdRevenue.ts +0 -24
- package/src/types/AdshiftConfig.ts +48 -55
- package/src/types/AdshiftConsent.ts +9 -49
- package/src/types/AdshiftConsentSnapshot.ts +11 -38
- package/src/types/AdshiftDeepLink.ts +87 -39
- package/src/types/AdshiftError.ts +7 -26
- package/src/types/AdshiftEventParam.ts +4 -38
- package/src/types/AdshiftEventType.ts +1 -39
- package/src/types/AdshiftThirdPartySharing.ts +7 -38
- package/src/types/index.ts +9 -2
- package/src/version.ts +1 -1
- package/vendor/ios/AdshiftSDK.files.json +30 -0
- package/vendor/ios/AdshiftSDK.xcframework/Info.plist +44 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/AdshiftSDK +0 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Headers/AdshiftSDK-Swift.h +315 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Info.plist +0 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.abi.json +18729 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.private.swiftinterface +414 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.swiftinterface +414 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/module.modulemap +4 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/PrivacyInfo.xcprivacy +132 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/AdshiftSDK +0 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Headers/AdshiftSDK-Swift.h +626 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Info.plist +0 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.abi.json +18729 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +414 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.swiftinterface +414 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.abi.json +18729 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +414 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +414 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/module.modulemap +4 -0
- package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/PrivacyInfo.xcprivacy +132 -0
|
@@ -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
|
+
}
|