diff --git a/src/main/java/com/pusher/client/connection/impl/PusherEventHandler.java b/src/main/java/com/pusher/client/connection/impl/PusherEventHandler.java new file mode 100644 index 00000000..5dd344f5 --- /dev/null +++ b/src/main/java/com/pusher/client/connection/impl/PusherEventHandler.java @@ -0,0 +1,12 @@ +package com.pusher.client.connection.impl; + +import com.pusher.client.channel.PusherEvent; + +/** + * Receives events from the connection. + *

+ * Used instead of java.util.function.Consumer, which isn't available before Android 7 (API 24). + */ +public interface PusherEventHandler { + void handle(PusherEvent event); +} diff --git a/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java b/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java index 81f650ce..b2893674 100644 --- a/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java +++ b/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java @@ -6,6 +6,7 @@ import com.pusher.client.connection.ConnectionState; import com.pusher.client.connection.ConnectionStateChange; import com.pusher.client.connection.impl.InternalConnection; +import com.pusher.client.connection.impl.PusherEventHandler; import com.pusher.client.util.Factory; import org.java_websocket.handshake.ServerHandshake; @@ -20,7 +21,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; import java.util.logging.Logger; import javax.net.ssl.SSLException; @@ -42,7 +42,7 @@ public class WebSocketConnection implements InternalConnection, WebSocketListene private volatile ConnectionState state = ConnectionState.DISCONNECTED; private WebSocketClientWrapper underlyingConnection; - private final Consumer eventHandler; + private final PusherEventHandler eventHandler; private String socketId; private int reconnectAttempts = 0; private volatile Future reconnectTimer; @@ -54,7 +54,7 @@ public WebSocketConnection( int maxReconnectionAttempts, int maxReconnectionGap, final Proxy proxy, - final Consumer eventHandler, + final PusherEventHandler eventHandler, final Factory factory ) throws URISyntaxException { webSocketUri = new URI(url); @@ -167,7 +167,7 @@ private void handleEvent(final PusherEvent event) { } else if (event.getEventName().equals("pusher:error")) { handleError(event); } - eventHandler.accept(event); + eventHandler.handle(event); } @SuppressWarnings("rawtypes") diff --git a/src/main/java/com/pusher/client/util/Factory.java b/src/main/java/com/pusher/client/util/Factory.java index 98f21209..71f44dc0 100644 --- a/src/main/java/com/pusher/client/util/Factory.java +++ b/src/main/java/com/pusher/client/util/Factory.java @@ -10,6 +10,7 @@ import com.pusher.client.channel.impl.PrivateChannelImpl; import com.pusher.client.channel.impl.PrivateEncryptedChannelImpl; import com.pusher.client.connection.impl.InternalConnection; +import com.pusher.client.connection.impl.PusherEventHandler; import com.pusher.client.connection.websocket.WebSocketClientWrapper; import com.pusher.client.connection.websocket.WebSocketConnection; import com.pusher.client.connection.websocket.WebSocketListener; @@ -23,7 +24,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; -import java.util.function.Consumer; import javax.net.ssl.SSLException; @@ -55,7 +55,7 @@ public class Factory { public synchronized InternalConnection getConnection( final String apiKey, final PusherOptions options, - final Consumer eventHandler + final PusherEventHandler eventHandler ) { if (connection == null) { try { diff --git a/src/test/java/com/pusher/client/EndToEndTest.java b/src/test/java/com/pusher/client/EndToEndTest.java index d4524175..a7a507ae 100644 --- a/src/test/java/com/pusher/client/EndToEndTest.java +++ b/src/test/java/com/pusher/client/EndToEndTest.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.pusher.client.connection.impl.PusherEventHandler; import com.pusher.client.channel.PusherEvent; import com.pusher.client.channel.impl.ChannelManager; import com.pusher.client.connection.ConnectionEventListener; @@ -33,7 +34,6 @@ import java.net.Proxy; import java.net.URI; -import java.util.function.Consumer; @RunWith(MockitoJUnitRunner.class) public class EndToEndTest { @@ -57,7 +57,7 @@ public class EndToEndTest { private ChannelAuthorizer mockChannelAuthorizer; @Mock - private Consumer mockEventHandler; + private PusherEventHandler mockEventHandler; @Mock private ConnectionEventListener mockConnectionEventListener; @@ -117,7 +117,7 @@ public WebSocketClientWrapper answer(final InvocationOnMock invocation) throws T } ); - when(factory.getConnection(eq(API_KEY), eq(pusherOptions), any(Consumer.class))).thenReturn(connection); + when(factory.getConnection(eq(API_KEY), eq(pusherOptions), any(PusherEventHandler.class))).thenReturn(connection); when(factory.getChannelManager()) .thenAnswer( diff --git a/src/test/java/com/pusher/client/PusherTest.java b/src/test/java/com/pusher/client/PusherTest.java index 6ef9e010..ae4ba43a 100644 --- a/src/test/java/com/pusher/client/PusherTest.java +++ b/src/test/java/com/pusher/client/PusherTest.java @@ -10,6 +10,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.pusher.client.connection.impl.PusherEventHandler; import com.pusher.client.channel.ChannelEventListener; import com.pusher.client.channel.PresenceChannelEventListener; import com.pusher.client.channel.PrivateChannelEventListener; @@ -33,7 +34,6 @@ import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; -import java.util.function.Consumer; @RunWith(MockitoJUnitRunner.class) public class PusherTest { @@ -88,7 +88,7 @@ public void setUp() { .setChannelAuthorizer(channelAuthorizer) .setUserAuthenticator(new HttpUserAuthenticator("http://user-auth.com")); - when(factory.getConnection(eq(API_KEY), any(PusherOptions.class), any(Consumer.class))) + when(factory.getConnection(eq(API_KEY), any(PusherOptions.class), any(PusherEventHandler.class))) .thenReturn(mockConnection); when(factory.getChannelManager()).thenReturn(mockChannelManager); when(factory.newPublicChannel(PUBLIC_CHANNEL_NAME)).thenReturn(mockPublicChannel); diff --git a/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java b/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java index 296f6e85..7adae136 100644 --- a/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java +++ b/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java @@ -15,6 +15,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.pusher.client.connection.impl.PusherEventHandler; import com.pusher.client.channel.PusherEvent; import com.pusher.client.connection.ConnectionEventListener; import com.pusher.client.connection.ConnectionState; @@ -39,7 +40,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; import javax.net.ssl.SSLException; @@ -62,7 +62,7 @@ public class WebSocketConnectionTest { private WebSocketClientWrapper mockUnderlyingConnection; @Mock - private Consumer mockEventHandler; + private PusherEventHandler mockEventHandler; @Mock private ConnectionEventListener mockEventListener; @@ -271,7 +271,7 @@ public void testReceiveUserMessagePassesMessageToEventHandler() { connect(); connection.onMessage(INCOMING_MESSAGE); - // verify(mockEventHandler).accept(EVENT_NAME, INCOMING_MESSAGE); + // verify(mockEventHandler).handle(EVENT_NAME, INCOMING_MESSAGE); } @Test