1
1
package com .github .heywhy .flutter_pusher ;
2
2
3
- import android .os .Handler ;
4
3
import android .util .Log ;
5
- import android .os .Looper ;
6
-
7
- import com .github .heywhy .flutter_pusher .listeners .*;
8
-
4
+ import com .github .heywhy .flutter_pusher .platform_messages .InstanceMessage ;
9
5
import com .google .gson .Gson ;
10
6
import com .google .gson .reflect .TypeToken ;
11
- import com .pusher .client .Pusher ;
12
- import com .pusher .client .PusherOptions ;
13
- import com .pusher .client .channel .Channel ;
14
- import com .pusher .client .connection .ConnectionEventListener ;
15
- import com .pusher .client .connection .ConnectionState ;
16
- import com .pusher .client .connection .ConnectionStateChange ;
17
- import com .pusher .client .util .ConnectionFactory ;
18
- import com .pusher .client .util .HttpAuthorizer ;
19
- import com .pusher .client .util .UrlEncodedConnectionFactory ;
20
-
21
- import org .json .JSONObject ;
22
-
23
- import java .lang .reflect .Type ;
24
- import java .util .HashMap ;
25
- import java .util .Map ;
26
-
27
7
import io .flutter .plugin .common .EventChannel ;
28
8
import io .flutter .plugin .common .MethodCall ;
29
9
import io .flutter .plugin .common .MethodChannel ;
30
10
import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
31
11
import io .flutter .plugin .common .MethodChannel .Result ;
32
12
import io .flutter .plugin .common .PluginRegistry .Registrar ;
33
13
14
+ import java .lang .reflect .Type ;
15
+ import java .util .HashMap ;
16
+ import java .util .Map ;
17
+
34
18
/**
35
19
* FlutterPusherPlugin
36
20
*/
37
21
public class FlutterPusherPlugin implements MethodCallHandler {
38
22
39
23
public static String TAG = "FlutterPusherPlugin" ;
40
24
public static EventChannel .EventSink eventSink ;
41
- public static boolean isLoggingEnabled = false ;
42
-
43
- private Pusher pusher ;
44
- private Map <String , Channel > channels = new HashMap <>();
45
25
46
- private static EventChannelListener eventListener ;
47
- private static PrivateChannelListener eventListenerPrivate ;
48
- private static PresenceChannelListener eventListenerPresence ;
26
+ private Map <String , PusherInstance > pusherInstanceMap = new HashMap <>();
49
27
50
28
/**
51
29
* Plugin registration.
@@ -54,10 +32,6 @@ public static void registerWith(Registrar registrar) {
54
32
final MethodChannel channel = new MethodChannel (registrar .messenger (), "com.github.heywhy/pusher" );
55
33
final EventChannel eventStream = new EventChannel (registrar .messenger (), "com.github.heywhy/pusherStream" );
56
34
57
- eventListener = new EventChannelListener ();
58
- eventListenerPrivate = new PrivateChannelListener ();
59
- eventListenerPresence = new PresenceChannelListener ();
60
-
61
35
channel .setMethodCallHandler (new FlutterPusherPlugin ());
62
36
eventStream .setStreamHandler (new EventChannel .StreamHandler () {
63
37
@ Override
@@ -74,296 +48,22 @@ public void onCancel(Object args) {
74
48
75
49
@ Override
76
50
public void onMethodCall (MethodCall call , Result result ) {
77
- switch (call .method ) {
78
- case "init" :
79
- init (call , result );
80
- break ;
81
- case "connect" :
82
- connect (call , result );
83
- break ;
84
- case "disconnect" :
85
- disconnect (call , result );
86
- break ;
87
- case "subscribe" :
88
- subscribe (call , result );
89
- break ;
90
- case "unsubscribe" :
91
- unsubscribe (call , result );
92
- break ;
93
- case "bind" :
94
- bind (call , result );
95
- break ;
96
- case "unbind" :
97
- unbind (call , result );
98
- break ;
99
- case "trigger" :
100
- // trigger(call, result);
Has a conversation. Original line has a conversation. 101
- break ;
102
- default :
103
- result .notImplemented ();
104
- break ;
51
+ Type type = new TypeToken <InstanceMessage >(){}.getType ();
52
+ InstanceMessage instanceMessage = new Gson ().fromJson (call .arguments .toString (), type );
53
+ String instanceId = instanceMessage .getInstanceId ();
54
+ PusherInstance instance = getPusherInstance (instanceId );
55
+ if (instance == null ) {
56
+ String message = String .format ("Instance with id %s not found" , instanceId );
57
+ throw new IllegalArgumentException (message );
105
58
}
106
- }
107
-
108
-
109
- private void init (MethodCall call , Result result ) {
110
- if (pusher != null ) {
111
- for (Map .Entry <String , Channel > entry : channels .entrySet ()) {
112
- String name = entry .getKey ();
113
- pusher .unsubscribe (name );
114
- channels .remove (name );
115
- }
116
- }
117
-
118
- try {
119
- final JSONObject json = new JSONObject (call .arguments .toString ());
120
- final JSONObject options = json .getJSONObject ("options" );
121
-
122
- if (json .has ("isLoggingEnabled" )) {
123
- isLoggingEnabled = json .getBoolean ("isLoggingEnabled" );
124
- }
125
59
126
- // setup options
127
- final PusherOptions pusherOptions = new PusherOptions ();
128
-
129
- if (options .has ("auth" )) {
130
- final JSONObject auth = options .getJSONObject ("auth" );
131
- final String endpoint = auth .getString ("endpoint" );
132
- final Type mapType = new TypeToken <Map <String , String >>() {}.getType ();
133
- final Map <String , String > headers = new Gson ().fromJson (auth .get ("headers" ).toString (), mapType );
134
-
135
- pusherOptions .setAuthorizer (getAuthorizer (endpoint , headers ));
136
- }
137
-
138
- if (options .has ("activityTimeout" )) {
139
- pusherOptions .setActivityTimeout (options .getInt ("activityTimeout" ));
140
- }
141
- if (options .has ("cluster" )) {
142
- pusherOptions .setCluster (options .getString ("cluster" ));
143
- }
144
- if (options .has ("host" )) {
145
- pusherOptions .setHost (options .getString ("host" ));
146
- }
147
-
148
- // defaults to encrypted connection on port 443
149
- final int port = options .has ("port" ) ? options .getInt ("port" ) : 443 ;
150
- final boolean encrypted = !options .has ("encrypted" ) || options .getBoolean ("encrypted" );
151
-
152
- if (encrypted ) {
153
- pusherOptions .setWssPort (port );
154
- } else {
155
- pusherOptions .setWsPort (port );
156
- }
157
- pusherOptions .setEncrypted (encrypted );
158
-
159
-
160
- // create client
161
- pusher = new Pusher (json .getString ("appKey" ), pusherOptions );
162
-
163
- if (isLoggingEnabled ) {
164
- Log .d (TAG , "init" );
165
- }
166
- result .success (null );
167
- } catch (Exception e ) {
168
- if (isLoggingEnabled ) {
169
- Log .d (TAG , "init error: " + e .getMessage ());
170
- e .printStackTrace ();
171
- }
172
- }
60
+ instance .onMethodCall (call , result );
173
61
}
174
62
175
- private void connect (MethodCall call , Result result ) {
176
- pusher .connect (new ConnectionEventListener () {
177
- @ Override
178
- public void onConnectionStateChange (final ConnectionStateChange change ) {
179
- new Handler (Looper .getMainLooper ()).post (new Runnable () {
180
- @ Override
181
- public void run () {
182
- try {
183
- final JSONObject eventStreamMessageJson = new JSONObject ();
184
- final JSONObject connectionStateChangeJson = new JSONObject ();
185
- connectionStateChangeJson .put ("currentState" , change .getCurrentState ().toString ());
186
- connectionStateChangeJson .put ("previousState" , change .getPreviousState ().toString ());
187
- eventStreamMessageJson .put ("connectionStateChange" , connectionStateChangeJson );
188
- eventSink .success (eventStreamMessageJson .toString ());
189
- } catch (Exception e ) {
190
- if (isLoggingEnabled ) {
191
- Log .d (TAG , "onConnectionStateChange error: " + e .getMessage ());
192
- e .printStackTrace ();
193
- }
194
- }
195
- }
196
- });
197
- }
198
-
199
- @ Override
200
- public void onError (final String message , final String code , final Exception ex ) {
201
- new Handler (Looper .getMainLooper ()).post (new Runnable () {
202
- @ Override
203
- public void run () {
204
- try {
205
- final String exMessage = ex != null ? ex .getMessage () : null ;
206
- final JSONObject eventStreamMessageJson = new JSONObject ();
207
- final JSONObject connectionErrorJson = new JSONObject ();
208
-
209
- connectionErrorJson .put ("message" , message );
210
- connectionErrorJson .put ("code" , code );
211
- connectionErrorJson .put ("exception" , exMessage );
212
- eventStreamMessageJson .put ("connectionError" , connectionErrorJson );
213
-
214
- eventSink .success (eventStreamMessageJson .toString ());
215
-
216
- } catch (Exception e ) {
217
- if (isLoggingEnabled ) {
218
- Log .d (TAG , "onError exception: " + e .getMessage ());
219
- e .printStackTrace ();
220
- }
221
- }
222
- }
223
- });
224
- }
225
-
226
- }, ConnectionState .ALL );
227
-
228
- if (isLoggingEnabled ) {
229
- Log .d (TAG , "connect" );
230
- }
231
- result .success (null );
232
- }
233
-
234
-
235
- private void disconnect (MethodCall call , Result result ) {
236
- pusher .disconnect ();
237
- if (isLoggingEnabled ) {
238
- Log .d (TAG , "disconnect" );
239
- }
240
- result .success (null );
241
- }
242
-
243
- private void subscribe (MethodCall call , Result result ) {
244
- final String channelName = call .arguments .toString ();
245
- final String channelType = channelName .split ("-" )[0 ];
246
- Channel channel = channels .get (channelName );
247
-
248
- if (channel != null && channel .isSubscribed ()) {
249
- if (isLoggingEnabled ) {
250
- Log .d (TAG , "Already subscribed, ignoring ..." );
251
- }
252
- result .success (null );
253
- return ;
254
- }
255
-
256
- switch (channelType ) {
257
- case "private" :
258
- channel = pusher .subscribePrivate (channelName , eventListenerPrivate );
259
- if (isLoggingEnabled ) {
260
- Log .d (TAG , "subscribe (private)" );
261
- }
262
- break ;
263
- case "presence" :
264
- channel = pusher .subscribePresence (channelName , eventListenerPresence );
265
- if (isLoggingEnabled ) {
266
- Log .d (TAG , "subscribe (presence)" );
267
- }
268
- break ;
269
- default :
270
- channel = pusher .subscribe (channelName , eventListener );
271
-
272
- if (isLoggingEnabled ) {
273
- Log .d (TAG , "subscribe" );
274
- }
275
- break ;
63
+ private PusherInstance getPusherInstance (String instanceId ) {
64
+ if (instanceId != null && !pusherInstanceMap .containsKey (instanceId )) {
65
+ pusherInstanceMap .put (instanceId , new PusherInstance (instanceId ));
276
66
}
277
-
278
- channels .put (channelName , channel );
279
- result .success (null );
280
- }
281
-
282
- private void unsubscribe (MethodCall call , Result result ) {
283
- final String channelName = call .arguments .toString ();
284
- pusher .unsubscribe (call .arguments .toString ());
285
- channels .remove (channelName );
286
-
287
- if (isLoggingEnabled ) {
288
- Log .d (TAG , String .format ("unsubscribe (%s)" , channelName ));
289
- }
290
- result .success (null );
291
- }
292
-
293
- private void bind (MethodCall call , Result result ) {
294
- try {
295
- final JSONObject json = new JSONObject (call .arguments .toString ());
296
- final String channelName = json .getString ("channelName" );
297
- final String channelType = channelName .split ("-" )[0 ];
298
- final String eventName = json .getString ("eventName" );
299
-
300
- Channel channel = channels .get (channelName );
301
-
302
- switch (channelType ) {
303
- case "private" :
304
- channel .bind (eventName , eventListenerPrivate );
305
- break ;
306
- case "presence" :
307
- channel .bind (eventName , eventListenerPresence );
308
- break ;
309
- default :
310
- channel .bind (eventName , eventListener );
311
- break ;
312
- }
313
-
314
- if (isLoggingEnabled ) {
315
- Log .d (TAG , String .format ("bind (%s)" , eventName ));
316
- }
317
- result .success (null );
318
- } catch (Exception e ) {
319
- if (isLoggingEnabled ) {
320
- Log .d (TAG , String .format ("bind exception: %s" , e .getMessage ()));
321
- e .printStackTrace ();
322
- }
323
- }
324
- }
325
-
326
- private void unbind (MethodCall call , Result result ) {
327
- try {
328
- final JSONObject json = new JSONObject (call .arguments .toString ());
329
- final String channelName = json .getString ("channelName" );
330
- final String channelType = channelName .split ("-" )[0 ];
331
- final String eventName = json .getString ("eventName" );
332
-
333
- Channel channel = channels .get (channelName );
334
- switch (channelType ) {
335
- case "private" :
336
- channel .unbind (eventName , eventListenerPrivate );
337
- break ;
338
- case "presence" :
339
- channel .unbind (eventName , eventListenerPresence );
340
- break ;
341
- default :
342
- channel .unbind (eventName , eventListener );
343
- break ;
344
- }
345
-
346
- if (isLoggingEnabled ) {
347
- Log .d (TAG , String .format ("unbind (%s)" , eventName ));
348
- }
349
- result .success (null );
350
- } catch (Exception e ) {
351
- if (isLoggingEnabled ) {
352
- Log .d (TAG , String .format ("unbind exception: %s" , e .getMessage ()));
353
- e .printStackTrace ();
354
- }
355
- }
356
- }
357
-
358
- private HttpAuthorizer getAuthorizer (String endpoint , Map <String , String > headers ) {
359
- final ConnectionFactory connection = headers .containsValue ("application/json" )
360
- ? new JsonEncodedConnectionFactory ()
361
- : new UrlEncodedConnectionFactory ();
362
-
363
- final HttpAuthorizer authorizer = new HttpAuthorizer (endpoint , connection );
364
- authorizer .setHeaders (headers );
365
-
366
- return authorizer ;
367
-
67
+ return pusherInstanceMap .get (instanceId );
368
68
}
369
69
}