Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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.

Image Modified

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(

...

)

...

 code

For initialization, object Сontext is passed to the init() method.

Code Block
languagejava
themeRDark
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
languagejsjava
themeRDark
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

...

languagejs
themeRDark

...

.connect(). code

Connection object with parameters required for establishing SIP connection is passed to the method

Code Block
languagejava
themeRDark
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
languagejava
themeRDark
@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
languagejsjava
themeRDark
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
languagejs
themeRDark
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
languagejs
themeRDark
 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
languagejava
themeRDark
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
languagejsjava
themeRDark
@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
languagejs
themeRDark
call.call();

4. Answering incoming call.

Incoming call is answered with method Call.answer().

Code Block
languagejs
themeRDark
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
languagejs
themeRDark
call.hangup();

7. Disconnection. line 358

Method Session.disconnect() is called to close connection to the server.

Code Block
languagejs
themeRDark
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
languagejava
themeRDark
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
languagejava
themeRDark
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
languagejava
themeRDark
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
languagejava
themeRDark
mCallButton.setEnabled(false);
call.hangup();
call = null;

12. Incoming call hangup.

Call.hangup() code

Code Block
languagejava
themeRDark
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
languagejava
themeRDark
mConnectButton.setEnabled(false);
session.disconnect();


PhoneMinActivity.java