diff options
author | Jose <jose@zeroc.com> | 2012-12-14 17:32:22 +0100 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2012-12-14 17:32:22 +0100 |
commit | 301cc174e4804045f66ce23b34bff84942db7a09 (patch) | |
tree | da51c05094cb3c379c9c4cb00cdf63e094568f1a /java/src | |
parent | Fixed (ICE-4913) - Deprecate Ice::Stats (diff) | |
download | ice-301cc174e4804045f66ce23b34bff84942db7a09.tar.bz2 ice-301cc174e4804045f66ce23b34bff84942db7a09.tar.xz ice-301cc174e4804045f66ce23b34bff84942db7a09.zip |
Fixed (ICE-5114) - android library demo exceptions.
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/IceInternal/TcpTransceiver.java | 12 | ||||
-rw-r--r-- | java/src/IceInternal/UdpTransceiver.java | 10 | ||||
-rw-r--r-- | java/src/IceInternal/Util.java | 41 | ||||
-rw-r--r-- | java/src/IceSSL/TransceiverI.java | 10 |
4 files changed, 72 insertions, 1 deletions
diff --git a/java/src/IceInternal/TcpTransceiver.java b/java/src/IceInternal/TcpTransceiver.java index 586433dd21e..e05df24c7d9 100644 --- a/java/src/IceInternal/TcpTransceiver.java +++ b/java/src/IceInternal/TcpTransceiver.java @@ -89,6 +89,16 @@ final class TcpTransceiver implements Transceiver public boolean write(Buffer buf) { + // + // We don't want write to be called on android main thread as this will cause + // NetworkOnMainThreadException to be thrown. If that is the android main thread + // we return false and this method will be later called from the thread pool. + // + if(Util.isAndroidMainThread(Thread.currentThread())) + { + return false; + } + final int size = buf.b.limit(); int packetSize = size - buf.b.position(); @@ -115,7 +125,7 @@ final class TcpTransceiver implements Transceiver else if(ret == 0) { // - // Writing would block, so we reset the limit (if necessary) and return true to indicate + // Writing would block, so we reset the limit (if necessary) and return false to indicate // that more data must be sent. // if(packetSize == _maxSendPacketSize) diff --git a/java/src/IceInternal/UdpTransceiver.java b/java/src/IceInternal/UdpTransceiver.java index 94912ddcf11..8b49ec6754b 100644 --- a/java/src/IceInternal/UdpTransceiver.java +++ b/java/src/IceInternal/UdpTransceiver.java @@ -52,6 +52,16 @@ final class UdpTransceiver implements Transceiver public boolean write(Buffer buf) { + // + // We don't want write or send to be called on android main thread as this will cause + // NetworkOnMainThreadException to be thrown. If that is the android main thread + // we return false and this method will be later called from the thread pool + // + if(Util.isAndroidMainThread(Thread.currentThread())) + { + return false; + } + assert(buf.b.position() == 0); assert(_fd != null && _state >= StateConnected); diff --git a/java/src/IceInternal/Util.java b/java/src/IceInternal/Util.java index d63ed0ea43e..04df20c01ae 100644 --- a/java/src/IceInternal/Util.java +++ b/java/src/IceInternal/Util.java @@ -193,4 +193,45 @@ public final class Util } return java.lang.Thread.NORM_PRIORITY; } + + // + // Return true if the given thread is the android main thread, also know as the GUI thread. + // + public static boolean + isAndroidMainThread(Thread thread) + { + if(!System.getProperty("java.vm.name").startsWith("Dalvik")) + { + return false; + } + + if(_androidMainThread == null) + { + try + { + Class<?> c = Util.findClass("android.os.Looper", null); + java.lang.reflect.Method getMainLooper = c.getDeclaredMethod("getMainLooper", (Class<?>[])null); + java.lang.reflect.Method getThread = c.getDeclaredMethod("getThread", (Class<?>[])null); + + Object looper = getMainLooper.invoke(null); + _androidMainThread = (Thread)getThread.invoke(looper); + } + catch(java.lang.reflect.InvocationTargetException ex) + { + assert false; + } + catch(java.lang.NoSuchMethodException ex) + { + assert false; + } + catch(IllegalAccessException ex) + { + assert false; + } + } + + return thread != null && _androidMainThread == thread; + } + + private static Thread _androidMainThread; } diff --git a/java/src/IceSSL/TransceiverI.java b/java/src/IceSSL/TransceiverI.java index d224ad79980..ecd2e8e12cf 100644 --- a/java/src/IceSSL/TransceiverI.java +++ b/java/src/IceSSL/TransceiverI.java @@ -163,6 +163,16 @@ final class TransceiverI implements IceInternal.Transceiver throw new Ice.ConnectionLostException(); } + // + // We don't want write to be called on android main thread as this will cause + // NetworkOnMainThreadException to be thrown. If that is the android main thread + // we return false and this method will be later called from the thread pool. + // + if(IceInternal.Util.isAndroidMainThread(Thread.currentThread())) + { + return false; + } + int status = writeNonBlocking(buf.b); if(status != IceInternal.SocketOperation.None) { |