Use Builder to create a new SIP account.

This commit is contained in:
Guillaume Beraudo 2014-03-11 15:02:19 +01:00
parent 3018cba56c
commit e3fa48e645
5 changed files with 145 additions and 109 deletions

View file

@ -147,64 +147,124 @@ public class LinphonePreferences {
getLc().addAuthInfo(authInfo); getLc().addAuthInfo(authInfo);
} }
private String tempUsername; public static class AccountBuilder {
private String tempUserId; private LinphoneCore lc;
private String tempPassword; public AccountBuilder(LinphoneCore lc) {
private String tempDomain; this.lc = lc;
private String tempProxy;
private boolean tempOutboundProxy;
private String tempContactsParams;
private String tempExpire;
private TransportType tempTransport;
/**
* Creates a new account using values previously set using setNew* functions
* @throws LinphoneCoreException
*/
public void saveNewAccount() throws LinphoneCoreException {
String identity = "sip:" + tempUsername + "@" + tempDomain;
String proxy = "sip:";
proxy += tempProxy == null ? tempDomain : tempProxy;
LinphoneAddress proxyAddr = LinphoneCoreFactory.instance().createLinphoneAddress(proxy);
if (tempTransport == null) {
tempTransport = TransportType.LinphoneTransportUdp;
} }
proxyAddr.setTransport(tempTransport); private String tempUsername;
private String tempUserId;
private String tempPassword;
private String tempDomain;
private String tempProxy;
private boolean tempOutboundProxy;
private String tempContactsParams;
private String tempExpire;
private TransportType tempTransport;
private boolean tempEnabled = true;
private boolean tempNoDefault = false;
String route = tempOutboundProxy ? proxyAddr.asStringUriOnly() : null; public AccountBuilder setTransport(TransportType transport) {
tempTransport = transport;
LinphoneProxyConfig prxCfg = LinphoneCoreFactory.instance().createProxyConfig(identity, proxyAddr.asStringUriOnly(), route, true); return this;
if (tempContactsParams != null) }
prxCfg.setContactUriParameters(tempContactsParams); public AccountBuilder setUsername(String username) {
if (tempExpire != null) { tempUsername = username;
try { return this;
prxCfg.setExpires(Integer.parseInt(tempExpire));
} catch (NumberFormatException nfe) { }
} }
LinphoneAuthInfo authInfo = LinphoneCoreFactory.instance().createAuthInfo(tempUsername, tempUserId, tempPassword, null, null, tempDomain); public AccountBuilder setPassword(String password) {
tempPassword = password;
return this;
}
getLc().addProxyConfig(prxCfg); public AccountBuilder setDomain(String domain) {
getLc().addAuthInfo(authInfo); tempDomain = domain;
return this;
}
if (getAccountCount() == 1) public AccountBuilder setProxy(String proxy) {
getLc().setDefaultProxyConfig(prxCfg); tempProxy = proxy;
return this;
}
public AccountBuilder setOutboundProxyEnabled(boolean enabled) {
tempOutboundProxy = enabled;
return this;
}
tempUsername = null; public AccountBuilder setContactParameters(String contactParams) {
tempUserId = null; tempContactsParams = contactParams;
tempPassword = null; return this;
tempDomain = null; }
tempProxy = null;
tempOutboundProxy = false; public AccountBuilder setExpires(String expire) {
tempContactsParams = null; tempExpire = expire;
tempExpire = null; return this;
tempTransport = null; }
public AccountBuilder setUserId(String userId) {
tempUserId = userId;
return this;
}
public AccountBuilder setEnabled(boolean enable) {
tempEnabled = enable;
return this;
}
public AccountBuilder setNoDefault(boolean yesno) {
tempNoDefault = yesno;
return this;
}
/**
* Creates a new account
* @throws LinphoneCoreException
*/
public void saveNewAccount() throws LinphoneCoreException {
String identity = "sip:" + tempUsername + "@" + tempDomain;
String proxy = "sip:";
if (tempProxy == null) {
proxy += tempDomain;
} else {
if (!tempProxy.startsWith("sip:") && !tempProxy.startsWith("<sip:")
&& !tempProxy.startsWith("sips:") && !tempProxy.startsWith("<sips:")) {
proxy += tempProxy;
} else {
proxy = tempProxy;
}
}
LinphoneAddress proxyAddr = LinphoneCoreFactory.instance().createLinphoneAddress(proxy);
if (tempTransport != null) {
proxyAddr.setTransport(tempTransport);
}
String route = tempOutboundProxy ? proxyAddr.asStringUriOnly() : null;
LinphoneProxyConfig prxCfg = LinphoneCoreFactory.instance().createProxyConfig(identity, proxyAddr.asStringUriOnly(), route, tempEnabled);
if (tempContactsParams != null)
prxCfg.setContactUriParameters(tempContactsParams);
if (tempExpire != null) {
try {
prxCfg.setExpires(Integer.parseInt(tempExpire));
} catch (NumberFormatException nfe) { }
}
LinphoneAuthInfo authInfo = LinphoneCoreFactory.instance().createAuthInfo(tempUsername, tempUserId, tempPassword, null, null, tempDomain);
lc.addProxyConfig(prxCfg);
lc.addAuthInfo(authInfo);
if (!tempNoDefault && LinphonePreferences.instance().getAccountCount() == 1)
lc.setDefaultProxyConfig(prxCfg);
}
} }
public void setNewAccountTransport(TransportType transport) {
tempTransport = transport;
}
public void setAccountTransport(int n, String transport) { public void setAccountTransport(int n, String transport) {
LinphoneProxyConfig proxyConfig = getProxyConfig(n); LinphoneProxyConfig proxyConfig = getProxyConfig(n);
@ -275,9 +335,7 @@ public class LinphonePreferences {
return getString(R.string.pref_transport_udp); return getString(R.string.pref_transport_udp);
} }
public void setNewAccountUsername(String username) {
tempUsername = username;
}
public void setAccountUsername(int n, String username) { public void setAccountUsername(int n, String username) {
String identity = "sip:" + username + "@" + getAccountDomain(n); String identity = "sip:" + username + "@" + getAccountDomain(n);
@ -299,10 +357,6 @@ public class LinphonePreferences {
return authInfo == null ? null : authInfo.getUsername(); return authInfo == null ? null : authInfo.getUsername();
} }
public void setNewAccountUserId(String userId) {
tempUserId = userId;
}
public void setAccountUserId(int n, String userId) { public void setAccountUserId(int n, String userId) {
LinphoneAuthInfo info = getClonedAuthInfo(n); LinphoneAuthInfo info = getClonedAuthInfo(n);
info.setUserId(userId); info.setUserId(userId);
@ -314,10 +368,6 @@ public class LinphonePreferences {
return authInfo == null ? null : authInfo.getUserId(); return authInfo == null ? null : authInfo.getUserId();
} }
public void setNewAccountPassword(String password) {
tempPassword = password;
}
public void setAccountPassword(int n, String password) { public void setAccountPassword(int n, String password) {
LinphoneAuthInfo info = getClonedAuthInfo(n); LinphoneAuthInfo info = getClonedAuthInfo(n);
info.setPassword(password); info.setPassword(password);
@ -329,10 +379,6 @@ public class LinphonePreferences {
return authInfo == null ? null : authInfo.getPassword(); return authInfo == null ? null : authInfo.getPassword();
} }
public void setNewAccountDomain(String domain) {
tempDomain = domain;
}
public void setAccountDomain(int n, String domain) { public void setAccountDomain(int n, String domain) {
String identity = "sip:" + getAccountUsername(n) + "@" + domain; String identity = "sip:" + getAccountUsername(n) + "@" + domain;
@ -353,10 +399,6 @@ public class LinphonePreferences {
return getProxyConfig(n).getDomain(); return getProxyConfig(n).getDomain();
} }
public void setNewAccountProxy(String proxy) {
tempProxy = proxy;
}
public void setAccountProxy(int n, String proxy) { public void setAccountProxy(int n, String proxy) {
if (proxy == null || proxy.length() <= 0) { if (proxy == null || proxy.length() <= 0) {
proxy = getAccountDomain(n); proxy = getAccountDomain(n);
@ -389,9 +431,6 @@ public class LinphonePreferences {
return proxy; return proxy;
} }
public void setNewAccountOutboundProxyEnabled(boolean enabled) {
tempOutboundProxy = enabled;
}
public void setAccountOutboundProxyEnabled(int n, boolean enabled) { public void setAccountOutboundProxyEnabled(int n, boolean enabled) {
try { try {
@ -412,10 +451,6 @@ public class LinphonePreferences {
return getProxyConfig(n).getRoute() != null; return getProxyConfig(n).getRoute() != null;
} }
public void setNewAccountContactParameters(String contactParams) {
tempContactsParams = contactParams;
}
public void setAccountContactParameters(int n, String contactParams) { public void setAccountContactParameters(int n, String contactParams) {
LinphoneProxyConfig prxCfg = getProxyConfig(n); LinphoneProxyConfig prxCfg = getProxyConfig(n);
prxCfg.setContactUriParameters(contactParams); prxCfg.setContactUriParameters(contactParams);
@ -426,10 +461,6 @@ public class LinphonePreferences {
return String.valueOf(getProxyConfig(n).getExpires()); return String.valueOf(getProxyConfig(n).getExpires());
} }
public void setNewAccountExpires(String expire) {
tempExpire = expire;
}
public void setExpires(int n, String expire) { public void setExpires(int n, String expire) {
try { try {
LinphoneProxyConfig prxCfg = getProxyConfig(n); LinphoneProxyConfig prxCfg = getProxyConfig(n);

View file

@ -19,6 +19,8 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
import org.linphone.LinphoneManager;
import org.linphone.LinphonePreferences.AccountBuilder;
import org.linphone.core.LinphoneCore; import org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneCoreException; import org.linphone.core.LinphoneCoreException;
@ -95,30 +97,31 @@ public class PreferencesMigrator {
String password = getPrefString(getString(R.string.pref_passwd_key) + key, null); String password = getPrefString(getString(R.string.pref_passwd_key) + key, null);
String domain = getPrefString(getString(R.string.pref_domain_key) + key, null); String domain = getPrefString(getString(R.string.pref_domain_key) + key, null);
if (username != null && username.length() > 0 && password != null) { if (username != null && username.length() > 0 && password != null) {
mNewPrefs.setNewAccountUsername(username);
mNewPrefs.setNewAccountUserId(userid);
mNewPrefs.setNewAccountDomain(domain);
mNewPrefs.setNewAccountPassword(password);
String proxy = getPrefString(getString(R.string.pref_proxy_key) + key, null); String proxy = getPrefString(getString(R.string.pref_proxy_key) + key, null);
mNewPrefs.setNewAccountProxy(proxy);
String expire = getPrefString(R.string.pref_expire_key, null); String expire = getPrefString(R.string.pref_expire_key, null);
mNewPrefs.setNewAccountExpires(expire);
AccountBuilder builder = new AccountBuilder(LinphoneManager.getLc())
.setUsername(username)
.setUserId(userid)
.setDomain(domain)
.setPassword(password)
.setProxy(proxy)
.setExpires(expire);
if (getPrefBoolean(getString(R.string.pref_enable_outbound_proxy_key) + key, false)) { if (getPrefBoolean(getString(R.string.pref_enable_outbound_proxy_key) + key, false)) {
mNewPrefs.setNewAccountOutboundProxyEnabled(true); builder.setOutboundProxyEnabled(true);
} }
if (mResources.getBoolean(R.bool.enable_push_id)) { if (mResources.getBoolean(R.bool.enable_push_id)) {
String regId = mNewPrefs.getPushNotificationRegistrationID(); String regId = mNewPrefs.getPushNotificationRegistrationID();
String appId = getString(R.string.push_sender_id); String appId = getString(R.string.push_sender_id);
if (regId != null && mNewPrefs.isPushNotificationEnabled()) { if (regId != null && mNewPrefs.isPushNotificationEnabled()) {
String contactInfos = "app-id=" + appId + ";pn-type=google;pn-tok=" + regId; String contactInfos = "app-id=" + appId + ";pn-type=google;pn-tok=" + regId;
mNewPrefs.setNewAccountContactParameters(contactInfos); builder.setContactParameters(contactInfos);
} }
} }
try { try {
mNewPrefs.saveNewAccount(); builder.saveNewAccount();
} catch (LinphoneCoreException e) { } catch (LinphoneCoreException e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
import org.linphone.LinphoneManager; import org.linphone.LinphoneManager;
import org.linphone.LinphonePreferences; import org.linphone.LinphonePreferences;
import org.linphone.LinphonePreferences.AccountBuilder;
import org.linphone.LinphoneSimpleListener.LinphoneOnRegistrationStateChangedListener; import org.linphone.LinphoneSimpleListener.LinphoneOnRegistrationStateChangedListener;
import org.linphone.R; import org.linphone.R;
import org.linphone.core.LinphoneAddress.TransportType; import org.linphone.core.LinphoneAddress.TransportType;
@ -285,30 +286,31 @@ public class SetupActivity extends FragmentActivity implements OnClickListener {
boolean isMainAccountLinphoneDotOrg = domain.equals(getString(R.string.default_domain)); boolean isMainAccountLinphoneDotOrg = domain.equals(getString(R.string.default_domain));
boolean useLinphoneDotOrgCustomPorts = getResources().getBoolean(R.bool.use_linphone_server_ports); boolean useLinphoneDotOrgCustomPorts = getResources().getBoolean(R.bool.use_linphone_server_ports);
mPrefs.setNewAccountUsername(username); AccountBuilder builder = new AccountBuilder(LinphoneManager.getLc())
mPrefs.setNewAccountDomain(domain); .setUsername(username)
mPrefs.setNewAccountPassword(password); .setDomain(domain)
.setPassword(password);
if (isMainAccountLinphoneDotOrg && useLinphoneDotOrgCustomPorts) { if (isMainAccountLinphoneDotOrg && useLinphoneDotOrgCustomPorts) {
if (getResources().getBoolean(R.bool.disable_all_security_features_for_markets)) { if (getResources().getBoolean(R.bool.disable_all_security_features_for_markets)) {
mPrefs.setNewAccountProxy(domain + ":5228"); builder.setProxy(domain + ":5228")
mPrefs.setNewAccountTransport(TransportType.LinphoneTransportTcp); .setTransport(TransportType.LinphoneTransportTcp);
} }
else { else {
mPrefs.setNewAccountProxy(domain + ":5223"); builder.setProxy(domain + ":5223")
mPrefs.setNewAccountTransport(TransportType.LinphoneTransportTls); .setTransport(TransportType.LinphoneTransportTls);
} }
mPrefs.setNewAccountExpires("604800"); builder.setExpires("604800")
mPrefs.setNewAccountOutboundProxyEnabled(true); .setOutboundProxyEnabled(true);
mPrefs.setStunServer(getString(R.string.default_stun)); mPrefs.setStunServer(getString(R.string.default_stun));
mPrefs.setIceEnabled(true); mPrefs.setIceEnabled(true);
mPrefs.setPushNotificationEnabled(true); mPrefs.setPushNotificationEnabled(true);
} else { } else {
String forcedProxy = getResources().getString(R.string.setup_forced_proxy); String forcedProxy = getResources().getString(R.string.setup_forced_proxy);
if (!TextUtils.isEmpty(forcedProxy)) { if (!TextUtils.isEmpty(forcedProxy)) {
mPrefs.setNewAccountProxy(forcedProxy); builder.setProxy(forcedProxy)
mPrefs.setNewAccountOutboundProxyEnabled(true); .setOutboundProxyEnabled(true);
} }
} }
@ -317,12 +319,12 @@ public class SetupActivity extends FragmentActivity implements OnClickListener {
String appId = getString(R.string.push_sender_id); String appId = getString(R.string.push_sender_id);
if (regId != null && mPrefs.isPushNotificationEnabled()) { if (regId != null && mPrefs.isPushNotificationEnabled()) {
String contactInfos = "app-id=" + appId + ";pn-type=google;pn-tok=" + regId; String contactInfos = "app-id=" + appId + ";pn-type=google;pn-tok=" + regId;
mPrefs.setNewAccountContactParameters(contactInfos); builder.setContactParameters(contactInfos);
} }
} }
try { try {
mPrefs.saveNewAccount(); builder.saveNewAccount();
accountCreated = true; accountCreated = true;
} catch (LinphoneCoreException e) { } catch (LinphoneCoreException e) {
e.printStackTrace(); e.printStackTrace();

@ -1 +1 @@
Subproject commit dbb21ad93461aa3cdf9663f1ba2cdb7dfb568ce4 Subproject commit 55b0f5439ad07c9829f6dd88ae1a9dbfdaca851d

@ -1 +1 @@
Subproject commit aad2df16d1945abd71bef720d079515383cf88d4 Subproject commit a45d28a328be70f037995c48dd4742fbb1eb9519