diff options
author | lookshe <github@lookshe.org> | 2016-02-22 14:36:35 +0100 |
---|---|---|
committer | lookshe <github@lookshe.org> | 2016-02-22 14:36:35 +0100 |
commit | c5e60b43b8e107702445e834bce8a3c77f676eb8 (patch) | |
tree | 266138fdf20f29f7ed8a31d4682f02cd7012cf0e /src/playstore/java | |
parent | 1e40764b20a08170f8e4bfe1f1ae1559111f2961 (diff) | |
parent | c20d8ac69eeade4d5337d958ada7910db0c6d3a0 (diff) |
Merge tag '1.10.0' into trz/merge_1.10.0
Conflicts:
README.md
build.gradle
src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java
src/main/java/eu/siacs/conversations/parser/MessageParser.java
src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
src/main/java/eu/siacs/conversations/ui/ConversationFragment.java
src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java
src/main/res/layout/activity_edit_account.xml
src/main/res/values-cs/strings.xml
src/main/res/values-de/strings.xml
src/main/res/values-es/strings.xml
src/main/res/values-eu/strings.xml
src/main/res/values-gl/strings.xml
src/main/res/values-it/strings.xml
src/main/res/values-ja/strings.xml
src/main/res/values-nb-rNO/strings.xml
src/main/res/values-nl/strings.xml
src/main/res/values-pl/strings.xml
src/main/res/values-sr/strings.xml
src/main/res/values-zh-rCN/strings.xml
src/main/res/values-zh-rTW/strings.xml
src/main/res/values/strings.xml
Diffstat (limited to 'src/playstore/java')
3 files changed, 154 insertions, 0 deletions
diff --git a/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java new file mode 100644 index 00000000..dc080430 --- /dev/null +++ b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java @@ -0,0 +1,15 @@ +package eu.siacs.conversations.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/playstore/java/eu/siacs/conversations/services/PushManagementService.java b/src/playstore/java/eu/siacs/conversations/services/PushManagementService.java new file mode 100644 index 00000000..3fdaf832 --- /dev/null +++ b/src/playstore/java/eu/siacs/conversations/services/PushManagementService.java @@ -0,0 +1,119 @@ +package eu.siacs.conversations.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 java.io.IOException; + +import eu.siacs.conversations.Config; +import eu.siacs.conversations.R; +import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.xml.Element; +import eu.siacs.conversations.xmpp.OnIqPacketReceived; +import eu.siacs.conversations.xmpp.XmppConnection; +import eu.siacs.conversations.xmpp.forms.Data; +import eu.siacs.conversations.xmpp.jid.InvalidJidException; +import eu.siacs.conversations.xmpp.jid.Jid; +import eu.siacs.conversations.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 (IOException e) { + } + } + }).start(); + + } + + + public boolean available(Account account) { + final XmppConnection connection = account.getXmppConnection(); + return connection != null && connection.getFeatures().push() && playServicesAvailable(); + } + + 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/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java new file mode 100644 index 00000000..37c95e13 --- /dev/null +++ b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java @@ -0,0 +1,20 @@ +package eu.siacs.conversations.services; + +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; + +import com.google.android.gms.gcm.GcmListenerService; + +import eu.siacs.conversations.Config; + +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); + } +} |