forked from Quarx2k/android_frameworks_base
/
CallManager.java
1849 lines (1626 loc) · 65.2 KB
/
CallManager.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
/*
* 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.internal.telephony;
import com.android.internal.telephony.sip.SipPhone;
import android.content.Context;
import android.media.AudioManager;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.Message;
import android.os.RegistrantList;
import android.os.Registrant;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @hide
*
* CallManager class provides an abstract layer for PhoneApp to access
* and control calls. It implements Phone interface.
*
* CallManager provides call and connection control as well as
* channel capability.
*
* There are three categories of APIs CallManager provided
*
* 1. Call control and operation, such as dial() and hangup()
* 2. Channel capabilities, such as CanConference()
* 3. Register notification
*
*
*/
public final class CallManager {
private static final String LOG_TAG ="CallManager";
private static final boolean DBG = true;
private static final boolean VDBG = false;
private static final int EVENT_DISCONNECT = 100;
private static final int EVENT_PRECISE_CALL_STATE_CHANGED = 101;
private static final int EVENT_NEW_RINGING_CONNECTION = 102;
private static final int EVENT_UNKNOWN_CONNECTION = 103;
private static final int EVENT_INCOMING_RING = 104;
private static final int EVENT_RINGBACK_TONE = 105;
private static final int EVENT_IN_CALL_VOICE_PRIVACY_ON = 106;
private static final int EVENT_IN_CALL_VOICE_PRIVACY_OFF = 107;
private static final int EVENT_CALL_WAITING = 108;
private static final int EVENT_DISPLAY_INFO = 109;
private static final int EVENT_SIGNAL_INFO = 110;
private static final int EVENT_CDMA_OTA_STATUS_CHANGE = 111;
private static final int EVENT_RESEND_INCALL_MUTE = 112;
private static final int EVENT_MMI_INITIATE = 113;
private static final int EVENT_MMI_COMPLETE = 114;
private static final int EVENT_ECM_TIMER_RESET = 115;
private static final int EVENT_SUBSCRIPTION_INFO_READY = 116;
private static final int EVENT_SUPP_SERVICE_FAILED = 117;
private static final int EVENT_SERVICE_STATE_CHANGED = 118;
private static final int EVENT_POST_DIAL_CHARACTER = 119;
// Singleton instance
private static final CallManager INSTANCE = new CallManager();
// list of registered phones, which are PhoneBase objs
private final ArrayList<Phone> mPhones;
// list of supported ringing calls
private final ArrayList<Call> mRingingCalls;
// list of supported background calls
private final ArrayList<Call> mBackgroundCalls;
// list of supported foreground calls
private final ArrayList<Call> mForegroundCalls;
// empty connection list
private final ArrayList<Connection> emptyConnections = new ArrayList<Connection>();
// default phone as the first phone registered, which is PhoneBase obj
private Phone mDefaultPhone;
// state registrants
protected final RegistrantList mPreciseCallStateRegistrants
= new RegistrantList();
protected final RegistrantList mNewRingingConnectionRegistrants
= new RegistrantList();
protected final RegistrantList mIncomingRingRegistrants
= new RegistrantList();
protected final RegistrantList mDisconnectRegistrants
= new RegistrantList();
protected final RegistrantList mMmiRegistrants
= new RegistrantList();
protected final RegistrantList mUnknownConnectionRegistrants
= new RegistrantList();
protected final RegistrantList mRingbackToneRegistrants
= new RegistrantList();
protected final RegistrantList mInCallVoicePrivacyOnRegistrants
= new RegistrantList();
protected final RegistrantList mInCallVoicePrivacyOffRegistrants
= new RegistrantList();
protected final RegistrantList mCallWaitingRegistrants
= new RegistrantList();
protected final RegistrantList mDisplayInfoRegistrants
= new RegistrantList();
protected final RegistrantList mSignalInfoRegistrants
= new RegistrantList();
protected final RegistrantList mCdmaOtaStatusChangeRegistrants
= new RegistrantList();
protected final RegistrantList mResendIncallMuteRegistrants
= new RegistrantList();
protected final RegistrantList mMmiInitiateRegistrants
= new RegistrantList();
protected final RegistrantList mMmiCompleteRegistrants
= new RegistrantList();
protected final RegistrantList mEcmTimerResetRegistrants
= new RegistrantList();
protected final RegistrantList mSubscriptionInfoReadyRegistrants
= new RegistrantList();
protected final RegistrantList mSuppServiceFailedRegistrants
= new RegistrantList();
protected final RegistrantList mServiceStateChangedRegistrants
= new RegistrantList();
protected final RegistrantList mPostDialCharacterRegistrants
= new RegistrantList();
private CallManager() {
mPhones = new ArrayList<Phone>();
mRingingCalls = new ArrayList<Call>();
mBackgroundCalls = new ArrayList<Call>();
mForegroundCalls = new ArrayList<Call>();
mDefaultPhone = null;
}
/**
* get singleton instance of CallManager
* @return CallManager
*/
public static CallManager getInstance() {
return INSTANCE;
}
/**
* Get the corresponding PhoneBase obj
*
* @param phone a Phone object
* @return the corresponding PhoneBase obj in Phone if Phone
* is a PhoneProxy obj
* or the Phone itself if Phone is not a PhoneProxy obj
*/
private static Phone getPhoneBase(Phone phone) {
if (phone instanceof PhoneProxy) {
return phone.getForegroundCall().getPhone();
}
return phone;
}
/**
* Check if two phones refer to the same PhoneBase obj
*
* Note: PhoneBase, not PhoneProxy, is to be used inside of CallManager
*
* Both PhoneBase and PhoneProxy implement Phone interface, so
* they have same phone APIs, such as dial(). The real implementation, for
* example in GSM, is in GSMPhone as extend from PhoneBase, so that
* foregroundCall.getPhone() returns GSMPhone obj. On the other hand,
* PhoneFactory.getDefaultPhone() returns PhoneProxy obj, which has a class
* member of GSMPhone.
*
* So for phone returned by PhoneFacotry, which is used by PhoneApp,
* phone.getForegroundCall().getPhone() != phone
* but
* isSamePhone(phone, phone.getForegroundCall().getPhone()) == true
*
* @param p1 is the first Phone obj
* @param p2 is the second Phone obj
* @return true if p1 and p2 refer to the same phone
*/
public static boolean isSamePhone(Phone p1, Phone p2) {
return (getPhoneBase(p1) == getPhoneBase(p2));
}
/**
* Returns all the registered phone objects.
* @return all the registered phone objects.
*/
public List<Phone> getAllPhones() {
return Collections.unmodifiableList(mPhones);
}
/**
* Get current coarse-grained voice call state.
* If the Call Manager has an active call and call waiting occurs,
* then the phone state is RINGING not OFFHOOK
*
*/
public Phone.State getState() {
Phone.State s = Phone.State.IDLE;
for (Phone phone : mPhones) {
if (phone.getState() == Phone.State.RINGING) {
s = Phone.State.RINGING;
} else if (phone.getState() == Phone.State.OFFHOOK) {
if (s == Phone.State.IDLE) s = Phone.State.OFFHOOK;
}
}
return s;
}
/**
* @return the service state of CallManager, which represents the
* highest priority state of all the service states of phones
*
* The priority is defined as
*
* STATE_IN_SERIVCE > STATE_OUT_OF_SERIVCE > STATE_EMERGENCY > STATE_POWER_OFF
*
*/
public int getServiceState() {
int resultState = ServiceState.STATE_OUT_OF_SERVICE;
for (Phone phone : mPhones) {
int serviceState = phone.getServiceState().getState();
if (serviceState == ServiceState.STATE_IN_SERVICE) {
// IN_SERVICE has the highest priority
resultState = serviceState;
break;
} else if (serviceState == ServiceState.STATE_OUT_OF_SERVICE) {
// OUT_OF_SERVICE replaces EMERGENCY_ONLY and POWER_OFF
// Note: EMERGENCY_ONLY is not in use at this moment
if ( resultState == ServiceState.STATE_EMERGENCY_ONLY ||
resultState == ServiceState.STATE_POWER_OFF) {
resultState = serviceState;
}
} else if (serviceState == ServiceState.STATE_EMERGENCY_ONLY) {
if (resultState == ServiceState.STATE_POWER_OFF) {
resultState = serviceState;
}
}
}
return resultState;
}
/**
* Register phone to CallManager
* @param phone to be registered
* @return true if register successfully
*/
public boolean registerPhone(Phone phone) {
Phone basePhone = getPhoneBase(phone);
if (basePhone != null && !mPhones.contains(basePhone)) {
if (DBG) {
Log.d(LOG_TAG, "registerPhone(" +
phone.getPhoneName() + " " + phone + ")");
}
if (mPhones.isEmpty()) {
mDefaultPhone = basePhone;
}
mPhones.add(basePhone);
mRingingCalls.add(basePhone.getRingingCall());
mBackgroundCalls.add(basePhone.getBackgroundCall());
mForegroundCalls.add(basePhone.getForegroundCall());
registerForPhoneStates(basePhone);
return true;
}
return false;
}
/**
* unregister phone from CallManager
* @param phone to be unregistered
*/
public void unregisterPhone(Phone phone) {
Phone basePhone = getPhoneBase(phone);
if (basePhone != null && mPhones.contains(basePhone)) {
if (DBG) {
Log.d(LOG_TAG, "unregisterPhone(" +
phone.getPhoneName() + " " + phone + ")");
}
mPhones.remove(basePhone);
mRingingCalls.remove(basePhone.getRingingCall());
mBackgroundCalls.remove(basePhone.getBackgroundCall());
mForegroundCalls.remove(basePhone.getForegroundCall());
unregisterForPhoneStates(basePhone);
if (basePhone == mDefaultPhone) {
if (mPhones.isEmpty()) {
mDefaultPhone = null;
} else {
mDefaultPhone = mPhones.get(0);
}
}
}
}
/**
* return the default phone or null if no phone available
*/
public Phone getDefaultPhone() {
return mDefaultPhone;
}
/**
* @return the phone associated with the foreground call
*/
public Phone getFgPhone() {
return getActiveFgCall().getPhone();
}
/**
* @return the phone associated with the background call
*/
public Phone getBgPhone() {
return getFirstActiveBgCall().getPhone();
}
/**
* @return the phone associated with the ringing call
*/
public Phone getRingingPhone() {
return getFirstActiveRingingCall().getPhone();
}
public void setAudioMode() {
Context context = getContext();
if (context == null) return;
AudioManager audioManager = (AudioManager)
context.getSystemService(Context.AUDIO_SERVICE);
int mode = AudioManager.MODE_NORMAL;
switch (getState()) {
case RINGING:
mode = AudioManager.MODE_RINGTONE;
break;
case OFFHOOK:
Phone offhookPhone = getFgPhone();
if (getActiveFgCallState() == Call.State.IDLE) {
// There is no active Fg calls, the OFFHOOK state
// is set by the Bg call. So set the phone to bgPhone.
offhookPhone = getBgPhone();
}
if (offhookPhone instanceof SipPhone) {
// enable IN_COMMUNICATION audio mode for sipPhone
mode = AudioManager.MODE_IN_COMMUNICATION;
} else {
// enable IN_CALL audio mode for telephony
mode = AudioManager.MODE_IN_CALL;
}
break;
}
// Set additional audio parameters needed for incall audio
String[] audioParams = context.getResources().getStringArray(com.android.internal.R.array.config_telephony_set_audioparameters);
for (String parameter : audioParams) {
if (mode == AudioManager.MODE_IN_CALL) {
Log.d(LOG_TAG, "setAudioMode(): " + parameter + "=on");
audioManager.setParameters(parameter + "=on");
} else if (mode == AudioManager.MODE_NORMAL) {
Log.d(LOG_TAG, "setAudioMode(): " + parameter + "=off");
audioManager.setParameters(parameter + "=off");
}
}
// calling audioManager.setMode() multiple times in a short period of
// time seems to break the audio recorder in in-call mode
if (audioManager.getMode() != mode) audioManager.setMode(mode);
}
private Context getContext() {
Phone defaultPhone = getDefaultPhone();
return ((defaultPhone == null) ? null : defaultPhone.getContext());
}
private void registerForPhoneStates(Phone phone) {
// for common events supported by all phones
phone.registerForPreciseCallStateChanged(mHandler, EVENT_PRECISE_CALL_STATE_CHANGED, null);
phone.registerForDisconnect(mHandler, EVENT_DISCONNECT, null);
phone.registerForNewRingingConnection(mHandler, EVENT_NEW_RINGING_CONNECTION, null);
phone.registerForUnknownConnection(mHandler, EVENT_UNKNOWN_CONNECTION, null);
phone.registerForIncomingRing(mHandler, EVENT_INCOMING_RING, null);
phone.registerForRingbackTone(mHandler, EVENT_RINGBACK_TONE, null);
phone.registerForInCallVoicePrivacyOn(mHandler, EVENT_IN_CALL_VOICE_PRIVACY_ON, null);
phone.registerForInCallVoicePrivacyOff(mHandler, EVENT_IN_CALL_VOICE_PRIVACY_OFF, null);
phone.registerForDisplayInfo(mHandler, EVENT_DISPLAY_INFO, null);
phone.registerForSignalInfo(mHandler, EVENT_SIGNAL_INFO, null);
phone.registerForResendIncallMute(mHandler, EVENT_RESEND_INCALL_MUTE, null);
phone.registerForMmiInitiate(mHandler, EVENT_MMI_INITIATE, null);
phone.registerForMmiComplete(mHandler, EVENT_MMI_COMPLETE, null);
phone.registerForSuppServiceFailed(mHandler, EVENT_SUPP_SERVICE_FAILED, null);
phone.registerForServiceStateChanged(mHandler, EVENT_SERVICE_STATE_CHANGED, null);
// for events supported only by GSM and CDMA phone
if (phone.getPhoneType() == Phone.PHONE_TYPE_GSM ||
phone.getPhoneType() == Phone.PHONE_TYPE_CDMA) {
phone.setOnPostDialCharacter(mHandler, EVENT_POST_DIAL_CHARACTER, null);
}
// for events supported only by CDMA phone
if (phone.getPhoneType() == Phone.PHONE_TYPE_CDMA ){
phone.registerForCdmaOtaStatusChange(mHandler, EVENT_CDMA_OTA_STATUS_CHANGE, null);
phone.registerForSubscriptionInfoReady(mHandler, EVENT_SUBSCRIPTION_INFO_READY, null);
phone.registerForCallWaiting(mHandler, EVENT_CALL_WAITING, null);
phone.registerForEcmTimerReset(mHandler, EVENT_ECM_TIMER_RESET, null);
}
}
private void unregisterForPhoneStates(Phone phone) {
// for common events supported by all phones
phone.unregisterForPreciseCallStateChanged(mHandler);
phone.unregisterForDisconnect(mHandler);
phone.unregisterForNewRingingConnection(mHandler);
phone.unregisterForUnknownConnection(mHandler);
phone.unregisterForIncomingRing(mHandler);
phone.unregisterForRingbackTone(mHandler);
phone.unregisterForInCallVoicePrivacyOn(mHandler);
phone.unregisterForInCallVoicePrivacyOff(mHandler);
phone.unregisterForDisplayInfo(mHandler);
phone.unregisterForSignalInfo(mHandler);
phone.unregisterForResendIncallMute(mHandler);
phone.unregisterForMmiInitiate(mHandler);
phone.unregisterForMmiComplete(mHandler);
phone.unregisterForSuppServiceFailed(mHandler);
phone.unregisterForServiceStateChanged(mHandler);
// for events supported only by GSM and CDMA phone
if (phone.getPhoneType() == Phone.PHONE_TYPE_GSM ||
phone.getPhoneType() == Phone.PHONE_TYPE_CDMA) {
phone.setOnPostDialCharacter(null, EVENT_POST_DIAL_CHARACTER, null);
}
// for events supported only by CDMA phone
if (phone.getPhoneType() == Phone.PHONE_TYPE_CDMA ){
phone.unregisterForCdmaOtaStatusChange(mHandler);
phone.unregisterForSubscriptionInfoReady(mHandler);
phone.unregisterForCallWaiting(mHandler);
phone.unregisterForEcmTimerReset(mHandler);
}
}
/**
* Answers a ringing or waiting call.
*
* Active call, if any, go on hold.
* If active call can't be held, i.e., a background call of the same channel exists,
* the active call will be hang up.
*
* Answering occurs asynchronously, and final notification occurs via
* {@link #registerForPreciseCallStateChanged(android.os.Handler, int,
* java.lang.Object) registerForPreciseCallStateChanged()}.
*
* @exception CallStateException when call is not ringing or waiting
*/
public void acceptCall(Call ringingCall) throws CallStateException {
Phone ringingPhone = ringingCall.getPhone();
if (VDBG) {
Log.d(LOG_TAG, "acceptCall(" +ringingCall + " from " + ringingCall.getPhone() + ")");
Log.d(LOG_TAG, this.toString());
}
if ( hasActiveFgCall() ) {
Phone activePhone = getActiveFgCall().getPhone();
boolean hasBgCall = ! (activePhone.getBackgroundCall().isIdle());
boolean sameChannel = (activePhone == ringingPhone);
if (VDBG) {
Log.d(LOG_TAG, "hasBgCall: "+ hasBgCall + "sameChannel:" + sameChannel);
}
if (sameChannel && hasBgCall) {
getActiveFgCall().hangup();
} else if (!sameChannel && !hasBgCall) {
activePhone.switchHoldingAndActive();
} else if (!sameChannel && hasBgCall) {
getActiveFgCall().hangup();
}
}
ringingPhone.acceptCall();
if (VDBG) {
Log.d(LOG_TAG, "End acceptCall(" +ringingCall + ")");
Log.d(LOG_TAG, this.toString());
}
}
/**
* Reject (ignore) a ringing call. In GSM, this means UDUB
* (User Determined User Busy). Reject occurs asynchronously,
* and final notification occurs via
* {@link #registerForPreciseCallStateChanged(android.os.Handler, int,
* java.lang.Object) registerForPreciseCallStateChanged()}.
*
* @exception CallStateException when no call is ringing or waiting
*/
public void rejectCall(Call ringingCall) throws CallStateException {
if (VDBG) {
Log.d(LOG_TAG, "rejectCall(" +ringingCall + ")");
Log.d(LOG_TAG, this.toString());
}
Phone ringingPhone = ringingCall.getPhone();
ringingPhone.rejectCall();
if (VDBG) {
Log.d(LOG_TAG, "End rejectCall(" +ringingCall + ")");
Log.d(LOG_TAG, this.toString());
}
}
/**
* Places active call on hold, and makes held call active.
* Switch occurs asynchronously and may fail.
*
* There are 4 scenarios
* 1. only active call but no held call, aka, hold
* 2. no active call but only held call, aka, unhold
* 3. both active and held calls from same phone, aka, swap
* 4. active and held calls from different phones, aka, phone swap
*
* Final notification occurs via
* {@link #registerForPreciseCallStateChanged(android.os.Handler, int,
* java.lang.Object) registerForPreciseCallStateChanged()}.
*
* @exception CallStateException if active call is ringing, waiting, or
* dialing/alerting, or heldCall can't be active.
* In these cases, this operation may not be performed.
*/
public void switchHoldingAndActive(Call heldCall) throws CallStateException {
Phone activePhone = null;
Phone heldPhone = null;
if (VDBG) {
Log.d(LOG_TAG, "switchHoldingAndActive(" +heldCall + ")");
Log.d(LOG_TAG, this.toString());
}
if (hasActiveFgCall()) {
activePhone = getActiveFgCall().getPhone();
}
if (heldCall != null) {
heldPhone = heldCall.getPhone();
}
if (activePhone != null) {
activePhone.switchHoldingAndActive();
}
if (heldPhone != null && heldPhone != activePhone) {
heldPhone.switchHoldingAndActive();
}
if (VDBG) {
Log.d(LOG_TAG, "End switchHoldingAndActive(" +heldCall + ")");
Log.d(LOG_TAG, this.toString());
}
}
/**
* Hangup foreground call and resume the specific background call
*
* Note: this is noop if there is no foreground call or the heldCall is null
*
* @param heldCall to become foreground
* @throws CallStateException
*/
public void hangupForegroundResumeBackground(Call heldCall) throws CallStateException {
Phone foregroundPhone = null;
Phone backgroundPhone = null;
if (VDBG) {
Log.d(LOG_TAG, "hangupForegroundResumeBackground(" +heldCall + ")");
Log.d(LOG_TAG, this.toString());
}
if (hasActiveFgCall()) {
foregroundPhone = getFgPhone();
if (heldCall != null) {
backgroundPhone = heldCall.getPhone();
if (foregroundPhone == backgroundPhone) {
getActiveFgCall().hangup();
} else {
// the call to be hangup and resumed belongs to different phones
getActiveFgCall().hangup();
switchHoldingAndActive(heldCall);
}
}
}
if (VDBG) {
Log.d(LOG_TAG, "End hangupForegroundResumeBackground(" +heldCall + ")");
Log.d(LOG_TAG, this.toString());
}
}
/**
* Whether or not the phone can conference in the current phone
* state--that is, one call holding and one call active.
* @return true if the phone can conference; false otherwise.
*/
public boolean canConference(Call heldCall) {
Phone activePhone = null;
Phone heldPhone = null;
if (hasActiveFgCall()) {
activePhone = getActiveFgCall().getPhone();
}
if (heldCall != null) {
heldPhone = heldCall.getPhone();
}
return heldPhone.getClass().equals(activePhone.getClass());
}
/**
* Conferences holding and active. Conference occurs asynchronously
* and may fail. Final notification occurs via
* {@link #registerForPreciseCallStateChanged(android.os.Handler, int,
* java.lang.Object) registerForPreciseCallStateChanged()}.
*
* @exception CallStateException if canConference() would return false.
* In these cases, this operation may not be performed.
*/
public void conference(Call heldCall) throws CallStateException {
if (VDBG) {
Log.d(LOG_TAG, "conference(" +heldCall + ")");
Log.d(LOG_TAG, this.toString());
}
Phone fgPhone = getFgPhone();
if (fgPhone instanceof SipPhone) {
((SipPhone) fgPhone).conference(heldCall);
} else if (canConference(heldCall)) {
fgPhone.conference();
} else {
throw(new CallStateException("Can't conference foreground and selected background call"));
}
if (VDBG) {
Log.d(LOG_TAG, "End conference(" +heldCall + ")");
Log.d(LOG_TAG, this.toString());
}
}
/**
* Initiate a new voice connection. This happens asynchronously, so you
* cannot assume the audio path is connected (or a call index has been
* assigned) until PhoneStateChanged notification has occurred.
*
* @exception CallStateException if a new outgoing call is not currently
* possible because no more call slots exist or a call exists that is
* dialing, alerting, ringing, or waiting. Other errors are
* handled asynchronously.
*/
public Connection dial(Phone phone, String dialString) throws CallStateException {
Phone basePhone = getPhoneBase(phone);
Connection result;
if (VDBG) {
Log.d(LOG_TAG, " dial(" + basePhone + ", "+ dialString + ")");
Log.d(LOG_TAG, this.toString());
}
if (!canDial(phone)) {
throw new CallStateException("cannot dial in current state");
}
if ( hasActiveFgCall() ) {
Phone activePhone = getActiveFgCall().getPhone();
boolean hasBgCall = !(activePhone.getBackgroundCall().isIdle());
if (DBG) {
Log.d(LOG_TAG, "hasBgCall: "+ hasBgCall + " sameChannel:" + (activePhone == basePhone));
}
if (activePhone != basePhone) {
if (hasBgCall) {
Log.d(LOG_TAG, "Hangup");
getActiveFgCall().hangup();
} else {
Log.d(LOG_TAG, "Switch");
activePhone.switchHoldingAndActive();
}
}
}
result = basePhone.dial(dialString);
if (VDBG) {
Log.d(LOG_TAG, "End dial(" + basePhone + ", "+ dialString + ")");
Log.d(LOG_TAG, this.toString());
}
return result;
}
/**
* Initiate a new voice connection. This happens asynchronously, so you
* cannot assume the audio path is connected (or a call index has been
* assigned) until PhoneStateChanged notification has occurred.
*
* @exception CallStateException if a new outgoing call is not currently
* possible because no more call slots exist or a call exists that is
* dialing, alerting, ringing, or waiting. Other errors are
* handled asynchronously.
*/
public Connection dial(Phone phone, String dialString, UUSInfo uusInfo) throws CallStateException {
return phone.dial(dialString, uusInfo);
}
/**
* clear disconnect connection for each phone
*/
public void clearDisconnected() {
for(Phone phone : mPhones) {
phone.clearDisconnected();
}
}
/**
* Phone can make a call only if ALL of the following are true:
* - Phone is not powered off
* - There's no incoming or waiting call
* - There's available call slot in either foreground or background
* - The foreground call is ACTIVE or IDLE or DISCONNECTED.
* (We mainly need to make sure it *isn't* DIALING or ALERTING.)
* @param phone
* @return true if the phone can make a new call
*/
private boolean canDial(Phone phone) {
int serviceState = phone.getServiceState().getState();
boolean hasRingingCall = hasActiveRingingCall();
boolean hasActiveCall = hasActiveFgCall();
boolean hasHoldingCall = hasActiveBgCall();
boolean allLinesTaken = hasActiveCall && hasHoldingCall;
Call.State fgCallState = getActiveFgCallState();
boolean result = (serviceState != ServiceState.STATE_POWER_OFF
&& !hasRingingCall
&& !allLinesTaken
&& ((fgCallState == Call.State.ACTIVE)
|| (fgCallState == Call.State.IDLE)
|| (fgCallState == Call.State.DISCONNECTED)));
if (result == false) {
Log.d(LOG_TAG, "canDial serviceState=" + serviceState
+ " hasRingingCall=" + hasRingingCall
+ " hasActiveCall=" + hasActiveCall
+ " hasHoldingCall=" + hasHoldingCall
+ " allLinesTaken=" + allLinesTaken
+ " fgCallState=" + fgCallState);
}
return result;
}
/**
* Whether or not the phone can do explicit call transfer in the current
* phone state--that is, one call holding and one call active.
* @return true if the phone can do explicit call transfer; false otherwise.
*/
public boolean canTransfer(Call heldCall) {
Phone activePhone = null;
Phone heldPhone = null;
if (hasActiveFgCall()) {
activePhone = getActiveFgCall().getPhone();
}
if (heldCall != null) {
heldPhone = heldCall.getPhone();
}
return (heldPhone == activePhone && activePhone.canTransfer());
}
/**
* Connects the held call and active call
* Disconnects the subscriber from both calls
*
* Explicit Call Transfer occurs asynchronously
* and may fail. Final notification occurs via
* {@link #registerForPreciseCallStateChanged(android.os.Handler, int,
* java.lang.Object) registerForPreciseCallStateChanged()}.
*
* @exception CallStateException if canTransfer() would return false.
* In these cases, this operation may not be performed.
*/
public void explicitCallTransfer(Call heldCall) throws CallStateException {
if (VDBG) {
Log.d(LOG_TAG, " explicitCallTransfer(" + heldCall + ")");
Log.d(LOG_TAG, this.toString());
}
if (canTransfer(heldCall)) {
heldCall.getPhone().explicitCallTransfer();
}
if (VDBG) {
Log.d(LOG_TAG, "End explicitCallTransfer(" + heldCall + ")");
Log.d(LOG_TAG, this.toString());
}
}
/**
* Returns a list of MMI codes that are pending for a phone. (They have initiated
* but have not yet completed).
* Presently there is only ever one.
*
* Use <code>registerForMmiInitiate</code>
* and <code>registerForMmiComplete</code> for change notification.
* @return null if phone doesn't have or support mmi code
*/
public List<? extends MmiCode> getPendingMmiCodes(Phone phone) {
Log.e(LOG_TAG, "getPendingMmiCodes not implemented");
return null;
}
/**
* Sends user response to a USSD REQUEST message. An MmiCode instance
* representing this response is sent to handlers registered with
* registerForMmiInitiate.
*
* @param ussdMessge Message to send in the response.
* @return false if phone doesn't support ussd service
*/
public boolean sendUssdResponse(Phone phone, String ussdMessge) {
Log.e(LOG_TAG, "sendUssdResponse not implemented");
return false;
}
/**
* Mutes or unmutes the microphone for the active call. The microphone
* is automatically unmuted if a call is answered, dialed, or resumed
* from a holding state.
*
* @param muted true to mute the microphone,
* false to activate the microphone.
*/
public void setMute(boolean muted) {
if (VDBG) {
Log.d(LOG_TAG, " setMute(" + muted + ")");
Log.d(LOG_TAG, this.toString());
}
if (hasActiveFgCall()) {
getActiveFgCall().getPhone().setMute(muted);
}
if (VDBG) {
Log.d(LOG_TAG, "End setMute(" + muted + ")");
Log.d(LOG_TAG, this.toString());
}
}
/**
* Gets current mute status. Use
* {@link #registerForPreciseCallStateChanged(android.os.Handler, int,
* java.lang.Object) registerForPreciseCallStateChanged()}
* as a change notifcation, although presently phone state changed is not
* fired when setMute() is called.
*
* @return true is muting, false is unmuting
*/
public boolean getMute() {
if (hasActiveFgCall()) {
return getActiveFgCall().getPhone().getMute();
} else if (hasActiveBgCall()) {
return getFirstActiveBgCall().getPhone().getMute();
}
return false;
}
/**
* Enables or disables echo suppression.
*/
public void setEchoSuppressionEnabled(boolean enabled) {
if (VDBG) {
Log.d(LOG_TAG, " setEchoSuppression(" + enabled + ")");
Log.d(LOG_TAG, this.toString());
}
if (hasActiveFgCall()) {
getActiveFgCall().getPhone().setEchoSuppressionEnabled(enabled);
}
if (VDBG) {
Log.d(LOG_TAG, "End setEchoSuppression(" + enabled + ")");
Log.d(LOG_TAG, this.toString());
}
}
/**
* Play a DTMF tone on the active call.
*
* @param c should be one of 0-9, '*' or '#'. Other values will be
* silently ignored.
* @return false if no active call or the active call doesn't support
* dtmf tone
*/
public boolean sendDtmf(char c) {
boolean result = false;
if (VDBG) {
Log.d(LOG_TAG, " sendDtmf(" + c + ")");
Log.d(LOG_TAG, this.toString());
}
if (hasActiveFgCall()) {
getActiveFgCall().getPhone().sendDtmf(c);
result = true;
}
if (VDBG) {
Log.d(LOG_TAG, "End sendDtmf(" + c + ")");
Log.d(LOG_TAG, this.toString());
}
return result;
}
/**
* Start to paly a DTMF tone on the active call.
* or there is a playing DTMF tone.
* @param c should be one of 0-9, '*' or '#'. Other values will be
* silently ignored.
*
* @return false if no active call or the active call doesn't support
* dtmf tone
*/
public boolean startDtmf(char c) {
boolean result = false;
if (VDBG) {
Log.d(LOG_TAG, " startDtmf(" + c + ")");
Log.d(LOG_TAG, this.toString());
}
if (hasActiveFgCall()) {
getActiveFgCall().getPhone().startDtmf(c);
result = true;
}
if (VDBG) {
Log.d(LOG_TAG, "End startDtmf(" + c + ")");
Log.d(LOG_TAG, this.toString());
}