diff options
-rw-r--r-- | src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java | 22 | ||||
-rw-r--r-- | src/standardPush/java/de/pixart/messenger/services/PushManagementService.java | 97 |
2 files changed, 51 insertions, 68 deletions
diff --git a/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java b/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java index 6625a0f99..e66dc60e5 100644 --- a/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java +++ b/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java @@ -22,20 +22,16 @@ public class MaintenanceReceiver extends BroadcastReceiver { } private void renewInstanceToken(final Context context) { - new Thread(new Runnable() { - @Override - public void run() { - InstanceID instanceID = InstanceID.getInstance(context); - try { - instanceID.deleteInstanceID(); - Intent intent = new Intent(context, XmppConnectionService.class); - intent.setAction(XmppConnectionService.ACTION_GCM_TOKEN_REFRESH); - context.startService(intent); - } catch (IOException e) { - Log.d(Config.LOGTAG, "unable to renew instance token", e); - } + new Thread(() -> { + InstanceID instanceID = InstanceID.getInstance(context); + try { + instanceID.deleteInstanceID(); + Intent intent = new Intent(context, XmppConnectionService.class); + intent.setAction(XmppConnectionService.ACTION_GCM_TOKEN_REFRESH); + context.startService(intent); + } catch (IOException e) { + Log.d(Config.LOGTAG, "unable to renew instance token", e); } }).start(); - } }
\ No newline at end of file diff --git a/src/standardPush/java/de/pixart/messenger/services/PushManagementService.java b/src/standardPush/java/de/pixart/messenger/services/PushManagementService.java index f18b981f8..2d3ec0ee2 100644 --- a/src/standardPush/java/de/pixart/messenger/services/PushManagementService.java +++ b/src/standardPush/java/de/pixart/messenger/services/PushManagementService.java @@ -11,8 +11,8 @@ import com.google.android.gms.iid.InstanceID; import de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.entities.Account; +import de.pixart.messenger.utils.Namespace; import de.pixart.messenger.xml.Element; -import de.pixart.messenger.xmpp.OnIqPacketReceived; import de.pixart.messenger.xmpp.XmppConnection; import de.pixart.messenger.xmpp.forms.Data; import de.pixart.messenger.xmpp.stanzas.IqPacket; @@ -20,78 +20,62 @@ import rocks.xmpp.addr.Jid; public class PushManagementService { - private static final String APP_SERVER = "push.siacs.eu"; + private static final Jid APP_SERVER = Jid.of("push.siacs.eu"); protected final XmppConnectionService mXmppConnectionService; - public PushManagementService(XmppConnectionService service) { + PushManagementService(XmppConnectionService service) { this.mXmppConnectionService = service; } - public void registerPushTokenOnServer(final Account account) { + void registerPushTokenOnServer(final Account account) { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has push support"); - retrieveGcmInstanceToken(new OnGcmInstanceTokenRetrieved() { - @Override - public void onGcmInstanceTokenRetrieved(String token) { - try { - final String deviceId = Settings.Secure.getString(mXmppConnectionService.getContentResolver(), Settings.Secure.ANDROID_ID); - IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(Jid.fromString(APP_SERVER), token, deviceId); - mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - Element command = packet.findChild("command", "http://jabber.org/protocol/commands"); - if (packet.getType() == IqPacket.TYPE.RESULT && command != null) { - Element x = command.findChild("x", Namespace.DATA); - if (x != null) { - Data data = Data.parse(x); - try { - String node = data.getValue("node"); - 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 { - Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": invalid response from app server"); + retrieveGcmInstanceToken(token -> { + final String deviceId = Settings.Secure.getString(mXmppConnectionService.getContentResolver(), Settings.Secure.ANDROID_ID); + IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(APP_SERVER, token, deviceId); + mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> { + Element command = p.findChild("command", "http://jabber.org/protocol/commands"); + if (p.getType() == IqPacket.TYPE.RESULT && command != null) { + Element x = command.findChild("x", Namespace.DATA); + if (x != null) { + Data data = Data.parse(x); + try { + String node = data.getValue("node"); + String secret = data.getValue("secret"); + Jid jid = Jid.of(data.getValue("jid")); + if (node != null && secret != null) { + enablePushOnServer(a, jid, node, secret); } + } catch (IllegalArgumentException e) { + e.printStackTrace(); } - }); - } catch (InvalidJidException ignored) { - + } + } else { + Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": invalid response from app server"); } - } + }); }); } private void enablePushOnServer(final Account account, final Jid jid, final String node, final String secret) { IqPacket enable = mXmppConnectionService.getIqGenerator().enablePush(jid, node, secret); - mXmppConnectionService.sendIqPacket(account, enable, new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - if (packet.getType() == IqPacket.TYPE.RESULT) { - Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": successfully enabled push on server"); - } else if (packet.getType() == IqPacket.TYPE.ERROR) { - Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": enabling push on server failed"); - } + mXmppConnectionService.sendIqPacket(account, enable, (a, p) -> { + if (p.getType() == IqPacket.TYPE.RESULT) { + Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": successfully enabled push on server"); + } else if (p.getType() == IqPacket.TYPE.ERROR) { + Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": enabling push on server failed"); } }); } private void retrieveGcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) { - new Thread(new Runnable() { - @Override - public void run() { - InstanceID instanceID = InstanceID.getInstance(mXmppConnectionService); - try { - String token = instanceID.getToken(mXmppConnectionService.getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); - instanceTokenRetrieved.onGcmInstanceTokenRetrieved(token); - } catch (Exception e) { - Log.d(Config.LOGTAG, "unable to get push token"); - } + new Thread(() -> { + InstanceID instanceID = InstanceID.getInstance(mXmppConnectionService); + try { + String token = instanceID.getToken(mXmppConnectionService.getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); + instanceTokenRetrieved.onGcmInstanceTokenRetrieved(token); + } catch (Exception e) { + Log.d(Config.LOGTAG, "unable to get push token"); } }).start(); @@ -100,7 +84,10 @@ public class PushManagementService { public boolean available(Account account) { final XmppConnection connection = account.getXmppConnection(); - return connection != null && connection.getFeatures().sm() && connection.getFeatures().push() && playServicesAvailable(); + return connection != null + && connection.getFeatures().sm() + && connection.getFeatures().push() + && playServicesAvailable(); } private boolean playServicesAvailable() { @@ -114,4 +101,4 @@ public class PushManagementService { interface OnGcmInstanceTokenRetrieved { void onGcmInstanceTokenRetrieved(String token); } -} +}
\ No newline at end of file |