1ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leepackage com.android.phone.settings;
2ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
316e0ff2349d98e109a974bb0aae624f9818d0709Jay Shraunerimport android.content.Context;
4ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport android.content.Intent;
56781b0c2375a905614781f8503bfd601f4020340Santos Cordonimport android.content.pm.PackageManager;
66781b0c2375a905614781f8503bfd601f4020340Santos Cordonimport android.content.pm.ResolveInfo;
7a3d9425d041a5f4df28583d086c1837e0d8b0b6fSantos Cordonimport android.graphics.drawable.Icon;
8ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport android.net.sip.SipManager;
9ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport android.os.Bundle;
10ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport android.preference.CheckBoxPreference;
11ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport android.preference.ListPreference;
12ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport android.preference.Preference;
13e3c15213a991df51d35e42926d47a629f55fbb63Andrew Leeimport android.preference.PreferenceCategory;
14ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport android.preference.PreferenceFragment;
156781b0c2375a905614781f8503bfd601f4020340Santos Cordonimport android.telecom.PhoneAccount;
16ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport android.telecom.PhoneAccountHandle;
17ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport android.telecom.TelecomManager;
180f3b5fcda1b76fbd6042f9a6f81f69d0b44bc3eeWink Savilleimport android.telephony.SubscriptionInfo;
19e3c15213a991df51d35e42926d47a629f55fbb63Andrew Leeimport android.telephony.SubscriptionManager;
20e3c15213a991df51d35e42926d47a629f55fbb63Andrew Leeimport android.telephony.TelephonyManager;
21499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordonimport android.text.TextUtils;
22499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordonimport android.util.Log;
23ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
240db135df689b874d7e881d8b6595021c23addf14Santos Cordonimport com.android.internal.telephony.Phone;
250db135df689b874d7e881d8b6595021c23addf14Santos Cordonimport com.android.phone.PhoneUtils;
26ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport com.android.phone.R;
275ed870cca68496757c2335d37d3818e9aa0006b6Andrew Leeimport com.android.phone.SubscriptionInfoHelper;
284d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Leeimport com.android.services.telephony.sip.SipAccountRegistry;
29ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport com.android.services.telephony.sip.SipSharedPreferences;
30ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport com.android.services.telephony.sip.SipUtil;
31ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
326781b0c2375a905614781f8503bfd601f4020340Santos Cordonimport java.util.ArrayList;
336781b0c2375a905614781f8503bfd601f4020340Santos Cordonimport java.util.Collections;
346781b0c2375a905614781f8503bfd601f4020340Santos Cordonimport java.util.Comparator;
35e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordonimport java.util.Iterator;
36ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leeimport java.util.List;
37ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
38ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Leepublic class PhoneAccountSettingsFragment extends PreferenceFragment
39ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        implements Preference.OnPreferenceChangeListener,
40ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee                AccountSelectionPreference.AccountSelectionListener {
41ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
42e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee    private static final String ACCOUNTS_LIST_CATEGORY_KEY =
43e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee            "phone_accounts_accounts_list_category_key";
44e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee
45ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    private static final String DEFAULT_OUTGOING_ACCOUNT_KEY = "default_outgoing_account";
46e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon    private static final String ALL_CALLING_ACCOUNTS_KEY = "phone_account_all_calling_accounts";
47ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
481e58e41b50b337ba00605b9a56f4fa49cacb1708Nancy Chen    private static final String SIP_SETTINGS_CATEGORY_PREF_KEY =
491e58e41b50b337ba00605b9a56f4fa49cacb1708Nancy Chen            "phone_accounts_sip_settings_category_key";
50ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    private static final String USE_SIP_PREF_KEY = "use_sip_calling_options_key";
514d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee    private static final String SIP_RECEIVE_CALLS_PREF_KEY = "sip_receive_calls_key";
52ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
53499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon    private static final String LEGACY_ACTION_CONFIGURE_PHONE_ACCOUNT =
54499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon            "android.telecom.action.CONNECTION_SERVICE_CONFIGURE";
55499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon
56e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon    /**
57e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon     * Value to start ordering of phone accounts relative to other preferences. By setting this
58e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon     * value on the phone account listings, we ensure that anything that is ordered before
59e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon     * {value} in the preference XML comes before the phone account list and anything with
60e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon     * a value significantly larger will list after.
61e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon     */
62e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon    private static final int ACCOUNT_ORDERING_START_VALUE = 100;
63e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon
64499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon    private static final String LOG_TAG = PhoneAccountSettingsFragment.class.getSimpleName();
65ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
66ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    private TelecomManager mTelecomManager;
676781b0c2375a905614781f8503bfd601f4020340Santos Cordon    private TelephonyManager mTelephonyManager;
683ab207e3711ebd717f9cfe7412018c51ae1c8131Wink Saville    private SubscriptionManager mSubscriptionManager;
69ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
70e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee    private PreferenceCategory mAccountList;
71e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee
72ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    private AccountSelectionPreference mDefaultOutgoingAccount;
73ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
74ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    private ListPreference mUseSipCalling;
754d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee    private CheckBoxPreference mSipReceiveCallsPreference;
76ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    private SipSharedPreferences mSipSharedPreferences;
77ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
78ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    @Override
79ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    public void onCreate(Bundle icicle) {
80ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        super.onCreate(icicle);
81ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
82ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        mTelecomManager = TelecomManager.from(getActivity());
836781b0c2375a905614781f8503bfd601f4020340Santos Cordon        mTelephonyManager = TelephonyManager.from(getActivity());
843ab207e3711ebd717f9cfe7412018c51ae1c8131Wink Saville        mSubscriptionManager = SubscriptionManager.from(getActivity());
85ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    }
86ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
87ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    @Override
88ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    public void onResume() {
89ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        super.onResume();
90ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
91ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        if (getPreferenceScreen() != null) {
92ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            getPreferenceScreen().removeAll();
93ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        }
94ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
95787a8cb70963e01282ca5db869e51e05e1d60011Andrew Lee        addPreferencesFromResource(R.xml.phone_account_settings);
96ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
970db135df689b874d7e881d8b6595021c23addf14Santos Cordon        /**
980db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * Here we make decisions about what we will and will not display with regards to phone-
990db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * account settings.  The basic settings structure is this:
1000db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * (1) <Make Calls With...>  // Lets user pick a default account for outgoing calls
1010db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * (2) <Account List>
1020db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *       <Account>
1030db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *       ...
1040db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *       <Account>
1050db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *     </Account List>
1060db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * (3) <All Accounts>  // Lets user enable/disable third-party accounts. SIM-based accounts
1070db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *                     // are always enabled and so aren't relevant here.
1080db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *
1090db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * Here are the rules that we follow:
1100db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * - (1) is only shown if there are multiple enabled accounts, including SIM accounts.
1110db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *   This can be 2+ SIM accounts, 2+ third party accounts or any combination.
1120db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * - (2) The account list only lists (a) enabled third party accounts and (b) SIM-based
1130db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *   accounts. However, for single-SIM devices, if the only account to show is the
1140db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *   SIM-based account, we don't show the list at all under the assumption that the user
1150db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *   already knows about the account.
1160db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * - (3) Is only shown if there exist any third party accounts.  If none exist, then the
1170db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *   option is hidden since there is nothing that can be done in it.
1180db135df689b874d7e881d8b6595021c23addf14Santos Cordon         *
1190db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * By far, the most common case for users will be the single-SIM device without any
1200db135df689b874d7e881d8b6595021c23addf14Santos Cordon         * third party accounts. IOW, the great majority of users won't see any of these options.
1210db135df689b874d7e881d8b6595021c23addf14Santos Cordon         */
122e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee        mAccountList = (PreferenceCategory) getPreferenceScreen().findPreference(
123e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee                ACCOUNTS_LIST_CATEGORY_KEY);
1240db135df689b874d7e881d8b6595021c23addf14Santos Cordon        List<PhoneAccountHandle> allNonSimAccounts =
1250db135df689b874d7e881d8b6595021c23addf14Santos Cordon                getCallingAccounts(false /* includeSims */, true /* includeDisabled */);
1260db135df689b874d7e881d8b6595021c23addf14Santos Cordon        // Check to see if we should show the entire section at all.
1270db135df689b874d7e881d8b6595021c23addf14Santos Cordon        if (shouldShowConnectionServiceList(allNonSimAccounts)) {
1280db135df689b874d7e881d8b6595021c23addf14Santos Cordon            List<PhoneAccountHandle> enabledAccounts =
1290db135df689b874d7e881d8b6595021c23addf14Santos Cordon                    getCallingAccounts(true /* includeSims */, false /* includeDisabled */);
1300db135df689b874d7e881d8b6595021c23addf14Santos Cordon            // Initialize the account list with the set of enabled & SIM accounts.
1310db135df689b874d7e881d8b6595021c23addf14Santos Cordon            initAccountList(enabledAccounts);
132e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee
1336781b0c2375a905614781f8503bfd601f4020340Santos Cordon            mDefaultOutgoingAccount = (AccountSelectionPreference)
1346781b0c2375a905614781f8503bfd601f4020340Santos Cordon                    getPreferenceScreen().findPreference(DEFAULT_OUTGOING_ACCOUNT_KEY);
135ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mDefaultOutgoingAccount.setListener(this);
1360db135df689b874d7e881d8b6595021c23addf14Santos Cordon
1370db135df689b874d7e881d8b6595021c23addf14Santos Cordon            // Only show the 'Make Calls With..." option if there are multiple accounts.
1380db135df689b874d7e881d8b6595021c23addf14Santos Cordon            if (enabledAccounts.size() > 1) {
1396781b0c2375a905614781f8503bfd601f4020340Santos Cordon                updateDefaultOutgoingAccountsModel();
1406781b0c2375a905614781f8503bfd601f4020340Santos Cordon            } else {
1418bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                mAccountList.removePreference(mDefaultOutgoingAccount);
1426781b0c2375a905614781f8503bfd601f4020340Santos Cordon            }
1436781b0c2375a905614781f8503bfd601f4020340Santos Cordon
144e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon            Preference allAccounts = getPreferenceScreen().findPreference(ALL_CALLING_ACCOUNTS_KEY);
1450db135df689b874d7e881d8b6595021c23addf14Santos Cordon            // If there are no third party (nonSim) accounts, then don't show enable/disable dialog.
1460db135df689b874d7e881d8b6595021c23addf14Santos Cordon            if (allNonSimAccounts.isEmpty() && allAccounts != null) {
1478bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                mAccountList.removePreference(allAccounts);
148e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon            }
149ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        } else {
1506781b0c2375a905614781f8503bfd601f4020340Santos Cordon            getPreferenceScreen().removePreference(mAccountList);
151ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        }
152ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
153ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        if (SipUtil.isVoipSupported(getActivity())) {
154ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mSipSharedPreferences = new SipSharedPreferences(getActivity());
155ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
156ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mUseSipCalling = (ListPreference)
157ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee                    getPreferenceScreen().findPreference(USE_SIP_PREF_KEY);
158ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mUseSipCalling.setEntries(!SipManager.isSipWifiOnly(getActivity())
159ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee                    ? R.array.sip_call_options_wifi_only_entries
160ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee                    : R.array.sip_call_options_entries);
161ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mUseSipCalling.setOnPreferenceChangeListener(this);
162829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee
163829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee            int optionsValueIndex =
164829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee                    mUseSipCalling.findIndexOfValue(mSipSharedPreferences.getSipCallOption());
165829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee            if (optionsValueIndex == -1) {
166829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee                // If the option is invalid (eg. deprecated value), default to SIP_ADDRESS_ONLY.
167829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee                mSipSharedPreferences.setSipCallOption(
168829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee                        getResources().getString(R.string.sip_address_only));
169829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee                optionsValueIndex =
170829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee                        mUseSipCalling.findIndexOfValue(mSipSharedPreferences.getSipCallOption());
171829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee            }
172829e46ecc3fa122b6c5f29d2771a62e89eedd1a7Andrew Lee            mUseSipCalling.setValueIndex(optionsValueIndex);
173ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mUseSipCalling.setSummary(mUseSipCalling.getEntry());
1744d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee
1754d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee            mSipReceiveCallsPreference = (CheckBoxPreference)
1764d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee                    getPreferenceScreen().findPreference(SIP_RECEIVE_CALLS_PREF_KEY);
1774d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee            mSipReceiveCallsPreference.setEnabled(SipUtil.isPhoneIdle(getActivity()));
1784d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee            mSipReceiveCallsPreference.setChecked(
1794d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee                    mSipSharedPreferences.isReceivingCallsEnabled());
1804d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee            mSipReceiveCallsPreference.setOnPreferenceChangeListener(this);
181ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        } else {
182ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            getPreferenceScreen().removePreference(
183ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee                    getPreferenceScreen().findPreference(SIP_SETTINGS_CATEGORY_PREF_KEY));
184ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        }
185ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    }
186ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
187ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    /**
1884d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee     * Handles changes to the preferences.
189ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     *
190ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * @param pref The preference changed.
191ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * @param objValue The changed value.
192ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * @return True if the preference change has been handled, and false otherwise.
193ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     */
194ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    @Override
195ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    public boolean onPreferenceChange(Preference pref, Object objValue) {
196ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        if (pref == mUseSipCalling) {
197ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            String option = objValue.toString();
198ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mSipSharedPreferences.setSipCallOption(option);
199ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mUseSipCalling.setValueIndex(mUseSipCalling.findIndexOfValue(option));
200ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mUseSipCalling.setSummary(mUseSipCalling.getEntry());
201ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            return true;
2024d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee        } else if (pref == mSipReceiveCallsPreference) {
2034d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee            final boolean isEnabled = !mSipReceiveCallsPreference.isChecked();
2044d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee            new Thread(new Runnable() {
2054d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee                public void run() {
2064d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee                    handleSipReceiveCallsOption(isEnabled);
2074d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee                }
2084d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee            }).start();
2094d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee            return true;
210ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        }
211ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        return false;
212ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    }
213ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
214ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    /**
215e822bb564453c906cc780af7de21ae28086f2664Yorke Lee     * Handles a phone account selection for the default outgoing phone account.
216ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     *
217ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * @param pref The account selection preference which triggered the account selected event.
218ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * @param account The account selected.
219ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * @return True if the account selection has been handled, and false otherwise.
220ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     */
221ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    @Override
222ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    public boolean onAccountSelected(AccountSelectionPreference pref, PhoneAccountHandle account) {
223ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        if (pref == mDefaultOutgoingAccount) {
224ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            mTelecomManager.setUserSelectedOutgoingPhoneAccount(account);
225ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            return true;
226ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        }
227ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        return false;
228ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    }
229ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
230ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    /**
231ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * Repopulate the dialog to pick up changes before showing.
232ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     *
233ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * @param pref The account selection preference dialog being shown.
234ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     */
235ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    @Override
236ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    public void onAccountSelectionDialogShow(AccountSelectionPreference pref) {
237ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        if (pref == mDefaultOutgoingAccount) {
238ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee            updateDefaultOutgoingAccountsModel();
239ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        }
240ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    }
241ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
242ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    @Override
243e822bb564453c906cc780af7de21ae28086f2664Yorke Lee    public void onAccountChanged(AccountSelectionPreference pref) {}
244ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
2454d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee    private synchronized void handleSipReceiveCallsOption(boolean isEnabled) {
2460ea038be6610481204d8d68bcbce4703f2a7b177Andrew Lee        Context context = getActivity();
2470ea038be6610481204d8d68bcbce4703f2a7b177Andrew Lee        if (context == null) {
2480ea038be6610481204d8d68bcbce4703f2a7b177Andrew Lee            // Return if the fragment is detached from parent activity before executed by thread.
2490ea038be6610481204d8d68bcbce4703f2a7b177Andrew Lee            return;
2500ea038be6610481204d8d68bcbce4703f2a7b177Andrew Lee        }
2510ea038be6610481204d8d68bcbce4703f2a7b177Andrew Lee
2524d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee        mSipSharedPreferences.setReceivingCallsEnabled(isEnabled);
2534d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee
2540ea038be6610481204d8d68bcbce4703f2a7b177Andrew Lee        SipUtil.useSipToReceiveIncomingCalls(context, isEnabled);
2554d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee
2564d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee        // Restart all Sip services to ensure we reflect whether we are receiving calls.
2574d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee        SipAccountRegistry sipAccountRegistry = SipAccountRegistry.getInstance();
2580ea038be6610481204d8d68bcbce4703f2a7b177Andrew Lee        sipAccountRegistry.restartSipService(context);
2594d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee    }
2604d37f5279f2896d16ec3d4e10ec4dcba63e86c3cAndrew Lee
261ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    /**
262ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * Queries the telcomm manager to update the default outgoing account selection preference
263ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     * with the list of outgoing accounts and the current default outgoing account.
264ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee     */
265ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    private void updateDefaultOutgoingAccountsModel() {
266ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee        mDefaultOutgoingAccount.setModel(
267ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee                mTelecomManager,
2680db135df689b874d7e881d8b6595021c23addf14Santos Cordon                getCallingAccounts(true /* includeSims */, false /* includeDisabled */),
269ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee                mTelecomManager.getUserSelectedOutgoingPhoneAccount(),
270ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee                getString(R.string.phone_accounts_ask_every_time));
271ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee    }
272ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee
2730db135df689b874d7e881d8b6595021c23addf14Santos Cordon    private void initAccountList(List<PhoneAccountHandle> enabledAccounts) {
2740db135df689b874d7e881d8b6595021c23addf14Santos Cordon
2758bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon        boolean isMultiSimDevice = mTelephonyManager.isMultiSimEnabled();
2768bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon
2778bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon        // On a single-SIM device, do not list any accounts if the only account is the SIM-based
2788bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon        // one. This is because on single-SIM devices, we do not expose SIM settings through the
2798bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon        // account listing entry so showing it does nothing to help the user. Nor does the lack of
2808bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon        // action match the "Settings" header above the listing.
2810db135df689b874d7e881d8b6595021c23addf14Santos Cordon        if (!isMultiSimDevice && getCallingAccounts(
2820db135df689b874d7e881d8b6595021c23addf14Santos Cordon                false /* includeSims */, false /* includeDisabled */).isEmpty()){
2838bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon            return;
2848bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon        }
2858bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon
2866781b0c2375a905614781f8503bfd601f4020340Santos Cordon        // Obtain the list of phone accounts.
2876781b0c2375a905614781f8503bfd601f4020340Santos Cordon        List<PhoneAccount> accounts = new ArrayList<>();
2880db135df689b874d7e881d8b6595021c23addf14Santos Cordon        for (PhoneAccountHandle handle : enabledAccounts) {
2896781b0c2375a905614781f8503bfd601f4020340Santos Cordon            PhoneAccount account = mTelecomManager.getPhoneAccount(handle);
2906781b0c2375a905614781f8503bfd601f4020340Santos Cordon            if (account != null) {
2916781b0c2375a905614781f8503bfd601f4020340Santos Cordon                accounts.add(account);
2926781b0c2375a905614781f8503bfd601f4020340Santos Cordon            }
2933ab207e3711ebd717f9cfe7412018c51ae1c8131Wink Saville        }
294e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee
2956781b0c2375a905614781f8503bfd601f4020340Santos Cordon        // Sort the accounts according to how we want to display them.
2966781b0c2375a905614781f8503bfd601f4020340Santos Cordon        Collections.sort(accounts, new Comparator<PhoneAccount>() {
2976781b0c2375a905614781f8503bfd601f4020340Santos Cordon            @Override
2986781b0c2375a905614781f8503bfd601f4020340Santos Cordon            public int compare(PhoneAccount account1, PhoneAccount account2) {
2996781b0c2375a905614781f8503bfd601f4020340Santos Cordon                int retval = 0;
3006781b0c2375a905614781f8503bfd601f4020340Santos Cordon
3016781b0c2375a905614781f8503bfd601f4020340Santos Cordon                // SIM accounts go first
3028bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                boolean isSim1 = account1.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
3038bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                boolean isSim2 = account2.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
3046781b0c2375a905614781f8503bfd601f4020340Santos Cordon                if (isSim1 != isSim2) {
3056781b0c2375a905614781f8503bfd601f4020340Santos Cordon                    retval = isSim1 ? -1 : 1;
3066781b0c2375a905614781f8503bfd601f4020340Santos Cordon                }
3076781b0c2375a905614781f8503bfd601f4020340Santos Cordon
3086781b0c2375a905614781f8503bfd601f4020340Santos Cordon                // Then order by package
3096781b0c2375a905614781f8503bfd601f4020340Santos Cordon                if (retval == 0) {
3106781b0c2375a905614781f8503bfd601f4020340Santos Cordon                    String pkg1 = account1.getAccountHandle().getComponentName().getPackageName();
3116781b0c2375a905614781f8503bfd601f4020340Santos Cordon                    String pkg2 = account2.getAccountHandle().getComponentName().getPackageName();
3126781b0c2375a905614781f8503bfd601f4020340Santos Cordon                    retval = pkg1.compareTo(pkg2);
3136781b0c2375a905614781f8503bfd601f4020340Santos Cordon                }
3146781b0c2375a905614781f8503bfd601f4020340Santos Cordon
3156781b0c2375a905614781f8503bfd601f4020340Santos Cordon                // Finally, order by label
3166781b0c2375a905614781f8503bfd601f4020340Santos Cordon                if (retval == 0) {
3176781b0c2375a905614781f8503bfd601f4020340Santos Cordon                    String label1 = nullToEmpty(account1.getLabel().toString());
3186781b0c2375a905614781f8503bfd601f4020340Santos Cordon                    String label2 = nullToEmpty(account2.getLabel().toString());
3196781b0c2375a905614781f8503bfd601f4020340Santos Cordon                    retval = label1.compareTo(label2);
3206781b0c2375a905614781f8503bfd601f4020340Santos Cordon                }
3216781b0c2375a905614781f8503bfd601f4020340Santos Cordon
3226781b0c2375a905614781f8503bfd601f4020340Santos Cordon                // Then by hashcode
3236781b0c2375a905614781f8503bfd601f4020340Santos Cordon                if (retval == 0) {
3246781b0c2375a905614781f8503bfd601f4020340Santos Cordon                    retval = account1.hashCode() - account2.hashCode();
3256781b0c2375a905614781f8503bfd601f4020340Santos Cordon                }
3266781b0c2375a905614781f8503bfd601f4020340Santos Cordon                return retval;
3276781b0c2375a905614781f8503bfd601f4020340Santos Cordon            }
3286781b0c2375a905614781f8503bfd601f4020340Santos Cordon        });
3296781b0c2375a905614781f8503bfd601f4020340Santos Cordon
330e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon        int order = ACCOUNT_ORDERING_START_VALUE;
331e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon
3326781b0c2375a905614781f8503bfd601f4020340Santos Cordon        // Add an entry for each account.
3336781b0c2375a905614781f8503bfd601f4020340Santos Cordon        for (PhoneAccount account : accounts) {
3346781b0c2375a905614781f8503bfd601f4020340Santos Cordon            PhoneAccountHandle handle = account.getAccountHandle();
3356781b0c2375a905614781f8503bfd601f4020340Santos Cordon            Intent intent = null;
3366781b0c2375a905614781f8503bfd601f4020340Santos Cordon
3376781b0c2375a905614781f8503bfd601f4020340Santos Cordon            // SIM phone accounts use a different setting intent and are thus handled differently.
3388bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon            if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
3398bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon
3408bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                // For SIM-based accounts, we only expose the settings through the account list
3418bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                // if we are on a multi-SIM device. For single-SIM devices, the settings are
3428bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                // more spread out so there is no good single place to take the user, so we don't.
3438bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                if (isMultiSimDevice) {
3448bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                    SubscriptionInfo subInfo = mSubscriptionManager.getActiveSubscriptionInfo(
3458bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                            mTelephonyManager.getSubIdForPhoneAccount(account));
3468bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon
3478bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                    if (subInfo != null) {
3488bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                        intent = new Intent(TelecomManager.ACTION_SHOW_CALL_SETTINGS);
3498bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
3508bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                        SubscriptionInfoHelper.addExtrasToIntent(intent, subInfo);
3518bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                    }
3526781b0c2375a905614781f8503bfd601f4020340Santos Cordon                }
3536781b0c2375a905614781f8503bfd601f4020340Santos Cordon            } else {
354499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon                intent = buildPhoneAccountConfigureIntent(getActivity(), handle);
3556781b0c2375a905614781f8503bfd601f4020340Santos Cordon            }
3566781b0c2375a905614781f8503bfd601f4020340Santos Cordon
3576781b0c2375a905614781f8503bfd601f4020340Santos Cordon            // Create the preference & add the label
3580ea038be6610481204d8d68bcbce4703f2a7b177Andrew Lee            Preference accountPreference = new Preference(getActivity());
3596781b0c2375a905614781f8503bfd601f4020340Santos Cordon            accountPreference.setTitle(account.getLabel());
3606781b0c2375a905614781f8503bfd601f4020340Santos Cordon
3616781b0c2375a905614781f8503bfd601f4020340Santos Cordon            // Add an icon.
362a3d9425d041a5f4df28583d086c1837e0d8b0b6fSantos Cordon            Icon icon = account.getIcon();
3630db72b85bb057844277e8fe319815a05337521a4Yorke Lee            if (icon != null) {
3640db72b85bb057844277e8fe319815a05337521a4Yorke Lee                accountPreference.setIcon(icon.loadDrawable(getActivity()));
3650db72b85bb057844277e8fe319815a05337521a4Yorke Lee            }
3666781b0c2375a905614781f8503bfd601f4020340Santos Cordon
3676781b0c2375a905614781f8503bfd601f4020340Santos Cordon            // Add an intent to send the user to the account's settings.
3686781b0c2375a905614781f8503bfd601f4020340Santos Cordon            if (intent != null) {
3696781b0c2375a905614781f8503bfd601f4020340Santos Cordon                accountPreference.setIntent(intent);
3706781b0c2375a905614781f8503bfd601f4020340Santos Cordon            }
3716781b0c2375a905614781f8503bfd601f4020340Santos Cordon
372e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon            accountPreference.setOrder(order++);
373e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee            mAccountList.addPreference(accountPreference);
374e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee        }
375e3c15213a991df51d35e42926d47a629f55fbb63Andrew Lee    }
376be890cfe4322ac4d4e02889b40159d3b6787bc78Sailesh Nepal
3770db135df689b874d7e881d8b6595021c23addf14Santos Cordon    private boolean shouldShowConnectionServiceList(List<PhoneAccountHandle> allNonSimAccounts) {
3780db135df689b874d7e881d8b6595021c23addf14Santos Cordon        return mTelephonyManager.isMultiSimEnabled() || allNonSimAccounts.size() > 0;
379e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon    }
380e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon
3810db135df689b874d7e881d8b6595021c23addf14Santos Cordon    private List<PhoneAccountHandle> getCallingAccounts(
3820db135df689b874d7e881d8b6595021c23addf14Santos Cordon            boolean includeSims, boolean includeDisabledAccounts) {
3830db135df689b874d7e881d8b6595021c23addf14Santos Cordon        PhoneAccountHandle emergencyAccountHandle = getEmergencyPhoneAccount();
3840db135df689b874d7e881d8b6595021c23addf14Santos Cordon
385e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon        List<PhoneAccountHandle> accountHandles =
3868bc2e2394705f37569b6baaaec866fe90074eb2bSantos Cordon                mTelecomManager.getCallCapablePhoneAccounts(includeDisabledAccounts);
387e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon        for (Iterator<PhoneAccountHandle> i = accountHandles.iterator(); i.hasNext();) {
388e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon            PhoneAccountHandle handle = i.next();
3890db135df689b874d7e881d8b6595021c23addf14Santos Cordon            if (handle.equals(emergencyAccountHandle)) {
3900db135df689b874d7e881d8b6595021c23addf14Santos Cordon                // never include emergency call accounts in this piece of code.
3910db135df689b874d7e881d8b6595021c23addf14Santos Cordon                i.remove();
3920db135df689b874d7e881d8b6595021c23addf14Santos Cordon                continue;
3930db135df689b874d7e881d8b6595021c23addf14Santos Cordon            }
3940db135df689b874d7e881d8b6595021c23addf14Santos Cordon
395e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon            PhoneAccount account = mTelecomManager.getPhoneAccount(handle);
3960db135df689b874d7e881d8b6595021c23addf14Santos Cordon            if (account == null) {
3970db135df689b874d7e881d8b6595021c23addf14Santos Cordon                i.remove();
3980db135df689b874d7e881d8b6595021c23addf14Santos Cordon            } else if (!includeSims &&
3990db135df689b874d7e881d8b6595021c23addf14Santos Cordon                    account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
400e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon                i.remove();
401e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon            }
402e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon        }
403e50b1903f2e84de2d23c035f8444cb83e1ae6226Santos Cordon        return accountHandles;
4046781b0c2375a905614781f8503bfd601f4020340Santos Cordon    }
4056781b0c2375a905614781f8503bfd601f4020340Santos Cordon
4066781b0c2375a905614781f8503bfd601f4020340Santos Cordon    private String nullToEmpty(String str) {
4076781b0c2375a905614781f8503bfd601f4020340Santos Cordon        return str == null ? "" : str;
4086781b0c2375a905614781f8503bfd601f4020340Santos Cordon    }
4090db135df689b874d7e881d8b6595021c23addf14Santos Cordon
4100db135df689b874d7e881d8b6595021c23addf14Santos Cordon    private PhoneAccountHandle getEmergencyPhoneAccount() {
4110db135df689b874d7e881d8b6595021c23addf14Santos Cordon        return PhoneUtils.makePstnPhoneAccountHandleWithPrefix(
4120db135df689b874d7e881d8b6595021c23addf14Santos Cordon                (Phone) null, "" /* prefix */, true /* isEmergency */);
4130db135df689b874d7e881d8b6595021c23addf14Santos Cordon    }
414499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon
415499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon    public static Intent buildPhoneAccountConfigureIntent(
416499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon            Context context, PhoneAccountHandle accountHandle) {
417499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        Intent intent = buildConfigureIntent(
418499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon                context, accountHandle, TelecomManager.ACTION_CONFIGURE_PHONE_ACCOUNT);
419499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon
420499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        if (intent == null) {
421499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon            // If the new configuration didn't work, try the old configuration intent.
422499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon            intent = buildConfigureIntent(
423499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon                    context, accountHandle, LEGACY_ACTION_CONFIGURE_PHONE_ACCOUNT);
424499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon            if (intent != null) {
425499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon                Log.w(LOG_TAG, "Phone account using old configuration intent: " + accountHandle);
426499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon            }
427499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        }
428499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        return intent;
429499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon    }
430499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon
431499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon    private static Intent buildConfigureIntent(
432499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon            Context context, PhoneAccountHandle accountHandle, String actionStr) {
433499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        if (accountHandle == null || accountHandle.getComponentName() == null ||
434499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon                TextUtils.isEmpty(accountHandle.getComponentName().getPackageName())) {
435499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon            return null;
436499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        }
437499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon
438499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        // Build the settings intent.
439499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        Intent intent = new Intent(actionStr);
440499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        intent.setPackage(accountHandle.getComponentName().getPackageName());
441499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        intent.addCategory(Intent.CATEGORY_DEFAULT);
442499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, accountHandle);
443499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon
444499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        // Check to see that the phone account package can handle the setting intent.
445499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        PackageManager pm = context.getPackageManager();
446499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        List<ResolveInfo> resolutions = pm.queryIntentActivities(intent, 0);
447499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        if (resolutions.size() == 0) {
448499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon            intent = null;  // set no intent if the package cannot handle it.
449499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        }
450499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon
451499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon        return intent;
452499378ad2961c34f18cf1c508d68e17e3d53722aSantos Cordon    }
453ce8ae2af29acbf5cfa9509b0770615e317ff03a0Andrew Lee}
454