-
Notifications
You must be signed in to change notification settings - Fork 4
WMSDK-445 #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
WMSDK-445 #114
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8629c9d
WMSDK-445: support auto init for send click data
b023579
WMSDK-445: change reactHost listener
40ebabd
WMSDK-445: review
ae4ecd6
WMSDK-445: added proguard rules
4458814
WMSDK-445: follow review
b44ed40
WMSDK-445: update example
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| -keep class com.facebook.react.ReactHost { *; } | ||
| -keep interface com.facebook.react.ReactInstanceEventListener { *; } | ||
| -keep interface com.facebook.react.ReactApplication { | ||
| public com.facebook.react.ReactHost getReactHost(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="com.mindboxsdk"> | ||
|
|
||
| <application> | ||
| <provider | ||
| android:name="com.mindboxsdk.MindboxSdkInitProvider" | ||
| android:authorities="${applicationId}.mindbox.init" | ||
| android:exported="false" /> | ||
| </application> | ||
| </manifest> |
55 changes: 55 additions & 0 deletions
55
android/src/main/java/com/mindboxsdk/MindboxEventEmitter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.mindboxsdk | ||
|
|
||
| import android.app.Activity | ||
| import android.app.Application | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.util.Log | ||
| import com.facebook.react.ReactApplication | ||
| import com.facebook.react.ReactInstanceManager | ||
| import com.facebook.react.bridge.ReactContext | ||
| import com.facebook.react.ReactActivity | ||
| import cloud.mindbox.mobile_sdk.Mindbox | ||
| import cloud.mindbox.mobile_sdk.logger.Level | ||
|
|
||
| internal class MindboxEventEmitter ( | ||
| private val application: Application | ||
| ) : MindboxEventSubscriber { | ||
|
|
||
| private var jsDelivery: MindboxJsDelivery? = null | ||
|
|
||
| override fun onEvent(event: MindboxSdkLifecycleEvent) { | ||
| when (event) { | ||
| is MindboxSdkLifecycleEvent.NewIntent -> handleNewIntent(event.reactContext, event.intent) | ||
| is MindboxSdkLifecycleEvent.ActivityCreated -> handleActivityCreated(event.reactContext, event.activity) | ||
| is MindboxSdkLifecycleEvent.ActivityDestroyed -> handleActivityDestroyed() | ||
| } | ||
| } | ||
|
|
||
| private fun handleNewIntent(context: ReactContext, intent: Intent) { | ||
| Mindbox.writeLog("[RN] Handle new intent in event emitter. ", Level.INFO) | ||
| Mindbox.onNewIntent(intent) | ||
| Mindbox.onPushClicked(context, intent) | ||
| jsDelivery?.sendPushClicked(intent) | ||
| } | ||
|
|
||
| private fun handleActivityCreated(reactContext:ReactContext, activity: Activity) { | ||
| Mindbox.writeLog("[RN] Handle activity created", Level.INFO) | ||
| runCatching { | ||
| reactContext.let { reactContext -> | ||
| initializeAndSendIntent(reactContext, activity) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun initializeAndSendIntent(context: ReactContext, activity: Activity) { | ||
| Mindbox.writeLog("[RN] Initialize MindboxJsDelivery", Level.INFO) | ||
| jsDelivery = MindboxJsDelivery.Shared.getInstance(context) | ||
| val currentActivity = context.currentActivity ?: activity | ||
| currentActivity.intent?.let { handleNewIntent(context, it) } | ||
| } | ||
|
|
||
| private fun handleActivityDestroyed() { | ||
| jsDelivery = null | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
android/src/main/java/com/mindboxsdk/MindboxEventSubscriber.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.mindboxsdk | ||
|
|
||
| internal interface MindboxEventSubscriber { | ||
| fun onEvent(event: MindboxSdkLifecycleEvent) | ||
| } |
64 changes: 64 additions & 0 deletions
64
android/src/main/java/com/mindboxsdk/MindboxSdkInitProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.mindboxsdk | ||
|
|
||
| import android.app.Application | ||
| import android.content.ContentProvider | ||
| import android.content.ContentValues | ||
| import android.content.pm.PackageManager | ||
| import android.database.Cursor | ||
| import android.net.Uri | ||
| import android.util.Log | ||
|
|
||
| internal class MindboxSdkInitProvider : ContentProvider() { | ||
|
|
||
| companion object { | ||
| private const val AUTO_INIT_ENABLED_KEY = "com.mindbox.sdk.AUTO_INIT_ENABLED" | ||
| private const val TAG = "MindboxSdkInitProvider" | ||
| } | ||
|
|
||
| override fun onCreate(): Boolean { | ||
| runCatching { | ||
| Log.i(TAG, "onCreate initProvider.") | ||
| (context?.applicationContext as? Application)?.let { application -> | ||
| if (isAutoInitEnabled(application)) { | ||
| Log.i(TAG, "Automatic initialization is enabled.") | ||
| MindboxSdkLifecycleListener.register(application) | ||
| } else { | ||
| Log.i(TAG, "Automatic initialization is disabled.") | ||
| } | ||
| } | ||
| }.onFailure { error -> | ||
| Log.e(TAG, "Automatic initialization failed", error) | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| private fun isAutoInitEnabled(application: Application): Boolean = | ||
| runCatching { | ||
| val appInfo = application.packageManager.getApplicationInfo( | ||
| application.packageName, | ||
| PackageManager.GET_META_DATA | ||
| ) | ||
| appInfo.metaData | ||
| ?.getBoolean(AUTO_INIT_ENABLED_KEY, false) | ||
| ?.also { Log.i(TAG, "Result of reading mindbox metadata is $it") } | ||
| ?: false | ||
| }.getOrElse { false } | ||
|
|
||
| override fun query( | ||
| uri: Uri, | ||
| projection: Array<String>?, | ||
| selection: String?, | ||
| selectionArgs: Array<String>?, | ||
| sortOrder: String? | ||
| ): Cursor? = null | ||
|
|
||
| override fun getType(uri: Uri): String? = null | ||
| override fun insert(uri: Uri, values: ContentValues?): Uri? = null | ||
| override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int = 0 | ||
| override fun update( | ||
| uri: Uri, | ||
| values: ContentValues?, | ||
| selection: String?, | ||
| selectionArgs: Array<String>? | ||
| ): Int = 0 | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
android/src/main/java/com/mindboxsdk/MindboxSdkLifecycleEvent.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.mindboxsdk | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Intent | ||
| import com.facebook.react.bridge.ReactContext | ||
|
|
||
| internal sealed class MindboxSdkLifecycleEvent { | ||
| data class NewIntent(val reactContext: ReactContext, val intent: Intent) : MindboxSdkLifecycleEvent() | ||
| data class ActivityCreated(val reactContext: ReactContext, val activity: Activity) : MindboxSdkLifecycleEvent() | ||
| data class ActivityDestroyed(val activity: Activity) : MindboxSdkLifecycleEvent() | ||
| } |
170 changes: 170 additions & 0 deletions
170
android/src/main/java/com/mindboxsdk/MindboxSdkLifecycleListener.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package com.mindboxsdk | ||
|
|
||
| import android.app.Activity | ||
| import android.app.Application | ||
| import android.content.Intent | ||
| import android.os.Bundle | ||
| import android.util.Log | ||
| import com.facebook.react.ReactApplication | ||
| import com.facebook.react.ReactInstanceManager | ||
| import com.facebook.react.bridge.ActivityEventListener | ||
| import com.facebook.react.bridge.ReactContext | ||
| import java.util.concurrent.atomic.AtomicBoolean | ||
| import cloud.mindbox.mobile_sdk.Mindbox | ||
| import cloud.mindbox.mobile_sdk.logger.Level | ||
|
|
||
|
|
||
| internal class MindboxSdkLifecycleListener private constructor( | ||
| private val application: Application, | ||
| private val subscriber: MindboxEventSubscriber | ||
| ) : Application.ActivityLifecycleCallbacks { | ||
|
|
||
| companion object { | ||
| fun register( | ||
| application: Application, | ||
| subscriber: MindboxEventSubscriber = MindboxEventEmitter(application) | ||
| ) { | ||
| val listener = MindboxSdkLifecycleListener(application, subscriber) | ||
| application.registerActivityLifecycleCallbacks(listener) | ||
| } | ||
| } | ||
|
|
||
| private val mainActivityClassName: String? by lazy { getLauncherActivityClassName() } | ||
|
|
||
| private fun getLauncherActivityClassName(): String? { | ||
| val pm = application.packageManager | ||
| val intent = pm.getLaunchIntentForPackage(application.packageName) | ||
sergeysozinov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return intent?.component?.className | ||
| } | ||
|
|
||
| private fun isMainActivity(activity: Activity): Boolean { | ||
| return activity.javaClass.name == mainActivityClassName | ||
| } | ||
|
|
||
| private var activityEventListener: ActivityEventListener? = null | ||
|
|
||
| private fun onReactContextAvailable(reactContext: ReactContext, activity: Activity) { | ||
| Mindbox.writeLog("[RN] ReactContext ready", Level.INFO) | ||
| addActivityEventListener(reactContext) | ||
| subscriber.onEvent(MindboxSdkLifecycleEvent.ActivityCreated(reactContext, activity)) | ||
| } | ||
|
|
||
| private fun registerReactContextListener( | ||
| application: Application, | ||
| onReady: (ReactContext) -> Unit | ||
| ) { | ||
| val reactApplication = application.getReactApplication() ?: return | ||
| val reactInstanceManager = getReactInstanceManager() | ||
|
|
||
| val wrapperListener = object : ReactInstanceManager.ReactInstanceEventListener { | ||
| private val called = AtomicBoolean(false) | ||
| override fun onReactContextInitialized(context: ReactContext) { | ||
| if (called.compareAndSet(false, true)) { | ||
| onReady(context) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| reactInstanceManager?.addReactInstanceEventListener(wrapperListener) | ||
sergeysozinov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // RN 0.78+ introduced ReactHost.addReactInstanceEventListener(...). | ||
| // Older RN versions (<= 0.74) expose only ReactInstanceManager.addReactInstanceEventListener(...). | ||
| // In New Architecture the ReactInstanceManager listener might not fire | ||
| // To support RN 0.78+ reliably while keeping backward compatibility, | ||
| // we try to register via ReactHost using reflection (no compile-time dependency). | ||
| // If ReactHost API is unavailable (older RN), this call is silently ignored and we rely on | ||
| // the ReactInstanceManager path. | ||
| addReactHostListener(application, wrapperListener) | ||
| } | ||
|
|
||
| override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { | ||
| if (!isMainActivity(activity)) return | ||
|
|
||
| getReactInstanceManager()?.currentReactContext?.let { | ||
| onReactContextAvailable(it, activity) | ||
| Mindbox.writeLog("[RN] ReactContext already available; skipping listener registration ", Level.INFO) | ||
| return | ||
| } | ||
|
|
||
| registerReactContextListener(application) { reactContext -> | ||
| onReactContextAvailable(reactContext, activity) | ||
| } | ||
| } | ||
|
|
||
| private fun addActivityEventListener(reactContext: ReactContext) { | ||
| activityEventListener?.let { reactContext.removeActivityEventListener(it) } | ||
|
|
||
| activityEventListener = object : ActivityEventListener { | ||
| override fun onNewIntent(intent: Intent?) { | ||
| intent ?: return | ||
| reactContext.currentActivity | ||
| ?.takeIf { isMainActivity(it) } | ||
| ?.let { | ||
| subscriber.onEvent( | ||
| MindboxSdkLifecycleEvent.NewIntent( | ||
| reactContext, | ||
| intent | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| override fun onActivityResult( | ||
| activity: Activity?, requestCode: Int, resultCode: Int, data: Intent? | ||
| ) { | ||
| } | ||
| } | ||
| reactContext.addActivityEventListener(activityEventListener) | ||
| } | ||
|
|
||
| override fun onActivityDestroyed(activity: Activity) { | ||
| if (!isMainActivity(activity)) return | ||
| subscriber.onEvent(MindboxSdkLifecycleEvent.ActivityDestroyed(activity)) | ||
| getReactInstanceManager() | ||
| ?.currentReactContext | ||
| ?.removeActivityEventListener(activityEventListener) | ||
| activityEventListener = null | ||
| } | ||
|
|
||
| override fun onActivityStarted(activity: Activity) {} | ||
| override fun onActivityResumed(activity: Activity) {} | ||
| override fun onActivityPaused(activity: Activity) {} | ||
| override fun onActivityStopped(activity: Activity) {} | ||
| override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {} | ||
|
|
||
|
|
||
| private fun getReactInstanceManager(): ReactInstanceManager? = | ||
| application.getReactApplication()?.reactNativeHost?.reactInstanceManager | ||
|
|
||
| private fun Application.getReactApplication() = this as? ReactApplication | ||
|
|
||
| private fun addReactHostListener( | ||
| application: Application, | ||
| wrapperListener: ReactInstanceManager.ReactInstanceEventListener | ||
| ) { | ||
| runCatching { | ||
| val reactApplication = application as ReactApplication | ||
|
|
||
| val hostClass = Class.forName("com.facebook.react.ReactHost") | ||
sergeysozinov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val listenerClass = Class.forName("com.facebook.react.ReactInstanceEventListener") | ||
|
|
||
| val addMethod = hostClass.getMethod("addReactInstanceEventListener", listenerClass) | ||
| val getHostMethod = reactApplication.javaClass.getMethod("getReactHost") | ||
| val reactHost = getHostMethod.invoke(reactApplication) | ||
|
|
||
| val proxy = java.lang.reflect.Proxy.newProxyInstance( | ||
| listenerClass.classLoader, | ||
| arrayOf(listenerClass) | ||
| ) { _, method, args -> | ||
| if (method.name == "onReactContextInitialized" && args?.size == 1 && args[0] is ReactContext) { | ||
| wrapperListener.onReactContextInitialized(args[0] as ReactContext) | ||
| } | ||
| null | ||
| } | ||
|
|
||
| addMethod.invoke(reactHost, proxy) | ||
| Mindbox.writeLog("[RN] success added react context listener for reactHost", Level.INFO) | ||
| }.onFailure { | ||
| Mindbox.writeLog("[RN] failed added react context listener for reactHost ", Level.ERROR) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.