11
11
import android .os .IBinder ;
12
12
import android .preference .PreferenceManager ;
13
13
import android .util .Log ;
14
+ import android .view .View ;
14
15
import android .view .Window ;
16
+ import android .widget .LinearLayout ;
17
+
18
+ import androidx .annotation .Nullable ;
19
+ import androidx .core .content .ContextCompat ;
15
20
16
21
import com .quickblox .conference .ConferenceSession ;
17
22
import com .quickblox .conference .WsException ;
18
23
import com .quickblox .conference .WsHangUpException ;
19
24
import com .quickblox .conference .WsNoResponseException ;
20
25
import com .quickblox .conference .callbacks .ConferenceSessionCallbacks ;
21
- import com .quickblox .core .QBEntityCallback ;
22
- import com .quickblox .core .exception .QBResponseException ;
23
26
import com .quickblox .sample .videochat .conference .java .R ;
24
27
import com .quickblox .sample .videochat .conference .java .fragments .ConversationFragment ;
25
28
import com .quickblox .sample .videochat .conference .java .fragments .ConversationFragmentCallback ;
29
+ import com .quickblox .sample .videochat .conference .java .fragments .ReconnectionCallback ;
26
30
import com .quickblox .sample .videochat .conference .java .fragments .ScreenShareFragment ;
27
31
import com .quickblox .sample .videochat .conference .java .managers .WebRtcSessionManager ;
28
32
import com .quickblox .sample .videochat .conference .java .services .CallService ;
29
33
import com .quickblox .sample .videochat .conference .java .utils .Consts ;
30
34
import com .quickblox .sample .videochat .conference .java .utils .SettingsUtils ;
31
35
import com .quickblox .sample .videochat .conference .java .utils .ToastUtils ;
32
- import com .quickblox .users .QBUsers ;
33
- import com .quickblox .users .model .QBUser ;
34
36
import com .quickblox .videochat .webrtc .BaseSession ;
35
37
import com .quickblox .videochat .webrtc .QBRTCScreenCapturer ;
36
38
import com .quickblox .videochat .webrtc .callbacks .QBRTCSessionStateCallback ;
41
43
import java .io .Serializable ;
42
44
import java .util .ArrayList ;
43
45
import java .util .HashMap ;
46
+ import java .util .HashSet ;
44
47
import java .util .List ;
45
-
46
- import androidx .annotation .Nullable ;
47
- import androidx .core .content .ContextCompat ;
48
- import androidx .fragment .app .Fragment ;
48
+ import java .util .Set ;
49
49
50
50
public class CallActivity extends BaseActivity implements QBRTCSessionStateCallback <ConferenceSession >, ConferenceSessionCallbacks ,
51
51
ConversationFragmentCallback , ScreenShareFragment .OnSharingEvents {
@@ -65,6 +65,9 @@ public class CallActivity extends BaseActivity implements QBRTCSessionStateCallb
65
65
private ArrayList <CallService .CurrentCallStateCallback > currentCallStateCallbackList = new ArrayList <>();
66
66
private volatile boolean connectedToJanus ;
67
67
private CallService callService ;
68
+ private final Set <ReconnectionCallback > reconnectionCallbacks = new HashSet <>();
69
+ private final ReconnectionListenerImpl reconnectionListener = new ReconnectionListenerImpl (TAG );
70
+ private LinearLayout reconnectingLayout ;
68
71
69
72
public static void start (Context context , String roomID , String roomTitle , String dialogID ,
70
73
List <Integer > occupants , boolean listenerRole ) {
@@ -84,6 +87,16 @@ public static void start(Context context) {
84
87
context .startActivity (intent );
85
88
}
86
89
90
+ @ Override
91
+ public void addReconnectionCallback (ReconnectionCallback reconnectionCallback ) {
92
+ reconnectionCallbacks .add (reconnectionCallback );
93
+ }
94
+
95
+ @ Override
96
+ public void removeReconnectionCallback (ReconnectionCallback reconnectionCallback ) {
97
+ reconnectionCallbacks .remove (reconnectionCallback );
98
+ }
99
+
87
100
@ Override
88
101
protected void onCreate (Bundle savedInstanceState ) {
89
102
super .onCreate (savedInstanceState );
@@ -95,6 +108,8 @@ protected void onCreate(Bundle savedInstanceState) {
95
108
PreferenceManager .setDefaultValues (this , R .xml .preferences_video , false );
96
109
PreferenceManager .setDefaultValues (this , R .xml .preferences_audio , false );
97
110
111
+ reconnectingLayout = findViewById (R .id .llReconnecting );
112
+
98
113
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
99
114
Window w = getWindow ();
100
115
w .setStatusBarColor (ContextCompat .getColor (this , R .color .color_new_blue ));
@@ -116,8 +131,12 @@ private void initScreen() {
116
131
currentSession = sessionManager .getCurrentSession ();
117
132
initListeners (currentSession );
118
133
119
- if (callService .isSharingScreenState ()) {
120
- startScreenSharing (null );
134
+ if (callService != null && callService .isSharingScreenState ()) {
135
+ if (callService .getReconnectionState () == CallService .ReconnectionState .COMPLETED ) {
136
+ QBRTCScreenCapturer .requestPermissions (this );
137
+ } else {
138
+ startScreenSharing (null );
139
+ }
121
140
} else {
122
141
startConversationFragment ();
123
142
}
@@ -128,6 +147,27 @@ protected void onResume() {
128
147
super .onResume ();
129
148
settingsSharedPref = PreferenceManager .getDefaultSharedPreferences (this );
130
149
bindCallService ();
150
+ if (callService != null ) {
151
+ switch (callService .getReconnectionState ()) {
152
+ case COMPLETED :
153
+ reconnectingLayout .setVisibility (View .GONE );
154
+ for (ReconnectionCallback reconnectionCallback : reconnectionCallbacks ) {
155
+ reconnectionCallback .completed ();
156
+ }
157
+ break ;
158
+ case IN_PROGRESS :
159
+ reconnectingLayout .setVisibility (View .VISIBLE );
160
+ for (ReconnectionCallback reconnectionCallback : reconnectionCallbacks ) {
161
+ reconnectionCallback .inProgress ();
162
+ }
163
+ break ;
164
+ case FAILED :
165
+ ToastUtils .shortToast (getApplicationContext (), R .string .reconnection_failed );
166
+ callService .leaveCurrentSession ();
167
+ finish ();
168
+ break ;
169
+ }
170
+ }
131
171
}
132
172
133
173
@ Override
@@ -137,6 +177,8 @@ protected void onPause() {
137
177
unbindService (callServiceConnection );
138
178
callServiceConnection = null ;
139
179
}
180
+
181
+ callService .unsubscribeReconnectionListener (reconnectionListener );
140
182
removeListeners ();
141
183
}
142
184
@@ -161,6 +203,7 @@ private void bindCallService() {
161
203
162
204
private void leaveCurrentSession () {
163
205
callService .leaveCurrentSession ();
206
+ finish ();
164
207
}
165
208
166
209
private void initListeners (ConferenceSession session ) {
@@ -324,23 +367,23 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
324
367
&& resultCode == Activity .RESULT_OK && data != null ) {
325
368
startScreenSharing (data );
326
369
Log .i (TAG , "Starting Screen Capture" );
370
+ } else if (requestCode == QBRTCScreenCapturer .REQUEST_MEDIA_PROJECTION && resultCode == Activity .RESULT_CANCELED ) {
371
+ callService .stopScreenSharing ();
372
+ startConversationFragment ();
327
373
}
328
374
if (requestCode == REQUEST_CODE_OPEN_CONVERSATION_CHAT ) {
329
375
Log .d (TAG , "Returning back from ChatActivity" );
330
376
}
331
377
}
332
378
333
379
private void startScreenSharing (final Intent data ) {
334
- Fragment fragmentByTag = getSupportFragmentManager ().findFragmentByTag (ScreenShareFragment .class .getSimpleName ());
335
- if (!(fragmentByTag instanceof ScreenShareFragment )) {
336
- ScreenShareFragment screenShareFragment = ScreenShareFragment .newInstance ();
337
- getSupportFragmentManager ().beginTransaction ().replace (R .id .fragment_container ,
338
- screenShareFragment , ScreenShareFragment .class .getSimpleName ())
339
- .commitAllowingStateLoss ();
340
-
341
- callService .setVideoEnabled (true );
342
- callService .startScreenSharing (data );
343
- }
380
+ ScreenShareFragment screenShareFragment = ScreenShareFragment .newInstance ();
381
+ getSupportFragmentManager ().beginTransaction ().replace (R .id .fragment_container ,
382
+ screenShareFragment , ScreenShareFragment .class .getSimpleName ())
383
+ .commitAllowingStateLoss ();
384
+
385
+ callService .setVideoEnabled (true );
386
+ callService .startScreenSharing (data );
344
387
}
345
388
346
389
@ Override
@@ -408,9 +451,6 @@ public void onError(WsException exception) {
408
451
@ Override
409
452
public void onSessionClosed (final ConferenceSession session ) {
410
453
Log .d (TAG , "Session " + session .getSessionID () + " start stop session" );
411
- if (session .equals (currentSession )) {
412
- finish ();
413
- }
414
454
}
415
455
416
456
private class CallServiceConnection implements ServiceConnection {
@@ -425,27 +465,66 @@ public void onServiceConnected(ComponentName name, IBinder service) {
425
465
callService = binder .getService ();
426
466
if (callService .currentSessionExist ()) {
427
467
currentDialogID = callService .getDialogID ();
428
- login ();
468
+ if (callService .getReconnectionState () != CallService .ReconnectionState .IN_PROGRESS ){
469
+ initScreen ();
470
+ }
429
471
} else {
430
472
//we have already currentSession == null, so it's no reason to do further initialization
431
473
CallService .stop (CallActivity .this );
432
474
finish ();
433
475
}
476
+ callService .subscribeReconnectionListener (reconnectionListener );
434
477
}
478
+ }
435
479
436
- private void login () {
437
- QBUser qbUser = getSharedPrefsHelper ().getQbUser ();
438
- QBUsers .signIn (qbUser ).performAsync (new QBEntityCallback <QBUser >() {
439
- @ Override
440
- public void onSuccess (QBUser qbUser , Bundle bundle ) {
441
- initScreen ();
442
- }
480
+ private class ReconnectionListenerImpl implements CallService .ReconnectionListener {
481
+ private final String tag ;
443
482
444
- @ Override
445
- public void onError (QBResponseException e ) {
483
+ ReconnectionListenerImpl (String tag ) {
484
+ this .tag = tag ;
485
+ }
486
+
487
+ @ Override
488
+ public void onChangedState (CallService .ReconnectionState reconnectionState ) {
489
+ switch (reconnectionState ) {
490
+ case COMPLETED :
491
+ reconnectingLayout .setVisibility (View .GONE );
492
+ for (ReconnectionCallback reconnectionCallback : reconnectionCallbacks ) {
493
+ reconnectionCallback .completed ();
494
+ }
495
+ initScreen ();
496
+ callService .setReconnectionState (CallService .ReconnectionState .DEFAULT );
497
+ break ;
498
+ case IN_PROGRESS :
499
+ reconnectingLayout .setVisibility (View .VISIBLE );
500
+ for (ReconnectionCallback reconnectionCallback : reconnectionCallbacks ) {
501
+ reconnectionCallback .inProgress ();
502
+ }
503
+ break ;
504
+ case FAILED :
505
+ ToastUtils .shortToast (getApplicationContext (), R .string .reconnection_failed );
506
+ callService .leaveCurrentSession ();
446
507
finish ();
447
- }
448
- });
508
+ break ;
509
+ }
510
+ }
511
+
512
+ @ Override
513
+ public int hashCode () {
514
+ int hash = 3 ;
515
+ hash = 53 * hash + tag .hashCode ();
516
+ return hash ;
517
+ }
518
+
519
+ @ Override
520
+ public boolean equals (Object obj ) {
521
+ boolean equals ;
522
+ if (obj instanceof ReconnectionListenerImpl ) {
523
+ equals = TAG .equals (((ReconnectionListenerImpl ) obj ).tag );
524
+ } else {
525
+ equals = super .equals (obj );
526
+ }
527
+ return equals ;
449
528
}
450
529
}
451
530
}
0 commit comments