@@ -50,11 +50,11 @@ public class CodePush {
50
50
private final String PENDING_UPDATE_IS_LOADING_KEY = "isLoading" ;
51
51
private final String ASSETS_BUNDLE_PREFIX = "assets://" ;
52
52
private final String CODE_PUSH_PREFERENCES = "CodePush" ;
53
- private final String CODE_PUSH_TAG = "CodePush" ;
54
53
private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress" ;
55
54
private final String RESOURCES_BUNDLE = "resources.arsc" ;
56
55
// 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
57
56
private final String REACT_DEV_BUNDLE_CACHE_FILE_NAME = "ReactNativeDevBundle.js" ;
57
+ private final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime" ;
58
58
59
59
private CodePushPackage codePushPackage ;
60
60
private CodePushReactPackage codePushReactPackage ;
@@ -96,39 +96,55 @@ public ReactPackage getReactPackage() {
96
96
return codePushReactPackage ;
97
97
}
98
98
99
- public String getBundleUrl (String assetsBundleFileName ) {
100
- this .assetsBundleFileName = assetsBundleFileName ;
101
- String binaryJsBundleUrl = ASSETS_BUNDLE_PREFIX + assetsBundleFileName ;
102
- ZipFile applicationFile ;
103
- long binaryResourcesModifiedTime = -1 ;
104
-
99
+ public long getBinaryResourcesModifiedTime () {
105
100
ApplicationInfo ai = null ;
101
+ ZipFile applicationFile = null ;
106
102
try {
107
103
ai = applicationContext .getPackageManager ().getApplicationInfo (applicationContext .getPackageName (), 0 );
108
104
applicationFile = new ZipFile (ai .sourceDir );
109
105
ZipEntry classesDexEntry = applicationFile .getEntry (RESOURCES_BUNDLE );
110
- binaryResourcesModifiedTime = classesDexEntry .getTime ();
111
- applicationFile .close ();
106
+ return classesDexEntry .getTime ();
112
107
} catch (PackageManager .NameNotFoundException | IOException e ) {
113
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
+ }
114
117
}
118
+ }
119
+
120
+ public String getBundleUrl (String assetsBundleFileName ) {
121
+ this .assetsBundleFileName = assetsBundleFileName ;
122
+ String binaryJsBundleUrl = ASSETS_BUNDLE_PREFIX + assetsBundleFileName ;
123
+ long binaryResourcesModifiedTime = getBinaryResourcesModifiedTime ();
115
124
116
125
try {
117
126
String packageFilePath = codePushPackage .getCurrentPackageBundlePath ();
118
127
if (packageFilePath == null ) {
119
128
// There has not been any downloaded updates.
129
+ CodePushUtils .logBundleUrl (binaryJsBundleUrl );
120
130
return binaryJsBundleUrl ;
121
131
}
122
132
123
- File packageFile = new File (packageFilePath );
124
- if (packageFile .lastModified () < binaryResourcesModifiedTime ) {
133
+ ReadableMap packageMetadata = codePushPackage .getCurrentPackage ();
134
+ // May throw NumberFormatException.
135
+ Long binaryModifiedDateDuringPackageInstall = Long .parseLong (CodePushUtils .tryGetString (packageMetadata , BINARY_MODIFIED_TIME_KEY ));
136
+ if (binaryModifiedDateDuringPackageInstall == binaryResourcesModifiedTime ) {
137
+ CodePushUtils .logBundleUrl (packageFilePath );
138
+ return packageFilePath ;
139
+ } else {
125
140
// The binary version is newer.
141
+ CodePushUtils .logBundleUrl (binaryJsBundleUrl );
126
142
return binaryJsBundleUrl ;
127
143
}
128
-
129
- return packageFilePath ;
130
144
} catch (IOException e ) {
131
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 );
132
148
}
133
149
}
134
150
@@ -221,8 +237,8 @@ private JSONObject getPendingUpdate() {
221
237
return pendingUpdate ;
222
238
} catch (JSONException e ) {
223
239
// Should not happen.
224
- Log . e ( CODE_PUSH_TAG , "Unable to parse pending update metadata " +
225
- pendingUpdateString + " stored in SharedPreferences" , e );
240
+ CodePushUtils . log ( "Unable to parse pending update metadata " + pendingUpdateString +
241
+ " stored in SharedPreferences" );
226
242
return null ;
227
243
}
228
244
}
@@ -236,6 +252,7 @@ private void initializeUpdateAfterRestart() {
236
252
if (updateIsLoading ) {
237
253
// Pending update was initialized, but notifyApplicationReady was not called.
238
254
// Therefore, deduce that it is a broken update and rollback.
255
+ CodePushUtils .log ("Update did not finish loading the last time, rolling back to a previous version." );
239
256
rollbackPackage ();
240
257
} else {
241
258
// Clear the React dev bundle cache so that new updates can be loaded.
@@ -326,7 +343,9 @@ public void downloadUpdate(final ReadableMap updatePackage, final Promise promis
326
343
@ Override
327
344
protected Void doInBackground (Object [] params ) {
328
345
try {
329
- codePushPackage .downloadPackage (applicationContext , updatePackage , new DownloadProgressCallback () {
346
+ WritableMap mutableUpdatePackage = CodePushUtils .convertReadableMapToWritableMap (updatePackage );
347
+ mutableUpdatePackage .putString (BINARY_MODIFIED_TIME_KEY , "" + getBinaryResourcesModifiedTime ());
348
+ codePushPackage .downloadPackage (applicationContext , mutableUpdatePackage , new DownloadProgressCallback () {
330
349
@ Override
331
350
public void call (DownloadProgress downloadProgress ) {
332
351
getReactApplicationContext ()
0 commit comments