@@ -50,11 +50,11 @@ public class CodePush {
5050 private final String PENDING_UPDATE_IS_LOADING_KEY = "isLoading" ;
5151 private final String ASSETS_BUNDLE_PREFIX = "assets://" ;
5252 private final String CODE_PUSH_PREFERENCES = "CodePush" ;
53- private final String CODE_PUSH_TAG = "CodePush" ;
5453 private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress" ;
5554 private final String RESOURCES_BUNDLE = "resources.arsc" ;
5655 // 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
5756 private final String REACT_DEV_BUNDLE_CACHE_FILE_NAME = "ReactNativeDevBundle.js" ;
57+ private final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime" ;
5858
5959 private CodePushPackage codePushPackage ;
6060 private CodePushReactPackage codePushReactPackage ;
@@ -96,39 +96,55 @@ public ReactPackage getReactPackage() {
9696 return codePushReactPackage ;
9797 }
9898
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 () {
105100 ApplicationInfo ai = null ;
101+ ZipFile applicationFile = null ;
106102 try {
107103 ai = applicationContext .getPackageManager ().getApplicationInfo (applicationContext .getPackageName (), 0 );
108104 applicationFile = new ZipFile (ai .sourceDir );
109105 ZipEntry classesDexEntry = applicationFile .getEntry (RESOURCES_BUNDLE );
110- binaryResourcesModifiedTime = classesDexEntry .getTime ();
111- applicationFile .close ();
106+ return classesDexEntry .getTime ();
112107 } catch (PackageManager .NameNotFoundException | IOException e ) {
113108 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+ }
114117 }
118+ }
119+
120+ public String getBundleUrl (String assetsBundleFileName ) {
121+ this .assetsBundleFileName = assetsBundleFileName ;
122+ String binaryJsBundleUrl = ASSETS_BUNDLE_PREFIX + assetsBundleFileName ;
123+ long binaryResourcesModifiedTime = getBinaryResourcesModifiedTime ();
115124
116125 try {
117126 String packageFilePath = codePushPackage .getCurrentPackageBundlePath ();
118127 if (packageFilePath == null ) {
119128 // There has not been any downloaded updates.
129+ CodePushUtils .logBundleUrl (binaryJsBundleUrl );
120130 return binaryJsBundleUrl ;
121131 }
122132
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 {
125140 // The binary version is newer.
141+ CodePushUtils .logBundleUrl (binaryJsBundleUrl );
126142 return binaryJsBundleUrl ;
127143 }
128-
129- return packageFilePath ;
130144 } catch (IOException e ) {
131145 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 );
132148 }
133149 }
134150
@@ -221,8 +237,8 @@ private JSONObject getPendingUpdate() {
221237 return pendingUpdate ;
222238 } catch (JSONException e ) {
223239 // 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" );
226242 return null ;
227243 }
228244 }
@@ -236,6 +252,7 @@ private void initializeUpdateAfterRestart() {
236252 if (updateIsLoading ) {
237253 // Pending update was initialized, but notifyApplicationReady was not called.
238254 // 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." );
239256 rollbackPackage ();
240257 } else {
241258 // 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
326343 @ Override
327344 protected Void doInBackground (Object [] params ) {
328345 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 () {
330349 @ Override
331350 public void call (DownloadProgress downloadProgress ) {
332351 getReactApplicationContext ()
0 commit comments