From 0204a2ee2ce5739ba23ee5d42a662035fd8889e6 Mon Sep 17 00:00:00 2001 From: Nikolay Demyankov Date: Fri, 22 Apr 2016 13:43:41 +0200 Subject: [PATCH] Added isUpdateAvailableForInstallation() method to Android. https://github.com/nordnet/cordova-hot-code-push/issues/102 --- .../chcp/main/HotCodePushPlugin.java | 25 +++++++++++++++++++ .../com/nordnetab/chcp/main/js/JSAction.java | 1 + .../chcp/main/js/PluginResultHelper.java | 4 ++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/android/src/com/nordnetab/chcp/main/HotCodePushPlugin.java b/src/android/src/com/nordnetab/chcp/main/HotCodePushPlugin.java index c9cd10da..32f099b5 100644 --- a/src/android/src/com/nordnetab/chcp/main/HotCodePushPlugin.java +++ b/src/android/src/com/nordnetab/chcp/main/HotCodePushPlugin.java @@ -47,7 +47,9 @@ import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -229,6 +231,8 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback jsSetPluginOptions(args, callbackContext); } else if (JSAction.REQUEST_APP_UPDATE.equals(action)) { jsRequestAppUpdate(args, callbackContext); + } else if (JSAction.IS_UPDATE_AVAILABLE_FOR_INSTALLATION.equals(action)) { + jsIsUpdateAvailableForInstallation(callbackContext); } else { cmdProcessed = false; } @@ -394,6 +398,27 @@ private void jsRequestAppUpdate(final CordovaArgs arguments, final CallbackConte new AppUpdateRequestDialog(cordova.getActivity(), msg, storeURL, callback).show(); } + /** + * Check if new version was loaded and can be installed. + * + * @param callback callback where to send the result + * */ + private void jsIsUpdateAvailableForInstallation(final CallbackContext callback) { + Map data = null; + ChcpError error = null; + final String readyForInstallationVersionName = pluginInternalPrefs.getReadyForInstallationReleaseVersionName(); + if (!TextUtils.isEmpty(readyForInstallationVersionName)) { + data = new HashMap(); + data.put("readyToInstallVersion", readyForInstallationVersionName); + data.put("currentVersion", pluginInternalPrefs.getCurrentReleaseVersionName()); + } else { + error = ChcpError.NOTHING_TO_INSTALL; + } + + PluginResult pluginResult = PluginResultHelper.createPluginResult(null, data, error); + callback.sendPluginResult(pluginResult); + } + /** * Perform update availability check. * diff --git a/src/android/src/com/nordnetab/chcp/main/js/JSAction.java b/src/android/src/com/nordnetab/chcp/main/js/JSAction.java index 5bcb5b47..abdd951e 100644 --- a/src/android/src/com/nordnetab/chcp/main/js/JSAction.java +++ b/src/android/src/com/nordnetab/chcp/main/js/JSAction.java @@ -12,6 +12,7 @@ public final class JSAction { public static final String INSTALL_UPDATE = "jsInstallUpdate"; public static final String CONFIGURE = "jsConfigure"; public static final String REQUEST_APP_UPDATE = "jsRequestAppUpdate"; + public static final String IS_UPDATE_AVAILABLE_FOR_INSTALLATION = "jsIsUpdateAvailableForInstallation"; // Private API public static final String INIT = "jsInitPlugin"; diff --git a/src/android/src/com/nordnetab/chcp/main/js/PluginResultHelper.java b/src/android/src/com/nordnetab/chcp/main/js/PluginResultHelper.java index 529cf5a4..d07318ec 100644 --- a/src/android/src/com/nordnetab/chcp/main/js/PluginResultHelper.java +++ b/src/android/src/com/nordnetab/chcp/main/js/PluginResultHelper.java @@ -101,7 +101,9 @@ private static PluginResult getResult(String action, JsonNode data, JsonNode err JsonNodeFactory factory = JsonNodeFactory.instance; ObjectNode resultObject = factory.objectNode(); - resultObject.set(JsParams.General.ACTION, factory.textNode(action)); + if (action != null) { + resultObject.set(JsParams.General.ACTION, factory.textNode(action)); + } if (data != null) { resultObject.set(JsParams.General.DATA, data);