This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
ChannelActivity.java
668 lines (608 loc) · 24.8 KB
/
ChannelActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package com.twilio.ipmessaging.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import com.twilio.ipmessaging.Channel;
import com.twilio.ipmessaging.Channel.ChannelType;
import com.twilio.ipmessaging.ChannelListener;
import com.twilio.ipmessaging.Channels;
import com.twilio.ipmessaging.Constants;
import com.twilio.ipmessaging.Constants.CreateChannelListener;
import com.twilio.ipmessaging.Constants.StatusListener;
import com.twilio.ipmessaging.IPMessagingClientListener;
import com.twilio.ipmessaging.Member;
import com.twilio.ipmessaging.Message;
import com.twilio.ipmessaging.IPMessagingClient;
import com.twilio.ipmessaging.ErrorInfo;
import com.twilio.ipmessaging.UserInfo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import uk.co.ribot.easyadapter.EasyAdapter;
import org.json.JSONObject;
import org.json.JSONException;
@SuppressLint("InflateParams")
public class ChannelActivity extends Activity implements ChannelListener, IPMessagingClientListener
{
private static final Logger logger = Logger.getLogger(ChannelActivity.class);
private static final String[] CHANNEL_OPTIONS = { "Join" };
private static final int JOIN = 0;
private ListView listView;
private BasicIPMessagingClient basicClient;
private List<Channel> channels = new ArrayList<Channel>();
private EasyAdapter<Channel> adapter;
private AlertDialog createChannelDialog;
private Channels channelsObject;
private Channel[] channelArray;
private static final Handler handler = new Handler();
private AlertDialog incomingChannelInvite;
private StatusListener joinListener;
private StatusListener declineInvitationListener;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_channel);
basicClient = TwilioApplication.get().getBasicClient();
if (basicClient != null && basicClient.getIpMessagingClient() != null) {
basicClient.getIpMessagingClient().setListener(ChannelActivity.this);
setupListView();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.channel, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.action_create_public:
showCreateChannelDialog(ChannelType.PUBLIC);
break;
case R.id.action_create_private:
showCreateChannelDialog(ChannelType.PRIVATE);
break;
case R.id.action_create_public_withoptions:
createChannelWithType(ChannelType.PUBLIC);
break;
case R.id.action_create_private_withoptions:
createChannelWithType(ChannelType.PRIVATE);
break;
case R.id.action_search_by_unique_name:
showSearchChannelDialog();
break;
case R.id.action_user_info:
startActivity(new Intent(getApplicationContext(), UserInfoActivity.class));
break;
case R.id.action_logout:
basicClient.getIpMessagingClient().shutdown();
finish();
break;
case R.id.action_unregistercm: {
String gcmToken = basicClient.getGCMToken();
basicClient.getIpMessagingClient().unregisterGCMToken(
gcmToken, new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
logger.w("GCM unregistration not successful");
runOnUiThread(new Runnable() {
public void run()
{
Toast
.makeText(ChannelActivity.this,
"GCM unregistration not successful",
Toast.LENGTH_SHORT)
.show();
}
});
}
@Override
public void onSuccess()
{
logger.d("GCM unregistration successful");
runOnUiThread(new Runnable() {
public void run()
{
Toast
.makeText(ChannelActivity.this,
"GCM unregistration successful",
Toast.LENGTH_SHORT)
.show();
}
});
}
});
}
break;
}
return super.onOptionsItemSelected(item);
}
private void createChannelWithType(ChannelType type)
{
Random rand = new Random();
int value = rand.nextInt(50);
final JSONObject attrs = new JSONObject();
try {
attrs.put("topic", "testing channel creation with options " + value);
} catch (JSONException xcp) {
logger.e("JSON exception", xcp);
}
basicClient.getIpMessagingClient().getChannels()
.channelBuilder()
.withFriendlyName("Pub_TestChannelF_" + value)
.withUniqueName("Pub_TestChannelU_" + value)
.withType(type)
.withAttributes(attrs)
.build(new CreateChannelListener() {
@Override
public void onCreated(final Channel newChannel)
{
logger.d("Successfully created a channel with options.");
}
@Override
public void onError(ErrorInfo errorInfo)
{
logger.e("Error creating a channel");
}
});
}
@Override
protected void onResume()
{
super.onResume();
handleIncomingIntent(getIntent());
getChannels();
}
private boolean handleIncomingIntent(Intent intent)
{
if (intent != null) {
Channel channel = intent.getParcelableExtra(Constants.EXTRA_CHANNEL);
String action = intent.getStringExtra(Constants.EXTRA_ACTION);
intent.removeExtra(Constants.EXTRA_CHANNEL);
intent.removeExtra(Constants.EXTRA_ACTION);
if (action != null) {
if (action.compareTo(Constants.EXTRA_ACTION_INVITE) == 0) {
this.showIncomingInvite(channel);
}
}
}
return false;
}
private void showCreateChannelDialog(final ChannelType type)
{
AlertDialog.Builder builder = new AlertDialog.Builder(ChannelActivity.this);
String title = "Enter " + type.toString() + " name";
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(getLayoutInflater().inflate(R.layout.dialog_add_channel, null))
.setTitle(title)
.setPositiveButton(
"Create",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id)
{
String channelName =
((EditText)createChannelDialog.findViewById(R.id.channel_name))
.getText()
.toString();
logger.d("Creating channel with friendly Name|" + channelName + "|");
Channels channelsLocal = basicClient.getIpMessagingClient().getChannels();
channelsLocal.createChannel(channelName, type, new CreateChannelListener() {
@Override
public void onCreated(final Channel newChannel)
{
logger.d("Successfully created a channel");
if (newChannel != null) {
final String sid = newChannel.getSid();
ChannelType type = newChannel.getType();
newChannel.setListener(ChannelActivity.this);
logger.d("Channel created with sid|" + sid + "| and type |"
+ type.toString()
+ "|");
}
}
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().logErrorInfo("Error creating channel",
errorInfo);
}
});
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
createChannelDialog = builder.create();
createChannelDialog.show();
}
private void showSearchChannelDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(ChannelActivity.this);
String title = "Enter unique channel name";
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(getLayoutInflater().inflate(R.layout.dialog_search_channel, null))
.setTitle(title)
.setPositiveButton("Search", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id)
{
String channelName =
((EditText)createChannelDialog.findViewById(R.id.channel_name))
.getText()
.toString();
logger.d("Searching for " + channelName);
Channels channelsLocal = basicClient.getIpMessagingClient().getChannels();
final Channel channel = channelsLocal.getChannelByUniqueName(channelName);
runOnUiThread(new Runnable() {
@Override
public void run()
{
if (channel != null) {
showToast(channel.getSid() + ":" + channel.getFriendlyName());
} else {
showToast("Channel not found.");
}
}
});
}
});
createChannelDialog = builder.create();
createChannelDialog.show();
}
private void setupListView()
{
listView = (ListView)findViewById(R.id.channel_list);
adapter = new EasyAdapter<Channel>(
this,
ChannelViewHolder.class,
channels,
new ChannelViewHolder.OnChannelClickListener() {
@Override
public void onChannelClicked(final Channel channel)
{
if (channel.getStatus() == Channel.ChannelStatus.JOINED) {
final Channel channelSelected = channelsObject.getChannel(channel.getSid());
if (channelSelected != null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run()
{
Intent i =
new Intent(ChannelActivity.this, MessageActivity.class);
i.putExtra(Constants.EXTRA_CHANNEL,
(Parcelable)channelSelected);
i.putExtra("C_SID", channelSelected.getSid());
startActivity(i);
}
}, 0);
}
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(ChannelActivity.this);
builder.setTitle("Select an option")
.setItems(CHANNEL_OPTIONS, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
if (which == JOIN) {
dialog.cancel();
joinListener = new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().logErrorInfo(
"failed to join channel", errorInfo);
}
@Override
public void onSuccess()
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
adapter.notifyDataSetChanged();
}
});
logger.d("Successfully joined channel");
}
};
channel.join(joinListener);
}
}
});
builder.show();
}
});
listView.setAdapter(adapter);
getChannels();
}
private void getChannels()
{
if (channels != null) {
if (basicClient != null && basicClient.getIpMessagingClient() != null) {
channelsObject = basicClient.getIpMessagingClient().getChannels();
channels.clear();
if (channelsObject != null) {
channelArray = channelsObject.getChannels();
setupListenersForChannel(channelArray);
if (channelArray != null) {
channels.addAll(new ArrayList<Channel>(Arrays.asList(channelArray)));
Collections.sort(channels, new CustomChannelComparator());
adapter.notifyDataSetChanged();
}
}
}
}
}
private void showIncomingInvite(final Channel channel)
{
handler.post(new Runnable() {
@Override
public void run()
{
if (incomingChannelInvite == null) {
incomingChannelInvite =
new AlertDialog.Builder(ChannelActivity.this)
.setTitle(R.string.incoming_call)
.setMessage(R.string.incoming_call_message)
.setPositiveButton(
R.string.join,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
channel.join(new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().logErrorInfo(
"Failed to join channel", errorInfo);
}
@Override
public void onSuccess()
{
runOnUiThread(new Runnable() {
@Override
public void run()
{
adapter.notifyDataSetChanged();
}
});
logger.d("Successfully joined channel");
}
});
incomingChannelInvite = null;
}
})
.setNegativeButton(
R.string.decline,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
declineInvitationListener = new StatusListener() {
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().logErrorInfo(
"Failed to decline channel invite", errorInfo);
}
@Override
public void onSuccess()
{
logger.d("Successfully declined channel invite");
}
};
channel.declineInvitation(declineInvitationListener);
incomingChannelInvite = null;
}
})
.create();
incomingChannelInvite.show();
}
}
});
}
private class CustomChannelComparator implements Comparator<Channel>
{
@Override
public int compare(Channel lhs, Channel rhs)
{
return lhs.getFriendlyName().compareTo(rhs.getFriendlyName());
}
}
private void setupListenersForChannel(Channel[] channelArray)
{
if (channelArray != null) {
for (int i = 0; i < channelArray.length; i++) {
if (channelArray[i] != null) {
channelArray[i].setListener(ChannelActivity.this);
}
}
}
}
private void showToast(String text)
{
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
// ChannelListener implementation
@Override
public void onTypingStarted(Member member)
{
if (member != null) {
logger.d(member.getUserInfo().getIdentity() + " started typing");
}
}
@Override
public void onTypingEnded(Member member)
{
if (member != null) {
logger.d(member.getUserInfo().getIdentity() + " ended typing");
}
}
@Override
public void onSynchronizationChange(Channel channel)
{
logger.e("Received onSynchronizationChange callback for channel |"
+ channel.getFriendlyName()
+ "|");
}
@Override
public void onChannelSynchronizationChange(Channel channel)
{
logger.e("Received onChannelSynchronizationChange callback for channel |"
+ channel.getFriendlyName()
+ "|");
}
@Override
public void onClientSynchronization(IPMessagingClient.SynchronizationStatus status)
{
logger.e("Received onClientSynchronization callback " + status.toString());
}
@Override
public void onUserInfoChange(UserInfo userInfo)
{
logger.e("Received onUserInfoChange callback");
}
@Override
public void onToastNotification(String channelId, String messageId)
{
logger.d("Received new push notification");
runOnUiThread(new Runnable() {
@Override
public void run()
{
showToast("Received new push notification");
}
});
}
@Override
public void onToastSubscribed()
{
logger.d("Subscribed to push notifications");
runOnUiThread(new Runnable() {
@Override
public void run()
{
showToast("Subscribed to push notifications");
}
});
}
@Override
public void onToastFailed(ErrorInfo errorInfo)
{
logger.d("Failed to subscribe to push notifications");
runOnUiThread(new Runnable() {
@Override
public void run()
{
showToast("Failed to subscribe to push notifications");
}
});
}
@Override
public void onChannelAdd(Channel channel)
{
logger.d("Received onChannelAdd callback for channel |" + channel.getFriendlyName() + "|");
runOnUiThread(new Runnable() {
@Override
public void run()
{
getChannels();
}
});
}
@Override
public void onChannelChange(Channel channel)
{
logger.d("Received onChannelChange callback for channel |" + channel.getFriendlyName()
+ "|");
}
@Override
public void onChannelDelete(Channel channel)
{
logger.d("Received onChannelDelete callback for channel |" + channel.getFriendlyName()
+ "|");
}
@Override
public void onError(ErrorInfo errorInfo)
{
TwilioApplication.get().logErrorInfo("Received onError callback", errorInfo);
}
// Message-related callbacks
@Override
public void onMessageAdd(Message message)
{
if (message != null) {
logger.d("Received onMessageAdd event");
}
}
@Override
public void onMessageChange(Message message)
{
if (message != null) {
logger.d("Received onMessageChange event");
}
}
@Override
public void onMessageDelete(Message message)
{
logger.d("Received onMessageDelete event");
}
// Member-related callbacks
@Override
public void onMemberJoin(Member member)
{
if (member != null) {
logger.d("Member " + member.getUserInfo().getIdentity() + " joined");
}
}
@Override
public void onMemberChange(Member member)
{
if (member != null) {
logger.d("Member " + member.getUserInfo().getIdentity() + " changed");
}
}
@Override
public void onMemberDelete(Member member)
{
if (member != null) {
logger.d("Member " + member.getUserInfo().getIdentity() + " deleted");
}
}
@Override
public void onAttributesChange(Map<String, String> map)
{
}
}