1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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);
}
}
|