Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.wmakit.samataxi.activities;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import com.sinch.android.rtc.ClientRegistration;
- import com.sinch.android.rtc.PushPair;
- import com.sinch.android.rtc.SinchClient;
- import com.sinch.android.rtc.SinchClientListener;
- import com.sinch.android.rtc.SinchError;
- import com.sinch.android.rtc.calling.Call;
- import com.sinch.android.rtc.calling.CallListener;
- import com.wmakit.samataxi.config.Constant;
- import com.wmakit.samataxi.volleynetwork.ControllerRequest;
- import java.util.HashMap;
- import java.util.List;
- import static com.sinch.android.rtc.calling.CallState.ENDED;
- import static com.sinch.android.rtc.calling.CallState.ESTABLISHED;
- import static com.sinch.android.rtc.calling.CallState.INITIATING;
- import static com.sinch.android.rtc.calling.CallState.PROGRESSING;
- public class OutgoingCallActivity extends AbstractCallActivity implements CallListener, SinchClientListener {
- private Call outgoingCall;
- BroadcastReceiver myBroadCastReceiver;
- private boolean manualyFinishCall = false;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- registerMyReceiver();
- answerCall.setVisibility(View.GONE);
- hangupCall.setVisibility(View.VISIBLE);
- speakerBtn.setVisibility(View.VISIBLE);
- hangupCall.setOnClickListener(v -> {
- Log.d("__sinch__", "hanup click " );
- manualyFinishCall = true;
- finishCall();
- });
- sinchClient.start();
- sinchClient.addSinchClientListener(this);
- }
- protected void hideLoader(){
- super.hideLoader();
- answerCall.setVisibility(View.GONE);
- }
- private void registerMyReceiver() {
- myBroadCastReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- if(intent.getAction() != null && intent.getAction().equalsIgnoreCase(Constant.ACTION_NOTIFY_PARTENER_LISTEN_STARTED)){
- if(sinchClient.isStarted()){
- if(!callAlreadyStarted && !alreadyNotifiyed){
- alreadyNotifiyed = true;
- callAlreadyStarted = true;
- startCall();
- }
- }
- else{
- alreadyNotifiyed = true;
- }
- }
- }
- };
- IntentFilter intentFilter = new IntentFilter();
- intentFilter.addAction(Constant.ACTION_NOTIFY_PARTENER_LISTEN_STARTED);
- registerReceiver(myBroadCastReceiver, intentFilter);
- }
- void finishIfNoConnection(){
- if(outgoingCall == null || (outgoingCall.getState() != ESTABLISHED && outgoingCall.getState() != PROGRESSING)){
- String state = "";
- if(outgoingCall != null){
- state = outgoingCall.getState() + "";
- }
- Log.d("__sinch__", "finish " + state);
- finishCall();
- }
- }
- @Override
- public void onCallProgressing(Call call) {
- //playRington(R.raw.ringing_wait);
- progressing = true;
- hideLoader();
- }
- @Override
- public void onCallEstablished(Call call) {
- callStarted();
- }
- @Override
- public void onCallEnded(Call call) {
- Log.d("__sinch__", "call ended ");
- finishCall();
- }
- @Override
- public void onShouldSendPushNotification(Call call, List<PushPair> list) {
- }
- private void finishCall() {
- runOnUiThread(() -> {
- if(!finishing){
- finishing = true;
- if(preferencesManager.isDriver()){
- preferencesManager.setVOIPFailled(true);
- }
- ControllerRequest.getInstance().stopVoice();
- notifyPartenerCallHangupStarted();
- //Toast.makeText(ControllerRequest.getInstance().getApplicationContext(), R.string.end_of_call, Toast.LENGTH_SHORT).show();
- //setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
- if (outgoingCall != null) {
- Log.d("__about 1", "here "+ outgoingCall.getState());
- if(outgoingCall.getState() == INITIATING){
- if(!manualyFinishCall){
- preferencesManager.setVOIPFailled(true);
- }
- Log.d("__about 3", "here");
- //Toast.makeText(ControllerRequest.getInstance().getApplicationContext(), R.string.cannot_reach_ur_correspodant, Toast.LENGTH_LONG).show();
- }
- outgoingCall.hangup();
- }
- else{
- Log.d("__about 2", "here");
- if(!manualyFinishCall){
- preferencesManager.setVOIPFailled(true);
- }
- }
- //stopRington();
- try {
- sinchClient.terminate();
- } catch (Exception e) {
- e.printStackTrace();
- }
- finish();
- }
- });
- }
- private void callStarted() {
- //stopRington();
- ControllerRequest.getInstance().stopVoice();
- //setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
- startCountDow();
- if (speakerDefaultStateEnabled) {
- sinchClient.getAudioController().enableSpeaker();
- speakerEnabled = true;
- //speakerBtn.setImageDrawable(getResources().getDrawable(R.drawable.speaker_disabled));
- speakerBtn.setAlpha(SPEAKER_DISABLED_OPACITY);
- }
- }
- private void startCall(){
- Intent intent = getIntent();
- HashMap<String, String> params = new HashMap<>();
- params.put("receiverId", intent.getStringExtra("id"));
- params.put("callerId", intent.getStringExtra("callerId"));
- params.put("receiverUserName", intent.getStringExtra("userName"));
- params.put("callerUserName", intent.getStringExtra("callerUserName"));
- params.put("receiverImage", intent.getStringExtra("image"));
- params.put("callerImage", intent.getStringExtra("callerImage"));
- params.put("receiverPhone", intent.getStringExtra("phone"));
- params.put("callerPhone", intent.getStringExtra("callerPhone"));
- outgoingCall = sinchClient.getCallClient().callUser(intent.getStringExtra("id"), params);
- outgoingCall.addCallListener(this);
- }
- @Override
- public void onClientStarted(SinchClient sinchClient) {
- if(!callAlreadyStarted && alreadyNotifiyed) {
- callAlreadyStarted = true;
- startCall();
- }
- }
- @Override
- public void onClientStopped(SinchClient sinchClient) {
- }
- @Override
- public void onClientFailed(SinchClient sinchClient, SinchError sinchError) {
- }
- @Override
- public void onRegistrationCredentialsRequired(SinchClient sinchClient, ClientRegistration clientRegistration) {
- }
- @Override
- public void onLogMessage(int i, String s, String s1) {
- }
- void connectionEndCheck() {
- if (outgoingCall != null && (outgoingCall.getState() == ENDED)) {
- Log.d("__sinch__", "connection interval failled " + outgoingCall);
- finishCall();
- timer.cancel();
- }
- }
- @Override
- protected void onStop() {
- super.onStop();
- try {
- unregisterReceiver(myBroadCastReceiver);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement