2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var utils_ = xwalk.utils;
18 var type_ = utils_.type;
19 var converter_ = utils_.converter;
20 var validator_ = utils_.validator;
21 var types_ = validator_.Types;
22 var native_ = new xwalk.utils.NativeManager(extension);
27 NOTIFICATION: 'NOTIFICATION',
40 function _createSoundDeviceInfoArray(e) {
41 var devices_array = [];
43 e.forEach(function (data) {
44 devices_array.push(new SoundDeviceInfo(data));
50 function ListenerManager(native, listenerName) {
53 this.nativeSet = false;
55 this.listenerName = listenerName;
58 ListenerManager.prototype.onListenerCalled = function(msg) {
59 var obj = new SoundDeviceInfo(msg);
60 for (var watchId in this.listeners) {
61 if (this.listeners.hasOwnProperty(watchId)) {
62 this.listeners[watchId](obj);
67 ListenerManager.prototype.addListener = function(callback) {
69 if (!this.nativeSet) {
70 this.native.addListener(this.listenerName, this.onListenerCalled.bind(this));
71 this.native.callSync('SoundManager_addDeviceStateChangeListener');
72 this.nativeSet = true;
75 this.listeners[id] = callback;
81 ListenerManager.prototype.removeListener = function(watchId) {
82 if (this.listeners.hasOwnProperty(watchId)) {
83 delete this.listeners[watchId];
85 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
86 'Listener with id: ' + watchId + ' does not exist.');
89 if (this.nativeSet && type_.isEmptyObject(this.listeners)) {
90 this.native.callSync('SoundManager_removeDeviceStateChangeListener');
91 this.native.removeListener(this.listenerName);
92 this.nativeSet = false;
96 var DEVICE_STATE_CHANGE_LISTENER = 'SoundDeviceStateChangeCallback';
97 var soundDeviceStateChangeListener = new ListenerManager(native_, DEVICE_STATE_CHANGE_LISTENER);
99 function SoundManager() {}
101 SoundManager.prototype.getSoundMode = function() {
102 var result = native_.callSync('SoundManager_getSoundMode');
103 if (native_.isFailure(result)) {
104 throw native_.getErrorObject(result);
107 return native_.getResultObject(result);
110 SoundManager.prototype.setVolume = function(type, volume) {
111 var args = validator_.validateArgs(arguments, [
112 {name: 'type', type: types_.ENUM, values: Object.keys(SoundType)},
113 {name: 'volume', type: types_.DOUBLE}
116 if (args.volume < 0 || args.volume > 1) {
117 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
119 var result = native_.callSync('SoundManager_setVolume', args);
120 if (native_.isFailure(result)) {
121 throw native_.getErrorObject(result);
125 SoundManager.prototype.getVolume = function(type) {
126 var args = validator_.validateArgs(arguments, [
127 {name: 'type', type: types_.ENUM, values: Object.keys(SoundType)}
130 var result = native_.callSync('SoundManager_getVolume', args);
131 if (native_.isFailure(result)) {
132 throw native_.getErrorObject(result);
135 return native_.getResultObject(result);
138 var _soundModeChangeListener;
140 function _soundModeChangeListenerCallback(result) {
141 native_.callIfPossible(_soundModeChangeListener, native_.getResultObject(result));
144 SoundManager.prototype.setSoundModeChangeListener = function(callback) {
145 var args = validator_.validateArgs(arguments, [
146 {name: 'callback', type: types_.FUNCTION}
149 _soundModeChangeListener = args.callback;
150 native_.addListener('SoundModeChangeListener', _soundModeChangeListenerCallback);
152 var result = native_.callSync('SoundManager_setSoundModeChangeListener', {});
154 if (native_.isFailure(result)) {
155 throw native_.getErrorObject(result);
159 SoundManager.prototype.unsetSoundModeChangeListener = function() {
160 native_.removeListener('SoundModeChangeListener', _soundModeChangeListenerCallback);
162 var result = native_.callSync('SoundManager_unsetSoundModeChangeListener', {});
164 _soundModeChangeListener = undefined;
166 if (native_.isFailure(result)) {
167 throw native_.getErrorObject(result);
171 var _volumeChangeListener;
173 function _volumeChangeListenerCallback(result) {
174 native_.callIfPossible(_volumeChangeListener, result.type, result.volume);
177 SoundManager.prototype.setVolumeChangeListener = function(callback) {
178 var args = validator_.validateArgs(arguments, [
179 {name: 'callback', type: types_.FUNCTION}
182 _volumeChangeListener = args.callback;
183 native_.addListener('VolumeChangeListener', _volumeChangeListenerCallback);
185 var result = native_.callSync('SoundManager_setVolumeChangeListener', {});
187 if (native_.isFailure(result)) {
188 throw native_.getErrorObject(result);
192 SoundManager.prototype.unsetVolumeChangeListener = function() {
193 native_.removeListener('VolumeChangeListener', _volumeChangeListenerCallback);
195 var result = native_.callSync('SoundManager_unsetVolumeChangeListener', {});
197 _volumeChangeListener = undefined;
199 if (native_.isFailure(result)) {
200 throw native_.getErrorObject(result);
204 SoundManager.prototype.getConnectedDeviceList = function() {
205 var result = native_.callSync('SoundManager_getConnectedDeviceList', {});
206 if (native_.isFailure(result)) {
207 throw native_.getErrorObject(result);
210 var devices = _createSoundDeviceInfoArray(native_.getResultObject(result));
214 SoundManager.prototype.getActivatedDeviceList = function() {
215 var result = native_.callSync('SoundManager_getActivatedDeviceList', {});
216 if (native_.isFailure(result)) {
217 throw native_.getErrorObject(result);
220 var devices = _createSoundDeviceInfoArray(native_.getResultObject(result));
224 SoundManager.prototype.addDeviceStateChangeListener = function() {
225 var args = validator_.validateArgs(arguments, [
227 name : 'eventCallback',
228 type : types_.FUNCTION
232 return soundDeviceStateChangeListener.addListener(args.eventCallback);
235 SoundManager.prototype.removeDeviceStateChangeListener = function() {
236 var args = validator_.validateArgs(arguments, [
243 soundDeviceStateChangeListener.removeListener(args.watchId);
246 function SoundDeviceInfo(data) {
247 Object.defineProperties(this, {
248 id: {value: data.id, writable: false, enumerable: true},
249 name: {value: data.name, writable: false, enumerable: true},
250 device : {value: data.device, writable: false, enumerable: true},
251 direction : {value: data.direction, writable: false, enumerable: true},
252 isConnected: {value: data.isConnected, writable: false, enumerable: true},
253 isActivated: {value: data.isActivated, writable: false, enumerable: true},
257 exports = new SoundManager();