forked from ge0rg/yaxim
/
XMPPService.java
531 lines (456 loc) · 15.4 KB
/
XMPPService.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
package org.yaxim.androidclient.service;
import java.util.HashSet;
import java.util.concurrent.atomic.AtomicBoolean;
import org.yaxim.androidclient.IXMPPRosterCallback;
import org.yaxim.androidclient.MainWindow;
import org.yaxim.androidclient.R;
import org.yaxim.androidclient.exceptions.YaximXMPPException;
import org.yaxim.androidclient.util.ConnectionState;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
public class XMPPService extends GenericService {
private AtomicBoolean mIsConnected = new AtomicBoolean(false);
private AtomicBoolean mConnectionDemanded = new AtomicBoolean(false); // should we try to reconnect?
private static final int RECONNECT_AFTER = 5;
private static final int RECONNECT_MAXIMUM = 10*60;
private static final String RECONNECT_ALARM = "org.yaxim.androidclient.RECONNECT_ALARM";
private int mReconnectTimeout = RECONNECT_AFTER;
private String mLastConnectionError = null;
private String mReconnectInfo = "";
private Intent mAlarmIntent = new Intent(RECONNECT_ALARM);
private PendingIntent mPAlarmIntent;
private BroadcastReceiver mAlarmReceiver = new ReconnectAlarmReceiver();
private ServiceNotification mServiceNotification = null;
private Thread mConnectingThread;
private Smackable mSmackable;
private boolean create_account = false;
private IXMPPRosterService.Stub mService2RosterConnection;
private IXMPPChatService.Stub mServiceChatConnection;
private RemoteCallbackList<IXMPPRosterCallback> mRosterCallbacks = new RemoteCallbackList<IXMPPRosterCallback>();
private HashSet<String> mIsBoundTo = new HashSet<String>();
private Handler mMainHandler = new Handler();
@Override
public IBinder onBind(Intent intent) {
super.onBind(intent);
String chatPartner = intent.getDataString();
if ((chatPartner != null)) {
resetNotificationCounter(chatPartner);
mIsBoundTo.add(chatPartner);
return mServiceChatConnection;
}
return mService2RosterConnection;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
String chatPartner = intent.getDataString();
if ((chatPartner != null)) {
mIsBoundTo.add(chatPartner);
resetNotificationCounter(chatPartner);
}
}
@Override
public boolean onUnbind(Intent intent) {
String chatPartner = intent.getDataString();
if ((chatPartner != null)) {
mIsBoundTo.remove(chatPartner);
}
return true;
}
@Override
public void onCreate() {
super.onCreate();
createServiceRosterStub();
createServiceChatStub();
mPAlarmIntent = PendingIntent.getBroadcast(this, 0, mAlarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
registerReceiver(mAlarmReceiver, new IntentFilter(RECONNECT_ALARM));
// for the initial connection, check if autoConnect is set
mConnectionDemanded.set(mConfig.autoConnect);
YaximBroadcastReceiver.initNetworkStatus(getApplicationContext());
if (mConfig.autoConnect) {
/*
* start our own service so it remains in background even when
* unbound
*/
Intent xmppServiceIntent = new Intent(this, XMPPService.class);
xmppServiceIntent.setAction("org.yaxim.androidclient.XMPPSERVICE");
startService(xmppServiceIntent);
}
mServiceNotification = ServiceNotification.getInstance();
}
@Override
public void onDestroy() {
super.onDestroy();
((AlarmManager)getSystemService(Context.ALARM_SERVICE)).cancel(mPAlarmIntent);
mRosterCallbacks.kill();
performDisconnect();
unregisterReceiver(mAlarmReceiver);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
logInfo("onStartCommand()");
if (intent != null) {
boolean disconnect = intent.getBooleanExtra("disconnect", false);
boolean reconnect = intent.getBooleanExtra("reconnect", false);
create_account = intent.getBooleanExtra("create_account", false);
logInfo("disconnect/reconnect: "+disconnect+ " " + reconnect);
if (disconnect) {
if (mConnectingThread != null || mIsConnected.get())
connectionFailed(getString(R.string.conn_networkchg));
return START_STICKY;
}
if (reconnect) {
// reset reconnection timeout
mReconnectTimeout = RECONNECT_AFTER;
if (mConnectionDemanded.get())
doConnect();
else
stopSelf(); // started by YaximBroadcastReceiver, no connection initiation
return START_STICKY;
}
}
mConnectionDemanded.set(mConfig.autoConnect);
doConnect();
return START_STICKY;
}
private void createServiceChatStub() {
mServiceChatConnection = new IXMPPChatService.Stub() {
public void sendMessage(String user, String message)
throws RemoteException {
if (mSmackable != null)
mSmackable.sendMessage(user, message);
else
SmackableImp.sendOfflineMessage(getContentResolver(),
user, message);
}
public boolean isAuthenticated() throws RemoteException {
if (mSmackable != null) {
return mSmackable.isAuthenticated();
}
return false;
}
public void clearNotifications(String Jid) throws RemoteException {
clearNotification(Jid);
}
};
}
private void createServiceRosterStub() {
mService2RosterConnection = new IXMPPRosterService.Stub() {
public void registerRosterCallback(IXMPPRosterCallback callback)
throws RemoteException {
if (callback != null)
mRosterCallbacks.register(callback);
}
public void unregisterRosterCallback(IXMPPRosterCallback callback)
throws RemoteException {
if (callback != null)
mRosterCallbacks.unregister(callback);
}
public int getConnectionState() throws RemoteException {
if (mSmackable != null && mSmackable.isAuthenticated()) {
return ConnectionState.AUTHENTICATED;
} else if (mConnectionDemanded.get() &&
networkConnectedOrConnecting()) {
return ConnectionState.CONNECTING;
} else {
return ConnectionState.OFFLINE;
}
}
public String getConnectionStateString() throws RemoteException {
if (mLastConnectionError != null)
return mLastConnectionError + mReconnectInfo;
else
return null;
}
public void setStatusFromConfig()
throws RemoteException {
mSmackable.setStatusFromConfig();
updateServiceNotification();
}
public void addRosterItem(String user, String alias, String group)
throws RemoteException {
try {
mSmackable.addRosterItem(user, alias, group);
} catch (YaximXMPPException e) {
shortToastNotify(e.getMessage());
logError("exception in addRosterItem(): " + e.getMessage());
}
}
public void addRosterGroup(String group) throws RemoteException {
mSmackable.addRosterGroup(group);
}
public void removeRosterItem(String user) throws RemoteException {
try {
mSmackable.removeRosterItem(user);
} catch (YaximXMPPException e) {
shortToastNotify(e.getMessage());
logError("exception in removeRosterItem(): "
+ e.getMessage());
}
}
public void moveRosterItemToGroup(String user, String group)
throws RemoteException {
try {
mSmackable.moveRosterItemToGroup(user, group);
} catch (YaximXMPPException e) {
shortToastNotify(e.getMessage());
logError("exception in moveRosterItemToGroup(): "
+ e.getMessage());
}
}
public void renameRosterItem(String user, String newName)
throws RemoteException {
try {
mSmackable.renameRosterItem(user, newName);
} catch (YaximXMPPException e) {
shortToastNotify(e.getMessage());
logError("exception in renameRosterItem(): "
+ e.getMessage());
}
}
public void renameRosterGroup(String group, String newGroup)
throws RemoteException {
mSmackable.renameRosterGroup(group, newGroup);
}
public void disconnect() throws RemoteException {
manualDisconnect();
}
public void connect() throws RemoteException {
mConnectionDemanded.set(true);
doConnect();
}
public void requestAuthorizationForRosterItem(String user)
throws RemoteException {
mSmackable.requestAuthorizationForRosterItem(user);
}
};
}
private void updateServiceNotification() {
if (!mConfig.foregroundService)
return;
String title = getString(R.string.conn_title, mConfig.jabberID);
Notification n = new Notification(R.drawable.ic_offline, title,
System.currentTimeMillis());
n.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
Intent notificationIntent = new Intent(this, MainWindow.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
n.contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
String message = mLastConnectionError;
if (message != null)
message += mReconnectInfo;
if (mIsConnected.get()) {
message = MainWindow.getStatusTitle(this, mConfig.statusMode, mConfig.statusMessage);
n.icon = R.drawable.ic_online;
}
n.setLatestEventInfo(this, title, message, n.contentIntent);
mServiceNotification.showNotification(this, SERVICE_NOTIFICATION,
n);
}
private void doConnect() {
if (mConnectingThread != null) {
// a connection is still goign on!
return;
}
mLastConnectionError = getString(R.string.conn_connecting);
updateServiceNotification();
if (mSmackable != null) {
mSmackable.unRegisterCallback();
}
mConnectingThread = new Thread() {
public void run() {
try {
createAdapter();
if (!mSmackable.doConnect(create_account)) {
postConnectionFailed("Inconsistency in Smackable.doConnect()");
}
postConnectionEstablished();
} catch (YaximXMPPException e) {
String message = e.getLocalizedMessage();
if (e.getCause() != null)
message += "\n" + e.getCause().getLocalizedMessage();
postConnectionFailed(message);
logError("YaximXMPPException in doConnect():");
e.printStackTrace();
} finally {
if (mConnectingThread != null) synchronized(mConnectingThread) {
mConnectingThread = null;
}
create_account = false;
}
}
};
mConnectingThread.start();
}
private void postConnectionFailed(final String reason) {
mMainHandler.post(new Runnable() {
public void run() {
connectionFailed(reason);
}
});
}
private void postConnectionEstablished() {
mMainHandler.post(new Runnable() {
public void run() {
connectionEstablished();
}
});
}
private void postRosterChanged() {
mMainHandler.post(new Runnable() {
public void run() {
rosterChanged();
}
});
}
private void broadcastConnectionStatus(boolean isConnected, boolean willReconnect) {
final int broadCastItems = mRosterCallbacks.beginBroadcast();
for (int i = 0; i < broadCastItems; i++) {
try {
mRosterCallbacks.getBroadcastItem(i).connectionStatusChanged(isConnected, willReconnect);
} catch (RemoteException e) {
logError("caught RemoteException: " + e.getMessage());
}
}
mRosterCallbacks.finishBroadcast();
}
private NetworkInfo getNetworkInfo() {
Context ctx = getApplicationContext();
ConnectivityManager connMgr =
(ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
return connMgr.getActiveNetworkInfo();
}
private boolean networkConnected() {
NetworkInfo info = getNetworkInfo();
return info != null && info.isConnected();
}
private boolean networkConnectedOrConnecting() {
NetworkInfo info = getNetworkInfo();
return info != null && info.isConnectedOrConnecting();
}
private void connectionFailed(String reason) {
logInfo("connectionFailed: " + reason);
mLastConnectionError = reason;
mIsConnected.set(false);
broadcastConnectionStatus(false, mConnectionDemanded.get());
if (!networkConnected()) {
mLastConnectionError = null;
mReconnectInfo = "";
updateServiceNotification();
if (mSmackable != null) {
mSmackable.unRegisterCallback();
mSmackable = null;
}
YaximBroadcastReceiver.initNetworkStatus(getApplicationContext());
} else if (mConnectionDemanded.get()) {
mReconnectInfo = getString(R.string.conn_reconnect, mReconnectTimeout);
updateServiceNotification();
logInfo("connectionFailed(): registering reconnect in " + mReconnectTimeout + "s");
((AlarmManager)getSystemService(Context.ALARM_SERVICE)).set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + mReconnectTimeout * 1000, mPAlarmIntent);
mReconnectTimeout = mReconnectTimeout * 2;
if (mReconnectTimeout > RECONNECT_MAXIMUM)
mReconnectTimeout = RECONNECT_MAXIMUM;
} else {
mReconnectInfo = "";
mServiceNotification.hideNotification(this, SERVICE_NOTIFICATION);
}
}
private void connectionEstablished() {
// once we are connected, use autoReconnect to determine reconnections
mConnectionDemanded.set(mConfig.autoReconnect);
mLastConnectionError = null;
mIsConnected.set(true);
mReconnectTimeout = RECONNECT_AFTER;
updateServiceNotification();
broadcastConnectionStatus(true, false);
}
private void rosterChanged() {
// gracefully handle^W ignore events after a disconnect
if (mSmackable == null)
return;
if (!mIsConnected.get() && mSmackable.isAuthenticated()) {
// We get a roster changed update, but we are not connected,
// that means we just got connected and need to notify the Activity.
logInfo("rosterChanged(): we just got connected");
//connectionEstablished();
}
if (mIsConnected.get() && mSmackable != null && !mSmackable.isAuthenticated()) {
logInfo("rosterChanged(): disconnected without warning");
connectionFailed(getString(R.string.conn_disconnected));
}
}
public void manualDisconnect() {
mConnectionDemanded.set(false);
performDisconnect();
}
public void performDisconnect() {
if (mConnectingThread != null) {
synchronized(mConnectingThread) {
try {
mConnectingThread.interrupt();
mConnectingThread.join(50);
} catch (InterruptedException e) {
logError("doDisconnect: failed catching connecting thread");
} finally {
mConnectingThread = null;
}
}
}
if (mSmackable != null) {
mSmackable.unRegisterCallback();
mSmackable = null;
}
connectionFailed(getString(R.string.conn_offline));
mServiceNotification.hideNotification(this, SERVICE_NOTIFICATION);
}
private void createAdapter() {
System.setProperty("smack.debugEnabled", "" + mConfig.smackdebug);
try {
mSmackable = new SmackableImp(mConfig, getContentResolver(), this);
} catch (NullPointerException e) {
e.printStackTrace();
}
mSmackable.registerCallback(new XMPPServiceCallback() {
public void newMessage(String from, String message) {
logInfo("notification: " + from);
notifyClient(from, mSmackable.getNameForJID(from), message, !mIsBoundTo.contains(from));
}
public void rosterChanged() {
postRosterChanged();
}
public void disconnectOnError() {
logInfo("Smackable disconnected on error");
postConnectionFailed(getString(R.string.conn_disconnected));
}
public boolean isBoundTo(String jabberID) {
return mIsBoundTo.contains(jabberID);
}
});
}
private class ReconnectAlarmReceiver extends BroadcastReceiver {
public void onReceive(Context ctx, Intent i) {
logInfo("Alarm received.");
if (!mConnectionDemanded.get()) {
return;
}
if (mIsConnected.get()) {
logError("Reconnect attempt aborted: we are connected again!");
return;
}
doConnect();
}
}
}