Merge "Revert "Fixed the set/getAllowedNetworkTypes debug messages""
diff --git a/ecc/input/eccdata.txt b/ecc/input/eccdata.txt
index e1c7306..6988f14 100644
--- a/ecc/input/eccdata.txt
+++ b/ecc/input/eccdata.txt
@@ -129,10 +129,35 @@
 countries {
   iso_code: "AT"
   eccs {
-    phone_number: "112"
-    types: POLICE
-    types: AMBULANCE
-    types: FIRE
+      phone_number: "112"
+      types: POLICE
+      types: AMBULANCE
+      types: FIRE
+  }
+  eccs {
+      phone_number: "122"
+      types: FIRE
+      routing: NORMAL
+  }
+  eccs {
+      phone_number: "133"
+      types: POLICE
+      routing: NORMAL
+  }
+  eccs {
+      phone_number: "144"
+      types: AMBULANCE
+      routing: NORMAL
+  }
+  eccs {
+      phone_number: "128"
+      types: MOUNTAIN_RESCUE
+      routing: NORMAL
+  }
+  eccs {
+      phone_number: "140"
+      types: TYPE_UNSPECIFIED
+      routing: NORMAL
   }
   ecc_fallback: "112"
 }
@@ -333,14 +358,22 @@
   eccs {
     phone_number: "190"
     types: POLICE
+    routing: EMERGENCY
   }
   eccs {
     phone_number: "192"
     types: AMBULANCE
+    routing: NORMAL
   }
   eccs {
     phone_number: "193"
     types: FIRE
+    routing: NORMAL
+  }
+  eccs {
+    phone_number: "185"
+    types: MARINE_GUARD
+    routing: NORMAL
   }
   ecc_fallback: "911"
 }
@@ -420,6 +453,12 @@
     types: AMBULANCE
     types: FIRE
   }
+  eccs {
+      phone_number: "112"
+      types: POLICE
+      types: AMBULANCE
+      types: FIRE
+   }
   ecc_fallback: "911"
 }
 countries {
@@ -451,16 +490,57 @@
 countries {
   iso_code: "CH"
   eccs {
+      phone_number: "112"
+      types: POLICE
+      types: AMBULANCE
+      types: FIRE
+      routing: EMERGENCY
+  }
+  eccs {
+        phone_number: "911"
+        types: POLICE
+        types: AMBULANCE
+        types: FIRE
+        routing: EMERGENCY
+    }
+  eccs {
     phone_number: "117"
     types: POLICE
+    routing: EMERGENCY
   }
   eccs {
     phone_number: "144"
     types: AMBULANCE
+    routing: EMERGENCY
   }
   eccs {
     phone_number: "118"
     types: FIRE
+    routing: EMERGENCY
+  }
+  eccs {
+      phone_number: "143"
+      types: TYPE_UNSPECIFIED
+      routing: NORMAL
+  }
+  eccs {
+      phone_number: "145"
+      types: TYPE_UNSPECIFIED
+      routing: NORMAL
+  }
+   eccs {
+      phone_number: "147"
+      types: TYPE_UNSPECIFIED
+      routing: NORMAL
+  }
+   eccs {
+      phone_number: "1414"
+      types: TYPE_UNSPECIFIED
+  }
+   eccs {
+      phone_number: "0800117117"
+      types: TYPE_UNSPECIFIED
+      routing: NORMAL
   }
   ecc_fallback: "112"
 }
@@ -735,6 +815,47 @@
     types: POLICE
     types: AMBULANCE
     types: FIRE
+    routing: EMERGENCY
+  }
+  eccs {
+      phone_number: "088"
+      types: POLICE
+      routing: EMERGENCY
+  }
+  eccs {
+      phone_number: "085"
+      types: FIRE
+      routing: EMERGENCY
+  }
+  eccs {
+      phone_number: "1006"
+      types: TYPE_UNSPECIFIED
+      routing: EMERGENCY
+  }
+  eccs {
+      phone_number: "061"
+      types: TYPE_UNSPECIFIED
+      routing: EMERGENCY
+  }
+  eccs {
+      phone_number: "062"
+      types: TYPE_UNSPECIFIED
+      routing: EMERGENCY
+  }
+  eccs {
+      phone_number: "080"
+      types: FIRE
+      routing: EMERGENCY
+  }
+  eccs {
+       phone_number: "091"
+       types: POLICE
+       routing: EMERGENCY
+  }
+  eccs {
+       phone_number: "092"
+       types: POLICE
+       routing: EMERGENCY
   }
   ecc_fallback: "112"
 }
@@ -889,6 +1010,7 @@
     types: POLICE
     types: AMBULANCE
     types: FIRE
+    routing: EMERGENCY
   }
   ecc_fallback: "112"
 }
@@ -2298,7 +2420,38 @@
     types: POLICE
     types: AMBULANCE
     types: FIRE
+    routing: EMERGENCY
   }
+  eccs {
+    phone_number: "101"
+    types: FIRE
+    routing: EMERGENCY
+  }
+  eccs {
+    phone_number: "102"
+    types: POLICE
+    routing: EMERGENCY
+  }
+  eccs {
+    phone_number: "103"
+    types: AMBULANCE
+    routing: EMERGENCY
+  }
+  eccs {
+    phone_number: "104"
+    types: TYPE_UNSPECIFIED
+    routing: EMERGENCY
+  }
+  eccs {
+    phone_number: "121"
+    types: TYPE_UNSPECIFIED
+    routing: EMERGENCY
+  }
+  eccs {
+    phone_number: "123"
+    types: TYPE_UNSPECIFIED
+    routing: EMERGENCY
+    }
   ecc_fallback: "112"
 }
 countries {
diff --git a/ecc/output/eccdata b/ecc/output/eccdata
index 8ab89d6..8b4f49b 100644
--- a/ecc/output/eccdata
+++ b/ecc/output/eccdata
Binary files differ
diff --git a/src/com/android/phone/CarrierConfigLoader.java b/src/com/android/phone/CarrierConfigLoader.java
index 58b8299..5ba148d 100644
--- a/src/com/android/phone/CarrierConfigLoader.java
+++ b/src/com/android/phone/CarrierConfigLoader.java
@@ -777,6 +777,7 @@
     }
 
     private void updateSubscriptionDatabase(int phoneId) {
+        logd("updateSubscriptionDatabase: phoneId=" + phoneId);
         String configPackageName;
         PersistableBundle configToSend;
         int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
diff --git a/src/com/android/phone/EmergencyCallbackModeExitDialog.java b/src/com/android/phone/EmergencyCallbackModeExitDialog.java
index fc0e513..6901789 100644
--- a/src/com/android/phone/EmergencyCallbackModeExitDialog.java
+++ b/src/com/android/phone/EmergencyCallbackModeExitDialog.java
@@ -43,6 +43,8 @@
 
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.TelephonyIntents;
+import com.android.internal.telephony.domainselection.DomainSelectionResolver;
+import com.android.internal.telephony.emergency.EmergencyStateTracker;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -244,9 +246,16 @@
                     .setMessage(text)
                     .setPositiveButton(R.string.alert_dialog_yes,
                             new DialogInterface.OnClickListener() {
-                                public void onClick(DialogInterface dialog,int whichButton) {
+                                public void onClick(DialogInterface dialog,
+                                        int whichButton) {
                                     // User clicked Yes. Exit Emergency Callback Mode.
-                                    mPhone.exitEmergencyCallbackMode();
+                                    if (DomainSelectionResolver.getInstance()
+                                            .isDomainSelectionSupported()) {
+                                        EmergencyStateTracker.getInstance()
+                                                .exitEmergencyCallbackMode();
+                                    } else {
+                                        mPhone.exitEmergencyCallbackMode();
+                                    }
 
                                     // Show progress dialog
                                     showDialog(EXIT_ECM_PROGRESS_DIALOG);
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 68956bd..4a89bd6 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -532,7 +532,17 @@
      */
     private final TelephonyConnection.TelephonyConnectionListener mEmergencyConnectionListener =
             new TelephonyConnection.TelephonyConnectionListener() {
-        @Override
+                @Override
+                public void onOriginalConnectionConfigured(TelephonyConnection c) {
+                    com.android.internal.telephony.Connection origConn = c.getOriginalConnection();
+                    if (origConn == null) return;
+                    // Update the domain in the case that it changes,for example during initial
+                    // setup or when there was an srvcc or internal redial.
+                    mEmergencyStateTracker.onEmergencyCallDomainUpdated(
+                            origConn.getPhoneType(), c.getTelecomCallId());
+                }
+
+                @Override
         public void onStateChanged(Connection connection, @Connection.ConnectionState int state) {
             if (connection != null) {
                 TelephonyConnection c = (TelephonyConnection) connection;
diff --git a/src/com/android/services/telephony/domainselection/EmergencyCallDomainSelector.java b/src/com/android/services/telephony/domainselection/EmergencyCallDomainSelector.java
index 53a60af..8100e63 100644
--- a/src/com/android/services/telephony/domainselection/EmergencyCallDomainSelector.java
+++ b/src/com/android/services/telephony/domainselection/EmergencyCallDomainSelector.java
@@ -274,12 +274,6 @@
                 onWwanNetworkTypeSelected(mCsNetworkType);
                 return;
             }
-        } else if (getImsNetworkTypeConfiguration().isEmpty()
-                || (mRequiresVoLteEnabled && !isAdvancedCallingSettingEnabled())) {
-            // Emergency call over IMS is not supported.
-            mCsNetworkType = UTRAN;
-            onWwanNetworkTypeSelected(mCsNetworkType);
-            return;
         }
 
         if (mLastTransportType == TRANSPORT_TYPE_WLAN) {
@@ -477,14 +471,6 @@
     }
 
     private void selectDomainFromInitialState() {
-        if (getImsNetworkTypeConfiguration().isEmpty()
-                || (mRequiresVoLteEnabled && !isAdvancedCallingSettingEnabled())) {
-            // Emergency call over IMS is not supported.
-            mCsNetworkType = UTRAN;
-            onWwanNetworkTypeSelected(mCsNetworkType);
-            return;
-        }
-
         boolean csInService = isCsInService();
         boolean psInService = isPsInService();
 
@@ -613,6 +599,12 @@
      * @return The list of preferred network types.
      */
     private @RadioAccessNetworkType List<Integer> getNextPreferredNetworks(boolean csPreferred) {
+        if (mRequiresVoLteEnabled && !isAdvancedCallingSettingEnabled()) {
+            // Emergency call over IMS is not supported.
+            logi("getNextPreferredNetworks VoLte setting is not enabled.");
+            return generatePreferredNetworks(getCsNetworkTypeConfiguration());
+        }
+
         List<Integer> preferredNetworks = new ArrayList<>();
 
         List<Integer> domains = getDomainPreference();
@@ -791,6 +783,11 @@
         EmergencyRegResult regResult = mSelectionAttributes.getEmergencyRegResult();
         logi("getSelectablePsNetworkType regResult=" + regResult);
         if (regResult == null) return UNKNOWN;
+        if (mRequiresVoLteEnabled && !isAdvancedCallingSettingEnabled()) {
+            // Emergency call over IMS is not supported.
+            logi("getSelectablePsNetworkType VoLte setting is not enabled.");
+            return UNKNOWN;
+        }
 
         int accessNetwork = regResult.getAccessNetwork();
         List<Integer> ratList = getImsNetworkTypeConfiguration();
diff --git a/tests/src/com/android/phone/CarrierConfigLoaderTest.java b/tests/src/com/android/phone/CarrierConfigLoaderTest.java
index 6807422..45a53f7 100644
--- a/tests/src/com/android/phone/CarrierConfigLoaderTest.java
+++ b/tests/src/com/android/phone/CarrierConfigLoaderTest.java
@@ -54,7 +54,9 @@
 
 import com.android.TelephonyTestBase;
 import com.android.internal.telephony.IccCardConstants;
+import com.android.internal.telephony.PhoneFactory;
 import com.android.internal.telephony.SubscriptionInfoUpdater;
+import com.android.internal.telephony.subscription.SubscriptionManagerService;
 
 import org.junit.After;
 import org.junit.Before;
@@ -87,6 +89,7 @@
     @Mock PackageManager mPackageManager;
     @Mock PackageInfo mPackageInfo;
     @Mock SubscriptionInfoUpdater mSubscriptionInfoUpdater;
+    @Mock SubscriptionManagerService mSubscriptionManagerService;
     @Mock SharedPreferences mSharedPreferences;
     @Mock TelephonyRegistryManager mTelephonyRegistryManager;
 
@@ -100,6 +103,9 @@
     public void setUp() throws Exception {
         super.setUp();
 
+        replaceInstance(SubscriptionManagerService.class, "sInstance", null,
+                mSubscriptionManagerService);
+
         // TODO: replace doReturn/when with when/thenReturn which is more readable
         doReturn(mSharedPreferences).when(mContext).getSharedPreferences(anyString(), anyInt());
         doReturn(Build.FINGERPRINT).when(mSharedPreferences).getString(eq("build_fingerprint"),
@@ -271,9 +277,15 @@
         mTestableLooper.processAllMessages();
 
         assertThat(mCarrierConfigLoader.getOverrideConfig(DEFAULT_PHONE_ID).isEmpty()).isTrue();
-        verify(mSubscriptionInfoUpdater).updateSubscriptionByCarrierConfigAndNotifyComplete(
-                eq(DEFAULT_PHONE_ID), eq(PLATFORM_CARRIER_CONFIG_PACKAGE),
-                any(PersistableBundle.class), any(Message.class));
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            verify(mSubscriptionManagerService).updateSubscriptionByCarrierConfig(
+                    eq(DEFAULT_PHONE_ID), eq(PLATFORM_CARRIER_CONFIG_PACKAGE),
+                    any(PersistableBundle.class), any(Runnable.class));
+        } else {
+            verify(mSubscriptionInfoUpdater).updateSubscriptionByCarrierConfigAndNotifyComplete(
+                    eq(DEFAULT_PHONE_ID), eq(PLATFORM_CARRIER_CONFIG_PACKAGE),
+                    any(PersistableBundle.class), any(Message.class));
+        }
     }
 
     /**
@@ -294,9 +306,15 @@
 
         assertThat(mCarrierConfigLoader.getOverrideConfig(DEFAULT_PHONE_ID).getInt(
                 CARRIER_CONFIG_EXAMPLE_KEY)).isEqualTo(CARRIER_CONFIG_EXAMPLE_VALUE);
-        verify(mSubscriptionInfoUpdater).updateSubscriptionByCarrierConfigAndNotifyComplete(
-                eq(DEFAULT_PHONE_ID), eq(PLATFORM_CARRIER_CONFIG_PACKAGE),
-                any(PersistableBundle.class), any(Message.class));
+        if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
+            verify(mSubscriptionManagerService).updateSubscriptionByCarrierConfig(
+                    eq(DEFAULT_PHONE_ID), eq(PLATFORM_CARRIER_CONFIG_PACKAGE),
+                    any(PersistableBundle.class), any(Runnable.class));
+        } else {
+            verify(mSubscriptionInfoUpdater).updateSubscriptionByCarrierConfigAndNotifyComplete(
+                    eq(DEFAULT_PHONE_ID), eq(PLATFORM_CARRIER_CONFIG_PACKAGE),
+                    any(PersistableBundle.class), any(Message.class));
+        }
     }
 
     /**
diff --git a/tests/src/com/android/services/telephony/domainselection/EmergencyCallDomainSelectorTest.java b/tests/src/com/android/services/telephony/domainselection/EmergencyCallDomainSelectorTest.java
index 7a01004..aedfe9e 100644
--- a/tests/src/com/android/services/telephony/domainselection/EmergencyCallDomainSelectorTest.java
+++ b/tests/src/com/android/services/telephony/domainselection/EmergencyCallDomainSelectorTest.java
@@ -864,7 +864,7 @@
     }
 
     @Test
-    public void testVoLteOffEpsImsNotRegisteredSelectCs() throws Exception {
+    public void testVoLteOffEpsImsNotRegisteredScanCsPreferred() throws Exception {
         PersistableBundle bundle = getDefaultPersistableBundle();
         bundle.putBoolean(KEY_EMERGENCY_REQUIRES_VOLTE_ENABLED_BOOL, true);
         when(mCarrierConfigManager.getConfigForSubId(anyInt())).thenReturn(bundle);
@@ -885,7 +885,7 @@
         bindImsServiceUnregistered();
 
         // Requires VoLTE enabled but VoLTE is'nt enabled.
-        verifyCsDialed();
+        verifyScanCsPreferred();
     }
 
     @Test