CDMAPhone.java revision 8c3879362ce5a7a7c8151ec415e4d394812df357
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 android.app.ActivityManagerNative;
20import android.content.ContentValues;
21import android.content.Context;
22import android.content.Intent;
23import android.content.SharedPreferences;
24import android.database.SQLException;
25import android.net.Uri;
26import android.os.AsyncResult;
27import android.os.Handler;
28import android.os.Message;
29import android.os.PowerManager;
30import android.os.PowerManager.WakeLock;
31import android.os.Registrant;
32import android.os.RegistrantList;
33import android.os.SystemProperties;
34import android.os.UserHandle;
35import android.preference.PreferenceManager;
36import android.provider.Settings;
37import android.provider.Telephony;
38import android.telephony.CellLocation;
39import android.telephony.PhoneNumberUtils;
40import android.telephony.ServiceState;
41import android.telephony.SubscriptionManager;
42import android.telephony.cdma.CdmaCellLocation;
43import android.text.TextUtils;
44import android.telephony.Rlog;
45
46import com.android.internal.telephony.Call;
47import com.android.internal.telephony.CallStateException;
48import com.android.internal.telephony.CallTracker;
49import com.android.internal.telephony.CommandException;
50import com.android.internal.telephony.CommandsInterface;
51import com.android.internal.telephony.Connection;
52import com.android.internal.telephony.IccPhoneBookInterfaceManager;
53import com.android.internal.telephony.IccSmsInterfaceManager;
54import com.android.internal.telephony.MccTable;
55import com.android.internal.telephony.MmiCode;
56import com.android.internal.telephony.OperatorInfo;
57import com.android.internal.telephony.PhoneBase;
58import com.android.internal.telephony.PhoneConstants;
59import com.android.internal.telephony.PhoneNotifier;
60import com.android.internal.telephony.PhoneProxy;
61import com.android.internal.telephony.PhoneSubInfo;
62import com.android.internal.telephony.ServiceStateTracker;
63import com.android.internal.telephony.SmsBroadcastUndelivered;
64import com.android.internal.telephony.TelephonyIntents;
65import com.android.internal.telephony.TelephonyProperties;
66import com.android.internal.telephony.UUSInfo;
67import com.android.internal.telephony.dataconnection.DcTracker;
68import com.android.internal.telephony.imsphone.ImsPhone;
69import com.android.internal.telephony.uicc.IccException;
70import com.android.internal.telephony.uicc.IccRecords;
71import com.android.internal.telephony.uicc.RuimRecords;
72import com.android.internal.telephony.uicc.UiccCard;
73import com.android.internal.telephony.uicc.UiccCardApplication;
74import com.android.internal.telephony.uicc.UiccController;
75import java.io.FileDescriptor;
76import java.io.PrintWriter;
77import java.util.ArrayList;
78import java.util.List;
79import java.util.regex.Matcher;
80import java.util.regex.Pattern;
81
82import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
83import static com.android.internal.telephony.TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA;
84import static com.android.internal.telephony.TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY;
85import static com.android.internal.telephony.TelephonyProperties.PROPERTY_ICC_OPERATOR_NUMERIC;
86
87/**
88 * {@hide}
89 */
90public class CDMAPhone extends PhoneBase {
91    static final String LOG_TAG = "CDMAPhone";
92    private static final boolean DBG = true;
93    private static final boolean VDBG = false; /* STOP SHIP if true */
94
95    // Default Emergency Callback Mode exit timer
96    private static final int DEFAULT_ECM_EXIT_TIMER_VALUE = 300000;
97
98    static final String VM_COUNT_CDMA = "vm_count_key_cdma";
99    private static final String VM_NUMBER_CDMA = "vm_number_key_cdma";
100    private String mVmNumber = null;
101
102    static final int RESTART_ECM_TIMER = 0; // restart Ecm timer
103    static final int CANCEL_ECM_TIMER = 1; // cancel Ecm timer
104
105    // Instance Variables
106    CdmaCallTracker mCT;
107    CdmaServiceStateTracker mSST;
108    CdmaSubscriptionSourceManager mCdmaSSM;
109    ArrayList <CdmaMmiCode> mPendingMmis = new ArrayList<CdmaMmiCode>();
110    RuimPhoneBookInterfaceManager mRuimPhoneBookInterfaceManager;
111    int mCdmaSubscriptionSource =
112            CdmaSubscriptionSourceManager.SUBSCRIPTION_SOURCE_UNKNOWN;
113    PhoneSubInfo mSubInfo;
114    EriManager mEriManager;
115    WakeLock mWakeLock;
116
117    // mEriFileLoadedRegistrants are informed after the ERI text has been loaded
118    private final RegistrantList mEriFileLoadedRegistrants = new RegistrantList();
119
120    // mEcmTimerResetRegistrants are informed after Ecm timer is canceled or re-started
121    private final RegistrantList mEcmTimerResetRegistrants = new RegistrantList();
122
123    // mEcmExitRespRegistrant is informed after the phone has been exited
124    //the emergency callback mode
125    //keep track of if phone is in emergency callback mode
126    protected boolean mIsPhoneInEcmState;
127    private Registrant mEcmExitRespRegistrant;
128    protected String mImei;
129    protected String mImeiSv;
130    private String mEsn;
131    private String mMeid;
132    // string to define how the carrier specifies its own ota sp number
133    protected String mCarrierOtaSpNumSchema;
134
135    // A runnable which is used to automatically exit from Ecm after a period of time.
136    private Runnable mExitEcmRunnable = new Runnable() {
137        @Override
138        public void run() {
139            exitEmergencyCallbackMode();
140        }
141    };
142
143    Registrant mPostDialHandler;
144
145    static String PROPERTY_CDMA_HOME_OPERATOR_NUMERIC = "ro.cdma.home.operator.numeric";
146
147    // Constructors
148    public CDMAPhone(Context context, CommandsInterface ci, PhoneNotifier notifier) {
149        super("CDMA", notifier, context, ci, false);
150        initSstIcc();
151        init(context, notifier);
152    }
153
154    public CDMAPhone(Context context, CommandsInterface ci, PhoneNotifier notifier,
155            int phoneId) {
156        super("CDMA", notifier, context, ci, false, phoneId);
157        initSstIcc();
158        init(context, notifier);
159    }
160
161    public CDMAPhone(Context context, CommandsInterface ci, PhoneNotifier notifier,
162            boolean unitTestMode) {
163        super("CDMA", notifier, context, ci, unitTestMode);
164        initSstIcc();
165        init(context, notifier);
166    }
167
168    protected void initSstIcc() {
169        mSST = new CdmaServiceStateTracker(this);
170    }
171
172    protected void init(Context context, PhoneNotifier notifier) {
173        mCi.setPhoneType(PhoneConstants.PHONE_TYPE_CDMA);
174        mCT = new CdmaCallTracker(this);
175        mCdmaSSM = CdmaSubscriptionSourceManager.getInstance(context, mCi, this,
176                EVENT_CDMA_SUBSCRIPTION_SOURCE_CHANGED, null);
177        mDcTracker = new DcTracker(this);
178        mRuimPhoneBookInterfaceManager = new RuimPhoneBookInterfaceManager(this);
179        mSubInfo = new PhoneSubInfo(this);
180        mEriManager = new EriManager(this, context, EriManager.ERI_FROM_XML);
181
182        mCi.registerForAvailable(this, EVENT_RADIO_AVAILABLE, null);
183        mCi.registerForOffOrNotAvailable(this, EVENT_RADIO_OFF_OR_NOT_AVAILABLE, null);
184        mCi.registerForOn(this, EVENT_RADIO_ON, null);
185        mCi.setOnSuppServiceNotification(this, EVENT_SSN, null);
186        mSST.registerForNetworkAttached(this, EVENT_REGISTERED_TO_NETWORK, null);
187        mCi.setEmergencyCallbackMode(this, EVENT_EMERGENCY_CALLBACK_MODE_ENTER, null);
188        mCi.registerForExitEmergencyCallbackMode(this, EVENT_EXIT_EMERGENCY_CALLBACK_RESPONSE,
189                null);
190
191        PowerManager pm
192            = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
193        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,LOG_TAG);
194
195        //Change the system setting
196        SystemProperties.set(TelephonyProperties.CURRENT_ACTIVE_PHONE,
197                Integer.toString(PhoneConstants.PHONE_TYPE_CDMA));
198
199        // This is needed to handle phone process crashes
200        String inEcm=SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE, "false");
201        mIsPhoneInEcmState = inEcm.equals("true");
202        if (mIsPhoneInEcmState) {
203            // Send a message which will invoke handleExitEmergencyCallbackMode
204            mCi.exitEmergencyCallbackMode(obtainMessage(EVENT_EXIT_EMERGENCY_CALLBACK_RESPONSE));
205        }
206
207        // get the string that specifies the carrier OTA Sp number
208        mCarrierOtaSpNumSchema = SystemProperties.get(
209                TelephonyProperties.PROPERTY_OTASP_NUM_SCHEMA,"");
210
211        // Sets operator properties by retrieving from build-time system property
212        String operatorAlpha = SystemProperties.get("ro.cdma.home.operator.alpha");
213        String operatorNumeric = SystemProperties.get(PROPERTY_CDMA_HOME_OPERATOR_NUMERIC);
214        log("init: operatorAlpha='" + operatorAlpha
215                + "' operatorNumeric='" + operatorNumeric + "'");
216        if (mUiccController.getUiccCardApplication(mPhoneId, UiccController.APP_FAM_3GPP) == null) {
217            log("init: APP_FAM_3GPP == NULL");
218            if (!TextUtils.isEmpty(operatorAlpha)) {
219                log("init: set 'gsm.sim.operator.alpha' to operator='" + operatorAlpha + "'");
220                setSystemProperty(PROPERTY_ICC_OPERATOR_ALPHA, operatorAlpha);
221            }
222            if (!TextUtils.isEmpty(operatorNumeric)) {
223                log("init: set 'gsm.sim.operator.numeric' to operator='" + operatorNumeric + "'");
224                log("update icc_operator_numeric=" + operatorNumeric);
225                setSystemProperty(PROPERTY_ICC_OPERATOR_NUMERIC, operatorNumeric);
226            }
227            setIsoCountryProperty(operatorNumeric);
228        }
229
230        // Sets current entry in the telephony carrier table
231        updateCurrentCarrierInProvider(operatorNumeric);
232
233        // Notify voicemails.
234        notifier.notifyMessageWaitingChanged(this);
235    }
236
237    @Override
238    public void dispose() {
239        synchronized(PhoneProxy.lockForRadioTechnologyChange) {
240            super.dispose();
241            log("dispose");
242
243            //Unregister from all former registered events
244            unregisterForRuimRecordEvents();
245            mCi.unregisterForAvailable(this); //EVENT_RADIO_AVAILABLE
246            mCi.unregisterForOffOrNotAvailable(this); //EVENT_RADIO_OFF_OR_NOT_AVAILABLE
247            mCi.unregisterForOn(this); //EVENT_RADIO_ON
248            mSST.unregisterForNetworkAttached(this); //EVENT_REGISTERED_TO_NETWORK
249            mCi.unSetOnSuppServiceNotification(this);
250            mCi.unregisterForExitEmergencyCallbackMode(this);
251            removeCallbacks(mExitEcmRunnable);
252
253            mPendingMmis.clear();
254
255            //Force all referenced classes to unregister their former registered events
256            mCT.dispose();
257            mDcTracker.dispose();
258            mSST.dispose();
259            mCdmaSSM.dispose(this);
260            mRuimPhoneBookInterfaceManager.dispose();
261            mSubInfo.dispose();
262            mEriManager.dispose();
263        }
264    }
265
266    @Override
267    public void removeReferences() {
268        log("removeReferences");
269        mRuimPhoneBookInterfaceManager = null;
270        mSubInfo = null;
271        mCT = null;
272        mSST = null;
273        mEriManager = null;
274        mExitEcmRunnable = null;
275
276        super.removeReferences();
277    }
278
279    @Override
280    protected void finalize() {
281        if(DBG) Rlog.d(LOG_TAG, "CDMAPhone finalized");
282        if (mWakeLock.isHeld()) {
283            Rlog.e(LOG_TAG, "UNEXPECTED; mWakeLock is held when finalizing.");
284            mWakeLock.release();
285        }
286    }
287
288    @Override
289    public ServiceState getServiceState() {
290        return mSST.mSS;
291    }
292
293    @Override
294    public CallTracker getCallTracker() {
295        return mCT;
296    }
297
298    @Override
299    public PhoneConstants.State getState() {
300        return mCT.mState;
301    }
302
303    @Override
304    public ServiceStateTracker getServiceStateTracker() {
305        return mSST;
306    }
307
308    @Override
309    public int getPhoneType() {
310        return PhoneConstants.PHONE_TYPE_CDMA;
311    }
312
313    @Override
314    public boolean canTransfer() {
315        Rlog.e(LOG_TAG, "canTransfer: not possible in CDMA");
316        return false;
317    }
318
319    @Override
320    public Call getRingingCall() {
321        ImsPhone imPhone = mImsPhone;
322        if ( mCT.mRingingCall != null && mCT.mRingingCall.isRinging() ) {
323            return mCT.mRingingCall;
324        } else if ( imPhone != null ) {
325            return imPhone.getRingingCall();
326        }
327        return mCT.mRingingCall;
328    }
329
330    @Override
331    public void setMute(boolean muted) {
332        mCT.setMute(muted);
333    }
334
335    @Override
336    public boolean getMute() {
337        return mCT.getMute();
338    }
339
340    @Override
341    public void conference() {
342        // three way calls in CDMA will be handled by feature codes
343        Rlog.e(LOG_TAG, "conference: not possible in CDMA");
344    }
345
346    @Override
347    public void enableEnhancedVoicePrivacy(boolean enable, Message onComplete) {
348        mCi.setPreferredVoicePrivacy(enable, onComplete);
349    }
350
351    @Override
352    public void getEnhancedVoicePrivacy(Message onComplete) {
353        mCi.getPreferredVoicePrivacy(onComplete);
354    }
355
356    @Override
357    public void clearDisconnected() {
358        mCT.clearDisconnected();
359    }
360
361    @Override
362    public DataActivityState getDataActivityState() {
363        DataActivityState ret = DataActivityState.NONE;
364
365        if (mSST.getCurrentDataConnectionState() == ServiceState.STATE_IN_SERVICE) {
366
367            switch (mDcTracker.getActivity()) {
368                case DATAIN:
369                    ret = DataActivityState.DATAIN;
370                break;
371
372                case DATAOUT:
373                    ret = DataActivityState.DATAOUT;
374                break;
375
376                case DATAINANDOUT:
377                    ret = DataActivityState.DATAINANDOUT;
378                break;
379
380                case DORMANT:
381                    ret = DataActivityState.DORMANT;
382                break;
383
384                default:
385                    ret = DataActivityState.NONE;
386                break;
387            }
388        }
389        return ret;
390    }
391
392    @Override
393    public Connection
394    dial (String dialString, int videoState) throws CallStateException {
395        ImsPhone imsPhone = mImsPhone;
396
397        boolean imsUseEnabled = SystemProperties.getInt(
398                TelephonyProperties.PROPERTY_DBG_IMS_VOLTE_ENABLE,
399                TelephonyProperties.PROPERTY_DBG_IMS_VOLTE_ENABLE_DEAFULT) == 1;
400        if (!imsUseEnabled) {
401            Rlog.w(LOG_TAG, "IMS is disabled: forced to CS");
402        }
403
404        if (imsUseEnabled && imsPhone != null
405                && ((imsPhone.getServiceState().getState() == ServiceState.STATE_IN_SERVICE
406                && !PhoneNumberUtils.isEmergencyNumber(dialString))
407                || (PhoneNumberUtils.isEmergencyNumber(dialString)
408                && mContext.getResources().getBoolean(
409                        com.android.internal.R.bool.useImsAlwaysForEmergencyCall))) ) {
410            try {
411                if (DBG) Rlog.d(LOG_TAG, "Trying IMS PS call");
412                return imsPhone.dial(dialString, videoState);
413            } catch (CallStateException e) {
414                if (DBG) Rlog.d(LOG_TAG, "IMS PS call exception " + e);
415                if (!ImsPhone.CS_FALLBACK.equals(e.getMessage())) {
416                    CallStateException ce = new CallStateException(e.getMessage());
417                    ce.setStackTrace(e.getStackTrace());
418                    throw ce;
419                }
420            }
421        }
422
423        if (DBG) Rlog.d(LOG_TAG, "Trying (non-IMS) CS call");
424        return dialInternal(dialString, null, videoState);
425    }
426
427
428    @Override
429    protected Connection
430    dialInternal (String dialString, UUSInfo uusInfo,
431            int videoState) throws CallStateException {
432        // Need to make sure dialString gets parsed properly
433        String newDialString = PhoneNumberUtils.stripSeparators(dialString);
434        return mCT.dial(newDialString);
435    }
436
437    @Override
438    public Connection dial(String dialString, UUSInfo uusInfo, int videoState)
439            throws CallStateException {
440        throw new CallStateException("Sending UUS information NOT supported in CDMA!");
441    }
442
443    @Override
444    public boolean
445    getMessageWaitingIndicator() {
446        return (getVoiceMessageCount() > 0);
447    }
448
449    @Override
450    public List<? extends MmiCode>
451    getPendingMmiCodes() {
452        return mPendingMmis;
453    }
454
455    @Override
456    public void registerForSuppServiceNotification(
457            Handler h, int what, Object obj) {
458        Rlog.e(LOG_TAG, "method registerForSuppServiceNotification is NOT supported in CDMA!");
459    }
460
461    @Override
462    public CdmaCall getBackgroundCall() {
463        return mCT.mBackgroundCall;
464    }
465
466    @Override
467    public boolean handleInCallMmiCommands(String dialString) {
468        Rlog.e(LOG_TAG, "method handleInCallMmiCommands is NOT supported in CDMA!");
469        return false;
470    }
471
472    boolean isInCall() {
473        CdmaCall.State foregroundCallState = getForegroundCall().getState();
474        CdmaCall.State backgroundCallState = getBackgroundCall().getState();
475        CdmaCall.State ringingCallState = getRingingCall().getState();
476
477        return (foregroundCallState.isAlive() || backgroundCallState.isAlive() || ringingCallState
478                .isAlive());
479    }
480
481    @Override
482    public void unregisterForSuppServiceNotification(Handler h) {
483        Rlog.e(LOG_TAG, "method unregisterForSuppServiceNotification is NOT supported in CDMA!");
484    }
485
486    @Override
487    public void
488    acceptCall(int videoState) throws CallStateException {
489        ImsPhone imsPhone = mImsPhone;
490        if ( imsPhone != null && imsPhone.getRingingCall().isRinging() ) {
491            imsPhone.acceptCall(videoState);
492        } else {
493            mCT.acceptCall();
494        }
495    }
496
497    @Override
498    public void
499    rejectCall() throws CallStateException {
500        mCT.rejectCall();
501    }
502
503    @Override
504    public void
505    switchHoldingAndActive() throws CallStateException {
506        mCT.switchWaitingOrHoldingAndActive();
507    }
508
509    @Override
510    public String getIccSerialNumber() {
511        IccRecords r = mIccRecords.get();
512        if (r == null) {
513            // to get ICCID form SIMRecords because it is on MF.
514            r = mUiccController.getIccRecords(mPhoneId, UiccController.APP_FAM_3GPP);
515        }
516        return (r != null) ? r.getIccId() : null;
517    }
518
519    @Override
520    public String getLine1Number() {
521        return mSST.getMdnNumber();
522    }
523
524    @Override
525    public String getCdmaPrlVersion(){
526        return mSST.getPrlVersion();
527    }
528
529    @Override
530    public String getCdmaMin() {
531        return mSST.getCdmaMin();
532    }
533
534    @Override
535    public boolean isMinInfoReady() {
536        return mSST.isMinInfoReady();
537    }
538
539    @Override
540    public void getCallWaiting(Message onComplete) {
541        mCi.queryCallWaiting(CommandsInterface.SERVICE_CLASS_VOICE, onComplete);
542    }
543
544    @Override
545    public void
546    setRadioPower(boolean power) {
547        mSST.setRadioPower(power);
548    }
549
550    @Override
551    public String getEsn() {
552        return mEsn;
553    }
554
555    @Override
556    public String getMeid() {
557        return mMeid;
558    }
559
560    //returns MEID or ESN in CDMA
561    @Override
562    public String getDeviceId() {
563        String id = getMeid();
564        if ((id == null) || id.matches("^0*$")) {
565            Rlog.d(LOG_TAG, "getDeviceId(): MEID is not initialized use ESN");
566            id = getEsn();
567        }
568        return id;
569    }
570
571    @Override
572    public String getDeviceSvn() {
573        Rlog.d(LOG_TAG, "getDeviceSvn(): return 0");
574        return "0";
575    }
576
577    @Override
578    public String getSubscriberId() {
579        return mSST.getImsi();
580    }
581
582    @Override
583    public String getGroupIdLevel1() {
584        Rlog.e(LOG_TAG, "GID1 is not available in CDMA");
585        return null;
586    }
587
588    @Override
589    public String getImei() {
590        Rlog.e(LOG_TAG, "IMEI is not available in CDMA");
591        return null;
592    }
593
594    @Override
595    public boolean canConference() {
596        Rlog.e(LOG_TAG, "canConference: not possible in CDMA");
597        return false;
598    }
599
600    @Override
601    public CellLocation getCellLocation() {
602        CdmaCellLocation loc = mSST.mCellLoc;
603
604        int mode = Settings.Secure.getInt(getContext().getContentResolver(),
605                Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
606        if (mode == Settings.Secure.LOCATION_MODE_OFF) {
607            // clear lat/long values for location privacy
608            CdmaCellLocation privateLoc = new CdmaCellLocation();
609            privateLoc.setCellLocationData(loc.getBaseStationId(),
610                    CdmaCellLocation.INVALID_LAT_LONG,
611                    CdmaCellLocation.INVALID_LAT_LONG,
612                    loc.getSystemId(), loc.getNetworkId());
613            loc = privateLoc;
614        }
615        return loc;
616    }
617
618    @Override
619    public CdmaCall getForegroundCall() {
620        return mCT.mForegroundCall;
621    }
622
623    @Override
624    public void setOnPostDialCharacter(Handler h, int what, Object obj) {
625        mPostDialHandler = new Registrant(h, what, obj);
626    }
627
628    @Override
629    public boolean handlePinMmi(String dialString) {
630        CdmaMmiCode mmi = CdmaMmiCode.newFromDialString(dialString, this, mUiccApplication.get());
631
632        if (mmi == null) {
633            Rlog.e(LOG_TAG, "Mmi is NULL!");
634            return false;
635        } else if (mmi.isPinPukCommand()) {
636            mPendingMmis.add(mmi);
637            mMmiRegistrants.notifyRegistrants(new AsyncResult(null, mmi, null));
638            mmi.processCode();
639            return true;
640        }
641        Rlog.e(LOG_TAG, "Unrecognized mmi!");
642        return false;
643    }
644
645    /**
646     * Removes the given MMI from the pending list and notifies registrants that
647     * it is complete.
648     *
649     * @param mmi MMI that is done
650     */
651    void onMMIDone(CdmaMmiCode mmi) {
652        /*
653         * Only notify complete if it's on the pending list. Otherwise, it's
654         * already been handled (eg, previously canceled).
655         */
656        if (mPendingMmis.remove(mmi)) {
657            mMmiCompleteRegistrants.notifyRegistrants(new AsyncResult(null, mmi, null));
658        }
659    }
660
661    @Override
662    public void setLine1Number(String alphaTag, String number, Message onComplete) {
663        Rlog.e(LOG_TAG, "setLine1Number: not possible in CDMA");
664    }
665
666    @Override
667    public void setCallWaiting(boolean enable, Message onComplete) {
668        Rlog.e(LOG_TAG, "method setCallWaiting is NOT supported in CDMA!");
669    }
670
671    @Override
672    public void updateServiceLocation() {
673        mSST.enableSingleLocationUpdate();
674    }
675
676    @Override
677    public void setDataRoamingEnabled(boolean enable) {
678        mDcTracker.setDataOnRoamingEnabled(enable);
679    }
680
681    @Override
682    public void registerForCdmaOtaStatusChange(Handler h, int what, Object obj) {
683        mCi.registerForCdmaOtaProvision(h, what, obj);
684    }
685
686    @Override
687    public void unregisterForCdmaOtaStatusChange(Handler h) {
688        mCi.unregisterForCdmaOtaProvision(h);
689    }
690
691    @Override
692    public void registerForSubscriptionInfoReady(Handler h, int what, Object obj) {
693        mSST.registerForSubscriptionInfoReady(h, what, obj);
694    }
695
696    @Override
697    public void unregisterForSubscriptionInfoReady(Handler h) {
698        mSST.unregisterForSubscriptionInfoReady(h);
699    }
700
701    @Override
702    public void setOnEcbModeExitResponse(Handler h, int what, Object obj) {
703        mEcmExitRespRegistrant = new Registrant (h, what, obj);
704    }
705
706    @Override
707    public void unsetOnEcbModeExitResponse(Handler h) {
708        mEcmExitRespRegistrant.clear();
709    }
710
711    @Override
712    public void registerForCallWaiting(Handler h, int what, Object obj) {
713        mCT.registerForCallWaiting(h, what, obj);
714    }
715
716    @Override
717    public void unregisterForCallWaiting(Handler h) {
718        mCT.unregisterForCallWaiting(h);
719    }
720
721    @Override
722    public void
723    getNeighboringCids(Message response) {
724        /*
725         * This is currently not implemented.  At least as of June
726         * 2009, there is no neighbor cell information available for
727         * CDMA because some party is resisting making this
728         * information readily available.  Consequently, calling this
729         * function can have no useful effect.  This situation may
730         * (and hopefully will) change in the future.
731         */
732        if (response != null) {
733            CommandException ce = new CommandException(
734                    CommandException.Error.REQUEST_NOT_SUPPORTED);
735            AsyncResult.forMessage(response).exception = ce;
736            response.sendToTarget();
737        }
738    }
739
740    @Override
741    public PhoneConstants.DataState getDataConnectionState(String apnType) {
742        PhoneConstants.DataState ret = PhoneConstants.DataState.DISCONNECTED;
743
744        if (mSST == null) {
745             // Radio Technology Change is ongoning, dispose() and removeReferences() have
746             // already been called
747
748             ret = PhoneConstants.DataState.DISCONNECTED;
749        } else if (mSST.getCurrentDataConnectionState() != ServiceState.STATE_IN_SERVICE) {
750            // If we're out of service, open TCP sockets may still work
751            // but no data will flow
752            ret = PhoneConstants.DataState.DISCONNECTED;
753        } else if (mDcTracker.isApnTypeEnabled(apnType) == false ||
754                mDcTracker.isApnTypeActive(apnType) == false) {
755            ret = PhoneConstants.DataState.DISCONNECTED;
756        } else {
757            switch (mDcTracker.getState(apnType)) {
758                case RETRYING:
759                case FAILED:
760                case IDLE:
761                    ret = PhoneConstants.DataState.DISCONNECTED;
762                break;
763
764                case CONNECTED:
765                case DISCONNECTING:
766                    if ( mCT.mState != PhoneConstants.State.IDLE
767                            && !mSST.isConcurrentVoiceAndDataAllowed()) {
768                        ret = PhoneConstants.DataState.SUSPENDED;
769                    } else {
770                        ret = PhoneConstants.DataState.CONNECTED;
771                    }
772                break;
773
774                case CONNECTING:
775                case SCANNING:
776                    ret = PhoneConstants.DataState.CONNECTING;
777                break;
778            }
779        }
780
781        log("getDataConnectionState apnType=" + apnType + " ret=" + ret);
782        return ret;
783    }
784
785    @Override
786    public void sendUssdResponse(String ussdMessge) {
787        Rlog.e(LOG_TAG, "sendUssdResponse: not possible in CDMA");
788    }
789
790    @Override
791    public void sendDtmf(char c) {
792        if (!PhoneNumberUtils.is12Key(c)) {
793            Rlog.e(LOG_TAG,
794                    "sendDtmf called with invalid character '" + c + "'");
795        } else {
796            if (mCT.mState ==  PhoneConstants.State.OFFHOOK) {
797                mCi.sendDtmf(c, null);
798            }
799        }
800    }
801
802    @Override
803    public void startDtmf(char c) {
804        if (!PhoneNumberUtils.is12Key(c)) {
805            Rlog.e(LOG_TAG,
806                    "startDtmf called with invalid character '" + c + "'");
807        } else {
808            mCi.startDtmf(c, null);
809        }
810    }
811
812    @Override
813    public void stopDtmf() {
814        mCi.stopDtmf(null);
815    }
816
817    @Override
818    public void sendBurstDtmf(String dtmfString, int on, int off, Message onComplete) {
819        boolean check = true;
820        for (int itr = 0;itr < dtmfString.length(); itr++) {
821            if (!PhoneNumberUtils.is12Key(dtmfString.charAt(itr))) {
822                Rlog.e(LOG_TAG,
823                        "sendDtmf called with invalid character '" + dtmfString.charAt(itr)+ "'");
824                check = false;
825                break;
826            }
827        }
828        if ((mCT.mState ==  PhoneConstants.State.OFFHOOK)&&(check)) {
829            mCi.sendBurstDtmf(dtmfString, on, off, onComplete);
830        }
831     }
832
833    @Override
834    public void getAvailableNetworks(Message response) {
835        Rlog.e(LOG_TAG, "getAvailableNetworks: not possible in CDMA");
836    }
837
838    @Override
839    public void setOutgoingCallerIdDisplay(int commandInterfaceCLIRMode, Message onComplete) {
840        Rlog.e(LOG_TAG, "setOutgoingCallerIdDisplay: not possible in CDMA");
841    }
842
843    @Override
844    public void enableLocationUpdates() {
845        mSST.enableLocationUpdates();
846    }
847
848    @Override
849    public void disableLocationUpdates() {
850        mSST.disableLocationUpdates();
851    }
852
853    @Override
854    public void getDataCallList(Message response) {
855        mCi.getDataCallList(response);
856    }
857
858    @Override
859    public boolean getDataRoamingEnabled() {
860        return mDcTracker.getDataOnRoamingEnabled();
861    }
862
863    @Override
864    public void setDataEnabled(boolean enable) {
865        mDcTracker.setDataEnabled(enable);
866    }
867
868    @Override
869    public boolean getDataEnabled() {
870        return mDcTracker.getDataEnabled();
871    }
872
873    @Override
874    public void setVoiceMailNumber(String alphaTag,
875                                   String voiceMailNumber,
876                                   Message onComplete) {
877        Message resp;
878        mVmNumber = voiceMailNumber;
879        resp = obtainMessage(EVENT_SET_VM_NUMBER_DONE, 0, 0, onComplete);
880        IccRecords r = mIccRecords.get();
881        if (r != null) {
882            r.setVoiceMailNumber(alphaTag, mVmNumber, resp);
883        }
884    }
885
886    @Override
887    public String getVoiceMailNumber() {
888        String number = null;
889        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
890        // TODO: The default value of voicemail number should be read from a system property
891
892        // Read platform settings for dynamic voicemail number
893        if (getContext().getResources().getBoolean(com.android.internal
894                .R.bool.config_telephony_use_own_number_for_voicemail)) {
895            number = sp.getString(VM_NUMBER_CDMA + getPhoneId(), getLine1Number());
896        } else {
897            number = sp.getString(VM_NUMBER_CDMA + getPhoneId(), "*86");
898        }
899        return number;
900    }
901
902    /* Returns Number of Voicemails
903     * @hide
904     */
905    @Override
906    public int getVoiceMessageCount() {
907        IccRecords r = mIccRecords.get();
908        int voicemailCount =  (r != null) ? r.getVoiceMessageCount() : 0;
909        // If mRuimRecords.getVoiceMessageCount returns zero, then there is possibility
910        // that phone was power cycled and would have lost the voicemail count.
911        // So get the count from preferences.
912        if (voicemailCount == 0) {
913            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
914            voicemailCount = sp.getInt(VM_COUNT_CDMA, 0);
915        }
916        return voicemailCount;
917    }
918
919    @Override
920    public String getVoiceMailAlphaTag() {
921        // TODO: Where can we get this value has to be clarified with QC.
922        String ret = "";//TODO: Remove = "", if we know where to get this value.
923
924        //ret = mSIMRecords.getVoiceMailAlphaTag();
925
926        if (ret == null || ret.length() == 0) {
927            return mContext.getText(
928                com.android.internal.R.string.defaultVoiceMailAlphaTag).toString();
929        }
930
931        return ret;
932    }
933
934    @Override
935    public void getCallForwardingOption(int commandInterfaceCFReason, Message onComplete) {
936        Rlog.e(LOG_TAG, "getCallForwardingOption: not possible in CDMA");
937    }
938
939    @Override
940    public void setCallForwardingOption(int commandInterfaceCFAction,
941            int commandInterfaceCFReason,
942            String dialingNumber,
943            int timerSeconds,
944            Message onComplete) {
945        Rlog.e(LOG_TAG, "setCallForwardingOption: not possible in CDMA");
946    }
947
948    @Override
949    public void
950    getOutgoingCallerIdDisplay(Message onComplete) {
951        Rlog.e(LOG_TAG, "getOutgoingCallerIdDisplay: not possible in CDMA");
952    }
953
954    @Override
955    public boolean
956    getCallForwardingIndicator() {
957        Rlog.e(LOG_TAG, "getCallForwardingIndicator: not possible in CDMA");
958        return false;
959    }
960
961    @Override
962    public void explicitCallTransfer() {
963        Rlog.e(LOG_TAG, "explicitCallTransfer: not possible in CDMA");
964    }
965
966    @Override
967    public String getLine1AlphaTag() {
968        Rlog.e(LOG_TAG, "getLine1AlphaTag: not possible in CDMA");
969        return null;
970    }
971
972    /**
973     * Notify any interested party of a Phone state change
974     * {@link com.android.internal.telephony.PhoneConstants.State}
975     */
976    /*package*/ void notifyPhoneStateChanged() {
977        mNotifier.notifyPhoneState(this);
978    }
979
980    /**
981     * Notify registrants of a change in the call state. This notifies changes in
982     * {@link com.android.internal.telephony.Call.State}. Use this when changes
983     * in the precise call state are needed, else use notifyPhoneStateChanged.
984     */
985    /*package*/ void notifyPreciseCallStateChanged() {
986        /* we'd love it if this was package-scoped*/
987        super.notifyPreciseCallStateChangedP();
988    }
989
990     void notifyServiceStateChanged(ServiceState ss) {
991         super.notifyServiceStateChangedP(ss);
992     }
993
994     void notifyLocationChanged() {
995         mNotifier.notifyCellLocation(this);
996     }
997
998    public void notifyNewRingingConnection(Connection c) {
999        super.notifyNewRingingConnectionP(c);
1000    }
1001
1002    /*package*/ void notifyDisconnect(Connection cn) {
1003        mDisconnectRegistrants.notifyResult(cn);
1004
1005        mNotifier.notifyDisconnectCause(cn.getDisconnectCause(), cn.getPreciseDisconnectCause());
1006    }
1007
1008    void notifyUnknownConnection() {
1009        mUnknownConnectionRegistrants.notifyResult(this);
1010    }
1011
1012    @Override
1013    public boolean isInEmergencyCall() {
1014        return mCT.isInEmergencyCall();
1015    }
1016
1017    @Override
1018    public boolean isInEcm() {
1019        return mIsPhoneInEcmState;
1020    }
1021
1022    void sendEmergencyCallbackModeChange(){
1023        //Send an Intent
1024        Intent intent = new Intent(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
1025        intent.putExtra(PhoneConstants.PHONE_IN_ECM_STATE, mIsPhoneInEcmState);
1026        SubscriptionManager.putPhoneIdAndSubIdExtra(intent, getPhoneId());
1027        ActivityManagerNative.broadcastStickyIntent(intent,null,UserHandle.USER_ALL);
1028        if (DBG) Rlog.d(LOG_TAG, "sendEmergencyCallbackModeChange");
1029    }
1030
1031    @Override
1032    public void exitEmergencyCallbackMode() {
1033        if (mWakeLock.isHeld()) {
1034            mWakeLock.release();
1035        }
1036        // Send a message which will invoke handleExitEmergencyCallbackMode
1037        mCi.exitEmergencyCallbackMode(obtainMessage(EVENT_EXIT_EMERGENCY_CALLBACK_RESPONSE));
1038    }
1039
1040    private void handleEnterEmergencyCallbackMode(Message msg) {
1041        if (DBG) {
1042            Rlog.d(LOG_TAG, "handleEnterEmergencyCallbackMode,mIsPhoneInEcmState= "
1043                    + mIsPhoneInEcmState);
1044        }
1045        // if phone is not in Ecm mode, and it's changed to Ecm mode
1046        if (mIsPhoneInEcmState == false) {
1047            mIsPhoneInEcmState = true;
1048            // notify change
1049            sendEmergencyCallbackModeChange();
1050            setSystemProperty(TelephonyProperties.PROPERTY_INECM_MODE, "true");
1051
1052            // Post this runnable so we will automatically exit
1053            // if no one invokes exitEmergencyCallbackMode() directly.
1054            long delayInMillis = SystemProperties.getLong(
1055                    TelephonyProperties.PROPERTY_ECM_EXIT_TIMER, DEFAULT_ECM_EXIT_TIMER_VALUE);
1056            postDelayed(mExitEcmRunnable, delayInMillis);
1057            // We don't want to go to sleep while in Ecm
1058            mWakeLock.acquire();
1059        }
1060    }
1061
1062    private void handleExitEmergencyCallbackMode(Message msg) {
1063        AsyncResult ar = (AsyncResult)msg.obj;
1064        if (DBG) {
1065            Rlog.d(LOG_TAG, "handleExitEmergencyCallbackMode,ar.exception , mIsPhoneInEcmState "
1066                    + ar.exception + mIsPhoneInEcmState);
1067        }
1068        // Remove pending exit Ecm runnable, if any
1069        removeCallbacks(mExitEcmRunnable);
1070
1071        if (mEcmExitRespRegistrant != null) {
1072            mEcmExitRespRegistrant.notifyRegistrant(ar);
1073        }
1074        // if exiting ecm success
1075        if (ar.exception == null) {
1076            if (mIsPhoneInEcmState) {
1077                mIsPhoneInEcmState = false;
1078                setSystemProperty(TelephonyProperties.PROPERTY_INECM_MODE, "false");
1079            }
1080            // send an Intent
1081            sendEmergencyCallbackModeChange();
1082            // Re-initiate data connection
1083            mDcTracker.setInternalDataEnabled(true);
1084        }
1085    }
1086
1087    /**
1088     * Handle to cancel or restart Ecm timer in emergency call back mode
1089     * if action is CANCEL_ECM_TIMER, cancel Ecm timer and notify apps the timer is canceled;
1090     * otherwise, restart Ecm timer and notify apps the timer is restarted.
1091     */
1092    void handleTimerInEmergencyCallbackMode(int action) {
1093        switch(action) {
1094        case CANCEL_ECM_TIMER:
1095            removeCallbacks(mExitEcmRunnable);
1096            mEcmTimerResetRegistrants.notifyResult(Boolean.TRUE);
1097            break;
1098        case RESTART_ECM_TIMER:
1099            long delayInMillis = SystemProperties.getLong(
1100                    TelephonyProperties.PROPERTY_ECM_EXIT_TIMER, DEFAULT_ECM_EXIT_TIMER_VALUE);
1101            postDelayed(mExitEcmRunnable, delayInMillis);
1102            mEcmTimerResetRegistrants.notifyResult(Boolean.FALSE);
1103            break;
1104        default:
1105            Rlog.e(LOG_TAG, "handleTimerInEmergencyCallbackMode, unsupported action " + action);
1106        }
1107    }
1108
1109    /**
1110     * Registration point for Ecm timer reset
1111     * @param h handler to notify
1112     * @param what User-defined message code
1113     * @param obj placed in Message.obj
1114     */
1115    @Override
1116    public void registerForEcmTimerReset(Handler h, int what, Object obj) {
1117        mEcmTimerResetRegistrants.addUnique(h, what, obj);
1118    }
1119
1120    @Override
1121    public void unregisterForEcmTimerReset(Handler h) {
1122        mEcmTimerResetRegistrants.remove(h);
1123    }
1124
1125    @Override
1126    public void handleMessage(Message msg) {
1127        AsyncResult ar;
1128        Message     onComplete;
1129
1130        if (!mIsTheCurrentActivePhone) {
1131            Rlog.e(LOG_TAG, "Received message " + msg +
1132                    "[" + msg.what + "] while being destroyed. Ignoring.");
1133            return;
1134        }
1135        switch(msg.what) {
1136            case EVENT_RADIO_AVAILABLE: {
1137                mCi.getBasebandVersion(obtainMessage(EVENT_GET_BASEBAND_VERSION_DONE));
1138
1139                mCi.getDeviceIdentity(obtainMessage(EVENT_GET_DEVICE_IDENTITY_DONE));
1140            }
1141            break;
1142
1143            case EVENT_GET_BASEBAND_VERSION_DONE:{
1144                ar = (AsyncResult)msg.obj;
1145
1146                if (ar.exception != null) {
1147                    break;
1148                }
1149
1150                if (DBG) Rlog.d(LOG_TAG, "Baseband version: " + ar.result);
1151                setSystemProperty(TelephonyProperties.PROPERTY_BASEBAND_VERSION, (String)ar.result);
1152            }
1153            break;
1154
1155            case EVENT_GET_DEVICE_IDENTITY_DONE:{
1156                ar = (AsyncResult)msg.obj;
1157
1158                if (ar.exception != null) {
1159                    break;
1160                }
1161                String[] respId = (String[])ar.result;
1162                mImei = respId[0];
1163                mImeiSv = respId[1];
1164                mEsn  =  respId[2];
1165                mMeid =  respId[3];
1166            }
1167            break;
1168
1169            case EVENT_EMERGENCY_CALLBACK_MODE_ENTER:{
1170                handleEnterEmergencyCallbackMode(msg);
1171            }
1172            break;
1173
1174            case EVENT_ICC_RECORD_EVENTS:
1175                ar = (AsyncResult)msg.obj;
1176                processIccRecordEvents((Integer)ar.result);
1177                break;
1178
1179            case  EVENT_EXIT_EMERGENCY_CALLBACK_RESPONSE:{
1180                handleExitEmergencyCallbackMode(msg);
1181            }
1182            break;
1183
1184            case EVENT_RUIM_RECORDS_LOADED:{
1185                Rlog.d(LOG_TAG, "Event EVENT_RUIM_RECORDS_LOADED Received");
1186                updateCurrentCarrierInProvider();
1187            }
1188            break;
1189
1190            case EVENT_RADIO_OFF_OR_NOT_AVAILABLE:{
1191                Rlog.d(LOG_TAG, "Event EVENT_RADIO_OFF_OR_NOT_AVAILABLE Received");
1192                ImsPhone imsPhone = mImsPhone;
1193                if (imsPhone != null) {
1194                    imsPhone.getServiceState().setStateOff();
1195                }
1196            }
1197            break;
1198
1199            case EVENT_RADIO_ON:{
1200                Rlog.d(LOG_TAG, "Event EVENT_RADIO_ON Received");
1201                handleCdmaSubscriptionSource(mCdmaSSM.getCdmaSubscriptionSource());
1202                ImsPhone imsPhone = mImsPhone;
1203                if (imsPhone != null) {
1204                    imsPhone.getServiceState().setStateOutOfService();
1205                }
1206            }
1207            break;
1208
1209            case EVENT_CDMA_SUBSCRIPTION_SOURCE_CHANGED:{
1210                Rlog.d(LOG_TAG, "EVENT_CDMA_SUBSCRIPTION_SOURCE_CHANGED");
1211                handleCdmaSubscriptionSource(mCdmaSSM.getCdmaSubscriptionSource());
1212            }
1213            break;
1214
1215            case EVENT_SSN:{
1216                Rlog.d(LOG_TAG, "Event EVENT_SSN Received");
1217            }
1218            break;
1219
1220            case EVENT_REGISTERED_TO_NETWORK:{
1221                Rlog.d(LOG_TAG, "Event EVENT_REGISTERED_TO_NETWORK Received");
1222            }
1223            break;
1224
1225            case EVENT_NV_READY:{
1226                Rlog.d(LOG_TAG, "Event EVENT_NV_READY Received");
1227                prepareEri();
1228            }
1229            break;
1230
1231            case EVENT_SET_VM_NUMBER_DONE:{
1232                ar = (AsyncResult)msg.obj;
1233                if (IccException.class.isInstance(ar.exception)) {
1234                    storeVoiceMailNumber(mVmNumber);
1235                    ar.exception = null;
1236                }
1237                onComplete = (Message) ar.userObj;
1238                if (onComplete != null) {
1239                    AsyncResult.forMessage(onComplete, ar.result, ar.exception);
1240                    onComplete.sendToTarget();
1241                }
1242            }
1243            break;
1244
1245            default:{
1246                super.handleMessage(msg);
1247            }
1248        }
1249    }
1250
1251    protected UiccCardApplication getUiccCardApplication() {
1252        return  mUiccController.getUiccCardApplication(mPhoneId, UiccController.APP_FAM_3GPP2);
1253    }
1254
1255    @Override
1256    protected void onUpdateIccAvailability() {
1257        if (mUiccController == null ) {
1258            return;
1259        }
1260
1261        UiccCardApplication newUiccApplication = getUiccCardApplication();
1262
1263        if (newUiccApplication == null) {
1264            log("can't find 3GPP2 application; trying APP_FAM_3GPP");
1265            newUiccApplication =
1266                    mUiccController.getUiccCardApplication(mPhoneId, UiccController.APP_FAM_3GPP);
1267        }
1268
1269        UiccCardApplication app = mUiccApplication.get();
1270        if (app != newUiccApplication) {
1271            if (app != null) {
1272                log("Removing stale icc objects.");
1273                if (mIccRecords.get() != null) {
1274                    unregisterForRuimRecordEvents();
1275                }
1276                mIccRecords.set(null);
1277                mUiccApplication.set(null);
1278            }
1279            if (newUiccApplication != null) {
1280                log("New Uicc application found");
1281                mUiccApplication.set(newUiccApplication);
1282                mIccRecords.set(newUiccApplication.getIccRecords());
1283                registerForRuimRecordEvents();
1284            }
1285        }
1286    }
1287
1288    private void processIccRecordEvents(int eventCode) {
1289        switch (eventCode) {
1290            case RuimRecords.EVENT_MWI:
1291                notifyMessageWaitingIndicator();
1292                break;
1293
1294            default:
1295                Rlog.e(LOG_TAG,"Unknown icc records event code " + eventCode);
1296                break;
1297        }
1298    }
1299
1300    /**
1301     * Handles the call to get the subscription source
1302     *
1303     * @param newSubscriptionSource holds the new CDMA subscription source value
1304     */
1305    private void handleCdmaSubscriptionSource(int newSubscriptionSource) {
1306        if (newSubscriptionSource != mCdmaSubscriptionSource) {
1307             mCdmaSubscriptionSource = newSubscriptionSource;
1308             if (newSubscriptionSource == CDMA_SUBSCRIPTION_NV) {
1309                 // NV is ready when subscription source is NV
1310                 sendMessage(obtainMessage(EVENT_NV_READY));
1311             }
1312        }
1313    }
1314
1315    /**
1316     * Retrieves the PhoneSubInfo of the CDMAPhone
1317     */
1318    @Override
1319    public PhoneSubInfo getPhoneSubInfo() {
1320        return mSubInfo;
1321    }
1322
1323    /**
1324     * Retrieves the IccPhoneBookInterfaceManager of the CDMAPhone
1325     */
1326    @Override
1327    public IccPhoneBookInterfaceManager getIccPhoneBookInterfaceManager() {
1328        return mRuimPhoneBookInterfaceManager;
1329    }
1330
1331    public void registerForEriFileLoaded(Handler h, int what, Object obj) {
1332        Registrant r = new Registrant (h, what, obj);
1333        mEriFileLoadedRegistrants.add(r);
1334    }
1335
1336    public void unregisterForEriFileLoaded(Handler h) {
1337        mEriFileLoadedRegistrants.remove(h);
1338    }
1339
1340    // override for allowing access from other classes of this package
1341    /**
1342     * {@inheritDoc}
1343     */
1344    @Override
1345    public void setSystemProperty(String property, String value) {
1346        super.setSystemProperty(property, value);
1347    }
1348
1349    /**
1350     * Activate or deactivate cell broadcast SMS.
1351     *
1352     * @param activate 0 = activate, 1 = deactivate
1353     * @param response Callback message is empty on completion
1354     */
1355    @Override
1356    public void activateCellBroadcastSms(int activate, Message response) {
1357        Rlog.e(LOG_TAG, "[CDMAPhone] activateCellBroadcastSms() is obsolete; use SmsManager");
1358        response.sendToTarget();
1359    }
1360
1361    /**
1362     * Query the current configuration of cdma cell broadcast SMS.
1363     *
1364     * @param response Callback message is empty on completion
1365     */
1366    @Override
1367    public void getCellBroadcastSmsConfig(Message response) {
1368        Rlog.e(LOG_TAG, "[CDMAPhone] getCellBroadcastSmsConfig() is obsolete; use SmsManager");
1369        response.sendToTarget();
1370    }
1371
1372    /**
1373     * Configure cdma cell broadcast SMS.
1374     *
1375     * @param response Callback message is empty on completion
1376     */
1377    @Override
1378    public void setCellBroadcastSmsConfig(int[] configValuesArray, Message response) {
1379        Rlog.e(LOG_TAG, "[CDMAPhone] setCellBroadcastSmsConfig() is obsolete; use SmsManager");
1380        response.sendToTarget();
1381    }
1382
1383    /**
1384     * Returns true if OTA Service Provisioning needs to be performed.
1385     */
1386    @Override
1387    public boolean needsOtaServiceProvisioning() {
1388        return mSST.getOtasp() != ServiceStateTracker.OTASP_NOT_NEEDED;
1389    }
1390
1391    private static final String IS683A_FEATURE_CODE = "*228";
1392    private static final int IS683A_FEATURE_CODE_NUM_DIGITS = 4;
1393    private static final int IS683A_SYS_SEL_CODE_NUM_DIGITS = 2;
1394    private static final int IS683A_SYS_SEL_CODE_OFFSET = 4;
1395
1396    private static final int IS683_CONST_800MHZ_A_BAND = 0;
1397    private static final int IS683_CONST_800MHZ_B_BAND = 1;
1398    private static final int IS683_CONST_1900MHZ_A_BLOCK = 2;
1399    private static final int IS683_CONST_1900MHZ_B_BLOCK = 3;
1400    private static final int IS683_CONST_1900MHZ_C_BLOCK = 4;
1401    private static final int IS683_CONST_1900MHZ_D_BLOCK = 5;
1402    private static final int IS683_CONST_1900MHZ_E_BLOCK = 6;
1403    private static final int IS683_CONST_1900MHZ_F_BLOCK = 7;
1404    private static final int INVALID_SYSTEM_SELECTION_CODE = -1;
1405
1406    private static boolean isIs683OtaSpDialStr(String dialStr) {
1407        int sysSelCodeInt;
1408        boolean isOtaspDialString = false;
1409        int dialStrLen = dialStr.length();
1410
1411        if (dialStrLen == IS683A_FEATURE_CODE_NUM_DIGITS) {
1412            if (dialStr.equals(IS683A_FEATURE_CODE)) {
1413                isOtaspDialString = true;
1414            }
1415        } else {
1416            sysSelCodeInt = extractSelCodeFromOtaSpNum(dialStr);
1417            switch (sysSelCodeInt) {
1418                case IS683_CONST_800MHZ_A_BAND:
1419                case IS683_CONST_800MHZ_B_BAND:
1420                case IS683_CONST_1900MHZ_A_BLOCK:
1421                case IS683_CONST_1900MHZ_B_BLOCK:
1422                case IS683_CONST_1900MHZ_C_BLOCK:
1423                case IS683_CONST_1900MHZ_D_BLOCK:
1424                case IS683_CONST_1900MHZ_E_BLOCK:
1425                case IS683_CONST_1900MHZ_F_BLOCK:
1426                    isOtaspDialString = true;
1427                    break;
1428                default:
1429                    break;
1430            }
1431        }
1432        return isOtaspDialString;
1433    }
1434    /**
1435     * This function extracts the system selection code from the dial string.
1436     */
1437    private static int extractSelCodeFromOtaSpNum(String dialStr) {
1438        int dialStrLen = dialStr.length();
1439        int sysSelCodeInt = INVALID_SYSTEM_SELECTION_CODE;
1440
1441        if ((dialStr.regionMatches(0, IS683A_FEATURE_CODE,
1442                                   0, IS683A_FEATURE_CODE_NUM_DIGITS)) &&
1443            (dialStrLen >= (IS683A_FEATURE_CODE_NUM_DIGITS +
1444                            IS683A_SYS_SEL_CODE_NUM_DIGITS))) {
1445                // Since we checked the condition above, the system selection code
1446                // extracted from dialStr will not cause any exception
1447                sysSelCodeInt = Integer.parseInt (
1448                                dialStr.substring (IS683A_FEATURE_CODE_NUM_DIGITS,
1449                                IS683A_FEATURE_CODE_NUM_DIGITS + IS683A_SYS_SEL_CODE_NUM_DIGITS));
1450        }
1451        if (DBG) Rlog.d(LOG_TAG, "extractSelCodeFromOtaSpNum " + sysSelCodeInt);
1452        return sysSelCodeInt;
1453    }
1454
1455    /**
1456     * This function checks if the system selection code extracted from
1457     * the dial string "sysSelCodeInt' is the system selection code specified
1458     * in the carrier ota sp number schema "sch".
1459     */
1460    private static boolean
1461    checkOtaSpNumBasedOnSysSelCode (int sysSelCodeInt, String sch[]) {
1462        boolean isOtaSpNum = false;
1463        try {
1464            // Get how many number of system selection code ranges
1465            int selRc = Integer.parseInt(sch[1]);
1466            for (int i = 0; i < selRc; i++) {
1467                if (!TextUtils.isEmpty(sch[i+2]) && !TextUtils.isEmpty(sch[i+3])) {
1468                    int selMin = Integer.parseInt(sch[i+2]);
1469                    int selMax = Integer.parseInt(sch[i+3]);
1470                    // Check if the selection code extracted from the dial string falls
1471                    // within any of the range pairs specified in the schema.
1472                    if ((sysSelCodeInt >= selMin) && (sysSelCodeInt <= selMax)) {
1473                        isOtaSpNum = true;
1474                        break;
1475                    }
1476                }
1477            }
1478        } catch (NumberFormatException ex) {
1479            // If the carrier ota sp number schema is not correct, we still allow dial
1480            // and only log the error:
1481            Rlog.e(LOG_TAG, "checkOtaSpNumBasedOnSysSelCode, error", ex);
1482        }
1483        return isOtaSpNum;
1484    }
1485
1486    // Define the pattern/format for carrier specified OTASP number schema.
1487    // It separates by comma and/or whitespace.
1488    private static Pattern pOtaSpNumSchema = Pattern.compile("[,\\s]+");
1489
1490    /**
1491     * The following function checks if a dial string is a carrier specified
1492     * OTASP number or not by checking against the OTASP number schema stored
1493     * in PROPERTY_OTASP_NUM_SCHEMA.
1494     *
1495     * Currently, there are 2 schemas for carriers to specify the OTASP number:
1496     * 1) Use system selection code:
1497     *    The schema is:
1498     *    SELC,the # of code pairs,min1,max1,min2,max2,...
1499     *    e.g "SELC,3,10,20,30,40,60,70" indicates that there are 3 pairs of
1500     *    selection codes, and they are {10,20}, {30,40} and {60,70} respectively.
1501     *
1502     * 2) Use feature code:
1503     *    The schema is:
1504     *    "FC,length of feature code,feature code".
1505     *     e.g "FC,2,*2" indicates that the length of the feature code is 2,
1506     *     and the code itself is "*2".
1507     */
1508    private boolean isCarrierOtaSpNum(String dialStr) {
1509        boolean isOtaSpNum = false;
1510        int sysSelCodeInt = extractSelCodeFromOtaSpNum(dialStr);
1511        if (sysSelCodeInt == INVALID_SYSTEM_SELECTION_CODE) {
1512            return isOtaSpNum;
1513        }
1514        // mCarrierOtaSpNumSchema is retrieved from PROPERTY_OTASP_NUM_SCHEMA:
1515        if (!TextUtils.isEmpty(mCarrierOtaSpNumSchema)) {
1516            Matcher m = pOtaSpNumSchema.matcher(mCarrierOtaSpNumSchema);
1517            if (DBG) {
1518                Rlog.d(LOG_TAG, "isCarrierOtaSpNum,schema" + mCarrierOtaSpNumSchema);
1519            }
1520
1521            if (m.find()) {
1522                String sch[] = pOtaSpNumSchema.split(mCarrierOtaSpNumSchema);
1523                // If carrier uses system selection code mechanism
1524                if (!TextUtils.isEmpty(sch[0]) && sch[0].equals("SELC")) {
1525                    if (sysSelCodeInt!=INVALID_SYSTEM_SELECTION_CODE) {
1526                        isOtaSpNum=checkOtaSpNumBasedOnSysSelCode(sysSelCodeInt,sch);
1527                    } else {
1528                        if (DBG) {
1529                            Rlog.d(LOG_TAG, "isCarrierOtaSpNum,sysSelCodeInt is invalid");
1530                        }
1531                    }
1532                } else if (!TextUtils.isEmpty(sch[0]) && sch[0].equals("FC")) {
1533                    int fcLen =  Integer.parseInt(sch[1]);
1534                    String fc = sch[2];
1535                    if (dialStr.regionMatches(0,fc,0,fcLen)) {
1536                        isOtaSpNum = true;
1537                    } else {
1538                        if (DBG) Rlog.d(LOG_TAG, "isCarrierOtaSpNum,not otasp number");
1539                    }
1540                } else {
1541                    if (DBG) {
1542                        Rlog.d(LOG_TAG, "isCarrierOtaSpNum,ota schema not supported" + sch[0]);
1543                    }
1544                }
1545            } else {
1546                if (DBG) {
1547                    Rlog.d(LOG_TAG, "isCarrierOtaSpNum,ota schema pattern not right" +
1548                          mCarrierOtaSpNumSchema);
1549                }
1550            }
1551        } else {
1552            if (DBG) Rlog.d(LOG_TAG, "isCarrierOtaSpNum,ota schema pattern empty");
1553        }
1554        return isOtaSpNum;
1555    }
1556
1557    /**
1558     * isOTASPNumber: checks a given number against the IS-683A OTASP dial string and carrier
1559     * OTASP dial string.
1560     *
1561     * @param dialStr the number to look up.
1562     * @return true if the number is in IS-683A OTASP dial string or carrier OTASP dial string
1563     */
1564    @Override
1565    public  boolean isOtaSpNumber(String dialStr){
1566        boolean isOtaSpNum = false;
1567        String dialableStr = PhoneNumberUtils.extractNetworkPortionAlt(dialStr);
1568        if (dialableStr != null) {
1569            isOtaSpNum = isIs683OtaSpDialStr(dialableStr);
1570            if (isOtaSpNum == false) {
1571                isOtaSpNum = isCarrierOtaSpNum(dialableStr);
1572            }
1573        }
1574        if (DBG) Rlog.d(LOG_TAG, "isOtaSpNumber " + isOtaSpNum);
1575        return isOtaSpNum;
1576    }
1577
1578    @Override
1579    public int getCdmaEriIconIndex() {
1580        return getServiceState().getCdmaEriIconIndex();
1581    }
1582
1583    /**
1584     * Returns the CDMA ERI icon mode,
1585     * 0 - ON
1586     * 1 - FLASHING
1587     */
1588    @Override
1589    public int getCdmaEriIconMode() {
1590        return getServiceState().getCdmaEriIconMode();
1591    }
1592
1593    /**
1594     * Returns the CDMA ERI text,
1595     */
1596    @Override
1597    public String getCdmaEriText() {
1598        int roamInd = getServiceState().getCdmaRoamingIndicator();
1599        int defRoamInd = getServiceState().getCdmaDefaultRoamingIndicator();
1600        return mEriManager.getCdmaEriText(roamInd, defRoamInd);
1601    }
1602
1603    /**
1604     * Store the voicemail number in preferences
1605     */
1606    private void storeVoiceMailNumber(String number) {
1607        // Update the preference value of voicemail number
1608        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
1609        SharedPreferences.Editor editor = sp.edit();
1610        editor.putString(VM_NUMBER_CDMA + getPhoneId(), number);
1611        editor.apply();
1612    }
1613
1614    /**
1615     * Sets PROPERTY_ICC_OPERATOR_ISO_COUNTRY property
1616     *
1617     */
1618    protected void setIsoCountryProperty(String operatorNumeric) {
1619        if (TextUtils.isEmpty(operatorNumeric)) {
1620            log("setIsoCountryProperty: clear 'gsm.sim.operator.iso-country'");
1621            setSystemProperty(PROPERTY_ICC_OPERATOR_ISO_COUNTRY, "");
1622        } else {
1623            String iso = "";
1624            try {
1625                iso = MccTable.countryCodeForMcc(Integer.parseInt(
1626                        operatorNumeric.substring(0,3)));
1627            } catch (NumberFormatException ex) {
1628                loge("setIsoCountryProperty: countryCodeForMcc error", ex);
1629            } catch (StringIndexOutOfBoundsException ex) {
1630                loge("setIsoCountryProperty: countryCodeForMcc error", ex);
1631            }
1632
1633            log("setIsoCountryProperty: set 'gsm.sim.operator.iso-country' to iso=" + iso);
1634            setSystemProperty(PROPERTY_ICC_OPERATOR_ISO_COUNTRY, iso);
1635        }
1636    }
1637
1638    /**
1639     * Sets the "current" field in the telephony provider according to the
1640     * build-time operator numeric property
1641     *
1642     * @return true for success; false otherwise.
1643     */
1644    boolean updateCurrentCarrierInProvider(String operatorNumeric) {
1645        log("CDMAPhone: updateCurrentCarrierInProvider called");
1646        if (!TextUtils.isEmpty(operatorNumeric)) {
1647            try {
1648                Uri uri = Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current");
1649                ContentValues map = new ContentValues();
1650                map.put(Telephony.Carriers.NUMERIC, operatorNumeric);
1651                log("updateCurrentCarrierInProvider from system: numeric=" + operatorNumeric);
1652                getContext().getContentResolver().insert(uri, map);
1653
1654                // Updates MCC MNC device configuration information
1655                log("update mccmnc=" + operatorNumeric);
1656                MccTable.updateMccMncConfiguration(mContext, operatorNumeric, false);
1657
1658                return true;
1659            } catch (SQLException e) {
1660                Rlog.e(LOG_TAG, "Can't store current operator", e);
1661            }
1662        }
1663        return false;
1664    }
1665
1666    /**
1667     * Sets the "current" field in the telephony provider according to the SIM's operator.
1668     * Implemented in {@link CDMALTEPhone} for CDMA/LTE devices.
1669     *
1670     * @return true for success; false otherwise.
1671     */
1672    boolean updateCurrentCarrierInProvider() {
1673        return true;
1674    }
1675
1676    public void prepareEri() {
1677        if (mEriManager == null) {
1678            Rlog.e(LOG_TAG, "PrepareEri: Trying to access stale objects");
1679            return;
1680        }
1681        mEriManager.loadEriFile();
1682        if(mEriManager.isEriFileLoaded()) {
1683            // when the ERI file is loaded
1684            log("ERI read, notify registrants");
1685            mEriFileLoadedRegistrants.notifyRegistrants();
1686        }
1687    }
1688
1689    public boolean isEriFileLoaded() {
1690        return mEriManager.isEriFileLoaded();
1691    }
1692
1693    protected void registerForRuimRecordEvents() {
1694        IccRecords r = mIccRecords.get();
1695        if (r == null) {
1696            return;
1697        }
1698        r.registerForRecordsEvents(this, EVENT_ICC_RECORD_EVENTS, null);
1699        r.registerForRecordsLoaded(this, EVENT_RUIM_RECORDS_LOADED, null);
1700    }
1701
1702    protected void unregisterForRuimRecordEvents() {
1703        IccRecords r = mIccRecords.get();
1704        if (r == null) {
1705            return;
1706        }
1707        r.unregisterForRecordsEvents(this);
1708        r.unregisterForRecordsLoaded(this);
1709    }
1710
1711    protected void log(String s) {
1712        if (DBG)
1713            Rlog.d(LOG_TAG, s);
1714    }
1715
1716    protected void loge(String s, Exception e) {
1717        if (DBG)
1718            Rlog.e(LOG_TAG, s, e);
1719    }
1720
1721    @Override
1722    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1723        pw.println("CDMAPhone extends:");
1724        super.dump(fd, pw, args);
1725        pw.println(" mVmNumber=" + mVmNumber);
1726        pw.println(" mCT=" + mCT);
1727        pw.println(" mSST=" + mSST);
1728        pw.println(" mCdmaSSM=" + mCdmaSSM);
1729        pw.println(" mPendingMmis=" + mPendingMmis);
1730        pw.println(" mRuimPhoneBookInterfaceManager=" + mRuimPhoneBookInterfaceManager);
1731        pw.println(" mCdmaSubscriptionSource=" + mCdmaSubscriptionSource);
1732        pw.println(" mSubInfo=" + mSubInfo);
1733        pw.println(" mEriManager=" + mEriManager);
1734        pw.println(" mWakeLock=" + mWakeLock);
1735        pw.println(" mIsPhoneInEcmState=" + mIsPhoneInEcmState);
1736        if (VDBG) pw.println(" mImei=" + mImei);
1737        if (VDBG) pw.println(" mImeiSv=" + mImeiSv);
1738        if (VDBG) pw.println(" mEsn=" + mEsn);
1739        if (VDBG) pw.println(" mMeid=" + mMeid);
1740        pw.println(" mCarrierOtaSpNumSchema=" + mCarrierOtaSpNumSchema);
1741        pw.println(" getCdmaEriIconIndex()=" + getCdmaEriIconIndex());
1742        pw.println(" getCdmaEriIconMode()=" + getCdmaEriIconMode());
1743        pw.println(" getCdmaEriText()=" + getCdmaEriText());
1744        pw.println(" isMinInfoReady()=" + isMinInfoReady());
1745        pw.println(" isCspPlmnEnabled()=" + isCspPlmnEnabled());
1746    }
1747
1748    @Override
1749    public boolean setOperatorBrandOverride(String iccId, String brand) {
1750        if (mUiccController == null) {
1751            return false;
1752        }
1753
1754        UiccCard card = mUiccController.getUiccCard();
1755        if (card == null) {
1756            return false;
1757        }
1758
1759        boolean status = card.setOperatorBrandOverride(iccId, brand);
1760
1761        // Refresh.
1762        if (status) {
1763            IccRecords iccRecords = mIccRecords.get();
1764            if (iccRecords != null) {
1765                SystemProperties.set(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA,
1766                        iccRecords.getServiceProviderName());
1767            }
1768            if (mSST != null) {
1769                mSST.pollState();
1770            }
1771        }
1772        return status;
1773    }
1774}
1775