Merge "Hide Wi-Fi and Video calling preferences if IMS service is not ready"
diff --git a/src/com/android/phone/CallFeaturesSetting.java b/src/com/android/phone/CallFeaturesSetting.java
index fd85585..dfbec46 100644
--- a/src/com/android/phone/CallFeaturesSetting.java
+++ b/src/com/android/phone/CallFeaturesSetting.java
@@ -133,7 +133,7 @@
 
         if (preference == mEnableVideoCalling) {
             if (mImsMgr.isEnhanced4gLteModeSettingEnabledByUser()) {
-                PhoneGlobals.getInstance().phoneMgr.enableVideoCalling((boolean) objValue);
+                mImsMgr.setVtSetting((boolean) objValue);
             } else {
                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                 DialogInterface.OnClickListener networkSettingsClickListener =
@@ -197,12 +197,10 @@
             if (mEnableVideoCalling != null) {
                 // Use TelephonyManager#getCallStete instead of 'state' parameter because it needs
                 // to check the current state of all phone calls.
-                TelephonyManager telephonyManager =
-                        (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
-                mEnableVideoCalling.setEnabled(
-                        telephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE);
-                mButtonWifiCalling.setEnabled(
-                        telephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE);
+                boolean isCallStateIdle =
+                        mTelecomManager.getCallState() == TelephonyManager.CALL_STATE_IDLE;
+                mEnableVideoCalling.setEnabled(isCallStateIdle);
+                mButtonWifiCalling.setEnabled(isCallStateIdle);
             }
         }
     };
@@ -212,7 +210,8 @@
         super.onPause();
         TelephonyManager telephonyManager =
                 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
-        telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
+        telephonyManager.createForSubscriptionId(mPhone.getSubId())
+                .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
     }
 
     @Override
@@ -227,8 +226,8 @@
 
         addPreferencesFromResource(R.xml.call_feature_setting);
 
-        TelephonyManager telephonyManager =
-                (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager telephonyManager = getSystemService(TelephonyManager.class)
+                .createForSubscriptionId(mPhone.getSubId());
         telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
 
         PreferenceScreen prefSet = getPreferenceScreen();
@@ -301,21 +300,13 @@
                 || mPhone.mDcTracker.isDataEnabled())) {
             boolean currentValue =
                     mImsMgr.isEnhanced4gLteModeSettingEnabledByUser()
-                    ? PhoneGlobals.getInstance().phoneMgr.isVideoCallingEnabled(
-                            getOpPackageName()) : false;
+                    ? mImsMgr.isVtEnabledByUser() : false;
             mEnableVideoCalling.setChecked(currentValue);
             mEnableVideoCalling.setOnPreferenceChangeListener(this);
         } else {
             prefSet.removePreference(mEnableVideoCalling);
         }
 
-        if (mImsMgr.isVolteEnabledByPlatform()
-                && !carrierConfig.getBoolean(
-                        CarrierConfigManager.KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL)) {
-            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
-            /* tm.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); */
-        }
-
         final PhoneAccountHandle simCallManager = mTelecomManager.getSimCallManager();
         if (simCallManager != null) {
             Intent intent = PhoneAccountSettingsFragment.buildPhoneAccountConfigureIntent(
diff --git a/src/com/android/phone/CallForwardEditPreference.java b/src/com/android/phone/CallForwardEditPreference.java
index 8a82e05..bfeee7a 100644
--- a/src/com/android/phone/CallForwardEditPreference.java
+++ b/src/com/android/phone/CallForwardEditPreference.java
@@ -1,9 +1,7 @@
 package com.android.phone;
 
-import com.android.internal.telephony.CallForwardInfo;
-import com.android.internal.telephony.CommandException;
-import com.android.internal.telephony.CommandsInterface;
-import com.android.internal.telephony.Phone;
+import static com.android.phone.TimeConsumingPreferenceActivity.EXCEPTION_ERROR;
+import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
 
 import android.app.AlertDialog;
 import android.content.Context;
@@ -24,8 +22,10 @@
 import android.util.Log;
 import android.view.View;
 
-import static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
-import static com.android.phone.TimeConsumingPreferenceActivity.EXCEPTION_ERROR;
+import com.android.internal.telephony.CallForwardInfo;
+import com.android.internal.telephony.CommandException;
+import com.android.internal.telephony.CommandsInterface;
+import com.android.internal.telephony.Phone;
 
 public class CallForwardEditPreference extends EditPhoneNumberPreference {
     private static final String LOG_TAG = "CallForwardEditPreference";
@@ -69,22 +69,11 @@
         this(context, null);
     }
 
-    void init(TimeConsumingPreferenceListener listener, boolean skipReading, Phone phone,
+    void init(TimeConsumingPreferenceListener listener, Phone phone,
             boolean replaceInvalidCFNumber) {
         mPhone = phone;
         mTcpListener = listener;
         mReplaceInvalidCFNumber = replaceInvalidCFNumber;
-
-        if (!skipReading) {
-            mPhone.getCallForwardingOption(reason,
-                    mHandler.obtainMessage(MyHandler.MESSAGE_GET_CF,
-                            // unused in this case
-                            CommandsInterface.CF_ACTION_DISABLE,
-                            MyHandler.MESSAGE_GET_CF, null));
-            if (mTcpListener != null) {
-                mTcpListener.onStarted(this, true);
-            }
-        }
     }
 
     @Override
@@ -176,6 +165,23 @@
         setPhoneNumber(displayVoicemailNumber ? voicemailNumber : callForwardInfo.number);
     }
 
+    /**
+     * Starts the Call Forwarding Option query to the network and calls
+     * {@link TimeConsumingPreferenceListener#onStarted}. Will call
+     * {@link TimeConsumingPreferenceListener#onFinished} when finished, or
+     * {@link TimeConsumingPreferenceListener#onError} if an error has occurred.
+     */
+    void startCallForwardOptionsQuery() {
+        mPhone.getCallForwardingOption(reason,
+                mHandler.obtainMessage(MyHandler.MESSAGE_GET_CF,
+                        // unused in this case
+                        CommandsInterface.CF_ACTION_DISABLE,
+                        MyHandler.MESSAGE_GET_CF, null));
+        if (mTcpListener != null) {
+            mTcpListener.onStarted(this, true);
+        }
+    }
+
     private void updateSummaryText() {
         if (isToggled()) {
             final String number = getRawPhoneNumber();
diff --git a/src/com/android/phone/CallNotifier.java b/src/com/android/phone/CallNotifier.java
index ec670f5..ebef00d 100644
--- a/src/com/android/phone/CallNotifier.java
+++ b/src/com/android/phone/CallNotifier.java
@@ -563,13 +563,16 @@
                     R.string.incall_error_supp_service_separate);
         } else if (r.result == Phone.SuppService.SWITCH) {
             if (DBG) log("onSuppServiceFailed: displaying switch failure message");
-            mApplication.getResources().getString(
+            mergeFailedString = mApplication.getResources().getString(
                     R.string.incall_error_supp_service_switch);
         } else if (r.result == Phone.SuppService.REJECT) {
             if (DBG) log("onSuppServiceFailed: displaying reject failure message");
-            mApplication.getResources().getString(
+            mergeFailedString = mApplication.getResources().getString(
                     R.string.incall_error_supp_service_reject);
-        } else {
+        } else if (r.result == Phone.SuppService.HANGUP) {
+            mergeFailedString = mApplication.getResources().getString(
+                    R.string.incall_error_supp_service_hangup);
+        }  else {
             if (DBG) log("onSuppServiceFailed: unknown failure");
             return;
         }
diff --git a/src/com/android/phone/EmergencyCallbackModeExitDialog.java b/src/com/android/phone/EmergencyCallbackModeExitDialog.java
index 9d43d60..fd6510b 100644
--- a/src/com/android/phone/EmergencyCallbackModeExitDialog.java
+++ b/src/com/android/phone/EmergencyCallbackModeExitDialog.java
@@ -219,7 +219,8 @@
         case EXIT_ECM_BLOCK_OTHERS:
         case EXIT_ECM_DIALOG:
             CharSequence text = getDialogText(mEcmTimeout);
-            mAlertDialog = new AlertDialog.Builder(EmergencyCallbackModeExitDialog.this)
+            mAlertDialog = new AlertDialog.Builder(EmergencyCallbackModeExitDialog.this,
+                    android.R.style.Theme_DeviceDefault_Dialog_Alert)
                     .setIcon(R.drawable.ic_emergency_callback_mode)
                     .setTitle(R.string.phone_in_ecm_notification_title)
                     .setMessage(text)
@@ -247,7 +248,8 @@
             return mAlertDialog;
 
         case EXIT_ECM_IN_EMERGENCY_CALL_DIALOG:
-            mAlertDialog = new AlertDialog.Builder(EmergencyCallbackModeExitDialog.this)
+            mAlertDialog = new AlertDialog.Builder(EmergencyCallbackModeExitDialog.this,
+                    android.R.style.Theme_DeviceDefault_Dialog_Alert)
                     .setIcon(R.drawable.ic_emergency_callback_mode)
                     .setTitle(R.string.phone_in_ecm_notification_title)
                     .setMessage(R.string.alert_dialog_in_ecm_call)
diff --git a/src/com/android/phone/GsmUmtsCallForwardOptions.java b/src/com/android/phone/GsmUmtsCallForwardOptions.java
index 77cc6cc..e562e46 100644
--- a/src/com/android/phone/GsmUmtsCallForwardOptions.java
+++ b/src/com/android/phone/GsmUmtsCallForwardOptions.java
@@ -1,9 +1,5 @@
 package com.android.phone;
 
-import com.android.internal.telephony.CallForwardInfo;
-import com.android.internal.telephony.CommandsInterface;
-import com.android.internal.telephony.Phone;
-
 import android.app.ActionBar;
 import android.content.Intent;
 import android.database.Cursor;
@@ -14,8 +10,11 @@
 import android.util.Log;
 import android.view.MenuItem;
 
-import java.util.ArrayList;
+import com.android.internal.telephony.CallForwardInfo;
+import com.android.internal.telephony.CommandsInterface;
+import com.android.internal.telephony.Phone;
 
+import java.util.ArrayList;
 
 public class GsmUmtsCallForwardOptions extends TimeConsumingPreferenceActivity {
     private static final String LOG_TAG = "GsmUmtsCallForwardOptions";
@@ -103,7 +102,10 @@
         if (mFirstResume) {
             if (mIcicle == null) {
                 Log.d(LOG_TAG, "start to init ");
-                mPreferences.get(mInitIndex).init(this, false, mPhone, mReplaceInvalidCFNumbers);
+                CallForwardEditPreference pref = mPreferences.get(mInitIndex);
+                pref.init(this, mPhone, mReplaceInvalidCFNumbers);
+                pref.startCallForwardOptionsQuery();
+
             } else {
                 mInitIndex = mPreferences.size();
 
@@ -113,8 +115,8 @@
                     CallForwardInfo cf = new CallForwardInfo();
                     cf.number = bundle.getString(KEY_NUMBER);
                     cf.status = bundle.getInt(KEY_STATUS);
+                    pref.init(this, mPhone, mReplaceInvalidCFNumbers);
                     pref.handleCallForwardResult(cf);
-                    pref.init(this, true, mPhone, mReplaceInvalidCFNumbers);
                 }
             }
             mFirstResume = false;
@@ -141,7 +143,9 @@
     public void onFinished(Preference preference, boolean reading) {
         if (mInitIndex < mPreferences.size()-1 && !isFinishing()) {
             mInitIndex++;
-            mPreferences.get(mInitIndex).init(this, false, mPhone, mReplaceInvalidCFNumbers);
+            CallForwardEditPreference pref = mPreferences.get(mInitIndex);
+            pref.init(this, mPhone, mReplaceInvalidCFNumbers);
+            pref.startCallForwardOptionsQuery();
         }
 
         super.onFinished(preference, reading);
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index 0fafeca..53fa2cb 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -720,13 +720,6 @@
             int max = mSubscriptionManager.getActiveSubscriptionInfoCountMax();
             mActiveSubInfos = new ArrayList<SubscriptionInfo>(max);
 
-            IntentFilter intentFilter = new IntentFilter(
-                    TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
-            activity.registerReceiver(mPhoneChangeReceiver, intentFilter);
-
-            activity.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false,
-                    mDpcEnforcedContentObserver);
-
             Log.i(LOG_TAG, "onCreate:-");
         }
 
@@ -753,6 +746,10 @@
             @Override
             public void onReceive(Context context, Intent intent) {
                 Log.i(LOG_TAG, "onReceive:");
+                if (getActivity() == null || getContext() == null) {
+                    // Received broadcast and activity is in the process of being torn down.
+                    return;
+                }
                 // When the radio changes (ex: CDMA->GSM), refresh all options.
                 updateBody();
             }
@@ -766,6 +763,10 @@
             @Override
             public void onChange(boolean selfChange) {
                 Log.i(LOG_TAG, "DPC enforced onChange:");
+                if (getActivity() == null || getContext() == null) {
+                    // Received content change and activity is in the process of being torn down.
+                    return;
+                }
                 updateBody();
             }
         }
@@ -774,11 +775,6 @@
         public void onDestroy() {
             unbindNetworkQueryService();
             super.onDestroy();
-            if (getActivity() != null) {
-                getActivity().unregisterReceiver(mPhoneChangeReceiver);
-                getActivity().getContentResolver().unregisterContentObserver(
-                        mDpcEnforcedContentObserver);
-            }
         }
 
         @Override
@@ -815,6 +811,13 @@
 
             mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
 
+            final Context context = getActivity();
+            IntentFilter intentFilter = new IntentFilter(
+                    TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
+            context.registerReceiver(mPhoneChangeReceiver, intentFilter);
+            context.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false,
+                    mDpcEnforcedContentObserver);
+
             Log.i(LOG_TAG, "onResume:-");
 
         }
@@ -1151,6 +1154,10 @@
 
             mSubscriptionManager
                     .removeOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+
+            final Context context = getActivity();
+            context.unregisterReceiver(mPhoneChangeReceiver);
+            context.getContentResolver().unregisterContentObserver(mDpcEnforcedContentObserver);
             if (DBG) log("onPause:-");
         }
 
diff --git a/src/com/android/phone/NetworkSelectListPreference.java b/src/com/android/phone/NetworkSelectListPreference.java
index 3ba1512..2a55839 100644
--- a/src/com/android/phone/NetworkSelectListPreference.java
+++ b/src/com/android/phone/NetworkSelectListPreference.java
@@ -227,7 +227,7 @@
         TelephonyManager telephonyManager = (TelephonyManager)
                 getContext().getSystemService(Context.TELEPHONY_SERVICE);
 
-        setSummary(telephonyManager.getNetworkOperatorName());
+        setSummary(telephonyManager.getNetworkOperatorName(mSubId));
 
         setOnPreferenceChangeListener(this);
     }
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index d181e5a..0026311 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -219,6 +219,9 @@
 
     private NetworkScanRequestTracker mNetworkScanRequestTracker;
 
+    private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
+    private static final int MANUFACTURER_CODE_LENGTH = 8;
+
     /**
      * A request object to use for transmitting data to an ICC.
      */
@@ -1835,6 +1838,17 @@
     }
 
     @Override
+    public String getTypeAllocationCodeForSlot(int slotIndex) {
+        Phone phone = PhoneFactory.getPhone(slotIndex);
+        String tac = null;
+        if (phone != null) {
+            String imei = phone.getImei();
+            tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
+        }
+        return tac;
+    }
+
+    @Override
     public String getMeidForSlot(int slotIndex, String callingPackage) {
         Phone phone = PhoneFactory.getPhone(slotIndex);
         if (phone == null) {
@@ -1849,6 +1863,17 @@
     }
 
     @Override
+    public String getManufacturerCodeForSlot(int slotIndex) {
+        Phone phone = PhoneFactory.getPhone(slotIndex);
+        String manufacturerCode = null;
+        if (phone != null) {
+            String meid = phone.getMeid();
+            manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
+        }
+        return manufacturerCode;
+    }
+
+    @Override
     public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
         Phone phone = PhoneFactory.getPhone(slotIndex);
         if (phone == null) {
@@ -2748,7 +2773,11 @@
      * @hide
      */
     public boolean setImsService(int slotId, boolean isCarrierImsService, String packageName) {
-        enforceModifyPermission();
+        int[] subIds = SubscriptionManager.getSubId(slotId);
+        TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
+                (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
+                "setImsService");
+
         return PhoneFactory.getImsResolver().overrideImsServiceConfiguration(slotId,
                 isCarrierImsService, packageName);
     }
@@ -2762,7 +2791,11 @@
      * @return the package name of the ImsService configuration.
      */
     public String getImsService(int slotId, boolean isCarrierImsService) {
-        enforceReadPrivilegedPermission();
+        int[] subIds = SubscriptionManager.getSubId(slotId);
+        TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
+                (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
+                "getImsService");
+
         return PhoneFactory.getImsResolver().getImsServiceConfiguration(slotId,
                 isCarrierImsService);
     }
diff --git a/src/com/android/phone/TelephonyShellCommand.java b/src/com/android/phone/TelephonyShellCommand.java
index fce1086..4acb46b 100644
--- a/src/com/android/phone/TelephonyShellCommand.java
+++ b/src/com/android/phone/TelephonyShellCommand.java
@@ -36,6 +36,7 @@
     private static final String LOG_TAG = "TelephonyShellCommand";
     // Don't commit with this true.
     private static final boolean VDBG = true;
+    private static final int DEFAULT_PHONE_ID = 0;
 
     private static final String IMS_SUBCOMMAND = "ims";
     private static final String IMS_SET_CARRIER_SERVICE = "set-ims-service";
@@ -130,7 +131,7 @@
     // ims set-ims-service
     private int handleImsSetServiceCommand() {
         PrintWriter errPw = getErrPrintWriter();
-        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        int slotId = getDefaultSlot();
         Boolean isCarrierService = null;
 
         String opt;
@@ -186,7 +187,7 @@
     // ims get-ims-service
     private int handleImsGetServiceCommand() {
         PrintWriter errPw = getErrPrintWriter();
-        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        int slotId = getDefaultSlot();
         Boolean isCarrierService = null;
 
         String opt;
@@ -232,7 +233,7 @@
     }
 
     private int handleEnableIms() {
-        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        int slotId = getDefaultSlot();
         String opt;
         while ((opt = getNextOption()) != null) {
             switch (opt) {
@@ -259,7 +260,7 @@
     }
 
     private int handleDisableIms() {
-        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        int slotId = getDefaultSlot();
         String opt;
         while ((opt = getNextOption()) != null) {
             switch (opt) {
@@ -285,4 +286,14 @@
         }
         return 0;
     }
+
+    private int getDefaultSlot() {
+        int slotId = SubscriptionManager.getDefaultVoicePhoneId();
+        if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX
+                || slotId == SubscriptionManager.DEFAULT_PHONE_INDEX) {
+            // If there is no default, default to slot 0.
+            slotId = DEFAULT_PHONE_ID;
+        }
+        return slotId;
+    }
 }
diff --git a/src/com/android/services/telephony/TelephonyConnection.java b/src/com/android/services/telephony/TelephonyConnection.java
index 55a13bb..b81eeab 100644
--- a/src/com/android/services/telephony/TelephonyConnection.java
+++ b/src/com/android/services/telephony/TelephonyConnection.java
@@ -538,6 +538,7 @@
         @Override
         public void onRttModifyResponseReceived(int status) {
             updateConnectionProperties();
+            refreshConferenceSupported();
             if (status == RttModifyStatus.SESSION_MODIFY_REQUEST_SUCCESS) {
                 sendRttInitiationSuccess();
             } else {
@@ -554,7 +555,12 @@
 
         @Override
         public void onRttInitiated() {
-            updateConnectionProperties();
+            if (mOriginalConnection != null) {
+                // if mOriginalConnection is null, the properties will get set when
+                // mOriginalConnection gets set.
+                updateConnectionProperties();
+                refreshConferenceSupported();
+            }
             sendRttInitiationSuccess();
         }
 
@@ -831,7 +837,11 @@
     public void onStartRtt(RttTextStream textStream) {
         if (isImsConnection()) {
             ImsPhoneConnection originalConnection = (ImsPhoneConnection) mOriginalConnection;
-            originalConnection.sendRttModifyRequest(textStream);
+            if (originalConnection.isRttEnabledForCall()) {
+                originalConnection.setCurrentRttTextStream(textStream);
+            } else {
+                originalConnection.sendRttModifyRequest(textStream);
+            }
         } else {
             Log.w(this, "onStartRtt - not in IMS, so RTT cannot be enabled.");
         }
@@ -2090,6 +2100,9 @@
         if (mTreatAsEmergencyCall) {
             isConferenceSupported = false;
             Log.d(this, "refreshConferenceSupported = false; emergency call");
+        } else if (isRtt()) {
+            isConferenceSupported = false;
+            Log.d(this, "refreshConferenceSupported = false; rtt call");
         } else if (!isConferencingSupported || isIms && !isImsConferencingSupported) {
             isConferenceSupported = false;
             Log.d(this, "refreshConferenceSupported = false; carrier doesn't support conf.");
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index b3369b5..c7b2096 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -1037,6 +1037,8 @@
             }
             connection.setDisconnected(DisconnectCauseUtil.toTelecomDisconnectCause(
                     cause, e.getMessage()));
+            connection.clearOriginalConnection();
+            connection.destroy();
             return;
         }
 
@@ -1059,6 +1061,8 @@
             Log.d(this, "placeOutgoingConnection, phone.dial returned null");
             connection.setDisconnected(DisconnectCauseUtil.toTelecomDisconnectCause(
                     telephonyDisconnectCause, "Connection is null"));
+            connection.clearOriginalConnection();
+            connection.destroy();
         } else {
             connection.setOriginalConnection(originalConnection);
         }