|
| 1 | +package com.mparticlesample |
| 2 | + |
| 3 | +import android.app.Application |
| 4 | +import android.os.Handler |
| 5 | +import android.os.Looper |
| 6 | +import android.util.Log |
| 7 | +import com.facebook.react.bridge.Promise |
| 8 | +import com.facebook.react.bridge.ReactApplicationContext |
| 9 | +import com.facebook.react.bridge.ReactContextBaseJavaModule |
| 10 | +import com.facebook.react.bridge.ReactMethod |
| 11 | +import com.mparticle.MParticle |
| 12 | +import com.mparticle.MParticleOptions |
| 13 | +import com.mparticle.identity.IdentityApiRequest |
| 14 | + |
| 15 | +/** |
| 16 | + * Edge-case example: "deferred initialisation with Turbo Modules". |
| 17 | + * |
| 18 | + * Instead of calling MParticle.start() in MainApplication.onCreate() (the standard, |
| 19 | + * supported integration), JS calls startMParticle() from this native module after the |
| 20 | + * first frame is painted. This reproduces a partner pattern used to shave init cost off |
| 21 | + * app startup -- and exposes an Android-specific race that does NOT occur on iOS: |
| 22 | + * |
| 23 | + * The Rokt SDK caches its "current Activity" only in onActivityResumed, via an observer |
| 24 | + * registered during Rokt.init() (which mParticle calls from RoktKit.onKitCreate()). When |
| 25 | + * init runs AFTER the host Activity has already resumed -- exactly what deferred init does |
| 26 | + * -- that resume is missed, so overlay/bottom-sheet placements (RoktModalActivity) have no |
| 27 | + * Activity to launch from until the next resume (the "press home and reopen" workaround). |
| 28 | + * iOS is immune because it resolves the presenter lazily at execute time. |
| 29 | + * |
| 30 | + * Fixed upstream in the Rokt Android SDK by observing the lifecycle from process start: |
| 31 | + * ROKT/sdk-android-source#1062 and #1063 (v5 backport #1082). This example is kept around |
| 32 | + * as an easy way to re-trigger the scenario and confirm the fix / catch regressions. |
| 33 | + * |
| 34 | + * To enable: flip DEFERRED_INIT_EXAMPLE to true and watch `adb logcat -s DeferredInitRepro` |
| 35 | + * -- the DEFERRED tracker's currentActivity stays null while the EAGER one captures it. |
| 36 | + * |
| 37 | + * (Registered through a legacy ReactPackage; under the New Architecture it is invoked via the |
| 38 | + * TurboModule interop layer. The JS call timing -- after first-frame paint -- is identical to |
| 39 | + * a pure TurboModule, which is what the race depends on.) |
| 40 | + */ |
| 41 | +class DeferredInitModule(private val reactContext: ReactApplicationContext) : |
| 42 | + ReactContextBaseJavaModule(reactContext) { |
| 43 | + |
| 44 | + companion object { |
| 45 | + const val TAG = "DeferredInitRepro" |
| 46 | + |
| 47 | + /** |
| 48 | + * Master switch for the deferred-init edge case. Keep this `false` so the sample uses the |
| 49 | + * standard eager init in MainApplication.onCreate(); set it to `true` to reproduce the |
| 50 | + * late-init Activity-capture race described above. |
| 51 | + */ |
| 52 | + const val DEFERRED_INIT_EXAMPLE = false |
| 53 | + |
| 54 | + // Registered in Application.onCreate -- the eager path, for contrast. |
| 55 | + var eagerTracker: ActivityTracker? = null |
| 56 | + } |
| 57 | + |
| 58 | + // Registered at deferred-start time, mirroring Rokt.init()'s late registration. |
| 59 | + private val deferredTracker = ActivityTracker("DEFERRED") |
| 60 | + |
| 61 | + override fun getName(): String = "DeferredInit" |
| 62 | + |
| 63 | + @ReactMethod |
| 64 | + fun startMParticle(promise: Promise) { |
| 65 | + if (!DEFERRED_INIT_EXAMPLE) { |
| 66 | + // Standard mode: mParticle was already started in Application.onCreate(); nothing to do. |
| 67 | + promise.resolve("eager-init (deferred example disabled)") |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + // Idempotent: JS may invoke this more than once (Fast Refresh, remounts). Once mParticle |
| 72 | + // has started, skip re-registering the lifecycle observer and re-starting the SDK. |
| 73 | + if (MParticle.getInstance() != null) { |
| 74 | + promise.resolve("already started") |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + Log.i(TAG, "startMParticle() called from JS (post first-frame). MParticle.getInstance()=${MParticle.getInstance()}") |
| 79 | + |
| 80 | + val app = reactContext.applicationContext as Application |
| 81 | + |
| 82 | + // Mirror Rokt SDK: register the activity observer at init time, NOT at process start. |
| 83 | + app.registerActivityLifecycleCallbacks(deferredTracker) |
| 84 | + Log.i(TAG, "[DEFERRED] tracker registered. currentActivity right now = ${deferredTracker.currentActivity}") |
| 85 | + |
| 86 | + val identityRequest = IdentityApiRequest.withEmptyUser() |
| 87 | + val options = MParticleOptions.builder(app) |
| 88 | + .credentials("REPLACE_ME", "REPLACE_ME") |
| 89 | + .logLevel(MParticle.LogLevel.VERBOSE) |
| 90 | + .identify(identityRequest.build()) |
| 91 | + .build() |
| 92 | + |
| 93 | + MParticle.start(options) |
| 94 | + Log.i(TAG, "MParticle.start() invoked from native module. getInstance()=${MParticle.getInstance()}") |
| 95 | + |
| 96 | + // Snapshot the captured activity 2s later: did the deferred observer ever |
| 97 | + // see a resume? (Rokt's currentActivity cache works exactly this way.) |
| 98 | + Handler(Looper.getMainLooper()).postDelayed({ |
| 99 | + Log.i( |
| 100 | + TAG, |
| 101 | + "SNAPSHOT after 2s -> EAGER.currentActivity=${eagerTracker?.currentActivity?.localClassName} | " + |
| 102 | + "DEFERRED.currentActivity=${deferredTracker.currentActivity?.localClassName}" |
| 103 | + ) |
| 104 | + }, 2000) |
| 105 | + |
| 106 | + promise.resolve("started") |
| 107 | + } |
| 108 | + |
| 109 | + @ReactMethod |
| 110 | + fun reportActivityState(promise: Promise) { |
| 111 | + val msg = "EAGER=${eagerTracker?.currentActivity?.localClassName} | DEFERRED=${deferredTracker.currentActivity?.localClassName}" |
| 112 | + Log.i(TAG, "reportActivityState -> $msg") |
| 113 | + promise.resolve(msg) |
| 114 | + } |
| 115 | +} |
0 commit comments