diff --git a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/NativeBridge.java b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/NativeBridge.java index c6f4ec33..585d1959 100644 --- a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/NativeBridge.java +++ b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/NativeBridge.java @@ -49,4 +49,22 @@ public void run() { } }); } + + void setNativeUserIdAndUserKey(String userId, String userKey) { + TrackMainThread.trackMain().postActionToTrackMain(new Runnable() { + @Override + public void run() { + UserInfoProvider.get().setLoginUserId(userId, userKey); + } + }); + } + + void clearNativeUserIdAndUserKey() { + TrackMainThread.trackMain().postActionToTrackMain(new Runnable() { + @Override + public void run() { + UserInfoProvider.get().setLoginUserId(null, null); + } + }); + } } diff --git a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/WebViewBridgeJavascriptInterface.java b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/WebViewBridgeJavascriptInterface.java index 704f40f6..c28bb0ba 100644 --- a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/WebViewBridgeJavascriptInterface.java +++ b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/WebViewBridgeJavascriptInterface.java @@ -68,4 +68,18 @@ public void clearNativeUserId() { mNativeBridge.clearNativeUserId(); } + @JavascriptInterface + @com.uc.webview.export.JavascriptInterface + public void setNativeUserIdAndUserKey(String userId, String userKey) { + Logger.d(TAG, "setNativeUserIdAndUserKey: " + userId + ", " + userKey); + mNativeBridge.setNativeUserIdAndUserKey(userId, userKey); + } + + @JavascriptInterface + @com.uc.webview.export.JavascriptInterface + public void clearNativeUserIdAndUserKey() { + Logger.d(TAG, "clearNativeUserIdAndUserKey: "); + mNativeBridge.clearNativeUserIdAndUserKey(); + } + } diff --git a/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java b/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java index 97380335..f4cc4bc4 100644 --- a/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java +++ b/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java @@ -25,6 +25,7 @@ import com.google.common.truth.Truth; import com.google.common.util.concurrent.Uninterruptibles; +import com.growingio.android.sdk.CoreConfiguration; import com.growingio.android.sdk.TrackerContext; import com.growingio.android.sdk.track.events.hybrid.HybridCustomEvent; import com.growingio.android.sdk.track.events.hybrid.HybridPageAttributesEvent; @@ -40,6 +41,8 @@ import com.growingio.android.sdk.track.modelloader.data.HybridBridge; import com.growingio.android.sdk.track.modelloader.data.HybridDom; import com.growingio.android.sdk.track.modelloader.data.HybridJson; +import com.growingio.android.sdk.track.providers.ConfigurationProvider; +import com.growingio.android.sdk.track.providers.UserInfoProvider; import org.json.JSONObject; import org.junit.Before; @@ -49,6 +52,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; +import java.util.HashMap; import java.util.concurrent.TimeUnit; @Config(manifest = Config.NONE, sdk = 23) @@ -61,6 +65,7 @@ public class HybridTest { public void setup() { TrackerContext.init(application); TrackerContext.initSuccess(); + ConfigurationProvider.initWithConfig(new CoreConfiguration("test", "test"), new HashMap<>()); } @@ -140,7 +145,24 @@ public void bridgeInterfaceTest() { WebViewJavascriptBridgeConfiguration configuration = new WebViewJavascriptBridgeConfiguration("test", "test", "test", "test", 23); WebViewBridgeJavascriptInterface webInterface = new WebViewBridgeJavascriptInterface(configuration); webInterface.setNativeUserId("cpacm"); + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + Truth.assertThat(UserInfoProvider.get().getLoginUserId()).isEqualTo("cpacm"); webInterface.clearNativeUserId(); + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + Truth.assertThat(UserInfoProvider.get().getLoginUserId()).isEqualTo(""); + + ConfigurationProvider.core().setIdMappingEnabled(true); + webInterface.setNativeUserIdAndUserKey("cpacm", "email"); + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + Truth.assertThat(UserInfoProvider.get().getLoginUserId()).isEqualTo("cpacm"); + Truth.assertThat(UserInfoProvider.get().getLoginUserKey()).isEqualTo("email"); + + webInterface.clearNativeUserIdAndUserKey(); + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); + Truth.assertThat(UserInfoProvider.get().getLoginUserId()).isEqualTo(""); + Truth.assertThat(UserInfoProvider.get().getLoginUserKey()).isEqualTo(""); + ConfigurationProvider.core().setIdMappingEnabled(false); + String testJson = "{\"eventType\":\"LOGIN_USER_ATTRIBUTES\",\"attributes\":{\"grow_index\":\"苹果\",\"grow_click\":14}}"; webInterface.dispatchEvent(testJson); Truth.assertThat(webInterface.getConfiguration()).contains("23");