Instantiate third party phone on call DO NOT MERGE

With this CL we now create a new third party phone as needed.

Change-Id: I31880c9ac758ddf7f9538e1c182197b8d3e073e4
diff --git a/src/com/android/phone/PhoneUtils.java b/src/com/android/phone/PhoneUtils.java
index 596426b..d3a8da5 100644
--- a/src/com/android/phone/PhoneUtils.java
+++ b/src/com/android/phone/PhoneUtils.java
@@ -21,6 +21,7 @@
 import android.app.ProgressDialog;
 import android.bluetooth.IBluetoothHeadsetPhone;
 import android.content.ActivityNotFoundException;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
@@ -55,10 +56,12 @@
 import com.android.internal.telephony.MmiCode;
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
+import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.TelephonyCapabilities;
 import com.android.internal.telephony.TelephonyProperties;
 import com.android.internal.telephony.cdma.CdmaConnection;
 import com.android.internal.telephony.sip.SipPhone;
+import com.android.internal.telephony.thirdpartyphone.ThirdPartyPhone;
 import com.android.phone.CallGatewayManager.RawGatewayInfo;
 
 import java.util.ArrayList;
@@ -2554,19 +2557,25 @@
      * @param scheme the scheme from the data URI that the number originally came from.
      * @param number the phone number, or SIP address.
      */
-    public static Phone pickPhoneBasedOnNumber(CallManager cm,
-            String scheme, String number, String primarySipUri) {
+    public static Phone pickPhoneBasedOnNumber(CallManager cm, String scheme, String number,
+            String primarySipUri, ComponentName thirdPartyCallComponent) {
         if (DBG) {
             log("pickPhoneBasedOnNumber: scheme " + scheme
                     + ", number " + toLogSafePhoneNumber(number)
                     + ", sipUri "
-                    + (primarySipUri != null ? Uri.parse(primarySipUri).toSafeString() : "null"));
+                    + (primarySipUri != null ? Uri.parse(primarySipUri).toSafeString() : "null")
+                    + ", thirdPartyCallComponent: " + thirdPartyCallComponent);
         }
 
         if (primarySipUri != null) {
             Phone phone = getSipPhoneFromUri(cm, primarySipUri);
             if (phone != null) return phone;
         }
+
+        if (thirdPartyCallComponent != null) {
+            return getThirdPartyPhoneFromComponent(cm, thirdPartyCallComponent);
+        }
+
         return cm.getDefaultPhone();
     }
 
@@ -2585,6 +2594,20 @@
         return null;
     }
 
+    public static Phone getThirdPartyPhoneFromComponent(CallManager cm, ComponentName component) {
+        for (Phone phone : cm.getAllPhones()) {
+            if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_THIRD_PARTY) {
+                ThirdPartyPhone thirdPartyPhone = (ThirdPartyPhone) phone;
+                if (thirdPartyPhone.getCallProviderComponent().equals(component)) {
+                    return thirdPartyPhone;
+                }
+            }
+        }
+        ThirdPartyPhone thirdPartyPhone = PhoneFactory.makeThirdPartyPhone(component);
+        cm.registerPhone(thirdPartyPhone);
+        return thirdPartyPhone;
+    }
+
     /**
      * Returns true when the given call is in INCOMING state and there's no foreground phone call,
      * meaning the call is the first real incoming call the phone is having.