      1 /*
      2  * This is utility cordova_plugin (TV specific API).
      3  * Apache License (2004). See http://www.apache.org/licenses/LICENSE-2.0
      4  *
      5  * Copyright (c) 2014, LG Electronics, Inc.
      6  */
      7
      8 /**
      9  * This represents the utility API itself, and provides a global namespace for operating utility service.
     10  * @class
     11  */
     12 cordova.define('cordova/plugin/utility', function (require, exports, module) { // jshint ignore:line
     13
     14     function log(msg) {
     15     //    //console.log//will be removed // jshint ignore:line
     16     }
     17
     18     var service;
     19     if (window.PalmSystem) { // jshint ignore:line
     20         log("Window.PalmSystem Available");
     21         service = require('cordova/plugin/webos/service');
     22     } else {
     23         service = {
     24             Request : function(uri, params) {
     25                 log(uri + " invoked. But I am a dummy because PalmSystem is not available");
     26
     27                 if (typeof params.onFailure === 'function') {
     28                     params.onFailure({
     29                         returnValue:false,
     30                         errorText:"PalmSystem Not Available. Cordova is not installed?"
     31                     });
     32                }
     33         }};
     34     }
     35
     36     /**
     37      * utility interface
     38      */
     39     var Utility = function () {
     40     };
     41
     42     function checkErrorCodeNText(result, errorCode, errorText) {
     43
     44         if (result.errorCode === undefined || result.errorCode === null ) {
     45             result.errorCode = errorCode;
     46         }
     47         if (result.errorText ===undefined || result.errorText === null) {
     48             result.errorText = errorText;
     49         }
     50     }
     51
     52     var version = null;
     53     var platformInfoObj = {};
     54     function checkPlatformVersion(cb) {
     55
     56         if (version === null) {
     57
     58             service.Request('luna://com.webos.service.tv.systemproperty', {
     59                 method: 'getSystemInfo',
     60                 parameters: {
     61                     keys: ["sdkVersion", "boardType"]
     62                 },
     63                 onSuccess: function(result) {
     64                     log("getPlatformInfo: onSuccess");
     65                     log("version : " + result.sdkVersion);
     66
     67                     var temp = result.sdkVersion.split('.');
     68                     if (temp.length >= 1 && temp[0] === '1') {
     69                         platformInfoObj = {
     70                             webOSVer: 1,
     71                             chipset: result.boardType.split("_")[0]
     72                         };
     73                     } else if (temp.length >= 1 && temp[0] === '2') {
     74                         platformInfoObj = {
     75                             webOSVer: 2,
     76                             chipset: result.boardType.split("_")[0]
     77                         };
     78                     } else if (temp.length >= 1 && temp[0] === '3') {
     79                         platformInfoObj = {
     80                             webOSVer: 3,
     81                             chipset: result.boardType.split("_")[0]
     82                         };
     83                     } else {
     84                         platformInfoObj = {
     85                             webOSVer: 0,
     86                             chipset: ""
     87                         };
     88                     }
     89                     version = platformInfoObj.webOSVer;
     90                     cb(platformInfoObj);
     91                 },
     92                 onFailure: function(error) {
     93                     log("getPlatformInfo: onFailure");
     94                     platformInfoObj = {
     95                         webOSVer: 0,
     96                         chipset: ""
     97                     }
     98                     cb(platformInfoObj);
     99                 }
    100             });
    101
    102         } else {
    103             cb(platformInfoObj);
    104         }
    105     }
    106
    107 		/**
    108      * create ToastMessage
    109      * @class Utility
    110      * @param {Function} successCallback success callback function.
    111      * @param {Function} errorCallback failure callback function.
    112      * @param {Object}
    113      * <div align=left>
    114      * <table class="hcap_spec" width=400>
    115      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
    116      *   <tbody>
    117      *       <tr><th>msg</th><th>String</th><th>output string to toast message</th><th>required</th></tr>
    118      *   </tbody>
    119      * </table>
    120      * </div>
    121      * @return <p>If the method is successfully executed, call the success callback function without a parameter.</br>
    122      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
    123      * @example
    124      * // Javascript code
    125      *
    126      * function createToast () {
    127      *   var options = {
    128      *      msg : "test message"
    129      *   };
    130      *
    131      *   function successCb() {
    132      *      // Do something
    133      *   }
    134      *
    135      *   function failureCb(cbObject) {
    136      *      var errorCode = cbObject.errorCode;
    137      *      var errorText = cbObject.errorText;
    138      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
    139      *   }
    140      *
    141      *   var utility = new Utility();
    142      *   utility.createToast(successCb, failureCb, options);
    143      * }
    144      * @since 1.4
    145      */
    146     Utility.prototype.createToast = function (successCallback, errorCallback, options) {
    147             log("createToast: " + options.msg);
    148
    149             if (options.msg === null && typeof errorCallback === 'function') {
    150                 var result = {};
    151                 checkErrorCodeNText(result, "UTCT", "Utility.createToast returns failure. command was not defined.");
    152                 errorCallback(result);
    153                 log("Utility.createToast invalid ");
    154                 return;
    155             }
    156
    157             service.Request("luna://com.webos.service.commercial.signage.storageservice/", {
    158                 method: "createToast",
    159                 parameters: {
    160                     text : options.msg
    161                 },
    162                 onSuccess: function(result) {
    163                     log("createToast: On Success");
    164
    165                     if (result.returnValue === true) {
    166                         if (typeof successCallback === 'function') {
    167                             successCallback();
    168                         }
    169                     }
    170                 },
    171                 onFailure: function(result) {
    172                     log("createToast: On Failure");
    173                     delete result.returnValue;
    174                     if (typeof errorCallback === 'function') {
    175                         checkErrorCodeNText(result, "UTCT", "Utility.createToast returns failure.");
    176                         errorCallback(result);
    177                     }
    178                 }
    179             });
    180
    181             log("Utility.createToast Done");
    182     };
    183
    184     module.exports = Utility;
    185 });
    186
    187 Utility = cordova.require('cordova/plugin/utility'); // jshint ignore:line
    188
    189