Add preliminary support for in-band registration

parent bcbb1c98
...@@ -344,13 +344,19 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -344,13 +344,19 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
boolean compression, ProxyType proxyType, String proxyHost, boolean compression, ProxyType proxyType, String proxyHost,
int proxyPort, String proxyUser, String proxyPassword, int proxyPort, String proxyUser, String proxyPassword,
boolean syncable, KeyPair keyPair, Date lastSync, boolean syncable, KeyPair keyPair, Date lastSync,
ArchiveMode archiveMode) { ArchiveMode archiveMode,
boolean registerNewAccount) {
AccountItem accountItem = new AccountItem(protocol, custom, host, port, AccountItem accountItem = new AccountItem(protocol, custom, host, port,
serverName, userName, resource, storePassword, password, color, serverName, userName, resource, storePassword, password, color,
priority, statusMode, statusText, enabled, saslEnabled, priority, statusMode, statusText, enabled, saslEnabled,
tlsMode, compression, proxyType, proxyHost, proxyPort, tlsMode, compression, proxyType, proxyHost, proxyPort,
proxyUser, proxyPassword, syncable, keyPair, lastSync, proxyUser, proxyPassword, syncable, keyPair, lastSync,
archiveMode); archiveMode);
if(registerNewAccount) {
// TODO: attempt to register account, if that fails return null;
accountItem.registerAccount();
//return(null);
}
requestToWriteAccount(accountItem); requestToWriteAccount(accountItem);
addAccount(accountItem); addAccount(accountItem);
accountItem.updateConnection(true); accountItem.updateConnection(true);
...@@ -371,7 +377,8 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -371,7 +377,8 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/ */
public String addAccount(String user, String password, public String addAccount(String user, String password,
AccountType accountType, boolean syncable, boolean storePassword, AccountType accountType, boolean syncable, boolean storePassword,
boolean useOrbot) throws NetworkException { boolean useOrbot,
boolean registerNewAccount) throws NetworkException {
if (accountType.getProtocol().isOAuth()) { if (accountType.getProtocol().isOAuth()) {
int index = 1; int index = 1;
while (true) { while (true) {
...@@ -446,7 +453,11 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -446,7 +453,11 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
SettingsManager.statusText(), true, true, SettingsManager.statusText(), true, true,
tlsRequired ? TLSMode.required : TLSMode.enabled, false, tlsRequired ? TLSMode.required : TLSMode.enabled, false,
useOrbot ? ProxyType.orbot : ProxyType.none, "localhost", 8080, useOrbot ? ProxyType.orbot : ProxyType.none, "localhost", 8080,
"", "", syncable, null, null, ArchiveMode.available); "", "", syncable, null, null, ArchiveMode.available, registerNewAccount);
if(accountItem == null) {
throw new NetworkException(R.string.ACCOUNT_REGISTER_FAILED);
}
onAccountChanged(accountItem.getAccount()); onAccountChanged(accountItem.getAccount());
if (accountItems.size() > 1 if (accountItems.size() > 1
&& SettingsManager.contactsEnableShowAccounts()) && SettingsManager.contactsEnableShowAccounts())
...@@ -609,7 +620,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener { ...@@ -609,7 +620,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
priority, statusMode, statusText, enabled, saslEnabled, priority, statusMode, statusText, enabled, saslEnabled,
tlsMode, compression, proxyType, proxyHost, proxyPort, tlsMode, compression, proxyType, proxyHost, proxyPort,
proxyUser, proxyPassword, syncable, keyPair, lastSync, proxyUser, proxyPassword, syncable, keyPair, lastSync,
archiveMode); archiveMode, false);
} }
onAccountChanged(result.getAccount()); onAccountChanged(result.getAccount());
} }
......
...@@ -53,6 +53,11 @@ public abstract class ConnectionItem { ...@@ -53,6 +53,11 @@ public abstract class ConnectionItem {
*/ */
private boolean disconnectionRequested; private boolean disconnectionRequested;
/**
* Need to register account on XMPP server.
*/
private boolean registerNewAccount;
public ConnectionItem(AccountProtocol protocol, boolean custom, public ConnectionItem(AccountProtocol protocol, boolean custom,
String host, int port, String serverName, String userName, String host, int port, String serverName, String userName,
String resource, boolean storePassword, String password, String resource, boolean storePassword, String password,
...@@ -69,6 +74,20 @@ public abstract class ConnectionItem { ...@@ -69,6 +74,20 @@ public abstract class ConnectionItem {
state = ConnectionState.offline; state = ConnectionState.offline;
} }
/**
* Register new account on server.
*/
public void registerAccount() {
registerNewAccount = true;
}
/**
* Report if this connection is to register a new account on XMPP server.
*/
public boolean isRegisterAccount() {
return(registerNewAccount);
}
/** /**
* Gets current connection thread. * Gets current connection thread.
* *
...@@ -152,10 +171,10 @@ public abstract class ConnectionItem { ...@@ -152,10 +171,10 @@ public abstract class ConnectionItem {
connectionThread = new ConnectionThread(this); connectionThread = new ConnectionThread(this);
if (connectionSettings.isCustom()) if (connectionSettings.isCustom())
connectionThread.start(connectionSettings.getHost(), connectionThread.start(connectionSettings.getHost(),
connectionSettings.getPort(), false); connectionSettings.getPort(), false, registerNewAccount);
else else
connectionThread.start(connectionSettings.getServerName(), connectionThread.start(connectionSettings.getServerName(),
5222, true); 5222, true, registerNewAccount);
return true; return true;
} else { } else {
return false; return false;
...@@ -235,6 +254,17 @@ public abstract class ConnectionItem { ...@@ -235,6 +254,17 @@ public abstract class ConnectionItem {
* Connection has been established. * Connection has been established.
*/ */
protected void onConnected(ConnectionThread connectionThread) { protected void onConnected(ConnectionThread connectionThread) {
if (isRegisterAccount())
state = ConnectionState.registration;
else if (isManaged(connectionThread))
state = ConnectionState.authentication;
}
/**
* New account has been registered on XMPP server.
*/
protected void onAccountRegistered(ConnectionThread connectionThread) {
registerNewAccount = false;
if (isManaged(connectionThread)) if (isManaged(connectionThread))
state = ConnectionState.authentication; state = ConnectionState.authentication;
} }
...@@ -302,7 +332,7 @@ public abstract class ConnectionItem { ...@@ -302,7 +332,7 @@ public abstract class ConnectionItem {
if (onDisconnect(connectionThread)) { if (onDisconnect(connectionThread)) {
state = ConnectionState.connecting; state = ConnectionState.connecting;
this.connectionThread = new ConnectionThread(this); this.connectionThread = new ConnectionThread(this);
this.connectionThread.start(fqdn, port, useSrvLookup); this.connectionThread.start(fqdn, port, useSrvLookup, registerNewAccount);
} }
} }
......
...@@ -38,6 +38,11 @@ public enum ConnectionState { ...@@ -38,6 +38,11 @@ public enum ConnectionState {
*/ */
connecting, connecting,
/**
* Connection was established, registration is in progress.
*/
registration,
/** /**
* Connection was established, authentication is in progress. * Connection was established, authentication is in progress.
*/ */
...@@ -73,6 +78,8 @@ public enum ConnectionState { ...@@ -73,6 +78,8 @@ public enum ConnectionState {
return R.string.account_state_waiting; return R.string.account_state_waiting;
else if (this == ConnectionState.connecting) else if (this == ConnectionState.connecting)
return R.string.account_state_connecting; return R.string.account_state_connecting;
else if (this == ConnectionState.registration)
return R.string.account_state_registration;
else if (this == ConnectionState.authentication) else if (this == ConnectionState.authentication)
return R.string.account_state_authentication; return R.string.account_state_authentication;
else if (this == ConnectionState.connected) else if (this == ConnectionState.connected)
......
...@@ -106,6 +106,8 @@ public class ConnectionThread implements ...@@ -106,6 +106,8 @@ public class ConnectionThread implements
private boolean started; private boolean started;
private boolean registerNewAccount;
public ConnectionThread(final ConnectionItem connectionItem) { public ConnectionThread(final ConnectionItem connectionItem) {
this.connectionItem = connectionItem; this.connectionItem = connectionItem;
executorService = Executors executorService = Executors
...@@ -488,6 +490,15 @@ public class ConnectionThread implements ...@@ -488,6 +490,15 @@ public class ConnectionThread implements
private void onConnected(final String password) { private void onConnected(final String password) {
connectionItem.onConnected(this); connectionItem.onConnected(this);
ConnectionManager.getInstance().onConnected(this); ConnectionManager.getInstance().onConnected(this);
if(registerNewAccount) {
runOnConnectionThread(new Runnable() {
@Override
public void run() {
registerAccount(password);
}
});
}
else {
runOnConnectionThread(new Runnable() { runOnConnectionThread(new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -495,6 +506,43 @@ public class ConnectionThread implements ...@@ -495,6 +506,43 @@ public class ConnectionThread implements
} }
}); });
} }
}
/**
* Register new account.
*
* @param password
*/
private void registerAccount(final String password) {
try {
xmppConnection.getAccountManager().createAccount(login, password);
}
catch (XMPPException e) {
LogManager.exception(connectionItem, e);
connectionClosedOnError(e);
// Server will destroy connection, but we can speedup
// it.
xmppConnection.disconnect();
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
onAccountRegistered(password);
}
});
}
/**
* New account has been registerd on the server.
*
* @param password
*/
private void onAccountRegistered(final String password) {
LogManager.i(this, "Account registered");
connectionItem.onAccountRegistered(this);
authorization(password);
}
/** /**
* Login. * Login.
...@@ -621,10 +669,12 @@ public class ConnectionThread implements ...@@ -621,10 +669,12 @@ public class ConnectionThread implements
* @param useSRVLookup Whether SRV lookup should be used. * @param useSRVLookup Whether SRV lookup should be used.
*/ */
synchronized void start(final String fqdn, final int port, synchronized void start(final String fqdn, final int port,
final boolean useSRVLookup) { final boolean useSRVLookup,
final boolean registerNewAccount) {
if (started) if (started)
throw new IllegalStateException(); throw new IllegalStateException();
started = true; started = true;
this.registerNewAccount = registerNewAccount;
runOnConnectionThread(new Runnable() { runOnConnectionThread(new Runnable() {
@Override @Override
public void run() { public void run() {
......
...@@ -51,6 +51,7 @@ public class AccountAdd extends ManagedActivity implements ...@@ -51,6 +51,7 @@ public class AccountAdd extends ManagedActivity implements
private CheckBox storePasswordView; private CheckBox storePasswordView;
private CheckBox useOrbotView; private CheckBox useOrbotView;
private CheckBox syncableView; private CheckBox syncableView;
private CheckBox createAccount;
private Spinner accountTypeView; private Spinner accountTypeView;
@Override @Override
...@@ -68,6 +69,7 @@ public class AccountAdd extends ManagedActivity implements ...@@ -68,6 +69,7 @@ public class AccountAdd extends ManagedActivity implements
syncableView.setVisibility(View.GONE); syncableView.setVisibility(View.GONE);
syncableView.setChecked(false); syncableView.setChecked(false);
} }
createAccount = (CheckBox) findViewById(R.id.register_account);
accountTypeView = (Spinner) findViewById(R.id.account_type); accountTypeView = (Spinner) findViewById(R.id.account_type);
accountTypeView.setAdapter(new AccountTypeAdapter(this)); accountTypeView.setAdapter(new AccountTypeAdapter(this));
...@@ -121,7 +123,8 @@ public class AccountAdd extends ManagedActivity implements ...@@ -121,7 +123,8 @@ public class AccountAdd extends ManagedActivity implements
.getSelectedItem(), .getSelectedItem(),
syncableView.isChecked(), syncableView.isChecked(),
storePasswordView.isChecked(), storePasswordView.isChecked(),
useOrbotView.isChecked()); useOrbotView.isChecked(),
false);
} catch (NetworkException e) { } catch (NetworkException e) {
Application.getInstance().onError(e); Application.getInstance().onError(e);
return; return;
...@@ -159,7 +162,8 @@ public class AccountAdd extends ManagedActivity implements ...@@ -159,7 +162,8 @@ public class AccountAdd extends ManagedActivity implements
passwordView.getText().toString(), accountType, passwordView.getText().toString(), accountType,
syncableView.isChecked(), syncableView.isChecked(),
storePasswordView.isChecked(), storePasswordView.isChecked(),
useOrbotView.isChecked()); useOrbotView.isChecked(),
createAccount.isChecked());
} catch (NetworkException e) { } catch (NetworkException e) {
Application.getInstance().onError(e); Application.getInstance().onError(e);
return; return;
......
...@@ -83,6 +83,13 @@ ...@@ -83,6 +83,13 @@
android:checked="false" android:checked="false"
android:text="@string/account_use_orbot" /> android:text="@string/account_use_orbot" />
<CheckBox
android:id="@+id/register_account"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/account_register" />
<LinearLayout <LinearLayout
android:orientation="horizontal" android:orientation="horizontal"
android:layout_width="match_parent" android:layout_width="match_parent"
......
...@@ -159,4 +159,5 @@ ...@@ -159,4 +159,5 @@
<string name="INCORRECT_USER_NAME">Incorrect user name. Check help text below for details.</string> <string name="INCORRECT_USER_NAME">Incorrect user name. Check help text below for details.</string>
<string name="orbot_required_message">In order to process using TOR you must have Orbot installed and activated to proxy traffic through it. Would you like to install it from Google Play?</string> <string name="orbot_required_message">In order to process using TOR you must have Orbot installed and activated to proxy traffic through it. Would you like to install it from Google Play?</string>
<string name="orbot_required_title">Install Orbot?</string> <string name="orbot_required_title">Install Orbot?</string>
<string name="ACCOUNT_REGISTER_FAILED">Failed to register account on the server.</string>
</resources> </resources>
...@@ -4,11 +4,14 @@ ...@@ -4,11 +4,14 @@
<string name="account_delete_confirm">Do you really want to delete account %s?\n(it won\'t be deleted from server, just from Xabber)</string> <string name="account_delete_confirm">Do you really want to delete account %s?\n(it won\'t be deleted from server, just from Xabber)</string>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png --> <!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png -->
<string name="account_add">Add account</string> <string name="account_add">Add account</string>
<string name="account_register">Register new account</string>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list_context_menu.png --> <!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list_context_menu.png -->
<string name="account_delete">Delete account</string> <string name="account_delete">Delete account</string>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list_context_menu.png --> <!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list_context_menu.png -->
<string name="account_editor">Edit account</string> <string name="account_editor">Edit account</string>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png --> <!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png -->
<string name="account_state_registration">Registering</string>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png -->
<string name="account_state_authentication">Authorizing</string> <string name="account_state_authentication">Authorizing</string>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png --> <!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png -->
<string name="account_state_connected">Online</string> <string name="account_state_connected">Online</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment