diff --git a/CodePush.m b/CodePush.m index 5e2370e..2c8c202 100644 --- a/CodePush.m +++ b/CodePush.m @@ -359,32 +359,34 @@ RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary*)updatePackage resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - [CodePushPackage downloadPackage:updatePackage - // The download is progressing forward - progressCallback:^(long long expectedContentLength, long long receivedContentLength) { - // Notify the script-side about the progress - [self.bridge.eventDispatcher - sendDeviceEventWithName:@"CodePushDownloadProgress" - body:@{ - @"totalBytes":[NSNumber numberWithLongLong:expectedContentLength], - @"receivedBytes":[NSNumber numberWithLongLong:receivedContentLength] - }]; - } - // The download completed - doneCallback:^{ - NSError *err; - NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err]; - - if (err) { - return reject(err); + dispatch_async(dispatch_get_main_queue(), ^{ + [CodePushPackage downloadPackage:updatePackage + // The download is progressing forward + progressCallback:^(long long expectedContentLength, long long receivedContentLength) { + // Notify the script-side about the progress + [self.bridge.eventDispatcher + sendDeviceEventWithName:@"CodePushDownloadProgress" + body:@{ + @"totalBytes":[NSNumber numberWithLongLong:expectedContentLength], + @"receivedBytes":[NSNumber numberWithLongLong:receivedContentLength] + }]; } - - resolve(newPackage); - } - // The download failed - failCallback:^(NSError *err) { - reject(err); - }]; + // The download completed + doneCallback:^{ + NSError *err; + NSDictionary *newPackage = [CodePushPackage getPackage:updatePackage[PackageHashKey] error:&err]; + + if (err) { + return reject(err); + } + + resolve(newPackage); + } + // The download failed + failCallback:^(NSError *err) { + reject(err); + }]; + }); } /* diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java index a89f414..80645cb 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java @@ -225,26 +225,17 @@ public class CodePushPackage { // Merge contents with current update based on the manifest String diffManifestFilePath = CodePushUtils.appendPathComponent(unzippedFolderPath, DIFF_MANIFEST_FILE_NAME); - File diffManifestFile = new File(unzippedFolderPath, DIFF_MANIFEST_FILE_NAME); - if (diffManifestFile.exists()) { + if (FileUtils.fileAtPathExists(diffManifestFilePath)) { String currentPackageFolderPath = getCurrentPackageFolderPath(); - FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath); - WritableMap diffManifest = CodePushUtils.getWritableMapFromFile(diffManifestFilePath); - ReadableArray deletedFiles = diffManifest.getArray("deletedFiles"); - for (int i = 0; i < deletedFiles.size(); i++) { - String fileNameToDelete = deletedFiles.getString(i); - File fileToDelete = new File(newPackageFolderPath, fileNameToDelete); - FileUtils.deleteFileSilently(fileToDelete); - } + CodePushUpdateUtils.copyNecessaryFilesFromCurrentPackage(diffManifestFilePath, currentPackageFolderPath, newPackageFolderPath); } - // Move merged update contents to a folder with the packageHash as its name FileUtils.copyDirectoryContents(unzippedFolderPath, newPackageFolderPath); FileUtils.deleteFileAtPathSilently(unzippedFolderPath); // For zip updates, we need to find the relative path to the jsBundle and save it in the // metadata so that we can find and run it easily the next time. - String relativeBundlePath = CodePushUtils.findJSBundleInUpdateContents(newPackageFolderPath); + String relativeBundlePath = CodePushUpdateUtils.findJSBundleInUpdateContents(newPackageFolderPath); if (relativeBundlePath == null) { throw new CodePushInvalidUpdateException(); @@ -257,6 +248,7 @@ public class CodePushPackage { RELATIVE_BUNDLE_PATH_KEY + " to value " + relativeBundlePath + " in update package.", e); } + updatePackage = CodePushUtils.convertJsonObjectToWriteable(updatePackageJSON); } } else { diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java new file mode 100644 index 0000000..a8cd21c --- /dev/null +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java @@ -0,0 +1,46 @@ +package com.microsoft.codepush.react; + +import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.WritableMap; + +import java.io.File; +import java.io.IOException; + +public class CodePushUpdateUtils { + + public static void copyNecessaryFilesFromCurrentPackage(String diffManifestFilePath, String currentPackageFolderPath, String newPackageFolderPath) throws IOException{ + FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath); + WritableMap diffManifest = CodePushUtils.getWritableMapFromFile(diffManifestFilePath); + ReadableArray deletedFiles = diffManifest.getArray("deletedFiles"); + for (int i = 0; i < deletedFiles.size(); i++) { + String fileNameToDelete = deletedFiles.getString(i); + File fileToDelete = new File(newPackageFolderPath, fileNameToDelete); + FileUtils.deleteFileSilently(fileToDelete); + } + } + + public static String findJSBundleInUpdateContents(String folderPath) { + File folder = new File(folderPath); + File[] folderFiles = folder.listFiles(); + for (File file : folderFiles) { + String fullFilePath = CodePushUtils.appendPathComponent(folderPath, file.getName()); + if (file.isDirectory()) { + String mainBundlePathInSubFolder = findJSBundleInUpdateContents(fullFilePath); + if (mainBundlePathInSubFolder != null) { + return CodePushUtils.appendPathComponent(file.getName(), mainBundlePathInSubFolder); + } + } else { + String fileName = file.getName(); + int dotIndex = fileName.lastIndexOf("."); + if (dotIndex >= 0) { + String fileExtension = fileName.substring(dotIndex + 1); + if (fileExtension.equals("bundle") || fileExtension.equals("js") || fileExtension.equals("jsbundle")) { + return fileName; + } + } + } + } + + return null; + } +} diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java index 750db71..63d2709 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java @@ -183,31 +183,6 @@ public class CodePushUtils { return jsonObj; } - public static String findJSBundleInUpdateContents(String folderPath) { - File folder = new File(folderPath); - File[] folderFiles = folder.listFiles(); - for (File file : folderFiles) { - String fullFilePath = CodePushUtils.appendPathComponent(folderPath, file.getName()); - if (file.isDirectory()) { - String mainBundlePathInSubFolder = findJSBundleInUpdateContents(fullFilePath); - if (mainBundlePathInSubFolder != null) { - return CodePushUtils.appendPathComponent(file.getName(), mainBundlePathInSubFolder); - } - } else { - String fileName = file.getName(); - int dotIndex = fileName.lastIndexOf("."); - if (dotIndex >= 0) { - String fileExtension = fileName.substring(dotIndex + 1); - if (fileExtension.equals("bundle") || fileExtension.equals("js") || fileExtension.equals("jsbundle")) { - return fileName; - } - } - } - } - - return null; - } - public static WritableMap getWritableMapFromFile(String filePath) throws IOException { String content = FileUtils.readFileToString(filePath); diff --git a/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java index 16c5731..113df51 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java @@ -1,7 +1,5 @@ package com.microsoft.codepush.react; -import android.util.Log; - import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File;