10825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville/*
20825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * Copyright (C) 2006 The Android Open Source Project
30825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville *
40825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * Licensed under the Apache License, Version 2.0 (the "License");
50825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * you may not use this file except in compliance with the License.
60825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * You may obtain a copy of the License at
70825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville *
80825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville *      http://www.apache.org/licenses/LICENSE-2.0
90825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville *
100825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * Unless required by applicable law or agreed to in writing, software
110825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * distributed under the License is distributed on an "AS IS" BASIS,
120825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * See the License for the specific language governing permissions and
140825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * limitations under the License.
150825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville */
160825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
170825495a331bb44df395a0cdb79fab85e68db5d5Wink Savillepackage com.android.internal.telephony;
180825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
19316deb284df5aff89060007fc7500996dd991ca2Jordan Liuimport android.content.Context;
200825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.os.AsyncResult;
210825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.os.Handler;
220825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.os.Message;
23316deb284df5aff89060007fc7500996dd991ca2Jordan Liuimport android.os.PersistableBundle;
240825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.os.SystemProperties;
25316deb284df5aff89060007fc7500996dd991ca2Jordan Liuimport android.telephony.CarrierConfigManager;
260825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport android.text.TextUtils;
270825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
280825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport java.io.FileDescriptor;
290825495a331bb44df395a0cdb79fab85e68db5d5Wink Savilleimport java.io.PrintWriter;
304be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingamimport java.util.ArrayList;
310825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
320825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
330825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville/**
340825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville * {@hide}
350825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville */
360825495a331bb44df395a0cdb79fab85e68db5d5Wink Savillepublic abstract class CallTracker extends Handler {
370825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
380825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private static final boolean DBG_POLL = false;
390825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
400825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    //***** Constants
410825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
420825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    static final int POLL_DELAY_MSEC = 250;
430825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
4422d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    protected int mPendingOperations;
4522d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    protected boolean mNeedsPoll;
4622d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    protected Message mLastRelevantPoll;
474be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam    protected ArrayList<Connection> mHandoverConnections = new ArrayList<Connection>();
480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
4922d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville    public CommandsInterface mCi;
500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
512d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying    protected boolean mNumberConverted = false;
522d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying    private final int VALID_COMPARE_LENGTH   = 3;
532d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying
540825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    //***** Events
550825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
560825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_POLL_CALLS_RESULT             = 1;
570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_CALL_STATE_CHANGE             = 2;
580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_REPOLL_AFTER_DELAY            = 3;
590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_OPERATION_COMPLETE            = 4;
600825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_GET_LAST_CALL_FAIL_CAUSE      = 5;
610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
620825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_SWITCH_RESULT                 = 8;
630825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_RADIO_AVAILABLE               = 9;
640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_RADIO_NOT_AVAILABLE           = 10;
650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_CONFERENCE_RESULT             = 11;
660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_SEPARATE_RESULT               = 12;
670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_ECT_RESULT                    = 13;
680825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_EXIT_ECM_RESPONSE_CDMA        = 14;
690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_CALL_WAITING_INFO_CDMA        = 15;
700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected static final int EVENT_THREE_WAY_DIAL_L2_RESULT_CDMA = 16;
71bb097a7ba08252c516e578ce30cdb658b63d7c9bJing Zhao    protected static final int EVENT_THREE_WAY_DIAL_BLANK_FLASH    = 20;
720825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
730825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected void pollCallsWhenSafe() {
7422d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        mNeedsPoll = true;
750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
760825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (checkNoOperationsPending()) {
7722d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            mLastRelevantPoll = obtainMessage(EVENT_POLL_CALLS_RESULT);
7822d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville            mCi.getCurrentCalls(mLastRelevantPoll);
790825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
800825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
810825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
820825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected void
830825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    pollCallsAfterDelay() {
840825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        Message msg = obtainMessage();
850825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
860825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        msg.what = EVENT_REPOLL_AFTER_DELAY;
870825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        sendMessageDelayed(msg, POLL_DELAY_MSEC);
880825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
890825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
900825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected boolean
910825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    isCommandExceptionRadioNotAvailable(Throwable e) {
920825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        return e != null && e instanceof CommandException
930825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                && ((CommandException)e).getCommandError()
940825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        == CommandException.Error.RADIO_NOT_AVAILABLE;
950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected abstract void handlePollCalls(AsyncResult ar);
980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
994be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam    protected Connection getHoConnection(DriverCall dc) {
1004be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam        for (Connection hoConn : mHandoverConnections) {
1014be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            log("getHoConnection - compare number: hoConn= " + hoConn.toString());
1024be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            if (hoConn.getAddress() != null && hoConn.getAddress().contains(dc.number)) {
1034be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam                log("getHoConnection: Handover connection match found = " + hoConn.toString());
1044be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam                return hoConn;
1054be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            }
1064be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam        }
1074be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam        for (Connection hoConn : mHandoverConnections) {
1084be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            log("getHoConnection: compare state hoConn= " + hoConn.toString());
1094be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            if (hoConn.getStateBeforeHandover() == Call.stateFromDCState(dc.state)) {
1104be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam                log("getHoConnection: Handover connection match found = " + hoConn.toString());
1114be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam                return hoConn;
1124be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            }
1134be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam        }
1144be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam        return null;
1154be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam    }
1164be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam
1174be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam    protected void notifySrvccState(Call.SrvccState state, ArrayList<Connection> c) {
1184be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam        if (state == Call.SrvccState.STARTED && c != null) {
1194be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            // SRVCC started. Prepare handover connections list
1204be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            mHandoverConnections.addAll(c);
121befe0bf21cf06c47b1f099caed9e6c6b7125c0feShriram Ganesh        } else if (state != Call.SrvccState.COMPLETED) {
1224be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            // SRVCC FAILED/CANCELED. Clear the handover connections list
1234be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            // Individual connections will be removed from the list in handlePollCalls()
1244be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam            mHandoverConnections.clear();
125befe0bf21cf06c47b1f099caed9e6c6b7125c0feShriram Ganesh        }
1264be56374921b52d54b80889540d982f39d26e3abUma Maheswari Ramalingam        log("notifySrvccState: mHandoverConnections= " + mHandoverConnections.toString());
127befe0bf21cf06c47b1f099caed9e6c6b7125c0feShriram Ganesh    }
128befe0bf21cf06c47b1f099caed9e6c6b7125c0feShriram Ganesh
1290825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected void handleRadioAvailable() {
1300825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        pollCallsWhenSafe();
1310825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
1320825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1330825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
1340825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Obtain a complete message that indicates that this operation
1350825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * does not require polling of getCurrentCalls(). However, if other
1360825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * operations that do need getCurrentCalls() are pending or are
1370825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * scheduled while this operation is pending, the invocation
1380825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * of getCurrentCalls() will be postponed until this
1390825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * operation is also complete.
1400825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
1410825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected Message
1420825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    obtainNoPollCompleteMessage(int what) {
14322d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        mPendingOperations++;
14422d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        mLastRelevantPoll = null;
1450825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        return obtainMessage(what);
1460825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
1470825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1480825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
1490825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @return true if we're idle or there's a call to getCurrentCalls() pending
1500825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * but nothing else
1510825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
1520825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    private boolean
1530825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    checkNoOperationsPending() {
1540825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (DBG_POLL) log("checkNoOperationsPending: pendingOperations=" +
15522d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville                mPendingOperations);
15622d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        return mPendingOperations == 0;
1570825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
1580825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
1590825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    /**
1600825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * Routine called from dial to check if the number is a test Emergency number
1610825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * and if so remap the number. This allows a short emergency number to be remapped
1620825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * to a regular number for testing how the frameworks handles emergency numbers
1630825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * without actually calling an emergency number.
1640825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
1650825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * This is not a full test and is not a substitute for testing real emergency
1660825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * numbers but can be useful.
1670825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
1680825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * To use this feature set a system property ril.test.emergencynumber to a pair of
1690825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * numbers separated by a colon. If the first number matches the number parameter
1700825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * this routine returns the second number. Example:
1710825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
1720825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * ril.test.emergencynumber=112:1-123-123-45678
1730825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
1740825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * To test Dial 112 take call then hang up on MO device to enter ECM
1750825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * see RIL#processSolicited RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND
1760825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     *
177cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville     * @param dialString to test if it should be remapped
1780825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     * @return the same number or the remapped number.
1790825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville     */
1800825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected String checkForTestEmergencyNumber(String dialString) {
1810825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        String testEn = SystemProperties.get("ril.test.emergencynumber");
1820825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (DBG_POLL) {
1830825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            log("checkForTestEmergencyNumber: dialString=" + dialString +
1840825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                " testEn=" + testEn);
1850825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
1860825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        if (!TextUtils.isEmpty(testEn)) {
1870825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            String values[] = testEn.split(":");
1880825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            log("checkForTestEmergencyNumber: values.length=" + values.length);
1890825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            if (values.length == 2) {
1900825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                if (values[0].equals(
1910825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                        android.telephony.PhoneNumberUtils.stripSeparators(dialString))) {
192f694b52d7d928b2058abda4b44b05d7dc4ae47c3Tyler Gunn                    // mCi will be null for ImsPhoneCallTracker.
193f694b52d7d928b2058abda4b44b05d7dc4ae47c3Tyler Gunn                    if (mCi != null) {
194f694b52d7d928b2058abda4b44b05d7dc4ae47c3Tyler Gunn                        mCi.testingEmergencyCall();
195f694b52d7d928b2058abda4b44b05d7dc4ae47c3Tyler Gunn                    }
1960825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    log("checkForTestEmergencyNumber: remap " +
1970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                            dialString + " to " + values[1]);
1980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                    dialString = values[1];
1990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville                }
2000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville            }
2010825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        }
2020825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        return dialString;
2030825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
2040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2056c806ef816f8ae0a1849ba05f3128a04577b32d0Amit Mahajan    protected String convertNumberIfNecessary(Phone phone, String dialNumber) {
2062d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        if (dialNumber == null) {
2072d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            return dialNumber;
2082d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        }
209316deb284df5aff89060007fc7500996dd991ca2Jordan Liu        String[] convertMaps = null;
210316deb284df5aff89060007fc7500996dd991ca2Jordan Liu        CarrierConfigManager configManager = (CarrierConfigManager)
211316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                phone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
212316deb284df5aff89060007fc7500996dd991ca2Jordan Liu        PersistableBundle bundle = configManager.getConfig();
213316deb284df5aff89060007fc7500996dd991ca2Jordan Liu        if (bundle != null) {
214316deb284df5aff89060007fc7500996dd991ca2Jordan Liu            convertMaps =
215316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                    bundle.getStringArray(CarrierConfigManager.KEY_DIAL_STRING_REPLACE_STRING_ARRAY);
216316deb284df5aff89060007fc7500996dd991ca2Jordan Liu        }
217316deb284df5aff89060007fc7500996dd991ca2Jordan Liu        if (convertMaps == null) {
218316deb284df5aff89060007fc7500996dd991ca2Jordan Liu            // By default no replacement is necessary
219316deb284df5aff89060007fc7500996dd991ca2Jordan Liu            log("convertNumberIfNecessary convertMaps is null");
220316deb284df5aff89060007fc7500996dd991ca2Jordan Liu            return dialNumber;
221316deb284df5aff89060007fc7500996dd991ca2Jordan Liu        }
222316deb284df5aff89060007fc7500996dd991ca2Jordan Liu
2232d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        log("convertNumberIfNecessary Roaming"
2242d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            + " convertMaps.length " + convertMaps.length
2252d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            + " dialNumber.length() " + dialNumber.length());
2262d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying
2272d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        if (convertMaps.length < 1 || dialNumber.length() < VALID_COMPARE_LENGTH) {
2282d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            return dialNumber;
2292d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        }
2302d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying
2312d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        String[] entry;
2322d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        String outNumber = "";
2332d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        for(String convertMap : convertMaps) {
2342d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            log("convertNumberIfNecessary: " + convertMap);
235316deb284df5aff89060007fc7500996dd991ca2Jordan Liu            // entry format is  "dialStringToReplace:dialStringReplacement"
2362d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            entry = convertMap.split(":");
237316deb284df5aff89060007fc7500996dd991ca2Jordan Liu            if (entry != null && entry.length > 1) {
238316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                String dsToReplace = entry[0];
239316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                String dsReplacement = entry[1];
240316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                if (!TextUtils.isEmpty(dsToReplace) && dialNumber.equals(dsToReplace)) {
241316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                    // Needs to be converted
242316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                    if (!TextUtils.isEmpty(dsReplacement) && dsReplacement.endsWith("MDN")) {
243316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                        String mdn = phone.getLine1Number();
244316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                        if (!TextUtils.isEmpty(mdn)) {
245316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                            if (mdn.startsWith("+")) {
246316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                                outNumber = mdn;
247316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                            } else {
248316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                                outNumber = dsReplacement.substring(0, dsReplacement.length() -3)
249316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                                        + mdn;
250d401e14a4bae195234983d18209dbdcd3155cf56Xia Ying                            }
2512d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying                        }
252316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                    } else {
253316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                        outNumber = dsReplacement;
2542d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying                    }
255316deb284df5aff89060007fc7500996dd991ca2Jordan Liu                    break;
2562d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying                }
2572d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            }
2582d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        }
2592d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying
260d401e14a4bae195234983d18209dbdcd3155cf56Xia Ying        if (!TextUtils.isEmpty(outNumber)) {
2612d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            log("convertNumberIfNecessary: convert service number");
262d401e14a4bae195234983d18209dbdcd3155cf56Xia Ying            mNumberConverted = true;
2632d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            return outNumber;
2642d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        }
2652d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying
2662d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        return dialNumber;
2672d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying
2682d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying    }
2692d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying
2706c806ef816f8ae0a1849ba05f3128a04577b32d0Amit Mahajan    private boolean compareGid1(Phone phone, String serviceGid1) {
2716c806ef816f8ae0a1849ba05f3128a04577b32d0Amit Mahajan        String gid1 = phone.getGroupIdLevel1();
2722d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        int gid_length = serviceGid1.length();
2732d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        boolean ret = true;
2742d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying
2752d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        if (serviceGid1 == null || serviceGid1.equals("")) {
2762d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            log("compareGid1 serviceGid is empty, return " + ret);
2772d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            return ret;
2782d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        }
2792d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        // Check if gid1 match service GID1
2802d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        if (!((gid1 != null) && (gid1.length() >= gid_length) &&
2812d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying                gid1.substring(0, gid_length).equalsIgnoreCase(serviceGid1))) {
2822d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            log(" gid1 " + gid1 + " serviceGid1 " + serviceGid1);
2832d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying            ret = false;
2842d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        }
2852d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        log("compareGid1 is " + (ret?"Same":"Different"));
2862d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying        return ret;
2872d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying    }
2882d265969ca301ba3720458ea9da2ec8d6a18bb8dXia Ying
2890825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    //***** Overridden from Handler
290cbaa45bbf2cab852b6c9c3a887e9f803d4e857eaWink Saville    @Override
2910825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    public abstract void handleMessage (Message msg);
2920825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    public abstract void registerForVoiceCallStarted(Handler h, int what, Object obj);
2930825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    public abstract void unregisterForVoiceCallStarted(Handler h);
2940825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    public abstract void registerForVoiceCallEnded(Handler h, int what, Object obj);
2950825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    public abstract void unregisterForVoiceCallEnded(Handler h);
296b79f845a0451895b0f0b8a926a8571511d476ce8Libin.Tang@motorola.com    public abstract PhoneConstants.State getState();
2970825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    protected abstract void log(String msg);
2980825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville
2990825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3000825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville        pw.println("CallTracker:");
30122d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        pw.println(" mPendingOperations=" + mPendingOperations);
30222d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        pw.println(" mNeedsPoll=" + mNeedsPoll);
30322d85a8e3a575a6d01d2c788587971657dfe20c6Wink Saville        pw.println(" mLastRelevantPoll=" + mLastRelevantPoll);
3040825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville    }
3050825495a331bb44df395a0cdb79fab85e68db5d5Wink Saville}
306