CDMAPhone.java revision 04e36a78936967df68d9175ac09dd3f087727888
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    public void notifyEcbmTimerReset(Boolean flag) {
1110        mEcmTimerResetRegistrants.notifyResult(flag);
1111    }
1112
1113    /**
1114     * Registration point for Ecm timer reset
1115     * @param h handler to notify
1116     * @param what User-defined message code
1117     * @param obj placed in Message.obj
1118     */
1119    @Override
1120    public void registerForEcmTimerReset(Handler h, int what, Object obj) {
1121        mEcmTimerResetRegistrants.addUnique(h, what, obj);
1122    }
1123
1124    @Override
1125    public void unregisterForEcmTimerReset(Handler h) {
1126        mEcmTimerResetRegistrants.remove(h);
1127    }
1128
1129    @Override
1130    public void handleMessage(Message msg) {
1131        AsyncResult ar;
1132        Message     onComplete;
1133
1134        if (!mIsTheCurrentActivePhone) {
1135            Rlog.e(LOG_TAG, "Received message " + msg +
1136                    "[" + msg.what + "] while being destroyed. Ignoring.");
1137            return;
1138        }
1139        switch(msg.what) {
1140            case EVENT_RADIO_AVAILABLE: {
1141                mCi.getBasebandVersion(obtainMessage(EVENT_GET_BASEBAND_VERSION_DONE));
1142
1143                mCi.getDeviceIdentity(obtainMessage(EVENT_GET_DEVICE_IDENTITY_DONE));
1144            }
1145            break;
1146
1147            case EVENT_GET_BASEBAND_VERSION_DONE:{
1148                ar = (AsyncResult)msg.obj;
1149
1150                if (ar.exception != null) {
1151                    break;
1152                }
1153
1154                if (DBG) Rlog.d(LOG_TAG, "Baseband version: " + ar.result);
1155                setSystemProperty(TelephonyProperties.PROPERTY_BASEBAND_VERSION, (String)ar.result);
1156            }
1157            break;
1158
1159            case EVENT_GET_DEVICE_IDENTITY_DONE:{
1160                ar = (AsyncResult)msg.obj;
1161
1162                if (ar.exception != null) {
1163                    break;
1164                }
1165                String[] respId = (String[])ar.result;
1166                mImei = respId[0];
1167                mImeiSv = respId[1];
1168                mEsn  =  respId[2];
1169                mMeid =  respId[3];
1170            }
1171            break;
1172
1173            case EVENT_EMERGENCY_CALLBACK_MODE_ENTER:{
1174                handleEnterEmergencyCallbackMode(msg);
1175            }
1176            break;
1177
1178            case EVENT_ICC_RECORD_EVENTS:
1179                ar = (AsyncResult)msg.obj;
1180                processIccRecordEvents((Integer)ar.result);
1181                break;
1182
1183            case  EVENT_EXIT_EMERGENCY_CALLBACK_RESPONSE:{
1184                handleExitEmergencyCallbackMode(msg);
1185            }
1186            break;
1187
1188            case EVENT_RUIM_RECORDS_LOADED:{
1189                Rlog.d(LOG_TAG, "Event EVENT_RUIM_RECORDS_LOADED Received");
1190                updateCurrentCarrierInProvider();
1191            }
1192            break;
1193
1194            case EVENT_RADIO_OFF_OR_NOT_AVAILABLE:{
1195                Rlog.d(LOG_TAG, "Event EVENT_RADIO_OFF_OR_NOT_AVAILABLE Received");
1196                ImsPhone imsPhone = mImsPhone;
1197                if (imsPhone != null) {
1198                    imsPhone.getServiceState().setStateOff();
1199                }
1200            }
1201            break;
1202
1203            case EVENT_RADIO_ON:{
1204                Rlog.d(LOG_TAG, "Event EVENT_RADIO_ON Received");
1205                handleCdmaSubscriptionSource(mCdmaSSM.getCdmaSubscriptionSource());
1206                ImsPhone imsPhone = mImsPhone;
1207                if (imsPhone != null) {
1208                    imsPhone.getServiceState().setStateOutOfService();
1209                }
1210            }
1211            break;
1212
1213            case EVENT_CDMA_SUBSCRIPTION_SOURCE_CHANGED:{
1214                Rlog.d(LOG_TAG, "EVENT_CDMA_SUBSCRIPTION_SOURCE_CHANGED");
1215                handleCdmaSubscriptionSource(mCdmaSSM.getCdmaSubscriptionSource());
1216            }
1217            break;
1218
1219            case EVENT_SSN:{
1220                Rlog.d(LOG_TAG, "Event EVENT_SSN Received");
1221            }
1222            break;
1223
1224            case EVENT_REGISTERED_TO_NETWORK:{
1225                Rlog.d(LOG_TAG, "Event EVENT_REGISTERED_TO_NETWORK Received");
1226            }
1227            break;
1228
1229            case EVENT_NV_READY:{
1230                Rlog.d(LOG_TAG, "Event EVENT_NV_READY Received");
1231                prepareEri();
1232            }
1233            break;
1234
1235            case EVENT_SET_VM_NUMBER_DONE:{
1236                ar = (AsyncResult)msg.obj;
1237                if (IccException.class.isInstance(ar.exception)) {
1238                    storeVoiceMailNumber(mVmNumber);
1239                    ar.exception = null;
1240                }
1241                onComplete = (Message) ar.userObj;
1242                if (onComplete != null) {
1243                    AsyncResult.forMessage(onComplete, ar.result, ar.exception);
1244                    onComplete.sendToTarget();
1245                }
1246            }
1247            break;
1248
1249            default:{
1250                super.handleMessage(msg);
1251            }
1252        }
1253    }
1254
1255    protected UiccCardApplication getUiccCardApplication() {
1256        return  mUiccController.getUiccCardApplication(mPhoneId, UiccController.APP_FAM_3GPP2);
1257    }
1258
1259    @Override
1260    protected void onUpdateIccAvailability() {
1261        if (mUiccController == null ) {
1262            return;
1263        }
1264
1265        UiccCardApplication newUiccApplication = getUiccCardApplication();
1266
1267        if (newUiccApplication == null) {
1268            log("can't find 3GPP2 application; trying APP_FAM_3GPP");
1269            newUiccApplication =
1270                    mUiccController.getUiccCardApplication(mPhoneId, UiccController.APP_FAM_3GPP);
1271        }
1272
1273        UiccCardApplication app = mUiccApplication.get();
1274        if (app != newUiccApplication) {
1275            if (app != null) {
1276                log("Removing stale icc objects.");
1277                if (mIccRecords.get() != null) {
1278                    unregisterForRuimRecordEvents();
1279                }
1280                mIccRecords.set(null);
1281                mUiccApplication.set(null);
1282            }
1283            if (newUiccApplication != null) {
1284                log("New Uicc application found");
1285                mUiccApplication.set(newUiccApplication);
1286                mIccRecords.set(newUiccApplication.getIccRecords());
1287                registerForRuimRecordEvents();
1288            }
1289        }
1290    }
1291
1292    private void processIccRecordEvents(int eventCode) {
1293        switch (eventCode) {
1294            case RuimRecords.EVENT_MWI:
1295                notifyMessageWaitingIndicator();
1296                break;
1297
1298            default:
1299                Rlog.e(LOG_TAG,"Unknown icc records event code " + eventCode);
1300                break;
1301        }
1302    }
1303
1304    /**
1305     * Handles the call to get the subscription source
1306     *
1307     * @param newSubscriptionSource holds the new CDMA subscription source value
1308     */
1309    private void handleCdmaSubscriptionSource(int newSubscriptionSource) {
1310        if (newSubscriptionSource != mCdmaSubscriptionSource) {
1311             mCdmaSubscriptionSource = newSubscriptionSource;
1312             if (newSubscriptionSource == CDMA_SUBSCRIPTION_NV) {
1313                 // NV is ready when subscription source is NV
1314                 sendMessage(obtainMessage(EVENT_NV_READY));
1315             }
1316        }
1317    }
1318
1319    /**
1320     * Retrieves the PhoneSubInfo of the CDMAPhone
1321     */
1322    @Override
1323    public PhoneSubInfo getPhoneSubInfo() {
1324        return mSubInfo;
1325    }
1326
1327    /**
1328     * Retrieves the IccPhoneBookInterfaceManager of the CDMAPhone
1329     */
1330    @Override
1331    public IccPhoneBookInterfaceManager getIccPhoneBookInterfaceManager() {
1332        return mRuimPhoneBookInterfaceManager;
1333    }
1334
1335    public void registerForEriFileLoaded(Handler h, int what, Object obj) {
1336        Registrant r = new Registrant (h, what, obj);
1337        mEriFileLoadedRegistrants.add(r);
1338    }
1339
1340    public void unregisterForEriFileLoaded(Handler h) {
1341        mEriFileLoadedRegistrants.remove(h);
1342    }
1343
1344    // override for allowing access from other classes of this package
1345    /**
1346     * {@inheritDoc}
1347     */
1348    @Override
1349    public void setSystemProperty(String property, String value) {
1350        super.setSystemProperty(property, value);
1351    }
1352
1353    /**
1354     * Activate or deactivate cell broadcast SMS.
1355     *
1356     * @param activate 0 = activate, 1 = deactivate
1357     * @param response Callback message is empty on completion
1358     */
1359    @Override
1360    public void activateCellBroadcastSms(int activate, Message response) {
1361        Rlog.e(LOG_TAG, "[CDMAPhone] activateCellBroadcastSms() is obsolete; use SmsManager");
1362        response.sendToTarget();
1363    }
1364
1365    /**
1366     * Query the current configuration of cdma cell broadcast SMS.
1367     *
1368     * @param response Callback message is empty on completion
1369     */
1370    @Override
1371    public void getCellBroadcastSmsConfig(Message response) {
1372        Rlog.e(LOG_TAG, "[CDMAPhone] getCellBroadcastSmsConfig() is obsolete; use SmsManager");
1373        response.sendToTarget();
1374    }
1375
1376    /**
1377     * Configure cdma cell broadcast SMS.
1378     *
1379     * @param response Callback message is empty on completion
1380     */
1381    @Override
1382    public void setCellBroadcastSmsConfig(int[] configValuesArray, Message response) {
1383        Rlog.e(LOG_TAG, "[CDMAPhone] setCellBroadcastSmsConfig() is obsolete; use SmsManager");
1384        response.sendToTarget();
1385    }
1386
1387    /**
1388     * Returns true if OTA Service Provisioning needs to be performed.
1389     */
1390    @Override
1391    public boolean needsOtaServiceProvisioning() {
1392        return mSST.getOtasp() != ServiceStateTracker.OTASP_NOT_NEEDED;
1393    }
1394
1395    private static final String IS683A_FEATURE_CODE = "*228";
1396    private static final int IS683A_FEATURE_CODE_NUM_DIGITS = 4;
1397    private static final int IS683A_SYS_SEL_CODE_NUM_DIGITS = 2;
1398    private static final int IS683A_SYS_SEL_CODE_OFFSET = 4;
1399
1400    private static final int IS683_CONST_800MHZ_A_BAND = 0;
1401    private static final int IS683_CONST_800MHZ_B_BAND = 1;
1402    private static final int IS683_CONST_1900MHZ_A_BLOCK = 2;
1403    private static final int IS683_CONST_1900MHZ_B_BLOCK = 3;
1404    private static final int IS683_CONST_1900MHZ_C_BLOCK = 4;
1405    private static final int IS683_CONST_1900MHZ_D_BLOCK = 5;
1406    private static final int IS683_CONST_1900MHZ_E_BLOCK = 6;
1407    private static final int IS683_CONST_1900MHZ_F_BLOCK = 7;
1408    private static final int INVALID_SYSTEM_SELECTION_CODE = -1;
1409
1410    private static boolean isIs683OtaSpDialStr(String dialStr) {
1411        int sysSelCodeInt;
1412        boolean isOtaspDialString = false;
1413        int dialStrLen = dialStr.length();
1414
1415        if (dialStrLen == IS683A_FEATURE_CODE_NUM_DIGITS) {
1416            if (dialStr.equals(IS683A_FEATURE_CODE)) {
1417                isOtaspDialString = true;
1418            }
1419        } else {
1420            sysSelCodeInt = extractSelCodeFromOtaSpNum(dialStr);
1421            switch (sysSelCodeInt) {
1422                case IS683_CONST_800MHZ_A_BAND:
1423                case IS683_CONST_800MHZ_B_BAND:
1424                case IS683_CONST_1900MHZ_A_BLOCK:
1425                case IS683_CONST_1900MHZ_B_BLOCK:
1426                case IS683_CONST_1900MHZ_C_BLOCK:
1427                case IS683_CONST_1900MHZ_D_BLOCK:
1428                case IS683_CONST_1900MHZ_E_BLOCK:
1429                case IS683_CONST_1900MHZ_F_BLOCK:
1430                    isOtaspDialString = true;
1431                    break;
1432                default:
1433                    break;
1434            }
1435        }
1436        return isOtaspDialString;
1437    }
1438    /**
1439     * This function extracts the system selection code from the dial string.
1440     */
1441    private static int extractSelCodeFromOtaSpNum(String dialStr) {
1442        int dialStrLen = dialStr.length();
1443        int sysSelCodeInt = INVALID_SYSTEM_SELECTION_CODE;
1444
1445        if ((dialStr.regionMatches(0, IS683A_FEATURE_CODE,
1446                                   0, IS683A_FEATURE_CODE_NUM_DIGITS)) &&
1447            (dialStrLen >= (IS683A_FEATURE_CODE_NUM_DIGITS +
1448                            IS683A_SYS_SEL_CODE_NUM_DIGITS))) {
1449                // Since we checked the condition above, the system selection code
1450                // extracted from dialStr will not cause any exception
1451                sysSelCodeInt = Integer.parseInt (
1452                                dialStr.substring (IS683A_FEATURE_CODE_NUM_DIGITS,
1453                                IS683A_FEATURE_CODE_NUM_DIGITS + IS683A_SYS_SEL_CODE_NUM_DIGITS));
1454        }
1455        if (DBG) Rlog.d(LOG_TAG, "extractSelCodeFromOtaSpNum " + sysSelCodeInt);
1456        return sysSelCodeInt;
1457    }
1458
1459    /**
1460     * This function checks if the system selection code extracted from
1461     * the dial string "sysSelCodeInt' is the system selection code specified
1462     * in the carrier ota sp number schema "sch".
1463     */
1464    private static boolean
1465    checkOtaSpNumBasedOnSysSelCode (int sysSelCodeInt, String sch[]) {
1466        boolean isOtaSpNum = false;
1467        try {
1468            // Get how many number of system selection code ranges
1469            int selRc = Integer.parseInt(sch[1]);
1470            for (int i = 0; i < selRc; i++) {
1471                if (!TextUtils.isEmpty(sch[i+2]) && !TextUtils.isEmpty(sch[i+3])) {
1472                    int selMin = Integer.parseInt(sch[i+2]);
1473                    int selMax = Integer.parseInt(sch[i+3]);
1474                    // Check if the selection code extracted from the dial string falls
1475                    // within any of the range pairs specified in the schema.
1476                    if ((sysSelCodeInt >= selMin) && (sysSelCodeInt <= selMax)) {
1477                        isOtaSpNum = true;
1478                        break;
1479                    }
1480                }
1481            }
1482        } catch (NumberFormatException ex) {
1483            // If the carrier ota sp number schema is not correct, we still allow dial
1484            // and only log the error:
1485            Rlog.e(LOG_TAG, "checkOtaSpNumBasedOnSysSelCode, error", ex);
1486        }
1487        return isOtaSpNum;
1488    }
1489
1490    // Define the pattern/format for carrier specified OTASP number schema.
1491    // It separates by comma and/or whitespace.
1492    private static Pattern pOtaSpNumSchema = Pattern.compile("[,\\s]+");
1493
1494    /**
1495     * The following function checks if a dial string is a carrier specified
1496     * OTASP number or not by checking against the OTASP number schema stored
1497     * in PROPERTY_OTASP_NUM_SCHEMA.
1498     *
1499     * Currently, there are 2 schemas for carriers to specify the OTASP number:
1500     * 1) Use system selection code:
1501     *    The schema is:
1502     *    SELC,the # of code pairs,min1,max1,min2,max2,...
1503     *    e.g "SELC,3,10,20,30,40,60,70" indicates that there are 3 pairs of
1504     *    selection codes, and they are {10,20}, {30,40} and {60,70} respectively.
1505     *
1506     * 2) Use feature code:
1507     *    The schema is:
1508     *    "FC,length of feature code,feature code".
1509     *     e.g "FC,2,*2" indicates that the length of the feature code is 2,
1510     *     and the code itself is "*2".
1511     */
1512    private boolean isCarrierOtaSpNum(String dialStr) {
1513        boolean isOtaSpNum = false;
1514        int sysSelCodeInt = extractSelCodeFromOtaSpNum(dialStr);
1515        if (sysSelCodeInt == INVALID_SYSTEM_SELECTION_CODE) {
1516            return isOtaSpNum;
1517        }
1518        // mCarrierOtaSpNumSchema is retrieved from PROPERTY_OTASP_NUM_SCHEMA:
1519        if (!TextUtils.isEmpty(mCarrierOtaSpNumSchema)) {
1520            Matcher m = pOtaSpNumSchema.matcher(mCarrierOtaSpNumSchema);
1521            if (DBG) {
1522                Rlog.d(LOG_TAG, "isCarrierOtaSpNum,schema" + mCarrierOtaSpNumSchema);
1523            }
1524
1525            if (m.find()) {
1526                String sch[] = pOtaSpNumSchema.split(mCarrierOtaSpNumSchema);
1527                // If carrier uses system selection code mechanism
1528                if (!TextUtils.isEmpty(sch[0]) && sch[0].equals("SELC")) {
1529                    if (sysSelCodeInt!=INVALID_SYSTEM_SELECTION_CODE) {
1530                        isOtaSpNum=checkOtaSpNumBasedOnSysSelCode(sysSelCodeInt,sch);
1531                    } else {
1532                        if (DBG) {
1533                            Rlog.d(LOG_TAG, "isCarrierOtaSpNum,sysSelCodeInt is invalid");
1534                        }
1535                    }
1536                } else if (!TextUtils.isEmpty(sch[0]) && sch[0].equals("FC")) {
1537                    int fcLen =  Integer.parseInt(sch[1]);
1538                    String fc = sch[2];
1539                    if (dialStr.regionMatches(0,fc,0,fcLen)) {
1540                        isOtaSpNum = true;
1541                    } else {
1542                        if (DBG) Rlog.d(LOG_TAG, "isCarrierOtaSpNum,not otasp number");
1543                    }
1544                } else {
1545                    if (DBG) {
1546                        Rlog.d(LOG_TAG, "isCarrierOtaSpNum,ota schema not supported" + sch[0]);
1547                    }
1548                }
1549            } else {
1550                if (DBG) {
1551                    Rlog.d(LOG_TAG, "isCarrierOtaSpNum,ota schema pattern not right" +
1552                          mCarrierOtaSpNumSchema);
1553                }
1554            }
1555        } else {
1556            if (DBG) Rlog.d(LOG_TAG, "isCarrierOtaSpNum,ota schema pattern empty");
1557        }
1558        return isOtaSpNum;
1559    }
1560
1561    /**
1562     * isOTASPNumber: checks a given number against the IS-683A OTASP dial string and carrier
1563     * OTASP dial string.
1564     *
1565     * @param dialStr the number to look up.
1566     * @return true if the number is in IS-683A OTASP dial string or carrier OTASP dial string
1567     */
1568    @Override
1569    public  boolean isOtaSpNumber(String dialStr){
1570        boolean isOtaSpNum = false;
1571        String dialableStr = PhoneNumberUtils.extractNetworkPortionAlt(dialStr);
1572        if (dialableStr != null) {
1573            isOtaSpNum = isIs683OtaSpDialStr(dialableStr);
1574            if (isOtaSpNum == false) {
1575                isOtaSpNum = isCarrierOtaSpNum(dialableStr);
1576            }
1577        }
1578        if (DBG) Rlog.d(LOG_TAG, "isOtaSpNumber " + isOtaSpNum);
1579        return isOtaSpNum;
1580    }
1581
1582    @Override
1583    public int getCdmaEriIconIndex() {
1584        return getServiceState().getCdmaEriIconIndex();
1585    }
1586
1587    /**
1588     * Returns the CDMA ERI icon mode,
1589     * 0 - ON
1590     * 1 - FLASHING
1591     */
1592    @Override
1593    public int getCdmaEriIconMode() {
1594        return getServiceState().getCdmaEriIconMode();
1595    }
1596
1597    /**
1598     * Returns the CDMA ERI text,
1599     */
1600    @Override
1601    public String getCdmaEriText() {
1602        int roamInd = getServiceState().getCdmaRoamingIndicator();
1603        int defRoamInd = getServiceState().getCdmaDefaultRoamingIndicator();
1604        return mEriManager.getCdmaEriText(roamInd, defRoamInd);
1605    }
1606
1607    /**
1608     * Store the voicemail number in preferences
1609     */
1610    private void storeVoiceMailNumber(String number) {
1611        // Update the preference value of voicemail number
1612        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
1613        SharedPreferences.Editor editor = sp.edit();
1614        editor.putString(VM_NUMBER_CDMA + getPhoneId(), number);
1615        editor.apply();
1616    }
1617
1618    /**
1619     * Sets PROPERTY_ICC_OPERATOR_ISO_COUNTRY property
1620     *
1621     */
1622    protected void setIsoCountryProperty(String operatorNumeric) {
1623        if (TextUtils.isEmpty(operatorNumeric)) {
1624            log("setIsoCountryProperty: clear 'gsm.sim.operator.iso-country'");
1625            setSystemProperty(PROPERTY_ICC_OPERATOR_ISO_COUNTRY, "");
1626        } else {
1627            String iso = "";
1628            try {
1629                iso = MccTable.countryCodeForMcc(Integer.parseInt(
1630                        operatorNumeric.substring(0,3)));
1631            } catch (NumberFormatException ex) {
1632                loge("setIsoCountryProperty: countryCodeForMcc error", ex);
1633            } catch (StringIndexOutOfBoundsException ex) {
1634                loge("setIsoCountryProperty: countryCodeForMcc error", ex);
1635            }
1636
1637            log("setIsoCountryProperty: set 'gsm.sim.operator.iso-country' to iso=" + iso);
1638            setSystemProperty(PROPERTY_ICC_OPERATOR_ISO_COUNTRY, iso);
1639        }
1640    }
1641
1642    /**
1643     * Sets the "current" field in the telephony provider according to the
1644     * build-time operator numeric property
1645     *
1646     * @return true for success; false otherwise.
1647     */
1648    boolean updateCurrentCarrierInProvider(String operatorNumeric) {
1649        log("CDMAPhone: updateCurrentCarrierInProvider called");
1650        if (!TextUtils.isEmpty(operatorNumeric)) {
1651            try {
1652                Uri uri = Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current");
1653                ContentValues map = new ContentValues();
1654                map.put(Telephony.Carriers.NUMERIC, operatorNumeric);
1655                log("updateCurrentCarrierInProvider from system: numeric=" + operatorNumeric);
1656                getContext().getContentResolver().insert(uri, map);
1657
1658                // Updates MCC MNC device configuration information
1659                log("update mccmnc=" + operatorNumeric);
1660                MccTable.updateMccMncConfiguration(mContext, operatorNumeric, false);
1661
1662                return true;
1663            } catch (SQLException e) {
1664                Rlog.e(LOG_TAG, "Can't store current operator", e);
1665            }
1666        }
1667        return false;
1668    }
1669
1670    /**
1671     * Sets the "current" field in the telephony provider according to the SIM's operator.
1672     * Implemented in {@link CDMALTEPhone} for CDMA/LTE devices.
1673     *
1674     * @return true for success; false otherwise.
1675     */
1676    boolean updateCurrentCarrierInProvider() {
1677        return true;
1678    }
1679
1680    public void prepareEri() {
1681        if (mEriManager == null) {
1682            Rlog.e(LOG_TAG, "PrepareEri: Trying to access stale objects");
1683            return;
1684        }
1685        mEriManager.loadEriFile();
1686        if(mEriManager.isEriFileLoaded()) {
1687            // when the ERI file is loaded
1688            log("ERI read, notify registrants");
1689            mEriFileLoadedRegistrants.notifyRegistrants();
1690        }
1691    }
1692
1693    public boolean isEriFileLoaded() {
1694        return mEriManager.isEriFileLoaded();
1695    }
1696
1697    protected void registerForRuimRecordEvents() {
1698        IccRecords r = mIccRecords.get();
1699        if (r == null) {
1700            return;
1701        }
1702        r.registerForRecordsEvents(this, EVENT_ICC_RECORD_EVENTS, null);
1703        r.registerForRecordsLoaded(this, EVENT_RUIM_RECORDS_LOADED, null);
1704    }
1705
1706    protected void unregisterForRuimRecordEvents() {
1707        IccRecords r = mIccRecords.get();
1708        if (r == null) {
1709            return;
1710        }
1711        r.unregisterForRecordsEvents(this);
1712        r.unregisterForRecordsLoaded(this);
1713    }
1714
1715    protected void log(String s) {
1716        if (DBG)
1717            Rlog.d(LOG_TAG, s);
1718    }
1719
1720    protected void loge(String s, Exception e) {
1721        if (DBG)
1722            Rlog.e(LOG_TAG, s, e);
1723    }
1724
1725    @Override
1726    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1727        pw.println("CDMAPhone extends:");
1728        super.dump(fd, pw, args);
1729        pw.println(" mVmNumber=" + mVmNumber);
1730        pw.println(" mCT=" + mCT);
1731        pw.println(" mSST=" + mSST);
1732        pw.println(" mCdmaSSM=" + mCdmaSSM);
1733        pw.println(" mPendingMmis=" + mPendingMmis);
1734        pw.println(" mRuimPhoneBookInterfaceManager=" + mRuimPhoneBookInterfaceManager);
1735        pw.println(" mCdmaSubscriptionSource=" + mCdmaSubscriptionSource);
1736        pw.println(" mSubInfo=" + mSubInfo);
1737        pw.println(" mEriManager=" + mEriManager);
1738        pw.println(" mWakeLock=" + mWakeLock);
1739        pw.println(" mIsPhoneInEcmState=" + mIsPhoneInEcmState);
1740        if (VDBG) pw.println(" mImei=" + mImei);
1741        if (VDBG) pw.println(" mImeiSv=" + mImeiSv);
1742        if (VDBG) pw.println(" mEsn=" + mEsn);
1743        if (VDBG) pw.println(" mMeid=" + mMeid);
1744        pw.println(" mCarrierOtaSpNumSchema=" + mCarrierOtaSpNumSchema);
1745        pw.println(" getCdmaEriIconIndex()=" + getCdmaEriIconIndex());
1746        pw.println(" getCdmaEriIconMode()=" + getCdmaEriIconMode());
1747        pw.println(" getCdmaEriText()=" + getCdmaEriText());
1748        pw.println(" isMinInfoReady()=" + isMinInfoReady());
1749        pw.println(" isCspPlmnEnabled()=" + isCspPlmnEnabled());
1750    }
1751
1752    @Override
1753    public boolean setOperatorBrandOverride(String iccId, String brand) {
1754        if (mUiccController == null) {
1755            return false;
1756        }
1757
1758        UiccCard card = mUiccController.getUiccCard();
1759        if (card == null) {
1760            return false;
1761        }
1762
1763        boolean status = card.setOperatorBrandOverride(iccId, brand);
1764
1765        // Refresh.
1766        if (status) {
1767            IccRecords iccRecords = mIccRecords.get();
1768            if (iccRecords != null) {
1769                SystemProperties.set(TelephonyProperties.PROPERTY_ICC_OPERATOR_ALPHA,
1770                        iccRecords.getServiceProviderName());
1771            }
1772            if (mSST != null) {
1773                mSST.pollState();
1774            }
1775        }
1776        return status;
1777    }
1778}
1779