Fix phone account preference removal in telephony.

Additionally add/maintain following behavior changes:
For multi-SIM devices:
  - Always show the account list (even if only SIM-based accounts)
  - Always list the SIM accounts
For single-SIM devices
  - If there are no 3rd party (3P) accounts, do not show the account
    list nor any options (make-calls-with, enable-accounts-list)
  - If there are only disabled 3P accounts, show the "enable accounts"
    preference but do not list any accounts (including SIM)
  - If there is at least one enabled 3P account display all options
    and list all accounts, including SIM-based accounts.

Bug: 20303449
Change-Id: I0d36a2fab0abea01853003ef60cce48cf4f1f6c6
diff --git a/res/xml/phone_account_settings.xml b/res/xml/phone_account_settings.xml
index 82e7a87..f0458cd 100644
--- a/res/xml/phone_account_settings.xml
+++ b/res/xml/phone_account_settings.xml
@@ -29,16 +29,16 @@
             android:persistent="false"
             android:order="1" />
 
-    <PreferenceScreen
-        android:title="@string/phone_accounts_all_calling_accounts"
-        android:summary="@string/phone_accounts_all_calling_accounts_summary"
-        android:persistent="false"
-        android:order="1000" >
+        <PreferenceScreen
+            android:title="@string/phone_accounts_all_calling_accounts"
+            android:summary="@string/phone_accounts_all_calling_accounts_summary"
+            android:persistent="false"
+            android:order="1000" >
 
-        <intent android:action="android.intent.action.MAIN"
-            android:targetPackage="com.android.server.telecom"
-            android:targetClass="com.android.server.telecom.settings.EnableAccountPreferenceActivity" />
-    </PreferenceScreen>
+            <intent android:action="android.intent.action.MAIN"
+                android:targetPackage="com.android.server.telecom"
+                android:targetClass="com.android.server.telecom.settings.EnableAccountPreferenceActivity" />
+        </PreferenceScreen>
 
     </PreferenceCategory>
 
diff --git a/src/com/android/phone/settings/PhoneAccountSettingsFragment.java b/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
index c7e6c7b..ce738b6 100644
--- a/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
+++ b/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
@@ -114,12 +114,12 @@
             if (mTelecomManager.getCallCapablePhoneAccounts().size() > 1) {
                 updateDefaultOutgoingAccountsModel();
             } else {
-                getPreferenceScreen().removePreference(mDefaultOutgoingAccount);
+                mAccountList.removePreference(mDefaultOutgoingAccount);
             }
 
             Preference allAccounts = getPreferenceScreen().findPreference(ALL_CALLING_ACCOUNTS_KEY);
-            if (getNonSimCallingAccounts().size() == 0 && allAccounts != null) {
-                getPreferenceScreen().removePreference(allAccounts);
+            if (getNonSimCallingAccounts(true).isEmpty() && allAccounts != null) {
+                mAccountList.removePreference(allAccounts);
             }
         } else {
             getPreferenceScreen().removePreference(mAccountList);
@@ -330,6 +330,16 @@
     }
 
     private void initAccountList() {
+        boolean isMultiSimDevice = mTelephonyManager.isMultiSimEnabled();
+
+        // On a single-SIM device, do not list any accounts if the only account is the SIM-based
+        // one. This is because on single-SIM devices, we do not expose SIM settings through the
+        // account listing entry so showing it does nothing to help the user. Nor does the lack of
+        // action match the "Settings" header above the listing.
+        if (!isMultiSimDevice && getNonSimCallingAccounts(false).isEmpty()) {
+            return;
+        }
+
         // Obtain the list of phone accounts.
         List<PhoneAccount> accounts = new ArrayList<>();
         for (PhoneAccountHandle handle : mTelecomManager.getCallCapablePhoneAccounts()) {
@@ -346,10 +356,8 @@
                 int retval = 0;
 
                 // SIM accounts go first
-                boolean isSim1 = (account1.getCapabilities() &
-                        PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0;
-                boolean isSim2 = (account2.getCapabilities() &
-                        PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0;
+                boolean isSim1 = account1.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
+                boolean isSim2 = account2.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
                 if (isSim1 != isSim2) {
                     retval = isSim1 ? -1 : 1;
                 }
@@ -382,18 +390,22 @@
         for (PhoneAccount account : accounts) {
             PhoneAccountHandle handle = account.getAccountHandle();
             Intent intent = null;
-            boolean isSimAccount = false;
 
             // SIM phone accounts use a different setting intent and are thus handled differently.
-            if ((PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION & account.getCapabilities()) != 0) {
-                isSimAccount = true;
-                SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(
-                        mTelephonyManager.getSubIdForPhoneAccount(account));
+            if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
 
-                if (subInfo != null) {
-                    intent = new Intent(TelecomManager.ACTION_SHOW_CALL_SETTINGS);
-                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-                    SubscriptionInfoHelper.addExtrasToIntent(intent, subInfo);
+                // For SIM-based accounts, we only expose the settings through the account list
+                // if we are on a multi-SIM device. For single-SIM devices, the settings are
+                // more spread out so there is no good single place to take the user, so we don't.
+                if (isMultiSimDevice) {
+                    SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(
+                            mTelephonyManager.getSubIdForPhoneAccount(account));
+
+                    if (subInfo != null) {
+                        intent = new Intent(TelecomManager.ACTION_SHOW_CALL_SETTINGS);
+                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+                        SubscriptionInfoHelper.addExtrasToIntent(intent, subInfo);
+                    }
                 }
             } else {
                 // Build the settings intent.
@@ -443,17 +455,17 @@
 
     private boolean shouldShowConnectionServiceList() {
         return mTelephonyManager.isMultiSimEnabled() ||
-            getNonSimCallingAccounts().size() > 0;
+            getNonSimCallingAccounts(true).size() > 0;
     }
 
-    private List<PhoneAccountHandle> getNonSimCallingAccounts() {
+    private List<PhoneAccountHandle> getNonSimCallingAccounts(boolean includeDisabledAccounts) {
         List<PhoneAccountHandle> accountHandles =
-                mTelecomManager.getCallCapablePhoneAccounts();
+                mTelecomManager.getCallCapablePhoneAccounts(includeDisabledAccounts);
         for (Iterator<PhoneAccountHandle> i = accountHandles.iterator(); i.hasNext();) {
             PhoneAccountHandle handle = i.next();
             PhoneAccount account = mTelecomManager.getPhoneAccount(handle);
-            if (account == null || (account.getCapabilities() &
-                    PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0) {
+            if (account == null ||
+                    (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION))) {
                 // If the account is no longer valid OR the account is a built-in SIM account,
                 // remove!
                 i.remove();