Skip to content

Observe call events

Federico Marin edited this page May 16, 2022 · 13 revisions

Observe call events

To observe the call UI state it is necessary to add a call ui observer to the call module.

CallUIObserver callUIObserver = new CallUIObserver() {

    @Override
    public void onActivityStarted(@NonNull Call call,
                                  @NonNull WeakReference<AppCompatActivity> weakReference) {
         Log.d("Bandyer SDK", "onCallActivityStarted: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees()));
    }

    @Override
    public void onActivityDestroyed(@NonNull Call call,
                                    @NonNull WeakReference<AppCompatActivity> weakReference) {
         Log.d("Bandyer SDK", "onCallActivityDestroyed: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees()));
    }

    @Override
    public void onActivityError(@NonNull Call call,
                                @NonNull WeakReference<AppCompatActivity> weakReference,
                                @NonNull CallException e) {
         Log.d("Bandyer SDK", "onCallActivityError: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees())
               + "\n"
               + "exception: " + e.getMessage());
    }
};

CallModule callModule = BandyerSDK.getInstance().getCallModule();
if (callModule == null) return;

callModule.addCallUIObserver(callUIObserver);
// or callModule.addCallUIObserver(fragmentActivity, callObserver);
// to automatically dispose the observer on activity destroy.  

To observe a call that has started, has ended or has ended with an error is it necessary to add a call observer to the call module.

CallObserver callObserver = new CallObserver() {

    @Override
    public void onCallCreated(@NonNull Call call) {
         Log.d("Bandyer SDK", "onCallCreated: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees()));
    }
    
    @Override
    public void onCallStarted(@NonNull Call call) {
         Log.d("Bandyer SDK", "onCallStarted: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees()));
    }

    @Override
    public void onCallEnded(@NonNull Call call) {
         Log.d("Bandyer SDK", "onCallEnded: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees()));
    }

    @Override
    public void onCallEndedWithError(@NonNull Call call,
                                     @NonNull CallException e) {
         Log.d("Bandyer SDK", "onCallEndedWithError: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees())
               + "\n"
               + "exception: " + e.getMessage());
    }
};

CallModule callModule = BandyerSDK.getInstance().getCallModule();
if (callModule == null) return;

callModule.addCallObserver(callObserver);
// or callModule.addCallObserver(fragmentActivity, callObserver);
// to automatically dispose the observer on activity destroy.   

Observe call recording

Whenever a call is recorded, the call recording type can be either manual (recording is started by admin callee) or automatic (recording starts when the call is connected).

CallRecordingType can be retrieved from call info as show below.

// Retrieves CallRecordingType.AUTOMATIC or CallRecordingType.MANUAL 
// or CallRecordingType.NONE if the call will not be recorded

CallRecordingType recordingType = ongoingCall.getCallInfo().getRecordingType(); 

Call recording can be observed via CallRecordingObserver as shown below:

CallRecordingObserver callRecordingObserver = new CallRecordingObserver() {
	@Override
	public void onCallRecordingStateChanged(@NonNull Call call, @NonNull CallRecordingState callRecordingState) {
		Log.e(TAG, "Call recording state changed: " + callRecordingState);
		showCallRecordingSnackbar(callRecordingState);
	}

	@Override
	public void onCallRecordingFailed(@NonNull Call call, @NonNull String reason) {
		Log.e(TAG, "Call recording failed with error: " + resaon);
	}
	
	private void showCallRecordingSnackbar(CallRecordingState callRecordingState) {
		KaleyraRecordingSnackbar.Type type = callRecordingState == CallRecordingState.STARTED 
			? KaleyraRecordingSnackbar.Type.TYPE_STARTED : KaleyraRecordingSnackbar.Type.TYPE_ENDED;
		KaleyraRecordingSnackbar.make(findViewById(R.id.main_view), type , Snackbar.LENGTH_LONG).show();
	}
};

ongoingCall.addCallRecordingObserver(callRecordingObserver);

CallRecordingState can be also retrieved at any time as shown below:

// Retrieves CallRecordingState.STOPPED or CallRecordingState.STARTED

CallRecordingState callRecordingState = ongoingCall.getCallRecordingState();

Observe Call Events via Broadcast Receiver

If you need to decouple the logic of observing a call it is also provided a way to observe call events through a broadcast receiver as shown below:

Register the call event broadcast receiver in AndroidManifest.xml as shown below:

<!-- Bandyer call event broadcast receiver -->

<application>

    <receiver
        android:name=".MyCallEventBroadcastReceiver"
        android:exported="false">
            <intent-filter>
                <action android:name="com.bandyer.android_sdk.CALL_EVENT_ACTION" />
            </intent-filter>
    </receiver>

</application>

Add the call event broadcast receiver class to your project:

public class MyCallEventBroadcastReceiver extends CallEventBroadcastReceiver {

    @Override
    public void onCallCreated(@NonNull Call call) {
         Log.d("Bandyer SDK", "onCallCreated: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees()));
    }

    @Override
    public void onCallStarted(@NonNull Call call) {
         Log.d("Bandyer SDK", "onCallStarted: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees()));
    }

    @Override
    public void onCallEnded(@NonNull Call call) {
         Log.d("Bandyer SDK", "onCallEnded: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees()));
    }

    @Override
    public void onCallEndedWithError(@NonNull Call call,
                                     @NonNull CallException e) {
         Log.d("Bandyer SDK", "onCallEndedWithError: "
               + call.getCallInfo().getCaller() + ", "
               + TextUtils.join(", ", call.getCallInfo().getCallees())
               + "\n"
               + "exception: " + e.getMessage());
    }
}
Clone this wiki locally