From 1f8edcc3691e3b093184c4e3c1494eb7a9c3feb4 Mon Sep 17 00:00:00 2001 From: VIGNESH S Date: Fri, 6 Sep 2024 16:22:06 +0530 Subject: [PATCH] Migrated to React Host for new Arch --- .../microsoft/codepush/react/CodePush.java | 16 +++++++ .../codepush/react/CodePushNativeModule.java | 43 ++++++++++++++----- .../codepush/react/ReactInstanceHolder.java | 2 + package.json | 2 +- react-native.config.js | 2 +- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index 2718204d6..57e6c01ad 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -5,6 +5,7 @@ import android.content.pm.PackageManager; import android.content.res.Resources; +import com.facebook.react.ReactHost; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactPackage; import com.facebook.react.bridge.JavaScriptModule; @@ -50,6 +51,7 @@ public class CodePush implements ReactPackage { private static ReactInstanceHolder mReactInstanceHolder; private static CodePush mCurrentInstance; + public CodePush(String deploymentKey, Context context) { this(deploymentKey, context, false); } @@ -109,6 +111,20 @@ public CodePush(String deploymentKey, Context context, boolean isDebugMode, Stri mServerUrl = serverUrl; } + static ReactHost getReactHostFromHolder() { + if (mReactInstanceHolder == null) { + return null; + } + return mReactInstanceHolder.getReactHost(); + } + + public static CodePush getInstance(String deploymentKey, Context context, boolean isDebugMode) { + if (mCurrentInstance == null) { + mCurrentInstance = new CodePush(deploymentKey,context,isDebugMode); + } + return mCurrentInstance; + } + private String getPublicKeyByResourceDescriptor(int publicKeyResourceDescriptor){ String publicKey; try { diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java index e996486d6..99f0aa60d 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java @@ -9,6 +9,7 @@ import android.view.View; import com.facebook.react.ReactApplication; +import com.facebook.react.ReactHost; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactRootView; import com.facebook.react.bridge.Arguments; @@ -23,6 +24,7 @@ import com.facebook.react.modules.core.ChoreographerCompat; import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.modules.core.ReactChoreographer; +import com.facebook.react.runtime.ReactHostDelegate; import org.json.JSONArray; import org.json.JSONException; @@ -129,10 +131,30 @@ private void setJSBundle(ReactInstanceManager instanceManager, String latestJSBu } } + private void setJSBundle(ReactHost host, String latestJSBundleFile) throws { + try { + Field reactHostDelegate = host.getClass().getDeclaredField("mReactHostDelegate"); + reactHostDelegate.setAccessible(true); + ReactHostDelegate delegate = (ReactHostDelegate) reactHostDelegate.get(host); + Field loader = delegate.getClass().getDeclaredField("jsBundleLoader"); + loader.setAccessible(true); + JSBundleLoader latestJSBundleLoader; + if (latestJSBundleFile.toLowerCase().startsWith("assets://")) { + latestJSBundleLoader = JSBundleLoader.createAssetLoader(getReactApplicationContext(), latestJSBundleFile, true); + } else { + latestJSBundleLoader = JSBundleLoader.createFileLoader(latestJSBundleFile); + } + loader.set(delegate,latestJSBundleLoader); + } catch (Exception e) { + CodePushUtils.log("Unable to set JSBundle - CodePush may not support this version of React Native"); + throw new IllegalAccessException("Could not setJSBundle"); + } + } + private void loadBundle() { clearLifecycleEventListener(); try { - mCodePush.clearDebugCacheIfNeeded(resolveInstanceManager()); +// mCodePush.clearDebugCacheIfNeeded(resolveInstanceManager()); } catch(Exception e) { // If we got error in out reflection we should clear debug cache anyway. mCodePush.clearDebugCacheIfNeeded(null); @@ -141,15 +163,15 @@ private void loadBundle() { try { // #1) Get the ReactInstanceManager instance, which is what includes the // logic to reload the current React context. - final ReactInstanceManager instanceManager = resolveInstanceManager(); - if (instanceManager == null) { - return; - } +// final ReactInstanceManager instanceManager = resolveInstanceManager(); +// if (instanceManager == null) { +// return; +// } String latestJSBundleFile = mCodePush.getJSBundleFileInternal(mCodePush.getAssetsBundleFileName()); // #2) Update the locally stored JS bundle file path - setJSBundle(instanceManager, latestJSBundleFile); +// setJSBundle(instanceManager, latestJSBundleFile); // #3) Get the context creation method and fire it on the UI thread (which RN enforces) new Handler(Looper.getMainLooper()).post(new Runnable() { @@ -159,10 +181,11 @@ public void run() { // We don't need to resetReactRootViews anymore // due the issue https://github.com/facebook/react-native/issues/14533 // has been fixed in RN 0.46.0 - //resetReactRootViews(instanceManager); - - instanceManager.recreateReactContextInBackground(); - mCodePush.initializeUpdateAfterRestart(); +// resetReactRootViews(instanceManager); + ReactHost host = CodePush.getReactHostFromHolder(); + CodePush.getReactHostFromHolder().reload("Restart-app"); +// instanceManager.recreateReactContextInBackground(); +// mCodePush.initializeUpdateAfterRestart(); } catch (Exception e) { // The recreation method threw an unknown exception // so just simply fallback to restarting the Activity (if it exists) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/ReactInstanceHolder.java b/android/app/src/main/java/com/microsoft/codepush/react/ReactInstanceHolder.java index dfe871f61..7282a6a00 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/ReactInstanceHolder.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/ReactInstanceHolder.java @@ -1,5 +1,6 @@ package com.microsoft.codepush.react; +import com.facebook.react.ReactHost; import com.facebook.react.ReactInstanceManager; /** @@ -14,4 +15,5 @@ public interface ReactInstanceHolder { * Get the current {@link ReactInstanceManager} instance. May return null. */ ReactInstanceManager getReactInstanceManager(); + ReactHost getReactHost(); } diff --git a/package.json b/package.json index 12b6faca6..12eb05004 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ }, "rnpm": { "android": { - "packageInstance": "new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)" + "packageInstance": "CodePush.getInstance(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)" }, "ios": { "sharedLibraries": [ diff --git a/react-native.config.js b/react-native.config.js index 29f627a4d..b027a7379 100644 --- a/react-native.config.js +++ b/react-native.config.js @@ -3,7 +3,7 @@ module.exports = { platforms: { android: { packageInstance: - "new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)" + "CodePush.getInstance(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)" } } }