Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 84a357bb authored by Hung-ying Tyan's avatar Hung-ying Tyan
Browse files

Refactoring SIP classes to get ready for API review.

+ replace SipAudioCall and its Listener interfaces with real implementations,
  + remove SipAudioCallImpl.java, most of it is has become part of SipAudioCall,
+ add SipSession and its Listener classes to wrap ISipSession and ISipSessionListener,
+ move SipSessionState to SipSession.State,
+ make SipManager keep context and remove the context argument from many methods of its,
+ rename SipManager.getInstance() to newInstance(),
+ rename constant names for action strings and extra keys to follow conventions,
+ set thread names for debugging purpose.

Change-Id: Ie1790dc0e8f49c06c7fc80d33fec0f673a9c3044
parent 524a6d8e
Loading
Loading
Loading
Loading
+13 −12
Original line number Diff line number Diff line
@@ -30,8 +30,8 @@ import android.net.sip.ISipSessionListener;
import android.net.sip.SipErrorCode;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipSession;
import android.net.sip.SipSessionAdapter;
import android.net.sip.SipSessionState;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Bundle;
@@ -143,7 +143,7 @@ public final class SipService extends ISipService.Stub {
    }

    private void openToReceiveCalls(SipProfile localProfile) {
        open3(localProfile, SipManager.SIP_INCOMING_CALL_ACTION, null);
        open3(localProfile, SipManager.ACTION_SIP_INCOMING_CALL, null);
    }

    public synchronized void open3(SipProfile localProfile,
@@ -255,15 +255,15 @@ public final class SipService extends ISipService.Stub {

    private void notifyProfileAdded(SipProfile localProfile) {
        if (DEBUG) Log.d(TAG, "notify: profile added: " + localProfile);
        Intent intent = new Intent(SipManager.SIP_ADD_PHONE_ACTION);
        intent.putExtra(SipManager.LOCAL_URI_KEY, localProfile.getUriString());
        Intent intent = new Intent(SipManager.ACTION_SIP_ADD_PHONE);
        intent.putExtra(SipManager.EXTRA_LOCAL_URI, localProfile.getUriString());
        mContext.sendBroadcast(intent);
    }

    private void notifyProfileRemoved(SipProfile localProfile) {
        if (DEBUG) Log.d(TAG, "notify: profile removed: " + localProfile);
        Intent intent = new Intent(SipManager.SIP_REMOVE_PHONE_ACTION);
        intent.putExtra(SipManager.LOCAL_URI_KEY, localProfile.getUriString());
        Intent intent = new Intent(SipManager.ACTION_SIP_REMOVE_PHONE);
        intent.putExtra(SipManager.EXTRA_LOCAL_URI, localProfile.getUriString());
        mContext.sendBroadcast(intent);
    }

@@ -474,8 +474,8 @@ public final class SipService extends ISipService.Stub {
                    // send out incoming call broadcast
                    addPendingSession(session);
                    Intent intent = SipManager.createIncomingCallBroadcast(
                            mIncomingCallBroadcastAction, session.getCallId(),
                            sessionDescription);
                            session.getCallId(), sessionDescription)
                            .setAction(mIncomingCallBroadcastAction);
                    if (DEBUG) Log.d(TAG, " ringing~~ " + getUri() + ": "
                            + caller.getUri() + ": " + session.getCallId()
                            + " " + mIncomingCallBroadcastAction);
@@ -613,10 +613,10 @@ public final class SipService extends ISipService.Stub {

                try {
                    int state = (mSession == null)
                            ? SipSessionState.READY_TO_CALL
                            ? SipSession.State.READY_TO_CALL
                            : mSession.getState();
                    if ((state == SipSessionState.REGISTERING)
                            || (state == SipSessionState.DEREGISTERING)) {
                    if ((state == SipSession.State.REGISTERING)
                            || (state == SipSession.State.DEREGISTERING)) {
                        mProxy.onRegistering(mSession);
                    } else if (mRegistered) {
                        int duration = (int)
@@ -1138,7 +1138,8 @@ public final class SipService extends ISipService.Stub {
                event.mTriggerTime += event.mPeriod;

                // run the callback in a new thread to prevent deadlock
                new Thread(event.mCallback).start();
                new Thread(event.mCallback, "SipServiceTimerCallbackThread")
                        .start();
            }
            if (DEBUG_TIMER) {
                Log.d(TAG, "after timeout execution");
+49 −49
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ import android.net.sip.ISipSessionListener;
import android.net.sip.SessionDescription;
import android.net.sip.SipErrorCode;
import android.net.sip.SipProfile;
import android.net.sip.SipSession;
import android.net.sip.SipSessionAdapter;
import android.net.sip.SipSessionState;
import android.text.TextUtils;
import android.util.Log;

@@ -121,7 +121,7 @@ class SipSessionGroup implements SipListener {
        reset(localIp);
    }

    void reset(String localIp) throws SipException, IOException {
    synchronized void reset(String localIp) throws SipException, IOException {
        mLocalIp = localIp;
        if (localIp == null) return;

@@ -301,7 +301,7 @@ class SipSessionGroup implements SipListener {
            boolean processed = (session != null) && session.process(event);
            if (isLoggable && processed) {
                Log.d(TAG, "new state after: "
                        + SipSessionState.toString(session.mState));
                        + SipSession.State.toString(session.mState));
            }
        } catch (Throwable e) {
            Log.w(TAG, "event process error: " + event, e);
@@ -332,7 +332,7 @@ class SipSessionGroup implements SipListener {

        public boolean process(EventObject evt) throws SipException {
            if (isLoggable(this, evt)) Log.d(TAG, " ~~~~~   " + this + ": "
                    + SipSessionState.toString(mState) + ": processing "
                    + SipSession.State.toString(mState) + ": processing "
                    + log(evt));
            if (isRequestEvent(Request.INVITE, evt)) {
                RequestEvent event = (RequestEvent) evt;
@@ -342,7 +342,7 @@ class SipSessionGroup implements SipListener {
                newSession.mDialog = newSession.mServerTransaction.getDialog();
                newSession.mInviteReceived = event;
                newSession.mPeerProfile = createPeerProfile(event.getRequest());
                newSession.mState = SipSessionState.INCOMING_CALL;
                newSession.mState = SipSession.State.INCOMING_CALL;
                newSession.mPeerSessionDescription =
                        extractContent(event.getRequest());
                addSipSession(newSession);
@@ -361,7 +361,7 @@ class SipSessionGroup implements SipListener {
    class SipSessionImpl extends ISipSession.Stub {
        SipProfile mPeerProfile;
        SipSessionListenerProxy mProxy = new SipSessionListenerProxy();
        int mState = SipSessionState.READY_TO_CALL;
        int mState = SipSession.State.READY_TO_CALL;
        RequestEvent mInviteReceived;
        Dialog mDialog;
        ServerTransaction mServerTransaction;
@@ -381,7 +381,7 @@ class SipSessionGroup implements SipListener {
                        sleep(timeout);
                        if (mRunning) timeout();
                    }
                }).start();
                }, "SipSessionTimerThread").start();
            }

            synchronized void cancel() {
@@ -416,7 +416,7 @@ class SipSessionGroup implements SipListener {
            mInCall = false;
            removeSipSession(this);
            mPeerProfile = null;
            mState = SipSessionState.READY_TO_CALL;
            mState = SipSession.State.READY_TO_CALL;
            mInviteReceived = null;
            mDialog = null;
            mServerTransaction = null;
@@ -473,7 +473,7 @@ class SipSessionGroup implements SipListener {
                            onError(e);
                        }
                    }
            }).start();
            }, "SipSessionAsyncCmdThread").start();
        }

        public void makeCall(SipProfile peerProfile, String sessionDescription,
@@ -523,10 +523,10 @@ class SipSessionGroup implements SipListener {
        }

        public void sendKeepAlive() {
            mState = SipSessionState.PINGING;
            mState = SipSession.State.PINGING;
            try {
                processCommand(new OptionsCommand());
                while (SipSessionState.PINGING == mState) {
                while (SipSession.State.PINGING == mState) {
                    Thread.sleep(1000);
                }
            } catch (SipException e) {
@@ -553,7 +553,7 @@ class SipSessionGroup implements SipListener {
            try {
                String s = super.toString();
                return s.substring(s.indexOf("@")) + ":"
                        + SipSessionState.toString(mState);
                        + SipSession.State.toString(mState);
            } catch (Throwable e) {
                return super.toString();
            }
@@ -561,7 +561,7 @@ class SipSessionGroup implements SipListener {

        public boolean process(EventObject evt) throws SipException {
            if (isLoggable(this, evt)) Log.d(TAG, " ~~~~~   " + this + ": "
                    + SipSessionState.toString(mState) + ": processing "
                    + SipSession.State.toString(mState) + ": processing "
                    + log(evt));
            synchronized (SipSessionGroup.this) {
                if (isClosed()) return false;
@@ -577,30 +577,30 @@ class SipSessionGroup implements SipListener {
                boolean processed;

                switch (mState) {
                case SipSessionState.REGISTERING:
                case SipSessionState.DEREGISTERING:
                case SipSession.State.REGISTERING:
                case SipSession.State.DEREGISTERING:
                    processed = registeringToReady(evt);
                    break;
                case SipSessionState.PINGING:
                case SipSession.State.PINGING:
                    processed = keepAliveProcess(evt);
                    break;
                case SipSessionState.READY_TO_CALL:
                case SipSession.State.READY_TO_CALL:
                    processed = readyForCall(evt);
                    break;
                case SipSessionState.INCOMING_CALL:
                case SipSession.State.INCOMING_CALL:
                    processed = incomingCall(evt);
                    break;
                case SipSessionState.INCOMING_CALL_ANSWERING:
                case SipSession.State.INCOMING_CALL_ANSWERING:
                    processed = incomingCallToInCall(evt);
                    break;
                case SipSessionState.OUTGOING_CALL:
                case SipSessionState.OUTGOING_CALL_RING_BACK:
                case SipSession.State.OUTGOING_CALL:
                case SipSession.State.OUTGOING_CALL_RING_BACK:
                    processed = outgoingCall(evt);
                    break;
                case SipSessionState.OUTGOING_CALL_CANCELING:
                case SipSession.State.OUTGOING_CALL_CANCELING:
                    processed = outgoingCallToReady(evt);
                    break;
                case SipSessionState.IN_CALL:
                case SipSession.State.IN_CALL:
                    processed = inCall(evt);
                    break;
                default:
@@ -650,8 +650,8 @@ class SipSessionGroup implements SipListener {
        private void processTransactionTerminated(
                TransactionTerminatedEvent event) {
            switch (mState) {
                case SipSessionState.IN_CALL:
                case SipSessionState.READY_TO_CALL:
                case SipSession.State.IN_CALL:
                case SipSession.State.READY_TO_CALL:
                    Log.d(TAG, "Transaction terminated; do nothing");
                    break;
                default:
@@ -670,27 +670,27 @@ class SipSessionGroup implements SipListener {
                    ? event.getServerTransaction()
                    : event.getClientTransaction();

            if ((current != target) && (mState != SipSessionState.PINGING)) {
            if ((current != target) && (mState != SipSession.State.PINGING)) {
                Log.d(TAG, "not the current transaction; current=" + current
                        + ", timed out=" + target);
                return;
            }
            switch (mState) {
                case SipSessionState.REGISTERING:
                case SipSessionState.DEREGISTERING:
                case SipSession.State.REGISTERING:
                case SipSession.State.DEREGISTERING:
                    reset();
                    mProxy.onRegistrationTimeout(this);
                    break;
                case SipSessionState.INCOMING_CALL:
                case SipSessionState.INCOMING_CALL_ANSWERING:
                case SipSessionState.OUTGOING_CALL:
                case SipSessionState.OUTGOING_CALL_CANCELING:
                case SipSession.State.INCOMING_CALL:
                case SipSession.State.INCOMING_CALL_ANSWERING:
                case SipSession.State.OUTGOING_CALL:
                case SipSession.State.OUTGOING_CALL_CANCELING:
                    onError(SipErrorCode.TIME_OUT, event.toString());
                    break;
                case SipSessionState.PINGING:
                case SipSession.State.PINGING:
                    reset();
                    mReRegisterFlag = true;
                    mState = SipSessionState.READY_TO_CALL;
                    mState = SipSession.State.READY_TO_CALL;
                    break;

                default:
@@ -764,7 +764,7 @@ class SipSessionGroup implements SipListener {
                switch (statusCode) {
                case Response.OK:
                    int state = mState;
                    onRegistrationDone((state == SipSessionState.REGISTERING)
                    onRegistrationDone((state == SipSession.State.REGISTERING)
                            ? getExpiryTime(((ResponseEvent) evt).getResponse())
                            : -1);
                    mLastNonce = null;
@@ -851,7 +851,7 @@ class SipSessionGroup implements SipListener {
                        generateTag());
                mDialog = mClientTransaction.getDialog();
                addSipSession(this);
                mState = SipSessionState.OUTGOING_CALL;
                mState = SipSession.State.OUTGOING_CALL;
                mProxy.onCalling(this);
                startSessionTimer(cmd.getTimeout());
                return true;
@@ -861,7 +861,7 @@ class SipSessionGroup implements SipListener {
                        generateTag(), duration);
                mDialog = mClientTransaction.getDialog();
                addSipSession(this);
                mState = SipSessionState.REGISTERING;
                mState = SipSession.State.REGISTERING;
                mProxy.onRegistering(this);
                return true;
            } else if (DEREGISTER == evt) {
@@ -869,7 +869,7 @@ class SipSessionGroup implements SipListener {
                        generateTag(), 0);
                mDialog = mClientTransaction.getDialog();
                addSipSession(this);
                mState = SipSessionState.DEREGISTERING;
                mState = SipSession.State.DEREGISTERING;
                mProxy.onRegistering(this);
                return true;
            }
@@ -884,7 +884,7 @@ class SipSessionGroup implements SipListener {
                        mLocalProfile,
                        ((MakeCallCommand) evt).getSessionDescription(),
                        mServerTransaction);
                mState = SipSessionState.INCOMING_CALL_ANSWERING;
                mState = SipSession.State.INCOMING_CALL_ANSWERING;
                startSessionTimer(((MakeCallCommand) evt).getTimeout());
                return true;
            } else if (END_CALL == evt) {
@@ -925,8 +925,8 @@ class SipSessionGroup implements SipListener {
                int statusCode = response.getStatusCode();
                switch (statusCode) {
                case Response.RINGING:
                    if (mState == SipSessionState.OUTGOING_CALL) {
                        mState = SipSessionState.OUTGOING_CALL_RING_BACK;
                    if (mState == SipSession.State.OUTGOING_CALL) {
                        mState = SipSession.State.OUTGOING_CALL_RING_BACK;
                        mProxy.onRingingBack(this);
                        cancelSessionTimer();
                    }
@@ -969,7 +969,7 @@ class SipSessionGroup implements SipListener {
                // response comes back yet. We are cheating for not checking
                // response.
                mSipHelper.sendCancel(mClientTransaction);
                mState = SipSessionState.OUTGOING_CALL_CANCELING;
                mState = SipSession.State.OUTGOING_CALL_CANCELING;
                startSessionTimer(CANCEL_CALL_TIMER);
                return true;
            }
@@ -1025,7 +1025,7 @@ class SipSessionGroup implements SipListener {
            } else if (isRequestEvent(Request.INVITE, evt)) {
                // got Re-INVITE
                RequestEvent event = mInviteReceived = (RequestEvent) evt;
                mState = SipSessionState.INCOMING_CALL;
                mState = SipSession.State.INCOMING_CALL;
                mPeerSessionDescription = extractContent(event.getRequest());
                mServerTransaction = null;
                mProxy.onRinging(this, mPeerProfile, mPeerSessionDescription);
@@ -1038,7 +1038,7 @@ class SipSessionGroup implements SipListener {
                // to change call
                mClientTransaction = mSipHelper.sendReinvite(mDialog,
                        ((MakeCallCommand) evt).getSessionDescription());
                mState = SipSessionState.OUTGOING_CALL;
                mState = SipSession.State.OUTGOING_CALL;
                startSessionTimer(((MakeCallCommand) evt).getTimeout());
                return true;
            }
@@ -1066,14 +1066,14 @@ class SipSessionGroup implements SipListener {
        }

        private void establishCall() {
            mState = SipSessionState.IN_CALL;
            mState = SipSession.State.IN_CALL;
            mInCall = true;
            cancelSessionTimer();
            mProxy.onCallEstablished(this, mPeerSessionDescription);
        }

        private void fallbackToPreviousInCall(int errorCode, String message) {
            mState = SipSessionState.IN_CALL;
            mState = SipSession.State.IN_CALL;
            mProxy.onCallChangeFailed(this, errorCode, message);
        }

@@ -1095,8 +1095,8 @@ class SipSessionGroup implements SipListener {
        private void onError(int errorCode, String message) {
            cancelSessionTimer();
            switch (mState) {
                case SipSessionState.REGISTERING:
                case SipSessionState.DEREGISTERING:
                case SipSession.State.REGISTERING:
                case SipSession.State.DEREGISTERING:
                    onRegistrationFailed(errorCode, message);
                    break;
                default:
@@ -1270,7 +1270,7 @@ class SipSessionGroup implements SipListener {
    private static boolean isLoggable(SipSessionImpl s) {
        if (s != null) {
            switch (s.mState) {
                case SipSessionState.PINGING:
                case SipSession.State.PINGING:
                    return DEBUG_PING;
            }
        }
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ class SipSessionListenerProxy extends ISipSessionListener.Stub {
        // One thread for each calling back.
        // Note: Guarantee ordering if the issue becomes important. Currently,
        // the chance of handling two callback events at a time is none.
        new Thread(runnable).start();
        new Thread(runnable, "SipSessionCallbackThread").start();
    }

    public void onCalling(final ISipSession session) {
+12 −12
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ import android.net.sip.SipErrorCode;
import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipSessionState;
import android.net.sip.SipSession;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.Looper;
@@ -92,7 +92,7 @@ public class SipPhone extends SipPhoneBase {
        foregroundCall = new SipCall();
        backgroundCall = new SipCall();
        mProfile = profile;
        mSipManager = SipManager.getInstance(context);
        mSipManager = SipManager.newInstance(context);

        // FIXME: what's this for SIP?
        //Change the system property
@@ -707,8 +707,8 @@ public class SipPhone extends SipPhoneBase {

        void dial() throws SipException {
            setState(Call.State.DIALING);
            mSipAudioCall = mSipManager.makeAudioCall(mContext, mProfile,
                    mPeer, null, SESSION_TIMEOUT);
            mSipAudioCall = mSipManager.makeAudioCall(mProfile, mPeer, null,
                    SESSION_TIMEOUT);
            mSipAudioCall.setRingbackToneEnabled(false);
            mSipAudioCall.setListener(mAdapter);
        }
@@ -808,20 +808,20 @@ public class SipPhone extends SipPhoneBase {
        if (sipAudioCall.isOnHold()) return Call.State.HOLDING;
        int sessionState = sipAudioCall.getState();
        switch (sessionState) {
            case SipSessionState.READY_TO_CALL:            return Call.State.IDLE;
            case SipSessionState.INCOMING_CALL:
            case SipSessionState.INCOMING_CALL_ANSWERING:  return Call.State.INCOMING;
            case SipSessionState.OUTGOING_CALL:            return Call.State.DIALING;
            case SipSessionState.OUTGOING_CALL_RING_BACK:  return Call.State.ALERTING;
            case SipSessionState.OUTGOING_CALL_CANCELING:  return Call.State.DISCONNECTING;
            case SipSessionState.IN_CALL:                  return Call.State.ACTIVE;
            case SipSession.State.READY_TO_CALL:            return Call.State.IDLE;
            case SipSession.State.INCOMING_CALL:
            case SipSession.State.INCOMING_CALL_ANSWERING:  return Call.State.INCOMING;
            case SipSession.State.OUTGOING_CALL:            return Call.State.DIALING;
            case SipSession.State.OUTGOING_CALL_RING_BACK:  return Call.State.ALERTING;
            case SipSession.State.OUTGOING_CALL_CANCELING:  return Call.State.DISCONNECTING;
            case SipSession.State.IN_CALL:                  return Call.State.ACTIVE;
            default:
                Log.w(LOG_TAG, "illegal connection state: " + sessionState);
                return Call.State.DISCONNECTED;
        }
    }

    private abstract class SipAudioCallAdapter extends SipAudioCall.Adapter {
    private abstract class SipAudioCallAdapter extends SipAudioCall.Listener {
        protected abstract void onCallEnded(Connection.DisconnectCause cause);
        protected abstract void onError(Connection.DisconnectCause cause);

+830 −125

File changed.

Preview size limit exceeded, changes collapsed.

Loading