Merge "Fix issue where device does not create any phone accounts."
diff --git a/src/com/android/services/telephony/TelecomAccountRegistry.java b/src/com/android/services/telephony/TelecomAccountRegistry.java
index 3505fe7..6bbcdc4 100644
--- a/src/com/android/services/telephony/TelecomAccountRegistry.java
+++ b/src/com/android/services/telephony/TelecomAccountRegistry.java
@@ -790,6 +790,7 @@
         @Override
         public void onSubscriptionsChanged() {
             // Any time the SubscriptionInfo changes...rerun the setup
+            Log.i(this, "onSubscriptionsChanged - update accounts");
             tearDownAccounts();
             setupAccounts();
         }
@@ -1146,37 +1147,48 @@
         // Go through SIM-based phones and register ourselves -- registering an existing account
         // will cause the existing entry to be replaced.
         Phone[] phones = PhoneFactory.getPhones();
-        Log.d(this, "Found %d phones.  Attempting to register.", phones.length);
+        Log.i(this, "setupAccounts: Found %d phones.  Attempting to register.", phones.length);
 
         final boolean phoneAccountsEnabled = mContext.getResources().getBoolean(
                 R.bool.config_pstn_phone_accounts_enabled);
 
         synchronized (mAccountsLock) {
-            if (phoneAccountsEnabled) {
-                for (Phone phone : phones) {
-                    int subscriptionId = phone.getSubId();
-                    Log.d(this, "Phone with subscription id %d", subscriptionId);
-                    // setupAccounts can be called multiple times during service changes. Don't add an
-                    // account if the Icc has not been set yet.
-                    if (!SubscriptionManager.isValidSubscriptionId(subscriptionId)
-                            || phone.getFullIccSerialNumber() == null) return;
-                    // Don't add account if it's opportunistic subscription, which is considered
-                    // data only for now.
-                    SubscriptionInfo info = SubscriptionManager.from(mContext)
-                            .getActiveSubscriptionInfo(subscriptionId);
-                    if (info == null || info.isOpportunistic()) return;
+            try {
+                if (phoneAccountsEnabled) {
+                    for (Phone phone : phones) {
+                        int subscriptionId = phone.getSubId();
+                        Log.i(this, "setupAccounts: Phone with subscription id %d", subscriptionId);
+                        // setupAccounts can be called multiple times during service changes.
+                        // Don't add an account if the Icc has not been set yet.
+                        if (!SubscriptionManager.isValidSubscriptionId(subscriptionId)
+                                || phone.getFullIccSerialNumber() == null) {
+                            Log.d(this, "setupAccounts: skipping invalid subid %d", subscriptionId);
+                            continue;
+                        }
+                        // Don't add account if it's opportunistic subscription, which is considered
+                        // data only for now.
+                        SubscriptionInfo info = SubscriptionManager.from(mContext)
+                                .getActiveSubscriptionInfo(subscriptionId);
+                        if (info == null || info.isOpportunistic()) {
+                            Log.d(this, "setupAccounts: skipping unknown or opportunistic subid %d",
+                                    subscriptionId);
+                            continue;
+                        }
 
-                    mAccounts.add(new AccountEntry(phone, false /* emergency */,
-                            false /* isDummy */));
+                        mAccounts.add(new AccountEntry(phone, false /* emergency */,
+                                false /* isDummy */));
+                    }
                 }
-            }
-
-            // If we did not list ANY accounts, we need to provide a "default" SIM account
-            // for emergency numbers since no actual SIM is needed for dialing emergency
-            // numbers but a phone account is.
-            if (mAccounts.isEmpty()) {
-                mAccounts.add(new AccountEntry(PhoneFactory.getDefaultPhone(), true /* emergency */,
-                        false /* isDummy */));
+            } finally {
+                // If we did not list ANY accounts, we need to provide a "default" SIM account
+                // for emergency numbers since no actual SIM is needed for dialing emergency
+                // numbers but a phone account is.
+                if (mAccounts.isEmpty()) {
+                    Log.i(this, "setupAccounts: adding default");
+                    mAccounts.add(
+                            new AccountEntry(PhoneFactory.getDefaultPhone(), true /* emergency */,
+                                    false /* isDummy */));
+                }
             }
 
             // Add a fake account entry.