1 package com.android.phone.settings; 2 3 import android.content.ActivityNotFoundException; 4 import android.content.Intent; 5 import android.net.sip.SipManager; 6 import android.os.Bundle; 7 import android.preference.CheckBoxPreference; 8 import android.preference.ListPreference; 9 import android.preference.Preference; 10 import android.preference.PreferenceFragment; 11 import android.telecom.PhoneAccountHandle; 12 import android.telecom.TelecomManager; 13 import android.util.Log; 14 15 import com.android.phone.R; 16 import com.android.services.telephony.sip.SipAccountRegistry; 17 import com.android.services.telephony.sip.SipSharedPreferences; 18 import com.android.services.telephony.sip.SipUtil; 19 20 import java.util.List; 21 22 public class PhoneAccountSettingsFragment extends PreferenceFragment 23 implements Preference.OnPreferenceChangeListener, 24 Preference.OnPreferenceClickListener, 25 AccountSelectionPreference.AccountSelectionListener { 26 27 private static final Intent CONNECTION_SERVICE_CONFIGURE_INTENT = 28 new Intent(TelecomManager.ACTION_CONNECTION_SERVICE_CONFIGURE) 29 .addCategory(Intent.CATEGORY_DEFAULT); 30 31 private static final String DEFAULT_OUTGOING_ACCOUNT_KEY = "default_outgoing_account"; 32 33 private static final String CONFIGURE_CALL_ASSISTANT_PREF_KEY = 34 "wifi_calling_configure_call_assistant_preference"; 35 private static final String CALL_ASSISTANT_CATEGORY_PREF_KEY = 36 "phone_accounts_call_assistant_settings_category_key"; 37 private static final String SELECT_CALL_ASSISTANT_PREF_KEY = 38 "wifi_calling_call_assistant_preference"; 39 40 private static final String SIP_SETTINGS_CATEGORY_PREF_KEY = 41 "phone_accounts_sip_settings_category_key"; 42 private static final String USE_SIP_PREF_KEY = "use_sip_calling_options_key"; 43 private static final String SIP_RECEIVE_CALLS_PREF_KEY = "sip_receive_calls_key"; 44 45 private String LOG_TAG = PhoneAccountSettingsFragment.class.getSimpleName(); 46 47 private TelecomManager mTelecomManager; 48 49 private AccountSelectionPreference mDefaultOutgoingAccount; 50 private AccountSelectionPreference mSelectCallAssistant; 51 private Preference mConfigureCallAssistant; 52 53 private ListPreference mUseSipCalling; 54 private CheckBoxPreference mSipReceiveCallsPreference; 55 private SipSharedPreferences mSipSharedPreferences; 56 57 @Override 58 public void onCreate(Bundle icicle) { 59 super.onCreate(icicle); 60 61 mTelecomManager = TelecomManager.from(getActivity()); 62 } 63 64 @Override 65 public void onResume() { 66 super.onResume(); 67 68 if (getPreferenceScreen() != null) { 69 getPreferenceScreen().removeAll(); 70 } 71 72 addPreferencesFromResource(com.android.phone.R.xml.phone_account_settings); 73 74 mDefaultOutgoingAccount = (AccountSelectionPreference) 75 getPreferenceScreen().findPreference(DEFAULT_OUTGOING_ACCOUNT_KEY); 76 if (mTelecomManager.getAllPhoneAccountsCount() > 1) { 77 mDefaultOutgoingAccount.setListener(this); 78 updateDefaultOutgoingAccountsModel(); 79 } else { 80 getPreferenceScreen().removePreference(mDefaultOutgoingAccount); 81 } 82 83 if (!mTelecomManager.getSimCallManagers().isEmpty()) { 84 mSelectCallAssistant = (AccountSelectionPreference) 85 getPreferenceScreen().findPreference(SELECT_CALL_ASSISTANT_PREF_KEY); 86 mSelectCallAssistant.setListener(this); 87 mSelectCallAssistant.setDialogTitle( 88 R.string.wifi_calling_select_call_assistant_summary); 89 90 mConfigureCallAssistant = 91 getPreferenceScreen().findPreference(CONFIGURE_CALL_ASSISTANT_PREF_KEY); 92 mConfigureCallAssistant.setOnPreferenceClickListener(this); 93 updateCallAssistantModel(); 94 } else { 95 getPreferenceScreen().removePreference( 96 getPreferenceScreen().findPreference(CALL_ASSISTANT_CATEGORY_PREF_KEY)); 97 } 98 99 if (SipUtil.isVoipSupported(getActivity())) { 100 mSipSharedPreferences = new SipSharedPreferences(getActivity()); 101 102 mUseSipCalling = (ListPreference) 103 getPreferenceScreen().findPreference(USE_SIP_PREF_KEY); 104 mUseSipCalling.setEntries(!SipManager.isSipWifiOnly(getActivity()) 105 ? R.array.sip_call_options_wifi_only_entries 106 : R.array.sip_call_options_entries); 107 mUseSipCalling.setOnPreferenceChangeListener(this); 108 109 int optionsValueIndex = 110 mUseSipCalling.findIndexOfValue(mSipSharedPreferences.getSipCallOption()); 111 if (optionsValueIndex == -1) { 112 // If the option is invalid (eg. deprecated value), default to SIP_ADDRESS_ONLY. 113 mSipSharedPreferences.setSipCallOption( 114 getResources().getString(R.string.sip_address_only)); 115 optionsValueIndex = 116 mUseSipCalling.findIndexOfValue(mSipSharedPreferences.getSipCallOption()); 117 } 118 mUseSipCalling.setValueIndex(optionsValueIndex); 119 mUseSipCalling.setSummary(mUseSipCalling.getEntry()); 120 121 mSipReceiveCallsPreference = (CheckBoxPreference) 122 getPreferenceScreen().findPreference(SIP_RECEIVE_CALLS_PREF_KEY); 123 mSipReceiveCallsPreference.setEnabled(SipUtil.isPhoneIdle(getActivity())); 124 mSipReceiveCallsPreference.setChecked( 125 mSipSharedPreferences.isReceivingCallsEnabled()); 126 mSipReceiveCallsPreference.setOnPreferenceChangeListener(this); 127 } else { 128 getPreferenceScreen().removePreference( 129 getPreferenceScreen().findPreference(SIP_SETTINGS_CATEGORY_PREF_KEY)); 130 } 131 } 132 133 /** 134 * Handles changes to the preferences. 135 * 136 * @param pref The preference changed. 137 * @param objValue The changed value. 138 * @return True if the preference change has been handled, and false otherwise. 139 */ 140 @Override 141 public boolean onPreferenceChange(Preference pref, Object objValue) { 142 if (pref == mUseSipCalling) { 143 String option = objValue.toString(); 144 mSipSharedPreferences.setSipCallOption(option); 145 mUseSipCalling.setValueIndex(mUseSipCalling.findIndexOfValue(option)); 146 mUseSipCalling.setSummary(mUseSipCalling.getEntry()); 147 return true; 148 } else if (pref == mSipReceiveCallsPreference) { 149 final boolean isEnabled = !mSipReceiveCallsPreference.isChecked(); 150 new Thread(new Runnable() { 151 public void run() { 152 handleSipReceiveCallsOption(isEnabled); 153 } 154 }).start(); 155 return true; 156 } 157 return false; 158 } 159 160 @Override 161 public boolean onPreferenceClick(Preference pref) { 162 if (pref == mConfigureCallAssistant) { 163 try { 164 startActivity(CONNECTION_SERVICE_CONFIGURE_INTENT); 165 } catch (ActivityNotFoundException e) { 166 Log.d(LOG_TAG, "Could not resolve telecom connection service configure intent."); 167 } 168 return true; 169 } 170 return false; 171 } 172 173 /** 174 * Handles a phone account selection, namely when a call assistant has been selected. 175 * 176 * @param pref The account selection preference which triggered the account selected event. 177 * @param account The account selected. 178 * @return True if the account selection has been handled, and false otherwise. 179 */ 180 @Override 181 public boolean onAccountSelected(AccountSelectionPreference pref, PhoneAccountHandle account) { 182 if (pref == mDefaultOutgoingAccount) { 183 mTelecomManager.setUserSelectedOutgoingPhoneAccount(account); 184 return true; 185 } else if (pref == mSelectCallAssistant) { 186 mTelecomManager.setSimCallManager(account); 187 return true; 188 } 189 return false; 190 } 191 192 /** 193 * Repopulate the dialog to pick up changes before showing. 194 * 195 * @param pref The account selection preference dialog being shown. 196 */ 197 @Override 198 public void onAccountSelectionDialogShow(AccountSelectionPreference pref) { 199 if (pref == mDefaultOutgoingAccount) { 200 updateDefaultOutgoingAccountsModel(); 201 } else if (pref == mSelectCallAssistant) { 202 updateCallAssistantModel(); 203 } 204 } 205 206 /** 207 * Update the configure preference summary when the call assistant changes. 208 */ 209 @Override 210 public void onAccountChanged(AccountSelectionPreference pref) { 211 if (pref == mSelectCallAssistant) { 212 updateConfigureCallAssistantSummary(); 213 } 214 } 215 216 private synchronized void handleSipReceiveCallsOption(boolean isEnabled) { 217 mSipSharedPreferences.setReceivingCallsEnabled(isEnabled); 218 219 SipUtil.useSipToReceiveIncomingCalls(getActivity(), isEnabled); 220 221 // Restart all Sip services to ensure we reflect whether we are receiving calls. 222 SipAccountRegistry sipAccountRegistry = SipAccountRegistry.getInstance(); 223 sipAccountRegistry.restartSipService(getActivity()); 224 } 225 226 /** 227 * Queries the telcomm manager to update the default outgoing account selection preference 228 * with the list of outgoing accounts and the current default outgoing account. 229 */ 230 private void updateDefaultOutgoingAccountsModel() { 231 mDefaultOutgoingAccount.setModel( 232 mTelecomManager, 233 mTelecomManager.getCallCapablePhoneAccounts(), 234 mTelecomManager.getUserSelectedOutgoingPhoneAccount(), 235 getString(R.string.phone_accounts_ask_every_time)); 236 } 237 238 /** 239 * Queries the telecomm manager to update the account selection preference with the list of 240 * call assistants, and the currently selected call assistant. 241 */ 242 public void updateCallAssistantModel() { 243 List<PhoneAccountHandle> simCallManagers = mTelecomManager.getSimCallManagers(); 244 mSelectCallAssistant.setModel( 245 mTelecomManager, 246 simCallManagers, 247 mTelecomManager.getSimCallManager(), 248 getString(R.string.wifi_calling_call_assistant_none)); 249 250 updateConfigureCallAssistantSummary(); 251 } 252 253 /** 254 * Updates the summary on the "configure call assistant" preference. If it is the last entry, 255 * show the summary for when no call assistant is selected. Otherwise, display the currently 256 * selected call assistant. 257 */ 258 private void updateConfigureCallAssistantSummary() { 259 if (mSelectCallAssistant.getEntries().length - 1 260 == mSelectCallAssistant.findIndexOfValue(mSelectCallAssistant.getValue())) { 261 mConfigureCallAssistant.setSummary( 262 R.string.wifi_calling_call_assistant_configure_no_selection); 263 mConfigureCallAssistant.setEnabled(false); 264 } else { 265 mConfigureCallAssistant.setSummary(null); 266 mConfigureCallAssistant.setEnabled(true); 267 } 268 } 269 } 270