...
SIP connection is established/closed when Connect/Disconnect button is clicked.
Call is placed/terminated when Call/Hangup button is clicked, and put on hold/retrieve when Hold/Unhold button is clicked.
Work with code of the example
To analyze the code, let's take class class PhoneMinActivity.java of the phone-min example version with hash 4ed4c6d77, which can be downloaded with corresponding build build 1.0.1.338.
1. Initialization of the API. line 71
...
...
Flashphoner.init(
...
)
...
For initialization, object Сontext is passed to the init() method.
Code Block | ||||
---|---|---|---|---|
| ||||
Flashphoner.init(this); |
2. Connection to server.Session for connection to server is created when Connect button is clicked. line 223Session creation.
Flashphoner.createSession() code
Object SessionOptions with URL of WCS server is passed to the method.
Code Block | ||||
---|---|---|---|---|
| ||||
SessionOptions sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
session = Flashphoner.createSession(sessionOptions); |
3. Connection to the server.
Session is created with method createSession(), to which object SessionOptions (line 222) with URL of WCS server is passed.
Callback functions for session events are added (line 224)
- onConnected() - will be called when connection is successfully established
- onRegistered() - will be called when SIP registration is successfully established
- onDisconnection() - will be called when connection is closed
...
language | js |
---|---|
theme | RDark |
...
.connect(). code
Connection object with parameters required for establishing SIP connection is passed to the method
Code Block | ||||
---|---|---|---|---|
| ||||
Connection connection = new Connection();
connection.setSipLogin(mSipLoginView.getText().toString());
connection.setSipPassword(mSipPasswordView.getText().toString());
connection.setSipDomain(mSipDomainView.getText().toString());
connection.setSipOutboundProxy(mSipDomainView.getText().toString());
connection.setSipPort(Integer.parseInt(mSipPortView.getText().toString()));
connection.setSipRegisterRequired(mSipRegisterRequiredView.isChecked());
session.connect(connection); |
4. Receiving the event confirming successful connection.
Session.onConnected() code
Code Block | ||||
---|---|---|---|---|
| ||||
@Override public void onConnected(final Connection connection) { runOnUiThread(new Runnable() { @Override public void onConnectedrun(final Connection connection) { mConnectButton.setText(R.string.action_disconnect); mConnectButton.setTag(R.string.action_disconnect); } mConnectButton.setEnabled(true); public void onRegistered(final Connection connectionif (!mSipRegisterRequiredView.isChecked()) { mConnectStatus.setText(connection....getStatus()); mCallButton.setEnabled(true); } public void onDisconnection(final Connection} connection)else { mConnectStatus.setText(connection.getStatus() + ". Registering..."); } } }); |
...
} |
5. Call/Hangup button click handler
Button.setOnClickListener() code
Code Block | ||||
---|---|---|---|---|
| ||||
sessionmCallButton.onsetOnClickListener(new IncomingCallEventOnClickListener() { @Override public void onCall(final Call call)onClick(View view) { if (mCallButton.getTag() == null || Integer.valueOf(R.string.action_call).equals(mCallButton.getTag())) { if ("".equals(mCalleeView.getText().toString())) { return; } ActivityCompat.requestPermissions(PhoneMinActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, CALL_REQUEST_CODE); ... }); |
Method Session.connect() is called to establish connection with WCS server. line 345
Connection object (line 338) with parameters required for establishing SIP connection is passed to the method.
Code Block | ||||
---|---|---|---|---|
| ||||
session.connect(connection); |
3. Outgoing call. line 387
New call is created with method Session.createCall().
CallOptions object with callee SIP username is passed to the method.
Code Block | ||||
---|---|---|---|---|
| ||||
else {
mCallButton.setEnabled(false);
call.hangup();
call = null;
}
View currentFocus = getCurrentFocus();
if (currentFocus != null) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}); |
6. Outgoing call.
Session.createCall(), Call.call() code
CallOptions object with these parameters is passed to the method:
- SIP username
- audio constraints
- SIP INVITE parameters
Code Block | ||||
---|---|---|---|---|
| ||||
case CALL_REQUEST_CODE: {
if (grantResults.length == 0 ||
grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permission has been denied by user");
} else {
mCallButton.setEnabled(false);
/**
* Get call options from the callee text field
*/
CallOptions callOptions = new CallOptions(mCalleeView.getText().toString());
AudioConstraints audioConstraints = callOptions.getConstraints().getAudioConstraints();
MediaConstraints mediaConstraints = audioConstraints.getMediaConstraints();
...
try {
Map<String, String> inviteParameters = new Gson().fromJson(mInviteParametersView.getText().toString(),
new TypeToken<Map<String, String>>() {
}.getType());
callOptions.setInviteParameters(inviteParameters);
} catch (Throwable t) {
Log.e(TAG, "Invite Parameters have wrong format of json object");
}
call = session.createCall(callOptions); |
...
call.on(callStatusEvent);
/**
* Make the outgoing call
*/
call.call();
Log.i(TAG, "Permission has been granted by user");
break;
}
} |
7. Receiving the event on incoming call
Session.onCall() code
Code Block | ||||
---|---|---|---|---|
| ||||
@Override public void onCall(final Call call) { call.on(callStatusEvent); |
Сallback functions are defined in CallStatusEvent object. (line 93).
Outgoing call is placed with method Call.call(). line 392
Code Block | ||||
---|---|---|---|---|
| ||||
call.call(); |
4. Answering incoming call.
Incoming call is answered with method Call.answer().
Code Block | ||||
---|---|---|---|---|
| ||||
call.answer(); |
5. Call hold.
The following methods are used to put call on hold and retrieve
6. Call hangup.
Method Call.hangup() is used to hang up call
Code Block | ||||
---|---|---|---|---|
| ||||
call.hangup(); |
7. Disconnection. line 358
Method Session.disconnect() is called to close connection to the server.
Code Block | ||||
---|---|---|---|---|
| ||||
session.disconnect(); /** * Display UI alert for the new incoming call */ runOnUiThread(new Runnable() { @Override public void run() { AlertDialog.Builder builder = new AlertDialog.Builder(PhoneMinActivity.this); builder.setTitle("Incoming call"); builder.setMessage("Incoming call from '" + call.getCaller() + "'"); builder.setPositiveButton("Answer", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { PhoneMinActivity.this.call = call; ActivityCompat.requestPermissions(PhoneMinActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, INCOMING_CALL_REQUEST_CODE); } }); builder.setNegativeButton("Hangup", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { call.hangup(); incomingCallAlert = null; } }); incomingCallAlert = builder.show(); } }); } |
8. Answering incoming call.
Call.answer() code
Code Block | ||||
---|---|---|---|---|
| ||||
case INCOMING_CALL_REQUEST_CODE: {
if (grantResults.length == 0 ||
grantResults[0] != PackageManager.PERMISSION_GRANTED) {
call.hangup();
incomingCallAlert = null;
Log.i(TAG, "Permission has been denied by user");
} else {
mCallButton.setText(R.string.action_hangup);
mCallButton.setTag(R.string.action_hangup);
mCallButton.setEnabled(true);
mCallStatus.setText(call.getStatus());
call.answer();
incomingCallAlert = null;
Log.i(TAG, "Permission has been granted by user");
}
} |
9. Call hold and retrieve.
Call.hold(), Call.unhold() code
Code Block | ||||
---|---|---|---|---|
| ||||
mHoldButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (mHoldButton.getTag() == null || Integer.valueOf(R.string.action_hold).equals(mHoldButton.getTag())) {
call.hold();
mHoldButton.setText(R.string.action_unhold);
mHoldButton.setTag(R.string.action_unhold);
} else {
call.unhold();
mHoldButton.setText(R.string.action_hold);
mHoldButton.setTag(R.string.action_hold);
}
}
}); |
10. DTMF sending
Call.sendDTMF() code
Code Block | ||||
---|---|---|---|---|
| ||||
mDTMF = (EditText) findViewById(R.id.dtmf);
mDTMFButton = (Button) findViewById(R.id.dtmf_button);
mDTMFButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (call != null) {
call.sendDTMF(mDTMF.getText().toString(), Call.DTMFType.RFC2833);
}
}
}); |
11. Outgoing call hangup.
Call.hangup() code
Code Block | ||||
---|---|---|---|---|
| ||||
mCallButton.setEnabled(false);
call.hangup();
call = null; |
12. Incoming call hangup.
Call.hangup() code
Code Block | ||||
---|---|---|---|---|
| ||||
builder.setNegativeButton("Hangup", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
call.hangup();
incomingCallAlert = null;
}
}); |
13. Disconnection.
Session.disconnect() code
Code Block | ||||
---|---|---|---|---|
| ||||
mConnectButton.setEnabled(false);
session.disconnect(); |
PhoneMinActivity.java