/
SmackableImp.java
1190 lines (1039 loc) · 41.4 KB
/
SmackableImp.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.yaxim.androidclient.service;
import java.io.File;
import java.util.Collection;
import java.util.Date;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import de.duenndns.ssl.MemorizingTrustManager;
import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.DNSUtil;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.dns.DNSJavaResolver;
import org.jivesoftware.smackx.entitycaps.EntityCapsManager;
import org.jivesoftware.smackx.entitycaps.cache.SimpleDirectoryPersistentCache;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.carbons.Carbon;
import org.jivesoftware.smackx.carbons.CarbonManager;
import org.jivesoftware.smackx.entitycaps.provider.CapsExtensionProvider;
import org.jivesoftware.smackx.forward.Forwarded;
import org.jivesoftware.smackx.provider.DelayInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverInfoProvider;
import org.jivesoftware.smackx.provider.DiscoverItemsProvider;
import org.jivesoftware.smackx.packet.DelayInformation;
import org.jivesoftware.smackx.packet.DelayInfo;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.Version;
import org.jivesoftware.smackx.ping.PingManager;
import org.jivesoftware.smackx.ping.packet.*;
import org.jivesoftware.smackx.ping.provider.PingProvider;
import org.jivesoftware.smackx.receipts.DeliveryReceipt;
import org.jivesoftware.smackx.receipts.DeliveryReceiptManager;
import org.jivesoftware.smackx.receipts.DeliveryReceiptRequest;
import org.jivesoftware.smackx.receipts.ReceiptReceivedListener;
import org.yaxim.androidclient.YaximApplication;
import org.yaxim.androidclient.data.ChatProvider;
import org.yaxim.androidclient.data.RosterProvider;
import org.yaxim.androidclient.data.YaximConfiguration;
import org.yaxim.androidclient.data.ChatProvider.ChatConstants;
import org.yaxim.androidclient.data.RosterProvider.RosterConstants;
import org.yaxim.androidclient.exceptions.YaximXMPPException;
import org.yaxim.androidclient.util.ConnectionState;
import org.yaxim.androidclient.util.LogConstants;
import org.yaxim.androidclient.util.PreferenceConstants;
import org.yaxim.androidclient.util.StatusMode;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
public class SmackableImp implements Smackable {
final static private String TAG = "yaxim.SmackableImp";
final static private int PACKET_TIMEOUT = 30000;
final static private String[] SEND_OFFLINE_PROJECTION = new String[] {
ChatConstants._ID, ChatConstants.JID,
ChatConstants.MESSAGE, ChatConstants.DATE, ChatConstants.PACKET_ID };
final static private String SEND_OFFLINE_SELECTION =
ChatConstants.DIRECTION + " = " + ChatConstants.OUTGOING + " AND " +
ChatConstants.DELIVERY_STATUS + " = " + ChatConstants.DS_NEW;
static final DiscoverInfo.Identity YAXIM_IDENTITY = new DiscoverInfo.Identity("client",
YaximApplication.XMPP_IDENTITY_NAME,
YaximApplication.XMPP_IDENTITY_TYPE);
static File capsCacheDir = null; ///< this is used to cache if we already initialized EntityCapsCache
static {
registerSmackProviders();
DNSUtil.setDNSResolver(DNSJavaResolver.getInstance());
// initialize smack defaults before any connections are created
SmackConfiguration.setPacketReplyTimeout(PACKET_TIMEOUT);
SmackConfiguration.setDefaultPingInterval(0);
}
static void registerSmackProviders() {
ProviderManager pm = ProviderManager.getInstance();
// add IQ handling
pm.addIQProvider("query","http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());
pm.addIQProvider("query","http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());
// add delayed delivery notifications
pm.addExtensionProvider("delay","urn:xmpp:delay", new DelayInfoProvider());
pm.addExtensionProvider("x","jabber:x:delay", new DelayInfoProvider());
// add XEP-0092 Software Version
pm.addIQProvider("query", Version.NAMESPACE, new Version.Provider());
// add carbons and forwarding
pm.addExtensionProvider("forwarded", Forwarded.NAMESPACE, new Forwarded.Provider());
pm.addExtensionProvider("sent", Carbon.NAMESPACE, new Carbon.Provider());
pm.addExtensionProvider("received", Carbon.NAMESPACE, new Carbon.Provider());
// add delivery receipts
pm.addExtensionProvider(DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceipt.Provider());
pm.addExtensionProvider(DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE, new DeliveryReceiptRequest.Provider());
// add XMPP Ping (XEP-0199)
pm.addIQProvider("ping","urn:xmpp:ping", new PingProvider());
ServiceDiscoveryManager.setDefaultIdentity(YAXIM_IDENTITY);
// XEP-0115 Entity Capabilities
pm.addExtensionProvider("c", "http://jabber.org/protocol/caps", new CapsExtensionProvider());
XmppStreamHandler.addExtensionProviders();
}
private final YaximConfiguration mConfig;
private ConnectionConfiguration mXMPPConfig;
private XmppStreamHandler.ExtXMPPConnection mXMPPConnection;
private XmppStreamHandler mStreamHandler;
private Thread mConnectingThread;
private Object mConnectingThreadMutex = new Object();
private ConnectionState mRequestedState = ConnectionState.OFFLINE;
private ConnectionState mState = ConnectionState.OFFLINE;
private String mLastError;
private XMPPServiceCallback mServiceCallBack;
private Roster mRoster;
private RosterListener mRosterListener;
private PacketListener mPacketListener;
private PacketListener mPresenceListener;
private ConnectionListener mConnectionListener;
private final ContentResolver mContentResolver;
private AlarmManager mAlarmManager;
private PacketListener mPongListener;
private String mPingID;
private long mPingTimestamp;
private PendingIntent mPingAlarmPendIntent;
private PendingIntent mPongTimeoutAlarmPendIntent;
private static final String PING_ALARM = "org.yaxim.androidclient.PING_ALARM";
private static final String PONG_TIMEOUT_ALARM = "org.yaxim.androidclient.PONG_TIMEOUT_ALARM";
private Intent mPingAlarmIntent = new Intent(PING_ALARM);
private Intent mPongTimeoutAlarmIntent = new Intent(PONG_TIMEOUT_ALARM);
private Service mService;
private PongTimeoutAlarmReceiver mPongTimeoutAlarmReceiver = new PongTimeoutAlarmReceiver();
private BroadcastReceiver mPingAlarmReceiver = new PingAlarmReceiver();
public SmackableImp(YaximConfiguration config,
ContentResolver contentResolver,
Service service) {
this.mConfig = config;
this.mContentResolver = contentResolver;
this.mService = service;
this.mAlarmManager = (AlarmManager)mService.getSystemService(Context.ALARM_SERVICE);
}
// this code runs a DNS resolver, might be blocking
private synchronized void initXMPPConnection() {
// allow custom server / custom port to override SRV record
if (mConfig.customServer.length() > 0)
mXMPPConfig = new ConnectionConfiguration(mConfig.customServer,
mConfig.port, mConfig.server);
else
mXMPPConfig = new ConnectionConfiguration(mConfig.server); // use SRV
mXMPPConfig.setReconnectionAllowed(false);
mXMPPConfig.setSendPresence(false);
mXMPPConfig.setCompressionEnabled(false); // disable for now
mXMPPConfig.setDebuggerEnabled(mConfig.smackdebug);
if (mConfig.require_ssl)
this.mXMPPConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
// register MemorizingTrustManager for HTTPS
try {
SSLContext sc = SSLContext.getInstance("TLS");
MemorizingTrustManager mtm = YaximApplication.getApp(mService).mMTM;
sc.init(null, new X509TrustManager[] { mtm },
new java.security.SecureRandom());
this.mXMPPConfig.setCustomSSLContext(sc);
this.mXMPPConfig.setHostnameVerifier(mtm.wrapHostnameVerifier(
new org.apache.http.conn.ssl.StrictHostnameVerifier()));
} catch (java.security.GeneralSecurityException e) {
debugLog("initialize MemorizingTrustManager: " + e);
}
this.mXMPPConnection = new XmppStreamHandler.ExtXMPPConnection(mXMPPConfig);
this.mStreamHandler = new XmppStreamHandler(mXMPPConnection, mConfig.smackdebug);
mStreamHandler.addAckReceivedListener(new XmppStreamHandler.AckReceivedListener() {
public void ackReceived(long handled, long total) {
gotServerPong("" + handled);
}
});
mConfig.reconnect_required = false;
initServiceDiscovery();
}
// blocking, run from a thread!
public boolean doConnect(boolean create_account) throws YaximXMPPException {
mRequestedState = ConnectionState.ONLINE;
updateConnectionState(ConnectionState.CONNECTING);
if (mXMPPConnection == null || mConfig.reconnect_required)
initXMPPConnection();
tryToConnect(create_account);
// actually, authenticated must be true now, or an exception must have
// been thrown.
if (isAuthenticated()) {
registerMessageListener();
registerPresenceListener();
registerPongListener();
sendOfflineMessages();
// we need to "ping" the service to let it know we are actually
// connected, even when no roster entries will come in
updateConnectionState(ConnectionState.ONLINE);
} else throw new YaximXMPPException("SMACK connected, but authentication failed");
return true;
}
// BLOCKING, call on a new Thread!
private void updateConnectingThread(Thread new_thread) {
synchronized(mConnectingThreadMutex) {
if (mConnectingThread == null) {
mConnectingThread = new_thread;
} else try {
Log.d(TAG, "updateConnectingThread: old thread is still running, killing it.");
mConnectingThread.interrupt();
mConnectingThread.join(50);
} catch (InterruptedException e) {
Log.d(TAG, "updateConnectingThread: failed to join(): " + e);
} finally {
mConnectingThread = new_thread;
}
}
}
private void finishConnectingThread() {
synchronized(mConnectingThreadMutex) {
mConnectingThread = null;
}
}
/** Non-blocking, synchronized function to connect/disconnect XMPP.
* This code is called from outside and returns immediately. The actual work
* is done on a background thread, and notified via callback.
* @param new_state The state to transition into. Possible values:
* OFFLINE to properly close the connection
* ONLINE to connect
* DISCONNECTED when network goes down
* @param create_account When going online, try to register an account.
*/
@Override
public synchronized void requestConnectionState(ConnectionState new_state, final boolean create_account) {
Log.d(TAG, "requestConnState: " + mState + " -> " + new_state + (create_account ? " create_account!" : ""));
mRequestedState = new_state;
if (new_state == mState)
return;
switch (new_state) {
case ONLINE:
switch (mState) {
case RECONNECT_DELAYED:
// TODO: cancel timer
case RECONNECT_NETWORK:
case OFFLINE:
// update state before starting thread to prevent race conditions
updateConnectionState(ConnectionState.CONNECTING);
// register ping (connection) timeout handler: 2*PACKET_TIMEOUT(30s) + 3s
registerPongTimeout(2*PACKET_TIMEOUT + 3000, "connection");
new Thread() {
@Override
public void run() {
updateConnectingThread(this);
try {
doConnect(create_account);
} catch (IllegalArgumentException e) {
// this might happen when DNS resolution in ConnectionConfiguration fails
onDisconnected(e);
} catch (YaximXMPPException e) {
onDisconnected(e);
} finally {
mAlarmManager.cancel(mPongTimeoutAlarmPendIntent);
finishConnectingThread();
}
}
}.start();
break;
case CONNECTING:
case DISCONNECTING:
// ignore all other cases
break;
}
break;
case DISCONNECTED:
// spawn thread to do disconnect
if (mState == ConnectionState.ONLINE) {
// update state before starting thread to prevent race conditions
updateConnectionState(ConnectionState.DISCONNECTING);
// register ping (connection) timeout handler: PACKET_TIMEOUT(30s)
registerPongTimeout(PACKET_TIMEOUT, "forced disconnect");
new Thread() {
public void run() {
updateConnectingThread(this);
mStreamHandler.quickShutdown();
onDisconnected("forced disconnect completed");
finishConnectingThread();
//updateConnectionState(ConnectionState.OFFLINE);
}
}.start();
}
break;
case OFFLINE:
switch (mState) {
case CONNECTING:
case ONLINE:
// update state before starting thread to prevent race conditions
updateConnectionState(ConnectionState.DISCONNECTING);
// register ping (connection) timeout handler: PACKET_TIMEOUT(30s)
registerPongTimeout(PACKET_TIMEOUT, "manual disconnect");
// spawn thread to do disconnect
new Thread() {
public void run() {
updateConnectingThread(this);
mXMPPConnection.shutdown();
mStreamHandler.close();
mAlarmManager.cancel(mPongTimeoutAlarmPendIntent);
// we should reset XMPPConnection the next time
mConfig.reconnect_required = true;
finishConnectingThread();
// reconnect if it was requested in the meantime
if (mRequestedState == ConnectionState.ONLINE)
requestConnectionState(ConnectionState.ONLINE);
}
}.start();
break;
case DISCONNECTING:
break;
case RECONNECT_DELAYED:
// TODO: clear timer
case RECONNECT_NETWORK:
updateConnectionState(ConnectionState.OFFLINE);
}
break;
case RECONNECT_NETWORK:
case RECONNECT_DELAYED:
switch (mState) {
case DISCONNECTED:
case RECONNECT_NETWORK:
case RECONNECT_DELAYED:
updateConnectionState(new_state);
break;
default:
throw new IllegalArgumentException("Can not go from " + mState + " to " + new_state);
}
}
}
@Override
public void requestConnectionState(ConnectionState new_state) {
requestConnectionState(new_state, false);
}
@Override
public ConnectionState getConnectionState() {
return mState;
}
// called at the end of a state transition
private synchronized void updateConnectionState(ConnectionState new_state) {
if (new_state == ConnectionState.ONLINE || new_state == ConnectionState.CONNECTING)
mLastError = null;
Log.d(TAG, "updateConnectionState: " + mState + " -> " + new_state + " (" + mLastError + ")");
if (new_state == mState)
return;
mState = new_state;
if (mServiceCallBack != null)
mServiceCallBack.connectionStateChanged();
}
private void initServiceDiscovery() {
// register connection features
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(mXMPPConnection);
// init Entity Caps manager with storage in app's cache dir
try {
if (capsCacheDir == null) {
capsCacheDir = new File(mService.getCacheDir(), "entity-caps-cache");
capsCacheDir.mkdirs();
EntityCapsManager.setPersistentCache(new SimpleDirectoryPersistentCache(capsCacheDir));
}
} catch (java.io.IOException e) {
Log.e(TAG, "Could not init Entity Caps cache: " + e.getLocalizedMessage());
}
// reference PingManager, set ping flood protection to 10s
PingManager.getInstanceFor(mXMPPConnection).setPingMinimumInterval(10*1000);
// set Version for replies
String app_name = mService.getString(org.yaxim.androidclient.R.string.app_name);
String build_revision = mService.getString(org.yaxim.androidclient.R.string.build_revision);
Version.Manager.getInstanceFor(mXMPPConnection).setVersion(
new Version(app_name, build_revision, "Android"));
// reference DeliveryReceiptManager, add listener
DeliveryReceiptManager dm = DeliveryReceiptManager.getInstanceFor(mXMPPConnection);
dm.enableAutoReceipts();
dm.addReceiptReceivedListener(new ReceiptReceivedListener() { // DOES NOT WORK IN CARBONS
public void onReceiptReceived(String fromJid, String toJid, String receiptId) {
Log.d(TAG, "got delivery receipt for " + receiptId);
changeMessageDeliveryStatus(receiptId, ChatConstants.DS_ACKED);
}});
}
public void addRosterItem(String user, String alias, String group)
throws YaximXMPPException {
tryToAddRosterEntry(user, alias, group);
}
public void removeRosterItem(String user) throws YaximXMPPException {
debugLog("removeRosterItem(" + user + ")");
tryToRemoveRosterEntry(user);
mServiceCallBack.rosterChanged();
}
public void renameRosterItem(String user, String newName)
throws YaximXMPPException {
RosterEntry rosterEntry = mRoster.getEntry(user);
if (!(newName.length() > 0) || (rosterEntry == null)) {
throw new YaximXMPPException("JabberID to rename is invalid!");
}
rosterEntry.setName(newName);
}
public void addRosterGroup(String group) {
mRoster.createGroup(group);
}
public void renameRosterGroup(String group, String newGroup) {
RosterGroup groupToRename = mRoster.getGroup(group);
groupToRename.setName(newGroup);
}
public void moveRosterItemToGroup(String user, String group)
throws YaximXMPPException {
tryToMoveRosterEntryToGroup(user, group);
}
public void sendPresenceRequest(String user, String type) {
// HACK: remove the fake roster entry added by handleIncomingSubscribe()
if ("unsubscribed".equals(type))
deleteRosterEntryFromDB(user);
Presence response = new Presence(Presence.Type.valueOf(type));
response.setTo(user);
mXMPPConnection.sendPacket(response);
}
private void onDisconnected(String reason) {
unregisterPongListener();
mLastError = reason;
updateConnectionState(ConnectionState.DISCONNECTED);
}
private void onDisconnected(Throwable reason) {
Log.e(TAG, "onDisconnected: " + reason);
reason.printStackTrace();
// iterate through to the deepest exception
while (reason.getCause() != null)
reason = reason.getCause();
onDisconnected(reason.getLocalizedMessage());
}
private void tryToConnect(boolean create_account) throws YaximXMPPException {
try {
if (mXMPPConnection.isConnected()) {
try {
mStreamHandler.quickShutdown(); // blocking shutdown prior to re-connection
} catch (Exception e) {
debugLog("conn.shutdown() failed: " + e);
}
}
registerRosterListener();
boolean need_bind = !mStreamHandler.isResumePossible();
mXMPPConnection.connect(need_bind);
// the following should not happen as of smack 3.3.1
if (!mXMPPConnection.isConnected()) {
throw new YaximXMPPException("SMACK connect failed without exception!");
}
if (mConnectionListener != null)
mXMPPConnection.removeConnectionListener(mConnectionListener);
mConnectionListener = new ConnectionListener() {
public void connectionClosedOnError(Exception e) {
onDisconnected(e);
}
public void connectionClosed() {
// TODO: fix reconnect when we got kicked by the server or SM failed!
//onDisconnected(null);
updateConnectionState(ConnectionState.OFFLINE);
}
public void reconnectingIn(int seconds) { }
public void reconnectionFailed(Exception e) { }
public void reconnectionSuccessful() { }
};
mXMPPConnection.addConnectionListener(mConnectionListener);
// SMACK auto-logins if we were authenticated before
if (!mXMPPConnection.isAuthenticated()) {
if (create_account) {
Log.d(TAG, "creating new server account...");
AccountManager am = new AccountManager(mXMPPConnection);
am.createAccount(mConfig.userName, mConfig.password);
}
mXMPPConnection.login(mConfig.userName, mConfig.password,
mConfig.ressource);
}
Log.d(TAG, "SM: can resume = " + mStreamHandler.isResumePossible() + " needbind=" + need_bind);
if (need_bind) {
mStreamHandler.notifyInitialLogin();
setStatusFromConfig();
}
} catch (YaximXMPPException e) {
throw e;
} catch (Exception e) {
// actually we just care for IllegalState or NullPointer or XMPPEx.
throw new YaximXMPPException("tryToConnect failed", e);
}
}
private void tryToMoveRosterEntryToGroup(String userName, String groupName)
throws YaximXMPPException {
RosterGroup rosterGroup = getRosterGroup(groupName);
RosterEntry rosterEntry = mRoster.getEntry(userName);
removeRosterEntryFromGroups(rosterEntry);
if (groupName.length() == 0)
return;
else {
try {
rosterGroup.addEntry(rosterEntry);
} catch (XMPPException e) {
throw new YaximXMPPException("tryToMoveRosterEntryToGroup", e);
}
}
}
private RosterGroup getRosterGroup(String groupName) {
RosterGroup rosterGroup = mRoster.getGroup(groupName);
// create group if unknown
if ((groupName.length() > 0) && rosterGroup == null) {
rosterGroup = mRoster.createGroup(groupName);
}
return rosterGroup;
}
private void removeRosterEntryFromGroups(RosterEntry rosterEntry)
throws YaximXMPPException {
Collection<RosterGroup> oldGroups = rosterEntry.getGroups();
for (RosterGroup group : oldGroups) {
tryToRemoveUserFromGroup(group, rosterEntry);
}
}
private void tryToRemoveUserFromGroup(RosterGroup group,
RosterEntry rosterEntry) throws YaximXMPPException {
try {
group.removeEntry(rosterEntry);
} catch (XMPPException e) {
throw new YaximXMPPException("tryToRemoveUserFromGroup", e);
}
}
private void tryToRemoveRosterEntry(String user) throws YaximXMPPException {
try {
RosterEntry rosterEntry = mRoster.getEntry(user);
if (rosterEntry != null) {
// first, unsubscribe the user
Presence unsub = new Presence(Presence.Type.unsubscribed);
unsub.setTo(rosterEntry.getUser());
mXMPPConnection.sendPacket(unsub);
// then, remove from roster
mRoster.removeEntry(rosterEntry);
}
} catch (XMPPException e) {
throw new YaximXMPPException("tryToRemoveRosterEntry", e);
}
}
private void tryToAddRosterEntry(String user, String alias, String group)
throws YaximXMPPException {
try {
mRoster.createEntry(user, alias, new String[] { group });
} catch (XMPPException e) {
throw new YaximXMPPException("tryToAddRosterEntry", e);
}
}
private void removeOldRosterEntries() {
Log.d(TAG, "removeOldRosterEntries()");
Collection<RosterEntry> rosterEntries = mRoster.getEntries();
StringBuilder exclusion = new StringBuilder(RosterConstants.JID + " NOT IN (");
boolean first = true;
for (RosterEntry rosterEntry : rosterEntries) {
updateRosterEntryInDB(rosterEntry);
if (first)
first = false;
else
exclusion.append(",");
exclusion.append("'").append(rosterEntry.getUser()).append("'");
}
exclusion.append(")");
int count = mContentResolver.delete(RosterProvider.CONTENT_URI, exclusion.toString(), null);
Log.d(TAG, "deleted " + count + " old roster entries");
}
// HACK: add an incoming subscription request as a fake roster entry
private void handleIncomingSubscribe(Presence request) {
final ContentValues values = new ContentValues();
values.put(RosterConstants.JID, request.getFrom());
values.put(RosterConstants.ALIAS, request.getFrom());
values.put(RosterConstants.GROUP, "");
values.put(RosterConstants.STATUS_MODE, getStatusInt(request));
values.put(RosterConstants.STATUS_MESSAGE, request.getStatus());
Uri uri = mContentResolver.insert(RosterProvider.CONTENT_URI, values);
debugLog("handleIncomingSubscribe: faked " + uri);
}
public void setStatusFromConfig() {
// TODO: only call this when carbons changed, not on every presence change
CarbonManager.getInstanceFor(mXMPPConnection).sendCarbonsEnabled(mConfig.messageCarbons);
Presence presence = new Presence(Presence.Type.available);
Mode mode = Mode.valueOf(mConfig.statusMode);
presence.setMode(mode);
presence.setStatus(mConfig.statusMessage);
presence.setPriority(mConfig.priority);
mXMPPConnection.sendPacket(presence);
mConfig.presence_required = false;
}
public void sendOfflineMessages() {
Cursor cursor = mContentResolver.query(ChatProvider.CONTENT_URI,
SEND_OFFLINE_PROJECTION, SEND_OFFLINE_SELECTION,
null, null);
final int _ID_COL = cursor.getColumnIndexOrThrow(ChatConstants._ID);
final int JID_COL = cursor.getColumnIndexOrThrow(ChatConstants.JID);
final int MSG_COL = cursor.getColumnIndexOrThrow(ChatConstants.MESSAGE);
final int TS_COL = cursor.getColumnIndexOrThrow(ChatConstants.DATE);
final int PACKETID_COL = cursor.getColumnIndexOrThrow(ChatConstants.PACKET_ID);
ContentValues mark_sent = new ContentValues();
mark_sent.put(ChatConstants.DELIVERY_STATUS, ChatConstants.DS_SENT_OR_READ);
while (cursor.moveToNext()) {
int _id = cursor.getInt(_ID_COL);
String toJID = cursor.getString(JID_COL);
String message = cursor.getString(MSG_COL);
String packetID = cursor.getString(PACKETID_COL);
long ts = cursor.getLong(TS_COL);
Log.d(TAG, "sendOfflineMessages: " + toJID + " > " + message);
final Message newMessage = new Message(toJID, Message.Type.chat);
newMessage.setBody(message);
DelayInformation delay = new DelayInformation(new Date(ts));
newMessage.addExtension(delay);
newMessage.addExtension(new DelayInfo(delay));
newMessage.addExtension(new DeliveryReceiptRequest());
if ((packetID != null) && (packetID.length() > 0)) {
newMessage.setPacketID(packetID);
} else {
packetID = newMessage.getPacketID();
mark_sent.put(ChatConstants.PACKET_ID, packetID);
}
Uri rowuri = Uri.parse("content://" + ChatProvider.AUTHORITY
+ "/" + ChatProvider.TABLE_NAME + "/" + _id);
mContentResolver.update(rowuri, mark_sent,
null, null);
mXMPPConnection.sendPacket(newMessage); // must be after marking delivered, otherwise it may override the SendFailListener
}
cursor.close();
}
public static void sendOfflineMessage(ContentResolver cr, String toJID, String message) {
ContentValues values = new ContentValues();
values.put(ChatConstants.DIRECTION, ChatConstants.OUTGOING);
values.put(ChatConstants.JID, toJID);
values.put(ChatConstants.MESSAGE, message);
values.put(ChatConstants.DELIVERY_STATUS, ChatConstants.DS_NEW);
values.put(ChatConstants.DATE, System.currentTimeMillis());
cr.insert(ChatProvider.CONTENT_URI, values);
}
public void sendReceipt(String toJID, String id) {
Log.d(TAG, "sending XEP-0184 ack to " + toJID + " id=" + id);
final Message ack = new Message(toJID, Message.Type.normal);
ack.addExtension(new DeliveryReceipt(id));
mXMPPConnection.sendPacket(ack);
}
public void sendMessage(String toJID, String message) {
final Message newMessage = new Message(toJID, Message.Type.chat);
newMessage.setBody(message);
newMessage.addExtension(new DeliveryReceiptRequest());
if (isAuthenticated()) {
addChatMessageToDB(ChatConstants.OUTGOING, toJID, message, ChatConstants.DS_SENT_OR_READ,
System.currentTimeMillis(), newMessage.getPacketID());
mXMPPConnection.sendPacket(newMessage);
} else {
// send offline -> store to DB
addChatMessageToDB(ChatConstants.OUTGOING, toJID, message, ChatConstants.DS_NEW,
System.currentTimeMillis(), newMessage.getPacketID());
}
}
public boolean isAuthenticated() {
if (mXMPPConnection != null) {
return (mXMPPConnection.isConnected() && mXMPPConnection
.isAuthenticated());
}
return false;
}
public void registerCallback(XMPPServiceCallback callBack) {
this.mServiceCallBack = callBack;
mService.registerReceiver(mPingAlarmReceiver, new IntentFilter(PING_ALARM));
mService.registerReceiver(mPongTimeoutAlarmReceiver, new IntentFilter(PONG_TIMEOUT_ALARM));
}
public void unRegisterCallback() {
debugLog("unRegisterCallback()");
// remove callbacks _before_ tossing old connection
try {
mXMPPConnection.getRoster().removeRosterListener(mRosterListener);
mXMPPConnection.removePacketListener(mPacketListener);
mXMPPConnection.removePacketListener(mPresenceListener);
mXMPPConnection.removePacketListener(mPongListener);
unregisterPongListener();
} catch (Exception e) {
// ignore it!
e.printStackTrace();
}
requestConnectionState(ConnectionState.OFFLINE);
setStatusOffline();
mService.unregisterReceiver(mPingAlarmReceiver);
mService.unregisterReceiver(mPongTimeoutAlarmReceiver);
this.mServiceCallBack = null;
}
public String getNameForJID(String jid) {
if (null != this.mRoster.getEntry(jid) && null != this.mRoster.getEntry(jid).getName() && this.mRoster.getEntry(jid).getName().length() > 0) {
return this.mRoster.getEntry(jid).getName();
} else {
return jid;
}
}
private void setStatusOffline() {
ContentValues values = new ContentValues();
values.put(RosterConstants.STATUS_MODE, StatusMode.offline.ordinal());
mContentResolver.update(RosterProvider.CONTENT_URI, values, null, null);
}
private void registerRosterListener() {
// flush roster on connecting.
mRoster = mXMPPConnection.getRoster();
mRoster.setSubscriptionMode(Roster.SubscriptionMode.manual);
if (mRosterListener != null)
mRoster.removeRosterListener(mRosterListener);
mRosterListener = new RosterListener() {
private boolean first_roster = true;
public void entriesAdded(Collection<String> entries) {
debugLog("entriesAdded(" + entries + ")");
ContentValues[] cvs = new ContentValues[entries.size()];
int i = 0;
for (String entry : entries) {
RosterEntry rosterEntry = mRoster.getEntry(entry);
cvs[i++] = getContentValuesForRosterEntry(rosterEntry);
}
mContentResolver.bulkInsert(RosterProvider.CONTENT_URI, cvs);
// when getting the roster in the beginning, remove remains of old one
if (first_roster) {
removeOldRosterEntries();
first_roster = false;
mServiceCallBack.rosterChanged();
}
debugLog("entriesAdded() done");
}
public void entriesDeleted(Collection<String> entries) {
debugLog("entriesDeleted(" + entries + ")");
for (String entry : entries) {
deleteRosterEntryFromDB(entry);
}
mServiceCallBack.rosterChanged();
}
public void entriesUpdated(Collection<String> entries) {
debugLog("entriesUpdated(" + entries + ")");
for (String entry : entries) {
RosterEntry rosterEntry = mRoster.getEntry(entry);
updateRosterEntryInDB(rosterEntry);
}
mServiceCallBack.rosterChanged();
}
public void presenceChanged(Presence presence) {
debugLog("presenceChanged(" + presence.getFrom() + "): " + presence);
String jabberID = getBareJID(presence.getFrom());
RosterEntry rosterEntry = mRoster.getEntry(jabberID);
updateRosterEntryInDB(rosterEntry);
mServiceCallBack.rosterChanged();
}
};
mRoster.addRosterListener(mRosterListener);
}
private String getBareJID(String from) {
String[] res = from.split("/");
return res[0].toLowerCase();
}
public boolean changeMessageDeliveryStatus(String packetID, int new_status) {
ContentValues cv = new ContentValues();
cv.put(ChatConstants.DELIVERY_STATUS, new_status);
Uri rowuri = Uri.parse("content://" + ChatProvider.AUTHORITY + "/"
+ ChatProvider.TABLE_NAME);
return mContentResolver.update(rowuri, cv,
ChatConstants.PACKET_ID + " = ? AND " +
ChatConstants.DELIVERY_STATUS + " != " + ChatConstants.DS_ACKED + " AND " +
ChatConstants.DIRECTION + " = " + ChatConstants.OUTGOING,
new String[] { packetID }) > 0;
}
/** Check the server connection, reconnect if needed.
*
* This function will try to ping the server if we are connected, and try
* to reestablish a connection otherwise.
*/
public void sendServerPing() {
if (mXMPPConnection == null || !mXMPPConnection.isAuthenticated()) {
debugLog("Ping: requested, but not connected to server.");
requestConnectionState(ConnectionState.ONLINE, false);
return;
}
if (mPingID != null) {
debugLog("Ping: requested, but still waiting for " + mPingID);
return; // a ping is still on its way
}
if (mStreamHandler.isSmEnabled()) {
debugLog("Ping: sending SM request");
mPingID = "" + mStreamHandler.requestAck();
} else {
Ping ping = new Ping();
ping.setType(Type.GET);
ping.setTo(mConfig.server);
mPingID = ping.getPacketID();
debugLog("Ping: sending ping " + mPingID);
mXMPPConnection.sendPacket(ping);
}
// register ping timeout handler: PACKET_TIMEOUT(30s) + 3s
registerPongTimeout(PACKET_TIMEOUT + 3000, mPingID);
}
private void gotServerPong(String pongID) {
long latency = System.currentTimeMillis() - mPingTimestamp;
if (pongID != null && pongID.equals(mPingID))
Log.i(TAG, String.format("Ping: server latency %1.3fs",
latency/1000.));
else
Log.i(TAG, String.format("Ping: server latency %1.3fs (estimated)",
latency/1000.));
mPingID = null;
mAlarmManager.cancel(mPongTimeoutAlarmPendIntent);
}
/** Register a "pong" timeout on the connection. */
private void registerPongTimeout(long wait_time, String id) {
mPingID = id;
mPingTimestamp = System.currentTimeMillis();
debugLog(String.format("Ping: registering timeout for %s: %1.3fs", id, wait_time/1000.));
mAlarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + wait_time,
mPongTimeoutAlarmPendIntent);
}
/**
* BroadcastReceiver to trigger reconnect on pong timeout.
*/
private class PongTimeoutAlarmReceiver extends BroadcastReceiver {
public void onReceive(Context ctx, Intent i) {
debugLog("Ping: timeout for " + mPingID);
onDisconnected("Ping timeout");
}
}
/**
* BroadcastReceiver to trigger sending pings to the server
*/
private class PingAlarmReceiver extends BroadcastReceiver {
public void onReceive(Context ctx, Intent i) {
sendServerPing();
}
}
/**
* Registers a smack packet listener for IQ packets, intended to recognize "pongs" with
* a packet id matching the last "ping" sent to the server.
*
* Also sets up the AlarmManager Timer plus necessary intents.
*/
private void registerPongListener() {
// reset ping expectation on new connection
mPingID = null;
if (mPongListener != null)
mXMPPConnection.removePacketListener(mPongListener);
mPongListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
if (packet == null) return;
gotServerPong(packet.getPacketID());
}
};
mXMPPConnection.addPacketListener(mPongListener, new PacketTypeFilter(IQ.class));
mPingAlarmPendIntent = PendingIntent.getBroadcast(mService.getApplicationContext(), 0, mPingAlarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mPongTimeoutAlarmPendIntent = PendingIntent.getBroadcast(mService.getApplicationContext(), 0, mPongTimeoutAlarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + AlarmManager.INTERVAL_FIFTEEN_MINUTES, AlarmManager.INTERVAL_FIFTEEN_MINUTES, mPingAlarmPendIntent);
}
private void unregisterPongListener() {
mAlarmManager.cancel(mPingAlarmPendIntent);
mAlarmManager.cancel(mPongTimeoutAlarmPendIntent);
}
private void registerMessageListener() {
// do not register multiple packet listeners
if (mPacketListener != null)
mXMPPConnection.removePacketListener(mPacketListener);
PacketTypeFilter filter = new PacketTypeFilter(Message.class);
mPacketListener = new PacketListener() {
public void processPacket(Packet packet) {
try {
if (packet instanceof Message) {
Message msg = (Message) packet;
String fromJID = getBareJID(msg.getFrom());