Merge "[Systeminfo] - refactoring logs" into tizen_3.0
[platform/core/api/webapi-plugins.git] / src / sound / sound_api.js
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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);
24
25
26 var SoundType = {
27   SYSTEM: 'SYSTEM',
28   NOTIFICATION: 'NOTIFICATION',
29   ALARM: 'ALARM',
30   MEDIA: 'MEDIA',
31   VOICE: 'VOICE',
32   RINGTONE: 'RINGTONE'
33 };
34
35 var SoundModeType = {
36   SOUND: 'SOUND',
37   VIBRATE: 'VIBRATE',
38   MUTE: 'MUTE'
39 };
40
41 function _createSoundDeviceInfoArray(e) {
42   var devices_array = [];
43
44   e.forEach(function (data) {
45     devices_array.push(new SoundDeviceInfo(data));
46   });
47
48   return devices_array;
49 };
50
51 function ListenerManager(native, listenerName) {
52   this.listeners = {};
53   this.nextId = 1;
54   this.nativeSet = false;
55   this.native = native;
56   this.listenerName = listenerName;
57 };
58
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);
64     }
65   }
66 };
67
68 ListenerManager.prototype.addListener = function(callback) {
69   var id = this.nextId;
70   if (!this.nativeSet) {
71     this.native.addListener(this.listenerName, this.onListenerCalled.bind(this));
72     this.native.callSync('SoundManager_addDeviceStateChangeListener');
73     this.nativeSet = true;
74   }
75
76   this.listeners[id] = callback;
77   ++this.nextId;
78
79   return id;
80 };
81
82 ListenerManager.prototype.removeListener = function(watchId) {
83   if (this.listeners.hasOwnProperty(watchId)) {
84     delete this.listeners[watchId];
85   } else {
86     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR,
87         'Listener with id: ' + watchId + ' does not exist.');
88   }
89
90   if (this.nativeSet && type_.isEmptyObject(this.listeners)) {
91       this.native.callSync('SoundManager_removeDeviceStateChangeListener');
92       this.native.removeListener(this.listenerName);
93       this.nativeSet = false;
94   }
95 };
96
97 var DEVICE_STATE_CHANGE_LISTENER = 'SoundDeviceStateChangeCallback';
98 var soundDeviceStateChangeListener = new ListenerManager(native_, DEVICE_STATE_CHANGE_LISTENER);
99
100 function SoundManager() {}
101
102 SoundManager.prototype.getSoundMode = function() {
103   var result = native_.callSync('SoundManager_getSoundMode');
104   if (native_.isFailure(result)) {
105     throw native_.getErrorObject(result);
106   }
107
108   return native_.getResultObject(result);
109 };
110
111 SoundManager.prototype.setVolume = function(type, volume) {
112   utils_.checkPrivilegeAccess(privilege_.VOLUME_SET);
113
114   var args = validator_.validateArgs(arguments, [
115     {name: 'type', type: types_.ENUM, values: Object.keys(SoundType)},
116     {name: 'volume', type: types_.DOUBLE}
117   ]);
118
119   if (args.volume < 0 || args.volume > 1) {
120     throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
121   }
122   var result = native_.callSync('SoundManager_setVolume', args);
123   if (native_.isFailure(result)) {
124     throw native_.getErrorObject(result);
125   }
126 };
127
128 SoundManager.prototype.getVolume = function(type) {
129   var args = validator_.validateArgs(arguments, [
130     {name: 'type', type: types_.ENUM, values: Object.keys(SoundType)}
131   ]);
132
133   var result = native_.callSync('SoundManager_getVolume', args);
134   if (native_.isFailure(result)) {
135     throw native_.getErrorObject(result);
136   }
137
138   return native_.getResultObject(result);
139 };
140
141 var _soundModeChangeListener;
142
143 function _soundModeChangeListenerCallback(result) {
144   native_.callIfPossible(_soundModeChangeListener, native_.getResultObject(result));
145 }
146
147 SoundManager.prototype.setSoundModeChangeListener = function(callback) {
148   var args = validator_.validateArgs(arguments, [
149     {name: 'callback', type: types_.FUNCTION}
150   ]);
151
152   _soundModeChangeListener = args.callback;
153   native_.addListener('SoundModeChangeListener', _soundModeChangeListenerCallback);
154
155   var result = native_.callSync('SoundManager_setSoundModeChangeListener', {});
156
157   if (native_.isFailure(result)) {
158     throw native_.getErrorObject(result);
159   }
160 };
161
162 SoundManager.prototype.unsetSoundModeChangeListener = function() {
163   native_.removeListener('SoundModeChangeListener', _soundModeChangeListenerCallback);
164
165   var result = native_.callSync('SoundManager_unsetSoundModeChangeListener', {});
166
167   _soundModeChangeListener = undefined;
168
169   if (native_.isFailure(result)) {
170     throw native_.getErrorObject(result);
171   }
172 };
173
174 var _volumeChangeListener;
175
176 function _volumeChangeListenerCallback(result) {
177   native_.callIfPossible(_volumeChangeListener, result.type, result.volume);
178 }
179
180 SoundManager.prototype.setVolumeChangeListener = function(callback) {
181   var args = validator_.validateArgs(arguments, [
182     {name: 'callback', type: types_.FUNCTION}
183   ]);
184
185   _volumeChangeListener = args.callback;
186   native_.addListener('VolumeChangeListener', _volumeChangeListenerCallback);
187
188   var result = native_.callSync('SoundManager_setVolumeChangeListener', {});
189
190   if (native_.isFailure(result)) {
191     throw native_.getErrorObject(result);
192   }
193 };
194
195 SoundManager.prototype.unsetVolumeChangeListener = function() {
196   native_.removeListener('VolumeChangeListener', _volumeChangeListenerCallback);
197
198   var result = native_.callSync('SoundManager_unsetVolumeChangeListener', {});
199
200   _volumeChangeListener = undefined;
201
202   if (native_.isFailure(result)) {
203     throw native_.getErrorObject(result);
204   }
205 };
206
207 SoundManager.prototype.getConnectedDeviceList = function() {
208   var result = native_.callSync('SoundManager_getConnectedDeviceList', {});
209   if (native_.isFailure(result)) {
210     throw native_.getErrorObject(result);
211   }
212
213   var devices = _createSoundDeviceInfoArray(native_.getResultObject(result));
214   return devices;
215 };
216
217 SoundManager.prototype.getActivatedDeviceList = function() {
218   var result = native_.callSync('SoundManager_getActivatedDeviceList', {});
219   if (native_.isFailure(result)) {
220     throw native_.getErrorObject(result);
221   }
222
223   var devices = _createSoundDeviceInfoArray(native_.getResultObject(result));
224   return devices;
225 };
226
227 SoundManager.prototype.addDeviceStateChangeListener = function() {
228   var args = validator_.validateArgs(arguments, [
229     {
230        name : 'eventCallback',
231        type : types_.FUNCTION
232      }
233   ]);
234
235   return soundDeviceStateChangeListener.addListener(args.eventCallback);
236 };
237
238 SoundManager.prototype.removeDeviceStateChangeListener = function() {
239   var args = validator_.validateArgs(arguments, [
240     {
241        name : 'watchId',
242        type : types_.LONG
243     }
244   ]);
245
246   soundDeviceStateChangeListener.removeListener(args.watchId);
247 };
248
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},
257   });
258 };
259
260 exports = new SoundManager();