      1 /*
      2  * This is sound 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 sound API itself, and provides a global namespace for operating sound service.
     10  * @class
     11  */
     12 cordova.define('cordova/plugin/sound', 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      * sound interface
     38      */
     39     var Sound = 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      * @namespace Sound.SoundMode
    109      */
    110     Sound.SoundMode = {
    111 	/**
    112      * standard
    113      * @since 1.4
    114      * @constant
    115      */
    116     Standard : "standard",
    117     /**
    118      * movie
    119      * @since 1.4
    120      * @constant
    121      */
    122     Cinema : "movie",
    123     /**
    124      * news
    125      * @since 1.4
    126      * @constant
    127      */
    128     ClearVoice : "news",
    129     /**
    130      * sports
    131      * @since 1.4
    132      * @constant
    133      */
    134     Sports : "sports",
    135     /**
    136      * music
    137      * @since 1.4
    138      * @constant
    139      */
    140     Music : "music",
    141     /**
    142      * game
    143      * @since 1.4
    144      * @constant
    145      */
    146     Game : "game"
    147     };
    148
    149 	/**
    150      * @namespace Sound.SoundMode
    151      */
    152     Sound.SpeakerType = {
    153     /**
    154      * tv_speaker
    155      * @since 1.4
    156      * @constant
    157      */
    158     SignageSpeaker : "tv_speaker",
    159     /**
    160      * bt_soundbar
    161      * @since 1.4
    162      * @constant
    163      */
    164     LGSoundSync : "bt_soundbar"
    165     };
    166
    167     /**
    168      * Gets sound information
    169      * @class Sound
    170      * @param {Function} successCallback success callback function.
    171      * @param {Function} errorCallback failure callback function.
    172      * @return {Object}
    173      * <div align=left>
    174      * <table class="hcap_spec" width=400>
    175      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th></tr></thead>
    176      *   <tbody>
    177      *       <tr><th>level</th><th>Number</th><th>volume level (0~100)</th></tr>
    178      *       <tr class="odd"><th>muted</th><th>Boolean</th><th>true: mute on / false: mute off </th></tr>
    179      *       <tr><th>externalSpeaker</th><th>Boolean</th><th>true : enabled / false : disabled </th></tr>
    180      *   </tbody>
    181      * </table>
    182      * </div>
    183      *
    184      * @example
    185      * // Javascript code
    186      * function getSoundStatus () {
    187      *   function successCb(cbObject) {
    188      *      console.log("cbObject : " + JSON.stringify(cbObject));
    189      *
    190      *      console.log("level : " + cbObject.level);
    191      *      console.log("muted : " + cbObject.muted);
    192      *      console.log("externalSpeaker : " + cbObject.externalSpeaker);
    193      *
    194      *      // Do something
    195      *         ...
    196      *   }
    197      *
    198      *   function failureCb(cbObject) {
    199      *      var errorCode = cbObject.errorCode;
    200      *      var errorText = cbObject.errorText;
    201      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
    202      *   }
    203      *
    204      *   var sound = new Sound();
    205      *   sound.getSoundStatus(successCb, failureCb);
    206      * }
    207      * @since 1.0
    208      * @see
    209      * <a href="Sound%23setVolumeLevel.html">Sound.setVolumeLevel()</a>,
    210      * <a href="Sound%23setExternalSpeaker.html">Sound.setExternalSpeaker()</a><br>
    211      */
    212     Sound.prototype.getSoundStatus = function(successCallback, errorCallback) {
    213         log("getSoundStatus: ");
    214
    215         service.Request("luna://com.webos.service.config/", {
    216             method: "getConfigs",
    217             parameters: {
    218                 configNames: ["com.webos.surfacemanager.supportCommerSoundSetting"]
    219             },
    220             onSuccess: function(result) {
    221                 log("getConfigs: On Success");
    222
    223                 if (result.returnValue === true) {
    224                     var supportCommerSoundSetting = result.configs["com.webos.surfacemanager.supportCommerSoundSetting"];
    225                     if (supportCommerSoundSetting === true) {
    226                         service.Request("luna://com.webos.audio/", {
    227                             method: "getVolume",
    228                             onSuccess: function(result) {
    229                                 log("getSoundStatus: On Success");
    230
    231                                 if (result.returnValue === true) {
    232                                     var cbObj = {};
    233                                     cbObj.level = result.volume;
    234                                     cbObj.muted = result.muted;
    235
    236                                     service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
    237                                         method: "get",
    238                                         parameters: {
    239                                             category: "commercial",
    240                                             keys: ["enableSpeaker"]
    241                                         },
    242                                         onSuccess: function(result) {
    243                                             log("getSoundStatus: On Success 2");
    244
    245                                             if (result.returnValue === true) {
    246                                                 cbObj.externalSpeaker = (result.settings.enableSpeaker === "on" ? true : false);
    247
    248                                                 if (typeof successCallback === 'function') {
    249                                                     successCallback(cbObj);
    250                                                 }
    251                                             }
    252                                         },
    253                                         onFailure: function(result) {
    254                                             log("getSoundStatus: On Failure 2");
    255                                             delete result.returnValue;
    256                                             if (typeof errorCallback === 'function') {
    257                                                 checkErrorCodeNText(result, "SGSS", "Sound.getSoundStatus returns failure.");
    258                                                 errorCallback(result);
    259                                             }
    260                                         }
    261                                     });
    262                                 }
    263                             },
    264                             onFailure: function(result) {
    265                                 log("getSoundStatus: On Failure");
    266                                 delete result.returnValue;
    267                                 if (typeof errorCallback === 'function') {
    268                                     checkErrorCodeNText(result, "SGSS", "Sound.getSoundStatus returns failure.");
    269                                     errorCallback(result);
    270                                 }
    271                             }
    272                         });
    273                     } else {
    274                         delete result.returnValue;
    275                         if (typeof errorCallback === 'function') {
    276                             checkErrorCodeNText(result, "SGSS", "unsupported feature");
    277                             errorCallback(result);
    278                         }
    279
    280                         return;
    281                     }
    282
    283                     log("Sound.getSoundStatus Done");
    284                 }
    285             },
    286             onFailure: function(result) {
    287                 log("getConfigs: On Failure");
    288                 delete result.returnValue;
    289                 if (typeof errorCallback === 'function') {
    290                     checkErrorCodeNText(result, "SGSS", "Sound.getSoundStatus returns failure.");
    291                     errorCallback(result);
    292                 }
    293             }
    294         });
    295
    296         log("Sound.getSoundStatus Done");
    297
    298     };
    299
    300     /**
    301      * Sets volume level
    302      * @class Sound
    303      * @param {Function} successCallback success callback function.
    304      * @param {Function} errorCallback failure callback function.
    305      * @param {Object} options
    306      * <div align=left>
    307      * <table class="hcap_spec" width=400>
    308      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
    309      *   <tbody>
    310      *       <tr><th>level</th><th>Number</th><th>volume level (0~100)</th><th>required</th></tr>
    311      *       <tr><th>volOsdEnabled</th><th>Boolean</th><th>true: vol osd is enabled(default), false: vol osd is disabled</th><th>optional</th></tr>
    312      *   </tbody>
    313      * </table>
    314      * </div>
    315      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
    316      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
    317      * @example
    318      * // Javascript code
    319      * function setVolumeLevel () {
    320      *   var options = {
    321      *      level : 15,
    322      *      volOsdEnabled : false
    323      *   };
    324      *
    325      *   function successCb() {
    326      *      // Do something
    327      *   }
    328      *
    329      *   function failureCb(cbObject) {
    330      *      var errorCode = cbObject.errorCode;
    331      *      var errorText = cbObject.errorText;
    332      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
    333      *   }
    334      *
    335      *   var sound = new Sound();
    336      *   sound.setVolumeLevel(successCb, failureCb, options);
    337      * }
    338      * @since 1.0
    339      * @see
    340      * <a href="Sound%23getSoundStatus.html">Sound.getSoundStatus()</a><br>
    341      */
    342     Sound.prototype.setVolumeLevel = function (successCallback, errorCallback, options) {
    343
    344         log("setVolumeLevel: " + JSON.stringify(options));
    345
    346         if (typeof options.level !== 'number' || isNaN(options.level) ||
    347             options.level < 0 || options.level > 100) {
    348
    349             if (typeof errorCallback === 'function') {
    350                 var result = {};
    351                 checkErrorCodeNText(result, "SSVL", "Sound.setVolumeLevel returns failure. out of range or invalid parameter type.");
    352                 errorCallback(result);
    353             }
    354
    355             return;
    356         }
    357
    358         service.Request("luna://com.webos.service.config/", {
    359             method: "getConfigs",
    360             parameters: {
    361                 configNames: ["com.webos.surfacemanager.supportCommerSoundSetting"]
    362             },
    363             onSuccess: function(result) {
    364                 log("getConfigs: On Success");
    365
    366                 if (result.returnValue === true) {
    367                     var supportCommerSoundSetting = result.configs["com.webos.surfacemanager.supportCommerSoundSetting"];
    368                     if (supportCommerSoundSetting === true) {
    369                         if (options.volOsdEnabled === false) {
    370                             service.Request("luna://com.webos.service.config/", {
    371                                 method: "setConfigs",
    372                                 parameters: {
    373                                     configs: {
    374                                         "com.webos.surfacemanager.volumeOSD": false
    375                                     }
    376                                 },
    377                                 onSuccess: function(result) {
    378                                     if (result.returnValue === true) {
    379                                         service.Request("luna://com.webos.audio/", {
    380                                             method: "setVolume",
    381                                             parameters: {
    382                                                 volume: options.level
    383                                             },
    384                                             onSuccess: function(result) {
    385                                                 log("setVolumeLevel: On Success");
    386
    387                                                 if (result.returnValue === true) {
    388                                                     setTimeout(function() {
    389                                                         service.Request("luna://com.webos.service.config/", {
    390                                                             method: "setConfigs",
    391                                                             parameters: {
    392                                                                 configs: {
    393                                                                     "com.webos.surfacemanager.volumeOSD": true
    394                                                                 }
    395                                                             },
    396                                                             onSuccess: function(result) {
    397                                                                 if (result.returnValue === true) {
    398                                                                     if (typeof successCallback === 'function') {
    399                                                                         successCallback();
    400                                                                     }
    401                                                                 }
    402                                                             },
    403                                                             onFailure: function(result) {
    404                                                                 delete result.returnValue;
    405                                                                 if (typeof errorCallback === 'function') {
    406                                                                     checkErrorCodeNText(result, "SSVL", "Sound.setVolumeLevel returns failure.");
    407                                                                     errorCallback(result);
    408
    409                                                                     return;
    410                                                                 }
    411                                                             }
    412                                                         });
    413                                                     }, 2000);
    414                                                 }
    415                                             },
    416                                             onFailure: function(result) {
    417                                                 log("setVolumeLevel: On Failure");
    418                                                 delete result.returnValue;
    419                                                 if (typeof errorCallback === 'function') {
    420                                                     checkErrorCodeNText(result, "SSVL", "Sound.setVolumeLevel returns failure.");
    421                                                     errorCallback(result);
    422                                                 }
    423                                             }
    424                                         });
    425                                     }
    426                                 },
    427                                 onFailure: function(result) {
    428                                     delete result.returnValue;
    429                                     if (typeof errorCallback === 'function') {
    430                                         checkErrorCodeNText(result, "SSVL", "Sound.setVolumeLevel returns failure.");
    431                                         errorCallback(result);
    432
    433                                         return;
    434                                     }
    435                                 }
    436                             });
    437                         } else {
    438
    439                             service.Request("luna://com.webos.audio/", {
    440                                 method: "setVolume",
    441                                 parameters: {
    442                                     volume: options.level
    443                                 },
    444                                 onSuccess: function(result) {
    445                                     log("setVolumeLevel: On Success");
    446
    447                                     if (result.returnValue === true) {
    448                                         if (typeof successCallback === 'function') {
    449                                             successCallback();
    450                                         }
    451                                     }
    452                                 },
    453                                 onFailure: function(result) {
    454                                     log("setVolumeLevel: On Failure");
    455                                     delete result.returnValue;
    456                                     if (typeof errorCallback === 'function') {
    457                                         checkErrorCodeNText(result, "SSVL", "Sound.setVolumeLevel returns failure.");
    458                                         errorCallback(result);
    459                                     }
    460                                 }
    461                             });
    462                         }
    463                     } else {
    464                         delete result.returnValue;
    465                         if (typeof errorCallback === 'function') {
    466                             checkErrorCodeNText(result, "SSVL", "unsupported feature");
    467                             errorCallback(result);
    468                         }
    469
    470                         return;
    471                     }
    472                 }
    473             },
    474             onFailure: function(result) {
    475                 log("getConfigs: On Failure");
    476                 delete result.returnValue;
    477                 if (typeof errorCallback === 'function') {
    478                     checkErrorCodeNText(result, "SSVL", "Sound.setVolumeLevel returns failure");
    479                     errorCallback(result);
    480                 }
    481             }
    482         });
    483
    484         log("Sound.setVolumeLevel Done");
    485     };
    486
    487     /**
    488      * Enable or disable external speaker.
    489      * If this method is successfully executed, change the external speaker status.
    490      * @class Sound
    491      * @param {Function} successCallback success callback function.
    492      * @param {Function} errorCallback failure callback function.
    493      * @param {Object} options
    494      * <div align=left>
    495      * <table class="hcap_spec" width=400>
    496      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
    497      *   <tbody>
    498      *       <th>externalSpeaker</th><th>Boolean</th><th>true : enabled / false : disabled </th><th>required</th></tr>
    499      *   </tbody>
    500      * </table>
    501      * </div>
    502      * @return <p>If the method is successfully executed, call the success callback function without a parameter.</br>
    503      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
    504      * @example
    505      * // Javascript code
    506      * function setExternalSpeaker () {
    507      *   var options = {
    508      *      externalSpeaker : true
    509      *   };
    510      *
    511      *   function successCb() {
    512      *      // Do something
    513      *   }
    514      *
    515      *   function failureCb(cbObject) {
    516      *      var errorCode = cbObject.errorCode;
    517      *      var errorText = cbObject.errorText;
    518      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
    519      *   }
    520      *
    521      *   var sound = new Sound();
    522      *   sound.setExternalSpeaker(successCb, failureCb, options);
    523      * }
    524      * @since 1.0
    525      * @see
    526      * <a href="Sound%23getSoundStatus.html">Sound.getSoundStatus()</a><br>
    527      */
    528     Sound.prototype.setExternalSpeaker = function (successCallback, errorCallback, options) {
    529
    530         log("setExternalSpeaker: " + JSON.stringify(options));
    531
    532         if (typeof options.externalSpeaker !== 'boolean') {
    533
    534             if (typeof errorCallback === 'function') {
    535                 var result = {};
    536                 checkErrorCodeNText(result, "SSVL", "Sound.setExternalSpeaker returns failure. out of range or invalid parameter type.");
    537                 errorCallback(result);
    538             }
    539
    540             return;
    541         }
    542
    543         var enable = null;
    544         switch (options.externalSpeaker) {
    545             case true :
    546                 enable = "on";
    547                 break;
    548             case false :
    549                 enable = "off";
    550                 break;
    551         }
    552
    553         service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
    554                 method : "set",
    555                 parameters : {
    556                     category : "commercial",
    557                     settings : {"enableSpeaker":enable}
    558                 },
    559                 onSuccess : function() {
    560                     log("setExternalSpeaker: On Success");
    561                     if (typeof successCallback === 'function') {
    562                         successCallback();
    563                     }
    564                 },
    565                 onFailure : function(result) {
    566                     log("setExternalSpeaker: On Failure");
    567                     delete result.returnValue;
    568                     if (typeof errorCallback === 'function') {
    569                         checkErrorCodeNText(result, "SSES", "Sound.setExternalSpeaker returns failure.");
    570                         errorCallback(result);
    571                     }
    572                 }
    573             });
    574
    575         log("Sound.setExternalSpeaker Done");
    576
    577     };
    578
    579     /**
    580      * mute on/off
    581      * @class Sound
    582      * @param {Function} successCallback success callback function.
    583      * @param {Function} errorCallback failure callback function.
    584      * @param {Object} options
    585      * <div align=left>
    586      * <table class="hcap_spec" width=400>
    587      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
    588      *   <tbody>
    589      *       <tr><th>muted</th><th>Boolean</th><th>true: mute on / false: mute off </th><th>required</th></tr>
    590      *   </tbody>
    591      * </table>
    592      * </div>
    593      * @return <p>If the method is successfully executed, call the success callback function without a parameter.</br>
    594      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
    595      * @example
    596      * // Javascript code
    597      * function setMuted () {
    598      *   var options = {
    599      *      muted : true
    600      *   };
    601      *
    602      *   function successCb() {
    603      *      // Do something
    604      *   }
    605      *
    606      *   function failureCb(cbObject) {
    607      *      var errorCode = cbObject.errorCode;
    608      *      var errorText = cbObject.errorText;
    609      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
    610      *   }
    611      *
    612      *   var sound = new Sound();
    613      *   sound.setMuted(successCb, failureCb, options);
    614      * }
    615      * @since 1.0
    616      * @see
    617      * <a href="Sound%23getSoundStatus.html">Sound.getSoundStatus()</a><br>
    618      */
    619     Sound.prototype.setMuted = function (successCallback, errorCallback, options) {
    620
    621         log("setMuted: " + JSON.stringify(options));
    622
    623         if (typeof options.muted !== 'boolean') {
    624
    625             if (typeof errorCallback === 'function') {
    626                 var result = {};
    627                 checkErrorCodeNText(result, "SSVL", "Sound.setMuted returns failure. out of range or invalid parameter type.");
    628                 errorCallback(result);
    629             }
    630
    631             return;
    632         }
    633
    634         service.Request("luna://com.webos.service.config/", {
    635             method: "getConfigs",
    636             parameters: {
    637                 configNames: ["com.webos.surfacemanager.supportCommerSoundSetting"]
    638             },
    639             onSuccess: function(result) {
    640                 log("getConfigs: On Success");
    641
    642                 if (result.returnValue === true) {
    643                     var supportCommerSoundSetting = result.configs["com.webos.surfacemanager.supportCommerSoundSetting"];
    644                     if (supportCommerSoundSetting === true) {
    645                         service.Request("luna://com.webos.audio/", {
    646                             method: "setMuted",
    647                             parameters: {
    648                                 muted: options.muted
    649                             },
    650                             onSuccess: function(result) {
    651                                 log("setMuted: On Success");
    652
    653                                 if (result.returnValue === true) {
    654                                     if (typeof successCallback === 'function') {
    655                                         successCallback();
    656                                     }
    657                                 }
    658                             },
    659                             onFailure: function(result) {
    660                                 log("setMuted: On Failure");
    661                                 delete result.returnValue;
    662                                 if (typeof errorCallback === 'function') {
    663                                     checkErrorCodeNText(result, "SSM", "Sound.setMuted returns failure.");
    664                                     errorCallback(result);
    665                                 }
    666                             }
    667                         });
    668                     } else {
    669                         delete result.returnValue;
    670                         if (typeof errorCallback === 'function') {
    671                             checkErrorCodeNText(result, "SSM", "unsupported feature");
    672                             errorCallback(result);
    673                         }
    674
    675                         return;
    676                     }
    677                 }
    678             },
    679             onFailure: function(result) {
    680                 log("getConfigs: On Failure");
    681                 delete result.returnValue;
    682                 if (typeof errorCallback === 'function') {
    683                     checkErrorCodeNText(result, "SSM", "Sound.setMuted returns failure");
    684                     errorCallback(result);
    685                 }
    686             }
    687         });
    688
    689         log("Sound.setMuted Done");
    690
    691     };
    692
    693
    694 	/**
    695      * Sets Sound mode. Each <a href="Sound.SoundMode.html#constructor">SoundMode</a> has a set of predefined sound properties. And each sound modes can be changed with setSoundMode().
    696      *
    697      * @class Sound
    698      * @param {Function} successCallback success callback function.
    699      * @param {Function} errorCallback failure callback function.
    700      * @param {Object} options
    701      * <div align=left>
    702      * <table class="hcap_spec" width=400>
    703      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
    704      *   <tbody>
    705      *       <tr><th>mode</th><th>String</th><th><a href="Sound.SoundMode.html#constructor">SoundMode</a></th><th>required</th></tr>
    706      * 		 <tr class="odd"><th>balance</th><th>Number</th><th>-50 ~ +50</th><th>optional</th></tr>
    707      *   </tbody>
    708      * </table>
    709      * </div>
    710      * @example
    711      * // Javascript code
    712      * function setSoundMode () {
    713      *   var options = {
    714 	 *		 mode : Sound.SoundMode.Standard,
    715 	 *		 balance : 20
    716 	 *	 };
    717      *
    718      *   function successCb() {
    719      *      // Do something
    720      *   }
    721      *
    722      *   function failureCb(cbObject) {
    723      *      var errorCode = cbObject.errorCode;
    724      *      var errorText = cbObject.errorText;
    725      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
    726      *   }
    727      *
    728      *   var sound = new Sound();
    729      *   sound.setSoundMode(successCb, failureCb, options);
    730      * }
    731      * @since 1.4
    732      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
    733      * If an error occurs, failure callback function is called with a failure callback object as a parameter.</p>
    734      * @see
    735      * <a href="Sound%23getSoundMode.html">Sound.getSoundMode()</a><br>
    736      */
    737
    738     Sound.prototype.setSoundMode = function (successCallback, errorCallback, options) {
    739         var mode = null;
    740
    741         switch (options.mode) {
    742             case Sound.SoundMode.Standard :
    743                 mode = "standard";
    744                 break;
    745             case Sound.SoundMode.Cinema :
    746                 mode = "movie";
    747                 break;
    748             case Sound.SoundMode.ClearVoice :
    749                 mode = "news";
    750                 break;
    751             case Sound.SoundMode.Sports :
    752                 mode = "sports";
    753                 break;
    754             case Sound.SoundMode.Music :
    755                 mode = "music";
    756                 break;
    757             case Sound.SoundMode.Game :
    758                 mode = "game";
    759                 break;
    760         }
    761
    762         if (((options.balance < -50) || (options.balance > 50)) && isNumber(options.balance)) {
    763             var result = {};
    764             checkErrorCodeNText(result, "SSSM", "Sound.setSoundMode returns failure. Out of range.");
    765             errorCallback(result);
    766             log("Sound.setSoundMode invalid range");
    767         }
    768
    769         log("setSoundMode: " + mode);
    770
    771         if (mode === null && typeof errorCallback === 'function') {
    772             var result = {};
    773             checkErrorCodeNText(result, "SSSM", "Sound.setSoundMode returns failure. command was not defined.");
    774             errorCallback(result);
    775             log("Sound.setSoundMode invalid ");
    776             return;
    777         }
    778
    779         service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
    780             method : "set",
    781             parameters : {
    782                 category : "sound",
    783                 settings : {
    784                     "soundMode" : mode,
    785                     "audioBalance" : options.balance
    786                 }
    787             },
    788             onSuccess : function(result) {
    789                 log("setSoundMode: On Success");
    790
    791                 if (result.returnValue === true) {
    792                     if(typeof successCallback === 'function') {
    793                         successCallback();
    794                     }
    795                 }
    796             },
    797             onFailure : function(result) {
    798                 log("setSoundMode: On Failure");
    799                 delete result.returnValue;
    800                 if (typeof errorCallback === 'function') {
    801                     checkErrorCodeNText(result, "SSSM", "Sound.setSoundMode returns failure.");
    802                     errorCallback(result);
    803                 }
    804             }
    805         });
    806
    807         log("Sound.setSoundMode Done");
    808     };
    809
    810 	/**
    811      * Gets sound mode. Each <a href="Sound.SoundMode.html#constructor">SoundMode</a> has a set of predefined sound properties.
    812      *
    813      * @class Sound
    814      * @param {Function} successCallback success callback function.
    815      * @param {Function} errorCallback failure callback function.
    816      * @return {Object}
    817      * <div align=left>
    818      * <table class="hcap_spec" width=400>
    819      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th></tr></thead>
    820      *   <tbody>
    821      *       <tr><th>mode</th><th>String</th><th><a href="Sound.SoundMode.html#constructor">Sound.SoundMode</a></th></tr>
    822      *   </tbody>
    823      * </table>
    824      * </div>
    825      *
    826      * @example
    827      * // Javascript code
    828      * function getSoundMode () {
    829      *   function successCb(cbObject) {
    830      *      console.log("cbObject : " + JSON.stringify(cbObject));
    831      *      console.log("mode : " + cbObject.mode);
    832      *
    833      *      // Do something
    834      *         ...
    835      *   }
    836      *
    837      *   function failureCb(cbObject) {
    838      *      var errorCode = cbObject.errorCode;
    839      *      var errorText = cbObject.errorText;
    840      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
    841      *   }
    842      *
    843      *   var sound = new Sound();
    844      *   sound.getSoundMode(successCb, failureCb);
    845      * }
    846      * @since 1.4
    847      * @see
    848      * <a href="Sound%23setSoundMode.html">Sound.setSoundMode()</a><br>
    849      */
    850
    851     Sound.prototype.getSoundMode = function (successCallback, errorCallback) {
    852
    853         log("getSoundMode: ");
    854
    855         service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
    856             method: "get",
    857             parameters: {
    858                 category: "sound",
    859                 keys: ["soundMode", "audioBalance"]
    860             },
    861             onSuccess: function(result) {
    862                 log("getSoundMode: On Success");
    863
    864                 if (result.returnValue === true) {
    865                     var cbObj = {};
    866                     cbObj.mode = result.settings.soundMode;
    867                     cbObj.balance = result.settings.audioBalance;
    868
    869                     if (typeof successCallback === 'function') {
    870                         successCallback(cbObj);
    871                     }
    872                 }
    873             },
    874             onFailure: function(result) {
    875                 log("getSoundMode: On Failure");
    876                 delete result.returnValue;
    877                 if (typeof errorCallback === 'function') {
    878                     checkErrorCodeNText(result, "SGSM", "Sound.getSoundMode returns failure.");
    879                     errorCallback(result);
    880                 }
    881             }
    882         });
    883
    884         log("Sound.getSoundMode Done");
    885     };
    886
    887     /**
    888      * Sets Sound speakerType. Each <a href="Sound.SoundOut.html#constructor">SoundOut</a> has a set of predefined sound properties. And each sound speakerTypes can be changed with setSoundOut().
    889      *
    890      * @class Sound
    891      * @param {Function} successCallback success callback function.
    892      * @param {Function} errorCallback failure callback function.
    893      * @param {Object} options
    894      * <div align=left>
    895      * <table class="hcap_spec" width=400>
    896      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
    897      *   <tbody>
    898      *       <tr><th>speakerType</th><th>String</th><th><a href="Sound.SoundOut.html#constructor">SoundOut</a></th><th>required</th></tr>
    899      *   </tbody>
    900      * </table>
    901      * </div>
    902      * @example
    903      * // Javascript code
    904      * function setSoundOut () {
    905      *   var options = {
    906 	 *		 speakerType : Sound.SpeakerType.SignageSpeaker
    907 	 *	 };
    908      *
    909      *   function successCb() {
    910      *      // Do something
    911      *   }
    912      *
    913      *   function failureCb(cbObject) {
    914      *      var errorCode = cbObject.errorCode;
    915      *      var errorText = cbObject.errorText;
    916      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
    917      *   }
    918      *
    919      *   var sound = new Sound();
    920      *   sound.setSoundOut(successCb, failureCb, options);
    921      * }
    922      * @since 1.4
    923      * @return <p>If the method is successfully executed, success callback function is called without a parameter.</br>
    924      * If an error occurs, failure callback function is called with a failure callback object as a parameter.</p>
    925      * @see
    926      * <a href="Sound%23getSoundOut.html">Sound.getSoundOut()</a><br>
    927      */
    928
    929     Sound.prototype.setSoundOut = function (successCallback, errorCallback, options) {
    930         checkPlatformVersion(function(platformInfo){
    931             if (platformInfo.webOSVer !== 3) {
    932                 // only webOS Signage 3.0 support setSoundOut API
    933                 var result = {};
    934                 checkErrorCodeNText(result, "SSSO", "Sound.setSoundOut returns failure. Only webOS 3.0 support setSoundOut API.");
    935                 errorCallback(result);
    936                 log("not support setSoundOut");
    937                 return;
    938             }
    939
    940             service.Request("luna://com.webos.service.config/", {
    941                 method: "getConfigs",
    942                 parameters: {
    943                     configNames: ["system.supportBluetoothFeatures"]
    944                 },
    945                 onSuccess: function(result) {
    946                     log("getConfigs: On Success");
    947
    948                     if (result.returnValue === true) {
    949
    950                         var bluetoothFeatures = result.configs["system.supportBluetoothFeatures"];
    951                         if (bluetoothFeatures.indexOf('btsound') === -1) //not supported btsound
    952                         {
    953                             if (options.speakerType === Sound.SpeakerType.LGSoundSync) {
    954                                 var result = {};
    955                                 checkErrorCodeNText(result, "SSSO", "Sound.setSoundOut returns failure. bluetooth soundsync is not supported.");
    956                                 errorCallback(result);
    957                                 return;
    958                             }
    959                         }
    960
    961                         var speakerType = null;
    962
    963                         switch (options.speakerType) {
    964                             case Sound.SpeakerType.SignageSpeaker:
    965                                 speakerType = "tv_speaker";
    966                                 break;
    967                             case Sound.SpeakerType.LGSoundSync:
    968                                 speakerType = "bt_soundbar";
    969                                 break;
    970                         }
    971
    972                         log("setSoundOut: " + speakerType);
    973
    974                         if (speakerType === null && typeof errorCallback === 'function') {
    975                             var result = {};
    976                             checkErrorCodeNText(result, "SSSO", "Sound.setSoundOut returns failure. command was not defined.");
    977                             errorCallback(result);
    978                             log("Sound.setSoundOut invalid ");
    979                             return;
    980                         }
    981
    982                         service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
    983                             method: "set",
    984                             parameters: {
    985                                 category: "sound",
    986                                 settings: {
    987                                     "soundOutput": speakerType
    988                                 }
    989                             },
    990                             onSuccess: function(result) {
    991                                 log("setSoundOut: On Success");
    992
    993                                 if (result.returnValue === true) {
    994                                     if (typeof successCallback === 'function') {
    995                                         successCallback();
    996                                     }
    997                                 }
    998                             },
    999                             onFailure: function(result) {
    1000                                 log("setSoundOut: On Failure");
    1001                                 delete result.returnValue;
    1002                                 if (typeof errorCallback === 'function') {
    1003                                     checkErrorCodeNText(result, "SSSO", "Sound.setSoundOut returns failure.");
    1004                                     errorCallback(result);
    1005                                 }
    1006                             }
    1007                         });
    1008
    1009                         log("Sound.setSoundOut Done");
    1010
    1011                     }
    1012                 },
    1013                 onFailure: function(result) {
    1014                     log("getConfigs: On Failure");
    1015                     delete result.returnValue;
    1016                     if (typeof errorCallback === 'function') {
    1017                         checkErrorCodeNText(result, "SSSO", "unsupported feature");
    1018                         errorCallback(result);
    1019                     }
    1020                 }
    1021             });
    1022         });
    1023     };
    1024
    1025 	/**
    1026      * Gets sound speakerType. Each <a href="Sound.SoundOut.html#constructor">SoundOut</a> has a set of predefined sound properties.
    1027      *
    1028      * @class Sound
    1029      * @param {Function} successCallback success callback function.
    1030      * @param {Function} errorCallback failure callback function.
    1031      * @return {Object}
    1032      * <div align=left>
    1033      * <table class="hcap_spec" width=400>
    1034      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th></tr></thead>
    1035      *   <tbody>
    1036      *       <tr><th>speakerType</th><th>String</th><th><a href="Sound.SoundOut.html#constructor">Sound.SoundOut</a></th></tr>
    1037      *   </tbody>
    1038      * </table>
    1039      * </div>
    1040      *
    1041      * @example
    1042      * // Javascript code
    1043      * function getSoundOut () {
    1044      *   function successCb(cbObject) {
    1045      *      console.log("cbObject : " + JSON.stringify(cbObject));
    1046      *      console.log("speakerType : " + cbObject.speakerType);
    1047      *
    1048      *      // Do something
    1049      *         ...
    1050      *   }
    1051      *
    1052      *   function failureCb(cbObject) {
    1053      *      var errorCode = cbObject.errorCode;
    1054      *      var errorText = cbObject.errorText;
    1055      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
    1056      *   }
    1057      *
    1058      *   var sound = new Sound();
    1059      *   sound.getSoundOut(successCb, failureCb);
    1060      * }
    1061      * @since 1.4
    1062      * @see
    1063      * <a href="Sound%23setSoundOut.html">Sound.setSoundOut()</a><br>
    1064      */
    1065
    1066     Sound.prototype.getSoundOut = function(successCallback, errorCallback) {
    1067         checkPlatformVersion(function(platformInfo) {
    1068             if (platformInfo.webOSVer !== 3) {
    1069                 // only webOS Signage 3.0 support getSoundOut API
    1070                 var result = {};
    1071                 checkErrorCodeNText(result, "SGSO", "Sound.getSoundOut returns failure. Only webOS 3.0 support getSoundOut API.");
    1072                 errorCallback(result);
    1073                 log("not support getSoundOut");
    1074                 return;
    1075             }
    1076
    1077             log("getSoundOut: ");
    1078
    1079             service.Request("luna://com.webos.service.commercial.signage.storageservice/settings/", {
    1080                 method: "get",
    1081                 parameters: {
    1082                     category: "sound",
    1083                     keys: ["soundOutput"]
    1084                 },
    1085                 onSuccess: function(result) {
    1086                     log("getSoundOut: On Success");
    1087
    1088                     if (result.returnValue === true) {
    1089                         var cbObj = {};
    1090                         cbObj.speakerType = result.settings.soundOutput;
    1091
    1092                         if (typeof successCallback === 'function') {
    1093                             successCallback(cbObj);
    1094                         }
    1095                     }
    1096                 },
    1097                 onFailure: function(result) {
    1098                     log("getSoundOut: On Failure");
    1099                     delete result.returnValue;
    1100                     if (typeof errorCallback === 'function') {
    1101                         checkErrorCodeNText(result, "SGSO", "Sound.getSoundOut returns failure.");
    1102                         errorCallback(result);
    1103                     }
    1104                 }
    1105             });
    1106
    1107             log("Sound.getSoundOut Done");
    1108         });
    1109     };
    1110
    1111     module.exports = Sound;
    1112 });
    1113
    1114 Sound = cordova.require('cordova/plugin/sound'); // jshint ignore:line
    1115
    1116