• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.telephony;
18 import android.content.Context;
19 import android.os.AsyncResult;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.os.Message;
23 import android.os.PersistableBundle;
24 import android.os.PowerManager;
25 import android.os.Registrant;
26 import android.os.SystemClock;
27 import android.telephony.CarrierConfigManager;
28 import android.telephony.DisconnectCause;
29 import android.telephony.Rlog;
30 import android.telephony.PhoneNumberUtils;
31 import android.telephony.ServiceState;
32 import android.text.TextUtils;
33 
34 import com.android.internal.telephony.cdma.CdmaCallWaitingNotification;
35 import com.android.internal.telephony.cdma.CdmaSubscriptionSourceManager;
36 import com.android.internal.telephony.uicc.UiccCardApplication;
37 import com.android.internal.telephony.uicc.UiccController;
38 import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppState;
39 
40 /**
41  * {@hide}
42  */
43 public class GsmCdmaConnection extends Connection {
44     private static final String LOG_TAG = "GsmCdmaConnection";
45     private static final boolean DBG = true;
46     private static final boolean VDBG = false;
47 
48     //***** Instance Variables
49 
50     GsmCdmaCallTracker mOwner;
51     GsmCdmaCall mParent;
52 
53     boolean mDisconnected;
54 
55     int mIndex;          // index in GsmCdmaCallTracker.connections[], -1 if unassigned
56                         // The GsmCdma index is 1 + this
57 
58     /*
59      * These time/timespan values are based on System.currentTimeMillis(),
60      * i.e., "wall clock" time.
61      */
62     long mDisconnectTime;
63 
64     UUSInfo mUusInfo;
65     int mPreciseCause = 0;
66     String mVendorCause;
67 
68     Connection mOrigConnection;
69 
70     Handler mHandler;
71 
72     private PowerManager.WakeLock mPartialWakeLock;
73 
74     private boolean mIsEmergencyCall = false;
75 
76     // The cached delay to be used between DTMF tones fetched from carrier config.
77     private int mDtmfToneDelay = 0;
78 
79     //***** Event Constants
80     static final int EVENT_DTMF_DONE = 1;
81     static final int EVENT_PAUSE_DONE = 2;
82     static final int EVENT_NEXT_POST_DIAL = 3;
83     static final int EVENT_WAKE_LOCK_TIMEOUT = 4;
84     static final int EVENT_DTMF_DELAY_DONE = 5;
85 
86     //***** Constants
87     static final int PAUSE_DELAY_MILLIS_GSM = 3 * 1000;
88     static final int PAUSE_DELAY_MILLIS_CDMA = 2 * 1000;
89     static final int WAKE_LOCK_TIMEOUT_MILLIS = 60*1000;
90 
91     //***** Inner Classes
92 
93     class MyHandler extends Handler {
MyHandler(Looper l)94         MyHandler(Looper l) {super(l);}
95 
96         @Override
97         public void
handleMessage(Message msg)98         handleMessage(Message msg) {
99 
100             switch (msg.what) {
101                 case EVENT_NEXT_POST_DIAL:
102                 case EVENT_DTMF_DELAY_DONE:
103                 case EVENT_PAUSE_DONE:
104                     processNextPostDialChar();
105                     break;
106                 case EVENT_WAKE_LOCK_TIMEOUT:
107                     releaseWakeLock();
108                     break;
109                 case EVENT_DTMF_DONE:
110                     // We may need to add a delay specified by carrier between DTMF tones that are
111                     // sent out.
112                     mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_DTMF_DELAY_DONE),
113                             mDtmfToneDelay);
114                     break;
115             }
116         }
117     }
118 
119     //***** Constructors
120 
121     /** This is probably an MT call that we first saw in a CLCC response or a hand over. */
GsmCdmaConnection(GsmCdmaPhone phone, DriverCall dc, GsmCdmaCallTracker ct, int index)122     public GsmCdmaConnection (GsmCdmaPhone phone, DriverCall dc, GsmCdmaCallTracker ct, int index) {
123         super(phone.getPhoneType());
124         createWakeLock(phone.getContext());
125         acquireWakeLock();
126 
127         mOwner = ct;
128         mHandler = new MyHandler(mOwner.getLooper());
129 
130         mAddress = dc.number;
131         mIsEmergencyCall = PhoneNumberUtils.isLocalEmergencyNumber(phone.getContext(), mAddress);
132         mIsIncoming = dc.isMT;
133         mCreateTime = System.currentTimeMillis();
134         mCnapName = dc.name;
135         mCnapNamePresentation = dc.namePresentation;
136         mNumberPresentation = dc.numberPresentation;
137         mUusInfo = dc.uusInfo;
138 
139         mIndex = index;
140 
141         mParent = parentFromDCState(dc.state);
142         mParent.attach(this, dc);
143 
144         fetchDtmfToneDelay(phone);
145     }
146 
147     /** This is an MO call, created when dialing */
GsmCdmaConnection(GsmCdmaPhone phone, String dialString, GsmCdmaCallTracker ct, GsmCdmaCall parent, boolean isEmergencyCall)148     public GsmCdmaConnection (GsmCdmaPhone phone, String dialString, GsmCdmaCallTracker ct,
149                               GsmCdmaCall parent, boolean isEmergencyCall) {
150         super(phone.getPhoneType());
151         createWakeLock(phone.getContext());
152         acquireWakeLock();
153 
154         mOwner = ct;
155         mHandler = new MyHandler(mOwner.getLooper());
156 
157         if (isPhoneTypeGsm()) {
158             mDialString = dialString;
159         } else {
160             Rlog.d(LOG_TAG, "[GsmCdmaConn] GsmCdmaConnection: dialString=" +
161                     maskDialString(dialString));
162             dialString = formatDialString(dialString);
163             Rlog.d(LOG_TAG,
164                     "[GsmCdmaConn] GsmCdmaConnection:formated dialString=" +
165                             maskDialString(dialString));
166         }
167 
168         mAddress = PhoneNumberUtils.extractNetworkPortionAlt(dialString);
169         mIsEmergencyCall = isEmergencyCall;
170         mPostDialString = PhoneNumberUtils.extractPostDialPortion(dialString);
171 
172         mIndex = -1;
173 
174         mIsIncoming = false;
175         mCnapName = null;
176         mCnapNamePresentation = PhoneConstants.PRESENTATION_ALLOWED;
177         mNumberPresentation = PhoneConstants.PRESENTATION_ALLOWED;
178         mCreateTime = System.currentTimeMillis();
179 
180         if (parent != null) {
181             mParent = parent;
182             if (isPhoneTypeGsm()) {
183                 parent.attachFake(this, GsmCdmaCall.State.DIALING);
184             } else {
185                 //for the three way call case, not change parent state
186                 if (parent.mState == GsmCdmaCall.State.ACTIVE) {
187                     parent.attachFake(this, GsmCdmaCall.State.ACTIVE);
188                 } else {
189                     parent.attachFake(this, GsmCdmaCall.State.DIALING);
190                 }
191 
192             }
193         }
194 
195         fetchDtmfToneDelay(phone);
196     }
197 
198     //CDMA
199     /** This is a Call waiting call*/
GsmCdmaConnection(Context context, CdmaCallWaitingNotification cw, GsmCdmaCallTracker ct, GsmCdmaCall parent)200     public GsmCdmaConnection(Context context, CdmaCallWaitingNotification cw, GsmCdmaCallTracker ct,
201                              GsmCdmaCall parent) {
202         super(parent.getPhone().getPhoneType());
203         createWakeLock(context);
204         acquireWakeLock();
205 
206         mOwner = ct;
207         mHandler = new MyHandler(mOwner.getLooper());
208         mAddress = cw.number;
209         mNumberPresentation = cw.numberPresentation;
210         mCnapName = cw.name;
211         mCnapNamePresentation = cw.namePresentation;
212         mIndex = -1;
213         mIsIncoming = true;
214         mCreateTime = System.currentTimeMillis();
215         mConnectTime = 0;
216         mParent = parent;
217         parent.attachFake(this, GsmCdmaCall.State.WAITING);
218     }
219 
220 
dispose()221     public void dispose() {
222         clearPostDialListeners();
223         if (mParent != null) {
224             mParent.detach(this);
225         }
226         releaseAllWakeLocks();
227     }
228 
229     static boolean
equalsHandlesNulls(Object a, Object b)230     equalsHandlesNulls (Object a, Object b) {
231         return (a == null) ? (b == null) : a.equals (b);
232     }
233 
234     //CDMA
235     /**
236      * format original dial string
237      * 1) convert international dialing prefix "+" to
238      *    string specified per region
239      *
240      * 2) handle corner cases for PAUSE/WAIT dialing:
241      *
242      *    If PAUSE/WAIT sequence at the end, ignore them.
243      *
244      *    If consecutive PAUSE/WAIT sequence in the middle of the string,
245      *    and if there is any WAIT in PAUSE/WAIT sequence, treat them like WAIT.
246      */
formatDialString(String phoneNumber)247     public static String formatDialString(String phoneNumber) {
248         /**
249          * TODO(cleanup): This function should move to PhoneNumberUtils, and
250          * tests should be added.
251          */
252 
253         if (phoneNumber == null) {
254             return null;
255         }
256         int length = phoneNumber.length();
257         StringBuilder ret = new StringBuilder();
258         char c;
259         int currIndex = 0;
260 
261         while (currIndex < length) {
262             c = phoneNumber.charAt(currIndex);
263             if (isPause(c) || isWait(c)) {
264                 if (currIndex < length - 1) {
265                     // if PW not at the end
266                     int nextIndex = findNextPCharOrNonPOrNonWCharIndex(phoneNumber, currIndex);
267                     // If there is non PW char following PW sequence
268                     if (nextIndex < length) {
269                         char pC = findPOrWCharToAppend(phoneNumber, currIndex, nextIndex);
270                         ret.append(pC);
271                         // If PW char sequence has more than 2 PW characters,
272                         // skip to the last PW character since the sequence already be
273                         // converted to WAIT character
274                         if (nextIndex > (currIndex + 1)) {
275                             currIndex = nextIndex - 1;
276                         }
277                     } else if (nextIndex == length) {
278                         // It means PW characters at the end, ignore
279                         currIndex = length - 1;
280                     }
281                 }
282             } else {
283                 ret.append(c);
284             }
285             currIndex++;
286         }
287         return PhoneNumberUtils.cdmaCheckAndProcessPlusCode(ret.toString());
288     }
289 
290     /*package*/ boolean
compareTo(DriverCall c)291     compareTo(DriverCall c) {
292         // On mobile originated (MO) calls, the phone number may have changed
293         // due to a SIM Toolkit call control modification.
294         //
295         // We assume we know when MO calls are created (since we created them)
296         // and therefore don't need to compare the phone number anyway.
297         if (! (mIsIncoming || c.isMT)) return true;
298 
299         // A new call appearing by SRVCC may have invalid number
300         //  if IMS service is not tightly coupled with cellular modem stack.
301         // Thus we prefer the preexisting handover connection instance.
302         if (isPhoneTypeGsm() && mOrigConnection != null) return true;
303 
304         // ... but we can compare phone numbers on MT calls, and we have
305         // no control over when they begin, so we might as well
306 
307         String cAddress = PhoneNumberUtils.stringFromStringAndTOA(c.number, c.TOA);
308         return mIsIncoming == c.isMT && equalsHandlesNulls(mAddress, cAddress);
309     }
310 
311     @Override
getOrigDialString()312     public String getOrigDialString(){
313         return mDialString;
314     }
315 
316     @Override
getCall()317     public GsmCdmaCall getCall() {
318         return mParent;
319     }
320 
321     @Override
getDisconnectTime()322     public long getDisconnectTime() {
323         return mDisconnectTime;
324     }
325 
326     @Override
getHoldDurationMillis()327     public long getHoldDurationMillis() {
328         if (getState() != GsmCdmaCall.State.HOLDING) {
329             // If not holding, return 0
330             return 0;
331         } else {
332             return SystemClock.elapsedRealtime() - mHoldingStartTime;
333         }
334     }
335 
336     @Override
getState()337     public GsmCdmaCall.State getState() {
338         if (mDisconnected) {
339             return GsmCdmaCall.State.DISCONNECTED;
340         } else {
341             return super.getState();
342         }
343     }
344 
345     @Override
hangup()346     public void hangup() throws CallStateException {
347         if (!mDisconnected) {
348             mOwner.hangup(this);
349         } else {
350             throw new CallStateException ("disconnected");
351         }
352     }
353 
354     @Override
separate()355     public void separate() throws CallStateException {
356         if (!mDisconnected) {
357             mOwner.separate(this);
358         } else {
359             throw new CallStateException ("disconnected");
360         }
361     }
362 
363     @Override
proceedAfterWaitChar()364     public void proceedAfterWaitChar() {
365         if (mPostDialState != PostDialState.WAIT) {
366             Rlog.w(LOG_TAG, "GsmCdmaConnection.proceedAfterWaitChar(): Expected "
367                     + "getPostDialState() to be WAIT but was " + mPostDialState);
368             return;
369         }
370 
371         setPostDialState(PostDialState.STARTED);
372 
373         processNextPostDialChar();
374     }
375 
376     @Override
proceedAfterWildChar(String str)377     public void proceedAfterWildChar(String str) {
378         if (mPostDialState != PostDialState.WILD) {
379             Rlog.w(LOG_TAG, "GsmCdmaConnection.proceedAfterWaitChar(): Expected "
380                 + "getPostDialState() to be WILD but was " + mPostDialState);
381             return;
382         }
383 
384         setPostDialState(PostDialState.STARTED);
385 
386         // make a new postDialString, with the wild char replacement string
387         // at the beginning, followed by the remaining postDialString.
388 
389         StringBuilder buf = new StringBuilder(str);
390         buf.append(mPostDialString.substring(mNextPostDialChar));
391         mPostDialString = buf.toString();
392         mNextPostDialChar = 0;
393         if (Phone.DEBUG_PHONE) {
394             log("proceedAfterWildChar: new postDialString is " +
395                     mPostDialString);
396         }
397 
398         processNextPostDialChar();
399     }
400 
401     @Override
cancelPostDial()402     public void cancelPostDial() {
403         setPostDialState(PostDialState.CANCELLED);
404     }
405 
406     /**
407      * Called when this Connection is being hung up locally (eg, user pressed "end")
408      * Note that at this point, the hangup request has been dispatched to the radio
409      * but no response has yet been received so update() has not yet been called
410      */
411     void
onHangupLocal()412     onHangupLocal() {
413         mCause = DisconnectCause.LOCAL;
414         mPreciseCause = 0;
415         mVendorCause = null;
416     }
417 
418     /**
419      * Maps RIL call disconnect code to {@link DisconnectCause}.
420      * @param causeCode RIL disconnect code
421      * @return the corresponding value from {@link DisconnectCause}
422      */
disconnectCauseFromCode(int causeCode)423     int disconnectCauseFromCode(int causeCode) {
424         /**
425          * See 22.001 Annex F.4 for mapping of cause codes
426          * to local tones
427          */
428 
429         switch (causeCode) {
430             case CallFailCause.USER_BUSY:
431                 return DisconnectCause.BUSY;
432 
433             case CallFailCause.NO_CIRCUIT_AVAIL:
434             case CallFailCause.TEMPORARY_FAILURE:
435             case CallFailCause.SWITCHING_CONGESTION:
436             case CallFailCause.CHANNEL_NOT_AVAIL:
437             case CallFailCause.QOS_NOT_AVAIL:
438             case CallFailCause.BEARER_NOT_AVAIL:
439                 return DisconnectCause.CONGESTION;
440 
441             case CallFailCause.ACM_LIMIT_EXCEEDED:
442                 return DisconnectCause.LIMIT_EXCEEDED;
443 
444             case CallFailCause.OPERATOR_DETERMINED_BARRING:
445             case CallFailCause.CALL_BARRED:
446                 return DisconnectCause.CALL_BARRED;
447 
448             case CallFailCause.FDN_BLOCKED:
449                 return DisconnectCause.FDN_BLOCKED;
450 
451             case CallFailCause.UNOBTAINABLE_NUMBER:
452                 return DisconnectCause.UNOBTAINABLE_NUMBER;
453 
454             case CallFailCause.DIAL_MODIFIED_TO_USSD:
455                 return DisconnectCause.DIAL_MODIFIED_TO_USSD;
456 
457             case CallFailCause.DIAL_MODIFIED_TO_SS:
458                 return DisconnectCause.DIAL_MODIFIED_TO_SS;
459 
460             case CallFailCause.DIAL_MODIFIED_TO_DIAL:
461                 return DisconnectCause.DIAL_MODIFIED_TO_DIAL;
462 
463             case CallFailCause.CDMA_LOCKED_UNTIL_POWER_CYCLE:
464                 return DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE;
465 
466             case CallFailCause.CDMA_DROP:
467                 return DisconnectCause.CDMA_DROP;
468 
469             case CallFailCause.CDMA_INTERCEPT:
470                 return DisconnectCause.CDMA_INTERCEPT;
471 
472             case CallFailCause.CDMA_REORDER:
473                 return DisconnectCause.CDMA_REORDER;
474 
475             case CallFailCause.CDMA_SO_REJECT:
476                 return DisconnectCause.CDMA_SO_REJECT;
477 
478             case CallFailCause.CDMA_RETRY_ORDER:
479                 return DisconnectCause.CDMA_RETRY_ORDER;
480 
481             case CallFailCause.CDMA_ACCESS_FAILURE:
482                 return DisconnectCause.CDMA_ACCESS_FAILURE;
483 
484             case CallFailCause.CDMA_PREEMPTED:
485                 return DisconnectCause.CDMA_PREEMPTED;
486 
487             case CallFailCause.CDMA_NOT_EMERGENCY:
488                 return DisconnectCause.CDMA_NOT_EMERGENCY;
489 
490             case CallFailCause.CDMA_ACCESS_BLOCKED:
491                 return DisconnectCause.CDMA_ACCESS_BLOCKED;
492 
493             case CallFailCause.ERROR_UNSPECIFIED:
494             case CallFailCause.NORMAL_CLEARING:
495             default:
496                 GsmCdmaPhone phone = mOwner.getPhone();
497                 int serviceState = phone.getServiceState().getState();
498                 UiccCardApplication cardApp = phone.getUiccCardApplication();
499                 AppState uiccAppState = (cardApp != null) ? cardApp.getState() :
500                         AppState.APPSTATE_UNKNOWN;
501                 if (serviceState == ServiceState.STATE_POWER_OFF) {
502                     return DisconnectCause.POWER_OFF;
503                 }
504                 if (!mIsEmergencyCall) {
505                     // Only send OUT_OF_SERVICE if it is not an emergency call. We can still
506                     // technically be in STATE_OUT_OF_SERVICE or STATE_EMERGENCY_ONLY during
507                     // an emergency call and when it ends, we do not want to mistakenly generate
508                     // an OUT_OF_SERVICE disconnect cause during normal call ending.
509                     if ((serviceState == ServiceState.STATE_OUT_OF_SERVICE
510                             || serviceState == ServiceState.STATE_EMERGENCY_ONLY)) {
511                         return DisconnectCause.OUT_OF_SERVICE;
512                     }
513                     // If we are placing an emergency call and the SIM is currently PIN/PUK
514                     // locked the AppState will always not be equal to APPSTATE_READY.
515                     if (uiccAppState != AppState.APPSTATE_READY) {
516                         if (isPhoneTypeGsm()) {
517                             return DisconnectCause.ICC_ERROR;
518                         } else { // CDMA
519                             if (phone.mCdmaSubscriptionSource ==
520                                     CdmaSubscriptionSourceManager.SUBSCRIPTION_FROM_RUIM) {
521                                 return DisconnectCause.ICC_ERROR;
522                             }
523                         }
524                     }
525                 }
526                 if (isPhoneTypeGsm()) {
527                     if (causeCode == CallFailCause.ERROR_UNSPECIFIED) {
528                         if (phone.mSST.mRestrictedState.isCsRestricted()) {
529                             return DisconnectCause.CS_RESTRICTED;
530                         } else if (phone.mSST.mRestrictedState.isCsEmergencyRestricted()) {
531                             return DisconnectCause.CS_RESTRICTED_EMERGENCY;
532                         } else if (phone.mSST.mRestrictedState.isCsNormalRestricted()) {
533                             return DisconnectCause.CS_RESTRICTED_NORMAL;
534                         }
535                     }
536                 }
537                 if (causeCode == CallFailCause.NORMAL_CLEARING) {
538                     return DisconnectCause.NORMAL;
539                 }
540                 // If nothing else matches, report unknown call drop reason
541                 // to app, not NORMAL call end.
542                 return DisconnectCause.ERROR_UNSPECIFIED;
543         }
544     }
545 
546     /*package*/ void
onRemoteDisconnect(int causeCode, String vendorCause)547     onRemoteDisconnect(int causeCode, String vendorCause) {
548         this.mPreciseCause = causeCode;
549         this.mVendorCause = vendorCause;
550         onDisconnect(disconnectCauseFromCode(causeCode));
551     }
552 
553     /**
554      * Called when the radio indicates the connection has been disconnected.
555      * @param cause call disconnect cause; values are defined in {@link DisconnectCause}
556      */
557     @Override
onDisconnect(int cause)558     public boolean onDisconnect(int cause) {
559         boolean changed = false;
560 
561         mCause = cause;
562 
563         if (!mDisconnected) {
564             doDisconnect();
565 
566             if (DBG) Rlog.d(LOG_TAG, "onDisconnect: cause=" + cause);
567 
568             mOwner.getPhone().notifyDisconnect(this);
569 
570             if (mParent != null) {
571                 changed = mParent.connectionDisconnected(this);
572             }
573 
574             mOrigConnection = null;
575         }
576         clearPostDialListeners();
577         releaseWakeLock();
578         return changed;
579     }
580 
581     //CDMA
582     /** Called when the call waiting connection has been hung up */
583     /*package*/ void
onLocalDisconnect()584     onLocalDisconnect() {
585         if (!mDisconnected) {
586             doDisconnect();
587             if (VDBG) Rlog.d(LOG_TAG, "onLoalDisconnect" );
588 
589             if (mParent != null) {
590                 mParent.detach(this);
591             }
592         }
593         releaseWakeLock();
594     }
595 
596     // Returns true if state has changed, false if nothing changed
597     public boolean
update(DriverCall dc)598     update (DriverCall dc) {
599         GsmCdmaCall newParent;
600         boolean changed = false;
601         boolean wasConnectingInOrOut = isConnectingInOrOut();
602         boolean wasHolding = (getState() == GsmCdmaCall.State.HOLDING);
603 
604         newParent = parentFromDCState(dc.state);
605 
606         if (Phone.DEBUG_PHONE) log("parent= " +mParent +", newParent= " + newParent);
607 
608         //Ignore dc.number and dc.name in case of a handover connection
609         if (isPhoneTypeGsm() && mOrigConnection != null) {
610             if (Phone.DEBUG_PHONE) log("update: mOrigConnection is not null");
611         } else {
612             log(" mNumberConverted " + mNumberConverted);
613             if (!equalsHandlesNulls(mAddress, dc.number) && (!mNumberConverted
614                     || !equalsHandlesNulls(mConvertedNumber, dc.number))) {
615                 if (Phone.DEBUG_PHONE) log("update: phone # changed!");
616                 mAddress = dc.number;
617                 changed = true;
618             }
619         }
620 
621         // A null cnapName should be the same as ""
622         if (TextUtils.isEmpty(dc.name)) {
623             if (!TextUtils.isEmpty(mCnapName)) {
624                 changed = true;
625                 mCnapName = "";
626             }
627         } else if (!dc.name.equals(mCnapName)) {
628             changed = true;
629             mCnapName = dc.name;
630         }
631 
632         if (Phone.DEBUG_PHONE) log("--dssds----"+mCnapName);
633         mCnapNamePresentation = dc.namePresentation;
634         mNumberPresentation = dc.numberPresentation;
635 
636         if (newParent != mParent) {
637             if (mParent != null) {
638                 mParent.detach(this);
639             }
640             newParent.attach(this, dc);
641             mParent = newParent;
642             changed = true;
643         } else {
644             boolean parentStateChange;
645             parentStateChange = mParent.update (this, dc);
646             changed = changed || parentStateChange;
647         }
648 
649         /** Some state-transition events */
650 
651         if (Phone.DEBUG_PHONE) log(
652                 "update: parent=" + mParent +
653                 ", hasNewParent=" + (newParent != mParent) +
654                 ", wasConnectingInOrOut=" + wasConnectingInOrOut +
655                 ", wasHolding=" + wasHolding +
656                 ", isConnectingInOrOut=" + isConnectingInOrOut() +
657                 ", changed=" + changed);
658 
659 
660         if (wasConnectingInOrOut && !isConnectingInOrOut()) {
661             onConnectedInOrOut();
662         }
663 
664         if (changed && !wasHolding && (getState() == GsmCdmaCall.State.HOLDING)) {
665             // We've transitioned into HOLDING
666             onStartedHolding();
667         }
668 
669         return changed;
670     }
671 
672     /**
673      * Called when this Connection is in the foregroundCall
674      * when a dial is initiated.
675      * We know we're ACTIVE, and we know we're going to end up
676      * HOLDING in the backgroundCall
677      */
678     void
fakeHoldBeforeDial()679     fakeHoldBeforeDial() {
680         if (mParent != null) {
681             mParent.detach(this);
682         }
683 
684         mParent = mOwner.mBackgroundCall;
685         mParent.attachFake(this, GsmCdmaCall.State.HOLDING);
686 
687         onStartedHolding();
688     }
689 
690     /*package*/ int
getGsmCdmaIndex()691     getGsmCdmaIndex() throws CallStateException {
692         if (mIndex >= 0) {
693             return mIndex + 1;
694         } else {
695             throw new CallStateException ("GsmCdma index not yet assigned");
696         }
697     }
698 
699     /**
700      * An incoming or outgoing call has connected
701      */
702     void
onConnectedInOrOut()703     onConnectedInOrOut() {
704         mConnectTime = System.currentTimeMillis();
705         mConnectTimeReal = SystemClock.elapsedRealtime();
706         mDuration = 0;
707 
708         // bug #678474: incoming call interpreted as missed call, even though
709         // it sounds like the user has picked up the call.
710         if (Phone.DEBUG_PHONE) {
711             log("onConnectedInOrOut: connectTime=" + mConnectTime);
712         }
713 
714         if (!mIsIncoming) {
715             // outgoing calls only
716             processNextPostDialChar();
717         } else {
718             // Only release wake lock for incoming calls, for outgoing calls the wake lock
719             // will be released after any pause-dial is completed
720             releaseWakeLock();
721         }
722     }
723 
724     private void
doDisconnect()725     doDisconnect() {
726         mIndex = -1;
727         mDisconnectTime = System.currentTimeMillis();
728         mDuration = SystemClock.elapsedRealtime() - mConnectTimeReal;
729         mDisconnected = true;
730         clearPostDialListeners();
731     }
732 
733     /*package*/ void
onStartedHolding()734     onStartedHolding() {
735         mHoldingStartTime = SystemClock.elapsedRealtime();
736     }
737 
738     /**
739      * Performs the appropriate action for a post-dial char, but does not
740      * notify application. returns false if the character is invalid and
741      * should be ignored
742      */
743     private boolean
processPostDialChar(char c)744     processPostDialChar(char c) {
745         if (PhoneNumberUtils.is12Key(c)) {
746             mOwner.mCi.sendDtmf(c, mHandler.obtainMessage(EVENT_DTMF_DONE));
747         } else if (isPause(c)) {
748             if (!isPhoneTypeGsm()) {
749                 setPostDialState(PostDialState.PAUSE);
750             }
751             // From TS 22.101:
752             // It continues...
753             // Upon the called party answering the UE shall send the DTMF digits
754             // automatically to the network after a delay of 3 seconds( 20 ).
755             // The digits shall be sent according to the procedures and timing
756             // specified in 3GPP TS 24.008 [13]. The first occurrence of the
757             // "DTMF Control Digits Separator" shall be used by the ME to
758             // distinguish between the addressing digits (i.e. the phone number)
759             // and the DTMF digits. Upon subsequent occurrences of the
760             // separator,
761             // the UE shall pause again for 3 seconds ( 20 ) before sending
762             // any further DTMF digits.
763             mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_PAUSE_DONE),
764                     isPhoneTypeGsm() ? PAUSE_DELAY_MILLIS_GSM: PAUSE_DELAY_MILLIS_CDMA);
765         } else if (isWait(c)) {
766             setPostDialState(PostDialState.WAIT);
767         } else if (isWild(c)) {
768             setPostDialState(PostDialState.WILD);
769         } else {
770             return false;
771         }
772 
773         return true;
774     }
775 
776     @Override
777     public String
getRemainingPostDialString()778     getRemainingPostDialString() {
779         String subStr = super.getRemainingPostDialString();
780         if (!isPhoneTypeGsm() && !TextUtils.isEmpty(subStr)) {
781             int wIndex = subStr.indexOf(PhoneNumberUtils.WAIT);
782             int pIndex = subStr.indexOf(PhoneNumberUtils.PAUSE);
783 
784             if (wIndex > 0 && (wIndex < pIndex || pIndex <= 0)) {
785                 subStr = subStr.substring(0, wIndex);
786             } else if (pIndex > 0) {
787                 subStr = subStr.substring(0, pIndex);
788             }
789         }
790         return subStr;
791     }
792 
793     //CDMA
updateParent(GsmCdmaCall oldParent, GsmCdmaCall newParent)794     public void updateParent(GsmCdmaCall oldParent, GsmCdmaCall newParent){
795         if (newParent != oldParent) {
796             if (oldParent != null) {
797                 oldParent.detach(this);
798             }
799             newParent.attachFake(this, GsmCdmaCall.State.ACTIVE);
800             mParent = newParent;
801         }
802     }
803 
804     @Override
finalize()805     protected void finalize()
806     {
807         /**
808          * It is understood that This finializer is not guaranteed
809          * to be called and the release lock call is here just in
810          * case there is some path that doesn't call onDisconnect
811          * and or onConnectedInOrOut.
812          */
813         if (mPartialWakeLock.isHeld()) {
814             Rlog.e(LOG_TAG, "[GsmCdmaConn] UNEXPECTED; mPartialWakeLock is held when finalizing.");
815         }
816         clearPostDialListeners();
817         releaseWakeLock();
818     }
819 
820     private void
processNextPostDialChar()821     processNextPostDialChar() {
822         char c = 0;
823         Registrant postDialHandler;
824 
825         if (mPostDialState == PostDialState.CANCELLED) {
826             releaseWakeLock();
827             return;
828         }
829 
830         if (mPostDialString == null ||
831                 mPostDialString.length() <= mNextPostDialChar) {
832             setPostDialState(PostDialState.COMPLETE);
833 
834             // We were holding a wake lock until pause-dial was complete, so give it up now
835             releaseWakeLock();
836 
837             // notifyMessage.arg1 is 0 on complete
838             c = 0;
839         } else {
840             boolean isValid;
841 
842             setPostDialState(PostDialState.STARTED);
843 
844             c = mPostDialString.charAt(mNextPostDialChar++);
845 
846             isValid = processPostDialChar(c);
847 
848             if (!isValid) {
849                 // Will call processNextPostDialChar
850                 mHandler.obtainMessage(EVENT_NEXT_POST_DIAL).sendToTarget();
851                 // Don't notify application
852                 Rlog.e(LOG_TAG, "processNextPostDialChar: c=" + c + " isn't valid!");
853                 return;
854             }
855         }
856 
857         notifyPostDialListenersNextChar(c);
858 
859         // TODO: remove the following code since the handler no longer executes anything.
860         postDialHandler = mOwner.getPhone().getPostDialHandler();
861 
862         Message notifyMessage;
863 
864         if (postDialHandler != null
865                 && (notifyMessage = postDialHandler.messageForRegistrant()) != null) {
866             // The AsyncResult.result is the Connection object
867             PostDialState state = mPostDialState;
868             AsyncResult ar = AsyncResult.forMessage(notifyMessage);
869             ar.result = this;
870             ar.userObj = state;
871 
872             // arg1 is the character that was/is being processed
873             notifyMessage.arg1 = c;
874 
875             //Rlog.v("GsmCdma", "##### processNextPostDialChar: send msg to postDialHandler, arg1=" + c);
876             notifyMessage.sendToTarget();
877         }
878     }
879 
880     /** "connecting" means "has never been ACTIVE" for both incoming
881      *  and outgoing calls
882      */
883     private boolean
isConnectingInOrOut()884     isConnectingInOrOut() {
885         return mParent == null || mParent == mOwner.mRingingCall
886             || mParent.mState == GsmCdmaCall.State.DIALING
887             || mParent.mState == GsmCdmaCall.State.ALERTING;
888     }
889 
890     private GsmCdmaCall
parentFromDCState(DriverCall.State state)891     parentFromDCState (DriverCall.State state) {
892         switch (state) {
893             case ACTIVE:
894             case DIALING:
895             case ALERTING:
896                 return mOwner.mForegroundCall;
897             //break;
898 
899             case HOLDING:
900                 return mOwner.mBackgroundCall;
901             //break;
902 
903             case INCOMING:
904             case WAITING:
905                 return mOwner.mRingingCall;
906             //break;
907 
908             default:
909                 throw new RuntimeException("illegal call state: " + state);
910         }
911     }
912 
913     /**
914      * Set post dial state and acquire wake lock while switching to "started" or "pause"
915      * state, the wake lock will be released if state switches out of "started" or "pause"
916      * state or after WAKE_LOCK_TIMEOUT_MILLIS.
917      * @param s new PostDialState
918      */
setPostDialState(PostDialState s)919     private void setPostDialState(PostDialState s) {
920         if (s == PostDialState.STARTED ||
921                 s == PostDialState.PAUSE) {
922             synchronized (mPartialWakeLock) {
923                 if (mPartialWakeLock.isHeld()) {
924                     mHandler.removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
925                 } else {
926                     acquireWakeLock();
927                 }
928                 Message msg = mHandler.obtainMessage(EVENT_WAKE_LOCK_TIMEOUT);
929                 mHandler.sendMessageDelayed(msg, WAKE_LOCK_TIMEOUT_MILLIS);
930             }
931         } else {
932             mHandler.removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
933             releaseWakeLock();
934         }
935         mPostDialState = s;
936         notifyPostDialListeners();
937     }
938 
939     private void
createWakeLock(Context context)940     createWakeLock(Context context) {
941         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
942         mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG);
943     }
944 
945     private void
acquireWakeLock()946     acquireWakeLock() {
947         log("acquireWakeLock");
948         mPartialWakeLock.acquire();
949     }
950 
951     private void
releaseWakeLock()952     releaseWakeLock() {
953         synchronized(mPartialWakeLock) {
954             if (mPartialWakeLock.isHeld()) {
955                 log("releaseWakeLock");
956                 mPartialWakeLock.release();
957             }
958         }
959     }
960 
961     private void
releaseAllWakeLocks()962     releaseAllWakeLocks() {
963         synchronized(mPartialWakeLock) {
964             while (mPartialWakeLock.isHeld()) {
965                 mPartialWakeLock.release();
966             }
967         }
968     }
969 
isPause(char c)970     private static boolean isPause(char c) {
971         return c == PhoneNumberUtils.PAUSE;
972     }
973 
isWait(char c)974     private static boolean isWait(char c) {
975         return c == PhoneNumberUtils.WAIT;
976     }
977 
isWild(char c)978     private static boolean isWild(char c) {
979         return c == PhoneNumberUtils.WILD;
980     }
981 
982     //CDMA
983     // This function is to find the next PAUSE character index if
984     // multiple pauses in a row. Otherwise it finds the next non PAUSE or
985     // non WAIT character index.
986     private static int
findNextPCharOrNonPOrNonWCharIndex(String phoneNumber, int currIndex)987     findNextPCharOrNonPOrNonWCharIndex(String phoneNumber, int currIndex) {
988         boolean wMatched = isWait(phoneNumber.charAt(currIndex));
989         int index = currIndex + 1;
990         int length = phoneNumber.length();
991         while (index < length) {
992             char cNext = phoneNumber.charAt(index);
993             // if there is any W inside P/W sequence,mark it
994             if (isWait(cNext)) {
995                 wMatched = true;
996             }
997             // if any characters other than P/W chars after P/W sequence
998             // we break out the loop and append the correct
999             if (!isWait(cNext) && !isPause(cNext)) {
1000                 break;
1001             }
1002             index++;
1003         }
1004 
1005         // It means the PAUSE character(s) is in the middle of dial string
1006         // and it needs to be handled one by one.
1007         if ((index < length) && (index > (currIndex + 1))  &&
1008                 ((wMatched == false) && isPause(phoneNumber.charAt(currIndex)))) {
1009             return (currIndex + 1);
1010         }
1011         return index;
1012     }
1013 
1014     //CDMA
1015     // This function returns either PAUSE or WAIT character to append.
1016     // It is based on the next non PAUSE/WAIT character in the phoneNumber and the
1017     // index for the current PAUSE/WAIT character
1018     private static char
findPOrWCharToAppend(String phoneNumber, int currPwIndex, int nextNonPwCharIndex)1019     findPOrWCharToAppend(String phoneNumber, int currPwIndex, int nextNonPwCharIndex) {
1020         char c = phoneNumber.charAt(currPwIndex);
1021         char ret;
1022 
1023         // Append the PW char
1024         ret = (isPause(c)) ? PhoneNumberUtils.PAUSE : PhoneNumberUtils.WAIT;
1025 
1026         // If the nextNonPwCharIndex is greater than currPwIndex + 1,
1027         // it means the PW sequence contains not only P characters.
1028         // Since for the sequence that only contains P character,
1029         // the P character is handled one by one, the nextNonPwCharIndex
1030         // equals to currPwIndex + 1.
1031         // In this case, skip P, append W.
1032         if (nextNonPwCharIndex > (currPwIndex + 1)) {
1033             ret = PhoneNumberUtils.WAIT;
1034         }
1035         return ret;
1036     }
1037 
maskDialString(String dialString)1038     private String maskDialString(String dialString) {
1039         if (VDBG) {
1040             return dialString;
1041         }
1042 
1043         return "<MASKED>";
1044     }
1045 
fetchDtmfToneDelay(GsmCdmaPhone phone)1046     private void fetchDtmfToneDelay(GsmCdmaPhone phone) {
1047         CarrierConfigManager configMgr = (CarrierConfigManager)
1048                 phone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
1049         PersistableBundle b = configMgr.getConfigForSubId(phone.getSubId());
1050         if (b != null) {
1051             mDtmfToneDelay = b.getInt(phone.getDtmfToneDelayKey());
1052         }
1053     }
1054 
isPhoneTypeGsm()1055     private boolean isPhoneTypeGsm() {
1056         return mOwner.getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_GSM;
1057     }
1058 
log(String msg)1059     private void log(String msg) {
1060         Rlog.d(LOG_TAG, "[GsmCdmaConn] " + msg);
1061     }
1062 
1063     @Override
getNumberPresentation()1064     public int getNumberPresentation() {
1065         return mNumberPresentation;
1066     }
1067 
1068     @Override
getUUSInfo()1069     public UUSInfo getUUSInfo() {
1070         return mUusInfo;
1071     }
1072 
getPreciseDisconnectCause()1073     public int getPreciseDisconnectCause() {
1074         return mPreciseCause;
1075     }
1076 
1077     @Override
getVendorDisconnectCause()1078     public String getVendorDisconnectCause() {
1079         return mVendorCause;
1080     }
1081 
1082     @Override
migrateFrom(Connection c)1083     public void migrateFrom(Connection c) {
1084         if (c == null) return;
1085 
1086         super.migrateFrom(c);
1087 
1088         this.mUusInfo = c.getUUSInfo();
1089 
1090         this.setUserData(c.getUserData());
1091     }
1092 
1093     @Override
getOrigConnection()1094     public Connection getOrigConnection() {
1095         return mOrigConnection;
1096     }
1097 
1098     @Override
isMultiparty()1099     public boolean isMultiparty() {
1100         if (mOrigConnection != null) {
1101             return mOrigConnection.isMultiparty();
1102         }
1103 
1104         return false;
1105     }
1106 }
1107