GsmCdmaConnection.java revision cdcf059efaf8f413cecc88f3ef8e05ef811fa5e9
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    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 {
94        MyHandler(Looper l) {super(l);}
95
96        @Override
97        public void
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. */
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 */
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*/
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
221    public void dispose() {
222        clearPostDialListeners();
223        if (mParent != null) {
224            mParent.detach(this);
225        }
226        releaseAllWakeLocks();
227    }
228
229    static boolean
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     */
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
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
312    public String getOrigDialString(){
313        return mDialString;
314    }
315
316    @Override
317    public GsmCdmaCall getCall() {
318        return mParent;
319    }
320
321    @Override
322    public long getDisconnectTime() {
323        return mDisconnectTime;
324    }
325
326    @Override
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
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 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
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
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
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     */
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.CALL_BARRED:
445                return DisconnectCause.CALL_BARRED;
446
447            case CallFailCause.FDN_BLOCKED:
448                return DisconnectCause.FDN_BLOCKED;
449
450            case CallFailCause.UNOBTAINABLE_NUMBER:
451                return DisconnectCause.UNOBTAINABLE_NUMBER;
452
453            case CallFailCause.DIAL_MODIFIED_TO_USSD:
454                return DisconnectCause.DIAL_MODIFIED_TO_USSD;
455
456            case CallFailCause.DIAL_MODIFIED_TO_SS:
457                return DisconnectCause.DIAL_MODIFIED_TO_SS;
458
459            case CallFailCause.DIAL_MODIFIED_TO_DIAL:
460                return DisconnectCause.DIAL_MODIFIED_TO_DIAL;
461
462            case CallFailCause.CDMA_LOCKED_UNTIL_POWER_CYCLE:
463                return DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE;
464
465            case CallFailCause.CDMA_DROP:
466                return DisconnectCause.CDMA_DROP;
467
468            case CallFailCause.CDMA_INTERCEPT:
469                return DisconnectCause.CDMA_INTERCEPT;
470
471            case CallFailCause.CDMA_REORDER:
472                return DisconnectCause.CDMA_REORDER;
473
474            case CallFailCause.CDMA_SO_REJECT:
475                return DisconnectCause.CDMA_SO_REJECT;
476
477            case CallFailCause.CDMA_RETRY_ORDER:
478                return DisconnectCause.CDMA_RETRY_ORDER;
479
480            case CallFailCause.CDMA_ACCESS_FAILURE:
481                return DisconnectCause.CDMA_ACCESS_FAILURE;
482
483            case CallFailCause.CDMA_PREEMPTED:
484                return DisconnectCause.CDMA_PREEMPTED;
485
486            case CallFailCause.CDMA_NOT_EMERGENCY:
487                return DisconnectCause.CDMA_NOT_EMERGENCY;
488
489            case CallFailCause.CDMA_ACCESS_BLOCKED:
490                return DisconnectCause.CDMA_ACCESS_BLOCKED;
491
492            case CallFailCause.ERROR_UNSPECIFIED:
493            case CallFailCause.NORMAL_CLEARING:
494            default:
495                GsmCdmaPhone phone = mOwner.getPhone();
496                int serviceState = phone.getServiceState().getState();
497                UiccCardApplication cardApp = phone.getUiccCardApplication();
498                AppState uiccAppState = (cardApp != null) ? cardApp.getState() :
499                        AppState.APPSTATE_UNKNOWN;
500                if (serviceState == ServiceState.STATE_POWER_OFF) {
501                    return DisconnectCause.POWER_OFF;
502                }
503                if (!mIsEmergencyCall) {
504                    // Only send OUT_OF_SERVICE if it is not an emergency call. We can still
505                    // technically be in STATE_OUT_OF_SERVICE or STATE_EMERGENCY_ONLY during
506                    // an emergency call and when it ends, we do not want to mistakenly generate
507                    // an OUT_OF_SERVICE disconnect cause during normal call ending.
508                    if ((serviceState == ServiceState.STATE_OUT_OF_SERVICE
509                            || serviceState == ServiceState.STATE_EMERGENCY_ONLY)) {
510                        return DisconnectCause.OUT_OF_SERVICE;
511                    }
512                    // If we are placing an emergency call and the SIM is currently PIN/PUK
513                    // locked the AppState will always not be equal to APPSTATE_READY.
514                    if (uiccAppState != AppState.APPSTATE_READY) {
515                        if (isPhoneTypeGsm()) {
516                            return DisconnectCause.ICC_ERROR;
517                        } else { // CDMA
518                            if (phone.mCdmaSubscriptionSource ==
519                                    CdmaSubscriptionSourceManager.SUBSCRIPTION_FROM_RUIM) {
520                                return DisconnectCause.ICC_ERROR;
521                            }
522                        }
523                    }
524                }
525                if (isPhoneTypeGsm()) {
526                    if (causeCode == CallFailCause.ERROR_UNSPECIFIED) {
527                        if (phone.mSST.mRestrictedState.isCsRestricted()) {
528                            return DisconnectCause.CS_RESTRICTED;
529                        } else if (phone.mSST.mRestrictedState.isCsEmergencyRestricted()) {
530                            return DisconnectCause.CS_RESTRICTED_EMERGENCY;
531                        } else if (phone.mSST.mRestrictedState.isCsNormalRestricted()) {
532                            return DisconnectCause.CS_RESTRICTED_NORMAL;
533                        }
534                    }
535                }
536                if (causeCode == CallFailCause.NORMAL_CLEARING) {
537                    return DisconnectCause.NORMAL;
538                }
539                // If nothing else matches, report unknown call drop reason
540                // to app, not NORMAL call end.
541                return DisconnectCause.ERROR_UNSPECIFIED;
542        }
543    }
544
545    /*package*/ void
546    onRemoteDisconnect(int causeCode, String vendorCause) {
547        this.mPreciseCause = causeCode;
548        this.mVendorCause = vendorCause;
549        onDisconnect(disconnectCauseFromCode(causeCode));
550    }
551
552    /**
553     * Called when the radio indicates the connection has been disconnected.
554     * @param cause call disconnect cause; values are defined in {@link DisconnectCause}
555     */
556    @Override
557    public 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.getPhone().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    public 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            // Only release wake lock for incoming calls, for outgoing calls the wake lock
718            // will be released after any pause-dial is completed
719            releaseWakeLock();
720        }
721    }
722
723    private void
724    doDisconnect() {
725        mIndex = -1;
726        mDisconnectTime = System.currentTimeMillis();
727        mDuration = SystemClock.elapsedRealtime() - mConnectTimeReal;
728        mDisconnected = true;
729        clearPostDialListeners();
730    }
731
732    /*package*/ void
733    onStartedHolding() {
734        mHoldingStartTime = SystemClock.elapsedRealtime();
735    }
736
737    /**
738     * Performs the appropriate action for a post-dial char, but does not
739     * notify application. returns false if the character is invalid and
740     * should be ignored
741     */
742    private boolean
743    processPostDialChar(char c) {
744        if (PhoneNumberUtils.is12Key(c)) {
745            mOwner.mCi.sendDtmf(c, mHandler.obtainMessage(EVENT_DTMF_DONE));
746        } else if (isPause(c)) {
747            if (!isPhoneTypeGsm()) {
748                setPostDialState(PostDialState.PAUSE);
749            }
750            // From TS 22.101:
751            // It continues...
752            // Upon the called party answering the UE shall send the DTMF digits
753            // automatically to the network after a delay of 3 seconds( 20 ).
754            // The digits shall be sent according to the procedures and timing
755            // specified in 3GPP TS 24.008 [13]. The first occurrence of the
756            // "DTMF Control Digits Separator" shall be used by the ME to
757            // distinguish between the addressing digits (i.e. the phone number)
758            // and the DTMF digits. Upon subsequent occurrences of the
759            // separator,
760            // the UE shall pause again for 3 seconds ( 20 ) before sending
761            // any further DTMF digits.
762            mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_PAUSE_DONE),
763                    isPhoneTypeGsm() ? PAUSE_DELAY_MILLIS_GSM: PAUSE_DELAY_MILLIS_CDMA);
764        } else if (isWait(c)) {
765            setPostDialState(PostDialState.WAIT);
766        } else if (isWild(c)) {
767            setPostDialState(PostDialState.WILD);
768        } else {
769            return false;
770        }
771
772        return true;
773    }
774
775    @Override
776    public String
777    getRemainingPostDialString() {
778        String subStr = super.getRemainingPostDialString();
779        if (!isPhoneTypeGsm() && !TextUtils.isEmpty(subStr)) {
780            int wIndex = subStr.indexOf(PhoneNumberUtils.WAIT);
781            int pIndex = subStr.indexOf(PhoneNumberUtils.PAUSE);
782
783            if (wIndex > 0 && (wIndex < pIndex || pIndex <= 0)) {
784                subStr = subStr.substring(0, wIndex);
785            } else if (pIndex > 0) {
786                subStr = subStr.substring(0, pIndex);
787            }
788        }
789        return subStr;
790    }
791
792    //CDMA
793    public void updateParent(GsmCdmaCall oldParent, GsmCdmaCall newParent){
794        if (newParent != oldParent) {
795            if (oldParent != null) {
796                oldParent.detach(this);
797            }
798            newParent.attachFake(this, GsmCdmaCall.State.ACTIVE);
799            mParent = newParent;
800        }
801    }
802
803    @Override
804    protected void finalize()
805    {
806        /**
807         * It is understood that This finializer is not guaranteed
808         * to be called and the release lock call is here just in
809         * case there is some path that doesn't call onDisconnect
810         * and or onConnectedInOrOut.
811         */
812        if (mPartialWakeLock.isHeld()) {
813            Rlog.e(LOG_TAG, "[GsmCdmaConn] UNEXPECTED; mPartialWakeLock is held when finalizing.");
814        }
815        clearPostDialListeners();
816        releaseWakeLock();
817    }
818
819    private void
820    processNextPostDialChar() {
821        char c = 0;
822        Registrant postDialHandler;
823
824        if (mPostDialState == PostDialState.CANCELLED) {
825            releaseWakeLock();
826            return;
827        }
828
829        if (mPostDialString == null ||
830                mPostDialString.length() <= mNextPostDialChar) {
831            setPostDialState(PostDialState.COMPLETE);
832
833            // We were holding a wake lock until pause-dial was complete, so give it up now
834            releaseWakeLock();
835
836            // notifyMessage.arg1 is 0 on complete
837            c = 0;
838        } else {
839            boolean isValid;
840
841            setPostDialState(PostDialState.STARTED);
842
843            c = mPostDialString.charAt(mNextPostDialChar++);
844
845            isValid = processPostDialChar(c);
846
847            if (!isValid) {
848                // Will call processNextPostDialChar
849                mHandler.obtainMessage(EVENT_NEXT_POST_DIAL).sendToTarget();
850                // Don't notify application
851                Rlog.e(LOG_TAG, "processNextPostDialChar: c=" + c + " isn't valid!");
852                return;
853            }
854        }
855
856        notifyPostDialListenersNextChar(c);
857
858        // TODO: remove the following code since the handler no longer executes anything.
859        postDialHandler = mOwner.getPhone().getPostDialHandler();
860
861        Message notifyMessage;
862
863        if (postDialHandler != null
864                && (notifyMessage = postDialHandler.messageForRegistrant()) != null) {
865            // The AsyncResult.result is the Connection object
866            PostDialState state = mPostDialState;
867            AsyncResult ar = AsyncResult.forMessage(notifyMessage);
868            ar.result = this;
869            ar.userObj = state;
870
871            // arg1 is the character that was/is being processed
872            notifyMessage.arg1 = c;
873
874            //Rlog.v("GsmCdma", "##### processNextPostDialChar: send msg to postDialHandler, arg1=" + c);
875            notifyMessage.sendToTarget();
876        }
877    }
878
879    /** "connecting" means "has never been ACTIVE" for both incoming
880     *  and outgoing calls
881     */
882    private boolean
883    isConnectingInOrOut() {
884        return mParent == null || mParent == mOwner.mRingingCall
885            || mParent.mState == GsmCdmaCall.State.DIALING
886            || mParent.mState == GsmCdmaCall.State.ALERTING;
887    }
888
889    private GsmCdmaCall
890    parentFromDCState (DriverCall.State state) {
891        switch (state) {
892            case ACTIVE:
893            case DIALING:
894            case ALERTING:
895                return mOwner.mForegroundCall;
896            //break;
897
898            case HOLDING:
899                return mOwner.mBackgroundCall;
900            //break;
901
902            case INCOMING:
903            case WAITING:
904                return mOwner.mRingingCall;
905            //break;
906
907            default:
908                throw new RuntimeException("illegal call state: " + state);
909        }
910    }
911
912    /**
913     * Set post dial state and acquire wake lock while switching to "started" or "pause"
914     * state, the wake lock will be released if state switches out of "started" or "pause"
915     * state or after WAKE_LOCK_TIMEOUT_MILLIS.
916     * @param s new PostDialState
917     */
918    private void setPostDialState(PostDialState s) {
919        if (s == PostDialState.STARTED ||
920                s == PostDialState.PAUSE) {
921            synchronized (mPartialWakeLock) {
922                if (mPartialWakeLock.isHeld()) {
923                    mHandler.removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
924                } else {
925                    acquireWakeLock();
926                }
927                Message msg = mHandler.obtainMessage(EVENT_WAKE_LOCK_TIMEOUT);
928                mHandler.sendMessageDelayed(msg, WAKE_LOCK_TIMEOUT_MILLIS);
929            }
930        } else {
931            mHandler.removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
932            releaseWakeLock();
933        }
934        mPostDialState = s;
935        notifyPostDialListeners();
936    }
937
938    private void
939    createWakeLock(Context context) {
940        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
941        mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG);
942    }
943
944    private void
945    acquireWakeLock() {
946        log("acquireWakeLock");
947        mPartialWakeLock.acquire();
948    }
949
950    private void
951    releaseWakeLock() {
952        synchronized(mPartialWakeLock) {
953            if (mPartialWakeLock.isHeld()) {
954                log("releaseWakeLock");
955                mPartialWakeLock.release();
956            }
957        }
958    }
959
960    private void
961    releaseAllWakeLocks() {
962        synchronized(mPartialWakeLock) {
963            while (mPartialWakeLock.isHeld()) {
964                mPartialWakeLock.release();
965            }
966        }
967    }
968
969    private static boolean isPause(char c) {
970        return c == PhoneNumberUtils.PAUSE;
971    }
972
973    private static boolean isWait(char c) {
974        return c == PhoneNumberUtils.WAIT;
975    }
976
977    private static boolean isWild(char c) {
978        return c == PhoneNumberUtils.WILD;
979    }
980
981    //CDMA
982    // This function is to find the next PAUSE character index if
983    // multiple pauses in a row. Otherwise it finds the next non PAUSE or
984    // non WAIT character index.
985    private static int
986    findNextPCharOrNonPOrNonWCharIndex(String phoneNumber, int currIndex) {
987        boolean wMatched = isWait(phoneNumber.charAt(currIndex));
988        int index = currIndex + 1;
989        int length = phoneNumber.length();
990        while (index < length) {
991            char cNext = phoneNumber.charAt(index);
992            // if there is any W inside P/W sequence,mark it
993            if (isWait(cNext)) {
994                wMatched = true;
995            }
996            // if any characters other than P/W chars after P/W sequence
997            // we break out the loop and append the correct
998            if (!isWait(cNext) && !isPause(cNext)) {
999                break;
1000            }
1001            index++;
1002        }
1003
1004        // It means the PAUSE character(s) is in the middle of dial string
1005        // and it needs to be handled one by one.
1006        if ((index < length) && (index > (currIndex + 1))  &&
1007                ((wMatched == false) && isPause(phoneNumber.charAt(currIndex)))) {
1008            return (currIndex + 1);
1009        }
1010        return index;
1011    }
1012
1013    //CDMA
1014    // This function returns either PAUSE or WAIT character to append.
1015    // It is based on the next non PAUSE/WAIT character in the phoneNumber and the
1016    // index for the current PAUSE/WAIT character
1017    private static char
1018    findPOrWCharToAppend(String phoneNumber, int currPwIndex, int nextNonPwCharIndex) {
1019        char c = phoneNumber.charAt(currPwIndex);
1020        char ret;
1021
1022        // Append the PW char
1023        ret = (isPause(c)) ? PhoneNumberUtils.PAUSE : PhoneNumberUtils.WAIT;
1024
1025        // If the nextNonPwCharIndex is greater than currPwIndex + 1,
1026        // it means the PW sequence contains not only P characters.
1027        // Since for the sequence that only contains P character,
1028        // the P character is handled one by one, the nextNonPwCharIndex
1029        // equals to currPwIndex + 1.
1030        // In this case, skip P, append W.
1031        if (nextNonPwCharIndex > (currPwIndex + 1)) {
1032            ret = PhoneNumberUtils.WAIT;
1033        }
1034        return ret;
1035    }
1036
1037    private String maskDialString(String dialString) {
1038        if (VDBG) {
1039            return dialString;
1040        }
1041
1042        return "<MASKED>";
1043    }
1044
1045    private void fetchDtmfToneDelay(GsmCdmaPhone phone) {
1046        CarrierConfigManager configMgr = (CarrierConfigManager)
1047                phone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
1048        PersistableBundle b = configMgr.getConfigForSubId(phone.getSubId());
1049        if (b != null) {
1050            mDtmfToneDelay = b.getInt(phone.getDtmfToneDelayKey());
1051        }
1052    }
1053
1054    private boolean isPhoneTypeGsm() {
1055        return mOwner.getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_GSM;
1056    }
1057
1058    private void log(String msg) {
1059        Rlog.d(LOG_TAG, "[GsmCdmaConn] " + msg);
1060    }
1061
1062    @Override
1063    public int getNumberPresentation() {
1064        return mNumberPresentation;
1065    }
1066
1067    @Override
1068    public UUSInfo getUUSInfo() {
1069        return mUusInfo;
1070    }
1071
1072    public int getPreciseDisconnectCause() {
1073        return mPreciseCause;
1074    }
1075
1076    @Override
1077    public String getVendorDisconnectCause() {
1078        return mVendorCause;
1079    }
1080
1081    @Override
1082    public void migrateFrom(Connection c) {
1083        if (c == null) return;
1084
1085        super.migrateFrom(c);
1086
1087        this.mUusInfo = c.getUUSInfo();
1088
1089        this.setUserData(c.getUserData());
1090    }
1091
1092    @Override
1093    public Connection getOrigConnection() {
1094        return mOrigConnection;
1095    }
1096
1097    @Override
1098    public boolean isMultiparty() {
1099        if (mOrigConnection != null) {
1100            return mOrigConnection.isMultiparty();
1101        }
1102
1103        return false;
1104    }
1105}
1106