...
To analyze the code, let's take class ClickToCallActivity.java of of the click-to-call example version with hash 4ed4c6d77, which can be downloaded with corresponding build 1.0.1.338.
1. Initialization of the API. line 56
...
Flashphoner.init(
...
)
...
For initialization, object Сontext is passed to the init() method.
2. Connection to server.
Session for connection to server is created when Call button is clicked. line 75
Code Block | ||||
---|---|---|---|---|
| ||||
session = Flashphoner.createSessioninit(sessionOptionsthis); |
Session is created with method createSession(), to which object SessionOptions (line 74) with 2. Session creation.
Flashphoner.createSession() code
Object SessionOptions with URL of WCS server is passed .
Callback functions for session events are added (line 76)
- onConnected() - will be called when connection is successfully established
- onDisconnection() - will be called when connection is closed
to createSession method
Code Block | ||||
---|---|---|---|---|
| ||||
session.on(new SessionEvent() {
public void onConnected(final Connection connection) {
.....
}
public void onDisconnection(final Connection connection) {
.....
}
}); |
...
SessionOptions sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
session = Flashphoner.createSession(sessionOptions); |
3. Connection to the server.
Session.connect(). code
Connection object with appKey of internal server-side application 'clickToCallApp' is passed to the method.
Code Block | ||||
---|---|---|---|---|
| ||||
Connection connection = new Connection();
connection.setAppKey("clickToCallApp");
/**
* Connect to WCS server
*/
session.connect(connection); |
3. Outgoing call. line 99
New 4. Receiving the event confirming successful connection.
Session.onConnected(), Session.createCall() code
On this event, outgoung call is created with method Session.createCall() method.
CallOptions object with callee SIP username is passed to the method.
Code Block | ||||
---|---|---|---|---|
| ||||
call@Override public =void session.createCall(streamOptions); |
Callback functions, which make appropriate changes in controls of the interface, are added for processing call statuses. (line 100).
Code Block | ||||
---|---|---|---|---|
| ||||
call.on(new CallStatusEventonConnected(final Connection connection) { runOnUiThread(new Runnable() { @Override public void onTryingrun(final Call call) { mCallButton.setText(R.string.action_hangup); mCallButton.setTag(R.string.action_hangup); } public void onBusy(final Call call) { mCallButton.setEnabled(true); mCallStatus.....setText("Connection: " + connection.getStatus()); } public void onFailed(final Call call) { /** * Pass 'callee' to the callOptions and create a new call object */ CallOptions callOptions = new CallOptions(mCalleeView.getText()....toString()); call = session.createCall(callOptions); } public void onRing(final Call call.on(new CallStatusEvent() { ... }); ActivityCompat.requestPermissions(ClickToCallActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, public void onHold(final Call call) { CALL_REQUEST_CODE); ... } }); } |
5. Making outgoing call when permissions are granted.
Call.call() code
Code Block | ||||
---|---|---|---|---|
| ||||
case CALL_REQUEST_CODE: { if (grantResults.length == 0 }|| public void onEstablished(final Call call grantResults[0] != PackageManager.PERMISSION_GRANTED) { mCallButton.setEnabled(false); session....disconnect(); Log.i(TAG, "Permission has been denied by user"); } else { public void onFinished(final Call call) { /** * Make the outgoing call */ call.....call(); Log.i(TAG, "Permission has been granted by user"); } }); |
...
6. Disconnection.
Session.disconnect() code
Code Block | ||||
---|---|---|---|---|
| ||||
call.callmCallButton.setEnabled(false); session.disconnect(); |
4. Disconnection. line 261
Method Session.disconnect() is called to close connection to the server.7. Receiving the event confirming successful disconnection.
session.onDisconnection() code
Code Block | ||||
---|---|---|---|---|
| ||||
session.disconnect();@Override public void onDisconnection(final Connection connection) { runOnUiThread(new Runnable() { @Override public void run() { mCallButton.setText(R.string.action_call); mCallButton.setTag(R.string.action_call); mCallButton.setEnabled(true); mCallStatus.setText("Connection: " + connection.getStatus()); } }); } |