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

Commit e6936b3

Browse files
AndrewJacksergey-akhalkov
authored andcommitted
Prevent Rollback crash when binary was updated. (#924)
* Prevent Rollback crash when binary was updated. * Self review
1 parent 85e094f commit e6936b3

File tree

1 file changed

+50
-36
lines changed
  • android/app/src/main/java/com/microsoft/codepush/react

1 file changed

+50
-36
lines changed

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

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ long getBinaryResourcesModifiedTime() {
105105
int codePushApkBuildTimeId = this.mContext.getResources().getIdentifier(CodePushConstants.CODE_PUSH_APK_BUILD_TIME_KEY, "string", packageName);
106106
String codePushApkBuildTime = this.mContext.getResources().getString(codePushApkBuildTimeId);
107107
return Long.parseLong(codePushApkBuildTime);
108-
} catch (Exception e) {
108+
} catch (Exception e) {
109109
throw new CodePushUnknownException("Error in getting binary resources modified time", e);
110110
}
111111
}
@@ -143,44 +143,30 @@ public static String getJSBundleFile(String assetsBundleFileName) {
143143
public String getJSBundleFileInternal(String assetsBundleFileName) {
144144
this.mAssetsBundleFileName = assetsBundleFileName;
145145
String binaryJsBundleUrl = CodePushConstants.ASSETS_BUNDLE_PREFIX + assetsBundleFileName;
146-
long binaryResourcesModifiedTime = this.getBinaryResourcesModifiedTime();
147146

148-
try {
149-
String packageFilePath = mUpdateManager.getCurrentPackageBundlePath(this.mAssetsBundleFileName);
150-
if (packageFilePath == null) {
151-
// There has not been any downloaded updates.
152-
CodePushUtils.logBundleUrl(binaryJsBundleUrl);
153-
sIsRunningBinaryVersion = true;
154-
return binaryJsBundleUrl;
155-
}
147+
String packageFilePath = mUpdateManager.getCurrentPackageBundlePath(this.mAssetsBundleFileName);
148+
if (packageFilePath == null) {
149+
// There has not been any downloaded updates.
150+
CodePushUtils.logBundleUrl(binaryJsBundleUrl);
151+
sIsRunningBinaryVersion = true;
152+
return binaryJsBundleUrl;
153+
}
156154

157-
JSONObject packageMetadata = this.mUpdateManager.getCurrentPackage();
158-
Long binaryModifiedDateDuringPackageInstall = null;
159-
String binaryModifiedDateDuringPackageInstallString = packageMetadata.optString(CodePushConstants.BINARY_MODIFIED_TIME_KEY, null);
160-
if (binaryModifiedDateDuringPackageInstallString != null) {
161-
binaryModifiedDateDuringPackageInstall = Long.parseLong(binaryModifiedDateDuringPackageInstallString);
155+
JSONObject packageMetadata = this.mUpdateManager.getCurrentPackage();
156+
if (isPackageBundleLatest(packageMetadata)) {
157+
CodePushUtils.logBundleUrl(packageFilePath);
158+
sIsRunningBinaryVersion = false;
159+
return packageFilePath;
160+
} else {
161+
// The binary version is newer.
162+
this.mDidUpdate = false;
163+
if (!this.mIsDebugMode || hasBinaryVersionChanged(packageMetadata)) {
164+
this.clearUpdates();
162165
}
163166

164-
String packageAppVersion = packageMetadata.optString("appVersion", null);
165-
if (binaryModifiedDateDuringPackageInstall != null &&
166-
binaryModifiedDateDuringPackageInstall == binaryResourcesModifiedTime &&
167-
(isUsingTestConfiguration() || sAppVersion.equals(packageAppVersion))) {
168-
CodePushUtils.logBundleUrl(packageFilePath);
169-
sIsRunningBinaryVersion = false;
170-
return packageFilePath;
171-
} else {
172-
// The binary version is newer.
173-
this.mDidUpdate = false;
174-
if (!this.mIsDebugMode || !sAppVersion.equals(packageAppVersion)) {
175-
this.clearUpdates();
176-
}
177-
178-
CodePushUtils.logBundleUrl(binaryJsBundleUrl);
179-
sIsRunningBinaryVersion = true;
180-
return binaryJsBundleUrl;
181-
}
182-
} catch (NumberFormatException e) {
183-
throw new CodePushUnknownException("Error in reading binary modified date from package metadata", e);
167+
CodePushUtils.logBundleUrl(binaryJsBundleUrl);
168+
sIsRunningBinaryVersion = true;
169+
return binaryJsBundleUrl;
184170
}
185171
}
186172

@@ -195,6 +181,12 @@ void initializeUpdateAfterRestart() {
195181

196182
JSONObject pendingUpdate = mSettingsManager.getPendingUpdate();
197183
if (pendingUpdate != null) {
184+
JSONObject packageMetadata = this.mUpdateManager.getCurrentPackage();
185+
if (!isPackageBundleLatest(packageMetadata) && hasBinaryVersionChanged(packageMetadata)) {
186+
CodePushUtils.log("Skipping initializeUpdateAfterRestart(), binary version is newer");
187+
return;
188+
}
189+
198190
try {
199191
boolean updateIsLoading = pendingUpdate.getBoolean(CodePushConstants.PENDING_UPDATE_IS_LOADING_KEY);
200192
if (updateIsLoading) {
@@ -232,6 +224,28 @@ boolean isRunningBinaryVersion() {
232224
return sIsRunningBinaryVersion;
233225
}
234226

227+
private boolean isPackageBundleLatest(JSONObject packageMetadata) {
228+
try {
229+
Long binaryModifiedDateDuringPackageInstall = null;
230+
String binaryModifiedDateDuringPackageInstallString = packageMetadata.optString(CodePushConstants.BINARY_MODIFIED_TIME_KEY, null);
231+
if (binaryModifiedDateDuringPackageInstallString != null) {
232+
binaryModifiedDateDuringPackageInstall = Long.parseLong(binaryModifiedDateDuringPackageInstallString);
233+
}
234+
String packageAppVersion = packageMetadata.optString("appVersion", null);
235+
long binaryResourcesModifiedTime = this.getBinaryResourcesModifiedTime();
236+
return binaryModifiedDateDuringPackageInstall != null &&
237+
binaryModifiedDateDuringPackageInstall == binaryResourcesModifiedTime &&
238+
(isUsingTestConfiguration() || sAppVersion.equals(packageAppVersion));
239+
} catch (NumberFormatException e) {
240+
throw new CodePushUnknownException("Error in reading binary modified date from package metadata", e);
241+
}
242+
}
243+
244+
private boolean hasBinaryVersionChanged(JSONObject packageMetadata) {
245+
String packageAppVersion = packageMetadata.optString("appVersion", null);
246+
return !sAppVersion.equals(packageAppVersion);
247+
}
248+
235249
boolean needToReportRollback() {
236250
return sNeedToReportRollback;
237251
}
@@ -276,7 +290,7 @@ static ReactInstanceManager getReactInstanceManager() {
276290
}
277291
return mReactInstanceHolder.getReactInstanceManager();
278292
}
279-
293+
280294
@Override
281295
public List<NativeModule> createNativeModules(ReactApplicationContext reactApplicationContext) {
282296
CodePushNativeModule codePushModule = new CodePushNativeModule(reactApplicationContext, this, mUpdateManager, mTelemetryManager, mSettingsManager);

0 commit comments

Comments
 (0)