Do not use stale PhoneAccounts when placing calls.

This is important in case the call log has a record of a phone account
which is no longer valid.  An example would be the emergency
PhoneAccount. That will always fail for non-emergency numbers...but if
it is the only account, then it will be used and fail. When the user
pops in a valid SIM, it will continue to fail because the phone account
saved into the call log was the stale emergency phoneAccount.

Without this fix, the user can get "Call not sent" frequently.

Bug: 16526102
Change-Id: Ic08768b85d374e6f7ab54f34b5caf501b061d49b
diff --git a/src/com/android/telecomm/CallsManager.java b/src/com/android/telecomm/CallsManager.java
index dd126df..8d6d772 100644
--- a/src/com/android/telecomm/CallsManager.java
+++ b/src/com/android/telecomm/CallsManager.java
@@ -29,6 +29,7 @@
 import com.google.common.collect.ImmutableList;
 
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 import java.util.concurrent.CopyOnWriteArraySet;
 
@@ -288,6 +289,7 @@
     void placeOutgoingCall(Uri handle, GatewayInfo gatewayInfo, PhoneAccountHandle accountHandle,
             boolean speakerphoneOn, int videoState) {
 
+        TelecommApp app = TelecommApp.getInstance();
         final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
 
         if (gatewayInfo == null) {
@@ -297,6 +299,16 @@
                     Log.pii(uriHandle), Log.pii(handle));
         }
 
+        // Only dial with the requested phoneAccount if it is still valid. Otherwise treat this call
+        // as if a phoneAccount was not specified (does the default behavior instead).
+        if (accountHandle != null) {
+            List<PhoneAccountHandle> enabledAccounts =
+                    app.getPhoneAccountRegistrar().getEnabledPhoneAccounts();
+            if (!enabledAccounts.contains(accountHandle)) {
+                accountHandle = null;
+            }
+        }
+
         Call call = new Call(
                 mConnectionServiceRepository,
                 uriHandle,
@@ -312,17 +324,17 @@
         addCall(call);
 
         // This block of code will attempt to pre-determine a phone account
-        final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(
-                TelecommApp.getInstance(), call.getHandle());
+        final boolean emergencyCall = TelephonyUtil.shouldProcessAsEmergency(app, call.getHandle());
         if (emergencyCall) {
             // Emergency -- CreateConnectionProcessor will choose accounts automatically
             call.setPhoneAccount(null);
         } else if (accountHandle != null) {
+            Log.d(this, "CALL with phone account: " + accountHandle);
             call.setPhoneAccount(accountHandle);
         } else {
             // No preset account, check if default exists
-            PhoneAccountHandle defaultAccountHandle = TelecommApp.getInstance()
-                    .getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount();
+            PhoneAccountHandle defaultAccountHandle =
+                    app.getPhoneAccountRegistrar().getDefaultOutgoingPhoneAccount();
             if (defaultAccountHandle != null) {
                 call.setPhoneAccount(defaultAccountHandle);
             }