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)) {
diff --git a/src/com/android/phone/settings/AccountSelectionPreference.java b/src/com/android/phone/settings/AccountSelectionPreference.java
new file mode 100644
index 0000000..32149ae
--- /dev/null
+++ b/src/com/android/phone/settings/AccountSelectionPreference.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.phone.settings;
+
+import android.content.Context;
+import android.preference.ListPreference;
+import android.preference.Preference;
+import android.telecomm.PhoneAccountHandle;
+import android.telecomm.TelecommManager;
+import android.util.AttributeSet;
+
+import java.util.List;
+import java.util.Objects;
+
+public class AccountSelectionPreference extends ListPreference implements
+        Preference.OnPreferenceChangeListener {
+
+    public interface AccountSelectionListener {
+        boolean onAccountSelected(AccountSelectionPreference pref, PhoneAccountHandle account);
+    }
+
+    private AccountSelectionListener mListener;
+    private PhoneAccountHandle[] mAccounts;
+    private String[] mEntryValues;
+    private CharSequence[] mEntries;
+
+    public AccountSelectionPreference(Context context) {
+        super(context);
+        setOnPreferenceChangeListener(this);
+    }
+
+    public AccountSelectionPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setOnPreferenceChangeListener(this);
+    }
+
+    public void setListener(AccountSelectionListener listener) {
+        mListener = listener;
+    }
+
+    public void setModel(
+            TelecommManager telecommManager,
+            List<PhoneAccountHandle> accountsList,
+            PhoneAccountHandle currentSelection,
+            CharSequence nullSelectionString) {
+
+        mAccounts = accountsList.toArray(new PhoneAccountHandle[accountsList.size()]);
+        mEntryValues = new String[mAccounts.length + 1];
+        mEntries = new CharSequence[mAccounts.length + 1];
+
+        int selectedIndex = mAccounts.length;  // Points to nullSelectionString by default
+        int i = 0;
+        for ( ; i < mAccounts.length; i++) {
+            CharSequence label = telecommManager.getPhoneAccount(mAccounts[i]).getLabel();
+            mEntries[i] = label == null ? null : label.toString();
+            mEntryValues[i] = Integer.toString(i);
+            if (Objects.equals(currentSelection, mAccounts[i])) {
+                selectedIndex = i;
+            }
+        }
+        mEntryValues[i] = Integer.toString(i);
+        mEntries[i] = nullSelectionString;
+
+        setEntryValues(mEntryValues);
+        setEntries(mEntries);
+        setValueIndex(selectedIndex);
+        setSummary(mEntries[selectedIndex]);
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        if (mListener != null) {
+            int index = Integer.parseInt((String) newValue);
+            PhoneAccountHandle account = index < mAccounts.length ? mAccounts[index] : null;
+            if (mListener.onAccountSelected(this, account)) {
+                setSummary(mEntries[index]);
+                return true;
+            }
+        }
+        return false;
+    }
+}