Skip to content

Commit ee01e7b

Browse files
committed
[android] Move isLiveReloadEnabled
1 parent 926e018 commit ee01e7b

2 files changed

Lines changed: 36 additions & 33 deletions

File tree

android/app/src/main/java/com/appzung/codepush/react/CodePush.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import com.facebook.react.ReactPackage;
99
import com.facebook.react.bridge.NativeModule;
1010
import com.facebook.react.bridge.ReactApplicationContext;
11-
import com.facebook.react.devsupport.interfaces.DevSupportManager;
12-
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
1311
import com.facebook.react.uimanager.ViewManager;
1412

1513
import org.json.JSONException;
@@ -18,7 +16,6 @@
1816
import java.io.File;
1917
import java.util.ArrayList;
2018
import java.util.List;
21-
import java.lang.reflect.Method;
2219

2320
public class CodePush implements ReactPackage {
2421

@@ -93,7 +90,7 @@ public CodePush(Context context) {
9390
mServerUrl = serverUrlFromStrings;
9491
}
9592

96-
clearDebugCacheIfNeeded(null);
93+
clearDebugCacheIfNeeded(false);
9794
initializeUpdateAfterRestart();
9895
}
9996

@@ -116,31 +113,9 @@ private String getCustomPropertyFromStringsIfExist(String propertyName) {
116113
return null;
117114
}
118115

119-
private boolean isLiveReloadEnabled(ReactInstanceManager instanceManager) {
120-
// Use instanceManager for checking if we use LiveReload mode. In this case we should not remove ReactNativeDevBundle.js file
121-
// because we get error with trying to get this after reloading. Issue: https://github.com/microsoft/react-native-code-push/issues/1272
122-
if (instanceManager != null) {
123-
DevSupportManager devSupportManager = instanceManager.getDevSupportManager();
124-
if (devSupportManager != null) {
125-
DeveloperSettings devSettings = devSupportManager.getDevSettings();
126-
Method[] methods = devSettings.getClass().getMethods();
127-
for (Method m : methods) {
128-
if (m.getName().equals("isReloadOnJSChangeEnabled")) {
129-
try {
130-
return (boolean) m.invoke(devSettings);
131-
} catch (Exception x) {
132-
return false;
133-
}
134-
}
135-
}
136-
}
137-
}
138-
139-
return false;
140-
}
141-
142-
public void clearDebugCacheIfNeeded(ReactInstanceManager instanceManager) {
143-
if (mIsDebugMode && mSettingsManager.isPendingUpdate(null) && !isLiveReloadEnabled(instanceManager)) {
116+
public void clearDebugCacheIfNeeded(boolean isLiveReloadEnabled) {
117+
// Issue: https://github.com/microsoft/react-native-code-push/issues/1272
118+
if (mIsDebugMode && mSettingsManager.isPendingUpdate(null) && !isLiveReloadEnabled) {
144119
// This needs to be kept in sync with https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java#L78
145120
File cachedDevBundle = new File(mContext.getFilesDir(), "ReactNativeDevBundle.js");
146121
if (cachedDevBundle.exists()) {

android/app/src/main/java/com/appzung/codepush/react/CodePushNativeModule.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.os.AsyncTask;
66
import android.os.Handler;
77
import android.os.Looper;
8-
import android.provider.Settings;
98
import android.view.View;
109

1110
import com.facebook.react.ReactApplication;
@@ -16,20 +15,22 @@
1615
import com.facebook.react.bridge.LifecycleEventListener;
1716
import com.facebook.react.bridge.Promise;
1817
import com.facebook.react.bridge.ReactApplicationContext;
19-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
2018
import com.facebook.react.bridge.ReactMethod;
2119
import com.facebook.react.bridge.ReadableMap;
2220
import com.facebook.react.bridge.WritableMap;
21+
import com.facebook.react.devsupport.interfaces.DevSupportManager;
2322
import com.facebook.react.modules.core.ChoreographerCompat;
2423
import com.facebook.react.modules.core.DeviceEventManagerModule;
2524
import com.facebook.react.modules.core.ReactChoreographer;
25+
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
2626

2727
import org.json.JSONArray;
2828
import org.json.JSONException;
2929
import org.json.JSONObject;
3030

3131
import java.io.IOException;
3232
import java.lang.reflect.Field;
33+
import java.lang.reflect.Method;
3334
import java.util.ArrayList;
3435
import java.util.Date;
3536
import java.util.HashMap;
@@ -132,10 +133,17 @@ private void setJSBundle(ReactInstanceManager instanceManager, String latestJSBu
132133
private void loadBundle() {
133134
clearLifecycleEventListener();
134135
try {
135-
mCodePush.clearDebugCacheIfNeeded(resolveInstanceManager());
136+
DevSupportManager devSupportManager = null;
137+
ReactInstanceManager reactInstanceManager = resolveInstanceManager();
138+
if (reactInstanceManager != null) {
139+
devSupportManager = reactInstanceManager.getDevSupportManager();
140+
}
141+
boolean isLiveReloadEnabled = isLiveReloadEnabled(devSupportManager);
142+
143+
mCodePush.clearDebugCacheIfNeeded(isLiveReloadEnabled);
136144
} catch(Exception e) {
137145
// If we got error in out reflection we should clear debug cache anyway.
138-
mCodePush.clearDebugCacheIfNeeded(null);
146+
mCodePush.clearDebugCacheIfNeeded(false);
139147
}
140148

141149
try {
@@ -179,6 +187,26 @@ public void run() {
179187
}
180188
}
181189

190+
private boolean isLiveReloadEnabled(DevSupportManager devSupportManager) {
191+
if (devSupportManager == null) {
192+
return false;
193+
}
194+
195+
DeveloperSettings devSettings = devSupportManager.getDevSettings();
196+
Method[] methods = devSettings.getClass().getMethods();
197+
for (Method m : methods) {
198+
if (m.getName().equals("isReloadOnJSChangeEnabled")) {
199+
try {
200+
return (boolean) m.invoke(devSettings);
201+
} catch (Exception x) {
202+
return false;
203+
}
204+
}
205+
}
206+
207+
return false;
208+
}
209+
182210
// This workaround has been implemented in order to fix https://github.com/facebook/react-native/issues/14533
183211
// resetReactRootViews allows to call recreateReactContextInBackground without any exceptions
184212
// This fix also relates to https://github.com/microsoft/react-native-code-push/issues/878

0 commit comments

Comments
 (0)