am addca877: am 7258f5c5: Merge "Simplify call assistant setting" into lmp-mr1-dev

* commit 'addca877e8816de9a01b2c9114113b296e7c21bc':
  Simplify call assistant setting
diff --git a/res/xml/phone_account_settings.xml b/res/xml/phone_account_settings.xml
index bf62ed0..18b75e6 100644
--- a/res/xml/phone_account_settings.xml
+++ b/res/xml/phone_account_settings.xml
@@ -36,7 +36,13 @@
         <com.android.phone.settings.AccountSelectionPreference
             android:key="wifi_calling_call_assistant_preference"
             android:title="@string/wifi_calling_call_assistant"
-            android:summary="@string/wifi_calling_call_assistant_none" />
+            android:summary="@string/wifi_calling_call_assistant_none"
+            android:persistent="false" />
+
+        <SwitchPreference
+            android:key="wifi_calling_call_assistant_switch"
+            android:title="@string/wifi_calling_call_assistant"
+            android:persistent="false" />
 
         <Preference
             android:key="wifi_calling_configure_call_assistant_preference"
diff --git a/src/com/android/phone/settings/PhoneAccountSettingsFragment.java b/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
index ce517ee..3869294 100644
--- a/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
+++ b/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
@@ -10,6 +10,7 @@
 import android.preference.Preference;
 import android.preference.PreferenceCategory;
 import android.preference.PreferenceFragment;
+import android.preference.SwitchPreference;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
 import android.telephony.SubscriptionInfo;
@@ -41,6 +42,8 @@
             "phone_accounts_call_assistant_settings_category_key";
     private static final String SELECT_CALL_ASSISTANT_PREF_KEY =
             "wifi_calling_call_assistant_preference";
+    private static final String SELECT_CALL_ASSISTANT_SWITCH_KEY =
+            "wifi_calling_call_assistant_switch";
 
     private static final String SIP_SETTINGS_CATEGORY_PREF_KEY =
             "phone_accounts_sip_settings_category_key";
@@ -56,6 +59,7 @@
 
     private AccountSelectionPreference mDefaultOutgoingAccount;
     private AccountSelectionPreference mSelectCallAssistant;
+    private SwitchPreference mCallAssistantSwitch;
     private Preference mConfigureCallAssistant;
 
     private ListPreference mUseSipCalling;
@@ -99,20 +103,44 @@
             getPreferenceScreen().removePreference(mDefaultOutgoingAccount);
         }
 
-        if (!mTelecomManager.getSimCallManagers().isEmpty()) {
-            mSelectCallAssistant = (AccountSelectionPreference)
-                    getPreferenceScreen().findPreference(SELECT_CALL_ASSISTANT_PREF_KEY);
-            mSelectCallAssistant.setListener(this);
-            mSelectCallAssistant.setDialogTitle(
-                    R.string.wifi_calling_select_call_assistant_summary);
+        List<PhoneAccountHandle> simCallManagers = mTelecomManager.getSimCallManagers();
+        PreferenceCategory callAssistantCategory = (PreferenceCategory)
+                getPreferenceScreen().findPreference(CALL_ASSISTANT_CATEGORY_PREF_KEY);
+        if (simCallManagers.isEmpty()) {
+            getPreferenceScreen().removePreference(callAssistantCategory);
+        } else {
+            if (simCallManagers.size() == 1) {
+                // If there's only a single call assistant then display an ON/OFF switch. Turning
+                // the switch on enables the call assistant and turning it off disables it.
+                callAssistantCategory.removePreference(
+                        getPreferenceScreen().findPreference(SELECT_CALL_ASSISTANT_PREF_KEY));
+                CharSequence title =
+                    mTelecomManager.getPhoneAccount(simCallManagers.get(0)).getLabel();
+                callAssistantCategory.setTitle(title);
+                mCallAssistantSwitch = (SwitchPreference)
+                        getPreferenceScreen().findPreference(SELECT_CALL_ASSISTANT_SWITCH_KEY);
+                mCallAssistantSwitch.setTitle(title);
+                mCallAssistantSwitch.setChecked(simCallManagers.get(0).equals(
+                        mTelecomManager.getSimCallManager()));
+                mCallAssistantSwitch.setOnPreferenceChangeListener(this);
+                mCallAssistantSwitch.setEnabled(true);
+            } else {
+                // If there's more than one call assistant then display a list. Choosing an item
+                // from the list enables the corresponding call assistant.
+                callAssistantCategory.removePreference(
+                        getPreferenceScreen().findPreference(SELECT_CALL_ASSISTANT_SWITCH_KEY));
+                mSelectCallAssistant = (AccountSelectionPreference)
+                        getPreferenceScreen().findPreference(SELECT_CALL_ASSISTANT_PREF_KEY);
+                mSelectCallAssistant.setListener(this);
+                mSelectCallAssistant.setDialogTitle(
+                        R.string.wifi_calling_select_call_assistant_summary);
+                updateCallAssistantModel();
+            }
 
             mConfigureCallAssistant =
                     getPreferenceScreen().findPreference(CONFIGURE_CALL_ASSISTANT_PREF_KEY);
             mConfigureCallAssistant.setOnPreferenceClickListener(this);
-            updateCallAssistantModel();
-        } else {
-            getPreferenceScreen().removePreference(
-                    getPreferenceScreen().findPreference(CALL_ASSISTANT_CATEGORY_PREF_KEY));
+            updateConfigureCallAssistantSummary();
         }
 
         if (SipUtil.isVoipSupported(getActivity())) {
@@ -172,6 +200,20 @@
                 }
             }).start();
             return true;
+        } else if (pref == mCallAssistantSwitch) {
+            List<PhoneAccountHandle> simCallManagers = mTelecomManager.getSimCallManagers();
+            if (simCallManagers.size() == 1) {
+                if (Boolean.TRUE.equals(objValue)) {
+                    mTelecomManager.setSimCallManager(simCallManagers.get(0));
+                } else {
+                    mTelecomManager.setSimCallManager(null);
+                }
+                updateConfigureCallAssistantSummary();
+            } else {
+                Log.w(LOG_TAG, "Single call assistant expected but " + simCallManagers.size()
+                    + " found. Ignoring preference change.");
+            }
+            return true;
         }
         return false;
     }
@@ -229,6 +271,7 @@
             updateDefaultOutgoingAccountsModel();
         } else if (pref == mSelectCallAssistant) {
             updateCallAssistantModel();
+            updateConfigureCallAssistantSummary();
         }
     }
 
@@ -281,18 +324,13 @@
                 simCallManagers,
                 mTelecomManager.getSimCallManager(),
                 getString(R.string.wifi_calling_call_assistant_none));
-
-        updateConfigureCallAssistantSummary();
     }
 
     /**
-     * Updates the summary on the "configure call assistant" preference. If it is the last entry,
-     * show the summary for when no call assistant is selected. Otherwise, display the currently
-     * selected call assistant.
+     * Updates the summary on the "configure call assistant" preference.
      */
     private void updateConfigureCallAssistantSummary() {
-        if (mSelectCallAssistant.getEntries().length - 1
-                == mSelectCallAssistant.findIndexOfValue(mSelectCallAssistant.getValue())) {
+        if (mTelecomManager.getSimCallManager() == null) {
             mConfigureCallAssistant.setSummary(
                     R.string.wifi_calling_call_assistant_configure_no_selection);
             mConfigureCallAssistant.setEnabled(false);