Fix emulator crash on sensor emulation. do not merge.

The reason for crash is that event socket's I/O looper has been used without
being properly initialized.

(cherry picked from commit 46ff3eaff40952df50ee14056cae88a3e7396a81)

Change-Id: Ibc08f2a242049e0ce964c45ceaa31073ffdf5e06
diff --git a/android/android-device.c b/android/android-device.c
index 498e304..e91185d 100644
--- a/android/android-device.c
+++ b/android/android-device.c
@@ -389,6 +389,14 @@
     memset(&ads->address, 0, sizeof(ads->address));
 }
 
+/* Event socket's asynchronous I/O looper callback.
+ * Param:
+ *  opaque - AndroidEventSocket instance.
+ *  fd - Socket's FD.
+ *  events - I/O type bitsmask (read | write).
+ */
+static void _on_event_socket_io(void* opaque, int fd, unsigned events);
+
 static int
 _android_dev_socket_connect(AndroidDevSocket* ads)
 {
@@ -403,6 +411,14 @@
     }
     socket_set_nonblock(ads->fd);
 
+    /* XXX: A quick fix for event channel init. Redo this later. */
+    if (ads->type == ADS_TYPE_EVENT) {
+        AndroidEventSocket* adsevent = (AndroidEventSocket*)ads;
+        /* Prepare for async I/O on the event socket. */
+        loopIo_init(adsevent->io, adsevent->dev_socket.ad->looper, ads->fd,
+                    _on_event_socket_io, adsevent);
+    }
+
     /* Synchronously connect to it. */
     ads->socket_status = ADS_CONNECTING;
     iolooper_add_write(_ads_io_looper(ads), ads->fd);
@@ -952,11 +968,14 @@
 {
     AndroidDevSocket* ads = &adsevent->dev_socket;
     const int res = _android_dev_socket_connect(&adsevent->dev_socket);
+    /* XXX: This is patch-fixed in _android_dev_socket_connect */
+#if 0
     if (res == 0) {
         /* Prepare for async I/O on the event socket. */
         loopIo_init(adsevent->io, _aes_looper(adsevent), ads->fd,
                     _on_event_socket_io, adsevent);
     }
+#endif
     return res;
 }
 
@@ -1208,6 +1227,7 @@
 
     /* Complete event socket connection by identifying it as "event" socket with
      * the application. */
+    ads->socket_status = ADS_CONNECTED;
     res = _android_dev_socket_register(ads);
 
     if (res) {