Add support for automatically sending modem power on request
[profile/ivi/sockdrawer.git] / Tizen.Device.js
1 ///////////////////////////////////////////////////////////////////////////////
2 // This is a partial implementation of the tizen.application web specification
3 // which using a websocket server for implementing the native backend
4
5 var __application = function () {
6     var appList;
7     var callback;
8
9     var socket = new WebSocket("ws://localhost:7681", "app-list-protocol");
10     socket.onmessage = function(msg) {
11         appList = JSON.parse(msg.data);
12         if (callback)
13             callback(appList);
14     }
15
16     this.launch = function (id, successCallback, errorCallback, argument) {
17         var cmd = {"cmd": "launch", "index": id};
18         socket.send(JSON.stringify(cmd));
19     }
20     this.getAppInfo = function(id) {
21         return appList[id];
22     }
23     this.addAppInfoEventListener = function(eventCallback, errorCallback) {
24         callback = eventCallback;
25     }
26 }
27
28 var __callmanager = function () {
29     var calls = new Array();
30     var callHandlers = new Array();
31     var socket = new WebSocket("ws://localhost:7681", "dialer-protocol");
32     var services = new Array();
33
34     var listener = {
35         "onAccountUpdated": function(account) {
36         },
37         "onAccountAdded": function(account) {
38             services.push(new __callservice(socket, account.id));
39         },
40         "onAccountRemoved": function(id) {
41             var tmp = new Array();
42             for (var i in services)
43                 if (services[i].id != id)
44                     tmp.push(id);
45             services = tmp;
46         }
47     }
48     tizen.__account.addAccountListener(listener);
49
50     socket.onmessage = function(msg) {
51         var ev = JSON.parse(msg.data);
52
53         if (ev.type == "ModemAdded") {
54             tizen.__account.addAccount(new tizen.Account(ev.ModemAdded, ev));
55         } else if (ev.type == "ModemRemoved") {
56             tizen.__account.deleteAccount(ev.id);
57         } else if (ev.type == "CallAdded") {
58             calls.push(new __call(this, socket, ev));
59         } else if (ev.type == "CallRemoved") {
60             var tmp = new Array();
61             for (var i in calls)
62                 if (calls[i].id != ev.id)
63                     tmp.push(calls[i]);
64             calls = tmp;
65         } else if (ev.type == "PropertyChanged" && ev.State) {
66             try {
67                 if (ev.State == "incoming") {
68                     for (var index in callHandlers)
69                         for (var i in calls)
70                             if (calls[i].id == ev.id)
71                                 callHandlers[index].onIncoming(calls[i]);
72                 } else if (ev.State == "dialing") {
73                     for (var index in callHandlers)
74                         for (var i in calls)
75                             if (calls[i].id == ev.id)
76                                 callHandlers[index].onDialing(calls[i]);
77                 } else if (ev.State == "alerting") {
78                     for (var index in callHandlers)
79                         for (var i in calls)
80                             if (calls[i].id == ev.id)
81                                 callHandlers[index].onAlerting(calls[i]);
82                 } else if (ev.State == "hold") {
83                     for (var index in callHandlers)
84                         for (var i in calls)
85                             if (calls[i].id == ev.id)
86                                 callHandlers[index].onHold(calls[i]);
87                 } else if (ev.State == "waiting") {
88                     for (var index in callHandlers)
89                         for (var i in calls)
90                             if (calls[i].id == ev.id)
91                                 callHandlers[index].onWaiting(calls[i]);
92                 } else if (ev.State == "disconnected") {
93                     for (var index in callHandlers)
94                         for (var i in calls)
95                             if (calls[i].id == ev.id)
96                                 callHandlers[index].onDisconnected(calls[i]);
97                 } else if (ev.State == "disconnecting") {
98                     for (var index in callHandlers)
99                         for (var i in calls)
100                             if (calls[i].id == ev.id)
101                                 callHandlers[index].onDisconnecting(calls[i]);
102                 } else if (ev.State == "accepted") {
103                     for (var index in callHandlers)
104                         for (var i in calls)
105                             if (calls[i].id == ev.id)
106                                 callHandlers[index].onAccepted(calls[i]);
107                 } else if (ev.State == "remotelyheld") {
108                     for (var index in callHandlers)
109                         for (var i in calls)
110                             if (calls[i].id == ev.id)
111                                 callHandlers[index].onRemotelyHeld(calls[i]);
112                 } else if (ev.State == "active") {
113                     for (var index in callHandlers)
114                         for (var i in calls)
115                             if (calls[i].id == ev.id)
116                                 callHandlers[index].onActivated(calls[i]);
117                 } else if (ev.State == "initializing") {
118                     for (var index in callHandlers)
119                         for (var i in calls)
120                             if (calls[i].id == ev.id)
121                                 callHandlers[index].onInitializing(calls[i]);
122                 } else if (ev.State == "initialized") {
123                     for (var index in callHandlers)
124                         for (var i in calls)
125                             if (calls[i].id == ev.id)
126                                 callHandlers[index].onInitialized(calls[i]);
127                 } else {
128                     console.log("Unhandled state: " + ev.State);
129                 }
130             } catch (err) {
131                 // handlers do not have to implement every callback
132             }
133         } else {
134             console.log("Unhandled event");
135             console.log(ev);
136         }
137     }
138
139     this.history = null;
140     this.isCallInProgress = function() {
141         return calls.length > 0;
142     }
143     this.addCallHandler = function(handler, callServiceType, serviceName) {
144         callHandlers.push(handler);
145     }
146     this.removeCallHandler = function(handler) {
147         var tmp = new Array();
148         for (var item in callHandlers)
149             if (item != handler)
150                 tmp.push(item);
151         callHandlers = tmp;
152
153     }
154     this.getCallService = function(account) {
155         try {
156             for (var i in services)
157                 if (services[i].id == account.id)
158                     return services[i];
159         } catch (ex) {}
160
161         return services[0];
162     }
163 }
164
165 var __callservice = function (wsi, serviceid) {
166     var socket = wsi;
167
168     var id = serviceid;
169     this.__defineGetter__("id", function() {
170         return id;
171     });
172
173     this.launchDialer = function(remotePartyId) {
174         socket.send(JSON.stringify({ "cmd": "launchDialer" }));
175     }
176
177     var voicemailNumbers = new Array();
178     this.__defineGetter__("voiceNumbers", function() {
179         return voicemailNumbers;
180     });
181
182     this.makeCall = function(remotePartyId, handler, extension, localVideoOn) {
183         var c = {
184             "cmd": "makeCall",
185             "remotePartyId": remotePartyId.toString(),
186             "extension": extension,
187             "localVideoOn": localVideoOn
188         };
189         socket.send(JSON.stringify(c));
190     }
191 }
192
193 var __call = function(manager, wsi, data) {
194     var socket = wsi;
195
196     var id = data.id;
197     this.__defineGetter__("id", function() {
198         return id;
199     });
200
201     var callData = data;
202     this.__defineGetter__("callData", function() {
203         return callData;
204     });
205
206     var streamList = new Array();
207     this.__defineGetter__("streamList", function() {
208         return streamList;
209     });
210
211     this.accept = function() {
212         socket.send(JSON.stringify({"cmd": "accept"}));
213     }
214     this.reject = function() {
215         socket.send(JSON.stringify({"cmd": "reject"}));
216     }
217     this.wait = function() {
218         socket.send(JSON.stringify({"cmd": "wait"}));
219     }
220     this.redirect = function(rid) {
221         socket.send(JSON.stringify({"cmd": "redirect"}));
222     }
223     this.end = function() {
224         socket.send(JSON.stringify({"cmd": "end", "path": id}));
225     }
226     this.hold = function() {
227         socket.send(JSON.stringify({"cmd": "hold"}));
228     }
229     this.activate = function() {
230         socket.send(JSON.stringify({"cmd": "activate"}));
231     }
232     this.migrate = function(callService, callParticipant) {
233         socket.send(JSON.stringify({"cmd": "migrate", "callService": callService, "callParticipant": callParticipant}));
234     }
235     this.merge = function(otherCall) {
236         socket.send(JSON.stringify({"cmd": "merge", "otherCall": otherCall}));
237     }
238     this.drop = function(participant) {
239         socket.send(JSON.stringify({"cmd": "drop", "participant": participant}));
240     }
241     this.split = function(participant) {
242         socket.send(JSON.stringify({"cmd": "split", "participant": participant}));
243     }
244     this.addParticipant = function(remotePartyId, successCallback, errorCallback) {
245     }
246     this.addStream = function(addStream, successCallback, errorCallback) {
247     }
248     this.removeStream = function(addStream, successCallback, errorCallback) {
249     }
250     this.setAudioVolume = function(volume, successCallback, errorCallback) {
251     }
252     this.setMicrophoneSensitivity = function(sensitivity, successCallback, errorCallback) {
253     }
254     this.setLocalVideoEnabled = function(onoff, successCallback, errorCallback) {
255     }
256     this.sendDTMF = function(tones, duration, interval, successCallback, errorCallback) {
257     }
258     this.record = function() {
259     }
260     this.pause = function() {
261     }
262 }
263
264 var __accountmanager = function() {
265     var listeners = new Array();
266     var accounts = new Array();
267
268     this.findServices = function(filter) {
269         /* brain dead matching, patches accepted */
270         var result = new Array();
271         if (accounts.length == 0)
272             return result;
273         try {
274             for (var i in accounts) {
275                 var a = accounts[i];
276                 for (var f in filter)
277                     if (a[f] && a[f] == filter[f]) {
278                         result.push(a);
279                         continue;
280                     }
281             }
282         } catch (err) {
283             console.log(err);
284         }
285
286         if (result.length == 0)
287             result.push(accounts[0]);
288
289         return result;
290     }
291     this.getAccountById = function(accountid) {
292         for (var i in accounts)
293             if (accounts[i].id == accountid)
294                 return accounts[i];
295         return null;
296     }
297     this.deleteAccount = function(accountid) {
298         var l = new Array();
299         for (var i in accounts)
300             if (accountid != accounts[i].id)
301                 l.push(accounts[i]);
302         accounts = l;
303
304         for (var i in listeners)
305             listeners[i].onAccountRemoved(accountid);
306     }
307     this.addAccount = function(account) {
308         accounts.push(account);
309         for (var i in listeners)
310             listeners[i].onAccountAdded(account);
311     }
312     this.addAccountListener = function(observer, errorCallback) {
313         listeners.push(observer);
314         return listeners.length;
315     }
316     this.removeAccountListener = function(handle) {
317         var l = new Array();
318         for (var i in listeners)
319             if (i != handle)
320                 l.push(listeners[i]);
321         listeners = l;
322     }
323 }
324
325 var __tizen = function() {
326     var application = null;
327     this.__defineGetter__("application", function() {
328         if (application == null)
329             application = new __application();
330         return application;
331     });
332
333     this.Account = function (accountId, accountProperties) {
334         var id = accountId;
335         var properties = accountProperties;
336         this.__defineGetter__("id", function() {
337             return id;
338         });
339         for (var p in properties) {
340             this.__defineGetter__(p, function() {
341                 return properties[p];
342             });
343         }
344     }
345
346     var call = null;
347     this.__defineGetter__("call", function() {
348         if (call == null) {
349             // ensure the account object is created first
350             tizen.account;
351
352             call = new __callmanager();
353         }
354         return call;
355     });
356     var account = null;
357     this.__defineGetter__("account", function() {
358         if (account == null)
359             account = new __accountmanager();
360
361         // ensure the call object is always the first listener
362         // for new accounts, which means we need a backdoor to
363         // access the acount object before the getter has finished
364         // executing for the first time
365         this.__defineGetter__("__account", function() {
366             return account;
367         });
368         if (call == null)
369             call = new __callmanager();
370         return account;
371     });
372 }
373
374 var tizen = new __tizen();