Add preliminary support for in-band registration

parent bcbb1c98
......@@ -344,13 +344,19 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
boolean compression, ProxyType proxyType, String proxyHost,
int proxyPort, String proxyUser, String proxyPassword,
boolean syncable, KeyPair keyPair, Date lastSync,
ArchiveMode archiveMode) {
ArchiveMode archiveMode,
boolean registerNewAccount) {
AccountItem accountItem = new AccountItem(protocol, custom, host, port,
serverName, userName, resource, storePassword, password, color,
priority, statusMode, statusText, enabled, saslEnabled,
tlsMode, compression, proxyType, proxyHost, proxyPort,
proxyUser, proxyPassword, syncable, keyPair, lastSync,
archiveMode);
if(registerNewAccount) {
// TODO: attempt to register account, if that fails return null;
accountItem.registerAccount();
//return(null);
}
requestToWriteAccount(accountItem);
addAccount(accountItem);
accountItem.updateConnection(true);
......@@ -371,7 +377,8 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
*/
public String addAccount(String user, String password,
AccountType accountType, boolean syncable, boolean storePassword,
boolean useOrbot) throws NetworkException {
boolean useOrbot,
boolean registerNewAccount) throws NetworkException {
if (accountType.getProtocol().isOAuth()) {
int index = 1;
while (true) {
......@@ -446,7 +453,11 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
SettingsManager.statusText(), true, true,
tlsRequired ? TLSMode.required : TLSMode.enabled, false,
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());
if (accountItems.size() > 1
&& SettingsManager.contactsEnableShowAccounts())
......@@ -609,7 +620,7 @@ public class AccountManager implements OnLoadListener, OnWipeListener {
priority, statusMode, statusText, enabled, saslEnabled,
tlsMode, compression, proxyType, proxyHost, proxyPort,
proxyUser, proxyPassword, syncable, keyPair, lastSync,
archiveMode);
archiveMode, false);
}
onAccountChanged(result.getAccount());
}
......
......@@ -53,6 +53,11 @@ public abstract class ConnectionItem {
*/
private boolean disconnectionRequested;
/**
* Need to register account on XMPP server.
*/
private boolean registerNewAccount;
public ConnectionItem(AccountProtocol protocol, boolean custom,
String host, int port, String serverName, String userName,
String resource, boolean storePassword, String password,
......@@ -69,6 +74,20 @@ public abstract class ConnectionItem {
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.
*
......@@ -152,10 +171,10 @@ public abstract class ConnectionItem {
connectionThread = new ConnectionThread(this);
if (connectionSettings.isCustom())
connectionThread.start(connectionSettings.getHost(),
connectionSettings.getPort(), false);
connectionSettings.getPort(), false, registerNewAccount);
else
connectionThread.start(connectionSettings.getServerName(),
5222, true);
5222, true, registerNewAccount);
return true;
} else {
return false;
......@@ -235,6 +254,17 @@ public abstract class ConnectionItem {
* Connection has been established.
*/
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))
state = ConnectionState.authentication;
}
......@@ -302,7 +332,7 @@ public abstract class ConnectionItem {
if (onDisconnect(connectionThread)) {
state = ConnectionState.connecting;
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 {
*/
connecting,
/**
* Connection was established, registration is in progress.
*/
registration,
/**
* Connection was established, authentication is in progress.
*/
......@@ -73,6 +78,8 @@ public enum ConnectionState {
return R.string.account_state_waiting;
else if (this == ConnectionState.connecting)
return R.string.account_state_connecting;
else if (this == ConnectionState.registration)
return R.string.account_state_registration;
else if (this == ConnectionState.authentication)
return R.string.account_state_authentication;
else if (this == ConnectionState.connected)
......
......@@ -106,6 +106,8 @@ public class ConnectionThread implements
private boolean started;
private boolean registerNewAccount;
public ConnectionThread(final ConnectionItem connectionItem) {
this.connectionItem = connectionItem;
executorService = Executors
......@@ -488,14 +490,60 @@ public class ConnectionThread implements
private void onConnected(final String password) {
connectionItem.onConnected(this);
ConnectionManager.getInstance().onConnected(this);
runOnConnectionThread(new Runnable() {
if(registerNewAccount) {
runOnConnectionThread(new Runnable() {
@Override
public void run() {
registerAccount(password);
}
});
}
else {
runOnConnectionThread(new Runnable() {
@Override
public void run() {
authorization(password);
}
});
}
}
/**
* 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() {
authorization(password);
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.
*
......@@ -621,10 +669,12 @@ public class ConnectionThread implements
* @param useSRVLookup Whether SRV lookup should be used.
*/
synchronized void start(final String fqdn, final int port,
final boolean useSRVLookup) {
final boolean useSRVLookup,
final boolean registerNewAccount) {
if (started)
throw new IllegalStateException();
started = true;
this.registerNewAccount = registerNewAccount;
runOnConnectionThread(new Runnable() {
@Override
public void run() {
......
......@@ -51,6 +51,7 @@ public class AccountAdd extends ManagedActivity implements
private CheckBox storePasswordView;
private CheckBox useOrbotView;
private CheckBox syncableView;
private CheckBox createAccount;
private Spinner accountTypeView;
@Override
......@@ -68,6 +69,7 @@ public class AccountAdd extends ManagedActivity implements
syncableView.setVisibility(View.GONE);
syncableView.setChecked(false);
}
createAccount = (CheckBox) findViewById(R.id.register_account);
accountTypeView = (Spinner) findViewById(R.id.account_type);
accountTypeView.setAdapter(new AccountTypeAdapter(this));
......@@ -121,7 +123,8 @@ public class AccountAdd extends ManagedActivity implements
.getSelectedItem(),
syncableView.isChecked(),
storePasswordView.isChecked(),
useOrbotView.isChecked());
useOrbotView.isChecked(),
false);
} catch (NetworkException e) {
Application.getInstance().onError(e);
return;
......@@ -159,7 +162,8 @@ public class AccountAdd extends ManagedActivity implements
passwordView.getText().toString(), accountType,
syncableView.isChecked(),
storePasswordView.isChecked(),
useOrbotView.isChecked());
useOrbotView.isChecked(),
createAccount.isChecked());
} catch (NetworkException e) {
Application.getInstance().onError(e);
return;
......
......@@ -83,6 +83,13 @@
android:checked="false"
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
android:orientation="horizontal"
android:layout_width="match_parent"
......
......@@ -159,4 +159,5 @@
<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_title">Install Orbot?</string>
</resources>
\ No newline at end of file
<string name="ACCOUNT_REGISTER_FAILED">Failed to register account on the server.</string>
</resources>
......@@ -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>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png -->
<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 -->
<string name="account_delete">Delete account</string>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list_context_menu.png -->
<string name="account_editor">Edit account</string>
<!-- 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>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png -->
<string name="account_state_connected">Online</string>
......@@ -20,4 +23,4 @@
<string name="account_state_offline">Offline</string>
<!-- http://dl.dropbox.com/u/1029995/com.xabber.android/account_list.png -->
<string name="account_state_waiting">Waiting to reconnect</string>
</resources>
\ No newline at end of file
</resources>
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