refactor captcha response handling to avoid network on main thread exception

This commit is contained in:
Daniel Gultsch 2016-05-05 09:58:35 +02:00
parent 8a81f85734
commit 12704fa640
3 changed files with 25 additions and 35 deletions

View file

@ -303,8 +303,10 @@ public class IqGenerator extends AbstractGenerator {
register.setTo(account.getServer()); register.setTo(account.getServer());
register.setId(id); register.setId(id);
register.query("jabber:iq:register").addChild(data); Element query = register.query("jabber:iq:register");
if (data != null) {
query.addChild(data);
}
return register; return register;
} }

View file

@ -3002,9 +3002,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
} }
public void sendCreateAccountWithCaptchaPacket(Account account, String id, Data data) { public void sendCreateAccountWithCaptchaPacket(Account account, String id, Data data) {
XmppConnection connection = account.getXmppConnection(); final XmppConnection connection = account.getXmppConnection();
if (connection != null) { if (connection != null) {
connection.sendCaptchaRegistryRequest(id, data); IqPacket request = mIqGenerator.generateCreateAccountWithCaptcha(account, id, data);
sendIqPacket(account, request, connection.registrationResponseListener);
} }
} }

View file

@ -176,18 +176,15 @@ public class XmppConnection implements Runnable {
}; };
private Identity mServerIdentity = Identity.UNKNOWN; private Identity mServerIdentity = Identity.UNKNOWN;
private OnIqPacketReceived createPacketReceiveHandler() { public final OnIqPacketReceived registrationResponseListener = new OnIqPacketReceived() {
return new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE.RESULT) { if (packet.getType() == IqPacket.TYPE.RESULT) {
account.setOption(Account.OPTION_REGISTER, account.setOption(Account.OPTION_REGISTER, false);
false);
forceCloseSocket(); forceCloseSocket();
changeStatus(Account.State.REGISTRATION_SUCCESSFUL); changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
} else if (packet.hasChild("error") } else if (packet.hasChild("error")
&& (packet.findChild("error") && (packet.findChild("error").hasChild("conflict"))) {
.hasChild("conflict"))) {
forceCloseSocket(); forceCloseSocket();
changeStatus(Account.State.REGISTRATION_CONFLICT); changeStatus(Account.State.REGISTRATION_CONFLICT);
} else { } else {
@ -197,7 +194,6 @@ public class XmppConnection implements Runnable {
} }
} }
}; };
}
public XmppConnection(final Account account, final XmppConnectionService service) { public XmppConnection(final Account account, final XmppConnectionService service) {
this.account = account; this.account = account;
@ -809,15 +805,6 @@ public class XmppConnection implements Runnable {
return mechanisms; return mechanisms;
} }
public void sendCaptchaRegistryRequest(String id, Data data) {
if (data == null) {
setAccountCreationFailed("");
} else {
IqPacket request = getIqGenerator().generateCreateAccountWithCaptcha(account, id, data);
sendIqPacket(request, createPacketReceiveHandler());
}
}
private void sendRegistryRequest() { private void sendRegistryRequest() {
final IqPacket register = new IqPacket(IqPacket.TYPE.GET); final IqPacket register = new IqPacket(IqPacket.TYPE.GET);
register.query("jabber:iq:register"); register.query("jabber:iq:register");
@ -835,7 +822,7 @@ public class XmppConnection implements Runnable {
final Element password = new Element("password").setContent(account.getPassword()); final Element password = new Element("password").setContent(account.getPassword());
register.query("jabber:iq:register").addChild(username); register.query("jabber:iq:register").addChild(username);
register.query().addChild(password); register.query().addChild(password);
sendIqPacket(register, createPacketReceiveHandler()); sendIqPacket(register, registrationResponseListener);
} else if (packet.getType() == IqPacket.TYPE.RESULT } else if (packet.getType() == IqPacket.TYPE.RESULT
&& (packet.query().hasChild("x", "jabber:x:data"))) { && (packet.query().hasChild("x", "jabber:x:data"))) {
final Data data = Data.parse(packet.query().findChild("x", "jabber:x:data")); final Data data = Data.parse(packet.query().findChild("x", "jabber:x:data"));