@sentiance-react-native/core 6.15.0-rc.3 → 6.15.0-rc.5
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/RNSentianceCore.podspec +1 -1
- package/android/build.gradle +23 -11
- package/android/consumer-rules.pro +4 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/common/base/AbstractSentianceEmitter.java +38 -41
- package/android/src/main/java/com/sentiance/react/bridge/core/common/util/ReactContextProvider.java +72 -0
- package/package.json +1 -1
package/RNSentianceCore.podspec
CHANGED
package/android/build.gradle
CHANGED
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
plugins {
|
|
2
|
-
|
|
2
|
+
id "com.android.library"
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
android {
|
|
6
|
-
|
|
6
|
+
namespace "com.sentiance.react.bridge.core"
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
compileOptions {
|
|
9
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
10
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
buildFeatures {
|
|
14
|
+
buildConfig = true
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
def isNewArchEnabled =
|
|
18
|
+
(project.findProperty('newArchEnabled') ?: "false").toBoolean()
|
|
19
|
+
|
|
20
|
+
defaultConfig {
|
|
21
|
+
consumerProguardFiles 'rules-consumer.pro'
|
|
22
|
+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchEnabled.toString()
|
|
23
|
+
}
|
|
12
24
|
}
|
|
13
25
|
|
|
14
26
|
apply from: "$project.projectDir/package-json-reader.gradle"
|
|
@@ -19,12 +31,12 @@ applyAndroidVersionsFrom(packageJson)
|
|
|
19
31
|
def sentianceSdkVersion = getSentianceSdkVersion()
|
|
20
32
|
|
|
21
33
|
dependencies {
|
|
22
|
-
|
|
23
|
-
|
|
34
|
+
implementation(platform("com.sentiance:sdk-bom:${sentianceSdkVersion}"))
|
|
35
|
+
api("com.sentiance:sdk") { transitive = true }
|
|
24
36
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
if (findProject(':test-common')) {
|
|
38
|
+
testImplementation project(':test-common')
|
|
39
|
+
}
|
|
28
40
|
}
|
|
29
41
|
|
|
30
42
|
applyReactNativeDependency()
|
|
@@ -4,56 +4,53 @@ import android.content.Context;
|
|
|
4
4
|
import android.os.Handler;
|
|
5
5
|
import android.os.Looper;
|
|
6
6
|
|
|
7
|
-
import
|
|
8
|
-
import
|
|
7
|
+
import androidx.annotation.NonNull;
|
|
8
|
+
import androidx.annotation.Nullable;
|
|
9
|
+
|
|
9
10
|
import com.facebook.react.bridge.ReactContext;
|
|
10
11
|
import com.facebook.react.bridge.WritableMap;
|
|
11
12
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
13
|
+
import com.sentiance.react.bridge.core.common.util.ReactContextProvider;
|
|
12
14
|
|
|
13
15
|
public abstract class AbstractSentianceEmitter {
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
} else if (retry.count-- > 0) {
|
|
45
|
-
mHandler.postDelayed(this, 500);
|
|
46
|
-
}
|
|
17
|
+
protected final Handler mHandler = new Handler(Looper.getMainLooper());
|
|
18
|
+
@Nullable
|
|
19
|
+
protected ReactContext reactContext;
|
|
20
|
+
@NonNull
|
|
21
|
+
protected ReactContextProvider reactContextProvider;
|
|
22
|
+
|
|
23
|
+
protected AbstractSentianceEmitter(Context context) {
|
|
24
|
+
reactContextProvider = new ReactContextProvider(context.getApplicationContext());
|
|
25
|
+
reactContext = reactContextProvider.createReactContext();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected void sendEvent(final String key, final WritableMap map) {
|
|
29
|
+
if (reactContext != null && reactContext.hasActiveCatalystInstance()) {
|
|
30
|
+
this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(key, map);
|
|
31
|
+
} else {
|
|
32
|
+
// add delay
|
|
33
|
+
final Counter retry = new Counter(20);
|
|
34
|
+
mHandler.postDelayed(new Runnable() {
|
|
35
|
+
@Override
|
|
36
|
+
public void run() {
|
|
37
|
+
if (AbstractSentianceEmitter.this.reactContext == null)
|
|
38
|
+
AbstractSentianceEmitter.this.reactContext = reactContextProvider.createReactContext();
|
|
39
|
+
if (AbstractSentianceEmitter.this.reactContext != null && AbstractSentianceEmitter.this.reactContext.hasActiveCatalystInstance()) {
|
|
40
|
+
AbstractSentianceEmitter.this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(key, map);
|
|
41
|
+
} else if (retry.count-- > 0) {
|
|
42
|
+
mHandler.postDelayed(this, 500);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}, 500);
|
|
47
46
|
}
|
|
48
|
-
}, 500);
|
|
49
47
|
}
|
|
50
|
-
}
|
|
51
48
|
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
private static class Counter {
|
|
50
|
+
int count;
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
Counter(int count) {
|
|
53
|
+
this.count = count;
|
|
54
|
+
}
|
|
57
55
|
}
|
|
58
|
-
}
|
|
59
56
|
}
|
package/android/src/main/java/com/sentiance/react/bridge/core/common/util/ReactContextProvider.java
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package com.sentiance.react.bridge.core.common.util;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.util.Log;
|
|
5
|
+
|
|
6
|
+
import androidx.annotation.Nullable;
|
|
7
|
+
|
|
8
|
+
import com.facebook.react.ReactApplication;
|
|
9
|
+
import com.facebook.react.ReactNativeHost;
|
|
10
|
+
import com.facebook.react.bridge.ReactContext;
|
|
11
|
+
import com.sentiance.react.bridge.core.BuildConfig;
|
|
12
|
+
|
|
13
|
+
public final class ReactContextProvider {
|
|
14
|
+
|
|
15
|
+
private static final String TAG = "ReactNativeContextProvider";
|
|
16
|
+
|
|
17
|
+
private final ReactApplication mReactApplication;
|
|
18
|
+
|
|
19
|
+
public ReactContextProvider(Context applicationContext) {
|
|
20
|
+
mReactApplication = (ReactApplication) applicationContext;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Triggers the initialization of a React context if it is not initializing already,
|
|
25
|
+
* or returns the current React context if it is already initialized.
|
|
26
|
+
*
|
|
27
|
+
* If this call does end up triggering the initialization of a React context,
|
|
28
|
+
* then it will probably return <code>null</code> since the operation is asynchronous.
|
|
29
|
+
*
|
|
30
|
+
* @return a ReactContext if it exists, null otherwise.
|
|
31
|
+
*/
|
|
32
|
+
@Nullable
|
|
33
|
+
public ReactContext createReactContext() {
|
|
34
|
+
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
|
|
35
|
+
return createReactContextInNewArchMode();
|
|
36
|
+
} else {
|
|
37
|
+
return createReactContextInLegacyArchMode();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@Nullable
|
|
42
|
+
private ReactContext createReactContextInNewArchMode() {
|
|
43
|
+
try {
|
|
44
|
+
java.lang.reflect.Method getReactHost = mReactApplication.getClass().getMethod("getReactHost");
|
|
45
|
+
Object reactHost = getReactHost.invoke(mReactApplication);
|
|
46
|
+
Class<?> reactHostClass = Class.forName("com.facebook.react.ReactHost");
|
|
47
|
+
|
|
48
|
+
java.lang.reflect.Method start = reactHostClass.getMethod("start");
|
|
49
|
+
java.lang.reflect.Method getCurrentReactContext = reactHostClass.getMethod("getCurrentReactContext");
|
|
50
|
+
|
|
51
|
+
// Trigger the initialization of a React context, should be safe to call multiple times
|
|
52
|
+
// https://github.com/facebook/react-native/blob/main/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.kt#L723
|
|
53
|
+
start.invoke(reactHost);
|
|
54
|
+
|
|
55
|
+
return (ReactContext) getCurrentReactContext.invoke(reactHost);
|
|
56
|
+
} catch (Throwable t) {
|
|
57
|
+
Log.d(TAG, "Failed to create React context");
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@Nullable
|
|
63
|
+
private ReactContext createReactContextInLegacyArchMode() {
|
|
64
|
+
ReactNativeHost reactNativeHost = mReactApplication.getReactNativeHost();
|
|
65
|
+
|
|
66
|
+
// Trigger the initialization of a React context
|
|
67
|
+
if (!reactNativeHost.getReactInstanceManager().hasStartedCreatingInitialContext())
|
|
68
|
+
reactNativeHost.getReactInstanceManager().createReactContextInBackground();
|
|
69
|
+
|
|
70
|
+
return reactNativeHost.getReactInstanceManager().getCurrentReactContext();
|
|
71
|
+
}
|
|
72
|
+
}
|