Revamp the call handling logic so it can deal with multiple calls
authorRusty Lynch <rusty.lynch@intel.com>
Sat, 18 Aug 2012 02:37:01 +0000 (19:37 -0700)
committerRusty Lynch <rusty.lynch@intel.com>
Sat, 18 Aug 2012 02:37:01 +0000 (19:37 -0700)
Tizen.Device.js
homescreen.js
main.c

index ab7e882..ab3e576 100644 (file)
@@ -26,69 +26,115 @@ var __application = function () {
 }
 
 var __callmanager = function () {
-    var activeCall = null;
+    var calls = new Array();
     var callHandlers = new Array();
     var socket = new WebSocket("ws://" + document.URL.substring(7), "dialer-protocol");
-    var callService = new __callservice(socket);
+    var services = new Array();
+
+    var listener = { 
+       "onAccountUpdated": function(account) { 
+       }, 
+       "onAccountAdded": function(account) { 
+           services.push(new __callservice(socket, account.id));
+       }, 
+       "onAccountRemoved": function(id) {
+           var tmp = new Array();
+           for (var i in services)
+               if (services[i].id != id)
+                   tmp.push(id);
+           services = tmp;
+       }
+    }
+    tizen.account.addAccountListener(listener);
 
     socket.onmessage = function(msg) {
         var ev = JSON.parse(msg.data);
 
-        if (activeCall == null) {
-            activeCall = new __call(this, callService, socket, ev);
-        }
-
-        if (ev.type && ev.type == "CallRemoved") {
-            activeCall = null;
-        }
-
-        if (ev.State) {
-            if (ev.State == "incoming")
+       if (ev.type == "ModemAdded") {
+           tizen.account.addAccount(new tizen.Account(ev.ModemAdded, ev));
+       } else if (ev.type == "ModemRemoved") {
+           tizen.account.deleteAccount(ev.ModemRemoved);
+       } else if (ev.type == "CallAdded") {
+           calls.push(new __call(this, socket, ev));
+       } else if (ev.type == "CallRemoved") {
+           var tmp = new Array();
+           for (var i in calls)
+               if (calls[i].id != ev.id)
+                   tmp.push(calls[i]);
+           calls = tmp;
+       } else if (ev.type == "PropertyChanged" && ev.State) {
+            if (ev.State == "incoming") {
                 for (var index in callHandlers)
-                    callHandlers[index].onIncoming(activeCall);
-            if (ev.State == "dialing")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onIncoming(calls[i]);
+           } else if (ev.State == "dialing") {
                 for (var index in callHandlers)
-                    callHandlers[index].onDialing(activeCall);
-            else if (ev.State == "alerting")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onDialing(calls[i]);
+           } else if (ev.State == "alerting") {
                 for (var index in callHandlers)
-                    callHandlers[index].onAlerting(activeCall);
-            else if (ev.State == "hold")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onAlerting(calls[i]);
+            } else if (ev.State == "hold") {
                 for (var index in callHandlers)
-                    callHandlers[index].onHold(activeCall);
-            else if (ev.State == "waiting")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onHold(calls[i]);
+            } else if (ev.State == "waiting") {
                 for (var index in callHandlers)
-                    callHandlers[index].onWaiting(activeCall);
-            else if (ev.State == "disconnected")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onWaiting(calls[i]);
+            } else if (ev.State == "disconnected") {
                 for (var index in callHandlers)
-                    callHandlers[index].onDisconnected(activeCall);
-            else if (ev.State == "disconnecting")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onDisconnected(calls[i]);
+            } else if (ev.State == "disconnecting") {
                 for (var index in callHandlers)
-                    callHandlers[index].onDisconnecting(activeCall);
-            else if (ev.State == "accepted")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onDisconnecting(calls[i]);
+            } else if (ev.State == "accepted") {
                 for (var index in callHandlers)
-                    callHandlers[index].onAccepted(activeCall);
-            else if (ev.State == "remotelyheld")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onAccepted(calls[i]);
+           } else if (ev.State == "remotelyheld") {
                 for (var index in callHandlers)
-                    callHandlers[index].onRemotelyHeld(activeCall);
-            else if (ev.State == "active")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onRemotelyHeld(calls[i]);
+            } else if (ev.State == "active") {
                 for (var index in callHandlers)
-                    callHandlers[index].onActivated(activeCall);
-            else if (ev.State == "initializing")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onActivated(calls[i]);
+            } else if (ev.State == "initializing") {
                 for (var index in callHandlers)
-                    callHandlers[index].onInitializing(activeCall);
-            else if (ev.State == "initialized")
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onInitializing(calls[i]);
+            } else if (ev.State == "initialized") {
                 for (var index in callHandlers)
-                    callHandlers[index].onInitialized(activeCall);
-        } else if (ev.ModemAdded) {
-           tizen.account.addAccount(new tizen.Account(ev.ModemAdded, ev));
-       } else if (ev.ModemRemoved) {
-           tizen.account.deleteAccount(ev.ModemRemoved);
+                   for (var i in calls)
+                       if (calls[i].id == ev.id)
+                           callHandlers[index].onInitialized(calls[i]);
+           } else {
+               console.log("Unhandled state: " + ev.State);
+           }
+       } else {
+           console.log("Unhandled event");
+           console.log(ev);
        }
     }
 
     this.history = null;
     this.isCallInProgress = function() { 
-        return activeCall != null
+        return calls.length > 0
     }
     this.addCallHandler = function(handler, callServiceType, serviceName) { 
         callHandlers.push(handler);
@@ -101,14 +147,25 @@ var __callmanager = function () {
         callHandlers = tmp;
         
     }
-    this.getCallSevice = function(service) { 
-        return callService;
+    this.getCallService = function(account) { 
+       try {
+           for (var i in services)
+               if (services[i].id == account.id)
+                   return services[i];
+       } catch (ex) {}
+       
+       return services[0];
     }
 }
 
-var __callservice = function (wsi) {
+var __callservice = function (wsi, serviceid) {
     var socket = wsi;
 
+    var id = serviceid;
+    this.__defineGetter__("id", function() {
+       return id;
+    });
+
     this.launchDialer = function(remotePartyId) {
         socket.send(JSON.stringify({ "cmd": "launchDialer" }));
     }
@@ -129,9 +186,14 @@ var __callservice = function (wsi) {
     }
 }
 
-var __call = function(manager, callService, wsi, data) {
+var __call = function(manager, wsi, data) {
     var socket = wsi;
 
+    var id = data.id;
+    this.__defineGetter__("id", function() {
+        return id;
+    });
+
     var callData = data;
     this.__defineGetter__("callData", function() {
         return callData;
@@ -155,7 +217,7 @@ var __call = function(manager, callService, wsi, data) {
         socket.send(JSON.stringify({"cmd": "redirect"}));
     }
     this.end = function() {
-        socket.send(JSON.stringify({"cmd": "end"}));
+        socket.send(JSON.stringify({"cmd": "end", "path": id}));
     }
     this.hold = function() {
         socket.send(JSON.stringify({"cmd": "hold"}));
index f06c1b6..c95ef4b 100644 (file)
@@ -44,6 +44,7 @@ document.body.onload = function() {
             activeCall = call;
         },
         onAlerting: function(call) {
+            activeCall = call;
             console.log("onAlerting: "); console.log(call);
         },
         onHold: function(call) {
diff --git a/main.c b/main.c
index f046ff8..eed39b6 100644 (file)
--- a/main.c
+++ b/main.c
@@ -226,7 +226,9 @@ callback_dialer(struct libwebsocket_context *context,
         if (voicecallmanager_path) {
             /* inform any connections about the addition of the modem */
             json_t *o = json_object();
-            json_object_set(o, "ModemAdded", json_string(voicecallmanager_path));
+
+            json_object_set(o, "type", json_string("ModemAdded"));
+            json_object_set(o, "id", json_string(voicecallmanager_path));
             char *dump = json_dumps(o, 0);
 
             unsigned char *b = malloc(LWS_SEND_BUFFER_PRE_PADDING + 
@@ -329,12 +331,14 @@ void handle_command(json_t *o)
                                  DBUS_TYPE_INVALID);
         e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
     } else if (strncmp(cmd, "end", 3) == 0) {
+        const char *path = json_string_value(json_object_get(o, "path"));
+
         DBusMessage *msg;
         msg = dbus_message_new_method_call(
                                            "org.ofono",
-                                           voicecallmanager_path,
-                                           "org.ofono.VoiceCallManager",
-                                           "HangupAll"
+                                           path,
+                                           "org.ofono.VoiceCall",
+                                           "Hangup"
                                            );
         e_dbus_message_send(bus, msg, dial_reply, -1, NULL);
 
@@ -472,6 +476,8 @@ void call_properties_changed_cb(void *data, DBusMessage *msg)
     dbus_message_iter_get_basic(&value, &v);
 
     object = json_object();
+    json_object_set(object, "type", json_string("PropertyChanged"));
+    json_object_set(object, "id", json_string(dbus_message_get_path(msg)));
     json_object_set(object, property, json_string(v));
     dump = json_dumps(object, 0);
     libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
@@ -501,7 +507,8 @@ void modem_properties_changed_cb(void *data, DBusMessage *msg)
                 dbus_message_iter_get_basic(&variant, &v);
                 if (!strcasecmp(v, "org.ofono.VoiceCallManager")) {
                     object = json_object();
-                    json_object_set(object, "ModemAdded", json_string(dbus_message_get_path(msg)));
+                    json_object_set(object, "type", json_string("ModemAdded"));
+                    json_object_set(object, "id", json_string(dbus_message_get_path(msg)));
                     dump = json_dumps(object, 0);
                     libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
                     free(dump);
@@ -510,7 +517,8 @@ void modem_properties_changed_cb(void *data, DBusMessage *msg)
                 // If the modem has removed all interfaces then its no longer
                 // available for making calls
                 object = json_object();
-                json_object_set(object, "ModemRemoved", json_string(dbus_message_get_path(msg)));
+                json_object_set(object, "type", json_string("ModemRemoved"));
+                json_object_set(object, "id", json_string(dbus_message_get_path(msg)));
                 dump = json_dumps(object, 0);
                 libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
                 free(dump);                
@@ -525,14 +533,16 @@ void call_added_cb(void *data, DBusMessage *msg)
     DBusMessageIter iter, properties;
     const char *path;
 
-    json_t *object = json_object();
-
     dbus_message_iter_init(msg, &iter);
     dbus_message_iter_get_basic(&iter, &path);
 
     dbus_message_iter_next(&iter);
     dbus_message_iter_recurse(&iter, &properties);
 
+    json_t *object = json_object();
+    json_object_set(object, "id", json_string(path));
+    json_object_set(object, "type", json_string("CallAdded"));
+
     while (dbus_message_iter_get_arg_type(&properties) == DBUS_TYPE_DICT_ENTRY) {
         DBusMessageIter entry, value;
         const char *key, *v;
@@ -565,7 +575,14 @@ void call_added_cb(void *data, DBusMessage *msg)
 
 void call_removed_cb(void *data, DBusMessage *msg)
 {
+    DBusMessageIter iter;
+    const char *path;
+
+    dbus_message_iter_init(msg, &iter);
+    dbus_message_iter_get_basic(&iter, &path);
+
     json_t *object = json_object();
+    json_object_set(object, "id", json_string(path));
     json_object_set(object, "type", json_string("CallRemoved"));
     char *dump = json_dumps(object, 0);
     libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
@@ -581,7 +598,8 @@ void modem_added_cb(void *data, DBusMessage *msg)
     dbus_message_iter_get_basic(&iter, &path);
 
     json_t *object = json_object();
-    json_object_set(object, "ModemAdded", json_string(path));
+    json_object_set(object, "type", json_string("ModemAdded"));
+    json_object_set(object, "id", json_string(path));
     char *dump = json_dumps(object, 0);
     libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
     free(dump);    
@@ -596,7 +614,8 @@ void modem_removed_cb(void *data, DBusMessage *msg)
     dbus_message_iter_get_basic(&iter, &path);
 
     json_t *object = json_object();
-    json_object_set(object, "ModemRemoved", json_string(path));
+    json_object_set(object, "type", json_string("ModemRemoved"));
+    json_object_set(object, "id", json_string(path));
     char *dump = json_dumps(object, 0);
     libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
     free(dump);
@@ -666,7 +685,8 @@ get_modems_reply(void *data, DBusMessage *reply, DBusError *error)
 
                         /* inform any connections about the addition of the modem */
                         json_t *o = json_object();
-                        json_object_set(o, "ModemAdded", json_string(path));
+                        json_object_set(o, "type", json_string("ModemAdded"));
+                        json_object_set(o, "id", json_string(path));
                         char *dump = json_dumps(o, 0);
                         libwebsockets_broadcast(&protocols[PROTOCOL_DIALER], dump, strlen(dump));
                         free(dump);