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

Commit 0021fd5

Browse files
committed
Merge pull request #108 from Microsoft/fix-date-comparison-bug
Fix date comparison bug
2 parents 931bb2a + 61f83fd commit 0021fd5

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

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

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.microsoft.codepush.react;
22

3+
import android.util.Log;
4+
35
import com.facebook.react.bridge.Arguments;
46
import com.facebook.react.bridge.NoSuchKeyException;
57
import com.facebook.react.bridge.ReadableArray;
@@ -23,6 +25,8 @@
2325

2426
public class CodePushUtils {
2527

28+
public static final String REACT_NATIVE_LOG_TAG = "ReactNative";
29+
2630
public static String appendPathComponent(String basePath, String appendPathComponent) {
2731
return new File(basePath, appendPathComponent).getAbsolutePath();
2832
}
@@ -248,11 +252,24 @@ public static JSONArray convertReadableToJsonArray(ReadableArray arr) {
248252
return jsonArr;
249253
}
250254

255+
public static WritableMap convertReadableMapToWritableMap(ReadableMap map) {
256+
JSONObject mapJSON = convertReadableToJsonObject(map);
257+
return convertJsonObjectToWriteable(mapJSON);
258+
}
259+
251260
public static String tryGetString(ReadableMap map, String key) {
252261
try {
253262
return map.getString(key);
254263
} catch (NoSuchKeyException e) {
255264
return null;
256265
}
257266
}
267+
268+
public static void log(String message) {
269+
Log.d(REACT_NATIVE_LOG_TAG, "[CodePush] " + message);
270+
}
271+
272+
public static void logBundleUrl(String path) {
273+
log("Loading JS bundle from \"" + path + "\"");
274+
}
258275
}

0 commit comments

Comments
 (0)