2016-02-12 10:39:27 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.google.android.gms.common.ConnectionResult;
|
2021-05-04 07:47:09 +00:00
|
|
|
import com.google.android.gms.common.GoogleApiAvailabilityLight;
|
2021-01-18 20:49:31 +00:00
|
|
|
import com.google.firebase.messaging.FirebaseMessaging;
|
2016-02-12 10:39:27 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
2018-05-20 14:43:10 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2016-02-12 10:39:27 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2018-05-12 15:23:37 +00:00
|
|
|
import eu.siacs.conversations.utils.PhoneHelper;
|
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;
|
2020-05-15 16:44:55 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
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;
|
|
|
|
|
|
|
|
public class PushManagementService {
|
|
|
|
|
2019-06-25 16:15:51 +00:00
|
|
|
protected final XmppConnectionService mXmppConnectionService;
|
|
|
|
|
|
|
|
PushManagementService(XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Data findResponseData(IqPacket response) {
|
|
|
|
final Element command = response.findChild("command", Namespace.COMMANDS);
|
|
|
|
final Element x = command == null ? null : command.findChild("x", Namespace.DATA);
|
|
|
|
return x == null ? null : Data.parse(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Jid getAppServer() {
|
|
|
|
return Jid.of(mXmppConnectionService.getString(R.string.app_server));
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerPushTokenOnServer(final Account account) {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has push support");
|
|
|
|
retrieveFcmInstanceToken(token -> {
|
|
|
|
final String androidId = PhoneHelper.getAndroidId(mXmppConnectionService);
|
|
|
|
final IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(getAppServer(), token, androidId);
|
|
|
|
mXmppConnectionService.sendIqPacket(account, packet, (a, response) -> {
|
|
|
|
final Data data = findResponseData(response);
|
|
|
|
if (response.getType() == IqPacket.TYPE.RESULT && data != null) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-18 20:49:31 +00:00
|
|
|
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": failed to enable push. invalid response from app server " + response);
|
2019-06-25 16:15:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enablePushOnServer(final Account account, final Jid appServer, final String node, final String secret) {
|
|
|
|
final IqPacket enable = mXmppConnectionService.getIqGenerator().enablePush(appServer, node, secret);
|
|
|
|
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 retrieveFcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) {
|
2021-01-18 20:49:31 +00:00
|
|
|
final FirebaseMessaging firebaseMessaging;
|
2019-08-06 13:04:59 +00:00
|
|
|
try {
|
2021-01-18 20:49:31 +00:00
|
|
|
firebaseMessaging = FirebaseMessaging.getInstance();
|
2019-08-06 13:04:59 +00:00
|
|
|
} catch (IllegalStateException e) {
|
2021-01-18 20:49:31 +00:00
|
|
|
Log.d(Config.LOGTAG, "unable to get firebase instance token ", e);
|
2019-08-06 13:04:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-18 20:49:31 +00:00
|
|
|
firebaseMessaging.getToken().addOnCompleteListener(task -> {
|
2019-06-25 16:15:51 +00:00
|
|
|
if (!task.isSuccessful()) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to get Firebase instance token", task.getException());
|
|
|
|
}
|
2021-01-18 20:49:31 +00:00
|
|
|
final String result;
|
2019-07-17 19:03:56 +00:00
|
|
|
try {
|
|
|
|
result = task.getResult();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to get Firebase instance token due to bug in library ", e);
|
|
|
|
return;
|
|
|
|
}
|
2019-06-25 16:15:51 +00:00
|
|
|
if (result != null) {
|
2021-01-18 20:49:31 +00:00
|
|
|
instanceTokenRetrieved.onGcmInstanceTokenRetrieved(result);
|
2019-06-25 16:15:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean available(Account account) {
|
|
|
|
final XmppConnection connection = account.getXmppConnection();
|
|
|
|
return connection != null
|
|
|
|
&& connection.getFeatures().sm()
|
|
|
|
&& connection.getFeatures().push()
|
|
|
|
&& playServicesAvailable();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean playServicesAvailable() {
|
2021-05-04 07:47:09 +00:00
|
|
|
return GoogleApiAvailabilityLight.getInstance().isGooglePlayServicesAvailable(mXmppConnectionService) == ConnectionResult.SUCCESS;
|
2019-06-25 16:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isStub() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface OnGcmInstanceTokenRetrieved {
|
|
|
|
void onGcmInstanceTokenRetrieved(String token);
|
|
|
|
}
|
2016-02-12 10:39:27 +00:00
|
|
|
}
|