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,6 +147,11 @@ public class LinphonePreferences {
getLc().addAuthInfo(authInfo);
}
public static class AccountBuilder {
private LinphoneCore lc;
public AccountBuilder(LinphoneCore lc) {
this.lc = lc;
}
private String tempUsername;
private String tempUserId;
private String tempPassword;
@ -156,25 +161,89 @@ public class LinphonePreferences {
private String tempContactsParams;
private String tempExpire;
private TransportType tempTransport;
private boolean tempEnabled = true;
private boolean tempNoDefault = false;
public AccountBuilder setTransport(TransportType transport) {
tempTransport = transport;
return this;
}
public AccountBuilder setUsername(String username) {
tempUsername = username;
return this;
}
public AccountBuilder setPassword(String password) {
tempPassword = password;
return this;
}
public AccountBuilder setDomain(String domain) {
tempDomain = domain;
return this;
}
public AccountBuilder setProxy(String proxy) {
tempProxy = proxy;
return this;
}
public AccountBuilder setOutboundProxyEnabled(boolean enabled) {
tempOutboundProxy = enabled;
return this;
}
public AccountBuilder setContactParameters(String contactParams) {
tempContactsParams = contactParams;
return this;
}
public AccountBuilder setExpires(String expire) {
tempExpire = expire;
return this;
}
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 using values previously set using setNew* functions
* Creates a new account
* @throws LinphoneCoreException
*/
public void saveNewAccount() throws LinphoneCoreException {
String identity = "sip:" + tempUsername + "@" + tempDomain;
String proxy = "sip:";
proxy += tempProxy == null ? tempDomain : tempProxy;
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) {
tempTransport = TransportType.LinphoneTransportUdp;
}
if (tempTransport != null) {
proxyAddr.setTransport(tempTransport);
}
String route = tempOutboundProxy ? proxyAddr.asStringUriOnly() : null;
LinphoneProxyConfig prxCfg = LinphoneCoreFactory.instance().createProxyConfig(identity, proxyAddr.asStringUriOnly(), route, true);
LinphoneProxyConfig prxCfg = LinphoneCoreFactory.instance().createProxyConfig(identity, proxyAddr.asStringUriOnly(), route, tempEnabled);
if (tempContactsParams != null)
prxCfg.setContactUriParameters(tempContactsParams);
if (tempExpire != null) {
@ -185,26 +254,17 @@ public class LinphonePreferences {
LinphoneAuthInfo authInfo = LinphoneCoreFactory.instance().createAuthInfo(tempUsername, tempUserId, tempPassword, null, null, tempDomain);
getLc().addProxyConfig(prxCfg);
getLc().addAuthInfo(authInfo);
lc.addProxyConfig(prxCfg);
lc.addAuthInfo(authInfo);
if (getAccountCount() == 1)
getLc().setDefaultProxyConfig(prxCfg);
tempUsername = null;
tempUserId = null;
tempPassword = null;
tempDomain = null;
tempProxy = null;
tempOutboundProxy = false;
tempContactsParams = null;
tempExpire = null;
tempTransport = null;
if (!tempNoDefault && LinphonePreferences.instance().getAccountCount() == 1)
lc.setDefaultProxyConfig(prxCfg);
}
}
public void setNewAccountTransport(TransportType transport) {
tempTransport = transport;
}
public void setAccountTransport(int n, String transport) {
LinphoneProxyConfig proxyConfig = getProxyConfig(n);
@ -275,9 +335,7 @@ public class LinphonePreferences {
return getString(R.string.pref_transport_udp);
}
public void setNewAccountUsername(String username) {
tempUsername = username;
}
public void setAccountUsername(int n, String username) {
String identity = "sip:" + username + "@" + getAccountDomain(n);
@ -299,10 +357,6 @@ public class LinphonePreferences {
return authInfo == null ? null : authInfo.getUsername();
}
public void setNewAccountUserId(String userId) {
tempUserId = userId;
}
public void setAccountUserId(int n, String userId) {
LinphoneAuthInfo info = getClonedAuthInfo(n);
info.setUserId(userId);
@ -314,10 +368,6 @@ public class LinphonePreferences {
return authInfo == null ? null : authInfo.getUserId();
}
public void setNewAccountPassword(String password) {
tempPassword = password;
}
public void setAccountPassword(int n, String password) {
LinphoneAuthInfo info = getClonedAuthInfo(n);
info.setPassword(password);
@ -329,10 +379,6 @@ public class LinphonePreferences {
return authInfo == null ? null : authInfo.getPassword();
}
public void setNewAccountDomain(String domain) {
tempDomain = domain;
}
public void setAccountDomain(int n, String domain) {
String identity = "sip:" + getAccountUsername(n) + "@" + domain;
@ -353,10 +399,6 @@ public class LinphonePreferences {
return getProxyConfig(n).getDomain();
}
public void setNewAccountProxy(String proxy) {
tempProxy = proxy;
}
public void setAccountProxy(int n, String proxy) {
if (proxy == null || proxy.length() <= 0) {
proxy = getAccountDomain(n);
@ -389,9 +431,6 @@ public class LinphonePreferences {
return proxy;
}
public void setNewAccountOutboundProxyEnabled(boolean enabled) {
tempOutboundProxy = enabled;
}
public void setAccountOutboundProxyEnabled(int n, boolean enabled) {
try {
@ -412,10 +451,6 @@ public class LinphonePreferences {
return getProxyConfig(n).getRoute() != null;
}
public void setNewAccountContactParameters(String contactParams) {
tempContactsParams = contactParams;
}
public void setAccountContactParameters(int n, String contactParams) {
LinphoneProxyConfig prxCfg = getProxyConfig(n);
prxCfg.setContactUriParameters(contactParams);
@ -426,10 +461,6 @@ public class LinphonePreferences {
return String.valueOf(getProxyConfig(n).getExpires());
}
public void setNewAccountExpires(String expire) {
tempExpire = expire;
}
public void setExpires(int n, String expire) {
try {
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.
*/
import org.linphone.LinphoneManager;
import org.linphone.LinphonePreferences.AccountBuilder;
import org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneCoreException;
@ -95,30 +97,31 @@ public class PreferencesMigrator {
String password = getPrefString(getString(R.string.pref_passwd_key) + key, null);
String domain = getPrefString(getString(R.string.pref_domain_key) + key, 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);
mNewPrefs.setNewAccountProxy(proxy);
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)) {
mNewPrefs.setNewAccountOutboundProxyEnabled(true);
builder.setOutboundProxyEnabled(true);
}
if (mResources.getBoolean(R.bool.enable_push_id)) {
String regId = mNewPrefs.getPushNotificationRegistrationID();
String appId = getString(R.string.push_sender_id);
if (regId != null && mNewPrefs.isPushNotificationEnabled()) {
String contactInfos = "app-id=" + appId + ";pn-type=google;pn-tok=" + regId;
mNewPrefs.setNewAccountContactParameters(contactInfos);
builder.setContactParameters(contactInfos);
}
}
try {
mNewPrefs.saveNewAccount();
builder.saveNewAccount();
} catch (LinphoneCoreException e) {
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.LinphonePreferences;
import org.linphone.LinphonePreferences.AccountBuilder;
import org.linphone.LinphoneSimpleListener.LinphoneOnRegistrationStateChangedListener;
import org.linphone.R;
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 useLinphoneDotOrgCustomPorts = getResources().getBoolean(R.bool.use_linphone_server_ports);
mPrefs.setNewAccountUsername(username);
mPrefs.setNewAccountDomain(domain);
mPrefs.setNewAccountPassword(password);
AccountBuilder builder = new AccountBuilder(LinphoneManager.getLc())
.setUsername(username)
.setDomain(domain)
.setPassword(password);
if (isMainAccountLinphoneDotOrg && useLinphoneDotOrgCustomPorts) {
if (getResources().getBoolean(R.bool.disable_all_security_features_for_markets)) {
mPrefs.setNewAccountProxy(domain + ":5228");
mPrefs.setNewAccountTransport(TransportType.LinphoneTransportTcp);
builder.setProxy(domain + ":5228")
.setTransport(TransportType.LinphoneTransportTcp);
}
else {
mPrefs.setNewAccountProxy(domain + ":5223");
mPrefs.setNewAccountTransport(TransportType.LinphoneTransportTls);
builder.setProxy(domain + ":5223")
.setTransport(TransportType.LinphoneTransportTls);
}
mPrefs.setNewAccountExpires("604800");
mPrefs.setNewAccountOutboundProxyEnabled(true);
builder.setExpires("604800")
.setOutboundProxyEnabled(true);
mPrefs.setStunServer(getString(R.string.default_stun));
mPrefs.setIceEnabled(true);
mPrefs.setPushNotificationEnabled(true);
} else {
String forcedProxy = getResources().getString(R.string.setup_forced_proxy);
if (!TextUtils.isEmpty(forcedProxy)) {
mPrefs.setNewAccountProxy(forcedProxy);
mPrefs.setNewAccountOutboundProxyEnabled(true);
builder.setProxy(forcedProxy)
.setOutboundProxyEnabled(true);
}
}
@ -317,12 +319,12 @@ public class SetupActivity extends FragmentActivity implements OnClickListener {
String appId = getString(R.string.push_sender_id);
if (regId != null && mPrefs.isPushNotificationEnabled()) {
String contactInfos = "app-id=" + appId + ";pn-type=google;pn-tok=" + regId;
mPrefs.setNewAccountContactParameters(contactInfos);
builder.setContactParameters(contactInfos);
}
}
try {
mPrefs.saveNewAccount();
builder.saveNewAccount();
accountCreated = true;
} catch (LinphoneCoreException e) {
e.printStackTrace();

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

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