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