2222import androidx .fragment .app .FragmentActivity ;
2323
2424import com .facebook .react .ReactApplication ;
25+ import com .facebook .react .ReactHost ;
2526import com .facebook .react .ReactInstanceManager ;
2627import com .facebook .react .ReactRootView ;
2728import com .facebook .react .bridge .ActivityEventListener ;
@@ -89,6 +90,8 @@ public class HyperSdkReactModule extends ReactContextBaseJavaModule implements A
8990
9091 private boolean wasProcessWithActivity = false ;
9192
93+ private boolean useNewApprochForMerchantView = false ;
94+
9295 private Set <String > registeredComponents = new HashSet <>();
9396
9497 @ NonNull
@@ -97,6 +100,7 @@ public class HyperSdkReactModule extends ReactContextBaseJavaModule implements A
97100 HyperSdkReactModule (ReactApplicationContext reactContext ) {
98101 super (reactContext );
99102 this .context = reactContext ;
103+ useNewApprochForMerchantView = setUseNewApprochForMerchantView ();
100104 reactContext .addActivityEventListener (this );
101105 }
102106
@@ -318,20 +322,56 @@ public View createReactSurfaceView(String viewName) {
318322 }
319323
320324 private View createMerchantView (String viewName ) {
321- if (newArchEnabled ) {
322- return createReactSurfaceView (viewName );
323- } else {
325+
326+ if (!useNewApprochForMerchantView ) {
327+ if (reactInstanceManager == null ) {
328+ Application app = activity .getApplication ();
329+ if (app instanceof ReactApplication ) {
330+ reactInstanceManager = ((ReactApplication ) app ).getReactNativeHost ().getReactInstanceManager ();
331+ }
332+ }
333+ if (newArchEnabled ) {
334+ return createReactSurfaceView (viewName );
335+ }
336+ if (reactInstanceManager == null ) {
337+ return null ;
338+ }
324339 ReactRootView reactRootView = new ReactRootView (activity );
325340 reactRootView .startReactApplication (reactInstanceManager , viewName );
326341 return reactRootView ;
327342 }
343+ try {
344+ ReactHost reactHost = ((ReactApplication ) activity .getApplication ()).getReactHost ();
345+ if (reactHost != null ) {
346+ Object surface = reactHost .createSurface (
347+ activity ,
348+ viewName ,
349+ null
350+ );
351+ surface .getClass ().getMethod ("start" ).invoke (surface );
352+
353+ return (View ) surface .getClass ().getMethod ("getView" ).invoke (surface );
354+ }
355+ return null ;
356+ } catch (Exception e ) {
357+ SdkTracker .trackAndLogBootException (
358+ NAME ,
359+ LogConstants .CATEGORY_LIFECYCLE ,
360+ LogConstants .SUBCATEGORY_HYPER_SDK ,
361+ LogConstants .SDK_TRACKER_LABEL ,
362+ "Exception in createMerchantView" ,
363+ e
364+ );
365+ return null ;
366+ }
328367 }
329368
369+
330370 @ Nullable
331371 @ Override
332372 public View getMerchantView (ViewGroup viewGroup , MerchantViewType merchantViewType ) {
333373 Activity activity = (Activity ) getCurrentActivity ();
334- if (reactInstanceManager == null || activity == null ) {
374+ if (activity == null ) {
335375 return super .getMerchantView (viewGroup , merchantViewType );
336376 } else {
337377 View merchantView = null ;
@@ -370,6 +410,17 @@ public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewTy
370410 }
371411 }
372412
413+ private Boolean setUseNewApprochForMerchantView () {
414+ String version = BuildConfig .REACT_NATIVE_VERSION ;
415+ String [] parts = version .split ("\\ ." );
416+ if (parts .length > 1 ) {
417+ int major = Integer .parseInt (parts [0 ]);
418+ int minor = Integer .parseInt (parts [1 ]);
419+ return major > 0 || (major == 0 && minor >= 82 );
420+ }
421+ return false ;
422+ }
423+
373424 private void createHyperService (@ Nullable String tenantId , @ Nullable String clientId ) {
374425 FragmentActivity activity = (FragmentActivity ) getCurrentActivity ();
375426 if (activity == null ) {
@@ -384,7 +435,6 @@ private void createHyperService(@Nullable String tenantId, @Nullable String clie
384435 Application app = activity .getApplication ();
385436 if (app instanceof ReactApplication ) {
386437 this .app = ((ReactApplication ) app );
387- reactInstanceManager = ((ReactApplication ) app ).getReactNativeHost ().getReactInstanceManager ();
388438 }
389439 if (tenantId != null && clientId != null ) {
390440 hyperServices = new HyperServices (activity , tenantId , clientId );
0 commit comments