Commit e3de3ad8 authored by Grigory Fedorov's avatar Grigory Fedorov

ConnectionItem: light refactoring.

parent 4a1fae99
...@@ -116,14 +116,17 @@ public abstract class ConnectionItem { ...@@ -116,14 +116,17 @@ public abstract class ConnectionItem {
*/ */
public String getRealJid() { public String getRealJid() {
ConnectionThread connectionThread = getConnectionThread(); ConnectionThread connectionThread = getConnectionThread();
if (connectionThread == null) if (connectionThread == null) {
return null; return null;
}
XMPPConnection xmppConnection = connectionThread.getXMPPConnection(); XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
if (xmppConnection == null) if (xmppConnection == null) {
return null; return null;
}
String user = xmppConnection.getUser(); String user = xmppConnection.getUser();
if (user == null) if (user == null) {
return null; return null;
}
return user; return user;
} }
...@@ -145,13 +148,12 @@ public abstract class ConnectionItem { ...@@ -145,13 +148,12 @@ public abstract class ConnectionItem {
boolean available = isConnectionAvailable(userRequest); boolean available = isConnectionAvailable(userRequest);
if (NetworkManager.getInstance().getState() != NetworkState.available if (NetworkManager.getInstance().getState() != NetworkState.available
|| !available || disconnectionRequested) { || !available || disconnectionRequested) {
ConnectionState target = available ? ConnectionState.waiting ConnectionState target = available ? ConnectionState.waiting : ConnectionState.offline;
: ConnectionState.offline; if (state == ConnectionState.connected || state == ConnectionState.authentication
if (state == ConnectionState.connected
|| state == ConnectionState.authentication
|| state == ConnectionState.connecting) { || state == ConnectionState.connecting) {
if (userRequest) if (userRequest) {
connectionRequest = false; connectionRequest = false;
}
if (connectionThread != null) { if (connectionThread != null) {
disconnect(connectionThread); disconnect(connectionThread);
// Force remove managed connection thread. // Force remove managed connection thread.
...@@ -164,18 +166,19 @@ public abstract class ConnectionItem { ...@@ -164,18 +166,19 @@ public abstract class ConnectionItem {
state = target; state = target;
return true; return true;
} else { } else {
if (state == ConnectionState.offline if (state == ConnectionState.offline || state == ConnectionState.waiting) {
|| state == ConnectionState.waiting) { if (userRequest) {
if (userRequest)
connectionRequest = true; connectionRequest = true;
}
state = ConnectionState.connecting; state = ConnectionState.connecting;
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, registerNewAccount); connectionSettings.getPort(), false, registerNewAccount);
else } else {
connectionThread.start(connectionSettings.getServerName(), connectionThread.start(connectionSettings.getServerName(),
5222, true, registerNewAccount); 5222, true, registerNewAccount);
}
return true; return true;
} else { } else {
return false; return false;
...@@ -187,8 +190,9 @@ public abstract class ConnectionItem { ...@@ -187,8 +190,9 @@ public abstract class ConnectionItem {
* Disconnect and connect using new connection. * Disconnect and connect using new connection.
*/ */
public void forceReconnect() { public void forceReconnect() {
if (!getState().isConnectable()) if (!getState().isConnectable()) {
return; return;
}
disconnectionRequested = true; disconnectionRequested = true;
boolean request = connectionRequest; boolean request = connectionRequest;
connectionRequest = false; connectionRequest = false;
...@@ -253,10 +257,11 @@ public abstract class ConnectionItem { ...@@ -253,10 +257,11 @@ public abstract class ConnectionItem {
* Connection has been established. * Connection has been established.
*/ */
protected void onConnected(ConnectionThread connectionThread) { protected void onConnected(ConnectionThread connectionThread) {
if (isRegisterAccount()) if (isRegisterAccount()) {
state = ConnectionState.registration; state = ConnectionState.registration;
else if (isManaged(connectionThread)) } else if (isManaged(connectionThread)) {
state = ConnectionState.authentication; state = ConnectionState.authentication;
}
} }
/** /**
...@@ -264,8 +269,9 @@ public abstract class ConnectionItem { ...@@ -264,8 +269,9 @@ public abstract class ConnectionItem {
*/ */
protected void onAccountRegistered(ConnectionThread connectionThread) { protected void onAccountRegistered(ConnectionThread connectionThread) {
registerNewAccount = false; registerNewAccount = false;
if (isManaged(connectionThread)) if (isManaged(connectionThread)) {
state = ConnectionState.authentication; state = ConnectionState.authentication;
}
} }
/** /**
...@@ -278,8 +284,9 @@ public abstract class ConnectionItem { ...@@ -278,8 +284,9 @@ public abstract class ConnectionItem {
* Authorization passed. * Authorization passed.
*/ */
protected void onAuthorized(ConnectionThread connectionThread) { protected void onAuthorized(ConnectionThread connectionThread) {
if (isManaged(connectionThread)) if (isManaged(connectionThread)) {
state = ConnectionState.connected; state = ConnectionState.connected;
}
} }
/** /**
...@@ -291,17 +298,17 @@ public abstract class ConnectionItem { ...@@ -291,17 +298,17 @@ public abstract class ConnectionItem {
private boolean onDisconnect(ConnectionThread connectionThread) { private boolean onDisconnect(ConnectionThread connectionThread) {
XMPPConnection xmppConnection = connectionThread.getXMPPConnection(); XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
boolean acceptable = isManaged(connectionThread); boolean acceptable = isManaged(connectionThread);
if (xmppConnection == null) if (xmppConnection == null) {
LogManager.i(this, "onClose " + acceptable); LogManager.i(this, "onClose " + acceptable);
else } else {
LogManager LogManager.i(this, "onClose " + xmppConnection.hashCode() + " - "
.i(this, "onClose " + xmppConnection.hashCode() + " - " + xmppConnection.getConnectionCounter() + ", " + acceptable);
+ xmppConnection.getConnectionCounter() + ", " }
+ acceptable);
ConnectionManager.getInstance().onDisconnect(connectionThread); ConnectionManager.getInstance().onDisconnect(connectionThread);
if (acceptable) if (acceptable) {
connectionThread.shutdown(); connectionThread.shutdown();
}
return acceptable; return acceptable;
} }
...@@ -312,8 +319,9 @@ public abstract class ConnectionItem { ...@@ -312,8 +319,9 @@ public abstract class ConnectionItem {
if (onDisconnect(connectionThread)) { if (onDisconnect(connectionThread)) {
state = ConnectionState.waiting; state = ConnectionState.waiting;
this.connectionThread = null; this.connectionThread = null;
if (connectionRequest) if (connectionRequest) {
Application.getInstance().onError(R.string.CONNECTION_FAILED); Application.getInstance().onError(R.string.CONNECTION_FAILED);
}
connectionRequest = false; connectionRequest = false;
} }
} }
......
...@@ -48,8 +48,7 @@ import java.util.Map.Entry; ...@@ -48,8 +48,7 @@ import java.util.Map.Entry;
* *
* @author alexander.ivanov * @author alexander.ivanov
*/ */
public class ConnectionManager implements OnInitializedListener, public class ConnectionManager implements OnInitializedListener, OnCloseListener, OnTimerListener {
OnCloseListener, OnTimerListener {
/** /**
* Timeout for receiving reply from server. * Timeout for receiving reply from server.
......
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