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 privilege_ = utils_.privilege;
22 var types_ = validator_.Types;
23 var native_ = new xwalk.utils.NativeManager(extension);
28 NOTIFICATION: 'NOTIFICATION',
41 function _createSoundDeviceInfoArray(e) {
42 var devices_array = [];
44 e.forEach(function (data) {
45 devices_array.push(new SoundDeviceInfo(data));
51 function ListenerManager(native, listenerName) {
54 this.nativeSet = false;
56 this.listenerName = listenerName;
59 ListenerManager.prototype.onListenerCalled = function(msg) {
60 var obj = new SoundDeviceInfo(msg);
61 for (var watchId in this.listeners) {
62 if (this.listeners.hasOwnProperty(watchId)) {
63 this.listeners[watchId](obj);
68 ListenerManager.prototype.addListener = function(callback) {
70 if (!this.nativeSet) {
71 this.native.addListener(this.listenerName, this.onListenerCalled.bind(this));
72 this.native.callSync('SoundManager_addDeviceStateChangeListener');
73 this.nativeSet = true;
76 this.listeners[id] = callback;
82 ListenerManager.prototype.removeListener = function(watchId) {
83 if (this.listeners.hasOwnProperty(watchId)) {
84 delete this.listeners[watchId];
86 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
87 'Listener with id: ' + watchId + ' does not exist.');
90 if (this.nativeSet && type_.isEmptyObject(this.listeners)) {
91 this.native.callSync('SoundManager_removeDeviceStateChangeListener');
92 this.native.removeListener(this.listenerName);
93 this.nativeSet = false;
97 var DEVICE_STATE_CHANGE_LISTENER = 'SoundDeviceStateChangeCallback';
98 var soundDeviceStateChangeListener = new ListenerManager(native_, DEVICE_STATE_CHANGE_LISTENER);
100 function SoundManager() {}
102 SoundManager.prototype.getSoundMode = function() {
103 var result = native_.callSync('SoundManager_getSoundMode');
104 if (native_.isFailure(result)) {
105 throw native_.getErrorObject(result);
108 return native_.getResultObject(result);
111 SoundManager.prototype.setVolume = function(type, volume) {
112 utils_.checkPrivilegeAccess(privilege_.VOLUME_SET);
114 var args = validator_.validateArgs(arguments, [
115 {name: 'type', type: types_.ENUM, values: Object.keys(SoundType)},
116 {name: 'volume', type: types_.DOUBLE}
119 if (args.volume < 0 || args.volume > 1) {
120 throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
122 var result = native_.callSync('SoundManager_setVolume', args);
123 if (native_.isFailure(result)) {
124 throw native_.getErrorObject(result);
128 SoundManager.prototype.getVolume = function(type) {
129 var args = validator_.validateArgs(arguments, [
130 {name: 'type', type: types_.ENUM, values: Object.keys(SoundType)}
133 var result = native_.callSync('SoundManager_getVolume', args);
134 if (native_.isFailure(result)) {
135 throw native_.getErrorObject(result);
138 return native_.getResultObject(result);
141 var _soundModeChangeListener;
143 function _soundModeChangeListenerCallback(result) {
144 native_.callIfPossible(_soundModeChangeListener, native_.getResultObject(result));
147 SoundManager.prototype.setSoundModeChangeListener = function(callback) {
148 var args = validator_.validateArgs(arguments, [
149 {name: 'callback', type: types_.FUNCTION}
152 _soundModeChangeListener = args.callback;
153 native_.addListener('SoundModeChangeListener', _soundModeChangeListenerCallback);
155 var result = native_.callSync('SoundManager_setSoundModeChangeListener', {});
157 if (native_.isFailure(result)) {
158 throw native_.getErrorObject(result);
162 SoundManager.prototype.unsetSoundModeChangeListener = function() {
163 native_.removeListener('SoundModeChangeListener', _soundModeChangeListenerCallback);
165 var result = native_.callSync('SoundManager_unsetSoundModeChangeListener', {});
167 _soundModeChangeListener = undefined;
169 if (native_.isFailure(result)) {
170 throw native_.getErrorObject(result);
174 var _volumeChangeListener;
176 function _volumeChangeListenerCallback(result) {
177 native_.callIfPossible(_volumeChangeListener, result.type, result.volume);
180 SoundManager.prototype.setVolumeChangeListener = function(callback) {
181 var args = validator_.validateArgs(arguments, [
182 {name: 'callback', type: types_.FUNCTION}
185 _volumeChangeListener = args.callback;
186 native_.addListener('VolumeChangeListener', _volumeChangeListenerCallback);
188 var result = native_.callSync('SoundManager_setVolumeChangeListener', {});
190 if (native_.isFailure(result)) {
191 throw native_.getErrorObject(result);
195 SoundManager.prototype.unsetVolumeChangeListener = function() {
196 native_.removeListener('VolumeChangeListener', _volumeChangeListenerCallback);
198 var result = native_.callSync('SoundManager_unsetVolumeChangeListener', {});
200 _volumeChangeListener = undefined;
202 if (native_.isFailure(result)) {
203 throw native_.getErrorObject(result);
207 SoundManager.prototype.getConnectedDeviceList = function() {
208 var result = native_.callSync('SoundManager_getConnectedDeviceList', {});
209 if (native_.isFailure(result)) {
210 throw native_.getErrorObject(result);
213 var devices = _createSoundDeviceInfoArray(native_.getResultObject(result));
217 SoundManager.prototype.getActivatedDeviceList = function() {
218 var result = native_.callSync('SoundManager_getActivatedDeviceList', {});
219 if (native_.isFailure(result)) {
220 throw native_.getErrorObject(result);
223 var devices = _createSoundDeviceInfoArray(native_.getResultObject(result));
227 SoundManager.prototype.addDeviceStateChangeListener = function() {
228 var args = validator_.validateArgs(arguments, [
230 name : 'eventCallback',
231 type : types_.FUNCTION
235 return soundDeviceStateChangeListener.addListener(args.eventCallback);
238 SoundManager.prototype.removeDeviceStateChangeListener = function() {
239 var args = validator_.validateArgs(arguments, [
246 soundDeviceStateChangeListener.removeListener(args.watchId);
249 function SoundDeviceInfo(data) {
250 Object.defineProperties(this, {
251 id: {value: data.id, writable: false, enumerable: true},
252 name: {value: data.name, writable: false, enumerable: true},
253 device : {value: data.device, writable: false, enumerable: true},
254 direction : {value: data.direction, writable: false, enumerable: true},
255 isConnected: {value: data.isConnected, writable: false, enumerable: true},
256 isActivated: {value: data.isActivated, writable: false, enumerable: true},
260 exports = new SoundManager();