Fix of unresponsive webview during opening connection to server on Android platform

This commit is contained in:
Martin Obrátil 2014-12-29 17:08:03 +01:00
parent 88bc09fb83
commit b67810f3c5
3 changed files with 81 additions and 42 deletions

View File

@ -5,12 +5,14 @@ import java.net.SocketException;
public interface SocketAdapter {
public void open(String host, int port) throws Throwable;
public void open(String host, int port);
public void write(byte[] data) throws IOException;
public void shutdownWrite() throws IOException;
public void close() throws IOException;
public void setOptions(SocketAdapterOptions options) throws SocketException;
public void setOpenEventHandler(Consumer<Void> openEventHandler);
public void setOpenErrorEventHandler(Consumer<String> openErrorEventHandler);
public void setDataConsumer(Consumer<byte[]> dataConsumer);
public void setCloseEventHandler(Consumer<Boolean> closeEventHandler);
public void setErrorHandler(Consumer<String> errorHandler);
public void setErrorEventHandler(Consumer<String> errorEventHandler);
}

View File

@ -16,9 +16,11 @@ public class SocketAdapterImpl implements SocketAdapter {
private final int INPUT_STREAM_BUFFER_SIZE = 16 * 1024;
private final Socket socket;
private Consumer<Void> openEventHandler;
private Consumer<String> openErrorEventHandler;
private Consumer<byte[]> dataConsumer;
private Consumer<Boolean> closeEventHandler;
private Consumer<String> exceptionHandler;
private Consumer<String> errorEventHandler;
private ExecutorService executor;
@ -28,9 +30,20 @@ public class SocketAdapterImpl implements SocketAdapter {
}
@Override
public void open(String host, int port) throws Throwable {
this.openWithBackgroundThread(host, port);
this.submitReadTask();
public void open(final String host, final int port) {
this.executor.submit(new Runnable() {
@Override
public void run() {
try {
socket.connect(new InetSocketAddress(host, port));
invokeOpenEventHandler();
submitReadTask();
} catch (IOException e) {
Logging.Error(SocketAdapterImpl.class.getName(), "Error during connecting of socket", e.getCause());
invokeOpenErrorEventHandler(e.getMessage());
}
}
});
}
@Override
@ -74,6 +87,16 @@ public class SocketAdapterImpl implements SocketAdapter {
}
}
@Override
public void setOpenEventHandler(Consumer<Void> openEventHandler) {
this.openEventHandler = openEventHandler;
}
@Override
public void setOpenErrorEventHandler(Consumer<String> openErrorEventHandler) {
this.openErrorEventHandler = openErrorEventHandler;
}
@Override
public void setDataConsumer(Consumer<byte[]> dataConsumer) {
this.dataConsumer = dataConsumer;
@ -85,29 +108,8 @@ public class SocketAdapterImpl implements SocketAdapter {
}
@Override
public void setErrorHandler(Consumer<String> exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}
private void openWithBackgroundThread(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.getCause());
throw new RuntimeException(e);
}
}
});
try {
future.get();
}
catch (ExecutionException e) {
throw e.getCause();
}
public void setErrorEventHandler(Consumer<String> errorEventHandler) {
this.errorEventHandler = errorEventHandler;
}
private void submitReadTask() {
@ -151,6 +153,18 @@ public class SocketAdapterImpl implements SocketAdapter {
}
}
private void invokeOpenEventHandler() {
if (this.openEventHandler != null) {
this.openEventHandler.accept((Void)null);
}
}
private void invokeOpenErrorEventHandler(String errorMessage) {
if (this.openErrorEventHandler != null) {
this.openErrorEventHandler.accept(errorMessage);
}
}
private void invokeDataConsumer(byte[] data) {
if (this.dataConsumer != null) {
this.dataConsumer.accept(data);
@ -164,8 +178,8 @@ public class SocketAdapterImpl implements SocketAdapter {
}
private void invokeExceptionHandler(String errorMessage) {
if (this.exceptionHandler != null) {
this.exceptionHandler.accept(errorMessage);
if (this.errorEventHandler != null) {
this.errorEventHandler.accept(errorMessage);
}
}
}

View File

@ -47,15 +47,11 @@ public class SocketPlugin extends CordovaPlugin {
SocketAdapter socketAdapter = new SocketAdapterImpl();
socketAdapter.setCloseEventHandler(new CloseEventHandler(socketKey));
socketAdapter.setDataConsumer(new DataConsumer(socketKey));
socketAdapter.setErrorHandler(new ErrorHandler(socketKey));
socketAdapter.setErrorEventHandler(new ErrorEventHandler(socketKey));
socketAdapter.setOpenErrorEventHandler(new OpenErrorEventHandler(callbackContext));
socketAdapter.setOpenEventHandler(new OpenEventHandler(socketKey, socketAdapter, callbackContext));
try {
socketAdapter.open(host, port);
this.socketAdapters.put(socketKey, socketAdapter);
callbackContext.success();
} catch (Throwable t) {
callbackContext.error(t.toString());
}
socketAdapter.open(host, port);
}
private void write(CordovaArgs args, CallbackContext callbackContext) throws JSONException {
@ -198,9 +194,9 @@ public class SocketPlugin extends CordovaPlugin {
}
}
private class ErrorHandler implements Consumer<String> {
private class ErrorEventHandler implements Consumer<String> {
private String socketKey;
public ErrorHandler(String socketKey) {
public ErrorEventHandler(String socketKey) {
this.socketKey = socketKey;
}
@Override
@ -217,4 +213,31 @@ public class SocketPlugin extends CordovaPlugin {
}
}
}
private class OpenErrorEventHandler implements Consumer<String> {
private CallbackContext openCallbackContext;
public OpenErrorEventHandler(CallbackContext openCallbackContext) {
this.openCallbackContext = openCallbackContext;
}
@Override
public void accept(String errorMessage) {
this.openCallbackContext.error(errorMessage);
}
}
private class OpenEventHandler implements Consumer<Void> {
private String socketKey;
private SocketAdapter socketAdapter;
private CallbackContext openCallbackContext;
public OpenEventHandler(String socketKey, SocketAdapter socketAdapter, CallbackContext openCallbackContext) {
this.socketKey = socketKey;
this.socketAdapter = socketAdapter;
this.openCallbackContext = openCallbackContext;
}
@Override
public void accept(Void voidObject) {
socketAdapters.put(socketKey, socketAdapter);
this.openCallbackContext.success();
}
}
}