Fix SIP bug of different transport/port used for requests.

bug: http://b/3156148
Change-Id: I4fa5b274d2e90ebde12d9e99822dc193a65bad32
diff --git a/telephony/java/com/android/internal/telephony/sip/SipPhone.java b/telephony/java/com/android/internal/telephony/sip/SipPhone.java
index 5cf25313..a92ac1c 100755
--- a/telephony/java/com/android/internal/telephony/sip/SipPhone.java
+++ b/telephony/java/com/android/internal/telephony/sip/SipPhone.java
@@ -383,7 +383,9 @@
         Connection dial(String originalNumber) throws SipException {
             String calleeSipUri = originalNumber;
             if (!calleeSipUri.contains("@")) {
-                calleeSipUri += "@" + getSipDomain(mProfile);
+                calleeSipUri = mProfile.getUriString().replaceFirst(
+                        mProfile.getUserName() + "@",
+                        calleeSipUri + "@");
             }
             try {
                 SipProfile callee =
diff --git a/voip/java/android/net/sip/SipProfile.java b/voip/java/android/net/sip/SipProfile.java
index 6977e30..4029ed0 100644
--- a/voip/java/android/net/sip/SipProfile.java
+++ b/voip/java/android/net/sip/SipProfile.java
@@ -20,6 +20,7 @@
 import android.os.Parcelable;
 import android.text.TextUtils;
 
+import java.io.ObjectStreamException;
 import java.io.Serializable;
 import java.text.ParseException;
 import javax.sip.InvalidArgumentException;
@@ -40,12 +41,15 @@
 public class SipProfile implements Parcelable, Serializable, Cloneable {
     private static final long serialVersionUID = 1L;
     private static final int DEFAULT_PORT = 5060;
+    private static final String TCP = "TCP";
+    private static final String UDP = "UDP";
     private Address mAddress;
     private String mProxyAddress;
     private String mPassword;
     private String mDomain;
-    private String mProtocol = ListeningPoint.UDP;
+    private String mProtocol = UDP;
     private String mProfileName;
+    private int mPort = DEFAULT_PORT;
     private boolean mSendKeepAlive = false;
     private boolean mAutoRegistration = true;
     private transient int mCallingUid = 0;
@@ -95,6 +99,7 @@
             mUri.setUserPassword(profile.getPassword());
             mDisplayName = profile.getDisplayName();
             mProxyAddress = profile.getProxyAddress();
+            mProfile.mPort = profile.getPort();
         }
 
         /**
@@ -171,12 +176,11 @@
          * @throws IllegalArgumentException if the port number is out of range
          */
         public Builder setPort(int port) throws IllegalArgumentException {
-            try {
-                mUri.setPort(port);
-                return this;
-            } catch (InvalidArgumentException e) {
-                throw new IllegalArgumentException(e);
+            if ((port > 65535) || (port < 1000)) {
+                throw new IllegalArgumentException("incorrect port arugment");
             }
+            mProfile.mPort = port;
+            return this;
         }
 
         /**
@@ -193,7 +197,7 @@
                 throw new NullPointerException("protocol cannot be null");
             }
             protocol = protocol.toUpperCase();
-            if (!protocol.equals("UDP") && !protocol.equals("TCP")) {
+            if (!protocol.equals(UDP) && !protocol.equals(TCP)) {
                 throw new IllegalArgumentException(
                         "unsupported protocol: " + protocol);
             }
@@ -258,13 +262,22 @@
             mProfile.mPassword = mUri.getUserPassword();
             mUri.setUserPassword(null);
             try {
-                mProfile.mAddress = mAddressFactory.createAddress(
-                        mDisplayName, mUri);
                 if (!TextUtils.isEmpty(mProxyAddress)) {
                     SipURI uri = (SipURI)
                             mAddressFactory.createURI(fix(mProxyAddress));
                     mProfile.mProxyAddress = uri.getHost();
+                } else {
+                    if (!mProfile.mProtocol.equals(UDP)) {
+                        mUri.setTransportParam(mProfile.mProtocol);
+                    }
+                    if (mProfile.mPort != DEFAULT_PORT) {
+                        mUri.setPort(mProfile.mPort);
+                    }
                 }
+                mProfile.mAddress = mAddressFactory.createAddress(
+                        mDisplayName, mUri);
+            } catch (InvalidArgumentException e) {
+                throw new RuntimeException(e);
             } catch (ParseException e) {
                 // must not occur
                 throw new RuntimeException(e);
@@ -286,6 +299,7 @@
         mSendKeepAlive = (in.readInt() == 0) ? false : true;
         mAutoRegistration = (in.readInt() == 0) ? false : true;
         mCallingUid = in.readInt();
+        mPort = in.readInt();
     }
 
     @Override
@@ -299,6 +313,7 @@
         out.writeInt(mSendKeepAlive ? 1 : 0);
         out.writeInt(mAutoRegistration ? 1 : 0);
         out.writeInt(mCallingUid);
+        out.writeInt(mPort);
     }
 
     @Override
@@ -322,7 +337,13 @@
      * @return the SIP URI string of this profile
      */
     public String getUriString() {
-        return mAddress.getURI().toString();
+        // We need to return the sip uri domain instead of
+        // the SIP URI with transport, port information if
+        // the outbound proxy address exists.
+        if (!TextUtils.isEmpty(mProxyAddress)) {
+            return "sip:" + getUserName() + "@" + mDomain;
+        }
+        return getUri().toString();
     }
 
     /**
@@ -377,8 +398,7 @@
      * @return the port number of the SIP server
      */
     public int getPort() {
-        int port = getUri().getPort();
-        return (port == -1) ? DEFAULT_PORT : port;
+        return mPort;
     }
 
     /**
@@ -441,4 +461,10 @@
     public int getCallingUid() {
         return mCallingUid;
     }
+
+    private Object readResolve() throws ObjectStreamException {
+        // For compatibility.
+        if (mPort == 0) mPort = DEFAULT_PORT;
+        return this;
+    }
 }
diff --git a/voip/java/com/android/server/sip/SipHelper.java b/voip/java/com/android/server/sip/SipHelper.java
index 13e6f14..518543a 100644
--- a/voip/java/com/android/server/sip/SipHelper.java
+++ b/voip/java/com/android/server/sip/SipHelper.java
@@ -215,8 +215,9 @@
             String tag) throws ParseException, SipException {
         FromHeader fromHeader = createFromHeader(userProfile, tag);
         ToHeader toHeader = createToHeader(userProfile);
-        SipURI requestURI = mAddressFactory.createSipURI("sip:"
-                + userProfile.getSipDomain());
+        SipURI requestURI = mAddressFactory.createSipURI(
+                userProfile.getUriString().replaceFirst(
+                userProfile.getUserName() + "@", ""));
         List<ViaHeader> viaHeaders = createViaHeaders();
         CallIdHeader callIdHeader = createCallIdHeader();
         CSeqHeader cSeqHeader = createCSeqHeader(requestType);