From 813e44083a55e0098c9c06518ce0b0b5881c4e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Obr=C3=A1til?= Date: Thu, 9 Oct 2014 20:14:19 +0200 Subject: [PATCH] Fix for Android older than 4.4 --- .../socketsforcordova/SocketAdapter.java | 4 +- .../socketsforcordova/SocketAdapterImpl.java | 41 +++++++++++++++---- .../socketsforcordova/SocketPlugin.java | 23 ++++++++--- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapter.java b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapter.java index ee131d5..8eb6c70 100644 --- a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapter.java +++ b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapter.java @@ -5,11 +5,11 @@ import java.net.SocketException; public interface SocketAdapter { - public void connect(String host, int port) throws IOException; + public void connect(String host, int port) throws Throwable; public void write(byte[] data) throws IOException; public void close() throws IOException; public void setOptions(SocketAdapterOptions options) throws SocketException; public void setDataConsumer(Consumer dataConsumer); public void setCloseEventHandler(Consumer closeEventHandler); - public void setErrorHandler(Consumer errorHandler); + public void setErrorHandler(Consumer errorHandler); } \ No newline at end of file diff --git a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java index 4a565a7..008dae8 100644 --- a/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java +++ b/src/android/src/cz/blocshop/socketsforcordova/SocketAdapterImpl.java @@ -1,12 +1,15 @@ package cz.blocshop.socketsforcordova; import java.io.IOException; +import java.io.InvalidObjectException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketException; import java.util.Arrays; +import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; public class SocketAdapterImpl implements SocketAdapter { @@ -16,15 +19,18 @@ public class SocketAdapterImpl implements SocketAdapter { private Consumer dataConsumer; private Consumer closeEventHandler; - private Consumer exceptionHandler; + private Consumer exceptionHandler; + + private ExecutorService executor; public SocketAdapterImpl() { this.socket = new Socket(); + this.executor = Executors.newSingleThreadExecutor(); } @Override - public void connect(String host, int port) throws IOException { - this.socket.connect(new InetSocketAddress(host, port)); + public void connect(String host, int port) throws Throwable { + this.connectSocket(host, port); this.submitReadTask(); } @@ -76,13 +82,32 @@ public class SocketAdapterImpl implements SocketAdapter { } @Override - public void setErrorHandler(Consumer exceptionHandler) { + public void setErrorHandler(Consumer exceptionHandler) { this.exceptionHandler = exceptionHandler; } + private void connectSocket(final String host, final int port) throws Throwable { + Future future = this.executor.submit(new Runnable() { + @Override + public void run() { + try { + socket.connect(new InetSocketAddress(host, port)); + } catch (IOException e) { + Logging.Error(SocketAdapterImpl.class.getName(), "Error during connecting of socket", e); + } + } + }); + + try { + Object result = future.get(); + } + catch (ExecutionException e) { + throw e.getCause(); + } + } + private void submitReadTask() { - ExecutorService executor = Executors.newSingleThreadExecutor(); - executor.submit(new Runnable() { + this.executor.submit(new Runnable() { @Override public void run() { runRead(); @@ -94,7 +119,7 @@ public class SocketAdapterImpl implements SocketAdapter { boolean hasError = false; try { runReadLoop(); - } catch (IOException e) { + } catch (Throwable e) { Logging.Error(SocketAdapterImpl.class.getName(), "Error during reading of socket input stream", e); hasError = true; invokeExceptionHandler(e); @@ -134,7 +159,7 @@ public class SocketAdapterImpl implements SocketAdapter { } } - private void invokeExceptionHandler(IOException exception) { + private void invokeExceptionHandler(Throwable exception) { if (this.exceptionHandler != null) { this.exceptionHandler.accept(exception); } diff --git a/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java b/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java index 9f8f531..862892e 100644 --- a/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java +++ b/src/android/src/cz/blocshop/socketsforcordova/SocketPlugin.java @@ -2,7 +2,11 @@ package cz.blocshop.socketsforcordova; import android.annotation.SuppressLint; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaArgs; @@ -60,8 +64,8 @@ public class SocketPlugin extends CordovaPlugin { try { socket.connect(host, port); callbackContext.success(); - } catch (IOException e) { - callbackContext.error(e.toString()); + } catch (Throwable t) { + callbackContext.error(t.toString()); } } @@ -174,7 +178,8 @@ public class SocketPlugin extends CordovaPlugin { try { JSONObject event = new JSONObject(); event.put("type", "DataReceived"); - event.put("data", new JSONArray(data)); + //event.put("data", new JSONArray(data)); NOT SUPPORTED IN API LEVEL LESS THAN 19 + event.put("data", new JSONArray(this.toByteList(data))); event.put("socketKey", socketKey); dispatchEvent(event); @@ -182,15 +187,23 @@ public class SocketPlugin extends CordovaPlugin { e.printStackTrace(); } } + + private List toByteList(byte[] array) { + List byteList = new ArrayList(array.length); + for (int i = 0; i < array.length; i++) { + byteList.add(array[i]); + } + return byteList; + } } - private class ErrorHandler implements Consumer { + private class ErrorHandler implements Consumer { private String socketKey; public ErrorHandler(String socketKey) { this.socketKey = socketKey; } @Override - public void accept(IOException exception) { + public void accept(Throwable exception) { try { JSONObject event = new JSONObject(); event.put("type", "Error");