From 959afda2cf40a2d1f0743fa717d9463dd4a12339 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 26 Feb 2019 17:22:36 +0100 Subject: [PATCH] Can hang up conversation --- .../client/data/helpers/CallsHelper.java | 18 +++++++ .../client/ui/activities/CallActivity.java | 47 ++++++++++++++++++- .../client/ui/asynctasks/HangUpCallTask.java | 23 +++++++++ .../client/ui/models/CallPeerConnection.java | 18 +++++++ .../SignalExchangerClient.java | 8 ++++ app/src/main/res/layout/activity_call.xml | 15 ++++++ app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/HangUpCallTask.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/CallsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/CallsHelper.java index 910f053..feea6a5 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/CallsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/CallsHelper.java @@ -239,6 +239,24 @@ public class CallsHelper extends BaseHelper { } } + /** + * Hang up a call + * + * @param callID Target call ID + * @return TRUE for a success / FALSE else + */ + public boolean hangUp(int callID){ + APIRequest request = new APIRequest(getContext(), "calls/hangUp"); + request.addInt("call_id", callID); + try { + return request.exec().getJSONObject().has("success"); + + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + /** * Turn a {@link JSONObject} object into a {@link CallsConfiguration} object. diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CallActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CallActivity.java index 7a19ca3..263bc07 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CallActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CallActivity.java @@ -8,6 +8,8 @@ import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.util.Log; +import android.view.View; +import android.widget.ImageButton; import android.widget.ProgressBar; import android.widget.Toast; @@ -23,6 +25,7 @@ import org.communiquons.android.comunic.client.data.models.CallsConfiguration; import org.communiquons.android.comunic.client.data.utils.AccountUtils; import org.communiquons.android.comunic.client.ui.arrays.CallPeersConnectionsList; import org.communiquons.android.comunic.client.ui.asynctasks.GetCallInformationTask; +import org.communiquons.android.comunic.client.ui.asynctasks.HangUpCallTask; import org.communiquons.android.comunic.client.ui.asynctasks.RespondToCallTask; import org.communiquons.android.comunic.client.ui.models.CallPeerConnection; import org.communiquons.android.comunic.client.ui.receivers.PendingCallsBroadcastReceiver; @@ -85,6 +88,11 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac */ private SignalExchangerClient mSignalExchangerClient = null; + /** + * Specify whether call was stopped or not + */ + private boolean mStopped = false; + /** * Connections list @@ -102,6 +110,7 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac * Views */ private ProgressBar mProgressBar; + private ImageButton mHangUpButton; @Override @@ -184,7 +193,8 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac private void initViews(){ mProgressBar = findViewById(R.id.progressBar); - + mHangUpButton = findViewById(R.id.hangUp); + mHangUpButton.setOnClickListener(v -> hangUp()); } @@ -214,6 +224,8 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac */ private void onGotCallInformation(@Nullable CallInformation info){ + if(mStopped) return; + if(info == null){ Toast.makeText(this, R.string.err_get_call_info, Toast.LENGTH_SHORT).show(); return; @@ -225,7 +237,7 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac //Check if everyone left the conversation if(mCallInformation.hasAllMembersLeftCallExcept(AccountUtils.getID(this))){ Toast.makeText(this, R.string.notice_call_terminated, Toast.LENGTH_SHORT).show(); - finish(); + hangUp(); return; } @@ -258,6 +270,9 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac private void processClientsConnections(){ + + if(mStopped) return; + //Process each peer connection for(CallMember member : mCallInformation.getMembers()) processClientConnection(member); @@ -294,6 +309,8 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac */ private void createPeerConnection(CallMember member, boolean isInitiator){ + if(mStopped) return; + Log.v(TAG, "Create peer connection for connection with user " + member.getUserID()); CallPeerConnection callPeer = new CallPeerConnection(member); @@ -359,10 +376,12 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac ProxyVideoSink localProxyVideoSink = new ProxyVideoSink(); localProxyVideoSink.setTarget(callPeer.getLocalVideoView()); + callPeer.setLocalProxyVideoSink(localProxyVideoSink); ProxyVideoSink remoteProxyRenderer = new ProxyVideoSink(); remoteProxyRenderer.setTarget(callPeer.getRemoteViewView()); callPeer.getRemoteSinks().add(remoteProxyRenderer); + callPeer.setRemoteProxyRenderer(remoteProxyRenderer); //Start connection peerConnectionClient.createPeerConnection( @@ -376,6 +395,27 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac peerConnectionClient.createOffer(); } + /** + * Hang up call + */ + private void hangUp(){ + + mHangUpButton.setVisibility(View.GONE); + mStopped = true; + + mRefreshCallInformation.interrupt(); + + mSignalExchangerClient.close(); + + for (CallPeerConnection client : mList) + disconnectFromPeer(client.getMember()); + + HangUpCallTask hangUpCallTask = new HangUpCallTask(getApplicationContext()); + hangUpCallTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mCallID); + + finish(); + } + /** * Disconnect from a specific peer * @@ -387,6 +427,9 @@ public class CallActivity extends BaseActivity implements SignalExchangerCallbac if(callPeer == null) return; + ((ProxyVideoSink)callPeer.getLocalProxyVideoSink()).setTarget(null); + ((ProxyVideoSink)callPeer.getRemoteProxyRenderer()).setTarget(null); + callPeer.getPeerConnectionClient().close(); mList.remove(callPeer); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/HangUpCallTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/HangUpCallTask.java new file mode 100644 index 0000000..f5cf8fe --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/HangUpCallTask.java @@ -0,0 +1,23 @@ +package org.communiquons.android.comunic.client.ui.asynctasks; + +import android.content.Context; + +import org.communiquons.android.comunic.client.data.helpers.CallsHelper; + +/** + * Hang up a call + * + * @author Pierre HUBERT + */ +public class HangUpCallTask extends SafeAsyncTask { + + public HangUpCallTask(Context context) { + super(context); + } + + @Override + protected Boolean doInBackground(Integer... integers) { + return new CallsHelper(getContext()).hangUp(integers[0]); + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/models/CallPeerConnection.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/models/CallPeerConnection.java index 41dd794..53f383b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/models/CallPeerConnection.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/models/CallPeerConnection.java @@ -17,6 +17,8 @@ public class CallPeerConnection { //Private fields private CallMember member; private PeerConnectionClient peerConnectionClient; + private VideoSink localProxyVideoSink; + private VideoSink remoteProxyRenderer; private ArrayList remoteSinks = new ArrayList<>(); //Views @@ -67,4 +69,20 @@ public class CallPeerConnection { public void setLocalVideoView(SurfaceViewRenderer mLocalVideoView) { this.mLocalVideoView = mLocalVideoView; } + + public VideoSink getLocalProxyVideoSink() { + return localProxyVideoSink; + } + + public void setLocalProxyVideoSink(VideoSink localProxyVideoSink) { + this.localProxyVideoSink = localProxyVideoSink; + } + + public VideoSink getRemoteProxyRenderer() { + return remoteProxyRenderer; + } + + public void setRemoteProxyRenderer(VideoSink remoteProxyRenderer) { + this.remoteProxyRenderer = remoteProxyRenderer; + } } diff --git a/app/src/main/java/org/communiquons/signalexchangerclient/SignalExchangerClient.java b/app/src/main/java/org/communiquons/signalexchangerclient/SignalExchangerClient.java index a435284..1ca3ddc 100644 --- a/app/src/main/java/org/communiquons/signalexchangerclient/SignalExchangerClient.java +++ b/app/src/main/java/org/communiquons/signalexchangerclient/SignalExchangerClient.java @@ -71,6 +71,14 @@ public class SignalExchangerClient extends WebSocketListener { mWebSocket = mClient.newWebSocket(request, this); } + /** + * Close this socket + */ + public void close(){ + if(mWebSocket != null) + mWebSocket.close(4999, null); + } + /** * Get current client configuration * diff --git a/app/src/main/res/layout/activity_call.xml b/app/src/main/res/layout/activity_call.xml index c8271f9..178cd6d 100644 --- a/app/src/main/res/layout/activity_call.xml +++ b/app/src/main/res/layout/activity_call.xml @@ -20,4 +20,19 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> + + \ No newline at end of file diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index c90c8b5..911a4cd 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -332,4 +332,5 @@ Impossible de récupérer les infoamtions sur l\'appel ! Impossible de connecter au signaling server ! Appel terminé. + Raccrocher \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f62966f..7b474de 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -331,4 +331,5 @@ Could not get call information! Could not connect to signaling server! Call terminated + Hang up