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

Commit

Permalink
If needed - you can define fetch update options from the native side.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikDemyankov committed Jun 8, 2016
1 parent 5e83cd4 commit fc8e543
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/android/src/com/nordnetab/chcp/main/HotCodePushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class HotCodePushPlugin extends CordovaPlugin {
private boolean dontReloadOnStart;

private List<PluginResult> defaultCallbackStoredResults;
private FetchUpdateOptions defaultFetchUpdateOptions;

// region Plugin lifecycle

Expand Down Expand Up @@ -172,6 +173,30 @@ public void onStop() {

// endregion

// region Plugin external properties

/**
* Setter for default fetch update options.
* If this one is defined and no options has come form JS side - we use them.
* If preferences came from JS side - we ignore the default ones.
*
* @param options options
*/
public void setDefaultFetchUpdateOptions(final FetchUpdateOptions options) {
this.defaultFetchUpdateOptions = options;
}

/**
* Getter for currently used default fetch update options.
*
* @return default fetch options
*/
public FetchUpdateOptions getDefaultFetchUpdateOptions() {
return defaultFetchUpdateOptions;
}

// endregion

// region Config loaders and initialization

/**
Expand Down Expand Up @@ -310,11 +335,10 @@ private void jsFetchUpdate(CallbackContext callback, CordovaArgs args) {
return;
}

FetchUpdateOptions fetchOptions;
FetchUpdateOptions fetchOptions = null;
try {
fetchOptions = new FetchUpdateOptions(args.optJSONObject(0));
} catch (JSONException e) {
fetchOptions = new FetchUpdateOptions();
} catch (JSONException ignored) {
}

fetchUpdate(callback, fetchOptions);
Expand Down Expand Up @@ -436,14 +460,24 @@ private void fetchUpdate(CallbackContext jsCallback, FetchUpdateOptions fetchOpt
return;
}

final String optionalConfigURL = fetchOptions.getConfigURL();
final String configURL = !TextUtils.isEmpty(optionalConfigURL) ? optionalConfigURL : chcpXmlConfig.getConfigUrl();
Map<String, String> requestHeaders = null;
String configURL = chcpXmlConfig.getConfigUrl();
if (fetchOptions == null) {
fetchOptions = defaultFetchUpdateOptions;
}
if (fetchOptions != null) {
requestHeaders = fetchOptions.getRequestHeaders();
final String optionalConfigURL = fetchOptions.getConfigURL();
if (!TextUtils.isEmpty(optionalConfigURL)) {
configURL = optionalConfigURL;
}
}

final UpdateDownloadRequest request = UpdateDownloadRequest.builder(cordova.getActivity())
.setConfigURL(configURL)
.setCurrentNativeVersion(chcpXmlConfig.getNativeInterfaceVersion())
.setCurrentReleaseVersion(pluginInternalPrefs.getCurrentReleaseVersionName())
.setRequestHeaders(fetchOptions.getRequestHeaders())
.setRequestHeaders(requestHeaders)
.build();

final ChcpError error = UpdatesLoader.downloadUpdate(request);
Expand Down

0 comments on commit fc8e543

Please sign in to comment.