Advertisement
Guest User

OutgoingCallActivity

a guest
Jan 22nd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.98 KB | None | 0 0
  1. package com.wmakit.samataxi.activities;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10.  
  11. import com.sinch.android.rtc.ClientRegistration;
  12. import com.sinch.android.rtc.PushPair;
  13. import com.sinch.android.rtc.SinchClient;
  14. import com.sinch.android.rtc.SinchClientListener;
  15. import com.sinch.android.rtc.SinchError;
  16. import com.sinch.android.rtc.calling.Call;
  17. import com.sinch.android.rtc.calling.CallListener;
  18. import com.wmakit.samataxi.config.Constant;
  19. import com.wmakit.samataxi.volleynetwork.ControllerRequest;
  20.  
  21. import java.util.HashMap;
  22. import java.util.List;
  23.  
  24. import static com.sinch.android.rtc.calling.CallState.ENDED;
  25. import static com.sinch.android.rtc.calling.CallState.ESTABLISHED;
  26. import static com.sinch.android.rtc.calling.CallState.INITIATING;
  27. import static com.sinch.android.rtc.calling.CallState.PROGRESSING;
  28.  
  29. public class OutgoingCallActivity extends AbstractCallActivity implements CallListener, SinchClientListener {
  30.  
  31.     private Call outgoingCall;
  32.     BroadcastReceiver myBroadCastReceiver;
  33.     private boolean manualyFinishCall = false;
  34.  
  35.  
  36.     @Override
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.  
  40.         registerMyReceiver();
  41.  
  42.         answerCall.setVisibility(View.GONE);
  43.         hangupCall.setVisibility(View.VISIBLE);
  44.         speakerBtn.setVisibility(View.VISIBLE);
  45.  
  46.         hangupCall.setOnClickListener(v -> {
  47.             Log.d("__sinch__", "hanup click " );
  48.             manualyFinishCall = true;
  49.             finishCall();
  50.         });
  51.  
  52.         sinchClient.start();
  53.         sinchClient.addSinchClientListener(this);
  54.     }
  55.  
  56.     protected void hideLoader(){
  57.         super.hideLoader();
  58.  
  59.         answerCall.setVisibility(View.GONE);
  60.     }
  61.  
  62.     private void registerMyReceiver() {
  63.         myBroadCastReceiver = new BroadcastReceiver() {
  64.             @Override
  65.             public void onReceive(Context context, Intent intent) {
  66.  
  67.                 if(intent.getAction() != null && intent.getAction().equalsIgnoreCase(Constant.ACTION_NOTIFY_PARTENER_LISTEN_STARTED)){
  68.                     if(sinchClient.isStarted()){
  69.                         if(!callAlreadyStarted && !alreadyNotifiyed){
  70.                             alreadyNotifiyed = true;
  71.                             callAlreadyStarted = true;
  72.                             startCall();
  73.                         }
  74.                     }
  75.                     else{
  76.                         alreadyNotifiyed = true;
  77.                     }
  78.                 }
  79.             }
  80.         };
  81.  
  82.         IntentFilter intentFilter = new IntentFilter();
  83.         intentFilter.addAction(Constant.ACTION_NOTIFY_PARTENER_LISTEN_STARTED);
  84.         registerReceiver(myBroadCastReceiver, intentFilter);
  85.     }
  86.  
  87.     void finishIfNoConnection(){
  88.         if(outgoingCall == null || (outgoingCall.getState() != ESTABLISHED && outgoingCall.getState() != PROGRESSING)){
  89.  
  90.             String state = "";
  91.             if(outgoingCall != null){
  92.                 state = outgoingCall.getState() + "";
  93.             }
  94.             Log.d("__sinch__", "finish " + state);
  95.             finishCall();
  96.         }
  97.     }
  98.  
  99.     @Override
  100.     public void onCallProgressing(Call call) {
  101.         //playRington(R.raw.ringing_wait);
  102.         progressing = true;
  103.         hideLoader();
  104.     }
  105.  
  106.     @Override
  107.     public void onCallEstablished(Call call) {
  108.         callStarted();
  109.     }
  110.  
  111.     @Override
  112.     public void onCallEnded(Call call) {
  113.         Log.d("__sinch__", "call ended ");
  114.         finishCall();
  115.     }
  116.  
  117.     @Override
  118.     public void onShouldSendPushNotification(Call call, List<PushPair> list) {
  119.  
  120.     }
  121.  
  122.     private void finishCall() {
  123.  
  124.         runOnUiThread(() -> {
  125.             if(!finishing){
  126.                 finishing = true;
  127.  
  128.                     if(preferencesManager.isDriver()){
  129.                         preferencesManager.setVOIPFailled(true);
  130.                     }
  131.  
  132.  
  133.                     ControllerRequest.getInstance().stopVoice();
  134.  
  135.                 notifyPartenerCallHangupStarted();
  136.  
  137.                 //Toast.makeText(ControllerRequest.getInstance().getApplicationContext(), R.string.end_of_call, Toast.LENGTH_SHORT).show();
  138.  
  139.                 //setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
  140.  
  141.  
  142.                 if (outgoingCall != null) {
  143.  
  144.                     Log.d("__about 1", "here "+ outgoingCall.getState());
  145.  
  146.                     if(outgoingCall.getState() == INITIATING){
  147.  
  148.                         if(!manualyFinishCall){
  149.                             preferencesManager.setVOIPFailled(true);
  150.                         }
  151.  
  152.                         Log.d("__about 3", "here");
  153.                         //Toast.makeText(ControllerRequest.getInstance().getApplicationContext(), R.string.cannot_reach_ur_correspodant, Toast.LENGTH_LONG).show();
  154.                     }
  155.  
  156.                     outgoingCall.hangup();
  157.                 }
  158.                 else{
  159.                     Log.d("__about 2", "here");
  160.                     if(!manualyFinishCall){
  161.                         preferencesManager.setVOIPFailled(true);
  162.                     }
  163.                 }
  164.  
  165.                 //stopRington();
  166.  
  167.                 try {
  168.                     sinchClient.terminate();
  169.                 } catch (Exception e) {
  170.                     e.printStackTrace();
  171.                 }
  172.  
  173.  
  174.                 finish();
  175.             }
  176.         });
  177.  
  178.     }
  179.  
  180.     private void callStarted() {
  181.         //stopRington();
  182.  
  183.         ControllerRequest.getInstance().stopVoice();
  184.         //setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
  185.         startCountDow();
  186.  
  187.         if (speakerDefaultStateEnabled) {
  188.             sinchClient.getAudioController().enableSpeaker();
  189.             speakerEnabled = true;
  190.             //speakerBtn.setImageDrawable(getResources().getDrawable(R.drawable.speaker_disabled));
  191.             speakerBtn.setAlpha(SPEAKER_DISABLED_OPACITY);
  192.         }
  193.     }
  194.  
  195.     private void startCall(){
  196.         Intent intent = getIntent();
  197.         HashMap<String, String> params = new HashMap<>();
  198.  
  199.         params.put("receiverId", intent.getStringExtra("id"));
  200.         params.put("callerId", intent.getStringExtra("callerId"));
  201.  
  202.         params.put("receiverUserName", intent.getStringExtra("userName"));
  203.         params.put("callerUserName", intent.getStringExtra("callerUserName"));
  204.  
  205.         params.put("receiverImage", intent.getStringExtra("image"));
  206.         params.put("callerImage", intent.getStringExtra("callerImage"));
  207.  
  208.         params.put("receiverPhone", intent.getStringExtra("phone"));
  209.         params.put("callerPhone", intent.getStringExtra("callerPhone"));
  210.  
  211.         outgoingCall = sinchClient.getCallClient().callUser(intent.getStringExtra("id"), params);
  212.         outgoingCall.addCallListener(this);
  213.     }
  214.  
  215.  
  216.  
  217.  
  218.     @Override
  219.     public void onClientStarted(SinchClient sinchClient) {
  220.         if(!callAlreadyStarted && alreadyNotifiyed) {
  221.             callAlreadyStarted = true;
  222.             startCall();
  223.         }
  224.     }
  225.  
  226.     @Override
  227.     public void onClientStopped(SinchClient sinchClient) {
  228.  
  229.     }
  230.  
  231.     @Override
  232.     public void onClientFailed(SinchClient sinchClient, SinchError sinchError) {
  233.  
  234.     }
  235.  
  236.     @Override
  237.     public void onRegistrationCredentialsRequired(SinchClient sinchClient, ClientRegistration clientRegistration) {
  238.  
  239.     }
  240.  
  241.     @Override
  242.     public void onLogMessage(int i, String s, String s1) {
  243.  
  244.     }
  245.  
  246.     void connectionEndCheck() {
  247.         if (outgoingCall != null && (outgoingCall.getState() == ENDED)) {
  248.             Log.d("__sinch__", "connection interval failled " + outgoingCall);
  249.             finishCall();
  250.             timer.cancel();
  251.         }
  252.     }
  253.  
  254.     @Override
  255.     protected void onStop() {
  256.         super.onStop();
  257.         try {
  258.             unregisterReceiver(myBroadCastReceiver);
  259.         } catch (Exception e) {
  260.             e.printStackTrace();
  261.         }
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement