Add default outgoing and sim call manager account settings.

+ Moved AccountSelectionPreference here from Telecomm.
+ Added new settings, but hide them if behavior is null-op.

Bug: 17179743
Change-Id: I23af1b55ac24c8532fc567b4c33aff5f3b0430db
diff --git a/src/com/android/phone/CallFeaturesSetting.java b/src/com/android/phone/CallFeaturesSetting.java
index aa01862..6324eae 100644
--- a/src/com/android/phone/CallFeaturesSetting.java
+++ b/src/com/android/phone/CallFeaturesSetting.java
@@ -22,7 +22,6 @@
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.app.ProgressDialog;
-import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -32,7 +31,6 @@
 import android.content.pm.ActivityInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
-import android.content.pm.ServiceInfo;
 import android.database.Cursor;
 import android.media.AudioManager;
 import android.media.RingtoneManager;
@@ -50,7 +48,7 @@
 import android.preference.PreferenceScreen;
 import android.provider.ContactsContract.CommonDataKinds;
 import android.provider.Settings;
-import android.telecomm.ConnectionService;
+import android.telecomm.PhoneAccountHandle;
 import android.telecomm.TelecommManager;
 import android.telephony.PhoneNumberUtils;
 import android.text.TextUtils;
@@ -64,6 +62,7 @@
 import com.android.internal.telephony.Phone;
 import com.android.internal.telephony.PhoneConstants;
 import com.android.phone.common.util.SettingsUtil;
+import com.android.phone.settings.AccountSelectionPreference;
 import com.android.services.telephony.sip.SipSharedPreferences;
 import com.android.services.telephony.sip.SipUtil;
 
@@ -100,7 +99,8 @@
                 Preference.OnPreferenceChangeListener,
                 Preference.OnPreferenceClickListener,
                 EditPhoneNumberPreference.OnDialogClosedListener,
-                EditPhoneNumberPreference.GetDefaultNumberListener {
+                EditPhoneNumberPreference.GetDefaultNumberListener,
+                AccountSelectionPreference.AccountSelectionListener {
     private static final String LOG_TAG = "CallFeaturesSetting";
     private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
 
@@ -190,6 +190,9 @@
     private static final String SIP_SETTINGS_PREFERENCE_SCREEN_KEY =
             "sip_settings_preference_screen_key";
 
+    private static final String DEFAULT_OUTGOING_ACCOUNT_KEY = "default_outgoing_account";
+    private static final String WIFI_CALL_MANAGER_ACCOUNT_KEY = "wifi_call_manager_account";
+
     private Intent mContactListIntent;
 
     /** Event for Async voicemail change call */
@@ -265,6 +268,8 @@
     private Preference mVoicemailNotificationRingtone;
     private CheckBoxPreference mVoicemailNotificationVibrate;
     private SipSharedPreferences mSipSharedPreferences;
+    private AccountSelectionPreference mDefaultOutgoingAccount;
+    private AccountSelectionPreference mSimCallManagerAccount;
 
     private class VoiceMailProvider {
         public VoiceMailProvider(String name, Intent intent) {
@@ -586,6 +591,19 @@
     }
 
     @Override
+    public boolean onAccountSelected(AccountSelectionPreference pref, PhoneAccountHandle account) {
+        TelecommManager telecommManager = TelecommManager.from(this);
+        if (pref == mDefaultOutgoingAccount) {
+            telecommManager.setUserSelectedOutgoingPhoneAccount(account);
+            return true;
+        } else if (pref == mSimCallManagerAccount) {
+            telecommManager.setSimCallManager(account);
+            return true;
+        }
+        return false;
+    }
+
+    @Override
     public void onDialogClosed(EditPhoneNumberPreference preference, int buttonClicked) {
         if (DBG) log("onPreferenceClick: request preference click on dialog close: " +
                 buttonClicked);
@@ -1494,6 +1512,8 @@
 
         addPreferencesFromResource(R.xml.call_feature_setting);
 
+        initPhoneAccountPreferences();
+
         mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
 
         // get buttons
@@ -1640,6 +1660,39 @@
         }
     }
 
+    private void initPhoneAccountPreferences() {
+        mDefaultOutgoingAccount = (AccountSelectionPreference)
+                findPreference(DEFAULT_OUTGOING_ACCOUNT_KEY);
+        mSimCallManagerAccount = (AccountSelectionPreference)
+                findPreference(WIFI_CALL_MANAGER_ACCOUNT_KEY);
+
+        TelecommManager telecommManager = TelecommManager.from(this);
+
+        List<PhoneAccountHandle> enabledPhoneAccounts = telecommManager.getEnabledPhoneAccounts();
+        if (enabledPhoneAccounts.size() > 1) {
+            mDefaultOutgoingAccount.setModel(
+                    telecommManager,
+                    enabledPhoneAccounts,
+                    telecommManager.getUserSelectedOutgoingPhoneAccount(),
+                    getString(R.string.phone_accounts_ask_every_time));
+            mDefaultOutgoingAccount.setListener(this);
+        } else {
+            getPreferenceScreen().removePreference(mDefaultOutgoingAccount);
+        }
+
+        List<PhoneAccountHandle> simCallManagers = telecommManager.getSimCallManagers();
+        if (simCallManagers.size() > 1) {
+            mSimCallManagerAccount.setModel(
+                    telecommManager,
+                    simCallManagers,
+                    telecommManager.getSimCallManager(),
+                    getString(R.string.wifi_calling_do_not_use));
+            mSimCallManagerAccount.setListener(this);
+        } else {
+            getPreferenceScreen().removePreference(mSimCallManagerAccount);
+        }
+    }
+
     private void createSipCallSettings() {
         // Add Internet call settings.
         if (SipUtil.isVoipSupported(this)) {