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,28 +176,24 @@ 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, false);
account.setOption(Account.OPTION_REGISTER, forceCloseSocket();
false); changeStatus(Account.State.REGISTRATION_SUCCESSFUL);
forceCloseSocket(); } else if (packet.hasChild("error")
changeStatus(Account.State.REGISTRATION_SUCCESSFUL); && (packet.findChild("error").hasChild("conflict"))) {
} else if (packet.hasChild("error") forceCloseSocket();
&& (packet.findChild("error") changeStatus(Account.State.REGISTRATION_CONFLICT);
.hasChild("conflict"))) { } else {
forceCloseSocket(); forceCloseSocket();
changeStatus(Account.State.REGISTRATION_CONFLICT); changeStatus(Account.State.REGISTRATION_FAILED);
} else { Log.d(Config.LOGTAG, packet.toString());
forceCloseSocket();
changeStatus(Account.State.REGISTRATION_FAILED);
Log.d(Config.LOGTAG, packet.toString());
}
} }
}; }
} };
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"));