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