removing tizen.call.manager and tizen.call.getCallService since the draft spec does...
[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://" + document.URL.substring(7), "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 activeCall = null;
30     var callHandlers = new Array();
31     var socket = new WebSocket("ws://" + document.URL.substring(7), "dialer-protocol");
32     var callService = new __callservice(socket);
33
34     socket.onmessage = function(msg) {
35         var ev = JSON.parse(msg.data);
36
37         if (activeCall == null) {
38             activeCall = new __call(this, callService, socket, ev);
39         }
40
41         if (ev.type && ev.type == "CallRemoved") {
42             activeCall = null;
43         }
44
45         if (ev.State) {
46             if (ev.State == "incoming")
47                 for (var index in callHandlers)
48                     callHandlers[index].onIncoming(activeCall);
49             if (ev.State == "dialing")
50                 for (var index in callHandlers)
51                     callHandlers[index].onDialing(activeCall);
52             else if (ev.State == "alerting")
53                 for (var index in callHandlers)
54                     callHandlers[index].onAlerting(activeCall);
55             else if (ev.State == "hold")
56                 for (var index in callHandlers)
57                     callHandlers[index].onHold(activeCall);
58             else if (ev.State == "waiting")
59                 for (var index in callHandlers)
60                     callHandlers[index].onWaiting(activeCall);
61             else if (ev.State == "disconnected")
62                 for (var index in callHandlers)
63                     callHandlers[index].onDisconnected(activeCall);
64             else if (ev.State == "disconnecting")
65                 for (var index in callHandlers)
66                     callHandlers[index].onDisconnecting(activeCall);
67             else if (ev.State == "accepted")
68                 for (var index in callHandlers)
69                     callHandlers[index].onAccepted(activeCall);
70             else if (ev.State == "remotelyheld")
71                 for (var index in callHandlers)
72                     callHandlers[index].onRemotelyHeld(activeCall);
73             else if (ev.State == "active")
74                 for (var index in callHandlers)
75                     callHandlers[index].onActivated(activeCall);
76             else if (ev.State == "initializing")
77                 for (var index in callHandlers)
78                     callHandlers[index].onInitializing(activeCall);
79             else if (ev.State == "initialized")
80                 for (var index in callHandlers)
81                     callHandlers[index].onInitialized(activeCall);
82         } else if (ev.ModemAdded) {
83             tizen.account.addAccount(new tizen.Account(ev.ModemAdded, ev));
84         } else if (ev.ModemRemoved) {
85             tizen.account.deleteAccount(ev.ModemRemoved);
86         }
87     }
88
89     this.history = null;
90     this.isCallInProgress = function() { 
91         return activeCall != null; 
92     }
93     this.addCallHandler = function(handler, callServiceType, serviceName) { 
94         callHandlers.push(handler);
95     }
96     this.removeCallHandler = function(handler) { 
97         var tmp = new Array();
98         for (var item in callHandlers)
99             if (item != handler)
100                 tmp.push(item);
101         callHandlers = tmp;
102         
103     }
104     this.getCallSevice = function(service) { 
105         return callService;
106     }
107 }
108
109 var __callservice = function (wsi) {
110     var socket = wsi;
111
112     this.launchDialer = function(remotePartyId) {
113         socket.send(JSON.stringify({ "cmd": "launchDialer" }));
114     }
115
116     var voicemailNumbers = new Array();
117     this.__defineGetter__("voiceNumbers", function() {
118         return voicemailNumbers;
119     });
120     
121     this.makeCall = function(remotePartyId, handler, extension, localVideoOn) {
122         var c = {
123             "cmd": "makeCall", 
124             "remotePartyId": remotePartyId.toString(), 
125             "extension": extension, 
126             "localVideoOn": localVideoOn
127         };
128         socket.send(JSON.stringify(c));
129     }
130 }
131
132 var __call = function(manager, callService, wsi, data) {
133     var socket = wsi;
134
135     var callData = data;
136     this.__defineGetter__("callData", function() {
137         return callData;
138     });
139     
140     var streamList = new Array();
141     this.__defineGetter__("streamList", function() {
142         return streamList;
143     });
144
145     this.accept = function() {
146         socket.send(JSON.stringify({"cmd": "accept"}));
147     }
148     this.reject = function() {
149         socket.send(JSON.stringify({"cmd": "reject"}));
150     }
151     this.wait = function() {
152         socket.send(JSON.stringify({"cmd": "wait"}));
153     }
154     this.redirect = function(rid) {
155         socket.send(JSON.stringify({"cmd": "redirect"}));
156     }
157     this.end = function() {
158         socket.send(JSON.stringify({"cmd": "end"}));
159     }
160     this.hold = function() {
161         socket.send(JSON.stringify({"cmd": "hold"}));
162     }
163     this.activate = function() {
164         socket.send(JSON.stringify({"cmd": "activate"}));
165     }
166     this.migrate = function(callService, callParticipant) {
167         socket.send(JSON.stringify({"cmd": "migrate", "callService": callService, "callParticipant": callParticipant}));
168     }
169     this.merge = function(otherCall) {
170         socket.send(JSON.stringify({"cmd": "merge", "otherCall": otherCall}));
171     }
172     this.drop = function(participant) {
173         socket.send(JSON.stringify({"cmd": "drop", "participant": participant}));
174     }
175     this.split = function(participant) {
176         socket.send(JSON.stringify({"cmd": "split", "participant": participant}));
177     }
178     this.addParticipant = function(remotePartyId, successCallback, errorCallback) {
179     }
180     this.addStream = function(addStream, successCallback, errorCallback) {
181     }
182     this.removeStream = function(addStream, successCallback, errorCallback) {
183     }
184     this.setAudioVolume = function(volume, successCallback, errorCallback) {
185     }
186     this.setMicrophoneSensitivity = function(sensitivity, successCallback, errorCallback) {
187     }
188     this.setLocalVideoEnabled = function(onoff, successCallback, errorCallback) {
189     }
190     this.sendDTMF = function(tones, duration, interval, successCallback, errorCallback) {
191     }
192     this.record = function() {
193     }
194     this.pause = function() {
195     }
196 }
197
198 var __accountmanager = function() {
199     var listeners = new Array();
200     var accounts = new Array();
201
202     this.findServices = function(filter) {
203         /* brain dead matching, patches accepted */
204         var result = new Array();
205         if (accounts.length == 0)
206             return result;
207         try {
208             for (var i in accounts) {
209                 var a = accounts[i];
210                 for (var f in filter)
211                     if (a[f] && a[f] == filter[f]) {
212                         result.push(a);
213                         continue;
214                     }
215             }
216         } catch (err) {
217             console.log(err);
218         }
219
220         if (result.length == 0)
221             result.push(accounts[0]);
222
223         return result;
224     }
225     this.getAccountById = function(accountid) {
226         for (var i in accounts)
227             if (accounts[i].id == accountid)
228                 return accounts[i];
229         return null;
230     }
231     this.deleteAccount = function(accountid) {
232         var l = new Array();
233         for (var i in accounts)
234             if (accountid != accounts[i].id)
235                 l.push(accounts[i]);
236         accounts = l;
237
238         for (var i in listeners)
239             listeners[i].onAccountRemoved(accountid);   
240     }
241     this.addAccount = function(account) {
242         accounts.push(account);
243         for (var i in listeners)
244             listeners[i].onAccountAdded(account);
245     }
246     this.addAccountListener = function(observer, errorCallback) {
247         listeners.push(observer);
248         return listeners.length;
249     }
250     this.removeAccountListener = function(handle) {
251         var l = new Array();
252         for (var i in listeners)
253             if (i != handle)
254                 l.push(listeners[i]);
255         listeners = l;
256     }
257 }
258
259 var __tizen = function() {
260     var application = null;
261     this.__defineGetter__("application", function() {
262         if (application == null)
263             application = new __application();
264         return application;
265     });
266
267     var call = null;
268     this.__defineGetter__("call", function() {
269         if (call == null)
270             call = new __callmanager();
271         return call;
272     });
273
274     this.Account = function (accountId, accountProperties) {
275         var id = accountId;
276         var properties = accountProperties;
277         this.__defineGetter__("id", function() {
278             return id;
279         });
280         for (var p in properties) {
281             this.__defineGetter__(p, function() {
282                 return properties[p];
283             });
284         }
285     }
286     var account = null;
287     this.__defineGetter__("account", function() {
288         if (account == null)
289             account = new __accountmanager();
290         return account;
291     });
292 }
293
294 var tizen = new __tizen();
295