5
5
import android .content .pm .PackageManager ;
6
6
import android .content .res .Resources ;
7
7
8
+ import com .facebook .react .ReactHost ;
8
9
import com .facebook .react .ReactInstanceManager ;
9
10
import com .facebook .react .ReactPackage ;
10
11
import com .facebook .react .bridge .JavaScriptModule ;
11
12
import com .facebook .react .bridge .NativeModule ;
12
13
import com .facebook .react .bridge .ReactApplicationContext ;
13
- import com .facebook .react .devsupport .interfaces .DevSupportManager ;
14
- import com .facebook .react .modules .debug .interfaces .DeveloperSettings ;
15
14
import com .facebook .react .uimanager .ViewManager ;
16
15
17
16
import org .json .JSONException ;
20
19
import java .io .File ;
21
20
import java .util .ArrayList ;
22
21
import java .util .List ;
23
- import java .lang .reflect .Method ;
24
22
25
23
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
+ }
26
36
27
37
private static boolean sIsRunningBinaryVersion = false ;
28
38
private static boolean sNeedToReportRollback = false ;
@@ -40,15 +50,16 @@ public class CodePush implements ReactPackage {
40
50
41
51
// Config properties.
42
52
private String mDeploymentKey ;
43
- private static String mServerUrl = "https://codepush.appcenter.ms /" ;
53
+ private static String mServerUrl = "https://api.revopush.org /" ;
44
54
45
55
private Context mContext ;
46
56
private final boolean mIsDebugMode ;
47
57
48
58
private static String mPublicKey ;
49
59
50
60
private static ReactInstanceHolder mReactInstanceHolder ;
51
- private static CodePush mCurrentInstance ;
61
+
62
+ private static ReactHostHolder mReactHostHolder ;
52
63
53
64
public CodePush (String deploymentKey , Context context ) {
54
65
this (deploymentKey , context , false );
@@ -58,7 +69,7 @@ public static String getServiceUrl() {
58
69
return mServerUrl ;
59
70
}
60
71
61
- public CodePush (String deploymentKey , Context context , boolean isDebugMode ) {
72
+ private CodePush (String deploymentKey , Context context , boolean isDebugMode ) {
62
73
mContext = context .getApplicationContext ();
63
74
64
75
mUpdateManager = new CodePushUpdateManager (context .getFilesDir ().getAbsolutePath ());
@@ -84,22 +95,23 @@ public CodePush(String deploymentKey, Context context, boolean isDebugMode) {
84
95
String serverUrlFromStrings = getCustomPropertyFromStringsIfExist ("ServerUrl" );
85
96
if (serverUrlFromStrings != null ) mServerUrl = serverUrlFromStrings ;
86
97
87
- clearDebugCacheIfNeeded (null );
98
+ // ignore liveReload when CodePush is initializing so that unneccessary cache could be cleared
99
+ clearDebugCacheIfNeeded (false );
88
100
initializeUpdateAfterRestart ();
89
101
}
90
102
91
- public CodePush (String deploymentKey , Context context , boolean isDebugMode , String serverUrl ) {
103
+ private CodePush (String deploymentKey , Context context , boolean isDebugMode , String serverUrl ) {
92
104
this (deploymentKey , context , isDebugMode );
93
105
mServerUrl = serverUrl ;
94
106
}
95
107
96
- public CodePush (String deploymentKey , Context context , boolean isDebugMode , int publicKeyResourceDescriptor ) {
108
+ private CodePush (String deploymentKey , Context context , boolean isDebugMode , int publicKeyResourceDescriptor ) {
97
109
this (deploymentKey , context , isDebugMode );
98
110
99
111
mPublicKey = getPublicKeyByResourceDescriptor (publicKeyResourceDescriptor );
100
112
}
101
113
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 ) {
103
115
this (deploymentKey , context , isDebugMode );
104
116
105
117
if (publicKeyResourceDescriptor != null ) {
@@ -129,48 +141,27 @@ private String getPublicKeyByResourceDescriptor(int publicKeyResourceDescriptor)
129
141
130
142
private String getCustomPropertyFromStringsIfExist (String propertyName ) {
131
143
String property ;
132
-
144
+
133
145
String packageName = mContext .getPackageName ();
134
146
int resId = mContext .getResources ().getIdentifier ("CodePush" + propertyName , "string" , packageName );
135
-
147
+
136
148
if (resId != 0 ) {
137
149
property = mContext .getString (resId );
138
150
139
151
if (!property .isEmpty ()) {
140
152
return property ;
141
153
} else {
142
154
CodePushUtils .log ("Specified " + propertyName + " is empty" );
143
- }
155
+ }
144
156
}
145
157
146
158
return null ;
147
159
}
148
160
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
151
163
// 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 ) {
174
165
// 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
175
166
File cachedDevBundle = new File (mContext .getFilesDir (), "ReactNativeDevBundle.js" );
176
167
if (cachedDevBundle .exists ()) {
@@ -411,13 +402,25 @@ public static void setReactInstanceHolder(ReactInstanceHolder reactInstanceHolde
411
402
mReactInstanceHolder = reactInstanceHolder ;
412
403
}
413
404
405
+ public static void setReactHost (ReactHostHolder reactHostHolder ) {
406
+ mReactHostHolder = reactHostHolder ;
407
+ }
408
+
414
409
static ReactInstanceManager getReactInstanceManager () {
415
410
if (mReactInstanceHolder == null ) {
416
411
return null ;
417
412
}
418
413
return mReactInstanceHolder .getReactInstanceManager ();
419
414
}
420
415
416
+ static ReactHost getReactHost () {
417
+ if (mReactHostHolder == null ) {
418
+ return null ;
419
+ }
420
+
421
+ return mReactHostHolder .getReactHost ();
422
+ }
423
+
421
424
@ Override
422
425
public List <NativeModule > createNativeModules (ReactApplicationContext reactApplicationContext ) {
423
426
CodePushNativeModule codePushModule = new CodePushNativeModule (reactApplicationContext , this , mUpdateManager , mTelemetryManager , mSettingsManager );
0 commit comments