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

Commit d297fc8

Browse files
committed
android-asset-updates
1 parent 73b744c commit d297fc8

File tree

4 files changed

+344
-308
lines changed

4 files changed

+344
-308
lines changed

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

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -425,41 +425,50 @@ protected Void doInBackground(Object... params) {
425425
}
426426

427427
@ReactMethod
428-
public void getNewStatusReport(Promise promise) {
429-
if (needToReportRollback) {
430-
needToReportRollback = false;
431-
JSONArray failedUpdates = getFailedUpdates();
432-
if (failedUpdates != null && failedUpdates.length() > 0) {
433-
try {
434-
JSONObject lastFailedPackageJSON = failedUpdates.getJSONObject(failedUpdates.length() - 1);
435-
WritableMap lastFailedPackage = CodePushUtils.convertJsonObjectToWriteable(lastFailedPackageJSON);
436-
WritableMap failedStatusReport = codePushTelemetryManager.getRollbackReport(lastFailedPackage);
437-
if (failedStatusReport != null) {
438-
promise.resolve(failedStatusReport);
439-
return;
428+
public void getNewStatusReport(final Promise promise) {
429+
430+
AsyncTask asyncTask = new AsyncTask() {
431+
@Override
432+
protected Void doInBackground(Object... params) {
433+
if (needToReportRollback) {
434+
needToReportRollback = false;
435+
JSONArray failedUpdates = getFailedUpdates();
436+
if (failedUpdates != null && failedUpdates.length() > 0) {
437+
try {
438+
JSONObject lastFailedPackageJSON = failedUpdates.getJSONObject(failedUpdates.length() - 1);
439+
WritableMap lastFailedPackage = CodePushUtils.convertJsonObjectToWriteable(lastFailedPackageJSON);
440+
WritableMap failedStatusReport = codePushTelemetryManager.getRollbackReport(lastFailedPackage);
441+
if (failedStatusReport != null) {
442+
promise.resolve(failedStatusReport);
443+
return null;
444+
}
445+
} catch (JSONException e) {
446+
throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e);
447+
}
448+
}
449+
} else if (didUpdate) {
450+
WritableMap currentPackage = codePushPackage.getCurrentPackage();
451+
if (currentPackage != null) {
452+
WritableMap newPackageStatusReport = codePushTelemetryManager.getUpdateReport(currentPackage);
453+
if (newPackageStatusReport != null) {
454+
promise.resolve(newPackageStatusReport);
455+
return null;
456+
}
457+
}
458+
} else if (isRunningBinaryVersion) {
459+
WritableMap newAppVersionStatusReport = codePushTelemetryManager.getBinaryUpdateReport(appVersion);
460+
if (newAppVersionStatusReport != null) {
461+
promise.resolve(newAppVersionStatusReport);
462+
return null;
440463
}
441-
} catch (JSONException e) {
442-
throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e);
443-
}
444-
}
445-
} else if (didUpdate) {
446-
WritableMap currentPackage = codePushPackage.getCurrentPackage();
447-
if (currentPackage != null) {
448-
WritableMap newPackageStatusReport = codePushTelemetryManager.getUpdateReport(currentPackage);
449-
if (newPackageStatusReport != null) {
450-
promise.resolve(newPackageStatusReport);
451-
return;
452464
}
465+
466+
promise.resolve("");
467+
return null;
453468
}
454-
} else if (isRunningBinaryVersion) {
455-
WritableMap newAppVersionStatusReport = codePushTelemetryManager.getBinaryUpdateReport(appVersion);
456-
if (newAppVersionStatusReport != null) {
457-
promise.resolve(newAppVersionStatusReport);
458-
return;
459-
}
460-
}
469+
};
461470

462-
promise.resolve("");
471+
asyncTask.execute();
463472
}
464473

465474
@ReactMethod

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

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public String getStatusFilePath() {
6969

7070
public WritableMap getCurrentPackageInfo() {
7171
String statusFilePath = getStatusFilePath();
72-
if (!CodePushUtils.fileAtPathExists(statusFilePath)) {
72+
if (!FileUtils.fileAtPathExists(statusFilePath)) {
7373
return new WritableNativeMap();
7474
}
7575

@@ -166,6 +166,7 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
166166
File downloadFile = null;
167167
boolean isZip = false;
168168

169+
// Download the file while checking if it is a zip and notifying client of progress.
169170
try {
170171
downloadUrl = new URL(downloadUrlString);
171172
connection = (HttpURLConnection) (downloadUrl.openConnection());
@@ -216,27 +217,34 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
216217
}
217218

218219
if (isZip) {
220+
// Unzip the downloaded file and then delete the zip
219221
String unzippedFolderPath = getUnzippedFolderPath();
220-
CodePushUtils.unzipFile(downloadFile, unzippedFolderPath);
221-
CodePushUtils.deleteFileSilently(downloadFile);
222+
FileUtils.unzipFile(downloadFile, unzippedFolderPath);
223+
FileUtils.deleteFileSilently(downloadFile);
224+
225+
// Merge contents with current update based on the manifest
222226
String diffManifestFilePath = CodePushUtils.appendPathComponent(unzippedFolderPath,
223227
DIFF_MANIFEST_FILE_NAME);
224228
File diffManifestFile = new File(unzippedFolderPath, DIFF_MANIFEST_FILE_NAME);
225229
if (diffManifestFile.exists()) {
226230
String currentPackageFolderPath = getCurrentPackageFolderPath();
227-
CodePushUtils.mergeEntriesInFolder(currentPackageFolderPath, newPackageFolderPath);
231+
FileUtils.copyDirectoryContents(currentPackageFolderPath, newPackageFolderPath);
228232
WritableMap diffManifest = CodePushUtils.getWritableMapFromFile(diffManifestFilePath);
229233
ReadableArray deletedFiles = diffManifest.getArray("deletedFiles");
230234
for (int i = 0; i < deletedFiles.size(); i++) {
231235
String fileNameToDelete = deletedFiles.getString(i);
232236
File fileToDelete = new File(newPackageFolderPath, fileNameToDelete);
233-
CodePushUtils.deleteFileSilently(fileToDelete);
237+
FileUtils.deleteFileSilently(fileToDelete);
234238
}
235239
}
236240

237-
CodePushUtils.mergeEntriesInFolder(unzippedFolderPath, newPackageFolderPath);
238-
CodePushUtils.deleteFileAtPathSilently(unzippedFolderPath);
239-
String relativeBundlePath = findMainBundleInFolder(newPackageFolderPath);
241+
// Move merged update contents to a folder with the packageHash as its name
242+
FileUtils.copyDirectoryContents(unzippedFolderPath, newPackageFolderPath);
243+
FileUtils.deleteFileAtPathSilently(unzippedFolderPath);
244+
245+
// For zip updates, we need to find the relative path to the jsBundle and save it in the
246+
// metadata so that we can find and run it easily the next time.
247+
String relativeBundlePath = CodePushUtils.findJSBundleInUpdateContents(newPackageFolderPath);
240248

241249
if (relativeBundlePath == null) {
242250
throw new CodePushInvalidPackageException();
@@ -252,46 +260,22 @@ public void downloadPackage(Context applicationContext, ReadableMap updatePackag
252260
updatePackage = CodePushUtils.convertJsonObjectToWriteable(updatePackageJSON);
253261
}
254262
} else {
255-
// File is not a zip.
263+
// File is a jsBundle, move it to a folder with the packageHash as its name
256264
File updateBundleFile = new File(newPackageFolderPath, UPDATE_BUNDLE_FILE_NAME);
257265
downloadFile.renameTo(updateBundleFile);
258266
}
259267

268+
// Save metadata to the folder.
260269
String bundlePath = CodePushUtils.appendPathComponent(newPackageFolderPath, PACKAGE_FILE_NAME);
261270
CodePushUtils.writeReadableMapToFile(updatePackage, bundlePath);
262271
}
263272

264-
public String findMainBundleInFolder(String folderPath) {
265-
File folder = new File(folderPath);
266-
File[] folderFiles = folder.listFiles();
267-
for (File file : folderFiles) {
268-
String fullFilePath = CodePushUtils.appendPathComponent(folderPath, file.getName());
269-
if (file.isDirectory()) {
270-
String mainBundlePathInSubFolder = findMainBundleInFolder(fullFilePath);
271-
if (mainBundlePathInSubFolder != null) {
272-
return CodePushUtils.appendPathComponent(file.getName(), mainBundlePathInSubFolder);
273-
}
274-
} else {
275-
String fileName = file.getName();
276-
int dotIndex = fileName.lastIndexOf(".");
277-
if (dotIndex >= 0) {
278-
String fileExtension = fileName.substring(dotIndex + 1);
279-
if (fileExtension.equals("bundle") || fileExtension.equals("js") || fileExtension.equals("jsbundle")) {
280-
return fileName;
281-
}
282-
}
283-
}
284-
}
285-
286-
return null;
287-
}
288-
289273
public void installPackage(ReadableMap updatePackage) throws IOException {
290274
String packageHash = CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY);
291275
WritableMap info = getCurrentPackageInfo();
292276
String previousPackageHash = getPreviousPackageHash();
293277
if (previousPackageHash != null && !previousPackageHash.equals(packageHash)) {
294-
CodePushUtils.deleteDirectoryAtPath(getPackageFolderPath(previousPackageHash));
278+
FileUtils.deleteDirectoryAtPath(getPackageFolderPath(previousPackageHash));
295279
}
296280

297281
info.putString(PREVIOUS_PACKAGE_KEY, CodePushUtils.tryGetString(info, CURRENT_PACKAGE_KEY));
@@ -302,7 +286,7 @@ public void installPackage(ReadableMap updatePackage) throws IOException {
302286
public void rollbackPackage() {
303287
WritableMap info = getCurrentPackageInfo();
304288
String currentPackageFolderPath = getCurrentPackageFolderPath();
305-
CodePushUtils.deleteDirectoryAtPath(currentPackageFolderPath);
289+
FileUtils.deleteDirectoryAtPath(currentPackageFolderPath);
306290
info.putString(CURRENT_PACKAGE_KEY, CodePushUtils.tryGetString(info, PREVIOUS_PACKAGE_KEY));
307291
info.putNull(PREVIOUS_PACKAGE_KEY);
308292
updateCurrentPackageInfo(info);
@@ -344,6 +328,6 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) throws IOExc
344328
public void clearUpdates() {
345329
File statusFile = new File(getStatusFilePath());
346330
statusFile.delete();
347-
CodePushUtils.deleteDirectoryAtPath(getCodePushPath());
331+
FileUtils.deleteDirectoryAtPath(getCodePushPath());
348332
}
349333
}

0 commit comments

Comments
 (0)