CdmaConnection.java revision 121eaf87527829acc2a909e4a0515cada28a674e
1/*
2 * Copyright (C) 2006 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.cdma;
18
19import com.android.internal.telephony.*;
20import android.content.Context;
21import android.os.AsyncResult;
22import android.os.Handler;
23import android.os.Looper;
24import android.os.Message;
25import android.os.PowerManager;
26import android.os.Registrant;
27import android.os.SystemClock;
28import android.telephony.DisconnectCause;
29import android.telephony.Rlog;
30import android.text.TextUtils;
31
32import android.telephony.PhoneNumberUtils;
33import android.telephony.ServiceState;
34
35import com.android.internal.telephony.uicc.UiccCardApplication;
36import com.android.internal.telephony.uicc.UiccController;
37import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppState;
38
39/**
40 * {@hide}
41 */
42public class CdmaConnection extends Connection {
43    static final String LOG_TAG = "CdmaConnection";
44    private static final boolean VDBG = false;
45
46    //***** Instance Variables
47
48    CdmaCallTracker mOwner;
49    CdmaCall mParent;
50
51
52    String mPostDialString;      // outgoing calls only
53    boolean mDisconnected;
54    int mIndex;          // index in CdmaCallTracker.connections[], -1 if unassigned
55
56    /*
57     * These time/timespan values are based on System.currentTimeMillis(),
58     * i.e., "wall clock" time.
59     */
60    long mDisconnectTime;
61
62    int mNextPostDialChar;       // index into postDialString
63
64    int mCause = DisconnectCause.NOT_DISCONNECTED;
65    PostDialState mPostDialState = PostDialState.NOT_STARTED;
66    int mPreciseCause = 0;
67
68    Handler mHandler;
69
70    private PowerManager.WakeLock mPartialWakeLock;
71
72    //***** Event Constants
73    static final int EVENT_DTMF_DONE = 1;
74    static final int EVENT_PAUSE_DONE = 2;
75    static final int EVENT_NEXT_POST_DIAL = 3;
76    static final int EVENT_WAKE_LOCK_TIMEOUT = 4;
77
78    //***** Constants
79    static final int WAKE_LOCK_TIMEOUT_MILLIS = 60*1000;
80    static final int PAUSE_DELAY_MILLIS = 2 * 1000;
81
82    //***** Inner Classes
83
84    class MyHandler extends Handler {
85        MyHandler(Looper l) {super(l);}
86
87        @Override
88        public void
89        handleMessage(Message msg) {
90
91            switch (msg.what) {
92                case EVENT_NEXT_POST_DIAL:
93                case EVENT_DTMF_DONE:
94                case EVENT_PAUSE_DONE:
95                    processNextPostDialChar();
96                    break;
97                case EVENT_WAKE_LOCK_TIMEOUT:
98                    releaseWakeLock();
99                    break;
100            }
101        }
102    }
103
104    //***** Constructors
105
106    /** This is probably an MT call that we first saw in a CLCC response */
107    /*package*/
108    CdmaConnection (Context context, DriverCall dc, CdmaCallTracker ct, int index) {
109        createWakeLock(context);
110        acquireWakeLock();
111
112        mOwner = ct;
113        mHandler = new MyHandler(mOwner.getLooper());
114
115        mAddress = dc.number;
116
117        mIsIncoming = dc.isMT;
118        mCreateTime = System.currentTimeMillis();
119        mCnapName = dc.name;
120        mCnapNamePresentation = dc.namePresentation;
121        mNumberPresentation = dc.numberPresentation;
122
123        mIndex = index;
124
125        mParent = parentFromDCState (dc.state);
126        mParent.attach(this, dc);
127    }
128
129    /** This is an MO call/three way call, created when dialing */
130    /*package*/
131    CdmaConnection(Context context, String dialString, CdmaCallTracker ct, CdmaCall parent) {
132        createWakeLock(context);
133        acquireWakeLock();
134
135        mOwner = ct;
136        mHandler = new MyHandler(mOwner.getLooper());
137
138        mDialString = dialString;
139        Rlog.d(LOG_TAG, "[CDMAConn] CdmaConnection: dialString=" + dialString);
140        dialString = formatDialString(dialString);
141        Rlog.d(LOG_TAG, "[CDMAConn] CdmaConnection:formated dialString=" + dialString);
142
143        mAddress = PhoneNumberUtils.extractNetworkPortionAlt(dialString);
144        mPostDialString = PhoneNumberUtils.extractPostDialPortion(dialString);
145
146        mIndex = -1;
147
148        mIsIncoming = false;
149        mCnapName = null;
150        mCnapNamePresentation = PhoneConstants.PRESENTATION_ALLOWED;
151        mNumberPresentation = PhoneConstants.PRESENTATION_ALLOWED;
152        mCreateTime = System.currentTimeMillis();
153
154        if (parent != null) {
155            mParent = parent;
156
157            //for the three way call case, not change parent state
158            if (parent.mState == CdmaCall.State.ACTIVE) {
159                parent.attachFake(this, CdmaCall.State.ACTIVE);
160            } else {
161                parent.attachFake(this, CdmaCall.State.DIALING);
162            }
163        }
164    }
165
166    /** This is a Call waiting call*/
167    CdmaConnection(Context context, CdmaCallWaitingNotification cw, CdmaCallTracker ct,
168            CdmaCall parent) {
169        createWakeLock(context);
170        acquireWakeLock();
171
172        mOwner = ct;
173        mHandler = new MyHandler(mOwner.getLooper());
174        mAddress = cw.number;
175        mNumberPresentation = cw.numberPresentation;
176        mCnapName = cw.name;
177        mCnapNamePresentation = cw.namePresentation;
178        mIndex = -1;
179        mIsIncoming = true;
180        mCreateTime = System.currentTimeMillis();
181        mConnectTime = 0;
182        mParent = parent;
183        parent.attachFake(this, CdmaCall.State.WAITING);
184    }
185
186    public void dispose() {
187    }
188
189    static boolean
190    equalsHandlesNulls (Object a, Object b) {
191        return (a == null) ? (b == null) : a.equals (b);
192    }
193
194    /*package*/ boolean
195    compareTo(DriverCall c) {
196        // On mobile originated (MO) calls, the phone number may have changed
197        // due to a SIM Toolkit call control modification.
198        //
199        // We assume we know when MO calls are created (since we created them)
200        // and therefore don't need to compare the phone number anyway.
201        if (! (mIsIncoming || c.isMT)) return true;
202
203        // ... but we can compare phone numbers on MT calls, and we have
204        // no control over when they begin, so we might as well
205
206        String cAddress = PhoneNumberUtils.stringFromStringAndTOA(c.number, c.TOA);
207        return mIsIncoming == c.isMT && equalsHandlesNulls(mAddress, cAddress);
208    }
209
210
211    @Override
212    public String getOrigDialString(){
213        return mDialString;
214    }
215
216    @Override
217    public CdmaCall getCall() {
218        return mParent;
219    }
220
221    @Override
222    public long getDisconnectTime() {
223        return mDisconnectTime;
224    }
225
226    @Override
227    public long getHoldDurationMillis() {
228        if (getState() != CdmaCall.State.HOLDING) {
229            // If not holding, return 0
230            return 0;
231        } else {
232            return SystemClock.elapsedRealtime() - mHoldingStartTime;
233        }
234    }
235
236    @Override
237    public int getDisconnectCause() {
238        return mCause;
239    }
240
241    @Override
242    public CdmaCall.State getState() {
243        if (mDisconnected) {
244            return CdmaCall.State.DISCONNECTED;
245        } else {
246            return super.getState();
247        }
248    }
249
250    @Override
251    public void hangup() throws CallStateException {
252        if (!mDisconnected) {
253            mOwner.hangup(this);
254        } else {
255            throw new CallStateException ("disconnected");
256        }
257    }
258
259    @Override
260    public void separate() throws CallStateException {
261        if (!mDisconnected) {
262            mOwner.separate(this);
263        } else {
264            throw new CallStateException ("disconnected");
265        }
266    }
267
268    @Override
269    public PostDialState getPostDialState() {
270        return mPostDialState;
271    }
272
273    @Override
274    public void proceedAfterWaitChar() {
275        if (mPostDialState != PostDialState.WAIT) {
276            Rlog.w(LOG_TAG, "CdmaConnection.proceedAfterWaitChar(): Expected "
277                + "getPostDialState() to be WAIT but was " + mPostDialState);
278            return;
279        }
280
281        setPostDialState(PostDialState.STARTED);
282
283        processNextPostDialChar();
284    }
285
286    @Override
287    public void proceedAfterWildChar(String str) {
288        if (mPostDialState != PostDialState.WILD) {
289            Rlog.w(LOG_TAG, "CdmaConnection.proceedAfterWaitChar(): Expected "
290                + "getPostDialState() to be WILD but was " + mPostDialState);
291            return;
292        }
293
294        setPostDialState(PostDialState.STARTED);
295
296        // make a new postDialString, with the wild char replacement string
297        // at the beginning, followed by the remaining postDialString.
298
299        StringBuilder buf = new StringBuilder(str);
300        buf.append(mPostDialString.substring(mNextPostDialChar));
301        mPostDialString = buf.toString();
302        mNextPostDialChar = 0;
303        if (Phone.DEBUG_PHONE) {
304            log("proceedAfterWildChar: new postDialString is " +
305                    mPostDialString);
306        }
307
308        processNextPostDialChar();
309    }
310
311    @Override
312    public void cancelPostDial() {
313        setPostDialState(PostDialState.CANCELLED);
314    }
315
316    /**
317     * Called when this Connection is being hung up locally (eg, user pressed "end")
318     * Note that at this point, the hangup request has been dispatched to the radio
319     * but no response has yet been received so update() has not yet been called
320     */
321    void
322    onHangupLocal() {
323        mCause = DisconnectCause.LOCAL;
324        mPreciseCause = 0;
325    }
326
327    /**
328     * Maps RIL call disconnect code to {@link DisconnectCause}.
329     * @param causeCode RIL disconnect code
330     * @return the corresponding value from {@link DisconnectCause}
331     */
332    int disconnectCauseFromCode(int causeCode) {
333        /**
334         * See 22.001 Annex F.4 for mapping of cause codes
335         * to local tones
336         */
337
338        switch (causeCode) {
339            case CallFailCause.USER_BUSY:
340                return DisconnectCause.BUSY;
341            case CallFailCause.NO_CIRCUIT_AVAIL:
342                return DisconnectCause.CONGESTION;
343            case CallFailCause.ACM_LIMIT_EXCEEDED:
344                return DisconnectCause.LIMIT_EXCEEDED;
345            case CallFailCause.CALL_BARRED:
346                return DisconnectCause.CALL_BARRED;
347            case CallFailCause.FDN_BLOCKED:
348                return DisconnectCause.FDN_BLOCKED;
349            case CallFailCause.CDMA_LOCKED_UNTIL_POWER_CYCLE:
350                return DisconnectCause.CDMA_LOCKED_UNTIL_POWER_CYCLE;
351            case CallFailCause.CDMA_DROP:
352                return DisconnectCause.CDMA_DROP;
353            case CallFailCause.CDMA_INTERCEPT:
354                return DisconnectCause.CDMA_INTERCEPT;
355            case CallFailCause.CDMA_REORDER:
356                return DisconnectCause.CDMA_REORDER;
357            case CallFailCause.CDMA_SO_REJECT:
358                return DisconnectCause.CDMA_SO_REJECT;
359            case CallFailCause.CDMA_RETRY_ORDER:
360                return DisconnectCause.CDMA_RETRY_ORDER;
361            case CallFailCause.CDMA_ACCESS_FAILURE:
362                return DisconnectCause.CDMA_ACCESS_FAILURE;
363            case CallFailCause.CDMA_PREEMPTED:
364                return DisconnectCause.CDMA_PREEMPTED;
365            case CallFailCause.CDMA_NOT_EMERGENCY:
366                return DisconnectCause.CDMA_NOT_EMERGENCY;
367            case CallFailCause.CDMA_ACCESS_BLOCKED:
368                return DisconnectCause.CDMA_ACCESS_BLOCKED;
369            case CallFailCause.ERROR_UNSPECIFIED:
370            case CallFailCause.NORMAL_CLEARING:
371            default:
372                CDMAPhone phone = mOwner.mPhone;
373                int serviceState = phone.getServiceState().getState();
374                UiccCardApplication app = UiccController
375                        .getInstance()
376                        .getUiccCardApplication(UiccController.APP_FAM_3GPP2);
377                AppState uiccAppState = (app != null) ? app.getState() : AppState.APPSTATE_UNKNOWN;
378                if (serviceState == ServiceState.STATE_POWER_OFF) {
379                    return DisconnectCause.POWER_OFF;
380                } else if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
381                        || serviceState == ServiceState.STATE_EMERGENCY_ONLY) {
382                    return DisconnectCause.OUT_OF_SERVICE;
383                } else if (phone.mCdmaSubscriptionSource ==
384                        CdmaSubscriptionSourceManager.SUBSCRIPTION_FROM_RUIM
385                        && uiccAppState != AppState.APPSTATE_READY) {
386                    return DisconnectCause.ICC_ERROR;
387                } else if (causeCode==CallFailCause.NORMAL_CLEARING) {
388                    return DisconnectCause.NORMAL;
389                } else {
390                    return DisconnectCause.ERROR_UNSPECIFIED;
391                }
392        }
393    }
394
395    /*package*/ void
396    onRemoteDisconnect(int causeCode) {
397        this.mPreciseCause = causeCode;
398        onDisconnect(disconnectCauseFromCode(causeCode));
399    }
400
401    /**
402     * Called when the radio indicates the connection has been disconnected.
403     * @param cause call disconnect cause; values are defined in {@link DisconnectCause}
404     */
405    /*package*/ boolean
406    onDisconnect(int cause) {
407        boolean changed = false;
408
409        mCause = cause;
410
411        if (!mDisconnected) {
412            doDisconnect();
413            if (VDBG) Rlog.d(LOG_TAG, "onDisconnect: cause=" + cause);
414
415            mOwner.mPhone.notifyDisconnect(this);
416
417            if (mParent != null) {
418                changed = mParent.connectionDisconnected(this);
419            }
420        }
421        releaseWakeLock();
422        return changed;
423    }
424
425    /** Called when the call waiting connection has been hung up */
426    /*package*/ void
427    onLocalDisconnect() {
428        if (!mDisconnected) {
429            doDisconnect();
430            if (VDBG) Rlog.d(LOG_TAG, "onLoalDisconnect" );
431
432            if (mParent != null) {
433                mParent.detach(this);
434            }
435        }
436        releaseWakeLock();
437    }
438
439    // Returns true if state has changed, false if nothing changed
440    /*package*/ boolean
441    update (DriverCall dc) {
442        CdmaCall newParent;
443        boolean changed = false;
444        boolean wasConnectingInOrOut = isConnectingInOrOut();
445        boolean wasHolding = (getState() == CdmaCall.State.HOLDING);
446
447        newParent = parentFromDCState(dc.state);
448
449        if (Phone.DEBUG_PHONE) log("parent= " +mParent +", newParent= " + newParent);
450
451        if (!equalsHandlesNulls(mAddress, dc.number)) {
452            if (Phone.DEBUG_PHONE) log("update: phone # changed!");
453            mAddress = dc.number;
454            changed = true;
455        }
456
457        // A null cnapName should be the same as ""
458        if (TextUtils.isEmpty(dc.name)) {
459            if (!TextUtils.isEmpty(mCnapName)) {
460                changed = true;
461                mCnapName = "";
462            }
463        } else if (!dc.name.equals(mCnapName)) {
464            changed = true;
465            mCnapName = dc.name;
466        }
467
468        if (Phone.DEBUG_PHONE) log("--dssds----"+mCnapName);
469        mCnapNamePresentation = dc.namePresentation;
470        mNumberPresentation = dc.numberPresentation;
471
472        if (newParent != mParent) {
473            if (mParent != null) {
474                mParent.detach(this);
475            }
476            newParent.attach(this, dc);
477            mParent = newParent;
478            changed = true;
479        } else {
480            boolean parentStateChange;
481            parentStateChange = mParent.update (this, dc);
482            changed = changed || parentStateChange;
483        }
484
485        /** Some state-transition events */
486
487        if (Phone.DEBUG_PHONE) log(
488                "Update, wasConnectingInOrOut=" + wasConnectingInOrOut +
489                ", wasHolding=" + wasHolding +
490                ", isConnectingInOrOut=" + isConnectingInOrOut() +
491                ", changed=" + changed);
492
493
494        if (wasConnectingInOrOut && !isConnectingInOrOut()) {
495            onConnectedInOrOut();
496        }
497
498        if (changed && !wasHolding && (getState() == CdmaCall.State.HOLDING)) {
499            // We've transitioned into HOLDING
500            onStartedHolding();
501        }
502
503        return changed;
504    }
505
506    /**
507     * Called when this Connection is in the foregroundCall
508     * when a dial is initiated.
509     * We know we're ACTIVE, and we know we're going to end up
510     * HOLDING in the backgroundCall
511     */
512    void
513    fakeHoldBeforeDial() {
514        if (mParent != null) {
515            mParent.detach(this);
516        }
517
518        mParent = mOwner.mBackgroundCall;
519        mParent.attachFake(this, CdmaCall.State.HOLDING);
520
521        onStartedHolding();
522    }
523
524    /*package*/ int
525    getCDMAIndex() throws CallStateException {
526        if (mIndex >= 0) {
527            return mIndex + 1;
528        } else {
529            throw new CallStateException ("CDMA connection index not assigned");
530        }
531    }
532
533    /**
534     * An incoming or outgoing call has connected
535     */
536    void
537    onConnectedInOrOut() {
538        mConnectTime = System.currentTimeMillis();
539        mConnectTimeReal = SystemClock.elapsedRealtime();
540        mDuration = 0;
541
542        // bug #678474: incoming call interpreted as missed call, even though
543        // it sounds like the user has picked up the call.
544        if (Phone.DEBUG_PHONE) {
545            log("onConnectedInOrOut: connectTime=" + mConnectTime);
546        }
547
548        if (!mIsIncoming) {
549            // outgoing calls only
550            processNextPostDialChar();
551        } else {
552            // Only release wake lock for incoming calls, for outgoing calls the wake lock
553            // will be released after any pause-dial is completed
554            releaseWakeLock();
555        }
556    }
557
558    private void
559    doDisconnect() {
560        mIndex = -1;
561        mDisconnectTime = System.currentTimeMillis();
562        mDuration = SystemClock.elapsedRealtime() - mConnectTimeReal;
563        mDisconnected = true;
564        clearPostDialListeners();
565    }
566
567    /*package*/ void
568    onStartedHolding() {
569        mHoldingStartTime = SystemClock.elapsedRealtime();
570    }
571    /**
572     * Performs the appropriate action for a post-dial char, but does not
573     * notify application. returns false if the character is invalid and
574     * should be ignored
575     */
576    private boolean
577    processPostDialChar(char c) {
578        if (PhoneNumberUtils.is12Key(c)) {
579            mOwner.mCi.sendDtmf(c, mHandler.obtainMessage(EVENT_DTMF_DONE));
580        } else if (c == PhoneNumberUtils.PAUSE) {
581            setPostDialState(PostDialState.PAUSE);
582
583            // Upon occurrences of the separator, the UE shall
584            // pause again for 2 seconds before sending any
585            // further DTMF digits.
586            mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_PAUSE_DONE),
587                                            PAUSE_DELAY_MILLIS);
588        } else if (c == PhoneNumberUtils.WAIT) {
589            setPostDialState(PostDialState.WAIT);
590        } else if (c == PhoneNumberUtils.WILD) {
591            setPostDialState(PostDialState.WILD);
592        } else {
593            return false;
594        }
595
596        return true;
597    }
598
599    @Override
600    public String getRemainingPostDialString() {
601        if (mPostDialState == PostDialState.CANCELLED
602                || mPostDialState == PostDialState.COMPLETE
603                || mPostDialString == null
604                || mPostDialString.length() <= mNextPostDialChar) {
605            return "";
606        }
607
608        String subStr = mPostDialString.substring(mNextPostDialChar);
609        if (subStr != null) {
610            int wIndex = subStr.indexOf(PhoneNumberUtils.WAIT);
611            int pIndex = subStr.indexOf(PhoneNumberUtils.PAUSE);
612
613            if (wIndex > 0 && (wIndex < pIndex || pIndex <= 0)) {
614                subStr = subStr.substring(0, wIndex);
615            } else if (pIndex > 0) {
616                subStr = subStr.substring(0, pIndex);
617            }
618        }
619        return subStr;
620    }
621
622    public void updateParent(CdmaCall oldParent, CdmaCall newParent){
623        if (newParent != oldParent) {
624            if (oldParent != null) {
625                oldParent.detach(this);
626            }
627            newParent.attachFake(this, CdmaCall.State.ACTIVE);
628            mParent = newParent;
629        }
630    }
631
632    @Override
633    protected void finalize()
634    {
635        /**
636         * It is understood that This finializer is not guaranteed
637         * to be called and the release lock call is here just in
638         * case there is some path that doesn't call onDisconnect
639         * and or onConnectedInOrOut.
640         */
641        if (mPartialWakeLock.isHeld()) {
642            Rlog.e(LOG_TAG, "[CdmaConn] UNEXPECTED; mPartialWakeLock is held when finalizing.");
643        }
644        releaseWakeLock();
645    }
646
647    void processNextPostDialChar() {
648        char c = 0;
649        Registrant postDialHandler;
650
651        if (mPostDialState == PostDialState.CANCELLED) {
652            releaseWakeLock();
653            //Rlog.v("CDMA", "##### processNextPostDialChar: postDialState == CANCELLED, bail");
654            return;
655        }
656
657        if (mPostDialString == null ||
658                mPostDialString.length() <= mNextPostDialChar) {
659            setPostDialState(PostDialState.COMPLETE);
660
661            // We were holding a wake lock until pause-dial was complete, so give it up now
662            releaseWakeLock();
663
664            // notifyMessage.arg1 is 0 on complete
665            c = 0;
666        } else {
667            boolean isValid;
668
669            setPostDialState(PostDialState.STARTED);
670
671            c = mPostDialString.charAt(mNextPostDialChar++);
672
673            isValid = processPostDialChar(c);
674
675            if (!isValid) {
676                // Will call processNextPostDialChar
677                mHandler.obtainMessage(EVENT_NEXT_POST_DIAL).sendToTarget();
678                // Don't notify application
679                Rlog.e("CDMA", "processNextPostDialChar: c=" + c + " isn't valid!");
680                return;
681            }
682        }
683
684        postDialHandler = mOwner.mPhone.mPostDialHandler;
685
686        Message notifyMessage;
687
688        if (postDialHandler != null &&
689                (notifyMessage = postDialHandler.messageForRegistrant()) != null) {
690            // The AsyncResult.result is the Connection object
691            PostDialState state = mPostDialState;
692            AsyncResult ar = AsyncResult.forMessage(notifyMessage);
693            ar.result = this;
694            ar.userObj = state;
695
696            // arg1 is the character that was/is being processed
697            notifyMessage.arg1 = c;
698
699            notifyMessage.sendToTarget();
700        }
701    }
702
703
704    /** "connecting" means "has never been ACTIVE" for both incoming
705     *  and outgoing calls
706     */
707    private boolean
708    isConnectingInOrOut() {
709        return mParent == null || mParent == mOwner.mRingingCall
710            || mParent.mState == CdmaCall.State.DIALING
711            || mParent.mState == CdmaCall.State.ALERTING;
712    }
713
714    private CdmaCall
715    parentFromDCState (DriverCall.State state) {
716        switch (state) {
717            case ACTIVE:
718            case DIALING:
719            case ALERTING:
720                return mOwner.mForegroundCall;
721            //break;
722
723            case HOLDING:
724                return mOwner.mBackgroundCall;
725            //break;
726
727            case INCOMING:
728            case WAITING:
729                return mOwner.mRingingCall;
730            //break;
731
732            default:
733                throw new RuntimeException("illegal call state: " + state);
734        }
735    }
736
737    /**
738     * Set post dial state and acquire wake lock while switching to "started" or "wait"
739     * state, the wake lock will be released if state switches out of "started" or "wait"
740     * state or after WAKE_LOCK_TIMEOUT_MILLIS.
741     * @param s new PostDialState
742     */
743    private void setPostDialState(PostDialState s) {
744        if (s == PostDialState.STARTED ||
745                s == PostDialState.PAUSE) {
746            synchronized (mPartialWakeLock) {
747                if (mPartialWakeLock.isHeld()) {
748                    mHandler.removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
749                } else {
750                    acquireWakeLock();
751                }
752                Message msg = mHandler.obtainMessage(EVENT_WAKE_LOCK_TIMEOUT);
753                mHandler.sendMessageDelayed(msg, WAKE_LOCK_TIMEOUT_MILLIS);
754            }
755        } else {
756            mHandler.removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
757            releaseWakeLock();
758        }
759        mPostDialState = s;
760        notifyPostDialListeners();
761    }
762
763    private void createWakeLock(Context context) {
764        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
765        mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG);
766    }
767
768    private void acquireWakeLock() {
769        log("acquireWakeLock");
770        mPartialWakeLock.acquire();
771    }
772
773    private void releaseWakeLock() {
774        synchronized (mPartialWakeLock) {
775            if (mPartialWakeLock.isHeld()) {
776                log("releaseWakeLock");
777                mPartialWakeLock.release();
778            }
779        }
780    }
781
782    private static boolean isPause(char c) {
783        return c == PhoneNumberUtils.PAUSE;
784    }
785
786    private static boolean isWait(char c) {
787        return c == PhoneNumberUtils.WAIT;
788    }
789
790    // This function is to find the next PAUSE character index if
791    // multiple pauses in a row. Otherwise it finds the next non PAUSE or
792    // non WAIT character index.
793    private static int
794    findNextPCharOrNonPOrNonWCharIndex(String phoneNumber, int currIndex) {
795        boolean wMatched = isWait(phoneNumber.charAt(currIndex));
796        int index = currIndex + 1;
797        int length = phoneNumber.length();
798        while (index < length) {
799            char cNext = phoneNumber.charAt(index);
800            // if there is any W inside P/W sequence,mark it
801            if (isWait(cNext)) {
802                wMatched = true;
803            }
804            // if any characters other than P/W chars after P/W sequence
805            // we break out the loop and append the correct
806            if (!isWait(cNext) && !isPause(cNext)) {
807                break;
808            }
809            index++;
810        }
811
812        // It means the PAUSE character(s) is in the middle of dial string
813        // and it needs to be handled one by one.
814        if ((index < length) && (index > (currIndex + 1))  &&
815            ((wMatched == false) && isPause(phoneNumber.charAt(currIndex)))) {
816            return (currIndex + 1);
817        }
818        return index;
819    }
820
821    // This function returns either PAUSE or WAIT character to append.
822    // It is based on the next non PAUSE/WAIT character in the phoneNumber and the
823    // index for the current PAUSE/WAIT character
824    private static char
825    findPOrWCharToAppend(String phoneNumber, int currPwIndex, int nextNonPwCharIndex) {
826        char c = phoneNumber.charAt(currPwIndex);
827        char ret;
828
829        // Append the PW char
830        ret = (isPause(c)) ? PhoneNumberUtils.PAUSE : PhoneNumberUtils.WAIT;
831
832        // If the nextNonPwCharIndex is greater than currPwIndex + 1,
833        // it means the PW sequence contains not only P characters.
834        // Since for the sequence that only contains P character,
835        // the P character is handled one by one, the nextNonPwCharIndex
836        // equals to currPwIndex + 1.
837        // In this case, skip P, append W.
838        if (nextNonPwCharIndex > (currPwIndex + 1)) {
839            ret = PhoneNumberUtils.WAIT;
840        }
841        return ret;
842    }
843
844    /**
845     * format original dial string
846     * 1) convert international dialing prefix "+" to
847     *    string specified per region
848     *
849     * 2) handle corner cases for PAUSE/WAIT dialing:
850     *
851     *    If PAUSE/WAIT sequence at the end, ignore them.
852     *
853     *    If consecutive PAUSE/WAIT sequence in the middle of the string,
854     *    and if there is any WAIT in PAUSE/WAIT sequence, treat them like WAIT.
855     */
856    public static String formatDialString(String phoneNumber) {
857        /**
858         * TODO(cleanup): This function should move to PhoneNumberUtils, and
859         * tests should be added.
860         */
861
862        if (phoneNumber == null) {
863            return null;
864        }
865        int length = phoneNumber.length();
866        StringBuilder ret = new StringBuilder();
867        char c;
868        int currIndex = 0;
869
870        while (currIndex < length) {
871            c = phoneNumber.charAt(currIndex);
872            if (isPause(c) || isWait(c)) {
873                if (currIndex < length - 1) {
874                    // if PW not at the end
875                    int nextIndex = findNextPCharOrNonPOrNonWCharIndex(phoneNumber, currIndex);
876                    // If there is non PW char following PW sequence
877                    if (nextIndex < length) {
878                        char pC = findPOrWCharToAppend(phoneNumber, currIndex, nextIndex);
879                        ret.append(pC);
880                        // If PW char sequence has more than 2 PW characters,
881                        // skip to the last PW character since the sequence already be
882                        // converted to WAIT character
883                        if (nextIndex > (currIndex + 1)) {
884                            currIndex = nextIndex - 1;
885                        }
886                    } else if (nextIndex == length) {
887                        // It means PW characters at the end, ignore
888                        currIndex = length - 1;
889                    }
890                }
891            } else {
892                ret.append(c);
893            }
894            currIndex++;
895        }
896        return PhoneNumberUtils.cdmaCheckAndProcessPlusCode(ret.toString());
897    }
898
899    private void log(String msg) {
900        Rlog.d(LOG_TAG, "[CDMAConn] " + msg);
901    }
902
903    @Override
904    public int getNumberPresentation() {
905        return mNumberPresentation;
906    }
907
908    @Override
909    public UUSInfo getUUSInfo() {
910        // UUS information not supported in CDMA
911        return null;
912    }
913
914    public int getPreciseDisconnectCause() {
915        return mPreciseCause;
916    }
917
918    @Override
919    public Connection getOrigConnection() {
920        return null;
921    }
922
923    @Override
924    public boolean isMultiparty() {
925        return false;
926    }
927}
928