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