aboutsummaryrefslogtreecommitdiffstats
path: root/src/standardPush/java
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2017-03-17 23:42:42 +0100
committerChristian Schneppe <christian@pix-art.de>2017-03-17 23:42:42 +0100
commitb9f1a63fce7161c09f1d438768dfc0abe56dfd75 (patch)
tree65c882d28c946a7fa784e320e73a549347a41c02 /src/standardPush/java
parent5397701f53c9f31ba64a7a9c3fdad4f02a67e5c6 (diff)
rename playstore to standardpush
Diffstat (limited to 'src/standardPush/java')
-rw-r--r--src/standardPush/java/de/pixart/messenger/services/InstanceIdService.java15
-rw-r--r--src/standardPush/java/de/pixart/messenger/services/PushManagementService.java123
-rw-r--r--src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java17
3 files changed, 155 insertions, 0 deletions
diff --git a/src/standardPush/java/de/pixart/messenger/services/InstanceIdService.java b/src/standardPush/java/de/pixart/messenger/services/InstanceIdService.java
new file mode 100644
index 000000000..e97d7181a
--- /dev/null
+++ b/src/standardPush/java/de/pixart/messenger/services/InstanceIdService.java
@@ -0,0 +1,15 @@
+package de.pixart.messenger.services;
+
+import android.content.Intent;
+
+import com.google.android.gms.iid.InstanceIDListenerService;
+
+public class InstanceIdService extends InstanceIDListenerService {
+
+ @Override
+ public void onTokenRefresh() {
+ Intent intent = new Intent(this, XmppConnectionService.class);
+ intent.setAction(XmppConnectionService.ACTION_GCM_TOKEN_REFRESH);
+ startService(intent);
+ }
+}
diff --git a/src/standardPush/java/de/pixart/messenger/services/PushManagementService.java b/src/standardPush/java/de/pixart/messenger/services/PushManagementService.java
new file mode 100644
index 000000000..f30bfae46
--- /dev/null
+++ b/src/standardPush/java/de/pixart/messenger/services/PushManagementService.java
@@ -0,0 +1,123 @@
+package de.pixart.messenger.services;
+
+import android.provider.Settings;
+import android.util.Log;
+
+import com.google.android.gms.common.ConnectionResult;
+import com.google.android.gms.common.GoogleApiAvailability;
+import com.google.android.gms.gcm.GoogleCloudMessaging;
+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.xml.Element;
+import de.pixart.messenger.xmpp.OnIqPacketReceived;
+import de.pixart.messenger.xmpp.Patches;
+import de.pixart.messenger.xmpp.XmppConnection;
+import de.pixart.messenger.xmpp.forms.Data;
+import de.pixart.messenger.xmpp.jid.InvalidJidException;
+import de.pixart.messenger.xmpp.jid.Jid;
+import de.pixart.messenger.xmpp.stanzas.IqPacket;
+
+public class PushManagementService {
+
+ private static final String APP_SERVER = "push.siacs.eu";
+
+ protected final XmppConnectionService mXmppConnectionService;
+
+ public PushManagementService(XmppConnectionService service) {
+ this.mXmppConnectionService = service;
+ }
+
+ public void registerPushTokenOnServer(final Account account) {
+ Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": 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", "jabber:x: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().toBareJid() + ": invalid response from app server");
+ }
+ }
+ });
+ } catch (InvalidJidException ignored) {
+
+ }
+ }
+ });
+ }
+
+ 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().toBareJid() + ": successfully enabled push on server");
+ } 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) {
+ 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");
+ }
+ }
+ }).start();
+
+ }
+
+
+ public boolean available(Account account) {
+ final XmppConnection connection = account.getXmppConnection();
+ return connection != null && connection.getFeatures().sm() && connection.getFeatures().push() && playServicesAvailable();
+ }
+
+ public boolean availableAndUseful(Account account) {
+ return Patches.SUFFICIENT_PUSH.contains(account.getServerIdentity()) && available(account);
+ }
+
+ private boolean playServicesAvailable() {
+ return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mXmppConnectionService) == ConnectionResult.SUCCESS;
+ }
+
+ public boolean isStub() {
+ return false;
+ }
+
+ interface OnGcmInstanceTokenRetrieved {
+ void onGcmInstanceTokenRetrieved(String token);
+ }
+}
diff --git a/src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java b/src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java
new file mode 100644
index 000000000..33c5119e0
--- /dev/null
+++ b/src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java
@@ -0,0 +1,17 @@
+package de.pixart.messenger.services;
+
+import android.content.Intent;
+import android.os.Bundle;
+
+import com.google.android.gms.gcm.GcmListenerService;
+
+public class PushMessageReceiver extends GcmListenerService {
+
+ @Override
+ public void onMessageReceived(String from, Bundle data) {
+ Intent intent = new Intent(this, XmppConnectionService.class);
+ intent.setAction(XmppConnectionService.ACTION_GCM_MESSAGE_RECEIVED);
+ intent.replaceExtras(data);
+ startService(intent);
+ }
+}