Add support for tizen.account which will be automotically populated
[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     var manager = manager;
146     this.__defineGetter__("manager", function() {
147         return manager;
148     });
149
150     var callService = callService;
151     this.getCallService = function(serviceName) {
152         return callService;
153     }
154
155     this.accept = function() {
156         socket.send(JSON.stringify({"cmd": "accept"}));
157     }
158     this.reject = function() {
159         socket.send(JSON.stringify({"cmd": "reject"}));
160     }
161     this.wait = function() {
162         socket.send(JSON.stringify({"cmd": "wait"}));
163     }
164     this.redirect = function(rid) {
165         socket.send(JSON.stringify({"cmd": "redirect"}));
166     }
167     this.end = function() {
168         socket.send(JSON.stringify({"cmd": "end"}));
169     }
170     this.hold = function() {
171         socket.send(JSON.stringify({"cmd": "hold"}));
172     }
173     this.activate = function() {
174         socket.send(JSON.stringify({"cmd": "activate"}));
175     }
176     this.migrate = function(callService, callParticipant) {
177         socket.send(JSON.stringify({"cmd": "migrate", "callService": callService, "callParticipant": callParticipant}));
178     }
179     this.merge = function(otherCall) {
180         socket.send(JSON.stringify({"cmd": "merge", "otherCall": otherCall}));
181     }
182     this.drop = function(participant) {
183         socket.send(JSON.stringify({"cmd": "drop", "participant": participant}));
184     }
185     this.split = function(participant) {
186         socket.send(JSON.stringify({"cmd": "split", "participant": participant}));
187     }
188     this.addParticipant = function(remotePartyId, successCallback, errorCallback) {
189     }
190     this.addStream = function(addStream, successCallback, errorCallback) {
191     }
192     this.removeStream = function(addStream, successCallback, errorCallback) {
193     }
194     this.setAudioVolume = function(volume, successCallback, errorCallback) {
195     }
196     this.setMicrophoneSensitivity = function(sensitivity, successCallback, errorCallback) {
197     }
198     this.setLocalVideoEnabled = function(onoff, successCallback, errorCallback) {
199     }
200     this.sendDTMF = function(tones, duration, interval, successCallback, errorCallback) {
201     }
202     this.record = function() {
203     }
204     this.pause = function() {
205     }
206 }
207
208 var __accountmanager = function() {
209     var listeners = new Array();
210     var accounts = new Array();
211
212     this.findServices = function(filter) {
213         /* brain dead matching, patches accepted */
214         var result = new Array();
215         if (accounts.length == 0)
216             return result;
217         try {
218             for (var i in accounts) {
219                 var a = accounts[i];
220                 for (var f in filter)
221                     if (a[f] && a[f] == filter[f]) {
222                         result.push(a);
223                         continue;
224                     }
225             }
226         } catch (err) {
227             console.log(err);
228         }
229
230         if (result.length == 0)
231             result.push(accounts[0]);
232
233         return result;
234     }
235     this.getAccountById = function(accountid) {
236         for (var i in accounts)
237             if (accounts[i].id == accountid)
238                 return accounts[i];
239         return null;
240     }
241     this.deleteAccount = function(accountid) {
242         var l = new Array();
243         for (var i in accounts)
244             if (accountid != accounts[i].id)
245                 l.push(accounts[i]);
246         accounts = l;
247
248         for (var i in listeners)
249             listeners[i].onAccountRemoved(accountid);   
250     }
251     this.addAccount = function(account) {
252         accounts.push(account);
253         for (var i in listeners)
254             listeners[i].onAccountAdded(account);
255     }
256     this.addAccountListener = function(observer, errorCallback) {
257         listeners.push(observer);
258         return listeners.length;
259     }
260     this.removeAccountListener = function(handle) {
261         var l = new Array();
262         for (var i in listeners)
263             if (i != handle)
264                 l.push(listeners[i]);
265         listeners = l;
266     }
267 }
268
269 var __tizen = function() {
270     var application = null;
271     this.__defineGetter__("application", function() {
272         if (application == null)
273             application = new __application();
274         return application;
275     });
276
277     var call = null;
278     this.__defineGetter__("call", function() {
279         if (call == null)
280             call = new __callmanager();
281         return call;
282     });
283
284     this.Account = function (accountId, accountProperties) {
285         var id = accountId;
286         var properties = accountProperties;
287         this.__defineGetter__("id", function() {
288             return id;
289         });
290         for (var p in properties) {
291             this.__defineGetter__(p, function() {
292                 return properties[p];
293             });
294         }
295     }
296     var account = null;
297     this.__defineGetter__("account", function() {
298         if (account == null)
299             account = new __accountmanager();
300         return account;
301     });
302 }
303
304 var tizen = new __tizen();
305