Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Fix race condition and android bugs with incomplete/corrupted install…
Browse files Browse the repository at this point in the history
…ation

This will make sure the www folder is never left in a corrupted state. If it is corrupted, it will always be recreated on app start.

Also fixes a race condition with two installations being started concurrently, and interfering with each other's files. For example both would compete over the www_backup folder, and in my app's case, this would always fail the update because I have a lot of files in the app and file system errors would occur.
  • Loading branch information
andreialecu committed Nov 21, 2015
1 parent b3ff31f commit c616fe1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 23 additions & 3 deletions src/android/src/com/nordnetab/chcp/main/HotCodePushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import com.nordnetab.chcp.main.utils.Paths;
import com.nordnetab.chcp.main.utils.VersionHelper;

import com.nordnetab.chcp.main.model.ChcpError;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.ConfigXmlParser;
import org.apache.cordova.CordovaArgs;
Expand Down Expand Up @@ -368,7 +370,12 @@ private void installUpdate(CallbackContext jsCallback) {
installJsCallback = jsCallback;
}

UpdatesInstaller.install(fileStructure);
if (UpdatesInstaller.install(fileStructure)) {
// ensure that we set the www folder as invalid, temporarily,
// so that if the app crashes or is killed we don't run from a corrupted www folder
pluginInternalPrefs.setWwwFolderInstalled(false);
pluginInternalPrefsStorage.storeInPreference(pluginInternalPrefs);
}
}

// endregion
Expand Down Expand Up @@ -436,6 +443,7 @@ private void redirectToLocalStorage() {
currentUrl = currentUrl.replace(LOCAL_ASSETS_FOLDER, "");
String external = Paths.get(fileStructure.wwwFolder(), currentUrl);
if (!new File(external).exists()) {
Log.d("CHCP", "External starting page not found. Aborting page change.");
return;
}

Expand Down Expand Up @@ -617,6 +625,10 @@ public void onEvent(NothingToUpdateEvent event) {
@SuppressWarnings("unused")
public void onEvent(UpdateDownloadErrorEvent event) {
Log.d("CHCP", "Failed to update");
if (event.error() == ChcpError.LOCAL_VERSION_OF_APPLICATION_CONFIG_NOT_FOUND || event.error() == ChcpError.LOCAL_VERSION_OF_MANIFEST_NOT_FOUND) {
Log.d("CHCP", "Can't load application config from installation folder. Reinstalling external folder");
installWwwFolder();
}

PluginResult jsResult = PluginResultHelper.pluginResultFromEvent(event);

Expand Down Expand Up @@ -645,6 +657,10 @@ public void onEvent(UpdateDownloadErrorEvent event) {
public void onEvent(UpdateInstalledEvent event) {
Log.d("CHCP", "Update is installed");

// reconfirm that our www folder is now valid
pluginInternalPrefs.setWwwFolderInstalled(true);
pluginInternalPrefsStorage.storeInPreference(pluginInternalPrefs);

final PluginResult jsResult = PluginResultHelper.pluginResultFromEvent(event);

if (installJsCallback != null) {
Expand All @@ -666,8 +682,12 @@ private void resetApplicationToStartingPage() {
public void run() {
webView.clearHistory();
webView.clearCache();
final String startingPage = getStartingPage() + "?" + System.currentTimeMillis();
final String externalStartingPage = FILE_PREFIX + Paths.get(fileStructure.wwwFolder(), startingPage);
final String external = Paths.get(fileStructure.wwwFolder(), getStartingPage());
if (!new File(external).exists()) {
Log.d("CHCP", "External starting page not found. Aborting page change.");
return;
}
final String externalStartingPage = FILE_PREFIX + external + "?" + System.currentTimeMillis();
webView.loadUrlIntoView(externalStartingPage, false);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public static boolean isInstalling() {
}

private static void execute(final InstallationWorker task) {
isInstalling = true;
new Thread(new Runnable() {
@Override
public void run() {
isInstalling = true;
task.run();
isInstalling = false;
}
Expand Down

0 comments on commit c616fe1

Please sign in to comment.