2016-02-12 10:39:27 +00:00
|
|
|
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 eu.siacs.conversations.Config;
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
2016-02-13 13:20:07 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2018-02-25 13:31:31 +00:00
|
|
|
import eu.siacs.conversations.xml.Namespace;
|
2016-02-14 14:36:37 +00:00
|
|
|
import eu.siacs.conversations.xmpp.XmppConnection;
|
2016-02-13 13:20:07 +00:00
|
|
|
import eu.siacs.conversations.xmpp.forms.Data;
|
2016-02-12 10:39:27 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
2018-03-08 15:27:33 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2016-02-12 10:39:27 +00:00
|
|
|
|
|
|
|
public class PushManagementService {
|
|
|
|
|
2018-03-08 15:27:33 +00:00
|
|
|
private static final Jid APP_SERVER = Jid.of("push.siacs.eu");
|
2016-02-12 10:39:27 +00:00
|
|
|
|
|
|
|
protected final XmppConnectionService mXmppConnectionService;
|
|
|
|
|
2018-03-08 15:27:33 +00:00
|
|
|
PushManagementService(XmppConnectionService service) {
|
2016-02-12 10:39:27 +00:00
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
2018-03-08 15:27:33 +00:00
|
|
|
void registerPushTokenOnServer(final Account account) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has push support");
|
2018-02-25 13:39:55 +00:00
|
|
|
retrieveGcmInstanceToken(token -> {
|
2018-03-08 15:27:33 +00:00
|
|
|
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);
|
2016-02-13 13:20:07 +00:00
|
|
|
}
|
2018-03-08 15:27:33 +00:00
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
e.printStackTrace();
|
2016-02-12 10:39:27 +00:00
|
|
|
}
|
2018-02-25 13:39:55 +00:00
|
|
|
}
|
2018-03-08 15:27:33 +00:00
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": invalid response from app server");
|
|
|
|
}
|
|
|
|
});
|
2016-02-12 10:39:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-13 13:20:07 +00:00
|
|
|
private void enablePushOnServer(final Account account, final Jid jid, final String node, final String secret) {
|
|
|
|
IqPacket enable = mXmppConnectionService.getIqGenerator().enablePush(jid, node, secret);
|
2018-02-25 13:39:55 +00:00
|
|
|
mXmppConnectionService.sendIqPacket(account, enable, (a, p) -> {
|
|
|
|
if (p.getType() == IqPacket.TYPE.RESULT) {
|
2018-03-08 15:27:33 +00:00
|
|
|
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": successfully enabled push on server");
|
2018-02-25 13:39:55 +00:00
|
|
|
} else if (p.getType() == IqPacket.TYPE.ERROR) {
|
2018-03-08 15:27:33 +00:00
|
|
|
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": enabling push on server failed");
|
2016-02-13 13:20:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-12 10:39:27 +00:00
|
|
|
private void retrieveGcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) {
|
2018-02-25 13:39:55 +00:00
|
|
|
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) {
|
2018-03-08 15:27:33 +00:00
|
|
|
Log.d(Config.LOGTAG, "unable to get push token");
|
2016-02-12 10:39:27 +00:00
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-12 23:03:57 +00:00
|
|
|
|
|
|
|
public boolean available(Account account) {
|
2016-02-14 14:36:37 +00:00
|
|
|
final XmppConnection connection = account.getXmppConnection();
|
2017-02-08 15:52:35 +00:00
|
|
|
return connection != null
|
|
|
|
&& connection.getFeatures().sm()
|
|
|
|
&& connection.getFeatures().push()
|
|
|
|
&& playServicesAvailable();
|
2016-02-12 10:39:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 23:03:57 +00:00
|
|
|
private boolean playServicesAvailable() {
|
|
|
|
return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mXmppConnectionService) == ConnectionResult.SUCCESS;
|
2016-02-12 10:39:27 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 17:19:11 +00:00
|
|
|
public boolean isStub() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-12 10:39:27 +00:00
|
|
|
interface OnGcmInstanceTokenRetrieved {
|
|
|
|
void onGcmInstanceTokenRetrieved(String token);
|
|
|
|
}
|
|
|
|
}
|