GsmConnection.java revision d720945f2be5ea5fe0faf67e67d9ea0e184eba67
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.gsm;
18import android.content.Context;
19import android.os.AsyncResult;
20import android.os.Handler;
21import android.os.Looper;
22import android.os.Message;
23import android.os.PowerManager;
24import android.os.Registrant;
25import android.os.SystemClock;
26import android.telephony.Rlog;
27import android.telephony.PhoneNumberUtils;
28import android.telephony.ServiceState;
29import android.text.TextUtils;
30
31import com.android.internal.telephony.*;
32import com.android.internal.telephony.uicc.UiccCardApplication;
33import com.android.internal.telephony.uicc.UiccController;
34import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppState;
35
36/**
37 * {@hide}
38 */
39public class GsmConnection extends Connection {
40    static final String LOG_TAG = "GSM";
41
42    //***** Instance Variables
43
44    GsmCallTracker owner;
45    GsmCall parent;
46
47    String address;     // MAY BE NULL!!!
48    String dialString;          // outgoing calls only
49    String postDialString;      // outgoing calls only
50    boolean isIncoming;
51    boolean disconnected;
52
53    int index;          // index in GsmCallTracker.connections[], -1 if unassigned
54                        // The GSM index is 1 + this
55
56    /*
57     * These time/timespan values are based on System.currentTimeMillis(),
58     * i.e., "wall clock" time.
59     */
60    long createTime;
61    long connectTime;
62    long disconnectTime;
63
64    /*
65     * These time/timespan values are based on SystemClock.elapsedRealTime(),
66     * i.e., time since boot.  They are appropriate for comparison and
67     * calculating deltas.
68     */
69    long connectTimeReal;
70    long duration;
71    long holdingStartTime;  // The time when the Connection last transitioned
72                            // into HOLDING
73
74    int nextPostDialChar;       // index into postDialString
75
76    DisconnectCause cause = DisconnectCause.NOT_DISCONNECTED;
77    PostDialState postDialState = PostDialState.NOT_STARTED;
78    int numberPresentation = PhoneConstants.PRESENTATION_ALLOWED;
79    UUSInfo uusInfo;
80
81    Handler h;
82
83    private PowerManager.WakeLock mPartialWakeLock;
84
85    //***** Event Constants
86    static final int EVENT_DTMF_DONE = 1;
87    static final int EVENT_PAUSE_DONE = 2;
88    static final int EVENT_NEXT_POST_DIAL = 3;
89    static final int EVENT_WAKE_LOCK_TIMEOUT = 4;
90
91    //***** Constants
92    static final int PAUSE_DELAY_FIRST_MILLIS = 100;
93    static final int PAUSE_DELAY_MILLIS = 3 * 1000;
94    static final int WAKE_LOCK_TIMEOUT_MILLIS = 60*1000;
95
96    //***** Inner Classes
97
98    class MyHandler extends Handler {
99        MyHandler(Looper l) {super(l);}
100
101        public void
102        handleMessage(Message msg) {
103
104            switch (msg.what) {
105                case EVENT_NEXT_POST_DIAL:
106                case EVENT_DTMF_DONE:
107                case EVENT_PAUSE_DONE:
108                    processNextPostDialChar();
109                    break;
110                case EVENT_WAKE_LOCK_TIMEOUT:
111                    releaseWakeLock();
112                    break;
113            }
114        }
115    }
116
117    //***** Constructors
118
119    /** This is probably an MT call that we first saw in a CLCC response */
120    /*package*/
121    GsmConnection (Context context, DriverCall dc, GsmCallTracker ct, int index) {
122        createWakeLock(context);
123        acquireWakeLock();
124
125        owner = ct;
126        h = new MyHandler(owner.getLooper());
127
128        address = dc.number;
129
130        isIncoming = dc.isMT;
131        createTime = System.currentTimeMillis();
132        cnapName = dc.name;
133        cnapNamePresentation = dc.namePresentation;
134        numberPresentation = dc.numberPresentation;
135        uusInfo = dc.uusInfo;
136
137        this.index = index;
138
139        parent = parentFromDCState (dc.state);
140        parent.attach(this, dc);
141    }
142
143    /** This is an MO call, created when dialing */
144    /*package*/
145    GsmConnection (Context context, String dialString, GsmCallTracker ct, GsmCall parent) {
146        createWakeLock(context);
147        acquireWakeLock();
148
149        owner = ct;
150        h = new MyHandler(owner.getLooper());
151
152        this.dialString = dialString;
153
154        this.address = PhoneNumberUtils.extractNetworkPortionAlt(dialString);
155        this.postDialString = PhoneNumberUtils.extractPostDialPortion(dialString);
156
157        index = -1;
158
159        isIncoming = false;
160        cnapName = null;
161        cnapNamePresentation = PhoneConstants.PRESENTATION_ALLOWED;
162        numberPresentation = PhoneConstants.PRESENTATION_ALLOWED;
163        createTime = System.currentTimeMillis();
164
165        this.parent = parent;
166        parent.attachFake(this, GsmCall.State.DIALING);
167    }
168
169    public void dispose() {
170    }
171
172    static boolean
173    equalsHandlesNulls (Object a, Object b) {
174        return (a == null) ? (b == null) : a.equals (b);
175    }
176
177    /*package*/ boolean
178    compareTo(DriverCall c) {
179        // On mobile originated (MO) calls, the phone number may have changed
180        // due to a SIM Toolkit call control modification.
181        //
182        // We assume we know when MO calls are created (since we created them)
183        // and therefore don't need to compare the phone number anyway.
184        if (! (isIncoming || c.isMT)) return true;
185
186        // ... but we can compare phone numbers on MT calls, and we have
187        // no control over when they begin, so we might as well
188
189        String cAddress = PhoneNumberUtils.stringFromStringAndTOA(c.number, c.TOA);
190        return isIncoming == c.isMT && equalsHandlesNulls(address, cAddress);
191    }
192
193    public String getAddress() {
194        return address;
195    }
196
197    public GsmCall getCall() {
198        return parent;
199    }
200
201    public long getCreateTime() {
202        return createTime;
203    }
204
205    public long getConnectTime() {
206        return connectTime;
207    }
208
209    public long getDisconnectTime() {
210        return disconnectTime;
211    }
212
213    public long getDurationMillis() {
214        if (connectTimeReal == 0) {
215            return 0;
216        } else if (duration == 0) {
217            return SystemClock.elapsedRealtime() - connectTimeReal;
218        } else {
219            return duration;
220        }
221    }
222
223    public long getHoldDurationMillis() {
224        if (getState() != GsmCall.State.HOLDING) {
225            // If not holding, return 0
226            return 0;
227        } else {
228            return SystemClock.elapsedRealtime() - holdingStartTime;
229        }
230    }
231
232    public DisconnectCause getDisconnectCause() {
233        return cause;
234    }
235
236    public boolean isIncoming() {
237        return isIncoming;
238    }
239
240    public GsmCall.State getState() {
241        if (disconnected) {
242            return GsmCall.State.DISCONNECTED;
243        } else {
244            return super.getState();
245        }
246    }
247
248    public void hangup() throws CallStateException {
249        if (!disconnected) {
250            owner.hangup(this);
251        } else {
252            throw new CallStateException ("disconnected");
253        }
254    }
255
256    public void separate() throws CallStateException {
257        if (!disconnected) {
258            owner.separate(this);
259        } else {
260            throw new CallStateException ("disconnected");
261        }
262    }
263
264    public PostDialState getPostDialState() {
265        return postDialState;
266    }
267
268    public void proceedAfterWaitChar() {
269        if (postDialState != PostDialState.WAIT) {
270            Rlog.w(LOG_TAG, "GsmConnection.proceedAfterWaitChar(): Expected "
271                + "getPostDialState() to be WAIT but was " + postDialState);
272            return;
273        }
274
275        setPostDialState(PostDialState.STARTED);
276
277        processNextPostDialChar();
278    }
279
280    public void proceedAfterWildChar(String str) {
281        if (postDialState != PostDialState.WILD) {
282            Rlog.w(LOG_TAG, "GsmConnection.proceedAfterWaitChar(): Expected "
283                + "getPostDialState() to be WILD but was " + postDialState);
284            return;
285        }
286
287        setPostDialState(PostDialState.STARTED);
288
289        if (false) {
290            boolean playedTone = false;
291            int len = (str != null ? str.length() : 0);
292
293            for (int i=0; i<len; i++) {
294                char c = str.charAt(i);
295                Message msg = null;
296
297                if (i == len-1) {
298                    msg = h.obtainMessage(EVENT_DTMF_DONE);
299                }
300
301                if (PhoneNumberUtils.is12Key(c)) {
302                    owner.cm.sendDtmf(c, msg);
303                    playedTone = true;
304                }
305            }
306
307            if (!playedTone) {
308                processNextPostDialChar();
309            }
310        } else {
311            // make a new postDialString, with the wild char replacement string
312            // at the beginning, followed by the remaining postDialString.
313
314            StringBuilder buf = new StringBuilder(str);
315            buf.append(postDialString.substring(nextPostDialChar));
316            postDialString = buf.toString();
317            nextPostDialChar = 0;
318            if (Phone.DEBUG_PHONE) {
319                log("proceedAfterWildChar: new postDialString is " +
320                        postDialString);
321            }
322
323            processNextPostDialChar();
324        }
325    }
326
327    public void cancelPostDial() {
328        setPostDialState(PostDialState.CANCELLED);
329    }
330
331    /**
332     * Called when this Connection is being hung up locally (eg, user pressed "end")
333     * Note that at this point, the hangup request has been dispatched to the radio
334     * but no response has yet been received so update() has not yet been called
335     */
336    void
337    onHangupLocal() {
338        cause = DisconnectCause.LOCAL;
339    }
340
341    DisconnectCause
342    disconnectCauseFromCode(int causeCode) {
343        /**
344         * See 22.001 Annex F.4 for mapping of cause codes
345         * to local tones
346         */
347
348        switch (causeCode) {
349            case CallFailCause.USER_BUSY:
350                return DisconnectCause.BUSY;
351
352            case CallFailCause.NO_CIRCUIT_AVAIL:
353            case CallFailCause.TEMPORARY_FAILURE:
354            case CallFailCause.SWITCHING_CONGESTION:
355            case CallFailCause.CHANNEL_NOT_AVAIL:
356            case CallFailCause.QOS_NOT_AVAIL:
357            case CallFailCause.BEARER_NOT_AVAIL:
358                return DisconnectCause.CONGESTION;
359
360            case CallFailCause.ACM_LIMIT_EXCEEDED:
361                return DisconnectCause.LIMIT_EXCEEDED;
362
363            case CallFailCause.CALL_BARRED:
364                return DisconnectCause.CALL_BARRED;
365
366            case CallFailCause.FDN_BLOCKED:
367                return DisconnectCause.FDN_BLOCKED;
368
369            case CallFailCause.UNOBTAINABLE_NUMBER:
370                return DisconnectCause.UNOBTAINABLE_NUMBER;
371
372            case CallFailCause.ERROR_UNSPECIFIED:
373            case CallFailCause.NORMAL_CLEARING:
374            default:
375                GSMPhone phone = owner.phone;
376                int serviceState = phone.getServiceState().getState();
377                UiccCardApplication cardApp = UiccController
378                        .getInstance()
379                        .getUiccCardApplication(UiccController.APP_FAM_3GPP);
380                AppState uiccAppState = (cardApp != null) ? cardApp.getState() :
381                                                            AppState.APPSTATE_UNKNOWN;
382                if (serviceState == ServiceState.STATE_POWER_OFF) {
383                    return DisconnectCause.POWER_OFF;
384                } else if (serviceState == ServiceState.STATE_OUT_OF_SERVICE
385                        || serviceState == ServiceState.STATE_EMERGENCY_ONLY ) {
386                    return DisconnectCause.OUT_OF_SERVICE;
387                } else if (uiccAppState != AppState.APPSTATE_READY) {
388                    return DisconnectCause.ICC_ERROR;
389                } else if (causeCode == CallFailCause.ERROR_UNSPECIFIED) {
390                    if (phone.mSST.mRestrictedState.isCsRestricted()) {
391                        return DisconnectCause.CS_RESTRICTED;
392                    } else if (phone.mSST.mRestrictedState.isCsEmergencyRestricted()) {
393                        return DisconnectCause.CS_RESTRICTED_EMERGENCY;
394                    } else if (phone.mSST.mRestrictedState.isCsNormalRestricted()) {
395                        return DisconnectCause.CS_RESTRICTED_NORMAL;
396                    } else {
397                        return DisconnectCause.ERROR_UNSPECIFIED;
398                    }
399                } else if (causeCode == CallFailCause.NORMAL_CLEARING) {
400                    return DisconnectCause.NORMAL;
401                } else {
402                    // If nothing else matches, report unknown call drop reason
403                    // to app, not NORMAL call end.
404                    return DisconnectCause.ERROR_UNSPECIFIED;
405                }
406        }
407    }
408
409    /*package*/ void
410    onRemoteDisconnect(int causeCode) {
411        onDisconnect(disconnectCauseFromCode(causeCode));
412    }
413
414    /** Called when the radio indicates the connection has been disconnected */
415    /*package*/ void
416    onDisconnect(DisconnectCause cause) {
417        this.cause = cause;
418
419        if (!disconnected) {
420            index = -1;
421
422            disconnectTime = System.currentTimeMillis();
423            duration = SystemClock.elapsedRealtime() - connectTimeReal;
424            disconnected = true;
425
426            if (false) Rlog.d(LOG_TAG,
427                    "[GSMConn] onDisconnect: cause=" + cause);
428
429            owner.phone.notifyDisconnect(this);
430
431            if (parent != null) {
432                parent.connectionDisconnected(this);
433            }
434        }
435        releaseWakeLock();
436    }
437
438    // Returns true if state has changed, false if nothing changed
439    /*package*/ boolean
440    update (DriverCall dc) {
441        GsmCall newParent;
442        boolean changed = false;
443        boolean wasConnectingInOrOut = isConnectingInOrOut();
444        boolean wasHolding = (getState() == GsmCall.State.HOLDING);
445
446        newParent = parentFromDCState(dc.state);
447
448        if (!equalsHandlesNulls(address, dc.number)) {
449            if (Phone.DEBUG_PHONE) log("update: phone # changed!");
450            address = dc.number;
451            changed = true;
452        }
453
454        // A null cnapName should be the same as ""
455        if (TextUtils.isEmpty(dc.name)) {
456            if (!TextUtils.isEmpty(cnapName)) {
457                changed = true;
458                cnapName = "";
459            }
460        } else if (!dc.name.equals(cnapName)) {
461            changed = true;
462            cnapName = dc.name;
463        }
464
465        if (Phone.DEBUG_PHONE) log("--dssds----"+cnapName);
466        cnapNamePresentation = dc.namePresentation;
467        numberPresentation = dc.numberPresentation;
468
469        if (newParent != parent) {
470            if (parent != null) {
471                parent.detach(this);
472            }
473            newParent.attach(this, dc);
474            parent = newParent;
475            changed = true;
476        } else {
477            boolean parentStateChange;
478            parentStateChange = parent.update (this, dc);
479            changed = changed || parentStateChange;
480        }
481
482        /** Some state-transition events */
483
484        if (Phone.DEBUG_PHONE) log(
485                "update: parent=" + parent +
486                ", hasNewParent=" + (newParent != parent) +
487                ", wasConnectingInOrOut=" + wasConnectingInOrOut +
488                ", wasHolding=" + wasHolding +
489                ", isConnectingInOrOut=" + isConnectingInOrOut() +
490                ", changed=" + changed);
491
492
493        if (wasConnectingInOrOut && !isConnectingInOrOut()) {
494            onConnectedInOrOut();
495        }
496
497        if (changed && !wasHolding && (getState() == GsmCall.State.HOLDING)) {
498            // We've transitioned into HOLDING
499            onStartedHolding();
500        }
501
502        return changed;
503    }
504
505    /**
506     * Called when this Connection is in the foregroundCall
507     * when a dial is initiated.
508     * We know we're ACTIVE, and we know we're going to end up
509     * HOLDING in the backgroundCall
510     */
511    void
512    fakeHoldBeforeDial() {
513        if (parent != null) {
514            parent.detach(this);
515        }
516
517        parent = owner.backgroundCall;
518        parent.attachFake(this, GsmCall.State.HOLDING);
519
520        onStartedHolding();
521    }
522
523    /*package*/ int
524    getGSMIndex() throws CallStateException {
525        if (index >= 0) {
526            return index + 1;
527        } else {
528            throw new CallStateException ("GSM index not yet assigned");
529        }
530    }
531
532    /**
533     * An incoming or outgoing call has connected
534     */
535    void
536    onConnectedInOrOut() {
537        connectTime = System.currentTimeMillis();
538        connectTimeReal = SystemClock.elapsedRealtime();
539        duration = 0;
540
541        // bug #678474: incoming call interpreted as missed call, even though
542        // it sounds like the user has picked up the call.
543        if (Phone.DEBUG_PHONE) {
544            log("onConnectedInOrOut: connectTime=" + connectTime);
545        }
546
547        if (!isIncoming) {
548            // outgoing calls only
549            processNextPostDialChar();
550        }
551        releaseWakeLock();
552    }
553
554    private void
555    onStartedHolding() {
556        holdingStartTime = SystemClock.elapsedRealtime();
557    }
558    /**
559     * Performs the appropriate action for a post-dial char, but does not
560     * notify application. returns false if the character is invalid and
561     * should be ignored
562     */
563    private boolean
564    processPostDialChar(char c) {
565        if (PhoneNumberUtils.is12Key(c)) {
566            owner.cm.sendDtmf(c, h.obtainMessage(EVENT_DTMF_DONE));
567        } else if (c == PhoneNumberUtils.PAUSE) {
568            // From TS 22.101:
569
570            // "The first occurrence of the "DTMF Control Digits Separator"
571            //  shall be used by the ME to distinguish between the addressing
572            //  digits (i.e. the phone number) and the DTMF digits...."
573
574            if (nextPostDialChar == 1) {
575                // The first occurrence.
576                // We don't need to pause here, but wait for just a bit anyway
577                h.sendMessageDelayed(h.obtainMessage(EVENT_PAUSE_DONE),
578                                            PAUSE_DELAY_FIRST_MILLIS);
579            } else {
580                // It continues...
581                // "Upon subsequent occurrences of the separator, the UE shall
582                //  pause again for 3 seconds (\u00B1 20 %) before sending any
583                //  further DTMF digits."
584                h.sendMessageDelayed(h.obtainMessage(EVENT_PAUSE_DONE),
585                                            PAUSE_DELAY_MILLIS);
586            }
587        } else if (c == PhoneNumberUtils.WAIT) {
588            setPostDialState(PostDialState.WAIT);
589        } else if (c == PhoneNumberUtils.WILD) {
590            setPostDialState(PostDialState.WILD);
591        } else {
592            return false;
593        }
594
595        return true;
596    }
597
598    public String
599    getRemainingPostDialString() {
600        if (postDialState == PostDialState.CANCELLED
601            || postDialState == PostDialState.COMPLETE
602            || postDialString == null
603            || postDialString.length() <= nextPostDialChar
604        ) {
605            return "";
606        }
607
608        return postDialString.substring(nextPostDialChar);
609    }
610
611    @Override
612    protected void finalize()
613    {
614        /**
615         * It is understood that This finializer is not guaranteed
616         * to be called and the release lock call is here just in
617         * case there is some path that doesn't call onDisconnect
618         * and or onConnectedInOrOut.
619         */
620        if (mPartialWakeLock.isHeld()) {
621            Rlog.e(LOG_TAG, "[GSMConn] UNEXPECTED; mPartialWakeLock is held when finalizing.");
622        }
623        releaseWakeLock();
624    }
625
626    private void
627    processNextPostDialChar() {
628        char c = 0;
629        Registrant postDialHandler;
630
631        if (postDialState == PostDialState.CANCELLED) {
632            //Rlog.v("GSM", "##### processNextPostDialChar: postDialState == CANCELLED, bail");
633            return;
634        }
635
636        if (postDialString == null ||
637                postDialString.length() <= nextPostDialChar) {
638            setPostDialState(PostDialState.COMPLETE);
639
640            // notifyMessage.arg1 is 0 on complete
641            c = 0;
642        } else {
643            boolean isValid;
644
645            setPostDialState(PostDialState.STARTED);
646
647            c = postDialString.charAt(nextPostDialChar++);
648
649            isValid = processPostDialChar(c);
650
651            if (!isValid) {
652                // Will call processNextPostDialChar
653                h.obtainMessage(EVENT_NEXT_POST_DIAL).sendToTarget();
654                // Don't notify application
655                Rlog.e("GSM", "processNextPostDialChar: c=" + c + " isn't valid!");
656                return;
657            }
658        }
659
660        postDialHandler = owner.phone.mPostDialHandler;
661
662        Message notifyMessage;
663
664        if (postDialHandler != null
665                && (notifyMessage = postDialHandler.messageForRegistrant()) != null) {
666            // The AsyncResult.result is the Connection object
667            PostDialState state = postDialState;
668            AsyncResult ar = AsyncResult.forMessage(notifyMessage);
669            ar.result = this;
670            ar.userObj = state;
671
672            // arg1 is the character that was/is being processed
673            notifyMessage.arg1 = c;
674
675            //Rlog.v("GSM", "##### processNextPostDialChar: send msg to postDialHandler, arg1=" + c);
676            notifyMessage.sendToTarget();
677        }
678    }
679
680
681    /** "connecting" means "has never been ACTIVE" for both incoming
682     *  and outgoing calls
683     */
684    private boolean
685    isConnectingInOrOut() {
686        return parent == null || parent == owner.ringingCall
687            || parent.state == GsmCall.State.DIALING
688            || parent.state == GsmCall.State.ALERTING;
689    }
690
691    private GsmCall
692    parentFromDCState (DriverCall.State state) {
693        switch (state) {
694            case ACTIVE:
695            case DIALING:
696            case ALERTING:
697                return owner.foregroundCall;
698            //break;
699
700            case HOLDING:
701                return owner.backgroundCall;
702            //break;
703
704            case INCOMING:
705            case WAITING:
706                return owner.ringingCall;
707            //break;
708
709            default:
710                throw new RuntimeException("illegal call state: " + state);
711        }
712    }
713
714    /**
715     * Set post dial state and acquire wake lock while switching to "started"
716     * state, the wake lock will be released if state switches out of "started"
717     * state or after WAKE_LOCK_TIMEOUT_MILLIS.
718     * @param s new PostDialState
719     */
720    private void setPostDialState(PostDialState s) {
721        if (postDialState != PostDialState.STARTED
722                && s == PostDialState.STARTED) {
723            acquireWakeLock();
724            Message msg = h.obtainMessage(EVENT_WAKE_LOCK_TIMEOUT);
725            h.sendMessageDelayed(msg, WAKE_LOCK_TIMEOUT_MILLIS);
726        } else if (postDialState == PostDialState.STARTED
727                && s != PostDialState.STARTED) {
728            h.removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
729            releaseWakeLock();
730        }
731        postDialState = s;
732    }
733
734    private void
735    createWakeLock(Context context) {
736        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
737        mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG);
738    }
739
740    private void
741    acquireWakeLock() {
742        log("acquireWakeLock");
743        mPartialWakeLock.acquire();
744    }
745
746    private void
747    releaseWakeLock() {
748        synchronized(mPartialWakeLock) {
749            if (mPartialWakeLock.isHeld()) {
750                log("releaseWakeLock");
751                mPartialWakeLock.release();
752            }
753        }
754    }
755
756    private void log(String msg) {
757        Rlog.d(LOG_TAG, "[GSMConn] " + msg);
758    }
759
760    @Override
761    public int getNumberPresentation() {
762        return numberPresentation;
763    }
764
765    @Override
766    public UUSInfo getUUSInfo() {
767        return uusInfo;
768    }
769}
770