1 ///////////////////////////////////////////////////////////////////////////////
2 // This is a partial implementation of the tizen.application web specification
3 // which using a websocket server for implementing the native backend
5 var __application = function () {
9 var socket = new WebSocket("ws://localhost:7681", "app-list-protocol");
10 socket.onmessage = function(msg) {
11 appList = JSON.parse(msg.data);
16 this.launch = function (id, successCallback, errorCallback, argument) {
17 var cmd = {"cmd": "launch", "index": id};
18 socket.send(JSON.stringify(cmd));
20 this.getAppInfo = function(id) {
23 this.addAppInfoEventListener = function(eventCallback, errorCallback) {
24 callback = eventCallback;
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();
35 "onAccountUpdated": function(account) {
37 "onAccountAdded": function(account) {
38 services.push(new __callservice(socket, account.id));
40 "onAccountRemoved": function(id) {
41 var tmp = new Array();
42 for (var i in services)
43 if (services[i].id != id)
48 tizen.__account.addAccountListener(listener);
50 socket.onmessage = function(msg) {
51 var ev = JSON.parse(msg.data);
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();
62 if (calls[i].id != ev.id)
65 } else if (ev.type == "PropertyChanged" && ev.State) {
66 if (ev.State == "incoming") {
67 for (var index in callHandlers)
69 if (calls[i].id == ev.id)
70 callHandlers[index].onIncoming(calls[i]);
71 } else if (ev.State == "dialing") {
72 for (var index in callHandlers)
74 if (calls[i].id == ev.id)
75 callHandlers[index].onDialing(calls[i]);
76 } else if (ev.State == "alerting") {
77 for (var index in callHandlers)
79 if (calls[i].id == ev.id)
80 callHandlers[index].onAlerting(calls[i]);
81 } else if (ev.State == "hold") {
82 for (var index in callHandlers)
84 if (calls[i].id == ev.id)
85 callHandlers[index].onHold(calls[i]);
86 } else if (ev.State == "waiting") {
87 for (var index in callHandlers)
89 if (calls[i].id == ev.id)
90 callHandlers[index].onWaiting(calls[i]);
91 } else if (ev.State == "disconnected") {
92 for (var index in callHandlers)
94 if (calls[i].id == ev.id)
95 callHandlers[index].onDisconnected(calls[i]);
96 } else if (ev.State == "disconnecting") {
97 for (var index in callHandlers)
99 if (calls[i].id == ev.id)
100 callHandlers[index].onDisconnecting(calls[i]);
101 } else if (ev.State == "accepted") {
102 for (var index in callHandlers)
104 if (calls[i].id == ev.id)
105 callHandlers[index].onAccepted(calls[i]);
106 } else if (ev.State == "remotelyheld") {
107 for (var index in callHandlers)
109 if (calls[i].id == ev.id)
110 callHandlers[index].onRemotelyHeld(calls[i]);
111 } else if (ev.State == "active") {
112 for (var index in callHandlers)
114 if (calls[i].id == ev.id)
115 callHandlers[index].onActivated(calls[i]);
116 } else if (ev.State == "initializing") {
117 for (var index in callHandlers)
119 if (calls[i].id == ev.id)
120 callHandlers[index].onInitializing(calls[i]);
121 } else if (ev.State == "initialized") {
122 for (var index in callHandlers)
124 if (calls[i].id == ev.id)
125 callHandlers[index].onInitialized(calls[i]);
127 console.log("Unhandled state: " + ev.State);
130 console.log("Unhandled event");
136 this.isCallInProgress = function() {
137 return calls.length > 0;
139 this.addCallHandler = function(handler, callServiceType, serviceName) {
140 callHandlers.push(handler);
142 this.removeCallHandler = function(handler) {
143 var tmp = new Array();
144 for (var item in callHandlers)
150 this.getCallService = function(account) {
152 for (var i in services)
153 if (services[i].id == account.id)
161 var __callservice = function (wsi, serviceid) {
165 this.__defineGetter__("id", function() {
169 this.launchDialer = function(remotePartyId) {
170 socket.send(JSON.stringify({ "cmd": "launchDialer" }));
173 var voicemailNumbers = new Array();
174 this.__defineGetter__("voiceNumbers", function() {
175 return voicemailNumbers;
178 this.makeCall = function(remotePartyId, handler, extension, localVideoOn) {
181 "remotePartyId": remotePartyId.toString(),
182 "extension": extension,
183 "localVideoOn": localVideoOn
185 socket.send(JSON.stringify(c));
189 var __call = function(manager, wsi, data) {
193 this.__defineGetter__("id", function() {
198 this.__defineGetter__("callData", function() {
202 var streamList = new Array();
203 this.__defineGetter__("streamList", function() {
207 this.accept = function() {
208 socket.send(JSON.stringify({"cmd": "accept"}));
210 this.reject = function() {
211 socket.send(JSON.stringify({"cmd": "reject"}));
213 this.wait = function() {
214 socket.send(JSON.stringify({"cmd": "wait"}));
216 this.redirect = function(rid) {
217 socket.send(JSON.stringify({"cmd": "redirect"}));
219 this.end = function() {
220 socket.send(JSON.stringify({"cmd": "end", "path": id}));
222 this.hold = function() {
223 socket.send(JSON.stringify({"cmd": "hold"}));
225 this.activate = function() {
226 socket.send(JSON.stringify({"cmd": "activate"}));
228 this.migrate = function(callService, callParticipant) {
229 socket.send(JSON.stringify({"cmd": "migrate", "callService": callService, "callParticipant": callParticipant}));
231 this.merge = function(otherCall) {
232 socket.send(JSON.stringify({"cmd": "merge", "otherCall": otherCall}));
234 this.drop = function(participant) {
235 socket.send(JSON.stringify({"cmd": "drop", "participant": participant}));
237 this.split = function(participant) {
238 socket.send(JSON.stringify({"cmd": "split", "participant": participant}));
240 this.addParticipant = function(remotePartyId, successCallback, errorCallback) {
242 this.addStream = function(addStream, successCallback, errorCallback) {
244 this.removeStream = function(addStream, successCallback, errorCallback) {
246 this.setAudioVolume = function(volume, successCallback, errorCallback) {
248 this.setMicrophoneSensitivity = function(sensitivity, successCallback, errorCallback) {
250 this.setLocalVideoEnabled = function(onoff, successCallback, errorCallback) {
252 this.sendDTMF = function(tones, duration, interval, successCallback, errorCallback) {
254 this.record = function() {
256 this.pause = function() {
260 var __accountmanager = function() {
261 var listeners = new Array();
262 var accounts = new Array();
264 this.findServices = function(filter) {
265 /* brain dead matching, patches accepted */
266 var result = new Array();
267 if (accounts.length == 0)
270 for (var i in accounts) {
272 for (var f in filter)
273 if (a[f] && a[f] == filter[f]) {
282 if (result.length == 0)
283 result.push(accounts[0]);
287 this.getAccountById = function(accountid) {
288 for (var i in accounts)
289 if (accounts[i].id == accountid)
293 this.deleteAccount = function(accountid) {
295 for (var i in accounts)
296 if (accountid != accounts[i].id)
300 for (var i in listeners)
301 listeners[i].onAccountRemoved(accountid);
303 this.addAccount = function(account) {
304 accounts.push(account);
305 for (var i in listeners)
306 listeners[i].onAccountAdded(account);
308 this.addAccountListener = function(observer, errorCallback) {
309 listeners.push(observer);
310 return listeners.length;
312 this.removeAccountListener = function(handle) {
314 for (var i in listeners)
316 l.push(listeners[i]);
321 var __tizen = function() {
322 var application = null;
323 this.__defineGetter__("application", function() {
324 if (application == null)
325 application = new __application();
329 this.Account = function (accountId, accountProperties) {
331 var properties = accountProperties;
332 this.__defineGetter__("id", function() {
335 for (var p in properties) {
336 this.__defineGetter__(p, function() {
337 return properties[p];
343 this.__defineGetter__("call", function() {
345 // ensure the account object is created first
348 call = new __callmanager();
353 this.__defineGetter__("account", function() {
355 account = new __accountmanager();
357 // ensure the call object is always the first listener
358 // for new accounts, which means we need a backdoor to
359 // access the acount object before the getter has finished
360 // executing for the first time
361 this.__defineGetter__("__account", function() {
365 call = new __callmanager();
370 var tizen = new __tizen();