2323import com .sendbird .calls .SendBirdCall ;
2424import com .sendbird .calls .handler .DirectCallListener ;
2525import com .sendbird .calls .quickstart .R ;
26- import com .sendbird .calls .quickstart .utils .ActivityUtils ;
2726import com .sendbird .calls .quickstart .utils .AuthenticationUtils ;
27+ import com .sendbird .calls .quickstart .utils .BroadcastUtils ;
2828import com .sendbird .calls .quickstart .utils .UserInfoUtils ;
2929
3030import java .util .ArrayList ;
@@ -52,6 +52,7 @@ enum STATE {
5252
5353 Context mContext ;
5454 private String mIncomingCallId ;
55+ private boolean mIsEnd ;
5556 private Timer mEndingTimer ;
5657
5758 STATE mState ;
@@ -103,23 +104,45 @@ protected void onCreate(Bundle savedInstanceState) {
103104 initViews ();
104105 setViews ();
105106
106- mIncomingCallId = getIntent ().getStringExtra (ActivityUtils .EXTRA_INCOMING_CALL_ID );
107+ init (getIntent ());
108+ }
109+
110+ private void init (Intent intent ) {
111+ mIncomingCallId = intent .getStringExtra (CallService .EXTRA_CALL_ID );
112+ mIsEnd = intent .getBooleanExtra (CallService .EXTRA_IS_END , false );
113+
114+ if (mIsEnd ) {
115+ CallService .stopService (mContext );
116+ }
117+
107118 if (mIncomingCallId != null ) { // as callee
108119 mDirectCall = SendBirdCall .getCall (mIncomingCallId );
109120 mCalleeId = mDirectCall .getCallee ().getUserId ();
110121 mIsVideoCall = mDirectCall .isVideoCall ();
111122
112123 setListener (mDirectCall );
113124 } else { // as caller
114- mCalleeId = getIntent () .getStringExtra (ActivityUtils .EXTRA_CALLEE_ID );
115- mIsVideoCall = getIntent () .getBooleanExtra (ActivityUtils .EXTRA_IS_VIDEO_CALL , false );
125+ mCalleeId = intent .getStringExtra (CallService .EXTRA_CALLEE_ID );
126+ mIsVideoCall = intent .getBooleanExtra (CallService .EXTRA_IS_VIDEO_CALL , false );
116127 }
117128
118129 if (setInitialState ()) {
119130 checkAuthenticate ();
120131 }
121132 }
122133
134+ @ Override
135+ protected void onNewIntent (Intent intent ) {
136+ super .onNewIntent (intent );
137+ Log .d (TAG , "onNewIntent()" );
138+
139+ mIsEnd = intent .getBooleanExtra (CallService .EXTRA_IS_END , false );
140+ if (mIsEnd ) {
141+ CallService .stopService (mContext );
142+ end (mDirectCall );
143+ }
144+ }
145+
123146 protected void initViews () {
124147 mLinearLayoutInfo = findViewById (R .id .linear_layout_info );
125148 mImageViewProfile = findViewById (R .id .image_view_profile );
@@ -141,9 +164,7 @@ protected void initViews() {
141164
142165 protected void setViews () {
143166 mImageViewDecline .setOnClickListener (view -> {
144- if (mDirectCall != null ) {
145- end (mDirectCall );
146- }
167+ end (mDirectCall );
147168 });
148169
149170 mImageViewAccept .setOnClickListener (view -> {
@@ -152,7 +173,7 @@ protected void setViews() {
152173 return ;
153174 }
154175
155- if (mState == STATE .STATE_ENDING ) {
176+ if (mState == STATE .STATE_ENDING || mState == STATE . STATE_ENDED ) {
156177 Log .d (TAG , "mImageViewAccept clicked => Already ending call." );
157178 return ;
158179 }
@@ -163,7 +184,6 @@ protected void setViews() {
163184 }
164185
165186 setState (STATE .STATE_ACCEPTING , mDirectCall );
166- startCall (true );
167187 });
168188
169189 if (mIsAudioEnabled ) {
@@ -188,9 +208,7 @@ protected void setViews() {
188208 });
189209
190210 mImageViewEnd .setOnClickListener (view -> {
191- if (mDirectCall != null ) {
192- end (mDirectCall );
193- }
211+ end (mDirectCall );
194212 });
195213 }
196214
@@ -208,6 +226,8 @@ public void onConnected(DirectCall call) {
208226 public void onEnded (DirectCall call ) {
209227 Log .d (TAG , "onEnded()" );
210228 setState (STATE .STATE_ENDED , call );
229+
230+ BroadcastUtils .sendCallLogBroadcast (mContext , call .getCallLog ());
211231 }
212232
213233 @ Override
@@ -229,18 +249,6 @@ public void onAudioDeviceChanged(DirectCall call, AudioDevice currentAudioDevice
229249 });
230250 }
231251
232- @ Override
233- protected void onNewIntent (Intent intent ) {
234- super .onNewIntent (intent );
235- Log .d (TAG , "onNewIntent()" );
236-
237- String incomingCallId = intent .getStringExtra (ActivityUtils .EXTRA_INCOMING_CALL_ID );
238- if (incomingCallId != null ) {
239- DirectCall call = SendBirdCall .getCall (incomingCallId );
240- call .end ();
241- }
242- }
243-
244252 @ TargetApi (19 )
245253 private static int getSystemUiVisibility () {
246254 int flags = View .SYSTEM_UI_FLAG_HIDE_NAVIGATION ;
@@ -260,6 +268,8 @@ private boolean setInitialState() {
260268 return false ;
261269 }
262270
271+ CallService .startService (mContext , mDirectCall , false );
272+
263273 setState (STATE .STATE_INCOMING , mDirectCall );
264274 } else {
265275 setState (STATE .STATE_OUTGOING , mDirectCall );
@@ -318,8 +328,15 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
318328 }
319329
320330 private void ready () {
331+ if (mIsEnd ) {
332+ end (mDirectCall );
333+ return ;
334+ }
335+
321336 if (mState == STATE .STATE_OUTGOING ) {
322337 startCall (false );
338+ } else if (mState == STATE .STATE_INCOMING ) {
339+ setState (STATE .STATE_ACCEPTING , mDirectCall );
323340 }
324341 }
325342
@@ -348,6 +365,7 @@ protected boolean setState(STATE state, DirectCall call) {
348365 }
349366
350367 case STATE_ACCEPTING : {
368+ startCall (true );
351369 setInfo (call , getString (R .string .calls_connecting_call ));
352370 break ;
353371 }
@@ -406,7 +424,7 @@ protected void setInfo(DirectCall call, String status) {
406424 DirectCallUser remoteUser = (call != null ? call .getRemoteUser () : null );
407425 if (remoteUser != null ) {
408426 UserInfoUtils .setProfileImage (mContext , remoteUser , mImageViewProfile );
409- UserInfoUtils .setUserId (remoteUser , mTextViewUserId );
427+ UserInfoUtils .setNicknameOrUserId (remoteUser , mTextViewUserId );
410428 } else {
411429 mTextViewUserId .setText (mCalleeId );
412430 }
@@ -476,7 +494,7 @@ protected void end(DirectCall call) {
476494 if (call != null ) {
477495 Log .d (TAG , "end(callId: " + call .getCallId () + ")" );
478496
479- if (mState == STATE .STATE_ENDING ) {
497+ if (mState == STATE .STATE_ENDING || mState == STATE . STATE_ENDED ) {
480498 Log .d (TAG , "Already ending call." );
481499 return ;
482500 }
@@ -497,6 +515,7 @@ public void run() {
497515 runOnUiThread (() -> {
498516 Log .d (TAG , "finish()" );
499517 finish ();
518+ CallService .stopService (mContext );
500519 });
501520 }
502521 }, ENDING_TIME_MS );
@@ -506,6 +525,7 @@ public void run() {
506525 @ Override
507526 protected void onDestroy () {
508527 super .onDestroy ();
528+ Log .d (TAG , "onDestroy()" );
509529 sIsRunning = false ;
510530 }
511531}
0 commit comments