1
1
package com .microsoft .codepush .react ;
2
2
3
- import com .facebook .react .ReactPackage ;
3
+ import com .facebook .react .* ;
4
4
import com .facebook .react .bridge .JavaScriptModule ;
5
5
import com .facebook .react .bridge .LifecycleEventListener ;
6
6
import com .facebook .react .bridge .NativeModule ;
@@ -54,6 +54,7 @@ public class CodePush {
54
54
private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress" ;
55
55
private final String RESOURCES_BUNDLE = "resources.arsc" ;
56
56
private final String REACT_DEV_BUNDLE_CACHE_FILE_NAME = "ReactNativeDevBundle.js" ;
57
+ private final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime" ;
57
58
58
59
private CodePushPackage codePushPackage ;
59
60
private CodePushReactPackage codePushReactPackage ;
@@ -95,22 +96,31 @@ public ReactPackage getReactPackage() {
95
96
return codePushReactPackage ;
96
97
}
97
98
98
- public String getBundleUrl (String assetsBundleFileName ) {
99
- this .assetsBundleFileName = assetsBundleFileName ;
100
- String binaryJsBundleUrl = ASSETS_BUNDLE_PREFIX + assetsBundleFileName ;
101
- ZipFile applicationFile ;
102
- long binaryResourcesModifiedTime = -1 ;
103
-
99
+ public long getBinaryResourcesModifiedTime () {
104
100
ApplicationInfo ai = null ;
101
+ ZipFile applicationFile = null ;
105
102
try {
106
103
ai = applicationContext .getPackageManager ().getApplicationInfo (applicationContext .getPackageName (), 0 );
107
104
applicationFile = new ZipFile (ai .sourceDir );
108
105
ZipEntry classesDexEntry = applicationFile .getEntry (RESOURCES_BUNDLE );
109
- binaryResourcesModifiedTime = classesDexEntry .getTime ();
110
- applicationFile .close ();
106
+ return classesDexEntry .getTime ();
111
107
} catch (PackageManager .NameNotFoundException | IOException e ) {
112
108
throw new CodePushUnknownException ("Error in getting file information about compiled resources" , e );
109
+ } finally {
110
+ if (applicationFile != null ) {
111
+ try {
112
+ applicationFile .close ();
113
+ } catch (IOException e ) {
114
+ throw new CodePushUnknownException ("Error in closing application file." , e );
115
+ }
116
+ }
113
117
}
118
+ }
119
+
120
+ public String getBundleUrl (String assetsBundleFileName ) {
121
+ this .assetsBundleFileName = assetsBundleFileName ;
122
+ String binaryJsBundleUrl = ASSETS_BUNDLE_PREFIX + assetsBundleFileName ;
123
+ long binaryResourcesModifiedTime = getBinaryResourcesModifiedTime ();
114
124
115
125
try {
116
126
String packageFilePath = codePushPackage .getCurrentPackageBundlePath ();
@@ -119,15 +129,22 @@ public String getBundleUrl(String assetsBundleFileName) {
119
129
return binaryJsBundleUrl ;
120
130
}
121
131
122
- File packageFile = new File (packageFilePath );
123
- if (packageFile .lastModified () < binaryResourcesModifiedTime ) {
132
+ ReadableMap packageMetadata = codePushPackage .getCurrentPackage ();
133
+ // May throw NumberFormatException.
134
+ Long binaryModifiedDateDuringPackageInstall = Long .parseLong (CodePushUtils .tryGetString (packageMetadata , BINARY_MODIFIED_TIME_KEY ));
135
+ if (binaryModifiedDateDuringPackageInstall == binaryResourcesModifiedTime ) {
136
+ return packageFilePath ;
137
+ } else {
124
138
// The binary version is newer.
139
+ Log .d (CODE_PUSH_TAG , "Found a package installed via CodePush that was " +
140
+ "installed under a different binary version, so the JS bundle packaged " +
141
+ "in the binary will be used as the most current package." );
125
142
return binaryJsBundleUrl ;
126
143
}
127
-
128
- return packageFilePath ;
129
144
} catch (IOException e ) {
130
145
throw new CodePushUnknownException ("Error in getting current package bundle path" , e );
146
+ } catch (NumberFormatException e ) {
147
+ throw new CodePushUnknownException ("Error in reading binary modified date from package metadata" , e );
131
148
}
132
149
}
133
150
@@ -235,9 +252,11 @@ private void initializeUpdateAfterRestart() {
235
252
if (updateIsLoading ) {
236
253
// Pending update was initialized, but notifyApplicationReady was not called.
237
254
// Therefore, deduce that it is a broken update and rollback.
255
+ Log .d (CODE_PUSH_TAG , "Update did not finish loading the last time, rolling back to a previous version." );
238
256
rollbackPackage ();
239
257
} else {
240
258
// Clear the React dev bundle cache so that new updates can be loaded.
259
+ if (com .facebook .react .BuildConfig .DEBUG )
241
260
clearReactDevBundleCache ();
242
261
// Mark that we tried to initialize the new update, so that if it crashes,
243
262
// we will know that we need to rollback when the app next starts.
@@ -325,7 +344,9 @@ public void downloadUpdate(final ReadableMap updatePackage, final Promise promis
325
344
@ Override
326
345
protected Void doInBackground (Object [] params ) {
327
346
try {
328
- codePushPackage .downloadPackage (applicationContext , updatePackage , new DownloadProgressCallback () {
347
+ WritableMap mutableUpdatePackage = CodePushUtils .convertReadableMapToWritableMap (updatePackage );
348
+ mutableUpdatePackage .putString (BINARY_MODIFIED_TIME_KEY , "" + getBinaryResourcesModifiedTime ());
349
+ codePushPackage .downloadPackage (applicationContext , mutableUpdatePackage , new DownloadProgressCallback () {
329
350
@ Override
330
351
public void call (DownloadProgress downloadProgress ) {
331
352
getReactApplicationContext ()
0 commit comments