Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 2eddfd2

Browse files
committed
fix-date-comparison-bug
1 parent 9b9afb6 commit 2eddfd2

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

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

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.microsoft.codepush.react;
22

3-
import com.facebook.react.ReactPackage;
3+
import com.facebook.react.*;
44
import com.facebook.react.bridge.JavaScriptModule;
55
import com.facebook.react.bridge.LifecycleEventListener;
66
import com.facebook.react.bridge.NativeModule;
@@ -54,6 +54,7 @@ public class CodePush {
5454
private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress";
5555
private final String RESOURCES_BUNDLE = "resources.arsc";
5656
private final String REACT_DEV_BUNDLE_CACHE_FILE_NAME = "ReactNativeDevBundle.js";
57+
private final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime";
5758

5859
private CodePushPackage codePushPackage;
5960
private CodePushReactPackage codePushReactPackage;
@@ -95,22 +96,31 @@ public ReactPackage getReactPackage() {
9596
return codePushReactPackage;
9697
}
9798

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() {
104100
ApplicationInfo ai = null;
101+
ZipFile applicationFile = null;
105102
try {
106103
ai = applicationContext.getPackageManager().getApplicationInfo(applicationContext.getPackageName(), 0);
107104
applicationFile = new ZipFile(ai.sourceDir);
108105
ZipEntry classesDexEntry = applicationFile.getEntry(RESOURCES_BUNDLE);
109-
binaryResourcesModifiedTime = classesDexEntry.getTime();
110-
applicationFile.close();
106+
return classesDexEntry.getTime();
111107
} catch (PackageManager.NameNotFoundException | IOException e) {
112108
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+
}
113117
}
118+
}
119+
120+
public String getBundleUrl(String assetsBundleFileName) {
121+
this.assetsBundleFileName = assetsBundleFileName;
122+
String binaryJsBundleUrl = ASSETS_BUNDLE_PREFIX + assetsBundleFileName;
123+
long binaryResourcesModifiedTime = getBinaryResourcesModifiedTime();
114124

115125
try {
116126
String packageFilePath = codePushPackage.getCurrentPackageBundlePath();
@@ -119,15 +129,22 @@ public String getBundleUrl(String assetsBundleFileName) {
119129
return binaryJsBundleUrl;
120130
}
121131

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 {
124138
// 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.");
125142
return binaryJsBundleUrl;
126143
}
127-
128-
return packageFilePath;
129144
} catch (IOException e) {
130145
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);
131148
}
132149
}
133150

@@ -235,9 +252,11 @@ private void initializeUpdateAfterRestart() {
235252
if (updateIsLoading) {
236253
// Pending update was initialized, but notifyApplicationReady was not called.
237254
// 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.");
238256
rollbackPackage();
239257
} else {
240258
// Clear the React dev bundle cache so that new updates can be loaded.
259+
if (com.facebook.react.BuildConfig.DEBUG)
241260
clearReactDevBundleCache();
242261
// Mark that we tried to initialize the new update, so that if it crashes,
243262
// 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
325344
@Override
326345
protected Void doInBackground(Object[] params) {
327346
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() {
329350
@Override
330351
public void call(DownloadProgress downloadProgress) {
331352
getReactApplicationContext()

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ public static JSONArray convertReadableToJsonArray(ReadableArray arr) {
248248
return jsonArr;
249249
}
250250

251+
public static WritableMap convertReadableMapToWritableMap(ReadableMap map) {
252+
JSONObject mapJSON = convertReadableToJsonObject(map);
253+
return convertJsonObjectToWriteable(mapJSON);
254+
}
255+
251256
public static String tryGetString(ReadableMap map, String key) {
252257
try {
253258
return map.getString(key);

0 commit comments

Comments
 (0)