small code cleanup
[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         }
83     }
84
85     this.history = null;
86     this.isCallInProgress = function() { 
87         return activeCall != null; 
88     }
89     this.addCallHandler = function(handler, callServiceType, serviceName) { 
90         callHandlers.push(handler);
91     }
92     this.removeCallHandler = function(handler) { 
93         var tmp = new Array();
94         for (var item in callHandlers)
95             if (item != handler)
96                 tmp.push(item);
97         callHandlers = tmp;
98         
99     }
100     this.getCallSevice = function(service) { 
101         return callService;
102     }
103 }
104
105 var __callservice = function (wsi) {
106     var socket = wsi;
107
108     this.launchDialer = function(remotePartyId) {
109         socket.send(JSON.stringify({ "cmd": "launchDialer" }));
110     }
111
112     var voicemailNumbers = new Array();
113     this.__defineGetter__("voiceNumbers", function() {
114         return voicemailNumbers;
115     });
116     
117     this.makeCall = function(remotePartyId, handler, extension, localVideoOn) {
118         var c = {
119             "cmd": "makeCall", 
120             "remotePartyId": remotePartyId.toString(), 
121             "extension": extension, 
122             "localVideoOn": localVideoOn
123         };
124         socket.send(JSON.stringify(c));
125     }
126 }
127
128 var __call = function(manager, callService, wsi, data) {
129     var socket = wsi;
130
131     var callData = data;
132     this.__defineGetter__("callData", function() {
133         return callData;
134     });
135     
136     var streamList = new Array();
137     this.__defineGetter__("streamList", function() {
138         return streamList;
139     });
140
141     var manager = manager;
142     this.__defineGetter__("manager", function() {
143         return manager;
144     });
145
146     var callService = callService;
147     this.getCallService = function(serviceName) {
148         return callService;
149     }
150
151     this.accept = function() {
152         socket.send(JSON.stringify({"cmd": "accept"}));
153     }
154     this.reject = function() {
155         socket.send(JSON.stringify({"cmd": "reject"}));
156     }
157     this.wait = function() {
158         socket.send(JSON.stringify({"cmd": "wait"}));
159     }
160     this.redirect = function(rid) {
161         socket.send(JSON.stringify({"cmd": "redirect"}));
162     }
163     this.end = function() {
164         socket.send(JSON.stringify({"cmd": "end"}));
165     }
166     this.hold = function() {
167         socket.send(JSON.stringify({"cmd": "hold"}));
168     }
169     this.activate = function() {
170         socket.send(JSON.stringify({"cmd": "activate"}));
171     }
172     this.migrate = function(callService, callParticipant) {
173         socket.send(JSON.stringify({"cmd": "migrate", "callService": callService, "callParticipant": callParticipant}));
174     }
175     this.merge = function(otherCall) {
176         socket.send(JSON.stringify({"cmd": "merge", "otherCall": otherCall}));
177     }
178     this.drop = function(participant) {
179         socket.send(JSON.stringify({"cmd": "drop", "participant": participant}));
180     }
181     this.split = function(participant) {
182         socket.send(JSON.stringify({"cmd": "split", "participant": participant}));
183     }
184     this.addParticipant = function(remotePartyId, successCallback, errorCallback) {
185     }
186     this.addStream = function(addStream, successCallback, errorCallback) {
187     }
188     this.removeStream = function(addStream, successCallback, errorCallback) {
189     }
190     this.setAudioVolume = function(volume, successCallback, errorCallback) {
191     }
192     this.setMicrophoneSensitivity = function(sensitivity, successCallback, errorCallback) {
193     }
194     this.setLocalVideoEnabled = function(onoff, successCallback, errorCallback) {
195     }
196     this.sendDTMF = function(tones, duration, interval, successCallback, errorCallback) {
197     }
198     this.record = function() {
199     }
200     this.pause = function() {
201     }
202 }
203
204 var __tizen = function() {
205     var application = null;
206     this.__defineGetter__("application", function() {
207         if (application == null)
208             application = new __application();
209         return application;
210     });
211
212     var call = null;
213     this.__defineGetter__("call", function() {
214         if (call == null)
215             call = new __callmanager();
216         return call;
217     });
218
219 }
220
221 var tizen = new __tizen();
222