1 // ${project.version} 2 var exec = require("cordova/exec"); 3 4 5 /** 6 * Provides settings exchange functionality 7 * 8 * @namespace 9 * @alias Settings 10 * @memberof sap 11 */ 12 var SettingsExchange = function () {}; 13 14 SettingsExchange.prototype.connectionData = null; 15 SettingsExchange.prototype.store = null; 16 SettingsExchange.prototype.settingsSuccess = null; 17 SettingsExchange.prototype.SettingsError = null; 18 SettingsExchange.prototype.isInitialized = false; 19 20 21 /** 22 * Starts the settings exchange process upon onSapLogonSuccess event. 23 * @private 24 */ 25 var doSettingExchange = function () { 26 27 28 sap.Settings.isInitialized = true; 29 var pd =""; 30 sap.Logon.unlock(function (connectionInfo) { 31 if (typeof connectionInfo["registrationContext"] === 'undefined'){ 32 // If registrationContext is not defined, we should assume that an SMP registration 33 // did not actually occur. This can happen if the resume event occurs while the 34 // app is not registered to SMP. 35 return; 36 } 37 var userName = connectionInfo["registrationContext"]["user"]; 38 var password = connectionInfo["registrationContext"]["password"]; 39 var applicationConnectionId = connectionInfo["applicationConnectionId"]; 40 var securityConfig = connectionInfo["registrationContext"]["securityConfig"]; 41 var endpoint = connectionInfo["applicationEndpointURL"]; 42 43 var keySSLEnabled = "false"; 44 var splitendpoint = endpoint.split("/"); 45 if (splitendpoint[0] == "https:") 46 { 47 keySSLEnabled="true"; 48 } 49 if (securityConfig == null) { 50 securityConfig = ""; 51 } 52 var burl = ""; 53 for (var i=2; i<splitendpoint.length-2; i++){ 54 burl += splitendpoint[i] +"/"; 55 } 56 burl += splitendpoint[splitendpoint.length-2]; 57 var appId = splitendpoint[splitendpoint.length-1]; 58 pd = appId+userName+password; 59 connectionData = { 60 "keyMAFLogonOperationContextConnectionData": { 61 "keyMAFLogonConnectionDataApplicationSettings": 62 { 63 "DeviceType":device.platform, 64 "DeviceModel":device.model, 65 "ApplicationConnectionId":applicationConnectionId 66 }, 67 "keyMAFLogonConnectionDataBaseURL":burl 68 }, 69 "keyMAFLogonOperationContextApplicationId":appId, 70 "keyMAFLogonOperationContextBackendUserName":userName, 71 "keyMAFLogonOperationContextBackendPassword":password, 72 "keyMAFLogonOperationContextSecurityConfig":securityConfig, 73 "keySSLEnabled":keySSLEnabled 74 }; 75 sap.Settings.start(connectionData, 76 function(mesg) { 77 sap.Settings.isInitialized = true; 78 sap.Logger.debug("Setting Exchange is successful "+mesg,"SMP_SETTINGS_JS",function(m){},function(m){}); 79 }, 80 function(mesg){ 81 sap.Logger.debug("Setting Exchange failed" + mesg,"SMP_SETTINGS_JS",function(m){},function(m){}); 82 sap.Settings.isInitialized = false; 83 }); 84 } 85 , function () { 86 sap.Logger.debug("unlock failed ","SMP_SETTINGS_JS",function(m){},function(m){}); 87 } 88 ); 89 90 91 }; 92 if (cordova.require("cordova/platform").id.indexOf("windows") === 0) { 93 WinJS.Application.addEventListener("onSapLogonSuccess", doSettingExchange, false); 94 WinJS.Application.addEventListener("onSapResumeSuccess", doSettingExchange, false); 95 } else { 96 document.addEventListener("onSapLogonSuccess", doSettingExchange, false); 97 document.addEventListener("onSapResumeSuccess", doSettingExchange, false); 98 } 99 100 101 SettingsExchange.prototype.reset = function(key, sucessCB, errorCB) 102 { 103 if ((typeof(sap.Settings.store) != undefined) && (sap.Settings.store != null)) { 104 sap.Settings.store.removeItem(key, sucessCB, errorCB); 105 } else { 106 errorCB("Cannot access setting store"); 107 } 108 } 109 110 SettingsExchange.prototype.updateDeviceToken = function(devtoken) { 111 var updateVals = ""; 112 if (device.platform == "Android") { 113 updateVals = {"AndroidGcmPushEnabled":true,AndroidGcmRegistrationId:devtoken}; 114 } 115 else if (device.platform == "iPhone" || device.platform == "iPad" || device.platform == "iPod touch" || device.platform == "iOS") { 116 updateVals = {"ApnsPushEnable":true,ApnsDeviceToken:devtoken}; 117 } 118 sap.Settings.setConfigProperty(updateVals,function(){}, function(){}); 119 } 120 121 122 /** 123 * Starts the settings exchange. 124 * @public 125 * @memberof sap.Settings 126 * @method start 127 * @param {String} connectionData This example below shows the structure of the connection data. 128 * @param {function} successCallback Function to invoke if the exchange is successful. 129 * @param {function} errorCallback Function to invoke if the exchange failed. 130 * @example 131 * connectionData = { 132 * "keyMAFLogonOperationContextConnectionData": { 133 * "keyMAFLogonConnectionDataApplicationSettings": 134 * { 135 * "DeviceType":device.platform, 136 * "DeviceModel":device.model, 137 * "ApplicationConnectionId":"yourappconnectionid" 138 * }, 139 * "keyMAFLogonConnectionDataBaseURL":"servername:port" 140 * }, 141 * "keyMAFLogonOperationContextApplicationId":"yourapplicationid", 142 * "keyMAFLogonOperationContextBackendUserName":"yourusername", 143 * "keyMAFLogonOperationContextBackendPassword":"password", 144 * "keyMAFLogonOperationContextSecurityConfig":"securityConfigName", 145 * "keySSLEnabled":keySSLEnabled 146 * }; 147 * sap.Settings.start(connectionData, function(mesg) { 148 * 149 * sap.Logger.debug("Setting Exchange is successful "+mesg,"SMP_SETTINGS_JS",function(m){},function(m){}); 150 * }, 151 * function(mesg){ 152 * sap.Logger.debug("Setting Exchange failed" + mesg,"SMP_SETTINGS_JS",function(m){},function(m){}); 153 * }); 154 */ 155 SettingsExchange.prototype.start = function (connectionData, successCallback, errorCallback) { 156 sap.Settings.settingsSuccess = successCallback; 157 sap.Settings.SettingsError = errorCallback; 158 sap.Settings.connectionData = connectionData; 159 sap.Logger.debug("Accessing the data from vault","SMP_SETTINGS_JS",function(m){},function(m){}); 160 sap.logon.Core.getSecureStoreObject( sap.Settings.getStoreDataSuccess, sap.Settings.getStoreDataError, "settingsdata"); 161 162 163 }; 164 /** 165 * This function return true if the feature is allowed and false if feature is not permitted. 166 * @public 167 * @param (String} name Name of the feature to fetch. 168 * @param {function} successCallback Function to invoke if the exchange is successful. 169 * @param {function} errorCallback Function to invoke if the exchange failed. 170 **/ 171 172 SettingsExchange.prototype.isFeatureEnabled = function (name, successCallback, errorCallback) { 173 if (!name) { 174 successCallback(false); 175 } else { 176 sap.Settings.getFeatureForName(function (feature) { 177 if (feature == null) { 178 successCallback(true); 179 } else { 180 successCallback(false); 181 } 182 }, 183 function () { 184 errorCallback() 185 }, name); 186 } 187 } 188 189 190 191 192 193 194 /** 195 * This is a private function. End user will not use this plugin directly. 196 * This function gets called after the start function is able to read the current settings from the secured storage. 197 * @private 198 * @param {String} value This is the value of the current setting exchange stored in the secured store. 199 **/ 200 201 SettingsExchange.prototype.getStoreDataSuccess = function(value){ 202 storedSettings = value; 203 sap.Logger.debug("Exchanging the data","SMP_SETTINGS_JS",function(m){},function(m){}); 204 sap.Settings.startSettings(sap.Settings.SettingsExchangeDone, 205 sap.Settings.SettingsExchangeError, 206 [JSON.stringify(connectionData),storedSettings]); 207 208 } 209 210 /** 211 * This is a private function. End user will not use this plugin directly. 212 * This function is called after the start function is unable to read the current settings from the secured storage. 213 * @private 214 * @param {String} message This is the error message produced by the encrypted storage. 215 **/ 216 SettingsExchange.prototype.getStoreDataError = function(mesage){ 217 sap.Logger.debug("Setting exchange failed to read data store: Proceeding without data",function(m){},function(m){}); 218 } 219 220 221 /** 222 * This is a private function. End user will not use this plugin directly. 223 * This function is called after the settings exchange completes succefully. 224 * @private 225 * @param {String} message This is the message produced when the settings plugin completes successfully. 226 **/ 227 228 SettingsExchange.prototype.SettingsExchangeDone = function(message) { 229 sap.Logger.debug("Setting Exchange Success","SMP_SETTINGS_JS",function(m){},function(m){}); 230 var jsondata = JSON.parse(message); 231 settingsString = jsondata["data"]; 232 sap.logon.Core.setSecureStoreObject(sap.Settings.SettingsWriteDone,sap.Settings.SettingsWriteError,"settingsdata",settingsString); 233 if (sap.Settings.settingsSuccess != null) { 234 sap.Logger.debug("Setting exchange successful","SMP_SETTINGS_JS",function(m){},function(m){}); 235 sap.Settings.settingsSuccess("Exchange Completed"); 236 } 237 } 238 239 /** 240 * This is a private function. End user will not use this plugin directly. 241 * This function is called after the settings exchange completes succefully 242 * @private 243 * @param {String} message This is the error message produced when the settings plugin has an error. 244 **/ 245 SettingsExchange.prototype.SettingsExchangeError = function(message) { 246 sap.Logger.error("Setting Exchange failed calling the error callback funciton","SMP_SETTINGS_JS",function(m){},function(m){}); 247 if (sap.Settings.SettingsError != null) { 248 sap.Settings.SettingsError("Exchange Failed"); 249 } 250 } 251 252 /** 253 * This is a private function. End user will not use this plugin directly. 254 * This function is called after the setting data is stored successfully. 255 * @private 256 * @param {String} message This is the message produced upon successful storing of settings to the encrypted store. 257 **/ 258 SettingsExchange.prototype.SettingsWriteDone = function(message) { 259 sap.Logger.debug("Setting stored","SMP_SETTINGS_JS",function(m){},function(m){}); 260 261 } 262 263 /** 264 * This is a private function. End user will not use this plugin directly. 265 * This function is called after the storing of the setting data fails. 266 * @private 267 * @param {String} message This is the message produced upon failure to store the settings to the encrypted store. 268 **/ 269 SettingsExchange.prototype.SettingsWriteError = function(message) { 270 sap.Logger.error("Setting store failed","SMP_SETTINGS_JS",function(m){},function(m){}); 271 } 272 273 /** 274 * This is a private function. End user will not use this plugin directly. 275 * This function is called after the deviceready. This uploads the logs to the server. 276 * @private 277 * @param {boolean} uploadLog This indicates whether the upload log is currently enabled or disbled. 278 **/ 279 SettingsExchange.prototype.logLevelUpdated = function(logLevel) 280 { 281 sap.Logger.setLogLevel(logLevel, sap.Settings.LogLevelSetSuccess, sap.Settings.LogLevelSetFailed); 282 sap.Logger.upload(sap.Settings.logUploadedSuccess, sap.Settings.logUploadFailed); 283 } 284 285 /** 286 * This is a private function. End user will not use this plugin directly. 287 * This function is called when the log upload succeeds. 288 * @private 289 * @param {mesg} logupload message 290 **/ 291 SettingsExchange.prototype.LogLevelSetSuccess = function(mesg){ 292 sap.Logger.debug("Log level set successful","SMP_SETTINGS_JS",function(m){},function(m){}); 293 } 294 /** 295 * This is a private function. End user will not use this plugin directly. 296 * This function is called when the log upload succeeds. 297 * @private 298 * @param {mesg} logupload message 299 **/ 300 SettingsExchange.prototype.LogLevelSetFailed = function(mesg){ 301 sap.Logger.error("Log level set failed","SMP_SETTINGS_JS",function(m){},function(m){}); 302 } 303 304 /** 305 * This is a private function. End user will not use this plugin directly. 306 * This function is called when the log upload succeeds. 307 * @private 308 * @param {mesg} logupload message 309 **/ 310 SettingsExchange.prototype.logUploadedSuccess = function(mesg){ 311 sap.Logger.debug("Log upload successful","SMP_SETTINGS_JS",function(m){},function(m){}); 312 } 313 /** 314 * This is a private function. End user will not use this plugin directly. 315 * This function is called when the log upload fails. 316 * @private 317 * @param {mesg} logupload failure message 318 **/ 319 SettingsExchange.prototype.logUploadFailed = function(mesg) { 320 sap.Logger.error("upload log failed","SMP_SETTINGS_JS",function(m){},function(m){}); 321 322 } 323 324 325 326 module.exports = new SettingsExchange(); 327 328