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

Commit

Permalink
Moved to UpdateDownloadRequest model. Better handling of invalid conf…
Browse files Browse the repository at this point in the history
…ig; added new error code for that.

#153
  • Loading branch information
nikDemyankov committed Jun 3, 2016
1 parent 210eb6f commit da2fb75
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 56 deletions.
12 changes: 10 additions & 2 deletions src/android/src/com/nordnetab/chcp/main/HotCodePushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.nordnetab.chcp.main.storage.IObjectFileStorage;
import com.nordnetab.chcp.main.storage.IObjectPreferenceStorage;
import com.nordnetab.chcp.main.storage.PluginInternalPreferencesStorage;
import com.nordnetab.chcp.main.updater.UpdateDownloadRequest;
import com.nordnetab.chcp.main.updater.UpdatesInstaller;
import com.nordnetab.chcp.main.updater.UpdatesLoader;
import com.nordnetab.chcp.main.utils.AssetsHelper;
Expand Down Expand Up @@ -425,8 +426,15 @@ private void fetchUpdate(CallbackContext jsCallback) {
return;
}

final PluginFilesStructure currentReleaseFS = new PluginFilesStructure(cordova.getActivity(), pluginInternalPrefs.getCurrentReleaseVersionName());
final ChcpError error = UpdatesLoader.downloadUpdate(chcpXmlConfig.getConfigUrl(), currentReleaseFS, chcpXmlConfig.getNativeInterfaceVersion());
// TODO: add JS side support
final UpdateDownloadRequest request = UpdateDownloadRequest.builder(cordova.getActivity())
.setConfigURL(chcpXmlConfig.getConfigUrl())
.setCurrentNativeVersion(chcpXmlConfig.getNativeInterfaceVersion())
.setCurrentReleaseVersion(pluginInternalPrefs.getCurrentReleaseVersionName())
.setRequestHeaders(null)
.build();

final ChcpError error = UpdatesLoader.downloadUpdate(request);
if (error != ChcpError.NONE) {
if (jsCallback != null) {
PluginResult errorResult = PluginResultHelper.createPluginResult(UpdateDownloadErrorEvent.EVENT_NAME, null, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum ChcpError {
INSTALLATION_ALREADY_IN_PROGRESS(-16, "Installation already in progress"),
DOWNLOAD_ALREADY_IN_PROGRESS(-17, "Download already in progress"),
ASSETS_FOLDER_IN_NOT_YET_INSTALLED(-18, "API requests are ignored until assets are installed. Please, wait."),

NEW_APPLICATION_CONFIG_IS_INVALID(-19, "Downloaded new application config, but it is not valid"),
// endregion

// region Kind of warnings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.nordnetab.chcp.main.config.ApplicationConfig;

import java.util.Map;

/**
* Created by Nikolay Demyankov on 24.07.15.
* <p/>
Expand All @@ -15,10 +17,11 @@ public class ApplicationConfigDownloader extends JsonDownloader<ApplicationConfi
/**
* Class constructor
*
* @param url url from where to download application config
* @param url url from where to download application config
* @param requestHeaders additional request headers
*/
public ApplicationConfigDownloader(String url) {
super(url);
public ApplicationConfigDownloader(final String url, final Map<String, String> requestHeaders) {
super(url, requestHeaders);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.nordnetab.chcp.main.config.ContentManifest;

import java.util.Map;

/**
* Created by Nikolay Demyankov on 22.07.15.
* <p/>
Expand All @@ -15,10 +17,11 @@ public class ContentManifestDownloader extends JsonDownloader<ContentManifest> {
/**
* Class constructor
*
* @param url url from where to download manifest
* @param url url from where to download manifest
* @param requestHeaders additional request headers
*/
public ContentManifestDownloader(String url) {
super(url);
public ContentManifestDownloader(final String url, final Map<String, String> requestHeaders) {
super(url, requestHeaders);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.util.Log;

import com.nordnetab.chcp.main.config.ApplicationConfig;
import com.nordnetab.chcp.main.config.ContentConfig;
import com.nordnetab.chcp.main.config.ContentManifest;
import com.nordnetab.chcp.main.events.NothingToUpdateEvent;
import com.nordnetab.chcp.main.events.UpdateDownloadErrorEvent;
Expand All @@ -25,6 +26,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* Created by Nikolay Demyankov on 28.07.15.
Expand All @@ -39,6 +41,7 @@ class UpdateLoaderWorker implements WorkerTask {
private final String applicationConfigUrl;
private final int appNativeVersion;
private final PluginFilesStructure filesStructure;
private final Map<String, String> requestHeaders;

private IObjectFileStorage<ApplicationConfig> appConfigStorage;
private IObjectFileStorage<ContentManifest> manifestStorage;
Expand All @@ -51,14 +54,13 @@ class UpdateLoaderWorker implements WorkerTask {
/**
* Constructor.
*
* @param configUrl application config url
* @param currentReleaseFileStructure files structure of the current release
* @param currentNativeVersion current native version of the
* @param request download request
*/
public UpdateLoaderWorker(final String configUrl, final PluginFilesStructure currentReleaseFileStructure, final int currentNativeVersion) {
filesStructure = currentReleaseFileStructure;
applicationConfigUrl = configUrl;
appNativeVersion = currentNativeVersion;
public UpdateLoaderWorker(final UpdateDownloadRequest request) {
applicationConfigUrl = request.getConfigURL();
appNativeVersion = request.getCurrentNativeVersion();
filesStructure = request.getCurrentReleaseFileStructure();
requestHeaders = request.getRequestHeaders();
}

@Override
Expand All @@ -70,33 +72,38 @@ public void run() {
}

// download new application config
ApplicationConfig newAppConfig = downloadApplicationConfig();
final ApplicationConfig newAppConfig = downloadApplicationConfig(applicationConfigUrl);
if (newAppConfig == null) {
setErrorResult(ChcpError.FAILED_TO_DOWNLOAD_APPLICATION_CONFIG, null);
return;
}
final ContentConfig newContentConfig = newAppConfig.getContentConfig();
if (newContentConfig == null || TextUtils.isEmpty(newContentConfig.getReleaseVersion())) {
setErrorResult(ChcpError.NEW_APPLICATION_CONFIG_IS_INVALID, null);
return;
}

// check if there is a new content version available
if (newAppConfig.getContentConfig().getReleaseVersion().equals(oldAppConfig.getContentConfig().getReleaseVersion())) {
if (newContentConfig.getReleaseVersion().equals(oldAppConfig.getContentConfig().getReleaseVersion())) {
setNothingToUpdateResult(newAppConfig);
return;
}

// check if current native version supports new content
if (newAppConfig.getContentConfig().getMinimumNativeVersion() > appNativeVersion) {
if (newContentConfig.getMinimumNativeVersion() > appNativeVersion) {
setErrorResult(ChcpError.APPLICATION_BUILD_VERSION_TOO_LOW, newAppConfig);
return;
}

// download new content manifest
ContentManifest newContentManifest = downloadContentManifest(newAppConfig);
final ContentManifest newContentManifest = downloadContentManifest(newContentConfig.getContentUrl());
if (newContentManifest == null) {
setErrorResult(ChcpError.FAILED_TO_DOWNLOAD_CONTENT_MANIFEST, newAppConfig);
return;
}

// find files that were updated
ManifestDiff diff = oldManifest.calculateDifference(newContentManifest);
final ManifestDiff diff = oldManifest.calculateDifference(newContentManifest);
if (diff.isEmpty()) {
manifestStorage.storeInFolder(newContentManifest, filesStructure.getWwwFolder());
appConfigStorage.storeInFolder(newAppConfig, filesStructure.getWwwFolder());
Expand All @@ -111,7 +118,7 @@ public void run() {
recreateDownloadFolder(filesStructure.getDownloadFolder());

// download files
boolean isDownloaded = downloadNewAndChangedFiles(newAppConfig, diff);
boolean isDownloaded = downloadNewAndChangedFiles(newContentConfig.getContentUrl(), diff);
if (!isDownloaded) {
cleanUp();
setErrorResult(ChcpError.FAILED_TO_DOWNLOAD_UPDATE_FILES, newAppConfig);
Expand Down Expand Up @@ -157,10 +164,12 @@ private boolean init() {
/**
* Download application config from server.
*
* @return new application config
* @param configUrl from what url it should be downloaded
* @return new application config; <code>null</code> when failed to download
*/
private ApplicationConfig downloadApplicationConfig() {
DownloadResult<ApplicationConfig> downloadResult = new ApplicationConfigDownloader(applicationConfigUrl).download();
private ApplicationConfig downloadApplicationConfig(final String configUrl) {
final ApplicationConfigDownloader downloader = new ApplicationConfigDownloader(configUrl, requestHeaders);
final DownloadResult<ApplicationConfig> downloadResult = downloader.download();
if (downloadResult.error != null) {
Log.d("CHCP", "Failed to download application config");

Expand All @@ -173,18 +182,13 @@ private ApplicationConfig downloadApplicationConfig() {
/**
* Download new content manifest from server.
*
* @param config new application config from which we will take content url
* @return new content manifest
* @param contentUrl url where our content lies
* @return new content manifest; <code>null</code> when failed to download
*/
private ContentManifest downloadContentManifest(ApplicationConfig config) {
final String contentUrl = config.getContentConfig().getContentUrl();
if (TextUtils.isEmpty(contentUrl)) {
Log.d("CHCP", "Content url is not set in your application config! Can't load updates.");
return null;
}

private ContentManifest downloadContentManifest(final String contentUrl) {
final String url = URLUtility.construct(contentUrl, PluginFilesStructure.MANIFEST_FILE_NAME);
DownloadResult<ContentManifest> downloadResult = new ContentManifestDownloader(url).download();
final ContentManifestDownloader downloader = new ContentManifestDownloader(url, requestHeaders);
final DownloadResult<ContentManifest> downloadResult = downloader.download();
if (downloadResult.error != null) {
Log.d("CHCP", "Failed to download content manifest");
return null;
Expand All @@ -206,23 +210,16 @@ private void recreateDownloadFolder(final String folder) {
/**
* Download from server new and update files.
*
* @param newAppConfig new application config, from which we will use content url
* @param diff manifest difference from which we will know, what files to download
* @param contentUrl content url
* @param diff manifest difference from which we will know, what files to download
* @return <code>true</code> if files are loaded; <code>false</code> - otherwise
*/
private boolean downloadNewAndChangedFiles(ApplicationConfig newAppConfig, ManifestDiff diff) {
final String contentUrl = newAppConfig.getContentConfig().getContentUrl();
if (TextUtils.isEmpty(contentUrl)) {
Log.d("CHCP", "Content url is not set in your application config! Can't load updates.");
return false;
}

List<ManifestFile> downloadFiles = diff.getUpdateFiles();

private boolean downloadNewAndChangedFiles(final String contentUrl, final ManifestDiff diff) {
final List<ManifestFile> downloadFiles = diff.getUpdateFiles();
boolean isFinishedWithSuccess = true;
try {
FileDownloader.downloadFiles(filesStructure.getDownloadFolder(), contentUrl, downloadFiles);
} catch (IOException e) {
FileDownloader.downloadFiles(filesStructure.getDownloadFolder(), contentUrl, downloadFiles, requestHeaders);
} catch (Exception e) {
e.printStackTrace();
isFinishedWithSuccess = false;
}
Expand All @@ -239,15 +236,15 @@ private void cleanUp() {

// region Events

private void setErrorResult(ChcpError error, ApplicationConfig newAppConfig) {
private void setErrorResult(final ChcpError error, final ApplicationConfig newAppConfig) {
resultEvent = new UpdateDownloadErrorEvent(error, newAppConfig);
}

private void setSuccessResult(ApplicationConfig newAppConfig) {
private void setSuccessResult(final ApplicationConfig newAppConfig) {
resultEvent = new UpdateIsReadyToInstallEvent(newAppConfig);
}

private void setNothingToUpdateResult(ApplicationConfig newAppConfig) {
private void setNothingToUpdateResult(final ApplicationConfig newAppConfig) {
resultEvent = new NothingToUpdateEvent(newAppConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ public static boolean isExecuting() {
* Request update download.
* Download performed in background. Events are dispatched to notify us about the result.
*
* @param configURL url from which we should download application config
* @param currentReleaseFileStructure files structure of the current release
* @param currentNativeVersion current native interface version
* @param request download request
* @return <code>ChcpError.NONE</code> if download has started; otherwise - error details
*/
public static ChcpError downloadUpdate(final String configURL, final PluginFilesStructure currentReleaseFileStructure, final int currentNativeVersion) {
public static ChcpError downloadUpdate(final UpdateDownloadRequest request) {
// if download already in progress - exit
if (isExecuting) {
return ChcpError.DOWNLOAD_ALREADY_IN_PROGRESS;
Expand All @@ -47,7 +45,7 @@ public static ChcpError downloadUpdate(final String configURL, final PluginFiles

isExecuting = true;

final UpdateLoaderWorker task = new UpdateLoaderWorker(configURL, currentReleaseFileStructure, currentNativeVersion);
final UpdateLoaderWorker task = new UpdateLoaderWorker(request);
executeTask(task);

return ChcpError.NONE;
Expand Down

0 comments on commit da2fb75

Please sign in to comment.