use java 8 in push management service

This commit is contained in:
Daniel Gultsch 2018-02-25 14:39:55 +01:00
parent d4ac8b3122
commit 7557de5479

View file

@ -13,7 +13,6 @@ import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.forms.Data; import eu.siacs.conversations.xmpp.forms.Data;
import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.InvalidJidException;
@ -32,68 +31,56 @@ public class PushManagementService {
public void registerPushTokenOnServer(final Account account) { public void registerPushTokenOnServer(final Account account) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": has push support"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": has push support");
retrieveGcmInstanceToken(new OnGcmInstanceTokenRetrieved() { retrieveGcmInstanceToken(token -> {
@Override try {
public void onGcmInstanceTokenRetrieved(String token) { final String deviceId = Settings.Secure.getString(mXmppConnectionService.getContentResolver(), Settings.Secure.ANDROID_ID);
try { IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(Jid.fromString(APP_SERVER), token, deviceId);
final String deviceId = Settings.Secure.getString(mXmppConnectionService.getContentResolver(), Settings.Secure.ANDROID_ID); mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> {
IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(Jid.fromString(APP_SERVER), token, deviceId); Element command = p.findChild("command","http://jabber.org/protocol/commands");
mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() { if (p.getType() == IqPacket.TYPE.RESULT && command != null) {
@Override Element x = command.findChild("x", Namespace.DATA);
public void onIqPacketReceived(Account account, IqPacket packet) { if (x != null) {
Element command = packet.findChild("command","http://jabber.org/protocol/commands"); Data data = Data.parse(x);
if (packet.getType() == IqPacket.TYPE.RESULT && command != null) { try {
Element x = command.findChild("x", Namespace.DATA); String node = data.getValue("node");
if (x != null) { String secret = data.getValue("secret");
Data data = Data.parse(x); Jid jid = Jid.fromString(data.getValue("jid"));
try { if (node != null && secret != null) {
String node = data.getValue("node"); enablePushOnServer(a, jid, node, secret);
String secret = data.getValue("secret");
Jid jid = Jid.fromString(data.getValue("jid"));
if (node != null && secret != null) {
enablePushOnServer(account, jid, node, secret);
}
} catch (InvalidJidException e) {
e.printStackTrace();
}
} }
} else { } catch (InvalidJidException e) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": invalid response from app server"); e.printStackTrace();
} }
} }
}); } else {
} catch (InvalidJidException ignored) { Log.d(Config.LOGTAG, a.getJid().toBareJid()+": invalid response from app server");
}
});
} catch (InvalidJidException ignored) {
}
} }
}); });
} }
private void enablePushOnServer(final Account account, final Jid jid, final String node, final String secret) { private void enablePushOnServer(final Account account, final Jid jid, final String node, final String secret) {
IqPacket enable = mXmppConnectionService.getIqGenerator().enablePush(jid, node, secret); IqPacket enable = mXmppConnectionService.getIqGenerator().enablePush(jid, node, secret);
mXmppConnectionService.sendIqPacket(account, enable, new OnIqPacketReceived() { mXmppConnectionService.sendIqPacket(account, enable, (a, p) -> {
@Override if (p.getType() == IqPacket.TYPE.RESULT) {
public void onIqPacketReceived(Account account, IqPacket packet) { Log.d(Config.LOGTAG, a.getJid().toBareJid() + ": successfully enabled push on server");
if (packet.getType() == IqPacket.TYPE.RESULT) { } else if (p.getType() == IqPacket.TYPE.ERROR) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": successfully enabled push on server"); Log.d(Config.LOGTAG, a.getJid().toBareJid() + ": enabling push on server failed");
} else if (packet.getType() == IqPacket.TYPE.ERROR) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": enabling push on server failed");
}
} }
}); });
} }
private void retrieveGcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) { private void retrieveGcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) {
new Thread(new Runnable() { new Thread(() -> {
@Override InstanceID instanceID = InstanceID.getInstance(mXmppConnectionService);
public void run() { try {
InstanceID instanceID = InstanceID.getInstance(mXmppConnectionService); String token = instanceID.getToken(mXmppConnectionService.getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
try { instanceTokenRetrieved.onGcmInstanceTokenRetrieved(token);
String token = instanceID.getToken(mXmppConnectionService.getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); } catch (Exception e) {
instanceTokenRetrieved.onGcmInstanceTokenRetrieved(token); Log.d(Config.LOGTAG,"unable to get push token");
} catch (Exception e) {
Log.d(Config.LOGTAG,"unable to get push token");
}
} }
}).start(); }).start();