ImsPhone.java revision 81357ffc6a5a19c6ee1f846797cf06fedc0c7b5e
1/*
2 * Copyright (C) 2013 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.imsphone;
18
19import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BAIC;
20import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BAICr;
21import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BAOC;
22import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BAOIC;
23import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BAOICxH;
24import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BA_ALL;
25import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BA_MO;
26import static com.android.internal.telephony.CommandsInterface.CB_FACILITY_BA_MT;
27import static com.android.internal.telephony.CommandsInterface.CF_ACTION_DISABLE;
28import static com.android.internal.telephony.CommandsInterface.CF_ACTION_ENABLE;
29import static com.android.internal.telephony.CommandsInterface.CF_ACTION_ERASURE;
30import static com.android.internal.telephony.CommandsInterface.CF_ACTION_REGISTRATION;
31import static com.android.internal.telephony.CommandsInterface.CF_REASON_ALL;
32import static com.android.internal.telephony.CommandsInterface.CF_REASON_ALL_CONDITIONAL;
33import static com.android.internal.telephony.CommandsInterface.CF_REASON_BUSY;
34import static com.android.internal.telephony.CommandsInterface.CF_REASON_NOT_REACHABLE;
35import static com.android.internal.telephony.CommandsInterface.CF_REASON_NO_REPLY;
36import static com.android.internal.telephony.CommandsInterface.CF_REASON_UNCONDITIONAL;
37import static com.android.internal.telephony.CommandsInterface.SERVICE_CLASS_NONE;
38import static com.android.internal.telephony.CommandsInterface.SERVICE_CLASS_VOICE;
39
40import android.app.Activity;
41import android.app.ActivityManager;
42import android.app.Notification;
43import android.app.NotificationManager;
44import android.app.PendingIntent;
45import android.content.BroadcastReceiver;
46import android.content.Context;
47import android.content.Intent;
48import android.net.NetworkStats;
49import android.net.Uri;
50import android.os.AsyncResult;
51import android.os.Bundle;
52import android.os.Handler;
53import android.os.Message;
54import android.os.PersistableBundle;
55import android.os.PowerManager;
56import android.os.PowerManager.WakeLock;
57import android.os.Registrant;
58import android.os.RegistrantList;
59import android.os.ResultReceiver;
60import android.os.SystemProperties;
61import android.os.UserHandle;
62import android.telecom.VideoProfile;
63import android.telephony.CarrierConfigManager;
64import android.telephony.PhoneNumberUtils;
65import android.telephony.Rlog;
66import android.telephony.ServiceState;
67import android.telephony.SubscriptionManager;
68import android.telephony.TelephonyManager;
69import android.telephony.UssdResponse;
70import android.text.TextUtils;
71
72import com.android.ims.ImsCallForwardInfo;
73import com.android.ims.ImsCallProfile;
74import com.android.ims.ImsEcbm;
75import com.android.ims.ImsEcbmStateListener;
76import com.android.ims.ImsException;
77import com.android.ims.ImsManager;
78import com.android.ims.ImsReasonInfo;
79import com.android.ims.ImsSsInfo;
80import com.android.ims.ImsUtInterface;
81import com.android.internal.annotations.VisibleForTesting;
82import com.android.internal.telephony.Call;
83import com.android.internal.telephony.CallForwardInfo;
84import com.android.internal.telephony.CallStateException;
85import com.android.internal.telephony.CallTracker;
86import com.android.internal.telephony.CommandException;
87import com.android.internal.telephony.CommandsInterface;
88import com.android.internal.telephony.Connection;
89import com.android.internal.telephony.GsmCdmaPhone;
90import com.android.internal.telephony.MmiCode;
91import com.android.internal.telephony.Phone;
92import com.android.internal.telephony.PhoneConstants;
93import com.android.internal.telephony.PhoneNotifier;
94import com.android.internal.telephony.TelephonyComponentFactory;
95import com.android.internal.telephony.TelephonyIntents;
96import com.android.internal.telephony.TelephonyProperties;
97import com.android.internal.telephony.UUSInfo;
98import com.android.internal.telephony.gsm.GsmMmiCode;
99import com.android.internal.telephony.gsm.SuppServiceNotification;
100import com.android.internal.telephony.uicc.IccRecords;
101import com.android.internal.telephony.util.NotificationChannelController;
102
103import java.io.FileDescriptor;
104import java.io.PrintWriter;
105import java.util.ArrayList;
106import java.util.List;
107
108/**
109 * {@hide}
110 */
111public class ImsPhone extends ImsPhoneBase {
112    private static final String LOG_TAG = "ImsPhone";
113    private static final boolean DBG = true;
114    private static final boolean VDBG = false; // STOPSHIP if true
115
116    private static final int EVENT_SET_CALL_BARRING_DONE             = EVENT_LAST + 1;
117    private static final int EVENT_GET_CALL_BARRING_DONE             = EVENT_LAST + 2;
118    private static final int EVENT_SET_CALL_WAITING_DONE             = EVENT_LAST + 3;
119    private static final int EVENT_GET_CALL_WAITING_DONE             = EVENT_LAST + 4;
120    private static final int EVENT_SET_CLIR_DONE                     = EVENT_LAST + 5;
121    private static final int EVENT_GET_CLIR_DONE                     = EVENT_LAST + 6;
122    private static final int EVENT_DEFAULT_PHONE_DATA_STATE_CHANGED  = EVENT_LAST + 7;
123    private static final int EVENT_SERVICE_STATE_CHANGED             = EVENT_LAST + 8;
124    private static final int EVENT_VOICE_CALL_ENDED                  = EVENT_LAST + 9;
125
126    static final int RESTART_ECM_TIMER = 0; // restart Ecm timer
127    static final int CANCEL_ECM_TIMER  = 1; // cancel Ecm timer
128
129    // Default Emergency Callback Mode exit timer
130    private static final int DEFAULT_ECM_EXIT_TIMER_VALUE = 300000;
131
132    // Instance Variables
133    Phone mDefaultPhone;
134    ImsPhoneCallTracker mCT;
135    ImsExternalCallTracker mExternalCallTracker;
136    private ArrayList <ImsPhoneMmiCode> mPendingMMIs = new ArrayList<ImsPhoneMmiCode>();
137    private ServiceState mSS = new ServiceState();
138
139    // To redial silently through GSM or CDMA when dialing through IMS fails
140    private String mLastDialString;
141
142    private WakeLock mWakeLock;
143
144    // mEcmExitRespRegistrant is informed after the phone has been exited the emergency
145    // callback mode keep track of if phone is in emergency callback mode
146    private Registrant mEcmExitRespRegistrant;
147
148    private final RegistrantList mSilentRedialRegistrants = new RegistrantList();
149
150    private boolean mImsRegistered = false;
151
152    private boolean mRoaming = false;
153
154    // List of Registrants to send supplementary service notifications to.
155    private RegistrantList mSsnRegistrants = new RegistrantList();
156
157    // A runnable which is used to automatically exit from Ecm after a period of time.
158    private Runnable mExitEcmRunnable = new Runnable() {
159        @Override
160        public void run() {
161            exitEmergencyCallbackMode();
162        }
163    };
164
165    private Uri[] mCurrentSubscriberUris;
166
167    protected void setCurrentSubscriberUris(Uri[] currentSubscriberUris) {
168        this.mCurrentSubscriberUris = currentSubscriberUris;
169    }
170
171    @Override
172    public Uri[] getCurrentSubscriberUris() {
173        return mCurrentSubscriberUris;
174    }
175
176    // Create Cf (Call forward) so that dialling number &
177    // mIsCfu (true if reason is call forward unconditional)
178    // mOnComplete (Message object passed by client) can be packed &
179    // given as a single Cf object as user data to UtInterface.
180    private static class Cf {
181        final String mSetCfNumber;
182        final Message mOnComplete;
183        final boolean mIsCfu;
184
185        Cf(String cfNumber, boolean isCfu, Message onComplete) {
186            mSetCfNumber = cfNumber;
187            mIsCfu = isCfu;
188            mOnComplete = onComplete;
189        }
190    }
191
192    // Constructors
193    public ImsPhone(Context context, PhoneNotifier notifier, Phone defaultPhone) {
194        this(context, notifier, defaultPhone, false);
195    }
196
197    @VisibleForTesting
198    public ImsPhone(Context context, PhoneNotifier notifier, Phone defaultPhone,
199                    boolean unitTestMode) {
200        super("ImsPhone", context, notifier, unitTestMode);
201
202        mDefaultPhone = defaultPhone;
203        // The ImsExternalCallTracker needs to be defined before the ImsPhoneCallTracker, as the
204        // ImsPhoneCallTracker uses a thread to spool up the ImsManager.  Part of this involves
205        // setting the multiendpoint listener on the external call tracker.  So we need to ensure
206        // the external call tracker is available first to avoid potential timing issues.
207        mExternalCallTracker =
208                TelephonyComponentFactory.getInstance().makeImsExternalCallTracker(this);
209        mCT = TelephonyComponentFactory.getInstance().makeImsPhoneCallTracker(this);
210        mCT.registerPhoneStateListener(mExternalCallTracker);
211        mExternalCallTracker.setCallPuller(mCT);
212
213        mSS.setStateOff();
214
215        mPhoneId = mDefaultPhone.getPhoneId();
216
217        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
218        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, LOG_TAG);
219        mWakeLock.setReferenceCounted(false);
220
221        if (mDefaultPhone.getServiceStateTracker() != null) {
222            mDefaultPhone.getServiceStateTracker()
223                    .registerForDataRegStateOrRatChanged(this,
224                            EVENT_DEFAULT_PHONE_DATA_STATE_CHANGED, null);
225        }
226        // Sets the Voice reg state to STATE_OUT_OF_SERVICE and also queries the data service
227        // state. We don't ever need the voice reg state to be anything other than in or out of
228        // service.
229        setServiceState(ServiceState.STATE_OUT_OF_SERVICE);
230
231        mDefaultPhone.registerForServiceStateChanged(this, EVENT_SERVICE_STATE_CHANGED, null);
232        // Force initial roaming state update later, on EVENT_CARRIER_CONFIG_CHANGED.
233        // Settings provider or CarrierConfig may not be loaded now.
234    }
235
236    //todo: get rid of this function. It is not needed since parentPhone obj never changes
237    @Override
238    public void dispose() {
239        Rlog.d(LOG_TAG, "dispose");
240        // Nothing to dispose in Phone
241        //super.dispose();
242        mPendingMMIs.clear();
243        mExternalCallTracker.tearDown();
244        mCT.unregisterPhoneStateListener(mExternalCallTracker);
245        mCT.unregisterForVoiceCallEnded(this);
246        mCT.dispose();
247
248        //Force all referenced classes to unregister their former registered events
249        if (mDefaultPhone != null && mDefaultPhone.getServiceStateTracker() != null) {
250            mDefaultPhone.getServiceStateTracker().
251                    unregisterForDataRegStateOrRatChanged(this);
252            mDefaultPhone.unregisterForServiceStateChanged(this);
253        }
254    }
255
256    @Override
257    public ServiceState getServiceState() {
258        return mSS;
259    }
260
261    @VisibleForTesting
262    public void setServiceState(int state) {
263        boolean isVoiceRegStateChanged = false;
264        synchronized (this) {
265            isVoiceRegStateChanged = mSS.getVoiceRegState() != state;
266            mSS.setVoiceRegState(state);
267        }
268        updateDataServiceState();
269
270        if (isVoiceRegStateChanged) {
271            if (mDefaultPhone.getServiceStateTracker() != null) {
272                mDefaultPhone.getServiceStateTracker().onImsServiceStateChanged();
273            }
274        }
275    }
276
277    @Override
278    public CallTracker getCallTracker() {
279        return mCT;
280    }
281
282    public ImsExternalCallTracker getExternalCallTracker() {
283        return mExternalCallTracker;
284    }
285
286    @Override
287    public List<? extends ImsPhoneMmiCode>
288    getPendingMmiCodes() {
289        return mPendingMMIs;
290    }
291
292    @Override
293    public void
294    acceptCall(int videoState) throws CallStateException {
295        mCT.acceptCall(videoState);
296    }
297
298    @Override
299    public void
300    rejectCall() throws CallStateException {
301        mCT.rejectCall();
302    }
303
304    @Override
305    public void
306    switchHoldingAndActive() throws CallStateException {
307        mCT.switchWaitingOrHoldingAndActive();
308    }
309
310    @Override
311    public boolean canConference() {
312        return mCT.canConference();
313    }
314
315    public boolean canDial() {
316        return mCT.canDial();
317    }
318
319    @Override
320    public void conference() {
321        mCT.conference();
322    }
323
324    @Override
325    public void clearDisconnected() {
326        mCT.clearDisconnected();
327    }
328
329    @Override
330    public boolean canTransfer() {
331        return mCT.canTransfer();
332    }
333
334    @Override
335    public void explicitCallTransfer() {
336        mCT.explicitCallTransfer();
337    }
338
339    @Override
340    public ImsPhoneCall
341    getForegroundCall() {
342        return mCT.mForegroundCall;
343    }
344
345    @Override
346    public ImsPhoneCall
347    getBackgroundCall() {
348        return mCT.mBackgroundCall;
349    }
350
351    @Override
352    public ImsPhoneCall
353    getRingingCall() {
354        return mCT.mRingingCall;
355    }
356
357    @Override
358    public boolean isImsAvailable() {
359        return mCT.isImsServiceReady();
360    }
361
362    private boolean handleCallDeflectionIncallSupplementaryService(
363            String dialString) {
364        if (dialString.length() > 1) {
365            return false;
366        }
367
368        if (getRingingCall().getState() != ImsPhoneCall.State.IDLE) {
369            if (DBG) Rlog.d(LOG_TAG, "MmiCode 0: rejectCall");
370            try {
371                mCT.rejectCall();
372            } catch (CallStateException e) {
373                if (DBG) Rlog.d(LOG_TAG, "reject failed", e);
374                notifySuppServiceFailed(Phone.SuppService.REJECT);
375            }
376        } else if (getBackgroundCall().getState() != ImsPhoneCall.State.IDLE) {
377            if (DBG) Rlog.d(LOG_TAG, "MmiCode 0: hangupWaitingOrBackground");
378            try {
379                mCT.hangup(getBackgroundCall());
380            } catch (CallStateException e) {
381                if (DBG) Rlog.d(LOG_TAG, "hangup failed", e);
382            }
383        }
384
385        return true;
386    }
387
388    private void sendUssdResponse(String ussdRequest, CharSequence message, int returnCode,
389                                   ResultReceiver wrappedCallback) {
390        UssdResponse response = new UssdResponse(ussdRequest, message);
391        Bundle returnData = new Bundle();
392        returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
393        wrappedCallback.send(returnCode, returnData);
394
395    }
396
397    @Override
398    public boolean handleUssdRequest(String ussdRequest, ResultReceiver wrappedCallback)
399            throws CallStateException {
400        if (mPendingMMIs.size() > 0) {
401            // There are MMI codes in progress; fail attempt now.
402            Rlog.i(LOG_TAG, "handleUssdRequest: queue full: " + Rlog.pii(LOG_TAG, ussdRequest));
403            sendUssdResponse(ussdRequest, null, TelephonyManager.USSD_RETURN_FAILURE,
404                    wrappedCallback );
405            return true;
406        }
407        try {
408            dialInternal(ussdRequest, VideoProfile.STATE_AUDIO_ONLY, null, wrappedCallback);
409        } catch (CallStateException cse) {
410            if (CS_FALLBACK.equals(cse.getMessage())) {
411                throw cse;
412            } else {
413                Rlog.w(LOG_TAG, "Could not execute USSD " + cse);
414                sendUssdResponse(ussdRequest, null, TelephonyManager.USSD_RETURN_FAILURE,
415                        wrappedCallback);
416            }
417        } catch (Exception e) {
418            Rlog.w(LOG_TAG, "Could not execute USSD " + e);
419            sendUssdResponse(ussdRequest, null, TelephonyManager.USSD_RETURN_FAILURE,
420                    wrappedCallback);
421            return false;
422        }
423        return true;
424    }
425
426    private boolean handleCallWaitingIncallSupplementaryService(
427            String dialString) {
428        int len = dialString.length();
429
430        if (len > 2) {
431            return false;
432        }
433
434        ImsPhoneCall call = getForegroundCall();
435
436        try {
437            if (len > 1) {
438                if (DBG) Rlog.d(LOG_TAG, "not support 1X SEND");
439                notifySuppServiceFailed(Phone.SuppService.HANGUP);
440            } else {
441                if (call.getState() != ImsPhoneCall.State.IDLE) {
442                    if (DBG) Rlog.d(LOG_TAG, "MmiCode 1: hangup foreground");
443                    mCT.hangup(call);
444                } else {
445                    if (DBG) Rlog.d(LOG_TAG, "MmiCode 1: switchWaitingOrHoldingAndActive");
446                    mCT.switchWaitingOrHoldingAndActive();
447                }
448            }
449        } catch (CallStateException e) {
450            if (DBG) Rlog.d(LOG_TAG, "hangup failed", e);
451            notifySuppServiceFailed(Phone.SuppService.HANGUP);
452        }
453
454        return true;
455    }
456
457    private boolean handleCallHoldIncallSupplementaryService(String dialString) {
458        int len = dialString.length();
459
460        if (len > 2) {
461            return false;
462        }
463
464        if (len > 1) {
465            if (DBG) Rlog.d(LOG_TAG, "separate not supported");
466            notifySuppServiceFailed(Phone.SuppService.SEPARATE);
467        } else {
468            try {
469                if (getRingingCall().getState() != ImsPhoneCall.State.IDLE) {
470                    if (DBG) Rlog.d(LOG_TAG, "MmiCode 2: accept ringing call");
471                    mCT.acceptCall(ImsCallProfile.CALL_TYPE_VOICE);
472                } else {
473                    if (DBG) Rlog.d(LOG_TAG, "MmiCode 2: switchWaitingOrHoldingAndActive");
474                    mCT.switchWaitingOrHoldingAndActive();
475                }
476            } catch (CallStateException e) {
477                if (DBG) Rlog.d(LOG_TAG, "switch failed", e);
478                notifySuppServiceFailed(Phone.SuppService.SWITCH);
479            }
480        }
481
482        return true;
483    }
484
485    private boolean handleMultipartyIncallSupplementaryService(
486            String dialString) {
487        if (dialString.length() > 1) {
488            return false;
489        }
490
491        if (DBG) Rlog.d(LOG_TAG, "MmiCode 3: merge calls");
492        conference();
493        return true;
494    }
495
496    private boolean handleEctIncallSupplementaryService(String dialString) {
497
498        int len = dialString.length();
499
500        if (len != 1) {
501            return false;
502        }
503
504        if (DBG) Rlog.d(LOG_TAG, "MmiCode 4: not support explicit call transfer");
505        notifySuppServiceFailed(Phone.SuppService.TRANSFER);
506        return true;
507    }
508
509    private boolean handleCcbsIncallSupplementaryService(String dialString) {
510        if (dialString.length() > 1) {
511            return false;
512        }
513
514        Rlog.i(LOG_TAG, "MmiCode 5: CCBS not supported!");
515        // Treat it as an "unknown" service.
516        notifySuppServiceFailed(Phone.SuppService.UNKNOWN);
517        return true;
518    }
519
520    public void notifySuppSvcNotification(SuppServiceNotification suppSvc) {
521        Rlog.d(LOG_TAG, "notifySuppSvcNotification: suppSvc = " + suppSvc);
522
523        AsyncResult ar = new AsyncResult(null, suppSvc, null);
524        mSsnRegistrants.notifyRegistrants(ar);
525    }
526
527    @Override
528    public boolean handleInCallMmiCommands(String dialString) {
529        if (!isInCall()) {
530            return false;
531        }
532
533        if (TextUtils.isEmpty(dialString)) {
534            return false;
535        }
536
537        boolean result = false;
538        char ch = dialString.charAt(0);
539        switch (ch) {
540            case '0':
541                result = handleCallDeflectionIncallSupplementaryService(
542                        dialString);
543                break;
544            case '1':
545                result = handleCallWaitingIncallSupplementaryService(
546                        dialString);
547                break;
548            case '2':
549                result = handleCallHoldIncallSupplementaryService(dialString);
550                break;
551            case '3':
552                result = handleMultipartyIncallSupplementaryService(dialString);
553                break;
554            case '4':
555                result = handleEctIncallSupplementaryService(dialString);
556                break;
557            case '5':
558                result = handleCcbsIncallSupplementaryService(dialString);
559                break;
560            default:
561                break;
562        }
563
564        return result;
565    }
566
567    boolean isInCall() {
568        ImsPhoneCall.State foregroundCallState = getForegroundCall().getState();
569        ImsPhoneCall.State backgroundCallState = getBackgroundCall().getState();
570        ImsPhoneCall.State ringingCallState = getRingingCall().getState();
571
572       return (foregroundCallState.isAlive() ||
573               backgroundCallState.isAlive() ||
574               ringingCallState.isAlive());
575    }
576
577    @Override
578    public boolean isInEcm() {
579        return mDefaultPhone.isInEcm();
580    }
581
582    @Override
583    public void setIsInEcm(boolean isInEcm){
584        mDefaultPhone.setIsInEcm(isInEcm);
585    }
586
587    public void notifyNewRingingConnection(Connection c) {
588        mDefaultPhone.notifyNewRingingConnectionP(c);
589    }
590
591    void notifyUnknownConnection(Connection c) {
592        mDefaultPhone.notifyUnknownConnectionP(c);
593    }
594
595    @Override
596    public void notifyForVideoCapabilityChanged(boolean isVideoCapable) {
597        mIsVideoCapable = isVideoCapable;
598        mDefaultPhone.notifyForVideoCapabilityChanged(isVideoCapable);
599    }
600
601    @Override
602    public Connection
603    dial(String dialString, int videoState) throws CallStateException {
604        return dialInternal(dialString, videoState, null, null);
605    }
606
607    @Override
608    public Connection
609    dial(String dialString, UUSInfo uusInfo, int videoState, Bundle intentExtras)
610            throws CallStateException {
611        // ignore UUSInfo
612        return dialInternal (dialString, videoState, intentExtras, null);
613    }
614
615    protected Connection dialInternal(String dialString, int videoState, Bundle intentExtras)
616            throws CallStateException {
617        return dialInternal(dialString, videoState, intentExtras, null);
618    }
619
620    private Connection dialInternal(String dialString, int videoState,
621                                    Bundle intentExtras, ResultReceiver wrappedCallback)
622            throws CallStateException {
623        // Need to make sure dialString gets parsed properly
624        String newDialString = PhoneNumberUtils.stripSeparators(dialString);
625
626        // handle in-call MMI first if applicable
627        if (handleInCallMmiCommands(newDialString)) {
628            return null;
629        }
630
631        if (mDefaultPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
632            return mCT.dial(dialString, videoState, intentExtras);
633        }
634
635        // Only look at the Network portion for mmi
636        String networkPortion = PhoneNumberUtils.extractNetworkPortionAlt(newDialString);
637        ImsPhoneMmiCode mmi =
638                ImsPhoneMmiCode.newFromDialString(networkPortion, this, wrappedCallback);
639        if (DBG) Rlog.d(LOG_TAG,
640                "dialInternal: dialing w/ mmi '" + mmi + "'...");
641
642        if (mmi == null) {
643            return mCT.dial(dialString, videoState, intentExtras);
644        } else if (mmi.isTemporaryModeCLIR()) {
645            return mCT.dial(mmi.getDialingNumber(), mmi.getCLIRMode(), videoState, intentExtras);
646        } else if (!mmi.isSupportedOverImsPhone()) {
647            // If the mmi is not supported by IMS service,
648            // try to initiate dialing with default phone
649            // Note: This code is never reached; there is a bug in isSupportedOverImsPhone which
650            // causes it to return true even though the "processCode" method ultimately throws the
651            // exception.
652            Rlog.i(LOG_TAG, "dialInternal: USSD not supported by IMS; fallback to CS.");
653            throw new CallStateException(CS_FALLBACK);
654        } else {
655            mPendingMMIs.add(mmi);
656            mMmiRegistrants.notifyRegistrants(new AsyncResult(null, mmi, null));
657
658            try {
659                mmi.processCode();
660            } catch (CallStateException cse) {
661                if (CS_FALLBACK.equals(cse.getMessage())) {
662                    Rlog.i(LOG_TAG, "dialInternal: fallback to GSM required.");
663                    // Make sure we remove from the list of pending MMIs since it will handover to
664                    // GSM.
665                    mPendingMMIs.remove(mmi);
666                    throw cse;
667                }
668            }
669
670            return null;
671        }
672    }
673
674    @Override
675    public void
676    sendDtmf(char c) {
677        if (!PhoneNumberUtils.is12Key(c)) {
678            Rlog.e(LOG_TAG,
679                    "sendDtmf called with invalid character '" + c + "'");
680        } else {
681            if (mCT.getState() ==  PhoneConstants.State.OFFHOOK) {
682                mCT.sendDtmf(c, null);
683            }
684        }
685    }
686
687    @Override
688    public void
689    startDtmf(char c) {
690        if (!(PhoneNumberUtils.is12Key(c) || (c >= 'A' && c <= 'D'))) {
691            Rlog.e(LOG_TAG,
692                    "startDtmf called with invalid character '" + c + "'");
693        } else {
694            mCT.startDtmf(c);
695        }
696    }
697
698    @Override
699    public void
700    stopDtmf() {
701        mCT.stopDtmf();
702    }
703
704    public void notifyIncomingRing() {
705        if (DBG) Rlog.d(LOG_TAG, "notifyIncomingRing");
706        AsyncResult ar = new AsyncResult(null, null, null);
707        sendMessage(obtainMessage(EVENT_CALL_RING, ar));
708    }
709
710    @Override
711    public void setMute(boolean muted) {
712        mCT.setMute(muted);
713    }
714
715    @Override
716    public void setTTYMode(int ttyMode, Message onComplete) {
717        mCT.setTtyMode(ttyMode);
718    }
719
720    @Override
721    public void setUiTTYMode(int uiTtyMode, Message onComplete) {
722        mCT.setUiTTYMode(uiTtyMode, onComplete);
723    }
724
725    @Override
726    public boolean getMute() {
727        return mCT.getMute();
728    }
729
730    @Override
731    public PhoneConstants.State getState() {
732        return mCT.getState();
733    }
734
735    private boolean isValidCommandInterfaceCFReason (int commandInterfaceCFReason) {
736        switch (commandInterfaceCFReason) {
737        case CF_REASON_UNCONDITIONAL:
738        case CF_REASON_BUSY:
739        case CF_REASON_NO_REPLY:
740        case CF_REASON_NOT_REACHABLE:
741        case CF_REASON_ALL:
742        case CF_REASON_ALL_CONDITIONAL:
743            return true;
744        default:
745            return false;
746        }
747    }
748
749    private boolean isValidCommandInterfaceCFAction (int commandInterfaceCFAction) {
750        switch (commandInterfaceCFAction) {
751        case CF_ACTION_DISABLE:
752        case CF_ACTION_ENABLE:
753        case CF_ACTION_REGISTRATION:
754        case CF_ACTION_ERASURE:
755            return true;
756        default:
757            return false;
758        }
759    }
760
761    private  boolean isCfEnable(int action) {
762        return (action == CF_ACTION_ENABLE) || (action == CF_ACTION_REGISTRATION);
763    }
764
765    private int getConditionFromCFReason(int reason) {
766        switch(reason) {
767            case CF_REASON_UNCONDITIONAL: return ImsUtInterface.CDIV_CF_UNCONDITIONAL;
768            case CF_REASON_BUSY: return ImsUtInterface.CDIV_CF_BUSY;
769            case CF_REASON_NO_REPLY: return ImsUtInterface.CDIV_CF_NO_REPLY;
770            case CF_REASON_NOT_REACHABLE: return ImsUtInterface.CDIV_CF_NOT_REACHABLE;
771            case CF_REASON_ALL: return ImsUtInterface.CDIV_CF_ALL;
772            case CF_REASON_ALL_CONDITIONAL: return ImsUtInterface.CDIV_CF_ALL_CONDITIONAL;
773            default:
774                break;
775        }
776
777        return ImsUtInterface.INVALID;
778    }
779
780    private int getCFReasonFromCondition(int condition) {
781        switch(condition) {
782            case ImsUtInterface.CDIV_CF_UNCONDITIONAL: return CF_REASON_UNCONDITIONAL;
783            case ImsUtInterface.CDIV_CF_BUSY: return CF_REASON_BUSY;
784            case ImsUtInterface.CDIV_CF_NO_REPLY: return CF_REASON_NO_REPLY;
785            case ImsUtInterface.CDIV_CF_NOT_REACHABLE: return CF_REASON_NOT_REACHABLE;
786            case ImsUtInterface.CDIV_CF_ALL: return CF_REASON_ALL;
787            case ImsUtInterface.CDIV_CF_ALL_CONDITIONAL: return CF_REASON_ALL_CONDITIONAL;
788            default:
789                break;
790        }
791
792        return CF_REASON_NOT_REACHABLE;
793    }
794
795    private int getActionFromCFAction(int action) {
796        switch(action) {
797            case CF_ACTION_DISABLE: return ImsUtInterface.ACTION_DEACTIVATION;
798            case CF_ACTION_ENABLE: return ImsUtInterface.ACTION_ACTIVATION;
799            case CF_ACTION_ERASURE: return ImsUtInterface.ACTION_ERASURE;
800            case CF_ACTION_REGISTRATION: return ImsUtInterface.ACTION_REGISTRATION;
801            default:
802                break;
803        }
804
805        return ImsUtInterface.INVALID;
806    }
807
808    @Override
809    public void getOutgoingCallerIdDisplay(Message onComplete) {
810        if (DBG) Rlog.d(LOG_TAG, "getCLIR");
811        Message resp;
812        resp = obtainMessage(EVENT_GET_CLIR_DONE, onComplete);
813
814        try {
815            ImsUtInterface ut = mCT.getUtInterface();
816            ut.queryCLIR(resp);
817        } catch (ImsException e) {
818            sendErrorResponse(onComplete, e);
819        }
820    }
821
822    @Override
823    public void setOutgoingCallerIdDisplay(int clirMode, Message onComplete) {
824        if (DBG) Rlog.d(LOG_TAG, "setCLIR action= " + clirMode);
825        Message resp;
826        // Packing CLIR value in the message. This will be required for
827        // SharedPreference caching, if the message comes back as part of
828        // a success response.
829        resp = obtainMessage(EVENT_SET_CLIR_DONE, clirMode, 0, onComplete);
830        try {
831            ImsUtInterface ut = mCT.getUtInterface();
832            ut.updateCLIR(clirMode, resp);
833        } catch (ImsException e) {
834            sendErrorResponse(onComplete, e);
835        }
836    }
837
838    @Override
839    public void getCallForwardingOption(int commandInterfaceCFReason,
840            Message onComplete) {
841        if (DBG) Rlog.d(LOG_TAG, "getCallForwardingOption reason=" + commandInterfaceCFReason);
842        if (isValidCommandInterfaceCFReason(commandInterfaceCFReason)) {
843            if (DBG) Rlog.d(LOG_TAG, "requesting call forwarding query.");
844            Message resp;
845            resp = obtainMessage(EVENT_GET_CALL_FORWARD_DONE, onComplete);
846
847            try {
848                ImsUtInterface ut = mCT.getUtInterface();
849                ut.queryCallForward(getConditionFromCFReason(commandInterfaceCFReason), null, resp);
850            } catch (ImsException e) {
851                sendErrorResponse(onComplete, e);
852            }
853        } else if (onComplete != null) {
854            sendErrorResponse(onComplete);
855        }
856    }
857
858    @Override
859    public void setCallForwardingOption(int commandInterfaceCFAction,
860            int commandInterfaceCFReason,
861            String dialingNumber,
862            int timerSeconds,
863            Message onComplete) {
864        setCallForwardingOption(commandInterfaceCFAction, commandInterfaceCFReason, dialingNumber,
865                CommandsInterface.SERVICE_CLASS_VOICE, timerSeconds, onComplete);
866    }
867
868    public void setCallForwardingOption(int commandInterfaceCFAction,
869            int commandInterfaceCFReason,
870            String dialingNumber,
871            int serviceClass,
872            int timerSeconds,
873            Message onComplete) {
874        if (DBG) Rlog.d(LOG_TAG, "setCallForwardingOption action=" + commandInterfaceCFAction
875                + ", reason=" + commandInterfaceCFReason + " serviceClass=" + serviceClass);
876        if ((isValidCommandInterfaceCFAction(commandInterfaceCFAction)) &&
877                (isValidCommandInterfaceCFReason(commandInterfaceCFReason))) {
878            Message resp;
879            Cf cf = new Cf(dialingNumber, GsmMmiCode.isVoiceUnconditionalForwarding(
880                    commandInterfaceCFReason, serviceClass), onComplete);
881            resp = obtainMessage(EVENT_SET_CALL_FORWARD_DONE,
882                    isCfEnable(commandInterfaceCFAction) ? 1 : 0, 0, cf);
883
884            try {
885                ImsUtInterface ut = mCT.getUtInterface();
886                ut.updateCallForward(getActionFromCFAction(commandInterfaceCFAction),
887                        getConditionFromCFReason(commandInterfaceCFReason),
888                        dialingNumber,
889                        serviceClass,
890                        timerSeconds,
891                        resp);
892            } catch (ImsException e) {
893                sendErrorResponse(onComplete, e);
894            }
895        } else if (onComplete != null) {
896            sendErrorResponse(onComplete);
897        }
898    }
899
900    @Override
901    public void getCallWaiting(Message onComplete) {
902        if (DBG) Rlog.d(LOG_TAG, "getCallWaiting");
903        Message resp;
904        resp = obtainMessage(EVENT_GET_CALL_WAITING_DONE, onComplete);
905
906        try {
907            ImsUtInterface ut = mCT.getUtInterface();
908            ut.queryCallWaiting(resp);
909        } catch (ImsException e) {
910            sendErrorResponse(onComplete, e);
911        }
912    }
913
914    @Override
915    public void setCallWaiting(boolean enable, Message onComplete) {
916        setCallWaiting(enable, CommandsInterface.SERVICE_CLASS_VOICE, onComplete);
917    }
918
919    public void setCallWaiting(boolean enable, int serviceClass, Message onComplete) {
920        if (DBG) Rlog.d(LOG_TAG, "setCallWaiting enable=" + enable);
921        Message resp;
922        resp = obtainMessage(EVENT_SET_CALL_WAITING_DONE, onComplete);
923
924        try {
925            ImsUtInterface ut = mCT.getUtInterface();
926            ut.updateCallWaiting(enable, serviceClass, resp);
927        } catch (ImsException e) {
928            sendErrorResponse(onComplete, e);
929        }
930    }
931
932    private int getCBTypeFromFacility(String facility) {
933        if (CB_FACILITY_BAOC.equals(facility)) {
934            return ImsUtInterface.CB_BAOC;
935        } else if (CB_FACILITY_BAOIC.equals(facility)) {
936            return ImsUtInterface.CB_BOIC;
937        } else if (CB_FACILITY_BAOICxH.equals(facility)) {
938            return ImsUtInterface.CB_BOIC_EXHC;
939        } else if (CB_FACILITY_BAIC.equals(facility)) {
940            return ImsUtInterface.CB_BAIC;
941        } else if (CB_FACILITY_BAICr.equals(facility)) {
942            return ImsUtInterface.CB_BIC_WR;
943        } else if (CB_FACILITY_BA_ALL.equals(facility)) {
944            return ImsUtInterface.CB_BA_ALL;
945        } else if (CB_FACILITY_BA_MO.equals(facility)) {
946            return ImsUtInterface.CB_BA_MO;
947        } else if (CB_FACILITY_BA_MT.equals(facility)) {
948            return ImsUtInterface.CB_BA_MT;
949        }
950
951        return 0;
952    }
953
954    public void getCallBarring(String facility, Message onComplete) {
955        getCallBarring(facility, onComplete, CommandsInterface.SERVICE_CLASS_NONE);
956    }
957
958    public void getCallBarring(String facility, Message onComplete, int serviceClass) {
959        if (DBG) {
960            Rlog.d(LOG_TAG, "getCallBarring facility=" + facility
961                    + ", serviceClass = " + serviceClass);
962        }
963        Message resp;
964        resp = obtainMessage(EVENT_GET_CALL_BARRING_DONE, onComplete);
965
966        try {
967            ImsUtInterface ut = mCT.getUtInterface();
968            ut.queryCallBarring(getCBTypeFromFacility(facility), resp, serviceClass);
969        } catch (ImsException e) {
970            sendErrorResponse(onComplete, e);
971        }
972    }
973
974    public void setCallBarring(String facility, boolean lockState, String password,
975            Message onComplete) {
976        setCallBarring(facility, lockState, password, onComplete,
977                CommandsInterface.SERVICE_CLASS_NONE);
978    }
979
980    public void setCallBarring(String facility, boolean lockState, String password,
981            Message onComplete,  int serviceClass) {
982        if (DBG) {
983            Rlog.d(LOG_TAG, "setCallBarring facility=" + facility
984                    + ", lockState=" + lockState + ", serviceClass = " + serviceClass);
985        }
986        Message resp;
987        resp = obtainMessage(EVENT_SET_CALL_BARRING_DONE, onComplete);
988
989        int action;
990        if (lockState) {
991            action = CommandsInterface.CF_ACTION_ENABLE;
992        }
993        else {
994            action = CommandsInterface.CF_ACTION_DISABLE;
995        }
996
997        try {
998            ImsUtInterface ut = mCT.getUtInterface();
999            // password is not required with Ut interface
1000            ut.updateCallBarring(getCBTypeFromFacility(facility), action,
1001                    resp, null,  serviceClass);
1002        } catch (ImsException e) {
1003            sendErrorResponse(onComplete, e);
1004        }
1005    }
1006
1007    @Override
1008    public void sendUssdResponse(String ussdMessge) {
1009        Rlog.d(LOG_TAG, "sendUssdResponse");
1010        ImsPhoneMmiCode mmi = ImsPhoneMmiCode.newFromUssdUserInput(ussdMessge, this);
1011        mPendingMMIs.add(mmi);
1012        mMmiRegistrants.notifyRegistrants(new AsyncResult(null, mmi, null));
1013        mmi.sendUssd(ussdMessge);
1014    }
1015
1016    public void sendUSSD(String ussdString, Message response) {
1017        mCT.sendUSSD(ussdString, response);
1018    }
1019
1020    @Override
1021    public void cancelUSSD() {
1022        mCT.cancelUSSD();
1023    }
1024
1025    private void sendErrorResponse(Message onComplete) {
1026        Rlog.d(LOG_TAG, "sendErrorResponse");
1027        if (onComplete != null) {
1028            AsyncResult.forMessage(onComplete, null,
1029                    new CommandException(CommandException.Error.GENERIC_FAILURE));
1030            onComplete.sendToTarget();
1031        }
1032    }
1033
1034    @VisibleForTesting
1035    public void sendErrorResponse(Message onComplete, Throwable e) {
1036        Rlog.d(LOG_TAG, "sendErrorResponse");
1037        if (onComplete != null) {
1038            AsyncResult.forMessage(onComplete, null, getCommandException(e));
1039            onComplete.sendToTarget();
1040        }
1041    }
1042
1043    private CommandException getCommandException(int code, String errorString) {
1044        Rlog.d(LOG_TAG, "getCommandException code= " + code
1045                + ", errorString= " + errorString);
1046        CommandException.Error error = CommandException.Error.GENERIC_FAILURE;
1047
1048        switch(code) {
1049            case ImsReasonInfo.CODE_UT_NOT_SUPPORTED:
1050                error = CommandException.Error.REQUEST_NOT_SUPPORTED;
1051                break;
1052            case ImsReasonInfo.CODE_UT_CB_PASSWORD_MISMATCH:
1053                error = CommandException.Error.PASSWORD_INCORRECT;
1054                break;
1055            case ImsReasonInfo.CODE_UT_SERVICE_UNAVAILABLE:
1056                error = CommandException.Error.RADIO_NOT_AVAILABLE;
1057                break;
1058            case ImsReasonInfo.CODE_FDN_BLOCKED:
1059                error = CommandException.Error.FDN_CHECK_FAILURE;
1060            default:
1061                break;
1062        }
1063
1064        return new CommandException(error, errorString);
1065    }
1066
1067    private CommandException getCommandException(Throwable e) {
1068        CommandException ex = null;
1069
1070        if (e instanceof ImsException) {
1071            ex = getCommandException(((ImsException)e).getCode(), e.getMessage());
1072        } else {
1073            Rlog.d(LOG_TAG, "getCommandException generic failure");
1074            ex = new CommandException(CommandException.Error.GENERIC_FAILURE);
1075        }
1076        return ex;
1077    }
1078
1079    private void
1080    onNetworkInitiatedUssd(ImsPhoneMmiCode mmi) {
1081        Rlog.d(LOG_TAG, "onNetworkInitiatedUssd");
1082        mMmiCompleteRegistrants.notifyRegistrants(
1083            new AsyncResult(null, mmi, null));
1084    }
1085
1086    /* package */
1087    void onIncomingUSSD(int ussdMode, String ussdMessage) {
1088        if (DBG) Rlog.d(LOG_TAG, "onIncomingUSSD ussdMode=" + ussdMode);
1089
1090        boolean isUssdError;
1091        boolean isUssdRequest;
1092
1093        isUssdRequest
1094            = (ussdMode == CommandsInterface.USSD_MODE_REQUEST);
1095
1096        isUssdError
1097            = (ussdMode != CommandsInterface.USSD_MODE_NOTIFY
1098                && ussdMode != CommandsInterface.USSD_MODE_REQUEST);
1099
1100        ImsPhoneMmiCode found = null;
1101        for (int i = 0, s = mPendingMMIs.size() ; i < s; i++) {
1102            if(mPendingMMIs.get(i).isPendingUSSD()) {
1103                found = mPendingMMIs.get(i);
1104                break;
1105            }
1106        }
1107
1108        if (found != null) {
1109            // Complete pending USSD
1110            if (isUssdError) {
1111                found.onUssdFinishedError();
1112            } else {
1113                found.onUssdFinished(ussdMessage, isUssdRequest);
1114            }
1115        } else if (!isUssdError && ussdMessage != null) {
1116                // pending USSD not found
1117                // The network may initiate its own USSD request
1118
1119                // ignore everything that isnt a Notify or a Request
1120                // also, discard if there is no message to present
1121                ImsPhoneMmiCode mmi;
1122                mmi = ImsPhoneMmiCode.newNetworkInitiatedUssd(ussdMessage,
1123                        isUssdRequest,
1124                        this);
1125                onNetworkInitiatedUssd(mmi);
1126        }
1127    }
1128
1129    /**
1130     * Removes the given MMI from the pending list and notifies
1131     * registrants that it is complete.
1132     * @param mmi MMI that is done
1133     */
1134    public void onMMIDone(ImsPhoneMmiCode mmi) {
1135        /* Only notify complete if it's on the pending list.
1136         * Otherwise, it's already been handled (eg, previously canceled).
1137         * The exception is cancellation of an incoming USSD-REQUEST, which is
1138         * not on the list.
1139         */
1140        Rlog.d(LOG_TAG, "onMMIDone: mmi=" + mmi);
1141        if (mPendingMMIs.remove(mmi) || mmi.isUssdRequest()) {
1142            ResultReceiver receiverCallback = mmi.getUssdCallbackReceiver();
1143            if (receiverCallback != null) {
1144                int returnCode = (mmi.getState() ==  MmiCode.State.COMPLETE) ?
1145                        TelephonyManager.USSD_RETURN_SUCCESS : TelephonyManager.USSD_RETURN_FAILURE;
1146                sendUssdResponse(mmi.getDialString(), mmi.getMessage(), returnCode,
1147                        receiverCallback );
1148            } else {
1149                Rlog.v(LOG_TAG, "onMMIDone: notifyRegistrants");
1150                mMmiCompleteRegistrants.notifyRegistrants(
1151                    new AsyncResult(null, mmi, null));
1152            }
1153        }
1154    }
1155
1156    @Override
1157    public ArrayList<Connection> getHandoverConnection() {
1158        ArrayList<Connection> connList = new ArrayList<Connection>();
1159        // Add all foreground call connections
1160        connList.addAll(getForegroundCall().mConnections);
1161        // Add all background call connections
1162        connList.addAll(getBackgroundCall().mConnections);
1163        // Add all background call connections
1164        connList.addAll(getRingingCall().mConnections);
1165        if (connList.size() > 0) {
1166            return connList;
1167        } else {
1168            return null;
1169        }
1170    }
1171
1172    @Override
1173    public void notifySrvccState(Call.SrvccState state) {
1174        mCT.notifySrvccState(state);
1175    }
1176
1177    /* package */ void
1178    initiateSilentRedial() {
1179        String result = mLastDialString;
1180        AsyncResult ar = new AsyncResult(null, result, null);
1181        if (ar != null) {
1182            mSilentRedialRegistrants.notifyRegistrants(ar);
1183        }
1184    }
1185
1186    @Override
1187    public void registerForSilentRedial(Handler h, int what, Object obj) {
1188        mSilentRedialRegistrants.addUnique(h, what, obj);
1189    }
1190
1191    @Override
1192    public void unregisterForSilentRedial(Handler h) {
1193        mSilentRedialRegistrants.remove(h);
1194    }
1195
1196    @Override
1197    public void registerForSuppServiceNotification(Handler h, int what, Object obj) {
1198        mSsnRegistrants.addUnique(h, what, obj);
1199    }
1200
1201    @Override
1202    public void unregisterForSuppServiceNotification(Handler h) {
1203        mSsnRegistrants.remove(h);
1204    }
1205
1206    @Override
1207    public int getSubId() {
1208        return mDefaultPhone.getSubId();
1209    }
1210
1211    @Override
1212    public int getPhoneId() {
1213        return mDefaultPhone.getPhoneId();
1214    }
1215
1216    private CallForwardInfo getCallForwardInfo(ImsCallForwardInfo info) {
1217        CallForwardInfo cfInfo = new CallForwardInfo();
1218        cfInfo.status = info.mStatus;
1219        cfInfo.reason = getCFReasonFromCondition(info.mCondition);
1220        cfInfo.serviceClass = SERVICE_CLASS_VOICE;
1221        cfInfo.toa = info.mToA;
1222        cfInfo.number = info.mNumber;
1223        cfInfo.timeSeconds = info.mTimeSeconds;
1224        return cfInfo;
1225    }
1226
1227    private CallForwardInfo[] handleCfQueryResult(ImsCallForwardInfo[] infos) {
1228        CallForwardInfo[] cfInfos = null;
1229
1230        if (infos != null && infos.length != 0) {
1231            cfInfos = new CallForwardInfo[infos.length];
1232        }
1233
1234        IccRecords r = mDefaultPhone.getIccRecords();
1235        if (infos == null || infos.length == 0) {
1236            if (r != null) {
1237                // Assume the default is not active
1238                // Set unconditional CFF in SIM to false
1239                setVoiceCallForwardingFlag(r, 1, false, null);
1240            }
1241        } else {
1242            for (int i = 0, s = infos.length; i < s; i++) {
1243                if (infos[i].mCondition == ImsUtInterface.CDIV_CF_UNCONDITIONAL) {
1244                    if (r != null) {
1245                        setVoiceCallForwardingFlag(r, 1, (infos[i].mStatus == 1),
1246                            infos[i].mNumber);
1247                    }
1248                }
1249                cfInfos[i] = getCallForwardInfo(infos[i]);
1250            }
1251        }
1252
1253        return cfInfos;
1254    }
1255
1256    private int[] handleCbQueryResult(ImsSsInfo[] infos) {
1257        int[] cbInfos = new int[1];
1258        cbInfos[0] = SERVICE_CLASS_NONE;
1259
1260        if (infos[0].mStatus == 1) {
1261            cbInfos[0] = SERVICE_CLASS_VOICE;
1262        }
1263
1264        return cbInfos;
1265    }
1266
1267    private int[] handleCwQueryResult(ImsSsInfo[] infos) {
1268        int[] cwInfos = new int[2];
1269        cwInfos[0] = 0;
1270
1271        if (infos[0].mStatus == 1) {
1272            cwInfos[0] = 1;
1273            cwInfos[1] = SERVICE_CLASS_VOICE;
1274        }
1275
1276        return cwInfos;
1277    }
1278
1279    private void
1280    sendResponse(Message onComplete, Object result, Throwable e) {
1281        if (onComplete != null) {
1282            CommandException ex = null;
1283            if (e != null) {
1284                ex = getCommandException(e);
1285            }
1286            AsyncResult.forMessage(onComplete, result, ex);
1287            onComplete.sendToTarget();
1288        }
1289    }
1290
1291    private void updateDataServiceState() {
1292        if (mSS != null && mDefaultPhone.getServiceStateTracker() != null
1293                && mDefaultPhone.getServiceStateTracker().mSS != null) {
1294            ServiceState ss = mDefaultPhone.getServiceStateTracker().mSS;
1295            mSS.setDataRegState(ss.getDataRegState());
1296            mSS.setRilDataRadioTechnology(ss.getRilDataRadioTechnology());
1297            Rlog.d(LOG_TAG, "updateDataServiceState: defSs = " + ss + " imsSs = " + mSS);
1298        }
1299    }
1300
1301    @Override
1302    public void handleMessage(Message msg) {
1303        AsyncResult ar = (AsyncResult) msg.obj;
1304
1305        if (DBG) Rlog.d(LOG_TAG, "handleMessage what=" + msg.what);
1306        switch (msg.what) {
1307            case EVENT_SET_CALL_FORWARD_DONE:
1308                IccRecords r = mDefaultPhone.getIccRecords();
1309                Cf cf = (Cf) ar.userObj;
1310                if (cf.mIsCfu && ar.exception == null && r != null) {
1311                    setVoiceCallForwardingFlag(r, 1, msg.arg1 == 1, cf.mSetCfNumber);
1312                }
1313                sendResponse(cf.mOnComplete, null, ar.exception);
1314                break;
1315
1316            case EVENT_GET_CALL_FORWARD_DONE:
1317                CallForwardInfo[] cfInfos = null;
1318                if (ar.exception == null) {
1319                    cfInfos = handleCfQueryResult((ImsCallForwardInfo[])ar.result);
1320                }
1321                sendResponse((Message) ar.userObj, cfInfos, ar.exception);
1322                break;
1323
1324            case EVENT_GET_CALL_BARRING_DONE:
1325            case EVENT_GET_CALL_WAITING_DONE:
1326                int[] ssInfos = null;
1327                if (ar.exception == null) {
1328                    if (msg.what == EVENT_GET_CALL_BARRING_DONE) {
1329                        ssInfos = handleCbQueryResult((ImsSsInfo[])ar.result);
1330                    } else if (msg.what == EVENT_GET_CALL_WAITING_DONE) {
1331                        ssInfos = handleCwQueryResult((ImsSsInfo[])ar.result);
1332                    }
1333                }
1334                sendResponse((Message) ar.userObj, ssInfos, ar.exception);
1335                break;
1336
1337            case EVENT_GET_CLIR_DONE:
1338                Bundle ssInfo = (Bundle) ar.result;
1339                int[] clirInfo = null;
1340                if (ssInfo != null) {
1341                    clirInfo = ssInfo.getIntArray(ImsPhoneMmiCode.UT_BUNDLE_KEY_CLIR);
1342                }
1343                sendResponse((Message) ar.userObj, clirInfo, ar.exception);
1344                break;
1345
1346            case EVENT_SET_CLIR_DONE:
1347                if (ar.exception == null) {
1348                    saveClirSetting(msg.arg1);
1349                }
1350                 // (Intentional fallthrough)
1351            case EVENT_SET_CALL_BARRING_DONE:
1352            case EVENT_SET_CALL_WAITING_DONE:
1353                sendResponse((Message) ar.userObj, null, ar.exception);
1354                break;
1355
1356            case EVENT_DEFAULT_PHONE_DATA_STATE_CHANGED:
1357                if (DBG) Rlog.d(LOG_TAG, "EVENT_DEFAULT_PHONE_DATA_STATE_CHANGED");
1358                updateDataServiceState();
1359                break;
1360
1361            case EVENT_SERVICE_STATE_CHANGED:
1362                if (VDBG) Rlog.d(LOG_TAG, "EVENT_SERVICE_STATE_CHANGED");
1363                ar = (AsyncResult) msg.obj;
1364                ServiceState newServiceState = (ServiceState) ar.result;
1365                // only update if roaming status changed
1366                if (mRoaming != newServiceState.getRoaming()) {
1367                    if (DBG) Rlog.d(LOG_TAG, "Roaming state changed");
1368                    updateRoamingState(newServiceState.getRoaming());
1369                }
1370                break;
1371            case EVENT_VOICE_CALL_ENDED:
1372                if (DBG) Rlog.d(LOG_TAG, "Voice call ended. Handle pending updateRoamingState.");
1373                mCT.unregisterForVoiceCallEnded(this);
1374                // only update if roaming status changed
1375                boolean newRoaming = getCurrentRoaming();
1376                if (mRoaming != newRoaming) {
1377                    updateRoamingState(newRoaming);
1378                }
1379                break;
1380
1381            default:
1382                super.handleMessage(msg);
1383                break;
1384        }
1385    }
1386
1387    /**
1388     * Listen to the IMS ECBM state change
1389     */
1390    private ImsEcbmStateListener mImsEcbmStateListener =
1391            new ImsEcbmStateListener() {
1392                @Override
1393                public void onECBMEntered() {
1394                    if (DBG) Rlog.d(LOG_TAG, "onECBMEntered");
1395                    handleEnterEmergencyCallbackMode();
1396                }
1397
1398                @Override
1399                public void onECBMExited() {
1400                    if (DBG) Rlog.d(LOG_TAG, "onECBMExited");
1401                    handleExitEmergencyCallbackMode();
1402                }
1403            };
1404
1405    @VisibleForTesting
1406    public ImsEcbmStateListener getImsEcbmStateListener() {
1407        return mImsEcbmStateListener;
1408    }
1409
1410    @Override
1411    public boolean isInEmergencyCall() {
1412        return mCT.isInEmergencyCall();
1413    }
1414
1415    private void sendEmergencyCallbackModeChange() {
1416        // Send an Intent
1417        Intent intent = new Intent(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
1418        intent.putExtra(PhoneConstants.PHONE_IN_ECM_STATE, isInEcm());
1419        SubscriptionManager.putPhoneIdAndSubIdExtra(intent, getPhoneId());
1420        ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
1421        if (DBG) Rlog.d(LOG_TAG, "sendEmergencyCallbackModeChange: isInEcm=" + isInEcm());
1422    }
1423
1424    @Override
1425    public void exitEmergencyCallbackMode() {
1426        if (mWakeLock.isHeld()) {
1427            mWakeLock.release();
1428        }
1429        if (DBG) Rlog.d(LOG_TAG, "exitEmergencyCallbackMode()");
1430
1431        // Send a message which will invoke handleExitEmergencyCallbackMode
1432        ImsEcbm ecbm;
1433        try {
1434            ecbm = mCT.getEcbmInterface();
1435            ecbm.exitEmergencyCallbackMode();
1436        } catch (ImsException e) {
1437            e.printStackTrace();
1438        }
1439    }
1440
1441    private void handleEnterEmergencyCallbackMode() {
1442        if (DBG) {
1443            Rlog.d(LOG_TAG, "handleEnterEmergencyCallbackMode,mIsPhoneInEcmState= "
1444                    + isInEcm());
1445        }
1446        // if phone is not in Ecm mode, and it's changed to Ecm mode
1447        if (!isInEcm()) {
1448            setIsInEcm(true);
1449            // notify change
1450            sendEmergencyCallbackModeChange();
1451
1452            // Post this runnable so we will automatically exit
1453            // if no one invokes exitEmergencyCallbackMode() directly.
1454            long delayInMillis = SystemProperties.getLong(
1455                    TelephonyProperties.PROPERTY_ECM_EXIT_TIMER, DEFAULT_ECM_EXIT_TIMER_VALUE);
1456            postDelayed(mExitEcmRunnable, delayInMillis);
1457            // We don't want to go to sleep while in Ecm
1458            mWakeLock.acquire();
1459        }
1460    }
1461
1462    @Override
1463    protected void handleExitEmergencyCallbackMode() {
1464        if (DBG) {
1465            Rlog.d(LOG_TAG, "handleExitEmergencyCallbackMode: mIsPhoneInEcmState = "
1466                    + isInEcm());
1467        }
1468
1469        if (isInEcm()) {
1470            setIsInEcm(false);
1471        }
1472
1473        // Remove pending exit Ecm runnable, if any
1474        removeCallbacks(mExitEcmRunnable);
1475
1476        if (mEcmExitRespRegistrant != null) {
1477            mEcmExitRespRegistrant.notifyResult(Boolean.TRUE);
1478        }
1479
1480        // release wakeLock
1481        if (mWakeLock.isHeld()) {
1482            mWakeLock.release();
1483        }
1484
1485        // send an Intent
1486        sendEmergencyCallbackModeChange();
1487        ((GsmCdmaPhone) mDefaultPhone).notifyEmergencyCallRegistrants(false);
1488    }
1489
1490    /**
1491     * Handle to cancel or restart Ecm timer in emergency call back mode if action is
1492     * CANCEL_ECM_TIMER, cancel Ecm timer and notify apps the timer is canceled; otherwise, restart
1493     * Ecm timer and notify apps the timer is restarted.
1494     */
1495    void handleTimerInEmergencyCallbackMode(int action) {
1496        switch (action) {
1497            case CANCEL_ECM_TIMER:
1498                removeCallbacks(mExitEcmRunnable);
1499                ((GsmCdmaPhone) mDefaultPhone).notifyEcbmTimerReset(Boolean.TRUE);
1500                break;
1501            case RESTART_ECM_TIMER:
1502                long delayInMillis = SystemProperties.getLong(
1503                        TelephonyProperties.PROPERTY_ECM_EXIT_TIMER, DEFAULT_ECM_EXIT_TIMER_VALUE);
1504                postDelayed(mExitEcmRunnable, delayInMillis);
1505                ((GsmCdmaPhone) mDefaultPhone).notifyEcbmTimerReset(Boolean.FALSE);
1506                break;
1507            default:
1508                Rlog.e(LOG_TAG, "handleTimerInEmergencyCallbackMode, unsupported action " + action);
1509        }
1510    }
1511
1512    @Override
1513    public void setOnEcbModeExitResponse(Handler h, int what, Object obj) {
1514        mEcmExitRespRegistrant = new Registrant(h, what, obj);
1515    }
1516
1517    @Override
1518    public void unsetOnEcbModeExitResponse(Handler h) {
1519        mEcmExitRespRegistrant.clear();
1520    }
1521
1522    public void onFeatureCapabilityChanged() {
1523        mDefaultPhone.getServiceStateTracker().onImsCapabilityChanged();
1524    }
1525
1526    @Override
1527    public boolean isVolteEnabled() {
1528        return mCT.isVolteEnabled();
1529    }
1530
1531    @Override
1532    public boolean isWifiCallingEnabled() {
1533        return mCT.isVowifiEnabled();
1534    }
1535
1536    @Override
1537    public boolean isVideoEnabled() {
1538        return mCT.isVideoCallEnabled();
1539    }
1540
1541    @Override
1542    public Phone getDefaultPhone() {
1543        return mDefaultPhone;
1544    }
1545
1546    @Override
1547    public boolean isImsRegistered() {
1548        return mImsRegistered;
1549    }
1550
1551    public void setImsRegistered(boolean value) {
1552        mImsRegistered = value;
1553    }
1554
1555    @Override
1556    public void callEndCleanupHandOverCallIfAny() {
1557        mCT.callEndCleanupHandOverCallIfAny();
1558    }
1559
1560    private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
1561        @Override
1562        public void onReceive(Context context, Intent intent) {
1563            // Add notification only if alert was not shown by WfcSettings
1564            if (getResultCode() == Activity.RESULT_OK) {
1565                // Default result code (as passed to sendOrderedBroadcast)
1566                // means that intent was not received by WfcSettings.
1567
1568                CharSequence title = intent.getCharSequenceExtra(EXTRA_KEY_ALERT_TITLE);
1569                CharSequence messageAlert = intent.getCharSequenceExtra(EXTRA_KEY_ALERT_MESSAGE);
1570                CharSequence messageNotification = intent.getCharSequenceExtra(EXTRA_KEY_NOTIFICATION_MESSAGE);
1571
1572                Intent resultIntent = new Intent(Intent.ACTION_MAIN);
1573                resultIntent.setClassName("com.android.settings",
1574                        "com.android.settings.Settings$WifiCallingSettingsActivity");
1575                resultIntent.putExtra(EXTRA_KEY_ALERT_SHOW, true);
1576                resultIntent.putExtra(EXTRA_KEY_ALERT_TITLE, title);
1577                resultIntent.putExtra(EXTRA_KEY_ALERT_MESSAGE, messageAlert);
1578                PendingIntent resultPendingIntent =
1579                        PendingIntent.getActivity(
1580                                mContext,
1581                                0,
1582                                resultIntent,
1583                                PendingIntent.FLAG_UPDATE_CURRENT
1584                        );
1585
1586                final Notification notification = new Notification.Builder(mContext)
1587                                .setSmallIcon(android.R.drawable.stat_sys_warning)
1588                                .setContentTitle(title)
1589                                .setContentText(messageNotification)
1590                                .setAutoCancel(true)
1591                                .setContentIntent(resultPendingIntent)
1592                                .setStyle(new Notification.BigTextStyle()
1593                                .bigText(messageNotification))
1594                                .setChannelId(NotificationChannelController.CHANNEL_ID_WFC)
1595                                .build();
1596                final String notificationTag = "wifi_calling";
1597                final int notificationId = 1;
1598
1599                NotificationManager notificationManager =
1600                        (NotificationManager) mContext.getSystemService(
1601                                Context.NOTIFICATION_SERVICE);
1602                notificationManager.notify(notificationTag, notificationId,
1603                        notification);
1604            }
1605        }
1606    };
1607
1608    /**
1609     * Show notification in case of some error codes.
1610     */
1611    public void processDisconnectReason(ImsReasonInfo imsReasonInfo) {
1612        if (imsReasonInfo.mCode == imsReasonInfo.CODE_REGISTRATION_ERROR
1613                && imsReasonInfo.mExtraMessage != null) {
1614            // Suppress WFC Registration notifications if WFC is not enabled by the user.
1615            if (ImsManager.isWfcEnabledByUser(mContext)) {
1616                processWfcDisconnectForNotification(imsReasonInfo);
1617            }
1618        }
1619    }
1620
1621    // Processes an IMS disconnect cause for possible WFC registration errors and optionally
1622    // disable WFC.
1623    private void processWfcDisconnectForNotification(ImsReasonInfo imsReasonInfo) {
1624        CarrierConfigManager configManager =
1625                (CarrierConfigManager) mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE);
1626        if (configManager == null) {
1627            Rlog.e(LOG_TAG, "processDisconnectReason: CarrierConfigManager is not ready");
1628            return;
1629        }
1630        PersistableBundle pb = configManager.getConfigForSubId(getSubId());
1631        if (pb == null) {
1632            Rlog.e(LOG_TAG, "processDisconnectReason: no config for subId " + getSubId());
1633            return;
1634        }
1635        final String[] wfcOperatorErrorCodes =
1636                pb.getStringArray(
1637                        CarrierConfigManager.KEY_WFC_OPERATOR_ERROR_CODES_STRING_ARRAY);
1638        if (wfcOperatorErrorCodes == null) {
1639            // no operator-specific error codes
1640            return;
1641        }
1642
1643        final String[] wfcOperatorErrorAlertMessages =
1644                mContext.getResources().getStringArray(
1645                        com.android.internal.R.array.wfcOperatorErrorAlertMessages);
1646        final String[] wfcOperatorErrorNotificationMessages =
1647                mContext.getResources().getStringArray(
1648                        com.android.internal.R.array.wfcOperatorErrorNotificationMessages);
1649
1650        for (int i = 0; i < wfcOperatorErrorCodes.length; i++) {
1651            String[] codes = wfcOperatorErrorCodes[i].split("\\|");
1652            if (codes.length != 2) {
1653                Rlog.e(LOG_TAG, "Invalid carrier config: " + wfcOperatorErrorCodes[i]);
1654                continue;
1655            }
1656
1657            // Match error code.
1658            if (!imsReasonInfo.mExtraMessage.startsWith(
1659                    codes[0])) {
1660                continue;
1661            }
1662            // If there is no delimiter at the end of error code string
1663            // then we need to verify that we are not matching partial code.
1664            // EXAMPLE: "REG9" must not match "REG99".
1665            // NOTE: Error code must not be empty.
1666            int codeStringLength = codes[0].length();
1667            char lastChar = codes[0].charAt(codeStringLength - 1);
1668            if (Character.isLetterOrDigit(lastChar)) {
1669                if (imsReasonInfo.mExtraMessage.length() > codeStringLength) {
1670                    char nextChar = imsReasonInfo.mExtraMessage.charAt(codeStringLength);
1671                    if (Character.isLetterOrDigit(nextChar)) {
1672                        continue;
1673                    }
1674                }
1675            }
1676
1677            final CharSequence title = mContext.getText(
1678                    com.android.internal.R.string.wfcRegErrorTitle);
1679
1680            int idx = Integer.parseInt(codes[1]);
1681            if (idx < 0
1682                    || idx >= wfcOperatorErrorAlertMessages.length
1683                    || idx >= wfcOperatorErrorNotificationMessages.length) {
1684                Rlog.e(LOG_TAG, "Invalid index: " + wfcOperatorErrorCodes[i]);
1685                continue;
1686            }
1687            String messageAlert = imsReasonInfo.mExtraMessage;
1688            String messageNotification = imsReasonInfo.mExtraMessage;
1689            if (!wfcOperatorErrorAlertMessages[idx].isEmpty()) {
1690                messageAlert = String.format(
1691                        wfcOperatorErrorAlertMessages[idx],
1692                        imsReasonInfo.mExtraMessage); // Fill IMS error code into alert message
1693            }
1694            if (!wfcOperatorErrorNotificationMessages[idx].isEmpty()) {
1695                messageNotification = String.format(
1696                        wfcOperatorErrorNotificationMessages[idx],
1697                        imsReasonInfo.mExtraMessage); // Fill IMS error code into notification
1698            }
1699
1700            // UX requirement is to disable WFC in case of "permanent" registration failures.
1701            ImsManager.setWfcSetting(mContext, false);
1702
1703            // If WfcSettings are active then alert will be shown
1704            // otherwise notification will be added.
1705            Intent intent = new Intent(ImsManager.ACTION_IMS_REGISTRATION_ERROR);
1706            intent.putExtra(EXTRA_KEY_ALERT_TITLE, title);
1707            intent.putExtra(EXTRA_KEY_ALERT_MESSAGE, messageAlert);
1708            intent.putExtra(EXTRA_KEY_NOTIFICATION_MESSAGE, messageNotification);
1709            mContext.sendOrderedBroadcast(intent, null, mResultReceiver,
1710                    null, Activity.RESULT_OK, null, null);
1711
1712            // We can only match a single error code
1713            // so should break the loop after a successful match.
1714            break;
1715        }
1716    }
1717
1718    @Override
1719    public boolean isUtEnabled() {
1720        return mCT.isUtEnabled();
1721    }
1722
1723    @Override
1724    public void sendEmergencyCallStateChange(boolean callActive) {
1725        mDefaultPhone.sendEmergencyCallStateChange(callActive);
1726    }
1727
1728    @Override
1729    public void setBroadcastEmergencyCallStateChanges(boolean broadcast) {
1730        mDefaultPhone.setBroadcastEmergencyCallStateChanges(broadcast);
1731    }
1732
1733    @VisibleForTesting
1734    public PowerManager.WakeLock getWakeLock() {
1735        return mWakeLock;
1736    }
1737
1738    @Override
1739    public NetworkStats getVtDataUsage(boolean perUidStats) {
1740        return mCT.getVtDataUsage(perUidStats);
1741    }
1742
1743    private void updateRoamingState(boolean newRoaming) {
1744        if (mCT.getState() == PhoneConstants.State.IDLE) {
1745            if (DBG) Rlog.d(LOG_TAG, "updateRoamingState now: " + newRoaming);
1746            mRoaming = newRoaming;
1747            ImsManager.setWfcMode(mContext,
1748                    ImsManager.getWfcMode(mContext, newRoaming), newRoaming);
1749        } else {
1750            if (DBG) Rlog.d(LOG_TAG, "updateRoamingState postponed: " + newRoaming);
1751            mCT.registerForVoiceCallEnded(this,
1752                    EVENT_VOICE_CALL_ENDED, null);
1753        }
1754    }
1755
1756    private boolean getCurrentRoaming() {
1757        TelephonyManager tm = (TelephonyManager) mContext
1758                .getSystemService(Context.TELEPHONY_SERVICE);
1759        return tm.isNetworkRoaming();
1760    }
1761
1762    @Override
1763    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1764        pw.println("ImsPhone extends:");
1765        super.dump(fd, pw, args);
1766        pw.flush();
1767
1768        pw.println("ImsPhone:");
1769        pw.println("  mDefaultPhone = " + mDefaultPhone);
1770        pw.println("  mPendingMMIs = " + mPendingMMIs);
1771        pw.println("  mPostDialHandler = " + mPostDialHandler);
1772        pw.println("  mSS = " + mSS);
1773        pw.println("  mWakeLock = " + mWakeLock);
1774        pw.println("  mIsPhoneInEcmState = " + isInEcm());
1775        pw.println("  mEcmExitRespRegistrant = " + mEcmExitRespRegistrant);
1776        pw.println("  mSilentRedialRegistrants = " + mSilentRedialRegistrants);
1777        pw.println("  mImsRegistered = " + mImsRegistered);
1778        pw.println("  mRoaming = " + mRoaming);
1779        pw.println("  mSsnRegistrants = " + mSsnRegistrants);
1780        pw.flush();
1781    }
1782}
1783