You can change the default user interface for the Direct Update dialog boxes and the messages that are displayed to the user.
You can control the direct update process without presenting a user interface to the user and control what happens when the direct update process fails.
Override the handleDirectUpdate function of the Direct Update challenge handler to customize the direct update process and interface in iOS and Android applications. The handleDirectUpdate function is defined inside worklights.js. It has the following format:
wl_directUpdateChallengeHandler.handleDirectUpdate = function (directUpdateData,directUpdateContext){...}
The function accepts the following arguments:
If the web resources are newer on the MobileFirst Server than in the application, Direct Update challenge data is added to the server response. Whenever the MobileFirst client-side framework detects this direct update challenge, it invokes the wl_directUpdateChallengeHandler.handleDirectUpdate function.
The function provides a default Direct Update design: a default message dialog that is displayed when a Direct Update is available and a default progress screen that is displayed when the direct update process is initiated. For examples of default screens, see Default Direct Update user interface. You can implement custom Direct Update user interface behavior or customize the Direct Update dialog box by overriding this function and implementing your own logic.
The following example handleDirectUpdate function implements a custom message in the Direct Update dialog. Add this code into the www/js/index.js file of the Cordova project .
wl_directUpdateChallengeHandler.handleDirectUpdate = function(directUpdateData, directUpdateContext) {
navigator.notification.confirm( // Creates a dialog.
'Custom dialog body text',
// Handle dialog buttons.
directUpdateContext.start();
},
'Custom dialog title text',
['Update']
);
};
The result is shown in Figure 1.
You can start the direct update process by running the directUpdateContext.start() method whenever the user clicks the dialog button. The default progress screen, which resembles the one in previous versions of MobileFirst Server is shown.
var directUpdateCustomListener = {
onStart : function ( totalSize ){ },
onProgress : function ( status , totalSize , completedSize ){ },
onFinish : function ( status ){ }
};
Status code | Description |
---|---|
SUCCESS | Direct update finished with no errors. |
CANCELED | Direct update was canceled (for example, because the stop() method was called). |
FAILURE_NETWORK_PROBLEM | There was a problem with a network connection during the update. |
FAILURE_DOWNLOADING | The file was not downloaded completely. |
FAILURE_NOT_ENOUGH_SPACE | There is not enough space on the device to download and unpack the update file. |
FAILURE_UNZIPPING | There was a problem unpacking the update file. |
FAILURE_ALREADY_IN_PROGRESS | The start method was called while direct update was already running. |
FAILURE_INTEGRITY | Authenticity of update file cannot be verified. |
FAILURE_UNKNOWN | Unexpected internal error. |
If you implement a custom direct update listener, you must ensure that the app is reloaded when the direct update process is complete and the onFinish() method has been called. You must also call wl_directUpdateChalengeHandler.submitFailure() if the direct update process fails to complete successfully.
The following example shows an implementation of a custom direct update listener:
var directUpdateCustomListener = {
onStart: function(totalSize){
//show custom progress dialog
},
onProgress: function(status,totalSize,completedSize){
//update custom progress dialog
},
onFinish: function(status){
if (status == 'SUCCESS'){
//show success message
WL.Client.reloadApp();
}
else {
//show custom error message
//submitFailure must be called is case of error
wl_directUpdateChallengeHandler.submitFailure();
}
}
};
wl_directUpdateChallengeHandler.handleDirectUpdate = function(directUpdateData, directUpdateContext){
WL.SimpleDialog.show('Update Avalible', 'Press update button to download version 2.0', [{
text : 'update',
handler : function() {
directUpdateContext.start(directUpdateCustomListener);
}
}]);
};
IBM MobileFirst Platform Foundation supports UI-less direct update when the application is in the foreground.
To run UI-less direct updates, implement directUpdateCustomListener. Provide empty function implementations to the onStart and onProgress methods. Empty implementations cause the direct update process to run in the background.
var directUpdateCustomListener = {
onStart: function(totalSize){
},
onProgress: function(status,totalSize,completeSize){
},
onFinish: function(status){
WL.SimpleDialog.show('New Update Available', 'Press reload button to update to new version', [ {
text : WL.ClientMessages.reload,
handler : WL.Client.reloadApp
}]);
}
};
Implement the wl_directUpdateChallengeHandler.handleDirectUpdate function. Pass the directUpdateCustomListener implementation that you have created as a parameter to the function. Make sure directUpdateContext.start(directUpdateCustomListener) is called. Here is an example wl_directUpdateChallengeHandler.handleDirectUpdate implementation:
wl_directUpdateChallengeHandler.handleDirectUpdate = function(directUpdateData, directUpdateContext){
directUpdateContext.start(directUpdateCustomListener);
};
This scenario shows how to handle a direct update failure that might be caused, for example, by loss of connectivity. In this scenario, the user is prevented from using the app even in offline mode. A dialog is displayed offering the user the option to try again.
Create a global variable to store the direct update context so that you can use it subsequently when the direct update process fails. For example:
var savedDirectUpdateContext;
Implement a direct update challenge handler. Save the direct update context here. For example:
wl_directUpdateChallengeHandler.handleDirectUpdate = function(directUpdateData, directUpdateContext){
savedDirectUpdateContext = directUpdateContext; // save direct update context
var downloadSizeInMB = (directUpdateData.downloadSize / 1048576).toFixed(1).replace(".", WL.App.getDecimalSeparator());
var directUpdateMsg = WL.Utils.formatString(WL.ClientMessages.directUpdateNotificationMessage, downloadSizeInMB);
WL.SimpleDialog.show(WL.ClientMessages.directUpdateNotificationTitle, directUpdateMsg, [{
text : WL.ClientMessages.update,
handler : function() {
directUpdateContext.start(directUpdateCustomListener);
}
}]);
};
Create a function that starts the direct update process by using the direct update context. For example:
restartDirectUpdate = function () {
savedDirectUpdateContext.start(directUpdateCustomListener); // use saved direct update context to restart direct update
};
Implement directUpdateCustomListener. Add status checking in the onFinish method. If the status starts with "FAILURE", open a modal only dialog with the option "Try Again". For example:
var directUpdateCustomListener = {
onStart: function(totalSize){
alert('onStart: totalSize = ' + totalSize + 'Byte');
},
onProgress: function(status,totalSize,completeSize){
alert('onProgress: status = ' + status + ' completeSize = ' + completeSize + 'Byte');
},
onFinish: function(status){
alert('onFinish: status = ' + status);
var pos = status.indexOf("FAILURE");
if (pos > -1) {
WL.SimpleDialog.show('Update Failed', 'Press try again button', [ {
text : "Try Again",
handler : restartDirectUpdate // restart direct update
}]);
}
}
};
When the user clicks the Try Again button, the application restarts the direct update process.