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