minor code clean up in XmppConnection class

This commit is contained in:
Daniel Gultsch 2020-12-31 10:27:06 +01:00
parent 0e54d8a2cf
commit 0569febf67

View file

@ -107,9 +107,7 @@ public class XmppConnection implements Runnable {
private static final int PACKET_IQ = 0; private static final int PACKET_IQ = 0;
private static final int PACKET_MESSAGE = 1; private static final int PACKET_MESSAGE = 1;
private static final int PACKET_PRESENCE = 2; private static final int PACKET_PRESENCE = 2;
public final OnIqPacketReceived registrationResponseListener = new OnIqPacketReceived() { public final OnIqPacketReceived registrationResponseListener = (account, packet) -> {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE.RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.setOption(Account.OPTION_REGISTER, false); account.setOption(Account.OPTION_REGISTER, false);
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": successfully registered new account on server"); Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": successfully registered new account on server");
@ -133,7 +131,6 @@ public class XmppConnection implements Runnable {
} }
throw new StateChangingError(state); throw new StateChangingError(state);
} }
}
}; };
protected final Account account; protected final Account account;
private final Features features = new Features(this); private final Features features = new Features(this);
@ -160,10 +157,10 @@ public class XmppConnection implements Runnable {
private long lastSessionStarted = 0; private long lastSessionStarted = 0;
private long lastDiscoStarted = 0; private long lastDiscoStarted = 0;
private boolean isMamPreferenceAlways = false; private boolean isMamPreferenceAlways = false;
private AtomicInteger mPendingServiceDiscoveries = new AtomicInteger(0); private final AtomicInteger mPendingServiceDiscoveries = new AtomicInteger(0);
private AtomicBoolean mWaitForDisco = new AtomicBoolean(true); private final AtomicBoolean mWaitForDisco = new AtomicBoolean(true);
private AtomicBoolean mWaitingForSmCatchup = new AtomicBoolean(false); private final AtomicBoolean mWaitingForSmCatchup = new AtomicBoolean(false);
private AtomicInteger mSmCatchupMessageCounter = new AtomicInteger(0); private final AtomicInteger mSmCatchupMessageCounter = new AtomicInteger(0);
private boolean mInteractive = false; private boolean mInteractive = false;
private int attempt = 0; private int attempt = 0;
private OnPresencePacketReceived presenceListener = null; private OnPresencePacketReceived presenceListener = null;
@ -772,7 +769,7 @@ public class XmppConnection implements Runnable {
} }
} }
private void processMessage(final Tag currentTag) throws XmlPullParserException, IOException { private void processMessage(final Tag currentTag) throws IOException {
final MessagePacket packet = (MessagePacket) processPacket(currentTag, PACKET_MESSAGE); final MessagePacket packet = (MessagePacket) processPacket(currentTag, PACKET_MESSAGE);
if (!packet.valid()) { if (!packet.valid()) {
Log.e(Config.LOGTAG, "encountered invalid message from='" + packet.getFrom() + "' to='" + packet.getTo() + "'"); Log.e(Config.LOGTAG, "encountered invalid message from='" + packet.getFrom() + "' to='" + packet.getTo() + "'");
@ -781,7 +778,7 @@ public class XmppConnection implements Runnable {
this.messageListener.onMessagePacketReceived(account, packet); this.messageListener.onMessagePacketReceived(account, packet);
} }
private void processPresence(final Tag currentTag) throws XmlPullParserException, IOException { private void processPresence(final Tag currentTag) throws IOException {
PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE); PresencePacket packet = (PresencePacket) processPacket(currentTag, PACKET_PRESENCE);
if (!packet.valid()) { if (!packet.valid()) {
Log.e(Config.LOGTAG, "encountered invalid presence from='" + packet.getFrom() + "' to='" + packet.getTo() + "'"); Log.e(Config.LOGTAG, "encountered invalid presence from='" + packet.getFrom() + "' to='" + packet.getTo() + "'");
@ -835,7 +832,7 @@ public class XmppConnection implements Runnable {
return sslSocket; return sslSocket;
} }
private void processStreamFeatures(final Tag currentTag) throws XmlPullParserException, IOException { private void processStreamFeatures(final Tag currentTag) throws IOException {
this.streamFeatures = tagReader.readElement(currentTag); this.streamFeatures = tagReader.readElement(currentTag);
final boolean isSecure = features.encryptionEnabled || Config.ALLOW_NON_TLS_CONNECTIONS || account.isOnion(); final boolean isSecure = features.encryptionEnabled || Config.ALLOW_NON_TLS_CONNECTIONS || account.isOnion();
final boolean needsBinding = !isBound && !account.isOptionSet(Account.OPTION_REGISTER); final boolean needsBinding = !isBound && !account.isOptionSet(Account.OPTION_REGISTER);
@ -1326,7 +1323,7 @@ public class XmppConnection implements Runnable {
iq.query("http://jabber.org/protocol/disco#items"); iq.query("http://jabber.org/protocol/disco#items");
this.sendIqPacket(iq, (account, packet) -> { this.sendIqPacket(iq, (account, packet) -> {
if (packet.getType() == IqPacket.TYPE.RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
HashSet<Jid> items = new HashSet<Jid>(); final HashSet<Jid> items = new HashSet<>();
final List<Element> elements = packet.query().getChildren(); final List<Element> elements = packet.query().getChildren();
for (final Element element : elements) { for (final Element element : elements) {
if (element.getName().equals("item")) { if (element.getName().equals("item")) {
@ -1354,10 +1351,7 @@ public class XmppConnection implements Runnable {
private void sendEnableCarbons() { private void sendEnableCarbons() {
final IqPacket iq = new IqPacket(IqPacket.TYPE.SET); final IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
iq.addChild("enable", "urn:xmpp:carbons:2"); iq.addChild("enable", "urn:xmpp:carbons:2");
this.sendIqPacket(iq, new OnIqPacketReceived() { this.sendIqPacket(iq, (account, packet) -> {
@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
if (!packet.hasChild("error")) { if (!packet.hasChild("error")) {
Log.d(Config.LOGTAG, account.getJid().asBareJid() Log.d(Config.LOGTAG, account.getJid().asBareJid()
+ ": successfully enabled carbons"); + ": successfully enabled carbons");
@ -1366,11 +1360,10 @@ public class XmppConnection implements Runnable {
Log.d(Config.LOGTAG, account.getJid().asBareJid() Log.d(Config.LOGTAG, account.getJid().asBareJid()
+ ": error enableing carbons " + packet.toString()); + ": error enableing carbons " + packet.toString());
} }
}
}); });
} }
private void processStreamError(final Tag currentTag) throws XmlPullParserException, IOException { private void processStreamError(final Tag currentTag) throws IOException {
final Element streamError = tagReader.readElement(currentTag); final Element streamError = tagReader.readElement(currentTag);
if (streamError == null) { if (streamError == null) {
return; return;
@ -1623,8 +1616,8 @@ public class XmppConnection implements Runnable {
} }
public List<String> getMucServersWithholdAccount() { public List<String> getMucServersWithholdAccount() {
List<String> servers = getMucServers(); final List<String> servers = getMucServers();
servers.remove(account.getDomain()); servers.remove(account.getDomain().toEscapedString());
return servers; return servers;
} }
@ -1795,7 +1788,7 @@ public class XmppConnection implements Runnable {
} }
} }
private class StateChangingError extends Error { private static class StateChangingError extends Error {
private final Account.State state; private final Account.State state;
public StateChangingError(Account.State state) { public StateChangingError(Account.State state) {
@ -1803,7 +1796,7 @@ public class XmppConnection implements Runnable {
} }
} }
private class StateChangingException extends IOException { private static class StateChangingException extends IOException {
private final Account.State state; private final Account.State state;
public StateChangingException(Account.State state) { public StateChangingException(Account.State state) {