Skip to content

Commit a2c04ac

Browse files
committed
[android] Move isLiveReloadEnabled
1 parent e1b756b commit a2c04ac

2 files changed

Lines changed: 36 additions & 32 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 & 3 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;
@@ -20,16 +19,19 @@
2019
import com.facebook.react.bridge.ReactMethod;
2120
import com.facebook.react.bridge.ReadableMap;
2221
import com.facebook.react.bridge.WritableMap;
22+
import com.facebook.react.devsupport.interfaces.DevSupportManager;
2323
import com.facebook.react.modules.core.ChoreographerCompat;
2424
import com.facebook.react.modules.core.DeviceEventManagerModule;
2525
import com.facebook.react.modules.core.ReactChoreographer;
26+
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
2627

2728
import org.json.JSONArray;
2829
import org.json.JSONException;
2930
import org.json.JSONObject;
3031

3132
import java.io.IOException;
3233
import java.lang.reflect.Field;
34+
import java.lang.reflect.Method;
3335
import java.util.ArrayList;
3436
import java.util.Date;
3537
import java.util.HashMap;
@@ -132,10 +134,17 @@ private void setJSBundle(ReactInstanceManager instanceManager, String latestJSBu
132134
private void loadBundle() {
133135
clearLifecycleEventListener();
134136
try {
135-
mCodePush.clearDebugCacheIfNeeded(resolveInstanceManager());
137+
DevSupportManager devSupportManager = null;
138+
ReactInstanceManager reactInstanceManager = resolveInstanceManager();
139+
if (reactInstanceManager != null) {
140+
devSupportManager = reactInstanceManager.getDevSupportManager();
141+
}
142+
boolean isLiveReloadEnabled = isLiveReloadEnabled(devSupportManager);
143+
144+
mCodePush.clearDebugCacheIfNeeded(isLiveReloadEnabled);
136145
} catch(Exception e) {
137146
// If we got error in out reflection we should clear debug cache anyway.
138-
mCodePush.clearDebugCacheIfNeeded(null);
147+
mCodePush.clearDebugCacheIfNeeded(false);
139148
}
140149

141150
try {
@@ -179,6 +188,26 @@ public void run() {
179188
}
180189
}
181190

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

0 commit comments

Comments
 (0)