Skip to content

Commit b83ab3e

Browse files
committed
Android: opt-in flag for bsdiff patches
1 parent 3d49b44 commit b83ab3e

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public static String getServiceUrl() {
7070
private CodePush(String deploymentKey, Context context, boolean isDebugMode) {
7171
mContext = context.getApplicationContext();
7272

73-
mUpdateManager = new CodePushUpdateManager(context.getFilesDir().getAbsolutePath());
73+
boolean enableBinaryDiffUpdates = getBooleanCustomPropertyFromStringsIfExist("EnableBinaryDiffUpdates", false);
74+
mUpdateManager = new CodePushUpdateManager(context.getFilesDir().getAbsolutePath(), enableBinaryDiffUpdates);
7475
mTelemetryManager = new CodePushTelemetryManager(mContext);
7576
mDeploymentKey = deploymentKey;
7677
mIsDebugMode = isDebugMode;
@@ -156,6 +157,17 @@ private String getCustomPropertyFromStringsIfExist(String propertyName) {
156157
return null;
157158
}
158159

160+
private boolean getBooleanCustomPropertyFromStringsIfExist(String propertyName, boolean defaultValue) {
161+
String packageName = mContext.getPackageName();
162+
int resId = mContext.getResources().getIdentifier("CodePush" + propertyName, "bool", packageName);
163+
164+
if (resId != 0) {
165+
return mContext.getResources().getBoolean(resId);
166+
}
167+
168+
return defaultValue;
169+
}
170+
159171
public void clearDebugCacheIfNeeded(boolean isLiveReloadEnabled) {
160172
// for checking if we use LiveReload mode. In this case we should not remove ReactNativeDevBundle.js file
161173
// because we get error with trying to get this after reloading. Issue: https://github.com/microsoft/react-native-code-push/issues/1272

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
public class CodePushUpdateManager {
2525

2626
private String mDocumentsDirectory;
27+
private boolean mEnableBinaryDiffUpdates;
2728

28-
public CodePushUpdateManager(String documentsDirectory) {
29+
public CodePushUpdateManager(String documentsDirectory, boolean enableBinaryDiffUpdates) {
2930
mDocumentsDirectory = documentsDirectory;
31+
mEnableBinaryDiffUpdates = enableBinaryDiffUpdates;
3032
}
3133

3234
private String getDownloadFilePath() {
@@ -260,12 +262,14 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
260262
if (isDiffUpdate) {
261263
// Run patching after copyNecessaryFilesFromCurrentPackage() so patched output overwrites
262264
// bytes copied in from the old package at the same paths.
263-
if (diffManifest.getVersion() == 2) {
265+
if (diffManifest.getVersion() > 2 || diffManifest.getVersion() < 1) {
266+
throw new IOException("Diff manifest version " + diffManifest.getVersion() + " is not supported by this SDK version.");
267+
} else if (diffManifest.getVersion() == 2 && !mEnableBinaryDiffUpdates) {
268+
throw new IOException("Received a binary diff update, but binary diff updates are not enabled on this client. Set CodePushEnableBinaryDiffUpdates to true in strings.xml to enable them.");
269+
} else if (diffManifest.getVersion() == 2) {
264270
String currentPackageFolderPath = getCurrentPackageFolderPath();
265271
BinaryDiffPatcher.applyBinaryDiffPatches(diffManifest, new File(currentPackageFolderPath), new File(unzippedFolderPath), new File(newUpdateFolderPath));
266272
FileUtils.deleteDirectoryAtPath(new File(newUpdateFolderPath, CodePushConstants.DIFF_PATCHES_FOLDER_NAME).getPath());
267-
} else if (diffManifest.getVersion() > 2) {
268-
throw new IOException("Diff manifest version " + diffManifest.getVersion() + " is not supported by this SDK version.");
269273
}
270274
}
271275

docs/api-android.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ Since `autolinking` uses `react-native.config.js` to link plugins, constructors
1515
<string moduleConfig="true" name="CodePushServerUrl">https://yourcodepush.server.com</string>
1616
```
1717

18+
- **Enable Binary Diff Updates** - switch for applying binary diff (bsdiff) patches during a diff update, off by default (at the moment). When disabled, only file-by-file diffing is applied (for example, skipping assets if only the main JS bundle changed, but that whole file is downloaded byte for byte). Add a `bool` resource named `CodePushEnableBinaryDiffUpdates` to `strings.xml` to turn it on:
19+
```xml
20+
<bool moduleConfig="true" name="CodePushEnableBinaryDiffUpdates">true</bool>
21+
```
22+
1823
The Java API is made available by importing the `com.microsoft.codepush.react.CodePush` class into your `MainActivity.java` file, and consists of a single public class named `CodePush`.
1924

2025
### Java API Reference (Android)

0 commit comments

Comments
 (0)