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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
|
/*
* Copyright (C) 2010, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.sip;
import android.app.AlarmManager;
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.net.sip.ISipService;
import android.net.sip.ISipSession;
import android.net.sip.ISipSessionListener;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.net.sip.SipSessionAdapter;
import android.net.sip.SipSessionState;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeSet;
import javax.sip.SipException;
public final class SipService extends ISipService.Stub {
private static final String TAG = "SipService";
private static final int EXPIRY_TIME = 3600;
private static final int SHORT_EXPIRY_TIME = 10;
private static final int MIN_EXPIRY_TIME = 60;
private Context mContext;
private String mLocalIp;
private String mNetworkType;
private boolean mConnected;
private WakeupTimer mTimer;
private WifiManager.WifiLock mWifiLock;
// SipProfile URI --> group
private Map<String, SipSessionGroupExt> mSipGroups =
new HashMap<String, SipSessionGroupExt>();
// session ID --> session
private Map<String, ISipSession> mPendingSessions =
new HashMap<String, ISipSession>();
private ConnectivityReceiver mConnectivityReceiver;
/**
* Creates a {@code SipService} instance. Returns null if SIP API is not
* supported.
*/
public static SipService create(Context context) {
return (SipManager.isApiSupported(context) ? new SipService(context)
: null);
}
private SipService(Context context) {
Log.v(TAG, " service started!");
mContext = context;
mConnectivityReceiver = new ConnectivityReceiver();
context.registerReceiver(mConnectivityReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
mTimer = new WakeupTimer(context);
}
public synchronized SipProfile[] getListOfProfiles() {
SipProfile[] profiles = new SipProfile[mSipGroups.size()];
int i = 0;
for (SipSessionGroupExt group : mSipGroups.values()) {
profiles[i++] = group.getLocalProfile();
}
return profiles;
}
public void open(SipProfile localProfile) {
localProfile.setCallingUid(Binder.getCallingUid());
if (localProfile.getAutoRegistration()) {
openToReceiveCalls(localProfile);
} else {
openToMakeCalls(localProfile);
}
}
private void openToMakeCalls(SipProfile localProfile) {
try {
createGroup(localProfile);
} catch (SipException e) {
Log.e(TAG, "openToMakeCalls()", e);
// TODO: how to send the exception back
}
}
private void openToReceiveCalls(SipProfile localProfile) {
open3(localProfile, SipManager.SIP_INCOMING_CALL_ACTION, null);
}
public synchronized void open3(SipProfile localProfile,
String incomingCallBroadcastAction, ISipSessionListener listener) {
localProfile.setCallingUid(Binder.getCallingUid());
if (TextUtils.isEmpty(incomingCallBroadcastAction)) {
throw new RuntimeException(
"empty broadcast action for incoming call");
}
Log.v(TAG, "open3: " + localProfile.getUriString() + ": "
+ incomingCallBroadcastAction + ": " + listener);
try {
SipSessionGroupExt group = createGroup(localProfile,
incomingCallBroadcastAction, listener);
if (localProfile.getAutoRegistration()) {
group.openToReceiveCalls();
if (isWifiOn()) grabWifiLock();
}
} catch (SipException e) {
Log.e(TAG, "openToReceiveCalls()", e);
// TODO: how to send the exception back
}
}
public synchronized void close(String localProfileUri) {
SipSessionGroupExt group = mSipGroups.remove(localProfileUri);
if (group != null) {
notifyProfileRemoved(group.getLocalProfile());
group.closeToNotReceiveCalls();
if (isWifiOn() && !anyOpened()) releaseWifiLock();
}
}
public synchronized boolean isOpened(String localProfileUri) {
SipSessionGroupExt group = mSipGroups.get(localProfileUri);
return ((group != null) ? group.isOpened() : false);
}
public synchronized boolean isRegistered(String localProfileUri) {
SipSessionGroupExt group = mSipGroups.get(localProfileUri);
return ((group != null) ? group.isRegistered() : false);
}
public synchronized void setRegistrationListener(String localProfileUri,
ISipSessionListener listener) {
SipSessionGroupExt group = mSipGroups.get(localProfileUri);
if (group != null) group.setListener(listener);
}
public synchronized ISipSession createSession(SipProfile localProfile,
ISipSessionListener listener) {
localProfile.setCallingUid(Binder.getCallingUid());
if (!mConnected) return null;
try {
SipSessionGroupExt group = createGroup(localProfile);
return group.createSession(listener);
} catch (SipException e) {
Log.w(TAG, "createSession()", e);
return null;
}
}
public synchronized ISipSession getPendingSession(String callId) {
if (callId == null) return null;
return mPendingSessions.get(callId);
}
private String determineLocalIp() {
try {
DatagramSocket s = new DatagramSocket();
s.connect(InetAddress.getByName("192.168.1.1"), 80);
return s.getLocalAddress().getHostAddress();
} catch (IOException e) {
Log.w(TAG, "determineLocalIp()", e);
// dont do anything; there should be a connectivity change going
return null;
}
}
private SipSessionGroupExt createGroup(SipProfile localProfile)
throws SipException {
String key = localProfile.getUriString();
SipSessionGroupExt group = mSipGroups.get(key);
if (group == null) {
group = new SipSessionGroupExt(localProfile, null, null);
mSipGroups.put(key, group);
notifyProfileAdded(localProfile);
}
return group;
}
private SipSessionGroupExt createGroup(SipProfile localProfile,
String incomingCallBroadcastAction, ISipSessionListener listener)
throws SipException {
String key = localProfile.getUriString();
SipSessionGroupExt group = mSipGroups.get(key);
if (group != null) {
group.setIncomingCallBroadcastAction(
incomingCallBroadcastAction);
group.setListener(listener);
} else {
group = new SipSessionGroupExt(localProfile,
incomingCallBroadcastAction, listener);
mSipGroups.put(key, group);
notifyProfileAdded(localProfile);
}
return group;
}
private void notifyProfileAdded(SipProfile localProfile) {
Log.d(TAG, "notify: profile added: " + localProfile);
Intent intent = new Intent(SipManager.SIP_ADD_PHONE_ACTION);
intent.putExtra(SipManager.LOCAL_URI_KEY, localProfile.getUriString());
mContext.sendBroadcast(intent);
}
private void notifyProfileRemoved(SipProfile localProfile) {
Log.d(TAG, "notify: profile removed: " + localProfile);
Intent intent = new Intent(SipManager.SIP_REMOVE_PHONE_ACTION);
intent.putExtra(SipManager.LOCAL_URI_KEY, localProfile.getUriString());
mContext.sendBroadcast(intent);
}
private boolean anyOpened() {
for (SipSessionGroupExt group : mSipGroups.values()) {
if (group.isOpened()) return true;
}
return false;
}
private void grabWifiLock() {
if (mWifiLock == null) {
Log.v(TAG, "acquire wifi lock");
mWifiLock = ((WifiManager)
mContext.getSystemService(Context.WIFI_SERVICE))
.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
mWifiLock.acquire();
}
}
private void releaseWifiLock() {
if (mWifiLock != null) {
Log.v(TAG, "release wifi lock");
mWifiLock.release();
mWifiLock = null;
}
}
private boolean isWifiOn() {
return "WIFI".equalsIgnoreCase(mNetworkType);
//return (mConnected && "WIFI".equalsIgnoreCase(mNetworkType));
}
private synchronized void onConnectivityChanged(
String type, boolean connected) {
Log.v(TAG, "onConnectivityChanged(): "
+ mNetworkType + (mConnected? " CONNECTED" : " DISCONNECTED")
+ " --> " + type + (connected? " CONNECTED" : " DISCONNECTED"));
boolean sameType = type.equals(mNetworkType);
if (!sameType && !connected) return;
boolean wasWifi = "WIFI".equalsIgnoreCase(mNetworkType);
boolean isWifi = "WIFI".equalsIgnoreCase(type);
boolean wifiOff = (isWifi && !connected) || (wasWifi && !sameType);
boolean wifiOn = isWifi && connected;
if (wifiOff) {
releaseWifiLock();
} else if (wifiOn) {
if (anyOpened()) grabWifiLock();
}
try {
boolean wasConnected = mConnected;
mNetworkType = type;
mConnected = connected;
if (wasConnected) {
mLocalIp = null;
for (SipSessionGroupExt group : mSipGroups.values()) {
group.onConnectivityChanged(false);
}
}
if (connected) {
mLocalIp = determineLocalIp();
for (SipSessionGroupExt group : mSipGroups.values()) {
group.onConnectivityChanged(true);
}
}
} catch (SipException e) {
Log.e(TAG, "onConnectivityChanged()", e);
}
}
private synchronized void addPendingSession(ISipSession session) {
try {
mPendingSessions.put(session.getCallId(), session);
} catch (RemoteException e) {
// should not happen with a local call
Log.e(TAG, "addPendingSession()", e);
}
}
private class SipSessionGroupExt extends SipSessionAdapter {
private SipSessionGroup mSipGroup;
private String mIncomingCallBroadcastAction;
private boolean mOpened;
private AutoRegistrationProcess mAutoRegistration =
new AutoRegistrationProcess();
public SipSessionGroupExt(SipProfile localProfile,
String incomingCallBroadcastAction,
ISipSessionListener listener) throws SipException {
String password = localProfile.getPassword();
SipProfile p = duplicate(localProfile);
mSipGroup = createSipSessionGroup(mLocalIp, p, password);
mIncomingCallBroadcastAction = incomingCallBroadcastAction;
mAutoRegistration.setListener(listener);
}
public SipProfile getLocalProfile() {
return mSipGroup.getLocalProfile();
}
// network connectivity is tricky because network can be disconnected
// at any instant so need to deal with exceptions carefully even when
// you think you are connected
private SipSessionGroup createSipSessionGroup(String localIp,
SipProfile localProfile, String password) throws SipException {
try {
return new SipSessionGroup(localIp, localProfile, password);
} catch (IOException e) {
// network disconnected
Log.w(TAG, "createSipSessionGroup(): network disconnected?");
if (localIp != null) {
return createSipSessionGroup(null, localProfile, password);
} else {
// recursive
Log.wtf(TAG, "impossible!");
throw new RuntimeException("createSipSessionGroup");
}
}
}
private SipProfile duplicate(SipProfile p) {
try {
return new SipProfile.Builder(p).setPassword("*").build();
} catch (Exception e) {
Log.wtf(TAG, "duplicate()", e);
throw new RuntimeException("duplicate profile", e);
}
}
public void setListener(ISipSessionListener listener) {
mAutoRegistration.setListener(listener);
}
public void setIncomingCallBroadcastAction(String action) {
mIncomingCallBroadcastAction = action;
}
public void openToReceiveCalls() throws SipException {
mOpened = true;
if (mConnected) {
mSipGroup.openToReceiveCalls(this);
mAutoRegistration.start(mSipGroup);
}
Log.v(TAG, " openToReceiveCalls: " + getUri() + ": "
+ mIncomingCallBroadcastAction);
}
public void onConnectivityChanged(boolean connected)
throws SipException {
if (connected) {
resetGroup(mLocalIp);
if (mOpened) openToReceiveCalls();
} else {
// close mSipGroup but remember mOpened
Log.v(TAG, " close auto reg temporarily: " + getUri() + ": "
+ mIncomingCallBroadcastAction);
mSipGroup.close();
mAutoRegistration.stop();
}
}
private void resetGroup(String localIp) throws SipException {
try {
mSipGroup.reset(localIp);
} catch (IOException e) {
// network disconnected
Log.w(TAG, "resetGroup(): network disconnected?");
if (localIp != null) {
resetGroup(null); // reset w/o local IP
} else {
// recursive
Log.wtf(TAG, "impossible!");
throw new RuntimeException("resetGroup");
}
}
}
public void closeToNotReceiveCalls() {
mOpened = false;
mSipGroup.closeToNotReceiveCalls();
mAutoRegistration.stop();
Log.v(TAG, " close: " + getUri() + ": "
+ mIncomingCallBroadcastAction);
}
public ISipSession createSession(ISipSessionListener listener) {
return mSipGroup.createSession(listener);
}
@Override
public void onRinging(ISipSession session, SipProfile caller,
byte[] sessionDescription) {
synchronized (SipService.this) {
try {
if (!isRegistered()) {
session.endCall();
return;
}
// send out incoming call broadcast
Log.d(TAG, " ringing~~ " + getUri() + ": " + caller.getUri()
+ ": " + session.getCallId());
addPendingSession(session);
Intent intent = SipManager.createIncomingCallBroadcast(
mIncomingCallBroadcastAction, session.getCallId(),
sessionDescription);
Log.d(TAG, " send out intent: " + intent);
mContext.sendBroadcast(intent);
} catch (RemoteException e) {
// should never happen with a local call
Log.e(TAG, "processCall()", e);
}
}
}
@Override
public void onError(ISipSession session, String errorClass,
String message) {
Log.v(TAG, "sip session error: " + errorClass + ": " + message);
}
public boolean isOpened() {
return mOpened;
}
public boolean isRegistered() {
return mAutoRegistration.isRegistered();
}
private String getUri() {
return mSipGroup.getLocalProfileUri();
}
}
private class KeepAliveProcess implements Runnable {
private static final String TAG = "\\KEEPALIVE/";
private static final int INTERVAL = 15;
private SipSessionGroup.SipSessionImpl mSession;
public KeepAliveProcess(SipSessionGroup.SipSessionImpl session) {
mSession = session;
}
public void start() {
mTimer.set(INTERVAL * 1000, this);
}
public void run() {
synchronized (SipService.this) {
SipSessionGroup.SipSessionImpl session = mSession.duplicate();
Log.d(TAG, " ~~~ keepalive");
mTimer.cancel(this);
session.sendKeepAlive();
if (session.isReRegisterRequired()) {
mSession.register(EXPIRY_TIME);
} else {
mTimer.set(INTERVAL * 1000, this);
}
}
}
public void stop() {
mTimer.cancel(this);
}
}
private class AutoRegistrationProcess extends SipSessionAdapter
implements Runnable {
private SipSessionGroup.SipSessionImpl mSession;
private SipSessionListenerProxy mProxy = new SipSessionListenerProxy();
private KeepAliveProcess mKeepAliveProcess;
private int mBackoff = 1;
private boolean mRegistered;
private long mExpiryTime;
private String getAction() {
return toString();
}
public void start(SipSessionGroup group) {
if (mSession == null) {
mBackoff = 1;
mSession = (SipSessionGroup.SipSessionImpl)
group.createSession(this);
// return right away if no active network connection.
if (mSession == null) return;
// start unregistration to clear up old registration at server
// TODO: when rfc5626 is deployed, use reg-id and sip.instance
// in registration to avoid adding duplicate entries to server
mSession.unregister();
Log.v(TAG, "start AutoRegistrationProcess for "
+ mSession.getLocalProfile().getUriString());
}
}
public void stop() {
if (mSession == null) return;
if (mConnected) mSession.unregister();
mTimer.cancel(this);
if (mKeepAliveProcess != null) {
mKeepAliveProcess.stop();
mKeepAliveProcess = null;
}
mSession = null;
mRegistered = false;
}
private boolean isStopped() {
return (mSession == null);
}
public void setListener(ISipSessionListener listener) {
Log.v(TAG, "setListener(): " + listener);
mProxy.setListener(listener);
if (mSession == null) return;
try {
if ((mSession != null) && SipSessionState.REGISTERING.equals(
mSession.getState())) {
mProxy.onRegistering(mSession);
} else if (mRegistered) {
int duration = (int)
(mExpiryTime - SystemClock.elapsedRealtime());
mProxy.onRegistrationDone(mSession, duration);
}
} catch (Throwable t) {
Log.w(TAG, "setListener(): " + t);
}
}
public boolean isRegistered() {
return mRegistered;
}
public void run() {
Log.v(TAG, " ~~~ registering");
synchronized (SipService.this) {
if (mConnected && !isStopped()) mSession.register(EXPIRY_TIME);
}
}
private boolean isBehindNAT(String address) {
try {
byte[] d = InetAddress.getByName(address).getAddress();
if ((d[0] == 10) ||
(((0x000000FF & ((int)d[0])) == 172) &&
((0x000000F0 & ((int)d[1])) == 16)) ||
(((0x000000FF & ((int)d[0])) == 192) &&
((0x000000FF & ((int)d[1])) == 168))) {
return true;
}
} catch (UnknownHostException e) {
Log.e(TAG, "isBehindAT()" + address, e);
}
return false;
}
private void restart(int duration) {
Log.v(TAG, "Refresh registration " + duration + "s later.");
mTimer.cancel(this);
mTimer.set(duration * 1000, this);
}
private int backoffDuration() {
int duration = SHORT_EXPIRY_TIME * mBackoff;
if (duration > 3600) {
duration = 3600;
} else {
mBackoff *= 2;
}
return duration;
}
@Override
public void onRegistering(ISipSession session) {
Log.v(TAG, "onRegistering(): " + session + ": " + mSession);
synchronized (SipService.this) {
if (!isStopped() && (session != mSession)) return;
mRegistered = false;
try {
mProxy.onRegistering(session);
} catch (Throwable t) {
Log.w(TAG, "onRegistering()", t);
}
}
}
@Override
public void onRegistrationDone(ISipSession session, int duration) {
Log.v(TAG, "onRegistrationDone(): " + session + ": " + mSession);
synchronized (SipService.this) {
if (!isStopped() && (session != mSession)) return;
try {
mProxy.onRegistrationDone(session, duration);
} catch (Throwable t) {
Log.w(TAG, "onRegistrationDone()", t);
}
if (isStopped()) return;
if (duration > 0) {
mSession.clearReRegisterRequired();
mExpiryTime = SystemClock.elapsedRealtime()
+ (duration * 1000);
if (!mRegistered) {
mRegistered = true;
// allow some overlap to avoid call drop during renew
duration -= MIN_EXPIRY_TIME;
if (duration < MIN_EXPIRY_TIME) {
duration = MIN_EXPIRY_TIME;
}
restart(duration);
if (isBehindNAT(mLocalIp) ||
mSession.getLocalProfile().getSendKeepAlive()) {
if (mKeepAliveProcess == null) {
mKeepAliveProcess =
new KeepAliveProcess(mSession);
}
mKeepAliveProcess.start();
}
}
} else {
mRegistered = false;
mExpiryTime = -1L;
Log.v(TAG, "Refresh registration immediately");
run();
}
}
}
@Override
public void onRegistrationFailed(ISipSession session, String className,
String message) {
Log.v(TAG, "onRegistrationFailed(): " + session + ": " + mSession
+ ": " + className + ": " + message);
synchronized (SipService.this) {
if (!isStopped() && (session != mSession)) return;
try {
mProxy.onRegistrationFailed(session, className, message);
} catch (Throwable t) {
Log.w(TAG, "onRegistrationFailed(): " + t);
}
if (!isStopped()) onError();
}
}
@Override
public void onRegistrationTimeout(ISipSession session) {
Log.v(TAG, "onRegistrationTimeout(): " + session + ": " + mSession);
synchronized (SipService.this) {
if (!isStopped() && (session != mSession)) return;
try {
mProxy.onRegistrationTimeout(session);
} catch (Throwable t) {
Log.w(TAG, "onRegistrationTimeout(): " + t);
}
if (!isStopped()) {
mRegistered = false;
onError();
}
}
}
private void onError() {
mRegistered = false;
restart(backoffDuration());
if (mKeepAliveProcess != null) {
mKeepAliveProcess.stop();
mKeepAliveProcess = null;
}
}
}
private class ConnectivityReceiver extends BroadcastReceiver {
private Timer mTimer = new Timer();
private MyTimerTask mTask;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Bundle b = intent.getExtras();
if (b != null) {
NetworkInfo netInfo = (NetworkInfo)
b.get(ConnectivityManager.EXTRA_NETWORK_INFO);
String type = netInfo.getTypeName();
NetworkInfo.State state = netInfo.getState();
if (state == NetworkInfo.State.CONNECTED) {
Log.v(TAG, "Connectivity alert: CONNECTED " + type);
onChanged(type, true);
} else if (state == NetworkInfo.State.DISCONNECTED) {
Log.v(TAG, "Connectivity alert: DISCONNECTED " + type);
onChanged(type, false);
} else {
Log.d(TAG, "Connectivity alert not processed: " + state
+ " " + type);
}
}
}
}
private void onChanged(String type, boolean connected) {
synchronized (SipService.this) {
// When turning on WIFI, it needs some time for network
// connectivity to get stabile so we defer good news (because
// we want to skip the interim ones) but deliver bad news
// immediately
if (connected) {
if (mTask != null) mTask.cancel();
mTask = new MyTimerTask(type, connected);
mTimer.schedule(mTask, 3 * 1000L);
// TODO: hold wakup lock so that we can finish change before
// the device goes to sleep
} else {
if ((mTask != null) && mTask.mNetworkType.equals(type)) {
mTask.cancel();
}
onConnectivityChanged(type, false);
}
}
}
private class MyTimerTask extends TimerTask {
private boolean mConnected;
private String mNetworkType;
public MyTimerTask(String type, boolean connected) {
mNetworkType = type;
mConnected = connected;
}
@Override
public void run() {
synchronized (SipService.this) {
if (mTask != this) {
Log.w(TAG, " unexpected task: " + mNetworkType
+ (mConnected ? " CONNECTED" : "DISCONNECTED"));
return;
}
mTask = null;
Log.v(TAG, " deliver change for " + mNetworkType
+ (mConnected ? " CONNECTED" : "DISCONNECTED"));
onConnectivityChanged(mNetworkType, mConnected);
}
}
}
}
// TODO: clean up pending SipSession(s) periodically
/**
* Timer that can schedule events to occur even when the device is in sleep.
* Only used internally in this package.
*/
class WakeupTimer extends BroadcastReceiver {
private static final String TAG = "_SIP.WkTimer_";
private static final String TRIGGER_TIME = "TriggerTime";
private Context mContext;
private AlarmManager mAlarmManager;
// runnable --> time to execute in SystemClock
private TreeSet<MyEvent> mEventQueue =
new TreeSet<MyEvent>(new MyEventComparator());
private PendingIntent mPendingIntent;
public WakeupTimer(Context context) {
mContext = context;
mAlarmManager = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
IntentFilter filter = new IntentFilter(getAction());
context.registerReceiver(this, filter);
}
/**
* Stops the timer. No event can be scheduled after this method is called.
*/
public synchronized void stop() {
mContext.unregisterReceiver(this);
if (mPendingIntent != null) {
mAlarmManager.cancel(mPendingIntent);
mPendingIntent = null;
}
mEventQueue.clear();
mEventQueue = null;
}
private synchronized boolean stopped() {
if (mEventQueue == null) {
Log.w(TAG, "Timer stopped");
return true;
} else {
return false;
}
}
private void cancelAlarm() {
mAlarmManager.cancel(mPendingIntent);
mPendingIntent = null;
}
private void recalculatePeriods() {
if (mEventQueue.isEmpty()) return;
MyEvent firstEvent = mEventQueue.first();
int minPeriod = firstEvent.mMaxPeriod;
long minTriggerTime = firstEvent.mTriggerTime;
for (MyEvent e : mEventQueue) {
e.mPeriod = e.mMaxPeriod / minPeriod * minPeriod;
int interval = (int) (e.mLastTriggerTime + e.mMaxPeriod
- minTriggerTime);
interval = interval / minPeriod * minPeriod;
e.mTriggerTime = minTriggerTime + interval;
}
TreeSet<MyEvent> newQueue = new TreeSet<MyEvent>(
mEventQueue.comparator());
newQueue.addAll((Collection<MyEvent>) mEventQueue);
mEventQueue.clear();
mEventQueue = newQueue;
Log.v(TAG, "queue re-calculated");
printQueue();
}
// Determines the period and the trigger time of the new event and insert it
// to the queue.
private void insertEvent(MyEvent event) {
long now = SystemClock.elapsedRealtime();
if (mEventQueue.isEmpty()) {
event.mTriggerTime = now + event.mPeriod;
mEventQueue.add(event);
return;
}
MyEvent firstEvent = mEventQueue.first();
int minPeriod = firstEvent.mPeriod;
if (minPeriod <= event.mMaxPeriod) {
event.mPeriod = event.mMaxPeriod / minPeriod * minPeriod;
int interval = event.mMaxPeriod;
interval -= (int) (firstEvent.mTriggerTime - now);
interval = interval / minPeriod * minPeriod;
event.mTriggerTime = firstEvent.mTriggerTime + interval;
mEventQueue.add(event);
} else {
long triggerTime = now + event.mPeriod;
if (firstEvent.mTriggerTime < triggerTime) {
event.mTriggerTime = firstEvent.mTriggerTime;
event.mLastTriggerTime -= event.mPeriod;
} else {
event.mTriggerTime = triggerTime;
}
mEventQueue.add(event);
recalculatePeriods();
}
}
/**
* Sets a periodic timer.
*
* @param period the timer period; in milli-second
* @param callback is called back when the timer goes off; the same callback
* can be specified in multiple timer events
*/
public synchronized void set(int period, Runnable callback) {
if (stopped()) return;
long now = SystemClock.elapsedRealtime();
MyEvent event = new MyEvent(period, callback, now);
insertEvent(event);
if (mEventQueue.first() == event) {
if (mEventQueue.size() > 1) cancelAlarm();
scheduleNext();
}
long triggerTime = event.mTriggerTime;
Log.v(TAG, " add event " + event + " scheduled at "
+ showTime(triggerTime) + " at " + showTime(now)
+ ", #events=" + mEventQueue.size());
printQueue();
}
/**
* Cancels all the timer events with the specified callback.
*
* @param callback the callback
*/
public synchronized void cancel(Runnable callback) {
if (stopped() || mEventQueue.isEmpty()) return;
Log.d(TAG, "cancel:" + callback);
MyEvent firstEvent = mEventQueue.first();
for (Iterator<MyEvent> iter = mEventQueue.iterator();
iter.hasNext();) {
MyEvent event = iter.next();
if (event.mCallback == callback) {
iter.remove();
Log.d(TAG, " cancel found:" + event);
}
}
if (mEventQueue.isEmpty()) {
cancelAlarm();
} else if (mEventQueue.first() != firstEvent) {
cancelAlarm();
firstEvent = mEventQueue.first();
firstEvent.mPeriod = firstEvent.mMaxPeriod;
firstEvent.mTriggerTime = firstEvent.mLastTriggerTime
+ firstEvent.mPeriod;
recalculatePeriods();
scheduleNext();
}
Log.d(TAG, "after cancel:");
printQueue();
}
private void scheduleNext() {
if (stopped() || mEventQueue.isEmpty()) return;
if (mPendingIntent != null) {
throw new RuntimeException("pendingIntent is not null!");
}
MyEvent event = mEventQueue.first();
Intent intent = new Intent(getAction());
intent.putExtra(TRIGGER_TIME, event.mTriggerTime);
PendingIntent pendingIntent = mPendingIntent =
PendingIntent.getBroadcast(mContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
event.mTriggerTime, pendingIntent);
}
@Override
public synchronized void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (getAction().equals(action)
&& intent.getExtras().containsKey(TRIGGER_TIME)) {
mPendingIntent = null;
long triggerTime = intent.getLongExtra(TRIGGER_TIME, -1L);
execute(triggerTime);
} else {
Log.d(TAG, "unrecognized intent: " + intent);
}
}
private void printQueue() {
int count = 0;
for (MyEvent event : mEventQueue) {
Log.d(TAG, " " + event + ": scheduled at "
+ showTime(event.mTriggerTime) + ": last at "
+ showTime(event.mLastTriggerTime));
if (++count >= 5) break;
}
if (mEventQueue.size() > count) {
Log.d(TAG, " .....");
} else if (count == 0) {
Log.d(TAG, " <empty>");
}
}
private void execute(long triggerTime) {
Log.d(TAG, "time's up, triggerTime = " + showTime(triggerTime) + ": "
+ mEventQueue.size());
if (stopped() || mEventQueue.isEmpty()) return;
for (MyEvent event : mEventQueue) {
if (event.mTriggerTime != triggerTime) break;
Log.d(TAG, "execute " + event);
event.mLastTriggerTime = event.mTriggerTime;
event.mTriggerTime += event.mPeriod;
// run the callback in a new thread to prevent deadlock
new Thread(event.mCallback).start();
}
Log.d(TAG, "after timeout execution");
printQueue();
scheduleNext();
}
private String getAction() {
return toString();
}
private String showTime(long time) {
int ms = (int) (time % 1000);
int s = (int) (time / 1000);
int m = s / 60;
s %= 60;
return String.format("%d.%d.%d", m, s, ms);
}
}
private static class MyEvent {
int mPeriod;
int mMaxPeriod;
long mTriggerTime;
long mLastTriggerTime;
Runnable mCallback;
MyEvent(int period, Runnable callback, long now) {
mPeriod = mMaxPeriod = period;
mCallback = callback;
mLastTriggerTime = now;
}
@Override
public String toString() {
String s = super.toString();
s = s.substring(s.indexOf("@"));
return s + ":" + (mPeriod / 1000) + ":" + (mMaxPeriod / 1000) + ":"
+ toString(mCallback);
}
private String toString(Object o) {
String s = o.toString();
int index = s.indexOf("$");
if (index > 0) s = s.substring(index + 1);
return s;
}
}
private static class MyEventComparator implements Comparator<MyEvent> {
public int compare(MyEvent e1, MyEvent e2) {
if (e1 == e2) return 0;
int diff = e1.mMaxPeriod - e2.mMaxPeriod;
if (diff == 0) diff = -1;
return diff;
}
public boolean equals(Object that) {
return (this == that);
}
}
}
|