Skip to content

Commit f18ff0d

Browse files
committed
Refactor CodePush class to improve singleton implementation and update server URL
1 parent 9d17937 commit f18ff0d

1 file changed

Lines changed: 40 additions & 37 deletions

File tree

  • android/app/src/main/java/com/microsoft/codepush/react

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

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import android.content.pm.PackageManager;
66
import android.content.res.Resources;
77

8+
import com.facebook.react.ReactHost;
89
import com.facebook.react.ReactInstanceManager;
910
import com.facebook.react.ReactPackage;
1011
import com.facebook.react.bridge.JavaScriptModule;
1112
import com.facebook.react.bridge.NativeModule;
1213
import com.facebook.react.bridge.ReactApplicationContext;
13-
import com.facebook.react.devsupport.interfaces.DevSupportManager;
14-
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
1514
import com.facebook.react.uimanager.ViewManager;
1615

1716
import org.json.JSONException;
@@ -20,9 +19,20 @@
2019
import java.io.File;
2120
import java.util.ArrayList;
2221
import java.util.List;
23-
import java.lang.reflect.Method;
2422

2523
public class CodePush implements ReactPackage {
24+
private static final Object LOCK = new Object();
25+
private static volatile CodePush mCurrentInstance;
26+
public static CodePush getInstance(String deploymentKey, Context context, boolean isDebugMode) {
27+
if (mCurrentInstance == null) {
28+
synchronized (LOCK) {
29+
if (mCurrentInstance == null) {
30+
mCurrentInstance = new CodePush(deploymentKey, context, isDebugMode);
31+
}
32+
}
33+
}
34+
return mCurrentInstance;
35+
}
2636

2737
private static boolean sIsRunningBinaryVersion = false;
2838
private static boolean sNeedToReportRollback = false;
@@ -40,15 +50,16 @@ public class CodePush implements ReactPackage {
4050

4151
// Config properties.
4252
private String mDeploymentKey;
43-
private static String mServerUrl = "https://codepush.appcenter.ms/";
53+
private static String mServerUrl = "https://api.srcpush.com/";
4454

4555
private Context mContext;
4656
private final boolean mIsDebugMode;
4757

4858
private static String mPublicKey;
4959

5060
private static ReactInstanceHolder mReactInstanceHolder;
51-
private static CodePush mCurrentInstance;
61+
62+
private static ReactHostHolder mReactHostHolder;
5263

5364
public CodePush(String deploymentKey, Context context) {
5465
this(deploymentKey, context, false);
@@ -58,7 +69,7 @@ public static String getServiceUrl() {
5869
return mServerUrl;
5970
}
6071

61-
public CodePush(String deploymentKey, Context context, boolean isDebugMode) {
72+
private CodePush(String deploymentKey, Context context, boolean isDebugMode) {
6273
mContext = context.getApplicationContext();
6374

6475
mUpdateManager = new CodePushUpdateManager(context.getFilesDir().getAbsolutePath());
@@ -84,22 +95,23 @@ public CodePush(String deploymentKey, Context context, boolean isDebugMode) {
8495
String serverUrlFromStrings = getCustomPropertyFromStringsIfExist("ServerUrl");
8596
if (serverUrlFromStrings != null) mServerUrl = serverUrlFromStrings;
8697

87-
clearDebugCacheIfNeeded(null);
98+
// ignore liveReload when CodePush is initializing so that unneccessary cache could be cleared
99+
clearDebugCacheIfNeeded(false);
88100
initializeUpdateAfterRestart();
89101
}
90102

91-
public CodePush(String deploymentKey, Context context, boolean isDebugMode, String serverUrl) {
103+
private CodePush(String deploymentKey, Context context, boolean isDebugMode, String serverUrl) {
92104
this(deploymentKey, context, isDebugMode);
93105
mServerUrl = serverUrl;
94106
}
95107

96-
public CodePush(String deploymentKey, Context context, boolean isDebugMode, int publicKeyResourceDescriptor) {
108+
private CodePush(String deploymentKey, Context context, boolean isDebugMode, int publicKeyResourceDescriptor) {
97109
this(deploymentKey, context, isDebugMode);
98110

99111
mPublicKey = getPublicKeyByResourceDescriptor(publicKeyResourceDescriptor);
100112
}
101113

102-
public CodePush(String deploymentKey, Context context, boolean isDebugMode, String serverUrl, Integer publicKeyResourceDescriptor) {
114+
private CodePush(String deploymentKey, Context context, boolean isDebugMode, String serverUrl, Integer publicKeyResourceDescriptor) {
103115
this(deploymentKey, context, isDebugMode);
104116

105117
if (publicKeyResourceDescriptor != null) {
@@ -129,48 +141,27 @@ private String getPublicKeyByResourceDescriptor(int publicKeyResourceDescriptor)
129141

130142
private String getCustomPropertyFromStringsIfExist(String propertyName) {
131143
String property;
132-
144+
133145
String packageName = mContext.getPackageName();
134146
int resId = mContext.getResources().getIdentifier("CodePush" + propertyName, "string", packageName);
135-
147+
136148
if (resId != 0) {
137149
property = mContext.getString(resId);
138150

139151
if (!property.isEmpty()) {
140152
return property;
141153
} else {
142154
CodePushUtils.log("Specified " + propertyName + " is empty");
143-
}
155+
}
144156
}
145157

146158
return null;
147159
}
148160

149-
private boolean isLiveReloadEnabled(ReactInstanceManager instanceManager) {
150-
// Use instanceManager for checking if we use LiveReload mode. In this case we should not remove ReactNativeDevBundle.js file
161+
public void clearDebugCacheIfNeeded(boolean isLiveReloadEnabled) {
162+
// for checking if we use LiveReload mode. In this case we should not remove ReactNativeDevBundle.js file
151163
// because we get error with trying to get this after reloading. Issue: https://github.com/microsoft/react-native-code-push/issues/1272
152-
if (instanceManager != null) {
153-
DevSupportManager devSupportManager = instanceManager.getDevSupportManager();
154-
if (devSupportManager != null) {
155-
DeveloperSettings devSettings = devSupportManager.getDevSettings();
156-
Method[] methods = devSettings.getClass().getMethods();
157-
for (Method m : methods) {
158-
if (m.getName().equals("isReloadOnJSChangeEnabled")) {
159-
try {
160-
return (boolean) m.invoke(devSettings);
161-
} catch (Exception x) {
162-
return false;
163-
}
164-
}
165-
}
166-
}
167-
}
168-
169-
return false;
170-
}
171-
172-
public void clearDebugCacheIfNeeded(ReactInstanceManager instanceManager) {
173-
if (mIsDebugMode && mSettingsManager.isPendingUpdate(null) && !isLiveReloadEnabled(instanceManager)) {
164+
if (mIsDebugMode && mSettingsManager.isPendingUpdate(null) && !isLiveReloadEnabled) {
174165
// 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
175166
File cachedDevBundle = new File(mContext.getFilesDir(), "ReactNativeDevBundle.js");
176167
if (cachedDevBundle.exists()) {
@@ -411,13 +402,25 @@ public static void setReactInstanceHolder(ReactInstanceHolder reactInstanceHolde
411402
mReactInstanceHolder = reactInstanceHolder;
412403
}
413404

405+
public static void setReactHost(ReactHostHolder reactHostHolder) {
406+
mReactHostHolder = reactHostHolder;
407+
}
408+
414409
static ReactInstanceManager getReactInstanceManager() {
415410
if (mReactInstanceHolder == null) {
416411
return null;
417412
}
418413
return mReactInstanceHolder.getReactInstanceManager();
419414
}
420415

416+
static ReactHost getReactHost() {
417+
if (mReactHostHolder == null) {
418+
return null;
419+
}
420+
421+
return mReactHostHolder.getReactHost();
422+
}
423+
421424
@Override
422425
public List<NativeModule> createNativeModules(ReactApplicationContext reactApplicationContext) {
423426
CodePushNativeModule codePushModule = new CodePushNativeModule(reactApplicationContext, this, mUpdateManager, mTelemetryManager, mSettingsManager);

0 commit comments

Comments
 (0)