DcTracker.java revision 45eaa2335e64a8ff1ad8d5e8224c580ef996f370
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.dataconnection;
18
19import android.app.AlarmManager;
20import android.app.PendingIntent;
21import android.app.ProgressDialog;
22import android.content.ActivityNotFoundException;
23import android.content.BroadcastReceiver;
24import android.content.ContentResolver;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.res.Resources;
30import android.database.ContentObserver;
31import android.database.Cursor;
32import android.net.ConnectivityManager;
33import android.net.LinkProperties;
34import android.net.NetworkCapabilities;
35import android.net.NetworkConfig;
36import android.net.NetworkUtils;
37import android.net.ProxyInfo;
38import android.net.Uri;
39import android.os.AsyncResult;
40import android.os.Build;
41import android.os.Bundle;
42import android.os.Handler;
43import android.os.Looper;
44import android.os.Message;
45import android.os.Messenger;
46import android.os.RegistrantList;
47import android.os.ServiceManager;
48import android.os.SystemClock;
49import android.os.SystemProperties;
50import android.os.UserHandle;
51import android.provider.Settings;
52import android.provider.Telephony;
53import android.telephony.CellLocation;
54import android.telephony.ServiceState;
55import android.telephony.TelephonyManager;
56import android.telephony.SubscriptionManager;
57import android.telephony.cdma.CdmaCellLocation;
58import android.telephony.gsm.GsmCellLocation;
59import android.text.TextUtils;
60import android.util.EventLog;
61import android.util.SparseArray;
62import android.view.WindowManager;
63import android.telephony.Rlog;
64
65import com.android.internal.telephony.cdma.CDMALTEPhone;
66import com.android.internal.telephony.Phone;
67import com.android.internal.telephony.PhoneBase;
68import com.android.internal.telephony.DctConstants;
69import com.android.internal.telephony.EventLogTags;
70import com.android.internal.telephony.ITelephony;
71import com.android.internal.telephony.TelephonyIntents;
72import com.android.internal.telephony.gsm.GSMPhone;
73import com.android.internal.telephony.PhoneConstants;
74import com.android.internal.telephony.RILConstants;
75import com.android.internal.telephony.uicc.IccRecords;
76import com.android.internal.telephony.uicc.UiccController;
77import com.android.internal.util.AsyncChannel;
78import com.android.internal.util.ArrayUtils;
79
80import java.io.FileDescriptor;
81import java.io.PrintWriter;
82import java.util.ArrayList;
83import java.util.Arrays;
84import java.util.concurrent.atomic.AtomicBoolean;
85import java.util.Objects;
86import java.lang.StringBuilder;
87
88import android.provider.Settings;
89
90import com.android.internal.telephony.ServiceStateTracker;
91/**
92 * {@hide}
93 */
94public final class DcTracker extends DcTrackerBase {
95    protected final String LOG_TAG = "DCT";
96
97    /**
98     * List of messages that are waiting to be posted, when data call disconnect
99     * is complete
100     */
101    private ArrayList<Message> mDisconnectAllCompleteMsgList = new ArrayList<Message>();
102
103    private RegistrantList mAllDataDisconnectedRegistrants = new RegistrantList();
104
105    protected int mDisconnectPendingCount = 0;
106
107    /**
108     * Handles changes to the APN db.
109     */
110    private class ApnChangeObserver extends ContentObserver {
111        public ApnChangeObserver () {
112            super(mDataConnectionTracker);
113        }
114
115        @Override
116        public void onChange(boolean selfChange) {
117            sendMessage(obtainMessage(DctConstants.EVENT_APN_CHANGED));
118        }
119    }
120
121    //***** Instance Variables
122
123    private boolean mReregisterOnReconnectFailure = false;
124
125
126    //***** Constants
127
128    // Used by puppetmaster/*/radio_stress.py
129    private static final String PUPPET_MASTER_RADIO_STRESS_TEST = "gsm.defaultpdpcontext.active";
130
131    private static final int POLL_PDP_MILLIS = 5 * 1000;
132
133    private static final int PROVISIONING_SPINNER_TIMEOUT_MILLIS = 120 * 1000;
134
135    static final Uri PREFERAPN_NO_UPDATE_URI_USING_SUBID =
136                        Uri.parse("content://telephony/carriers/preferapn_no_update/subId/");
137    static final String APN_ID = "apn_id";
138
139    private boolean mCanSetPreferApn = false;
140
141    private AtomicBoolean mAttached = new AtomicBoolean(false);
142
143    /** Watches for changes to the APN db. */
144    private ApnChangeObserver mApnObserver;
145
146    private final String mProvisionActionName;
147    private BroadcastReceiver mProvisionBroadcastReceiver;
148    private ProgressDialog mProvisioningSpinner;
149
150    public boolean mImsRegistrationState = false;
151    private ApnContext mWaitCleanUpApnContext = null;
152    private boolean mDeregistrationAlarmState = false;
153    private PendingIntent mImsDeregistrationDelayIntent = null;
154
155    //***** Constructor
156    public DcTracker(PhoneBase p) {
157        super(p);
158        if (DBG) log("GsmDCT.constructor");
159
160        mDataConnectionTracker = this;
161        update();
162        mApnObserver = new ApnChangeObserver();
163        p.getContext().getContentResolver().registerContentObserver(
164                Telephony.Carriers.CONTENT_URI, true, mApnObserver);
165
166        initApnContexts();
167
168        for (ApnContext apnContext : mApnContexts.values()) {
169            // Register the reconnect and restart actions.
170            IntentFilter filter = new IntentFilter();
171            filter.addAction(INTENT_RECONNECT_ALARM + '.' + apnContext.getApnType());
172            filter.addAction(INTENT_RESTART_TRYSETUP_ALARM + '.' + apnContext.getApnType());
173            mPhone.getContext().registerReceiver(mIntentReceiver, filter, null, mPhone);
174        }
175
176        // Add Emergency APN to APN setting list by default to support EPDN in sim absent cases
177        initEmergencyApnSetting();
178        addEmergencyApnSetting();
179
180        mProvisionActionName = "com.android.internal.telephony.PROVISION" + p.getPhoneId();
181    }
182
183    protected void registerForAllEvents() {
184        mPhone.mCi.registerForAvailable(this, DctConstants.EVENT_RADIO_AVAILABLE, null);
185        mPhone.mCi.registerForOffOrNotAvailable(this,
186               DctConstants.EVENT_RADIO_OFF_OR_NOT_AVAILABLE, null);
187        mPhone.mCi.registerForDataNetworkStateChanged(this,
188               DctConstants.EVENT_DATA_STATE_CHANGED, null);
189        mPhone.getCallTracker().registerForVoiceCallEnded (this,
190               DctConstants.EVENT_VOICE_CALL_ENDED, null);
191        mPhone.getCallTracker().registerForVoiceCallStarted (this,
192               DctConstants.EVENT_VOICE_CALL_STARTED, null);
193        mPhone.getServiceStateTracker().registerForDataConnectionAttached(this,
194               DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null);
195        mPhone.getServiceStateTracker().registerForDataConnectionDetached(this,
196               DctConstants.EVENT_DATA_CONNECTION_DETACHED, null);
197        mPhone.getServiceStateTracker().registerForDataRoamingOn(this,
198               DctConstants.EVENT_ROAMING_ON, null);
199        mPhone.getServiceStateTracker().registerForDataRoamingOff(this,
200               DctConstants.EVENT_ROAMING_OFF, null);
201        mPhone.getServiceStateTracker().registerForPsRestrictedEnabled(this,
202                DctConstants.EVENT_PS_RESTRICT_ENABLED, null);
203        mPhone.getServiceStateTracker().registerForPsRestrictedDisabled(this,
204                DctConstants.EVENT_PS_RESTRICT_DISABLED, null);
205     //   SubscriptionManager.registerForDdsSwitch(this,
206     //          DctConstants.EVENT_CLEAN_UP_ALL_CONNECTIONS, null);
207        mPhone.getServiceStateTracker().registerForDataRegStateOrRatChanged(this,
208                DctConstants.EVENT_DATA_RAT_CHANGED, null);
209    }
210    @Override
211    public void dispose() {
212        if (DBG) log("DcTracker.dispose");
213
214        if (mProvisionBroadcastReceiver != null) {
215            mPhone.getContext().unregisterReceiver(mProvisionBroadcastReceiver);
216            mProvisionBroadcastReceiver = null;
217        }
218        if (mProvisioningSpinner != null) {
219            mProvisioningSpinner.dismiss();
220            mProvisioningSpinner = null;
221        }
222
223        cleanUpAllConnections(true, null);
224
225        super.dispose();
226
227        mPhone.getContext().getContentResolver().unregisterContentObserver(mApnObserver);
228        mApnContexts.clear();
229        mPrioritySortedApnContexts.clear();
230
231        destroyDataConnections();
232    }
233    protected void unregisterForAllEvents() {
234         //Unregister for all events
235        mPhone.mCi.unregisterForAvailable(this);
236        mPhone.mCi.unregisterForOffOrNotAvailable(this);
237        IccRecords r = mIccRecords.get();
238        if (r != null) {
239            r.unregisterForRecordsLoaded(this);
240            mIccRecords.set(null);
241        }
242        mPhone.mCi.unregisterForDataNetworkStateChanged(this);
243        mPhone.getCallTracker().unregisterForVoiceCallEnded(this);
244        mPhone.getCallTracker().unregisterForVoiceCallStarted(this);
245        mPhone.getServiceStateTracker().unregisterForDataConnectionAttached(this);
246        mPhone.getServiceStateTracker().unregisterForDataConnectionDetached(this);
247        mPhone.getServiceStateTracker().unregisterForDataRoamingOn(this);
248        mPhone.getServiceStateTracker().unregisterForDataRoamingOff(this);
249        mPhone.getServiceStateTracker().unregisterForPsRestrictedEnabled(this);
250        mPhone.getServiceStateTracker().unregisterForPsRestrictedDisabled(this);
251        //SubscriptionManager.unregisterForDdsSwitch(this);
252    }
253
254    @Override
255    public void incApnRefCount(String name) {
256        ApnContext apnContext = mApnContexts.get(name);
257        if (apnContext != null) {
258            apnContext.incRefCount();
259        }
260    }
261
262    @Override
263    public void decApnRefCount(String name) {
264        ApnContext apnContext = mApnContexts.get(name);
265        if (apnContext != null) {
266            apnContext.decRefCount();
267        }
268    }
269
270    @Override
271    public boolean isApnSupported(String name) {
272        if (name == null) {
273            loge("isApnSupported: name=null");
274            return false;
275        }
276        ApnContext apnContext = mApnContexts.get(name);
277        if (apnContext == null) {
278            loge("Request for unsupported mobile name: " + name);
279            return false;
280        }
281        return true;
282    }
283
284    @Override
285    public int getApnPriority(String name) {
286        ApnContext apnContext = mApnContexts.get(name);
287        if (apnContext == null) {
288            loge("Request for unsupported mobile name: " + name);
289        }
290        return apnContext.priority;
291    }
292
293    // Turn telephony radio on or off.
294    private void setRadio(boolean on) {
295        final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
296        try {
297            phone.setRadio(on);
298        } catch (Exception e) {
299            // Ignore.
300        }
301    }
302
303    // Class to handle Intent dispatched with user selects the "Sign-in to network"
304    // notification.
305    private class ProvisionNotificationBroadcastReceiver extends BroadcastReceiver {
306        private final String mNetworkOperator;
307        // Mobile provisioning URL.  Valid while provisioning notification is up.
308        // Set prior to notification being posted as URL contains ICCID which
309        // disappears when radio is off (which is the case when notification is up).
310        private final String mProvisionUrl;
311
312        public ProvisionNotificationBroadcastReceiver(String provisionUrl, String networkOperator) {
313            mNetworkOperator = networkOperator;
314            mProvisionUrl = provisionUrl;
315        }
316
317        private void setEnableFailFastMobileData(int enabled) {
318            sendMessage(obtainMessage(DctConstants.CMD_SET_ENABLE_FAIL_FAST_MOBILE_DATA, enabled, 0));
319        }
320
321        private void enableMobileProvisioning() {
322            final Message msg = obtainMessage(DctConstants.CMD_ENABLE_MOBILE_PROVISIONING);
323            msg.setData(Bundle.forPair(DctConstants.PROVISIONING_URL_KEY, mProvisionUrl));
324            sendMessage(msg);
325        }
326
327        @Override
328        public void onReceive(Context context, Intent intent) {
329            // Turning back on the radio can take time on the order of a minute, so show user a
330            // spinner so they know something is going on.
331            mProvisioningSpinner = new ProgressDialog(context);
332            mProvisioningSpinner.setTitle(mNetworkOperator);
333            mProvisioningSpinner.setMessage(
334                    // TODO: Don't borrow "Connecting..." i18n string; give Telephony a version.
335                    context.getText(com.android.internal.R.string.media_route_status_connecting));
336            mProvisioningSpinner.setIndeterminate(true);
337            mProvisioningSpinner.setCancelable(true);
338            // Allow non-Activity Service Context to create a View.
339            mProvisioningSpinner.getWindow().setType(
340                    WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
341            mProvisioningSpinner.show();
342            // After timeout, hide spinner so user can at least use their device.
343            // TODO: Indicate to user that it is taking an unusually long time to connect?
344            sendMessageDelayed(obtainMessage(DctConstants.CMD_CLEAR_PROVISIONING_SPINNER,
345                    mProvisioningSpinner), PROVISIONING_SPINNER_TIMEOUT_MILLIS);
346            // This code is almost identical to the old
347            // ConnectivityService.handleMobileProvisioningAction code.
348            setRadio(true);
349            setEnableFailFastMobileData(DctConstants.ENABLED);
350            enableMobileProvisioning();
351        }
352    }
353
354    @Override
355    public boolean isApnTypeActive(String type) {
356        ApnContext apnContext = mApnContexts.get(type);
357        if (apnContext == null) return false;
358
359        return (apnContext.getDcAc() != null);
360    }
361
362    @Override
363    public boolean isDataPossible(String apnType) {
364        ApnContext apnContext = mApnContexts.get(apnType);
365        if (apnContext == null) {
366            return false;
367        }
368        boolean apnContextIsEnabled = apnContext.isEnabled();
369        DctConstants.State apnContextState = apnContext.getState();
370        boolean apnTypePossible = !(apnContextIsEnabled &&
371                (apnContextState == DctConstants.State.FAILED));
372        boolean isEmergencyApn = apnContext.getApnType().equals(PhoneConstants.APN_TYPE_EMERGENCY);
373        // Set the emergency APN availability status as TRUE irrespective of conditions checked in
374        // isDataAllowed() like IN_SERVICE, MOBILE DATA status etc.
375        boolean dataAllowed = isEmergencyApn || isDataAllowed();
376        boolean possible = dataAllowed && apnTypePossible;
377
378        if (VDBG) {
379            log(String.format("isDataPossible(%s): possible=%b isDataAllowed=%b " +
380                    "apnTypePossible=%b apnContextisEnabled=%b apnContextState()=%s",
381                    apnType, possible, dataAllowed, apnTypePossible,
382                    apnContextIsEnabled, apnContextState));
383        }
384        return possible;
385    }
386
387    @Override
388    protected void finalize() {
389        if(DBG) log("finalize");
390    }
391
392    protected void supplyMessenger() {
393        ConnectivityManager cm = (ConnectivityManager)mPhone.getContext().getSystemService(
394                Context.CONNECTIVITY_SERVICE);
395        cm.supplyMessenger(ConnectivityManager.TYPE_MOBILE, new Messenger(this));
396        cm.supplyMessenger(ConnectivityManager.TYPE_MOBILE_MMS, new Messenger(this));
397        cm.supplyMessenger(ConnectivityManager.TYPE_MOBILE_SUPL, new Messenger(this));
398        cm.supplyMessenger(ConnectivityManager.TYPE_MOBILE_DUN, new Messenger(this));
399        cm.supplyMessenger(ConnectivityManager.TYPE_MOBILE_HIPRI, new Messenger(this));
400        cm.supplyMessenger(ConnectivityManager.TYPE_MOBILE_FOTA, new Messenger(this));
401        cm.supplyMessenger(ConnectivityManager.TYPE_MOBILE_IMS, new Messenger(this));
402        cm.supplyMessenger(ConnectivityManager.TYPE_MOBILE_CBS, new Messenger(this));
403        cm.supplyMessenger(ConnectivityManager.TYPE_MOBILE_EMERGENCY, new Messenger(this));
404    }
405
406    private ApnContext addApnContext(String type, NetworkConfig networkConfig) {
407        ApnContext apnContext = new ApnContext(mPhone.getContext(), type, LOG_TAG, networkConfig,
408                this);
409        mApnContexts.put(type, apnContext);
410        mPrioritySortedApnContexts.add(apnContext);
411        return apnContext;
412    }
413
414    protected void initApnContexts() {
415        log("initApnContexts: E");
416        // Load device network attributes from resources
417        String[] networkConfigStrings = mPhone.getContext().getResources().getStringArray(
418                com.android.internal.R.array.networkAttributes);
419        for (String networkConfigString : networkConfigStrings) {
420            NetworkConfig networkConfig = new NetworkConfig(networkConfigString);
421            ApnContext apnContext = null;
422
423            switch (networkConfig.type) {
424            case ConnectivityManager.TYPE_MOBILE:
425                apnContext = addApnContext(PhoneConstants.APN_TYPE_DEFAULT, networkConfig);
426                break;
427            case ConnectivityManager.TYPE_MOBILE_MMS:
428                apnContext = addApnContext(PhoneConstants.APN_TYPE_MMS, networkConfig);
429                break;
430            case ConnectivityManager.TYPE_MOBILE_SUPL:
431                apnContext = addApnContext(PhoneConstants.APN_TYPE_SUPL, networkConfig);
432                break;
433            case ConnectivityManager.TYPE_MOBILE_DUN:
434                apnContext = addApnContext(PhoneConstants.APN_TYPE_DUN, networkConfig);
435                break;
436            case ConnectivityManager.TYPE_MOBILE_HIPRI:
437                apnContext = addApnContext(PhoneConstants.APN_TYPE_HIPRI, networkConfig);
438                break;
439            case ConnectivityManager.TYPE_MOBILE_FOTA:
440                apnContext = addApnContext(PhoneConstants.APN_TYPE_FOTA, networkConfig);
441                break;
442            case ConnectivityManager.TYPE_MOBILE_IMS:
443                apnContext = addApnContext(PhoneConstants.APN_TYPE_IMS, networkConfig);
444                break;
445            case ConnectivityManager.TYPE_MOBILE_CBS:
446                apnContext = addApnContext(PhoneConstants.APN_TYPE_CBS, networkConfig);
447                break;
448            case ConnectivityManager.TYPE_MOBILE_IA:
449                apnContext = addApnContext(PhoneConstants.APN_TYPE_IA, networkConfig);
450                break;
451            case ConnectivityManager.TYPE_MOBILE_EMERGENCY:
452                apnContext = addApnContext(PhoneConstants.APN_TYPE_EMERGENCY, networkConfig);
453                break;
454            default:
455                log("initApnContexts: skipping unknown type=" + networkConfig.type);
456                continue;
457            }
458            log("initApnContexts: apnContext=" + apnContext);
459        }
460        log("initApnContexts: X mApnContexts=" + mApnContexts);
461    }
462
463    @Override
464    public LinkProperties getLinkProperties(String apnType) {
465        ApnContext apnContext = mApnContexts.get(apnType);
466        if (apnContext != null) {
467            DcAsyncChannel dcac = apnContext.getDcAc();
468            if (dcac != null) {
469                if (DBG) log("return link properites for " + apnType);
470                return dcac.getLinkPropertiesSync();
471            }
472        }
473        if (DBG) log("return new LinkProperties");
474        return new LinkProperties();
475    }
476
477    @Override
478    public NetworkCapabilities getNetworkCapabilities(String apnType) {
479        ApnContext apnContext = mApnContexts.get(apnType);
480        if (apnContext!=null) {
481            DcAsyncChannel dataConnectionAc = apnContext.getDcAc();
482            if (dataConnectionAc != null) {
483                if (DBG) {
484                    log("get active pdp is not null, return NetworkCapabilities for " + apnType);
485                }
486                return dataConnectionAc.getNetworkCapabilitiesSync();
487            }
488        }
489        if (DBG) log("return new NetworkCapabilities");
490        return new NetworkCapabilities();
491    }
492
493    @Override
494    // Return all active apn types
495    public String[] getActiveApnTypes() {
496        if (DBG) log("get all active apn types");
497        ArrayList<String> result = new ArrayList<String>();
498
499        for (ApnContext apnContext : mApnContexts.values()) {
500            if (mAttached.get() && apnContext.isReady()) {
501                result.add(apnContext.getApnType());
502            }
503        }
504
505        return result.toArray(new String[0]);
506    }
507
508    @Override
509    // Return active apn of specific apn type
510    public String getActiveApnString(String apnType) {
511        if (VDBG) log( "get active apn string for type:" + apnType);
512        ApnContext apnContext = mApnContexts.get(apnType);
513        if (apnContext != null) {
514            ApnSetting apnSetting = apnContext.getApnSetting();
515            if (apnSetting != null) {
516                return apnSetting.apn;
517            }
518        }
519        return null;
520    }
521
522    @Override
523    public boolean isApnTypeEnabled(String apnType) {
524        ApnContext apnContext = mApnContexts.get(apnType);
525        if (apnContext == null) {
526            return false;
527        }
528        return apnContext.isEnabled();
529    }
530
531    @Override
532    protected void setState(DctConstants.State s) {
533        if (DBG) log("setState should not be used in GSM" + s);
534    }
535
536    // Return state of specific apn type
537    @Override
538    public DctConstants.State getState(String apnType) {
539        ApnContext apnContext = mApnContexts.get(apnType);
540        if (apnContext != null) {
541            return apnContext.getState();
542        }
543        return DctConstants.State.FAILED;
544    }
545
546    // Return if apn type is a provisioning apn.
547    @Override
548    protected boolean isProvisioningApn(String apnType) {
549        ApnContext apnContext = mApnContexts.get(apnType);
550        if (apnContext != null) {
551            return apnContext.isProvisioningApn();
552        }
553        return false;
554    }
555
556    // Return state of overall
557    @Override
558    public DctConstants.State getOverallState() {
559        boolean isConnecting = false;
560        boolean isFailed = true; // All enabled Apns should be FAILED.
561        boolean isAnyEnabled = false;
562
563        for (ApnContext apnContext : mApnContexts.values()) {
564            if (apnContext.isEnabled()) {
565                isAnyEnabled = true;
566                switch (apnContext.getState()) {
567                case CONNECTED:
568                case DISCONNECTING:
569                    if (DBG) log("overall state is CONNECTED");
570                    return DctConstants.State.CONNECTED;
571                case RETRYING:
572                case CONNECTING:
573                    isConnecting = true;
574                    isFailed = false;
575                    break;
576                case IDLE:
577                case SCANNING:
578                    isFailed = false;
579                    break;
580                default:
581                    isAnyEnabled = true;
582                    break;
583                }
584            }
585        }
586
587        if (!isAnyEnabled) { // Nothing enabled. return IDLE.
588            if (DBG) log( "overall state is IDLE");
589            return DctConstants.State.IDLE;
590        }
591
592        if (isConnecting) {
593            if (DBG) log( "overall state is CONNECTING");
594            return DctConstants.State.CONNECTING;
595        } else if (!isFailed) {
596            if (DBG) log( "overall state is IDLE");
597            return DctConstants.State.IDLE;
598        } else {
599            if (DBG) log( "overall state is FAILED");
600            return DctConstants.State.FAILED;
601        }
602    }
603
604    @Override
605    protected boolean isApnTypeAvailable(String type) {
606        if (type.equals(PhoneConstants.APN_TYPE_DUN) && fetchDunApn() != null) {
607            return true;
608        }
609
610        if (mAllApnSettings != null) {
611            for (ApnSetting apn : mAllApnSettings) {
612                if (apn.canHandleType(type)) {
613                    return true;
614                }
615            }
616        }
617        return false;
618    }
619
620    /**
621     * Report on whether data connectivity is enabled for any APN.
622     * @return {@code false} if data connectivity has been explicitly disabled,
623     * {@code true} otherwise.
624     */
625    @Override
626    public boolean getAnyDataEnabled() {
627        synchronized (mDataEnabledLock) {
628            if (!(mInternalDataEnabled && mUserDataEnabled && sPolicyDataEnabled)) return false;
629            for (ApnContext apnContext : mApnContexts.values()) {
630                // Make sure we don't have a context that is going down
631                // and is explicitly disabled.
632                if (isDataAllowed(apnContext)) {
633                    return true;
634                }
635            }
636            return false;
637        }
638    }
639
640    public boolean getAnyDataEnabled(boolean checkUserDataEnabled) {
641        synchronized (mDataEnabledLock) {
642            if (!(mInternalDataEnabled && (!checkUserDataEnabled || mUserDataEnabled)
643                        && (!checkUserDataEnabled || sPolicyDataEnabled)))
644                return false;
645
646            for (ApnContext apnContext : mApnContexts.values()) {
647                // Make sure we dont have a context that going down
648                // and is explicitly disabled.
649                if (isDataAllowed(apnContext)) {
650                    return true;
651                }
652            }
653            return false;
654        }
655    }
656
657    private boolean isDataAllowed(ApnContext apnContext) {
658        return apnContext.isReady() && isDataAllowed();
659    }
660
661    //****** Called from ServiceStateTracker
662    /**
663     * Invoked when ServiceStateTracker observes a transition from GPRS
664     * attach to detach.
665     */
666    protected void onDataConnectionDetached() {
667        /*
668         * We presently believe it is unnecessary to tear down the PDP context
669         * when GPRS detaches, but we should stop the network polling.
670         */
671        if (DBG) log ("onDataConnectionDetached: stop polling and notify detached");
672        stopNetStatPoll();
673        stopDataStallAlarm();
674        notifyDataConnection(Phone.REASON_DATA_DETACHED);
675        mAttached.set(false);
676    }
677
678    private void onDataConnectionAttached() {
679        if (DBG) log("onDataConnectionAttached");
680        mAttached.set(true);
681        if (getOverallState() == DctConstants.State.CONNECTED) {
682            if (DBG) log("onDataConnectionAttached: start polling notify attached");
683            startNetStatPoll();
684            startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
685            notifyDataConnection(Phone.REASON_DATA_ATTACHED);
686        } else {
687            // update APN availability so that APN can be enabled.
688            notifyOffApnsOfAvailability(Phone.REASON_DATA_ATTACHED);
689        }
690        if (mAutoAttachOnCreationConfig) {
691            mAutoAttachOnCreation = true;
692        }
693        setupDataOnConnectableApns(Phone.REASON_DATA_ATTACHED);
694    }
695
696    @Override
697    protected boolean isDataAllowed() {
698        final boolean internalDataEnabled;
699        synchronized (mDataEnabledLock) {
700            internalDataEnabled = mInternalDataEnabled;
701        }
702
703        boolean attachedState = mAttached.get();
704        boolean desiredPowerState = mPhone.getServiceStateTracker().getDesiredPowerState();
705        IccRecords r = mIccRecords.get();
706        boolean recordsLoaded = false;
707        if (r != null) {
708            recordsLoaded = r.getRecordsLoaded();
709            if (DBG) log("isDataAllowed getRecordsLoaded=" + recordsLoaded);
710        }
711
712        //FIXME always attach
713        boolean psRestricted = mIsPsRestricted;
714        int phoneNum = TelephonyManager.getDefault().getPhoneCount();
715        if (phoneNum > 1) {
716            attachedState = true;
717            psRestricted = false;
718        }
719        int dataSub = SubscriptionManager.getDefaultDataSubId();
720        boolean defaultDataSelected = SubscriptionManager.isValidSubscriptionId(dataSub);
721
722        boolean allowed =
723                    (attachedState || mAutoAttachOnCreation) &&
724                    recordsLoaded &&
725                    (mPhone.getState() == PhoneConstants.State.IDLE ||
726                     mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed()) &&
727                    internalDataEnabled &&
728                    defaultDataSelected &&
729                    (!mPhone.getServiceState().getDataRoaming() || getDataOnRoamingEnabled()) &&
730                    //!mIsPsRestricted &&
731                    !psRestricted &&
732                    desiredPowerState;
733        if (!allowed && DBG) {
734            String reason = "";
735            if (!(attachedState || mAutoAttachOnCreation)) {
736                reason += " - Attached= " + attachedState;
737            }
738            if (!recordsLoaded) reason += " - SIM not loaded";
739            if (mPhone.getState() != PhoneConstants.State.IDLE &&
740                    !mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed()) {
741                reason += " - PhoneState= " + mPhone.getState();
742                reason += " - Concurrent voice and data not allowed";
743            }
744            if (!internalDataEnabled) reason += " - mInternalDataEnabled= false";
745            if (!defaultDataSelected) reason += " - defaultDataSelected= false";
746            if (mPhone.getServiceState().getDataRoaming() && !getDataOnRoamingEnabled()) {
747                reason += " - Roaming and data roaming not enabled";
748            }
749            if (mIsPsRestricted) reason += " - mIsPsRestricted= true";
750            if (!desiredPowerState) reason += " - desiredPowerState= false";
751            if (DBG) log("isDataAllowed: not allowed due to" + reason);
752        }
753        return allowed;
754    }
755
756    // arg for setupDataOnConnectableApns
757    private enum RetryFailures {
758        // retry failed networks always (the old default)
759        ALWAYS,
760        // retry only when a substantial change has occured.  Either:
761        // 1) we were restricted by voice/data concurrency and aren't anymore
762        // 2) our apn list has change
763        ONLY_ON_CHANGE
764    };
765
766    private void setupDataOnConnectableApns(String reason) {
767        setupDataOnConnectableApns(reason, RetryFailures.ALWAYS);
768    }
769
770    private void setupDataOnConnectableApns(String reason, RetryFailures retryFailures) {
771        if (DBG) log("setupDataOnConnectableApns: " + reason);
772        ArrayList<ApnSetting> waitingApns = null;
773
774        for (ApnContext apnContext : mPrioritySortedApnContexts) {
775            if (DBG) log("setupDataOnConnectableApns: apnContext " + apnContext);
776            if (apnContext.getState() == DctConstants.State.FAILED) {
777                if (retryFailures == RetryFailures.ALWAYS) {
778                    apnContext.setState(DctConstants.State.IDLE);
779                } else if (apnContext.isConcurrentVoiceAndDataAllowed() == false &&
780                         mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed()) {
781                    // RetryFailures.ONLY_ON_CHANGE - check if voice concurrency has changed
782                    apnContext.setState(DctConstants.State.IDLE);
783                } else {
784                    // RetryFailures.ONLY_ON_CHANGE - check if the apns have changed
785                    int radioTech = mPhone.getServiceState().getRilDataRadioTechnology();
786                    ArrayList<ApnSetting> originalApns = apnContext.getOriginalWaitingApns();
787                    if (originalApns != null && originalApns.isEmpty() == false) {
788                        waitingApns = buildWaitingApns(apnContext.getApnType(), radioTech);
789                        if (originalApns.size() != waitingApns.size() ||
790                                originalApns.containsAll(waitingApns) == false) {
791                            apnContext.setState(DctConstants.State.IDLE);
792                        }
793                    }
794                }
795            }
796            if (apnContext.isConnectable()) {
797                log("setupDataOnConnectableApns: isConnectable() call trySetupData");
798                apnContext.setReason(reason);
799                trySetupData(apnContext, waitingApns);
800            }
801        }
802    }
803
804    private boolean trySetupData(ApnContext apnContext) {
805        return trySetupData(apnContext, null);
806    }
807
808    private boolean trySetupData(ApnContext apnContext, ArrayList<ApnSetting> waitingApns) {
809        if (DBG) {
810            log("trySetupData for type:" + apnContext.getApnType() +
811                    " due to " + apnContext.getReason() + " apnContext=" + apnContext);
812            log("trySetupData with mIsPsRestricted=" + mIsPsRestricted);
813        }
814
815        if (mPhone.getSimulatedRadioControl() != null) {
816            // Assume data is connected on the simulator
817            // FIXME  this can be improved
818            apnContext.setState(DctConstants.State.CONNECTED);
819            mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
820
821            log("trySetupData: X We're on the simulator; assuming connected retValue=true");
822            return true;
823        }
824
825        // Allow SETUP_DATA request for E-APN to be completed during emergency call
826        // and MOBILE DATA On/Off cases as well.
827        boolean isEmergencyApn = apnContext.getApnType().equals(PhoneConstants.APN_TYPE_EMERGENCY);
828        final ServiceStateTracker sst = mPhone.getServiceStateTracker();
829        boolean desiredPowerState = sst.getDesiredPowerState();
830        boolean checkUserDataEnabled =
831                    !(apnContext.getApnType().equals(PhoneConstants.APN_TYPE_IMS));
832
833        if (apnContext.isConnectable() && (isEmergencyApn ||
834                (isDataAllowed(apnContext) &&
835                getAnyDataEnabled(checkUserDataEnabled) && !isEmergency()))) {
836            if (apnContext.getState() == DctConstants.State.FAILED) {
837                if (DBG) log("trySetupData: make a FAILED ApnContext IDLE so its reusable");
838                apnContext.setState(DctConstants.State.IDLE);
839            }
840            int radioTech = mPhone.getServiceState().getRilDataRadioTechnology();
841            apnContext.setConcurrentVoiceAndDataAllowed(sst.isConcurrentVoiceAndDataAllowed());
842            if (apnContext.getState() == DctConstants.State.IDLE) {
843                if (waitingApns == null) {
844                    waitingApns = buildWaitingApns(apnContext.getApnType(), radioTech);
845                }
846                if (waitingApns.isEmpty()) {
847                    notifyNoData(DcFailCause.MISSING_UNKNOWN_APN, apnContext);
848                    notifyOffApnsOfAvailability(apnContext.getReason());
849                    if (DBG) log("trySetupData: X No APN found retValue=false");
850                    return false;
851                } else {
852                    apnContext.setWaitingApns(waitingApns);
853                    if (DBG) {
854                        log ("trySetupData: Create from mAllApnSettings : "
855                                    + apnListToString(mAllApnSettings));
856                    }
857                }
858            }
859
860            if (DBG) {
861                log("trySetupData: call setupData, waitingApns : "
862                        + apnListToString(apnContext.getWaitingApns()));
863            }
864            boolean retValue = setupData(apnContext, radioTech);
865            notifyOffApnsOfAvailability(apnContext.getReason());
866
867            if (DBG) log("trySetupData: X retValue=" + retValue);
868            return retValue;
869        } else {
870            if (!apnContext.getApnType().equals(PhoneConstants.APN_TYPE_DEFAULT)
871                    && apnContext.isConnectable()) {
872                mPhone.notifyDataConnectionFailed(apnContext.getReason(), apnContext.getApnType());
873            }
874            notifyOffApnsOfAvailability(apnContext.getReason());
875            if (DBG) log ("trySetupData: X apnContext not 'ready' retValue=false");
876            return false;
877        }
878    }
879
880    @Override
881    // Disabled apn's still need avail/unavail notificiations - send them out
882    protected void notifyOffApnsOfAvailability(String reason) {
883        for (ApnContext apnContext : mApnContexts.values()) {
884            if (!mAttached.get() || !apnContext.isReady()) {
885                if (VDBG) log("notifyOffApnOfAvailability type:" + apnContext.getApnType());
886                mPhone.notifyDataConnection(reason != null ? reason : apnContext.getReason(),
887                                            apnContext.getApnType(),
888                                            PhoneConstants.DataState.DISCONNECTED);
889            } else {
890                if (VDBG) {
891                    log("notifyOffApnsOfAvailability skipped apn due to attached && isReady " +
892                            apnContext.toString());
893                }
894            }
895        }
896    }
897
898    /**
899     * If tearDown is true, this only tears down a CONNECTED session. Presently,
900     * there is no mechanism for abandoning an CONNECTING session,
901     * but would likely involve cancelling pending async requests or
902     * setting a flag or new state to ignore them when they came in
903     * @param tearDown true if the underlying DataConnection should be
904     * disconnected.
905     * @param reason reason for the clean up.
906     * @return boolean - true if we did cleanup any connections, false if they
907     *                   were already all disconnected.
908     */
909    protected boolean cleanUpAllConnections(boolean tearDown, String reason) {
910        if (DBG) log("cleanUpAllConnections: tearDown=" + tearDown + " reason=" + reason);
911        boolean didDisconnect = false;
912        boolean specificdisable = false;
913
914        if (!TextUtils.isEmpty(reason)) {
915            specificdisable = reason.equals(Phone.REASON_DATA_SPECIFIC_DISABLED);
916        }
917
918        for (ApnContext apnContext : mApnContexts.values()) {
919            if (apnContext.isDisconnected() == false) didDisconnect = true;
920            if (specificdisable) {
921                if (!apnContext.getApnType().equals(PhoneConstants.APN_TYPE_IMS)) {
922                    if (DBG) log("ApnConextType: " + apnContext.getApnType());
923                    apnContext.setReason(reason);
924                    cleanUpConnection(tearDown, apnContext);
925                }
926            } else {
927                // TODO - only do cleanup if not disconnected
928                apnContext.setReason(reason);
929                cleanUpConnection(tearDown, apnContext);
930            }
931        }
932
933        stopNetStatPoll();
934        stopDataStallAlarm();
935
936        // TODO: Do we need mRequestedApnType?
937        mRequestedApnType = PhoneConstants.APN_TYPE_DEFAULT;
938
939        log("cleanUpConnection: mDisconnectPendingCount = " + mDisconnectPendingCount);
940        if (tearDown && mDisconnectPendingCount == 0) {
941            notifyDataDisconnectComplete();
942            notifyAllDataDisconnected();
943        }
944
945        return didDisconnect;
946    }
947
948    /**
949     * Cleanup all connections.
950     *
951     * TODO: Cleanup only a specified connection passed as a parameter.
952     *       Also, make sure when you clean up a conn, if it is last apply
953     *       logic as though it is cleanupAllConnections
954     *
955     * @param cause for the clean up.
956     */
957
958    @Override
959    protected void onCleanUpAllConnections(String cause) {
960        cleanUpAllConnections(true, cause);
961    }
962
963    protected void cleanUpConnection(boolean tearDown, ApnContext apnContext) {
964
965        if (apnContext == null) {
966            if (DBG) log("cleanUpConnection: apn context is null");
967            return;
968        }
969
970        DcAsyncChannel dcac = apnContext.getDcAc();
971        if (DBG) {
972            log("cleanUpConnection: E tearDown=" + tearDown + " reason=" + apnContext.getReason() +
973                    " apnContext=" + apnContext);
974        }
975        if (tearDown) {
976            if (apnContext.isDisconnected()) {
977                // The request is tearDown and but ApnContext is not connected.
978                // If apnContext is not enabled anymore, break the linkage to the DCAC/DC.
979                apnContext.setState(DctConstants.State.IDLE);
980                if (!apnContext.isReady()) {
981                    if (dcac != null) {
982                        if (DBG) {
983                            log("cleanUpConnection: teardown, disconnected, !ready apnContext="
984                                    + apnContext);
985                        }
986                        dcac.tearDown(apnContext, "", null);
987                    }
988                    apnContext.setDataConnectionAc(null);
989                }
990            } else {
991                // Connection is still there. Try to clean up.
992                if (dcac != null) {
993                    if (apnContext.getState() != DctConstants.State.DISCONNECTING) {
994                        boolean disconnectAll = false;
995                        if (PhoneConstants.APN_TYPE_DUN.equals(apnContext.getApnType())) {
996                            // CAF_MSIM is this below condition required.
997                            // if (PhoneConstants.APN_TYPE_DUN.equals(PhoneConstants.APN_TYPE_DEFAULT)) {
998                            if (teardownForDun()) {
999                                if (DBG) {
1000                                    log("cleanUpConnection: disconnectAll DUN connection");
1001                                }
1002                                // we need to tear it down - we brought it up just for dun and
1003                                // other people are camped on it and now dun is done.  We need
1004                                // to stop using it and let the normal apn list get used to find
1005                                // connections for the remaining desired connections
1006                                disconnectAll = true;
1007                            }
1008                        }
1009                        if (DBG) {
1010                            log("cleanUpConnection: tearing down" + (disconnectAll ? " all" :"")
1011                                    + "apnContext=" + apnContext);
1012                        }
1013                        Message msg = obtainMessage(DctConstants.EVENT_DISCONNECT_DONE, apnContext);
1014                        if (disconnectAll) {
1015                            apnContext.getDcAc().tearDownAll(apnContext.getReason(), msg);
1016                        } else {
1017                            apnContext.getDcAc()
1018                                .tearDown(apnContext, apnContext.getReason(), msg);
1019                        }
1020                        apnContext.setState(DctConstants.State.DISCONNECTING);
1021                        mDisconnectPendingCount++;
1022                    }
1023                } else {
1024                    // apn is connected but no reference to dcac.
1025                    // Should not be happen, but reset the state in case.
1026                    apnContext.setState(DctConstants.State.IDLE);
1027                    mPhone.notifyDataConnection(apnContext.getReason(),
1028                                                apnContext.getApnType());
1029                }
1030            }
1031        } else {
1032            // force clean up the data connection.
1033            if (dcac != null) dcac.reqReset();
1034            apnContext.setState(DctConstants.State.IDLE);
1035            mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
1036            apnContext.setDataConnectionAc(null);
1037        }
1038
1039        // Make sure reconnection alarm is cleaned up if there is no ApnContext
1040        // associated to the connection.
1041        if (dcac != null) {
1042            cancelReconnectAlarm(apnContext);
1043        }
1044        if (DBG) {
1045            log("cleanUpConnection: X tearDown=" + tearDown + " reason=" + apnContext.getReason() +
1046                    " apnContext=" + apnContext + " dcac=" + apnContext.getDcAc());
1047        }
1048    }
1049
1050    /**
1051     * Determine if DUN connection is special and we need to teardown on start/stop
1052     */
1053    private boolean teardownForDun() {
1054        // CDMA always needs to do this the profile id is correct
1055        final int rilRat = mPhone.getServiceState().getRilDataRadioTechnology();
1056        if (ServiceState.isCdma(rilRat)) return true;
1057
1058        return (fetchDunApn() != null);
1059    }
1060
1061    /**
1062     * Cancels the alarm associated with apnContext.
1063     *
1064     * @param apnContext on which the alarm should be stopped.
1065     */
1066    private void cancelReconnectAlarm(ApnContext apnContext) {
1067        if (apnContext == null) return;
1068
1069        PendingIntent intent = apnContext.getReconnectIntent();
1070
1071        if (intent != null) {
1072                AlarmManager am =
1073                    (AlarmManager) mPhone.getContext().getSystemService(Context.ALARM_SERVICE);
1074                am.cancel(intent);
1075                apnContext.setReconnectIntent(null);
1076        }
1077    }
1078
1079    /**
1080     * @param types comma delimited list of APN types
1081     * @return array of APN types
1082     */
1083    private String[] parseTypes(String types) {
1084        String[] result;
1085        // If unset, set to DEFAULT.
1086        if (types == null || types.equals("")) {
1087            result = new String[1];
1088            result[0] = PhoneConstants.APN_TYPE_ALL;
1089        } else {
1090            result = types.split(",");
1091        }
1092        return result;
1093    }
1094
1095    private boolean imsiMatches(String imsiDB, String imsiSIM) {
1096        // Note: imsiDB value has digit number or 'x' character for seperating USIM information
1097        // for MVNO operator. And then digit number is matched at same order and 'x' character
1098        // could replace by any digit number.
1099        // ex) if imsiDB inserted '310260x10xxxxxx' for GG Operator,
1100        //     that means first 6 digits, 8th and 9th digit
1101        //     should be set in USIM for GG Operator.
1102        int len = imsiDB.length();
1103        int idxCompare = 0;
1104
1105        if (len <= 0) return false;
1106        if (len > imsiSIM.length()) return false;
1107
1108        for (int idx=0; idx<len; idx++) {
1109            char c = imsiDB.charAt(idx);
1110            if ((c == 'x') || (c == 'X') || (c == imsiSIM.charAt(idx))) {
1111                continue;
1112            } else {
1113                return false;
1114            }
1115        }
1116        return true;
1117    }
1118
1119    @Override
1120    protected boolean mvnoMatches(IccRecords r, String mvnoType, String mvnoMatchData) {
1121        if (mvnoType.equalsIgnoreCase("spn")) {
1122            if ((r.getServiceProviderName() != null) &&
1123                    r.getServiceProviderName().equalsIgnoreCase(mvnoMatchData)) {
1124                return true;
1125            }
1126        } else if (mvnoType.equalsIgnoreCase("imsi")) {
1127            String imsiSIM = r.getIMSI();
1128            if ((imsiSIM != null) && imsiMatches(mvnoMatchData, imsiSIM)) {
1129                return true;
1130            }
1131        } else if (mvnoType.equalsIgnoreCase("gid")) {
1132            String gid1 = r.getGid1();
1133            int mvno_match_data_length = mvnoMatchData.length();
1134            if ((gid1 != null) && (gid1.length() >= mvno_match_data_length) &&
1135                    gid1.substring(0, mvno_match_data_length).equalsIgnoreCase(mvnoMatchData)) {
1136                return true;
1137            }
1138        }
1139        return false;
1140    }
1141
1142    @Override
1143    protected boolean isPermanentFail(DcFailCause dcFailCause) {
1144        return (dcFailCause.isPermanentFail() &&
1145                (mAttached.get() == false || dcFailCause != DcFailCause.SIGNAL_LOST));
1146    }
1147
1148    private ApnSetting makeApnSetting(Cursor cursor) {
1149        String[] types = parseTypes(
1150                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.TYPE)));
1151        ApnSetting apn = new ApnSetting(
1152                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers._ID)),
1153                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NUMERIC)),
1154                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NAME)),
1155                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.APN)),
1156                NetworkUtils.trimV4AddrZeros(
1157                        cursor.getString(
1158                        cursor.getColumnIndexOrThrow(Telephony.Carriers.PROXY))),
1159                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PORT)),
1160                NetworkUtils.trimV4AddrZeros(
1161                        cursor.getString(
1162                        cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSC))),
1163                NetworkUtils.trimV4AddrZeros(
1164                        cursor.getString(
1165                        cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPROXY))),
1166                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPORT)),
1167                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.USER)),
1168                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PASSWORD)),
1169                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.AUTH_TYPE)),
1170                types,
1171                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROTOCOL)),
1172                cursor.getString(cursor.getColumnIndexOrThrow(
1173                        Telephony.Carriers.ROAMING_PROTOCOL)),
1174                cursor.getInt(cursor.getColumnIndexOrThrow(
1175                        Telephony.Carriers.CARRIER_ENABLED)) == 1,
1176                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.BEARER)),
1177                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROFILE_ID)),
1178                cursor.getInt(cursor.getColumnIndexOrThrow(
1179                        Telephony.Carriers.MODEM_COGNITIVE)) == 1,
1180                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MAX_CONNS)),
1181                cursor.getInt(cursor.getColumnIndexOrThrow(
1182                        Telephony.Carriers.WAIT_TIME)),
1183                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MAX_CONNS_TIME)),
1184                cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers.MTU)),
1185                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.MVNO_TYPE)),
1186                cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.MVNO_MATCH_DATA)));
1187        return apn;
1188    }
1189
1190    private ArrayList<ApnSetting> createApnList(Cursor cursor) {
1191        ArrayList<ApnSetting> mnoApns = new ArrayList<ApnSetting>();
1192        ArrayList<ApnSetting> mvnoApns = new ArrayList<ApnSetting>();
1193        IccRecords r = mIccRecords.get();
1194
1195        if (cursor.moveToFirst()) {
1196            do {
1197                ApnSetting apn = makeApnSetting(cursor);
1198                if (apn == null) {
1199                    continue;
1200                }
1201
1202                if (apn.hasMvnoParams()) {
1203                    if (r != null && mvnoMatches(r, apn.mvnoType, apn.mvnoMatchData)) {
1204                        mvnoApns.add(apn);
1205                    }
1206                } else {
1207                    mnoApns.add(apn);
1208                }
1209            } while (cursor.moveToNext());
1210        }
1211
1212        ArrayList<ApnSetting> result = mvnoApns.isEmpty() ? mnoApns : mvnoApns;
1213        if (DBG) log("createApnList: X result=" + result);
1214        return result;
1215    }
1216
1217    private boolean dataConnectionNotInUse(DcAsyncChannel dcac) {
1218        if (DBG) log("dataConnectionNotInUse: check if dcac is inuse dcac=" + dcac);
1219        for (ApnContext apnContext : mApnContexts.values()) {
1220            if (apnContext.getDcAc() == dcac) {
1221                if (DBG) log("dataConnectionNotInUse: in use by apnContext=" + apnContext);
1222                return false;
1223            }
1224        }
1225        // TODO: Fix retry handling so free DataConnections have empty apnlists.
1226        // Probably move retry handling into DataConnections and reduce complexity
1227        // of DCT.
1228        if (DBG) log("dataConnectionNotInUse: tearDownAll");
1229        dcac.tearDownAll("No connection", null);
1230        if (DBG) log("dataConnectionNotInUse: not in use return true");
1231        return true;
1232    }
1233
1234    private DcAsyncChannel findFreeDataConnection() {
1235        for (DcAsyncChannel dcac : mDataConnectionAcHashMap.values()) {
1236            if (dcac.isInactiveSync() && dataConnectionNotInUse(dcac)) {
1237                if (DBG) {
1238                    log("findFreeDataConnection: found free DataConnection=" +
1239                        " dcac=" + dcac);
1240                }
1241                return dcac;
1242            }
1243        }
1244        log("findFreeDataConnection: NO free DataConnection");
1245        return null;
1246    }
1247
1248    private boolean setupData(ApnContext apnContext, int radioTech) {
1249        if (DBG) log("setupData: apnContext=" + apnContext);
1250        ApnSetting apnSetting;
1251        DcAsyncChannel dcac = null;
1252
1253        apnSetting = apnContext.getNextWaitingApn();
1254        if (apnSetting == null) {
1255            if (DBG) log("setupData: return for no apn found!");
1256            return false;
1257        }
1258
1259        int profileId = apnSetting.profileId;
1260        if (profileId == 0) {
1261            profileId = getApnProfileID(apnContext.getApnType());
1262        }
1263
1264        // On CDMA, if we're explicitly asking for DUN, we need have
1265        // a dun-profiled connection so we can't share an existing one
1266        // On GSM/LTE we can share existing apn connections provided they support
1267        // this type.
1268        if (apnContext.getApnType() != PhoneConstants.APN_TYPE_DUN ||
1269                teardownForDun() == false) {
1270            dcac = checkForCompatibleConnectedApnContext(apnContext);
1271            if (dcac != null) {
1272                // Get the dcacApnSetting for the connection we want to share.
1273                ApnSetting dcacApnSetting = dcac.getApnSettingSync();
1274                if (dcacApnSetting != null) {
1275                    // Setting is good, so use it.
1276                    apnSetting = dcacApnSetting;
1277                }
1278            }
1279        }
1280        if (dcac == null) {
1281            if (isOnlySingleDcAllowed(radioTech)) {
1282                if (isHigherPriorityApnContextActive(apnContext)) {
1283                    if (DBG) {
1284                        log("setupData: Higher priority ApnContext active.  Ignoring call");
1285                    }
1286                    return false;
1287                }
1288
1289                // Only lower priority calls left.  Disconnect them all in this single PDP case
1290                // so that we can bring up the requested higher priority call (once we receive
1291                // repsonse for deactivate request for the calls we are about to disconnect
1292                if (cleanUpAllConnections(true, Phone.REASON_SINGLE_PDN_ARBITRATION)) {
1293                    // If any call actually requested to be disconnected, means we can't
1294                    // bring up this connection yet as we need to wait for those data calls
1295                    // to be disconnected.
1296                    if (DBG) log("setupData: Some calls are disconnecting first.  Wait and retry");
1297                    return false;
1298                }
1299
1300                // No other calls are active, so proceed
1301                if (DBG) log("setupData: Single pdp. Continue setting up data call.");
1302            }
1303
1304            dcac = findFreeDataConnection();
1305
1306            if (dcac == null) {
1307                dcac = createDataConnection();
1308            }
1309
1310            if (dcac == null) {
1311                if (DBG) log("setupData: No free DataConnection and couldn't create one, WEIRD");
1312                return false;
1313            }
1314        }
1315        if (DBG) log("setupData: dcac=" + dcac + " apnSetting=" + apnSetting);
1316
1317        apnContext.setDataConnectionAc(dcac);
1318        apnContext.setApnSetting(apnSetting);
1319        apnContext.setState(DctConstants.State.CONNECTING);
1320        mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
1321
1322        Message msg = obtainMessage();
1323        msg.what = DctConstants.EVENT_DATA_SETUP_COMPLETE;
1324        msg.obj = apnContext;
1325        dcac.bringUp(apnContext, getInitialMaxRetry(), profileId, radioTech, mAutoAttachOnCreation,
1326                msg);
1327
1328        if (DBG) log("setupData: initing!");
1329        return true;
1330    }
1331
1332    /**
1333     * Handles changes to the APN database.
1334     */
1335    private void onApnChanged() {
1336        DctConstants.State overallState = getOverallState();
1337        boolean isDisconnected = (overallState == DctConstants.State.IDLE ||
1338                overallState == DctConstants.State.FAILED);
1339
1340        if (mPhone instanceof GSMPhone) {
1341            // The "current" may no longer be valid.  MMS depends on this to send properly. TBD
1342            ((GSMPhone)mPhone).updateCurrentCarrierInProvider();
1343        }
1344
1345        // TODO: It'd be nice to only do this if the changed entrie(s)
1346        // match the current operator.
1347        if (DBG) log("onApnChanged: createAllApnList and cleanUpAllConnections");
1348        createAllApnList();
1349        setInitialAttachApn();
1350        cleanUpConnectionsOnUpdatedApns(!isDisconnected);
1351
1352        // FIXME: See bug 17426028 maybe no conditional is needed.
1353        if (mPhone.getSubId() == SubscriptionManager.getDefaultDataSubId()) {
1354            setupDataOnConnectableApns(Phone.REASON_APN_CHANGED);
1355        }
1356    }
1357
1358    /**
1359     * @param cid Connection id provided from RIL.
1360     * @return DataConnectionAc associated with specified cid.
1361     */
1362    private DcAsyncChannel findDataConnectionAcByCid(int cid) {
1363        for (DcAsyncChannel dcac : mDataConnectionAcHashMap.values()) {
1364            if (dcac.getCidSync() == cid) {
1365                return dcac;
1366            }
1367        }
1368        return null;
1369    }
1370
1371    // TODO: For multiple Active APNs not exactly sure how to do this.
1372    @Override
1373    protected void gotoIdleAndNotifyDataConnection(String reason) {
1374        if (DBG) log("gotoIdleAndNotifyDataConnection: reason=" + reason);
1375        notifyDataConnection(reason);
1376        mActiveApn = null;
1377    }
1378
1379    /**
1380     * "Active" here means ApnContext isEnabled() and not in FAILED state
1381     * @param apnContext to compare with
1382     * @return true if higher priority active apn found
1383     */
1384    private boolean isHigherPriorityApnContextActive(ApnContext apnContext) {
1385        for (ApnContext otherContext : mPrioritySortedApnContexts) {
1386            if (apnContext.getApnType().equalsIgnoreCase(otherContext.getApnType())) return false;
1387            if (otherContext.isEnabled() && otherContext.getState() != DctConstants.State.FAILED) {
1388                return true;
1389            }
1390        }
1391        return false;
1392    }
1393
1394    /**
1395     * Reports if we support multiple connections or not.
1396     * This is a combination of factors, based on carrier and RAT.
1397     * @param rilRadioTech the RIL Radio Tech currently in use
1398     * @return true if only single DataConnection is allowed
1399     */
1400    private boolean isOnlySingleDcAllowed(int rilRadioTech) {
1401        int[] singleDcRats = mPhone.getContext().getResources().getIntArray(
1402                com.android.internal.R.array.config_onlySingleDcAllowed);
1403        boolean onlySingleDcAllowed = false;
1404        if (Build.IS_DEBUGGABLE &&
1405                SystemProperties.getBoolean("persist.telephony.test.singleDc", false)) {
1406            onlySingleDcAllowed = true;
1407        }
1408        if (singleDcRats != null) {
1409            for (int i=0; i < singleDcRats.length && onlySingleDcAllowed == false; i++) {
1410                if (rilRadioTech == singleDcRats[i]) onlySingleDcAllowed = true;
1411            }
1412        }
1413
1414        if (DBG) log("isOnlySingleDcAllowed(" + rilRadioTech + "): " + onlySingleDcAllowed);
1415        return onlySingleDcAllowed;
1416    }
1417
1418    @Override
1419    protected void restartRadio() {
1420        if (DBG) log("restartRadio: ************TURN OFF RADIO**************");
1421        cleanUpAllConnections(true, Phone.REASON_RADIO_TURNED_OFF);
1422        mPhone.getServiceStateTracker().powerOffRadioSafely(this);
1423        /* Note: no need to call setRadioPower(true).  Assuming the desired
1424         * radio power state is still ON (as tracked by ServiceStateTracker),
1425         * ServiceStateTracker will call setRadioPower when it receives the
1426         * RADIO_STATE_CHANGED notification for the power off.  And if the
1427         * desired power state has changed in the interim, we don't want to
1428         * override it with an unconditional power on.
1429         */
1430
1431        int reset = Integer.parseInt(SystemProperties.get("net.ppp.reset-by-timeout", "0"));
1432        SystemProperties.set("net.ppp.reset-by-timeout", String.valueOf(reset+1));
1433    }
1434
1435    /**
1436     * Return true if data connection need to be setup after disconnected due to
1437     * reason.
1438     *
1439     * @param reason the reason why data is disconnected
1440     * @return true if try setup data connection is need for this reason
1441     */
1442    private boolean retryAfterDisconnected(ApnContext apnContext) {
1443        boolean retry = true;
1444        String reason = apnContext.getReason();
1445
1446        if ( Phone.REASON_RADIO_TURNED_OFF.equals(reason) ||
1447                (isOnlySingleDcAllowed(mPhone.getServiceState().getRilDataRadioTechnology())
1448                 && isHigherPriorityApnContextActive(apnContext))) {
1449            retry = false;
1450        }
1451        return retry;
1452    }
1453
1454    private void startAlarmForReconnect(int delay, ApnContext apnContext) {
1455        String apnType = apnContext.getApnType();
1456
1457        Intent intent = new Intent(INTENT_RECONNECT_ALARM + "." + apnType);
1458        intent.putExtra(INTENT_RECONNECT_ALARM_EXTRA_REASON, apnContext.getReason());
1459        intent.putExtra(INTENT_RECONNECT_ALARM_EXTRA_TYPE, apnType);
1460
1461        // Get current sub id.
1462        int subId = SubscriptionManager.getDefaultDataSubId();
1463        intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId);
1464
1465        if (DBG) {
1466            log("startAlarmForReconnect: delay=" + delay + " action=" + intent.getAction()
1467                    + " apn=" + apnContext);
1468        }
1469
1470        PendingIntent alarmIntent = PendingIntent.getBroadcast (mPhone.getContext(), 0,
1471                                        intent, PendingIntent.FLAG_UPDATE_CURRENT);
1472        apnContext.setReconnectIntent(alarmIntent);
1473        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
1474                SystemClock.elapsedRealtime() + delay, alarmIntent);
1475    }
1476
1477    private void startAlarmForRestartTrySetup(int delay, ApnContext apnContext) {
1478        String apnType = apnContext.getApnType();
1479        Intent intent = new Intent(INTENT_RESTART_TRYSETUP_ALARM + "." + apnType);
1480        intent.putExtra(INTENT_RESTART_TRYSETUP_ALARM_EXTRA_TYPE, apnType);
1481
1482        if (DBG) {
1483            log("startAlarmForRestartTrySetup: delay=" + delay + " action=" + intent.getAction()
1484                    + " apn=" + apnContext);
1485        }
1486        PendingIntent alarmIntent = PendingIntent.getBroadcast (mPhone.getContext(), 0,
1487                                        intent, PendingIntent.FLAG_UPDATE_CURRENT);
1488        apnContext.setReconnectIntent(alarmIntent);
1489        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
1490                SystemClock.elapsedRealtime() + delay, alarmIntent);
1491    }
1492
1493    private void notifyNoData(DcFailCause lastFailCauseCode,
1494                              ApnContext apnContext) {
1495        if (DBG) log( "notifyNoData: type=" + apnContext.getApnType());
1496        if (isPermanentFail(lastFailCauseCode)
1497            && (!apnContext.getApnType().equals(PhoneConstants.APN_TYPE_DEFAULT))) {
1498            mPhone.notifyDataConnectionFailed(apnContext.getReason(), apnContext.getApnType());
1499        }
1500    }
1501
1502    private void onRecordsLoaded() {
1503        if (DBG) log("onRecordsLoaded: createAllApnList");
1504        mAutoAttachOnCreationConfig = mPhone.getContext().getResources()
1505                .getBoolean(com.android.internal.R.bool.config_auto_attach_data_on_creation);
1506
1507        createAllApnList();
1508        setInitialAttachApn();
1509        if (mPhone.mCi.getRadioState().isOn()) {
1510            if (DBG) log("onRecordsLoaded: notifying data availability");
1511            notifyOffApnsOfAvailability(Phone.REASON_SIM_LOADED);
1512        }
1513        setupDataOnConnectableApns(Phone.REASON_SIM_LOADED);
1514    }
1515
1516    @Override
1517    protected void onSetDependencyMet(String apnType, boolean met) {
1518        // don't allow users to tweak hipri to work around default dependency not met
1519        if (PhoneConstants.APN_TYPE_HIPRI.equals(apnType)) return;
1520
1521        ApnContext apnContext = mApnContexts.get(apnType);
1522        if (apnContext == null) {
1523            loge("onSetDependencyMet: ApnContext not found in onSetDependencyMet(" +
1524                    apnType + ", " + met + ")");
1525            return;
1526        }
1527        applyNewState(apnContext, apnContext.isEnabled(), met);
1528        if (PhoneConstants.APN_TYPE_DEFAULT.equals(apnType)) {
1529            // tie actions on default to similar actions on HIPRI regarding dependencyMet
1530            apnContext = mApnContexts.get(PhoneConstants.APN_TYPE_HIPRI);
1531            if (apnContext != null) applyNewState(apnContext, apnContext.isEnabled(), met);
1532        }
1533    }
1534
1535    private void applyNewState(ApnContext apnContext, boolean enabled, boolean met) {
1536        boolean cleanup = false;
1537        boolean trySetup = false;
1538        if (DBG) {
1539            log("applyNewState(" + apnContext.getApnType() + ", " + enabled +
1540                    "(" + apnContext.isEnabled() + "), " + met + "(" +
1541                    apnContext.getDependencyMet() +"))");
1542        }
1543        if (apnContext.isReady()) {
1544            cleanup = true;
1545            if (enabled && met) {
1546                DctConstants.State state = apnContext.getState();
1547                switch(state) {
1548                    case CONNECTING:
1549                    case SCANNING:
1550                    case CONNECTED:
1551                    case DISCONNECTING:
1552                        // We're "READY" and active so just return
1553                        if (DBG) log("applyNewState: 'ready' so return");
1554                        return;
1555                    case IDLE:
1556                        // fall through: this is unexpected but if it happens cleanup and try setup
1557                    case FAILED:
1558                    case RETRYING: {
1559                        // We're "READY" but not active so disconnect (cleanup = true) and
1560                        // connect (trySetup = true) to be sure we retry the connection.
1561                        trySetup = true;
1562                        apnContext.setReason(Phone.REASON_DATA_ENABLED);
1563                        break;
1564                    }
1565                }
1566            } else if (met) {
1567                apnContext.setReason(Phone.REASON_DATA_DISABLED);
1568                // If ConnectivityService has disabled this network, stop trying to bring
1569                // it up, but do not tear it down - ConnectivityService will do that
1570                // directly by talking with the DataConnection.
1571                //
1572                // This doesn't apply to DUN, however.  Those connections have special
1573                // requirements from carriers and we need stop using them when the dun
1574                // request goes away.  This applies to both CDMA and GSM because they both
1575                // can declare the DUN APN sharable by default traffic, thus still satisfying
1576                // those requests and not torn down organically.
1577                if (apnContext.getApnType() == PhoneConstants.APN_TYPE_DUN && teardownForDun()) {
1578                    cleanup = true;
1579                } else {
1580                    cleanup = false;
1581                }
1582            } else {
1583                apnContext.setReason(Phone.REASON_DATA_DEPENDENCY_UNMET);
1584            }
1585        } else {
1586            if (enabled && met) {
1587                if (apnContext.isEnabled()) {
1588                    apnContext.setReason(Phone.REASON_DATA_DEPENDENCY_MET);
1589                } else {
1590                    apnContext.setReason(Phone.REASON_DATA_ENABLED);
1591                }
1592                if (apnContext.getState() == DctConstants.State.FAILED) {
1593                    apnContext.setState(DctConstants.State.IDLE);
1594                }
1595                trySetup = true;
1596            }
1597        }
1598        apnContext.setEnabled(enabled);
1599        apnContext.setDependencyMet(met);
1600        if (cleanup) cleanUpConnection(true, apnContext);
1601        if (trySetup) trySetupData(apnContext);
1602    }
1603
1604    private DcAsyncChannel checkForCompatibleConnectedApnContext(ApnContext apnContext) {
1605        String apnType = apnContext.getApnType();
1606        ApnSetting dunSetting = null;
1607
1608        if (PhoneConstants.APN_TYPE_DUN.equals(apnType)) {
1609            dunSetting = fetchDunApn();
1610        }
1611        if (DBG) {
1612            log("checkForCompatibleConnectedApnContext: apnContext=" + apnContext );
1613        }
1614
1615        DcAsyncChannel potentialDcac = null;
1616        ApnContext potentialApnCtx = null;
1617        for (ApnContext curApnCtx : mApnContexts.values()) {
1618            DcAsyncChannel curDcac = curApnCtx.getDcAc();
1619            log("curDcac: " + curDcac);
1620            if (curDcac != null) {
1621                ApnSetting apnSetting = curApnCtx.getApnSetting();
1622                log("apnSetting: " + apnSetting);
1623                if (dunSetting != null) {
1624                    if (dunSetting.equals(apnSetting)) {
1625                        switch (curApnCtx.getState()) {
1626                            case CONNECTED:
1627                                if (DBG) {
1628                                    log("checkForCompatibleConnectedApnContext:"
1629                                            + " found dun conn=" + curDcac
1630                                            + " curApnCtx=" + curApnCtx);
1631                                }
1632                                return curDcac;
1633                            case RETRYING:
1634                            case CONNECTING:
1635                                potentialDcac = curDcac;
1636                                potentialApnCtx = curApnCtx;
1637                            default:
1638                                // Not connected, potential unchanged
1639                                break;
1640                        }
1641                    }
1642                } else if (apnSetting != null && apnSetting.canHandleType(apnType)) {
1643                    switch (curApnCtx.getState()) {
1644                        case CONNECTED:
1645                            if (DBG) {
1646                                log("checkForCompatibleConnectedApnContext:"
1647                                        + " found canHandle conn=" + curDcac
1648                                        + " curApnCtx=" + curApnCtx);
1649                            }
1650                            return curDcac;
1651                        case RETRYING:
1652                        case CONNECTING:
1653                            potentialDcac = curDcac;
1654                            potentialApnCtx = curApnCtx;
1655                        default:
1656                            // Not connected, potential unchanged
1657                            break;
1658                    }
1659                }
1660            } else {
1661                if (VDBG) {
1662                    log("checkForCompatibleConnectedApnContext: not conn curApnCtx=" + curApnCtx);
1663                }
1664            }
1665        }
1666        if (potentialDcac != null) {
1667            if (DBG) {
1668                log("checkForCompatibleConnectedApnContext: found potential conn=" + potentialDcac
1669                        + " curApnCtx=" + potentialApnCtx);
1670            }
1671            return potentialDcac;
1672        }
1673
1674        if (DBG) log("checkForCompatibleConnectedApnContext: NO conn apnContext=" + apnContext);
1675        return null;
1676    }
1677
1678    @Override
1679    protected void onEnableApn(int apnId, int enabled) {
1680        ApnContext apnContext = mApnContexts.get(apnIdToType(apnId));
1681        if (apnContext == null) {
1682            loge("onEnableApn(" + apnId + ", " + enabled + "): NO ApnContext");
1683            return;
1684        }
1685        // TODO change our retry manager to use the appropriate numbers for the new APN
1686        if (DBG) log("onEnableApn: apnContext=" + apnContext + " call applyNewState");
1687        applyNewState(apnContext, enabled == DctConstants.ENABLED, apnContext.getDependencyMet());
1688    }
1689
1690    @Override
1691    // TODO: We shouldnt need this.
1692    protected boolean onTrySetupData(String reason) {
1693        if (DBG) log("onTrySetupData: reason=" + reason);
1694        setupDataOnConnectableApns(reason);
1695        return true;
1696    }
1697
1698    protected boolean onTrySetupData(ApnContext apnContext) {
1699        if (DBG) log("onTrySetupData: apnContext=" + apnContext);
1700        return trySetupData(apnContext);
1701    }
1702
1703    @Override
1704    protected void onRoamingOff() {
1705        if (DBG) log("onRoamingOff");
1706
1707        if (!mUserDataEnabled) return;
1708
1709        if (getDataOnRoamingEnabled() == false) {
1710            notifyOffApnsOfAvailability(Phone.REASON_ROAMING_OFF);
1711            setupDataOnConnectableApns(Phone.REASON_ROAMING_OFF);
1712        } else {
1713            notifyDataConnection(Phone.REASON_ROAMING_OFF);
1714        }
1715    }
1716
1717    @Override
1718    protected void onRoamingOn() {
1719        if (DBG) log("onRoamingOn");
1720
1721        if (!mUserDataEnabled) return;
1722
1723        if (getDataOnRoamingEnabled()) {
1724            if (DBG) log("onRoamingOn: setup data on roaming");
1725            setupDataOnConnectableApns(Phone.REASON_ROAMING_ON);
1726            notifyDataConnection(Phone.REASON_ROAMING_ON);
1727        } else {
1728            if (DBG) log("onRoamingOn: Tear down data connection on roaming.");
1729            cleanUpAllConnections(true, Phone.REASON_ROAMING_ON);
1730            notifyOffApnsOfAvailability(Phone.REASON_ROAMING_ON);
1731        }
1732    }
1733
1734    @Override
1735    protected void onRadioAvailable() {
1736        if (DBG) log("onRadioAvailable");
1737        if (mPhone.getSimulatedRadioControl() != null) {
1738            // Assume data is connected on the simulator
1739            // FIXME  this can be improved
1740            // setState(DctConstants.State.CONNECTED);
1741            notifyDataConnection(null);
1742
1743            log("onRadioAvailable: We're on the simulator; assuming data is connected");
1744        }
1745
1746        IccRecords r = mIccRecords.get();
1747        if (r != null && r.getRecordsLoaded()) {
1748            notifyOffApnsOfAvailability(null);
1749        }
1750
1751        if (getOverallState() != DctConstants.State.IDLE) {
1752            cleanUpConnection(true, null);
1753        }
1754    }
1755
1756    @Override
1757    protected void onRadioOffOrNotAvailable() {
1758        // Make sure our reconnect delay starts at the initial value
1759        // next time the radio comes on
1760
1761        mReregisterOnReconnectFailure = false;
1762
1763        if (mPhone.getSimulatedRadioControl() != null) {
1764            // Assume data is connected on the simulator
1765            // FIXME  this can be improved
1766            log("We're on the simulator; assuming radio off is meaningless");
1767        } else {
1768            if (DBG) log("onRadioOffOrNotAvailable: is off and clean up all connections");
1769            cleanUpAllConnections(false, Phone.REASON_RADIO_TURNED_OFF);
1770        }
1771        notifyOffApnsOfAvailability(null);
1772    }
1773
1774    @Override
1775    protected void completeConnection(ApnContext apnContext) {
1776        boolean isProvApn = apnContext.isProvisioningApn();
1777
1778        if (DBG) log("completeConnection: successful, notify the world apnContext=" + apnContext);
1779
1780        if (mIsProvisioning && !TextUtils.isEmpty(mProvisioningUrl)) {
1781            if (DBG) {
1782                log("completeConnection: MOBILE_PROVISIONING_ACTION url="
1783                        + mProvisioningUrl);
1784            }
1785            Intent newIntent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
1786                    Intent.CATEGORY_APP_BROWSER);
1787            newIntent.setData(Uri.parse(mProvisioningUrl));
1788            newIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT |
1789                    Intent.FLAG_ACTIVITY_NEW_TASK);
1790            try {
1791                mPhone.getContext().startActivity(newIntent);
1792            } catch (ActivityNotFoundException e) {
1793                loge("completeConnection: startActivityAsUser failed" + e);
1794            }
1795        }
1796        mIsProvisioning = false;
1797        mProvisioningUrl = null;
1798        if (mProvisioningSpinner != null) {
1799            sendMessage(obtainMessage(DctConstants.CMD_CLEAR_PROVISIONING_SPINNER,
1800                    mProvisioningSpinner));
1801        }
1802
1803        mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
1804        startNetStatPoll();
1805        startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
1806    }
1807
1808    /**
1809     * A SETUP (aka bringUp) has completed, possibly with an error. If
1810     * there is an error this method will call {@link #onDataSetupCompleteError}.
1811     */
1812    @Override
1813    protected void onDataSetupComplete(AsyncResult ar) {
1814
1815        DcFailCause cause = DcFailCause.UNKNOWN;
1816        boolean handleError = false;
1817        ApnContext apnContext = null;
1818
1819        if(ar.userObj instanceof ApnContext){
1820            apnContext = (ApnContext)ar.userObj;
1821        } else {
1822            throw new RuntimeException("onDataSetupComplete: No apnContext");
1823        }
1824
1825        if (ar.exception == null) {
1826            DcAsyncChannel dcac = apnContext.getDcAc();
1827
1828            if (RADIO_TESTS) {
1829                // Note: To change radio.test.onDSC.null.dcac from command line you need to
1830                // adb root and adb remount and from the command line you can only change the
1831                // value to 1 once. To change it a second time you can reboot or execute
1832                // adb shell stop and then adb shell start. The command line to set the value is:
1833                // adb shell sqlite3 /data/data/com.android.providers.settings/databases/settings.db "insert into system (name,value) values ('radio.test.onDSC.null.dcac', '1');"
1834                ContentResolver cr = mPhone.getContext().getContentResolver();
1835                String radioTestProperty = "radio.test.onDSC.null.dcac";
1836                if (Settings.System.getInt(cr, radioTestProperty, 0) == 1) {
1837                    log("onDataSetupComplete: " + radioTestProperty +
1838                            " is true, set dcac to null and reset property to false");
1839                    dcac = null;
1840                    Settings.System.putInt(cr, radioTestProperty, 0);
1841                    log("onDataSetupComplete: " + radioTestProperty + "=" +
1842                            Settings.System.getInt(mPhone.getContext().getContentResolver(),
1843                                    radioTestProperty, -1));
1844                }
1845            }
1846            if (dcac == null) {
1847                log("onDataSetupComplete: no connection to DC, handle as error");
1848                cause = DcFailCause.CONNECTION_TO_DATACONNECTIONAC_BROKEN;
1849                handleError = true;
1850            } else {
1851                ApnSetting apn = apnContext.getApnSetting();
1852                if (DBG) {
1853                    log("onDataSetupComplete: success apn=" + (apn == null ? "unknown" : apn.apn));
1854                }
1855                if (apn != null && apn.proxy != null && apn.proxy.length() != 0) {
1856                    try {
1857                        String port = apn.port;
1858                        if (TextUtils.isEmpty(port)) port = "8080";
1859                        ProxyInfo proxy = new ProxyInfo(apn.proxy,
1860                                Integer.parseInt(port), null);
1861                        dcac.setLinkPropertiesHttpProxySync(proxy);
1862                    } catch (NumberFormatException e) {
1863                        loge("onDataSetupComplete: NumberFormatException making ProxyProperties (" +
1864                                apn.port + "): " + e);
1865                    }
1866                }
1867
1868                // everything is setup
1869                if(TextUtils.equals(apnContext.getApnType(),PhoneConstants.APN_TYPE_DEFAULT)) {
1870                    SystemProperties.set(PUPPET_MASTER_RADIO_STRESS_TEST, "true");
1871                    if (mCanSetPreferApn && mPreferredApn == null) {
1872                        if (DBG) log("onDataSetupComplete: PREFERED APN is null");
1873                        mPreferredApn = apn;
1874                        if (mPreferredApn != null) {
1875                            setPreferredApn(mPreferredApn.id);
1876                        }
1877                    }
1878                } else {
1879                    SystemProperties.set(PUPPET_MASTER_RADIO_STRESS_TEST, "false");
1880                }
1881
1882                // A connection is setup
1883                apnContext.setState(DctConstants.State.CONNECTED);
1884                boolean isProvApn = apnContext.isProvisioningApn();
1885                final ConnectivityManager cm = ConnectivityManager.from(mPhone.getContext());
1886                if (mProvisionBroadcastReceiver != null) {
1887                    mPhone.getContext().unregisterReceiver(mProvisionBroadcastReceiver);
1888                    mProvisionBroadcastReceiver = null;
1889                }
1890                if ((!isProvApn) || mIsProvisioning) {
1891                    // Hide any provisioning notification.
1892                    cm.setProvisioningNotificationVisible(false, ConnectivityManager.TYPE_MOBILE,
1893                            mProvisionActionName);
1894                    // Complete the connection normally notifying the world we're connected.
1895                    // We do this if this isn't a special provisioning apn or if we've been
1896                    // told its time to provision.
1897                    completeConnection(apnContext);
1898                } else {
1899                    // This is a provisioning APN that we're reporting as connected. Later
1900                    // when the user desires to upgrade this to a "default" connection,
1901                    // mIsProvisioning == true, we'll go through the code path above.
1902                    // mIsProvisioning becomes true when CMD_ENABLE_MOBILE_PROVISIONING
1903                    // is sent to the DCT.
1904                    if (DBG) {
1905                        log("onDataSetupComplete: successful, BUT send connected to prov apn as"
1906                                + " mIsProvisioning:" + mIsProvisioning + " == false"
1907                                + " && (isProvisioningApn:" + isProvApn + " == true");
1908                    }
1909
1910                    // While radio is up, grab provisioning URL.  The URL contains ICCID which
1911                    // disappears when radio is off.
1912                    mProvisionBroadcastReceiver = new ProvisionNotificationBroadcastReceiver(
1913                            cm.getMobileProvisioningUrl(),
1914                            TelephonyManager.getDefault().getNetworkOperatorName());
1915                    mPhone.getContext().registerReceiver(mProvisionBroadcastReceiver,
1916                            new IntentFilter(mProvisionActionName));
1917                    // Put up user notification that sign-in is required.
1918                    cm.setProvisioningNotificationVisible(true, ConnectivityManager.TYPE_MOBILE,
1919                            mProvisionActionName);
1920                    // Turn off radio to save battery and avoid wasting carrier resources.
1921                    // The network isn't usable and network validation will just fail anyhow.
1922                    setRadio(false);
1923
1924                    Intent intent = new Intent(
1925                            TelephonyIntents.ACTION_DATA_CONNECTION_CONNECTED_TO_PROVISIONING_APN);
1926                    intent.putExtra(PhoneConstants.DATA_APN_KEY, apnContext.getApnSetting().apn);
1927                    intent.putExtra(PhoneConstants.DATA_APN_TYPE_KEY, apnContext.getApnType());
1928
1929                    String apnType = apnContext.getApnType();
1930                    LinkProperties linkProperties = getLinkProperties(apnType);
1931                    if (linkProperties != null) {
1932                        intent.putExtra(PhoneConstants.DATA_LINK_PROPERTIES_KEY, linkProperties);
1933                        String iface = linkProperties.getInterfaceName();
1934                        if (iface != null) {
1935                            intent.putExtra(PhoneConstants.DATA_IFACE_NAME_KEY, iface);
1936                        }
1937                    }
1938                    NetworkCapabilities networkCapabilities = getNetworkCapabilities(apnType);
1939                    if (networkCapabilities != null) {
1940                        intent.putExtra(PhoneConstants.DATA_NETWORK_CAPABILITIES_KEY,
1941                                networkCapabilities);
1942                    }
1943
1944                    mPhone.getContext().sendBroadcastAsUser(intent, UserHandle.ALL);
1945                }
1946                if (DBG) {
1947                    log("onDataSetupComplete: SETUP complete type=" + apnContext.getApnType()
1948                        + ", reason:" + apnContext.getReason());
1949                }
1950            }
1951        } else {
1952            cause = (DcFailCause) (ar.result);
1953            if (DBG) {
1954                ApnSetting apn = apnContext.getApnSetting();
1955                log(String.format("onDataSetupComplete: error apn=%s cause=%s",
1956                        (apn == null ? "unknown" : apn.apn), cause));
1957            }
1958            if (cause.isEventLoggable()) {
1959                // Log this failure to the Event Logs.
1960                int cid = getCellLocationId();
1961                EventLog.writeEvent(EventLogTags.PDP_SETUP_FAIL,
1962                        cause.ordinal(), cid, TelephonyManager.getDefault().getNetworkType());
1963            }
1964            ApnSetting apn = apnContext.getApnSetting();
1965            mPhone.notifyPreciseDataConnectionFailed(apnContext.getReason(),
1966                    apnContext.getApnType(), apn != null ? apn.apn : "unknown", cause.toString());
1967
1968            // Count permanent failures and remove the APN we just tried
1969            if (isPermanentFail(cause)) apnContext.decWaitingApnsPermFailCount();
1970
1971            apnContext.removeWaitingApn(apnContext.getApnSetting());
1972            if (DBG) {
1973                log(String.format("onDataSetupComplete: WaitingApns.size=%d" +
1974                        " WaitingApnsPermFailureCountDown=%d",
1975                        apnContext.getWaitingApns().size(),
1976                        apnContext.getWaitingApnsPermFailCount()));
1977            }
1978            handleError = true;
1979        }
1980
1981        if (handleError) {
1982            onDataSetupCompleteError(ar);
1983        }
1984
1985        /* If flag is set to false after SETUP_DATA_CALL is invoked, we need
1986         * to clean data connections.
1987         */
1988        if (!mInternalDataEnabled) {
1989            cleanUpAllConnections(null);
1990        }
1991
1992    }
1993
1994    /**
1995     * @return number of milli-seconds to delay between trying apns'
1996     */
1997    private int getApnDelay() {
1998        if (mFailFast) {
1999            return SystemProperties.getInt("persist.radio.apn_ff_delay",
2000                    APN_FAIL_FAST_DELAY_DEFAULT_MILLIS);
2001        } else {
2002            return SystemProperties.getInt("persist.radio.apn_delay", APN_DELAY_DEFAULT_MILLIS);
2003        }
2004    }
2005
2006    /**
2007     * Error has occurred during the SETUP {aka bringUP} request and the DCT
2008     * should either try the next waiting APN or start over from the
2009     * beginning if the list is empty. Between each SETUP request there will
2010     * be a delay defined by {@link #getApnDelay()}.
2011     */
2012    @Override
2013    protected void onDataSetupCompleteError(AsyncResult ar) {
2014        String reason = "";
2015        ApnContext apnContext = null;
2016
2017        if(ar.userObj instanceof ApnContext){
2018            apnContext = (ApnContext)ar.userObj;
2019        } else {
2020            throw new RuntimeException("onDataSetupCompleteError: No apnContext");
2021        }
2022
2023        // See if there are more APN's to try
2024        if (apnContext.getWaitingApns().isEmpty()) {
2025            apnContext.setState(DctConstants.State.FAILED);
2026            mPhone.notifyDataConnection(Phone.REASON_APN_FAILED, apnContext.getApnType());
2027
2028            apnContext.setDataConnectionAc(null);
2029
2030            if (apnContext.getWaitingApnsPermFailCount() == 0) {
2031                if (DBG) {
2032                    log("onDataSetupCompleteError: All APN's had permanent failures, stop retrying");
2033                }
2034            } else {
2035                int delay = getApnDelay();
2036                if (DBG) {
2037                    log("onDataSetupCompleteError: Not all APN's had permanent failures delay="
2038                            + delay);
2039                }
2040                startAlarmForRestartTrySetup(delay, apnContext);
2041            }
2042        } else {
2043            if (DBG) log("onDataSetupCompleteError: Try next APN");
2044            apnContext.setState(DctConstants.State.SCANNING);
2045            // Wait a bit before trying the next APN, so that
2046            // we're not tying up the RIL command channel
2047            startAlarmForReconnect(getApnDelay(), apnContext);
2048        }
2049    }
2050
2051    /**
2052     * Called when EVENT_DISCONNECT_DONE is received.
2053     */
2054    @Override
2055    protected void onDisconnectDone(int connId, AsyncResult ar) {
2056        ApnContext apnContext = null;
2057
2058        if (ar.userObj instanceof ApnContext) {
2059            apnContext = (ApnContext) ar.userObj;
2060        } else {
2061            loge("onDisconnectDone: Invalid ar in onDisconnectDone, ignore");
2062            return;
2063        }
2064
2065        if(DBG) log("onDisconnectDone: EVENT_DISCONNECT_DONE apnContext=" + apnContext);
2066        apnContext.setState(DctConstants.State.IDLE);
2067
2068        mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
2069
2070        // if all data connection are gone, check whether Airplane mode request was
2071        // pending.
2072        if (isDisconnected()) {
2073            if (mPhone.getServiceStateTracker().processPendingRadioPowerOffAfterDataOff()) {
2074                if(DBG) log("onDisconnectDone: radio will be turned off, no retries");
2075                // Radio will be turned off. No need to retry data setup
2076                apnContext.setApnSetting(null);
2077                apnContext.setDataConnectionAc(null);
2078
2079                // Need to notify disconnect as well, in the case of switching Airplane mode.
2080                // Otherwise, it would cause 30s delayed to turn on Airplane mode.
2081                if (mDisconnectPendingCount > 0)
2082                    mDisconnectPendingCount--;
2083
2084                if (mDisconnectPendingCount == 0) {
2085                    notifyDataDisconnectComplete();
2086                    notifyAllDataDisconnected();
2087                }
2088                return;
2089            }
2090        }
2091
2092        // If APN is still enabled, try to bring it back up automatically
2093        if (mAttached.get() && apnContext.isReady() && retryAfterDisconnected(apnContext)) {
2094            SystemProperties.set(PUPPET_MASTER_RADIO_STRESS_TEST, "false");
2095            // Wait a bit before trying the next APN, so that
2096            // we're not tying up the RIL command channel.
2097            // This also helps in any external dependency to turn off the context.
2098            if(DBG) log("onDisconnectDone: attached, ready and retry after disconnect");
2099            startAlarmForReconnect(getApnDelay(), apnContext);
2100        } else {
2101            boolean restartRadioAfterProvisioning = mPhone.getContext().getResources().getBoolean(
2102                    com.android.internal.R.bool.config_restartRadioAfterProvisioning);
2103
2104            if (apnContext.isProvisioningApn() && restartRadioAfterProvisioning) {
2105                log("onDisconnectDone: restartRadio after provisioning");
2106                restartRadio();
2107            }
2108            apnContext.setApnSetting(null);
2109            apnContext.setDataConnectionAc(null);
2110            if (isOnlySingleDcAllowed(mPhone.getServiceState().getRilDataRadioTechnology())) {
2111                if(DBG) log("onDisconnectDone: isOnlySigneDcAllowed true so setup single apn");
2112                setupDataOnConnectableApns(Phone.REASON_SINGLE_PDN_ARBITRATION);
2113            } else {
2114                if(DBG) log("onDisconnectDone: not retrying");
2115            }
2116        }
2117
2118        if (mDisconnectPendingCount > 0)
2119            mDisconnectPendingCount--;
2120
2121        if (mDisconnectPendingCount == 0) {
2122            apnContext.setConcurrentVoiceAndDataAllowed(
2123                    mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed());
2124            notifyDataDisconnectComplete();
2125            notifyAllDataDisconnected();
2126        }
2127
2128    }
2129
2130    /**
2131     * Called when EVENT_DISCONNECT_DC_RETRYING is received.
2132     */
2133    @Override
2134    protected void onDisconnectDcRetrying(int connId, AsyncResult ar) {
2135        // We could just do this in DC!!!
2136        ApnContext apnContext = null;
2137
2138        if (ar.userObj instanceof ApnContext) {
2139            apnContext = (ApnContext) ar.userObj;
2140        } else {
2141            loge("onDisconnectDcRetrying: Invalid ar in onDisconnectDone, ignore");
2142            return;
2143        }
2144
2145        apnContext.setState(DctConstants.State.RETRYING);
2146        if(DBG) log("onDisconnectDcRetrying: apnContext=" + apnContext);
2147
2148        mPhone.notifyDataConnection(apnContext.getReason(), apnContext.getApnType());
2149    }
2150
2151
2152    @Override
2153    protected void onVoiceCallStarted() {
2154        if (DBG) log("onVoiceCallStarted");
2155        mInVoiceCall = true;
2156        if (isConnected() && ! mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed()) {
2157            if (DBG) log("onVoiceCallStarted stop polling");
2158            stopNetStatPoll();
2159            stopDataStallAlarm();
2160            notifyDataConnection(Phone.REASON_VOICE_CALL_STARTED);
2161        }
2162    }
2163
2164    @Override
2165    protected void onVoiceCallEnded() {
2166        if (DBG) log("onVoiceCallEnded");
2167        mInVoiceCall = false;
2168        if (isConnected()) {
2169            if (!mPhone.getServiceStateTracker().isConcurrentVoiceAndDataAllowed()) {
2170                startNetStatPoll();
2171                startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
2172                notifyDataConnection(Phone.REASON_VOICE_CALL_ENDED);
2173            } else {
2174                // clean slate after call end.
2175                resetPollStats();
2176            }
2177        }
2178        // reset reconnect timer
2179        setupDataOnConnectableApns(Phone.REASON_VOICE_CALL_ENDED);
2180    }
2181
2182    @Override
2183    protected void onCleanUpConnection(boolean tearDown, int apnId, String reason) {
2184        if (DBG) log("onCleanUpConnection");
2185        ApnContext apnContext = mApnContexts.get(apnIdToType(apnId));
2186        if (apnContext != null) {
2187            apnContext.setReason(reason);
2188            cleanUpConnection(tearDown, apnContext);
2189        }
2190    }
2191
2192    @Override
2193    protected boolean isConnected() {
2194        for (ApnContext apnContext : mApnContexts.values()) {
2195            if (apnContext.getState() == DctConstants.State.CONNECTED) {
2196                // At least one context is connected, return true
2197                return true;
2198            }
2199        }
2200        // There are not any contexts connected, return false
2201        return false;
2202    }
2203
2204    @Override
2205    public boolean isDisconnected() {
2206        for (ApnContext apnContext : mApnContexts.values()) {
2207            if (!apnContext.isDisconnected()) {
2208                // At least one context was not disconnected return false
2209                return false;
2210            }
2211        }
2212        // All contexts were disconnected so return true
2213        return true;
2214    }
2215
2216    @Override
2217    protected void notifyDataConnection(String reason) {
2218        if (DBG) log("notifyDataConnection: reason=" + reason);
2219        for (ApnContext apnContext : mApnContexts.values()) {
2220            if (mAttached.get() && apnContext.isReady()) {
2221                if (DBG) log("notifyDataConnection: type:" + apnContext.getApnType());
2222                mPhone.notifyDataConnection(reason != null ? reason : apnContext.getReason(),
2223                        apnContext.getApnType());
2224            }
2225        }
2226        notifyOffApnsOfAvailability(reason);
2227    }
2228
2229    /**
2230     * Based on the sim operator numeric, create a list for all possible
2231     * Data Connections and setup the preferredApn.
2232     */
2233    private void createAllApnList() {
2234        mAllApnSettings = new ArrayList<ApnSetting>();
2235        IccRecords r = mIccRecords.get();
2236        String operator = (r != null) ? r.getOperatorNumeric() : "";
2237        if (operator != null) {
2238            String selection = "numeric = '" + operator + "'";
2239            // query only enabled apn.
2240            // carrier_enabled : 1 means enabled apn, 0 disabled apn.
2241            // selection += " and carrier_enabled = 1";
2242            if (DBG) log("createAllApnList: selection=" + selection);
2243
2244            Cursor cursor = mPhone.getContext().getContentResolver().query(
2245                    Telephony.Carriers.CONTENT_URI, null, selection, null, null);
2246
2247            if (cursor != null) {
2248                if (cursor.getCount() > 0) {
2249                    mAllApnSettings = createApnList(cursor);
2250                }
2251                cursor.close();
2252            }
2253        }
2254
2255        addEmergencyApnSetting();
2256
2257        dedupeApnSettings();
2258
2259        if (mAllApnSettings.isEmpty()) {
2260            if (DBG) log("createAllApnList: No APN found for carrier: " + operator);
2261            mPreferredApn = null;
2262            // TODO: What is the right behavior?
2263            //notifyNoData(DataConnection.FailCause.MISSING_UNKNOWN_APN);
2264        } else {
2265            mPreferredApn = getPreferredApn();
2266            if (mPreferredApn != null && !mPreferredApn.numeric.equals(operator)) {
2267                mPreferredApn = null;
2268                setPreferredApn(-1);
2269            }
2270            if (DBG) log("createAllApnList: mPreferredApn=" + mPreferredApn);
2271        }
2272        if (DBG) log("createAllApnList: X mAllApnSettings=" + mAllApnSettings);
2273
2274        setDataProfilesAsNeeded();
2275    }
2276
2277    private void dedupeApnSettings() {
2278        ArrayList<ApnSetting> resultApns = new ArrayList<ApnSetting>();
2279
2280        // coalesce APNs if they are similar enough to prevent
2281        // us from bringing up two data calls with the same interface
2282        int i = 0;
2283        while (i < mAllApnSettings.size() - 1) {
2284            ApnSetting first = mAllApnSettings.get(i);
2285            ApnSetting second = null;
2286            int j = i + 1;
2287            while (j < mAllApnSettings.size()) {
2288                second = mAllApnSettings.get(j);
2289                if (apnsSimilar(first, second)) {
2290                    ApnSetting newApn = mergeApns(first, second);
2291                    mAllApnSettings.set(i, newApn);
2292                    first = newApn;
2293                    mAllApnSettings.remove(j);
2294                } else {
2295                    j++;
2296                }
2297            }
2298            i++;
2299        }
2300    }
2301
2302    //check whether the types of two APN same (even only one type of each APN is same)
2303    private boolean apnTypeSameAny(ApnSetting first, ApnSetting second) {
2304        if(VDBG) {
2305            StringBuilder apnType1 = new StringBuilder(first.apn + ": ");
2306            for(int index1 = 0; index1 < first.types.length; index1++) {
2307                apnType1.append(first.types[index1]);
2308                apnType1.append(",");
2309            }
2310
2311            StringBuilder apnType2 = new StringBuilder(second.apn + ": ");
2312            for(int index1 = 0; index1 < second.types.length; index1++) {
2313                apnType2.append(second.types[index1]);
2314                apnType2.append(",");
2315            }
2316            log("APN1: is " + apnType1);
2317            log("APN2: is " + apnType2);
2318        }
2319
2320        for(int index1 = 0; index1 < first.types.length; index1++) {
2321            for(int index2 = 0; index2 < second.types.length; index2++) {
2322                if(first.types[index1].equals(PhoneConstants.APN_TYPE_ALL) ||
2323                        second.types[index2].equals(PhoneConstants.APN_TYPE_ALL) ||
2324                        first.types[index1].equals(second.types[index2])) {
2325                    if(VDBG)log("apnTypeSameAny: return true");
2326                    return true;
2327                }
2328            }
2329        }
2330
2331        if(VDBG)log("apnTypeSameAny: return false");
2332        return false;
2333    }
2334
2335    // Check if neither mention DUN and are substantially similar
2336    private boolean apnsSimilar(ApnSetting first, ApnSetting second) {
2337        return (first.canHandleType(PhoneConstants.APN_TYPE_DUN) == false &&
2338                second.canHandleType(PhoneConstants.APN_TYPE_DUN) == false &&
2339                Objects.equals(first.apn, second.apn) &&
2340                !apnTypeSameAny(first, second) &&
2341                xorEquals(first.proxy, second.proxy) &&
2342                xorEquals(first.port, second.port) &&
2343                first.carrierEnabled == second.carrierEnabled &&
2344                first.bearer == second.bearer &&
2345                first.profileId == second.profileId &&
2346                Objects.equals(first.mvnoType, second.mvnoType) &&
2347                Objects.equals(first.mvnoMatchData, second.mvnoMatchData) &&
2348                xorEquals(first.mmsc, second.mmsc) &&
2349                xorEquals(first.mmsProxy, second.mmsProxy) &&
2350                xorEquals(first.mmsPort, second.mmsPort));
2351    }
2352
2353    // equal or one is not specified
2354    private boolean xorEquals(String first, String second) {
2355        return (Objects.equals(first, second) ||
2356                TextUtils.isEmpty(first) ||
2357                TextUtils.isEmpty(second));
2358    }
2359
2360    private ApnSetting mergeApns(ApnSetting dest, ApnSetting src) {
2361        ArrayList<String> resultTypes = new ArrayList<String>();
2362        resultTypes.addAll(Arrays.asList(dest.types));
2363        for (String srcType : src.types) {
2364            if (resultTypes.contains(srcType) == false) resultTypes.add(srcType);
2365        }
2366        String mmsc = (TextUtils.isEmpty(dest.mmsc) ? src.mmsc : dest.mmsc);
2367        String mmsProxy = (TextUtils.isEmpty(dest.mmsProxy) ? src.mmsProxy : dest.mmsProxy);
2368        String mmsPort = (TextUtils.isEmpty(dest.mmsPort) ? src.mmsPort : dest.mmsPort);
2369        String proxy = (TextUtils.isEmpty(dest.proxy) ? src.proxy : dest.proxy);
2370        String port = (TextUtils.isEmpty(dest.port) ? src.port : dest.port);
2371        String protocol = src.protocol.equals("IPV4V6") ? src.protocol : dest.protocol;
2372        String roamingProtocol = src.roamingProtocol.equals("IPV4V6") ? src.roamingProtocol :
2373                dest.roamingProtocol;
2374
2375        return new ApnSetting(dest.id, dest.numeric, dest.carrier, dest.apn,
2376                proxy, port, mmsc, mmsProxy, mmsPort, dest.user, dest.password,
2377                dest.authType, resultTypes.toArray(new String[0]), protocol,
2378                roamingProtocol, dest.carrierEnabled, dest.bearer, dest.profileId,
2379                (dest.modemCognitive || src.modemCognitive), dest.maxConns, dest.waitTime,
2380                dest.maxConnsTime, dest.mtu, dest.mvnoType, dest.mvnoMatchData);
2381    }
2382
2383    /** Return the DC AsyncChannel for the new data connection */
2384    private DcAsyncChannel createDataConnection() {
2385        if (DBG) log("createDataConnection E");
2386
2387        int id = mUniqueIdGenerator.getAndIncrement();
2388        DataConnection conn = DataConnection.makeDataConnection(mPhone, id,
2389                                                this, mDcTesterFailBringUpAll, mDcc);
2390        mDataConnections.put(id, conn);
2391        DcAsyncChannel dcac = new DcAsyncChannel(conn, LOG_TAG);
2392        int status = dcac.fullyConnectSync(mPhone.getContext(), this, conn.getHandler());
2393        if (status == AsyncChannel.STATUS_SUCCESSFUL) {
2394            mDataConnectionAcHashMap.put(dcac.getDataConnectionIdSync(), dcac);
2395        } else {
2396            loge("createDataConnection: Could not connect to dcac=" + dcac + " status=" + status);
2397        }
2398
2399        if (DBG) log("createDataConnection() X id=" + id + " dc=" + conn);
2400        return dcac;
2401    }
2402
2403    private void destroyDataConnections() {
2404        if(mDataConnections != null) {
2405            if (DBG) log("destroyDataConnections: clear mDataConnectionList");
2406            mDataConnections.clear();
2407        } else {
2408            if (DBG) log("destroyDataConnections: mDataConnecitonList is empty, ignore");
2409        }
2410    }
2411
2412    /**
2413     * Build a list of APNs to be used to create PDP's.
2414     *
2415     * @param requestedApnType
2416     * @return waitingApns list to be used to create PDP
2417     *          error when waitingApns.isEmpty()
2418     */
2419    private ArrayList<ApnSetting> buildWaitingApns(String requestedApnType, int radioTech) {
2420        if (DBG) log("buildWaitingApns: E requestedApnType=" + requestedApnType);
2421        ArrayList<ApnSetting> apnList = new ArrayList<ApnSetting>();
2422
2423        if (requestedApnType.equals(PhoneConstants.APN_TYPE_DUN)) {
2424            ApnSetting dun = fetchDunApn();
2425            if (dun != null) {
2426                apnList.add(dun);
2427                if (DBG) log("buildWaitingApns: X added APN_TYPE_DUN apnList=" + apnList);
2428                return apnList;
2429            }
2430        }
2431
2432        IccRecords r = mIccRecords.get();
2433        String operator = (r != null) ? r.getOperatorNumeric() : "";
2434
2435        // This is a workaround for a bug (7305641) where we don't failover to other
2436        // suitable APNs if our preferred APN fails.  On prepaid ATT sims we need to
2437        // failover to a provisioning APN, but once we've used their default data
2438        // connection we are locked to it for life.  This change allows ATT devices
2439        // to say they don't want to use preferred at all.
2440        boolean usePreferred = true;
2441        try {
2442            usePreferred = ! mPhone.getContext().getResources().getBoolean(com.android.
2443                    internal.R.bool.config_dontPreferApn);
2444        } catch (Resources.NotFoundException e) {
2445            if (DBG) log("buildWaitingApns: usePreferred NotFoundException set to true");
2446            usePreferred = true;
2447        }
2448        if (DBG) {
2449            log("buildWaitingApns: usePreferred=" + usePreferred
2450                    + " canSetPreferApn=" + mCanSetPreferApn
2451                    + " mPreferredApn=" + mPreferredApn
2452                    + " operator=" + operator + " radioTech=" + radioTech
2453                    + " IccRecords r=" + r);
2454        }
2455
2456        if (usePreferred && mCanSetPreferApn && mPreferredApn != null &&
2457                mPreferredApn.canHandleType(requestedApnType)) {
2458            if (DBG) {
2459                log("buildWaitingApns: Preferred APN:" + operator + ":"
2460                        + mPreferredApn.numeric + ":" + mPreferredApn);
2461            }
2462            if (mPreferredApn.numeric.equals(operator)) {
2463                if (mPreferredApn.bearer == 0 || mPreferredApn.bearer == radioTech) {
2464                    apnList.add(mPreferredApn);
2465                    if (DBG) log("buildWaitingApns: X added preferred apnList=" + apnList);
2466                    return apnList;
2467                } else {
2468                    if (DBG) log("buildWaitingApns: no preferred APN");
2469                    setPreferredApn(-1);
2470                    mPreferredApn = null;
2471                }
2472            } else {
2473                if (DBG) log("buildWaitingApns: no preferred APN");
2474                setPreferredApn(-1);
2475                mPreferredApn = null;
2476            }
2477        }
2478        if (mAllApnSettings != null) {
2479            if (DBG) log("buildWaitingApns: mAllApnSettings=" + mAllApnSettings);
2480            for (ApnSetting apn : mAllApnSettings) {
2481                if (DBG) log("buildWaitingApns: apn=" + apn);
2482                if (apn.canHandleType(requestedApnType)) {
2483                    if (apn.bearer == 0 || apn.bearer == radioTech) {
2484                        if (DBG) log("buildWaitingApns: adding apn=" + apn.toString());
2485                        apnList.add(apn);
2486                    } else {
2487                        if (DBG) {
2488                            log("buildWaitingApns: bearer:" + apn.bearer + " != "
2489                                    + "radioTech:" + radioTech);
2490                        }
2491                    }
2492                } else {
2493                if (DBG) {
2494                    log("buildWaitingApns: couldn't handle requesedApnType="
2495                            + requestedApnType);
2496                }
2497            }
2498            }
2499        } else {
2500            loge("mAllApnSettings is empty!");
2501        }
2502        if (DBG) log("buildWaitingApns: X apnList=" + apnList);
2503        return apnList;
2504    }
2505
2506    private String apnListToString (ArrayList<ApnSetting> apns) {
2507        StringBuilder result = new StringBuilder();
2508        for (int i = 0, size = apns.size(); i < size; i++) {
2509            result.append('[')
2510                  .append(apns.get(i).toString())
2511                  .append(']');
2512        }
2513        return result.toString();
2514    }
2515
2516    private void setPreferredApn(int pos) {
2517        if (!mCanSetPreferApn) {
2518            log("setPreferredApn: X !canSEtPreferApn");
2519            return;
2520        }
2521
2522        String subId = Long.toString(mPhone.getSubId());
2523        Uri uri = Uri.withAppendedPath(PREFERAPN_NO_UPDATE_URI_USING_SUBID, subId);
2524        log("setPreferredApn: delete");
2525        ContentResolver resolver = mPhone.getContext().getContentResolver();
2526        resolver.delete(uri, null, null);
2527
2528        if (pos >= 0) {
2529            log("setPreferredApn: insert");
2530            ContentValues values = new ContentValues();
2531            values.put(APN_ID, pos);
2532            resolver.insert(uri, values);
2533        }
2534    }
2535
2536    private ApnSetting getPreferredApn() {
2537        if (mAllApnSettings.isEmpty()) {
2538            log("getPreferredApn: X not found mAllApnSettings.isEmpty");
2539            return null;
2540        }
2541
2542        String subId = Long.toString(mPhone.getSubId());
2543        Uri uri = Uri.withAppendedPath(PREFERAPN_NO_UPDATE_URI_USING_SUBID, subId);
2544        Cursor cursor = mPhone.getContext().getContentResolver().query(
2545                uri, new String[] { "_id", "name", "apn" },
2546                null, null, Telephony.Carriers.DEFAULT_SORT_ORDER);
2547
2548        if (cursor != null) {
2549            mCanSetPreferApn = true;
2550        } else {
2551            mCanSetPreferApn = false;
2552        }
2553        log("getPreferredApn: mRequestedApnType=" + mRequestedApnType + " cursor=" + cursor
2554                + " cursor.count=" + ((cursor != null) ? cursor.getCount() : 0));
2555
2556        if (mCanSetPreferApn && cursor.getCount() > 0) {
2557            int pos;
2558            cursor.moveToFirst();
2559            pos = cursor.getInt(cursor.getColumnIndexOrThrow(Telephony.Carriers._ID));
2560            for(ApnSetting p : mAllApnSettings) {
2561                log("getPreferredApn: apnSetting=" + p);
2562                if (p.id == pos && p.canHandleType(mRequestedApnType)) {
2563                    log("getPreferredApn: X found apnSetting" + p);
2564                    cursor.close();
2565                    return p;
2566                }
2567            }
2568        }
2569
2570        if (cursor != null) {
2571            cursor.close();
2572        }
2573
2574        log("getPreferredApn: X not found");
2575        return null;
2576    }
2577
2578    @Override
2579    public void handleMessage (Message msg) {
2580        if (DBG) log("handleMessage msg=" + msg);
2581
2582        if (!mPhone.mIsTheCurrentActivePhone || mIsDisposed) {
2583            loge("handleMessage: Ignore GSM msgs since GSM phone is inactive");
2584            return;
2585        }
2586
2587        switch (msg.what) {
2588            case DctConstants.EVENT_RECORDS_LOADED:
2589                onRecordsLoaded();
2590                break;
2591
2592            case DctConstants.EVENT_DATA_CONNECTION_DETACHED:
2593                onDataConnectionDetached();
2594                break;
2595
2596            case DctConstants.EVENT_DATA_CONNECTION_ATTACHED:
2597                onDataConnectionAttached();
2598                break;
2599
2600            case DctConstants.EVENT_DO_RECOVERY:
2601                doRecovery();
2602                break;
2603
2604            case DctConstants.EVENT_APN_CHANGED:
2605                onApnChanged();
2606                break;
2607
2608            case DctConstants.EVENT_PS_RESTRICT_ENABLED:
2609                /**
2610                 * We don't need to explicitly to tear down the PDP context
2611                 * when PS restricted is enabled. The base band will deactive
2612                 * PDP context and notify us with PDP_CONTEXT_CHANGED.
2613                 * But we should stop the network polling and prevent reset PDP.
2614                 */
2615                if (DBG) log("EVENT_PS_RESTRICT_ENABLED " + mIsPsRestricted);
2616                stopNetStatPoll();
2617                stopDataStallAlarm();
2618                mIsPsRestricted = true;
2619                break;
2620
2621            case DctConstants.EVENT_PS_RESTRICT_DISABLED:
2622                /**
2623                 * When PS restrict is removed, we need setup PDP connection if
2624                 * PDP connection is down.
2625                 */
2626                if (DBG) log("EVENT_PS_RESTRICT_DISABLED " + mIsPsRestricted);
2627                mIsPsRestricted  = false;
2628                if (isConnected()) {
2629                    startNetStatPoll();
2630                    startDataStallAlarm(DATA_STALL_NOT_SUSPECTED);
2631                } else {
2632                    // TODO: Should all PDN states be checked to fail?
2633                    if (mState == DctConstants.State.FAILED) {
2634                        cleanUpAllConnections(false, Phone.REASON_PS_RESTRICT_ENABLED);
2635                        mReregisterOnReconnectFailure = false;
2636                    }
2637                    ApnContext apnContext = mApnContexts.get(PhoneConstants.APN_TYPE_DEFAULT);
2638                    if (apnContext != null) {
2639                        apnContext.setReason(Phone.REASON_PS_RESTRICT_ENABLED);
2640                        trySetupData(apnContext);
2641                    } else {
2642                        loge("**** Default ApnContext not found ****");
2643                        if (Build.IS_DEBUGGABLE) {
2644                            throw new RuntimeException("Default ApnContext not found");
2645                        }
2646                    }
2647                }
2648                break;
2649
2650            case DctConstants.EVENT_TRY_SETUP_DATA:
2651                if (msg.obj instanceof ApnContext) {
2652                    onTrySetupData((ApnContext)msg.obj);
2653                } else if (msg.obj instanceof String) {
2654                    onTrySetupData((String)msg.obj);
2655                } else {
2656                    loge("EVENT_TRY_SETUP request w/o apnContext or String");
2657                }
2658                break;
2659
2660            case DctConstants.EVENT_CLEAN_UP_CONNECTION:
2661                boolean tearDown = (msg.arg1 == 0) ? false : true;
2662                if (DBG) log("EVENT_CLEAN_UP_CONNECTION tearDown=" + tearDown);
2663                if (msg.obj instanceof ApnContext) {
2664                    cleanUpConnection(tearDown, (ApnContext)msg.obj);
2665                } else {
2666                    loge("EVENT_CLEAN_UP_CONNECTION request w/o apn context, call super");
2667                    super.handleMessage(msg);
2668                }
2669                break;
2670            case DctConstants.EVENT_SET_INTERNAL_DATA_ENABLE:
2671                boolean enabled = (msg.arg1 == DctConstants.ENABLED) ? true : false;
2672                onSetInternalDataEnabled(enabled, (Message) msg.obj);
2673                break;
2674
2675            case DctConstants.EVENT_CLEAN_UP_ALL_CONNECTIONS:
2676                Message mCause = obtainMessage(DctConstants.EVENT_CLEAN_UP_ALL_CONNECTIONS, null);
2677                if ((msg.obj != null) && (msg.obj instanceof String)) {
2678                    mCause.obj = msg.obj;
2679                }
2680                super.handleMessage(mCause);
2681                break;
2682
2683            case DctConstants.EVENT_DATA_RAT_CHANGED:
2684                //May new Network allow setupData, so try it here
2685                setupDataOnConnectableApns(Phone.REASON_NW_TYPE_CHANGED,
2686                        RetryFailures.ONLY_ON_CHANGE);
2687                break;
2688
2689            case DctConstants.CMD_CLEAR_PROVISIONING_SPINNER:
2690                // Check message sender intended to clear the current spinner.
2691                if (mProvisioningSpinner == msg.obj) {
2692                    mProvisioningSpinner.dismiss();
2693                    mProvisioningSpinner = null;
2694                }
2695                break;
2696
2697            default:
2698                // handle the message in the super class DataConnectionTracker
2699                super.handleMessage(msg);
2700                break;
2701        }
2702    }
2703
2704    protected int getApnProfileID(String apnType) {
2705        if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_IMS)) {
2706            return RILConstants.DATA_PROFILE_IMS;
2707        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_FOTA)) {
2708            return RILConstants.DATA_PROFILE_FOTA;
2709        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_CBS)) {
2710            return RILConstants.DATA_PROFILE_CBS;
2711        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_IA)) {
2712            return RILConstants.DATA_PROFILE_DEFAULT; // DEFAULT for now
2713        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_DUN)) {
2714            return RILConstants.DATA_PROFILE_TETHERED;
2715        } else {
2716            return RILConstants.DATA_PROFILE_DEFAULT;
2717        }
2718    }
2719
2720    private int getCellLocationId() {
2721        int cid = -1;
2722        CellLocation loc = mPhone.getCellLocation();
2723
2724        if (loc != null) {
2725            if (loc instanceof GsmCellLocation) {
2726                cid = ((GsmCellLocation)loc).getCid();
2727            } else if (loc instanceof CdmaCellLocation) {
2728                cid = ((CdmaCellLocation)loc).getBaseStationId();
2729            }
2730        }
2731        return cid;
2732    }
2733
2734    private IccRecords getUiccRecords(int appFamily) {
2735        return mUiccController.getIccRecords(mPhone.getPhoneId(), appFamily);
2736    }
2737
2738
2739    @Override
2740    protected void onUpdateIcc() {
2741        if (mUiccController == null ) {
2742            return;
2743        }
2744
2745        IccRecords newIccRecords = getUiccRecords(UiccController.APP_FAM_3GPP);
2746
2747        IccRecords r = mIccRecords.get();
2748        if (r != newIccRecords) {
2749            if (r != null) {
2750                log("Removing stale icc objects.");
2751                r.unregisterForRecordsLoaded(this);
2752                mIccRecords.set(null);
2753            }
2754            if (newIccRecords != null) {
2755                log("New records found");
2756                mIccRecords.set(newIccRecords);
2757                newIccRecords.registerForRecordsLoaded(
2758                        this, DctConstants.EVENT_RECORDS_LOADED, null);
2759            }
2760        }
2761    }
2762
2763    public void update() {
2764        log("update sub = " + mPhone.getSubId());
2765        log("update(): Active DDS, register for all events now!");
2766        registerForAllEvents();
2767        onUpdateIcc();
2768
2769        mUserDataEnabled = getDataEnabled();
2770
2771        if (mPhone instanceof CDMALTEPhone) {
2772            ((CDMALTEPhone)mPhone).updateCurrentCarrierInProvider();
2773            supplyMessenger();
2774        } else if (mPhone instanceof GSMPhone) {
2775            ((GSMPhone)mPhone).updateCurrentCarrierInProvider();
2776            supplyMessenger();
2777        } else {
2778            log("Phone object is not MultiSim. This should not hit!!!!");
2779        }
2780    }
2781
2782    @Override
2783    public void cleanUpAllConnections(String cause) {
2784        cleanUpAllConnections(cause, null);
2785    }
2786
2787    public void updateRecords() {
2788        onUpdateIcc();
2789    }
2790
2791    public void cleanUpAllConnections(String cause, Message disconnectAllCompleteMsg) {
2792        log("cleanUpAllConnections");
2793        if (disconnectAllCompleteMsg != null) {
2794            mDisconnectAllCompleteMsgList.add(disconnectAllCompleteMsg);
2795        }
2796
2797        Message msg = obtainMessage(DctConstants.EVENT_CLEAN_UP_ALL_CONNECTIONS);
2798        msg.obj = cause;
2799        sendMessage(msg);
2800    }
2801
2802    protected void notifyDataDisconnectComplete() {
2803        log("notifyDataDisconnectComplete");
2804        for (Message m: mDisconnectAllCompleteMsgList) {
2805            m.sendToTarget();
2806        }
2807        mDisconnectAllCompleteMsgList.clear();
2808    }
2809
2810
2811    protected void notifyAllDataDisconnected() {
2812        sEnableFailFastRefCounter = 0;
2813        mFailFast = false;
2814        mAllDataDisconnectedRegistrants.notifyRegistrants();
2815    }
2816
2817    public void registerForAllDataDisconnected(Handler h, int what, Object obj) {
2818        mAllDataDisconnectedRegistrants.addUnique(h, what, obj);
2819
2820        if (isDisconnected()) {
2821            log("notify All Data Disconnected");
2822            notifyAllDataDisconnected();
2823        }
2824    }
2825
2826    public void unregisterForAllDataDisconnected(Handler h) {
2827        mAllDataDisconnectedRegistrants.remove(h);
2828    }
2829
2830
2831    @Override
2832    protected void onSetInternalDataEnabled(boolean enable) {
2833        if (DBG) log("onSetInternalDataEnabled: enabled=" + enable);
2834        onSetInternalDataEnabled(enable, null);
2835    }
2836
2837    protected void onSetInternalDataEnabled(boolean enabled, Message onCompleteMsg) {
2838        if (DBG) log("onSetInternalDataEnabled: enabled=" + enabled);
2839        boolean sendOnComplete = true;
2840
2841        synchronized (mDataEnabledLock) {
2842            mInternalDataEnabled = enabled;
2843            if (enabled) {
2844                log("onSetInternalDataEnabled: changed to enabled, try to setup data call");
2845                onTrySetupData(Phone.REASON_DATA_ENABLED);
2846            } else {
2847                sendOnComplete = false;
2848                log("onSetInternalDataEnabled: changed to disabled, cleanUpAllConnections");
2849                cleanUpAllConnections(null, onCompleteMsg);
2850            }
2851        }
2852
2853        if (sendOnComplete) {
2854            if (onCompleteMsg != null) {
2855                onCompleteMsg.sendToTarget();
2856            }
2857        }
2858    }
2859
2860    public boolean setInternalDataEnabledFlag(boolean enable) {
2861        if (DBG) log("setInternalDataEnabledFlag(" + enable + ")");
2862
2863        if (mInternalDataEnabled != enable) {
2864            mInternalDataEnabled = enable;
2865        }
2866        return true;
2867    }
2868
2869    @Override
2870    public boolean setInternalDataEnabled(boolean enable) {
2871        return setInternalDataEnabled(enable, null);
2872    }
2873
2874    public boolean setInternalDataEnabled(boolean enable, Message onCompleteMsg) {
2875        if (DBG) log("setInternalDataEnabled(" + enable + ")");
2876
2877        Message msg = obtainMessage(DctConstants.EVENT_SET_INTERNAL_DATA_ENABLE, onCompleteMsg);
2878        msg.arg1 = (enable ? DctConstants.ENABLED : DctConstants.DISABLED);
2879        sendMessage(msg);
2880        return true;
2881    }
2882
2883    public void setDataAllowed(boolean enable, Message response) {
2884         if (DBG) log("setDataAllowed: enable=" + enable);
2885         mIsCleanupRequired = !enable;
2886         mPhone.mCi.setDataAllowed(enable, response);
2887         mInternalDataEnabled = enable;
2888    }
2889
2890    @Override
2891    protected void log(String s) {
2892        Rlog.d(LOG_TAG, "[" + mPhone.getPhoneId() + "]" + s);
2893    }
2894
2895    @Override
2896    protected void loge(String s) {
2897        Rlog.e(LOG_TAG, "[" + mPhone.getPhoneId() + "]" + s);
2898    }
2899
2900    @Override
2901    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2902        pw.println("DcTracker extends:");
2903        super.dump(fd, pw, args);
2904        pw.println(" mReregisterOnReconnectFailure=" + mReregisterOnReconnectFailure);
2905        pw.println(" canSetPreferApn=" + mCanSetPreferApn);
2906        pw.println(" mApnObserver=" + mApnObserver);
2907        pw.println(" getOverallState=" + getOverallState());
2908        pw.println(" mDataConnectionAsyncChannels=%s\n" + mDataConnectionAcHashMap);
2909        pw.println(" mAttached=" + mAttached.get());
2910    }
2911
2912    @Override
2913    public String[] getPcscfAddress(String apnType) {
2914        log("getPcscfAddress()");
2915        ApnContext apnContext = null;
2916
2917        if(apnType == null){
2918            log("apnType is null, return null");
2919            return null;
2920        }
2921
2922        if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_EMERGENCY)) {
2923            apnContext = mApnContexts.get(PhoneConstants.APN_TYPE_EMERGENCY);
2924        } else if (TextUtils.equals(apnType, PhoneConstants.APN_TYPE_IMS)) {
2925            apnContext = mApnContexts.get(PhoneConstants.APN_TYPE_IMS);
2926        } else {
2927            log("apnType is invalid, return null");
2928            return null;
2929        }
2930
2931        if (apnContext == null) {
2932            log("apnContext is null, return null");
2933            return null;
2934        }
2935
2936        DcAsyncChannel dcac = apnContext.getDcAc();
2937        String[] result = null;
2938
2939        if (dcac != null) {
2940            result = dcac.getPcscfAddr();
2941
2942            for (int i = 0; i < result.length; i++) {
2943                log("Pcscf[" + i + "]: " + result[i]);
2944            }
2945            return result;
2946        }
2947        return null;
2948    }
2949
2950    @Override
2951    public void setImsRegistrationState(boolean registered) {
2952        log("setImsRegistrationState - mImsRegistrationState(before): "+ mImsRegistrationState
2953                + ", registered(current) : " + registered);
2954
2955        if (mPhone == null) return;
2956
2957        ServiceStateTracker sst = mPhone.getServiceStateTracker();
2958        if (sst == null) return;
2959
2960        sst.setImsRegistrationState(registered);
2961    }
2962
2963    /**
2964     * Read APN configuration from Telephony.db for Emergency APN
2965     * All opertors recognize the connection request for EPDN based on APN type
2966     * PLMN name,APN name are not mandatory parameters
2967     */
2968    private void initEmergencyApnSetting() {
2969        // Operator Numeric is not available when sim records are not loaded.
2970        // Query Telephony.db with APN type as EPDN request does not
2971        // require APN name, plmn and all operators support same APN config.
2972        // DB will contain only one entry for Emergency APN
2973        String selection = "type=\"emergency\"";
2974        Cursor cursor = mPhone.getContext().getContentResolver().query(
2975                Telephony.Carriers.CONTENT_URI, null, selection, null, null);
2976
2977        if (cursor != null) {
2978            if (cursor.getCount() > 0) {
2979                if (cursor.moveToFirst()) {
2980                    mEmergencyApn = makeApnSetting(cursor);
2981                }
2982            }
2983            cursor.close();
2984        }
2985    }
2986
2987    /**
2988     * Add the Emergency APN settings to APN settings list
2989     */
2990    private void addEmergencyApnSetting() {
2991        if(mEmergencyApn != null) {
2992            if(mAllApnSettings == null) {
2993                mAllApnSettings = new ArrayList<ApnSetting>();
2994            } else {
2995                boolean hasEmergencyApn = false;
2996                for (ApnSetting apn : mAllApnSettings) {
2997                    if (ArrayUtils.contains(apn.types, PhoneConstants.APN_TYPE_EMERGENCY)) {
2998                        hasEmergencyApn = true;
2999                        break;
3000                    }
3001                }
3002
3003                if(hasEmergencyApn == false) {
3004                    mAllApnSettings.add(mEmergencyApn);
3005                } else {
3006                    log("addEmergencyApnSetting - E-APN setting is already present");
3007                }
3008            }
3009        }
3010    }
3011
3012    private void cleanUpConnectionsOnUpdatedApns(boolean tearDown) {
3013        if (DBG) log("cleanUpConnectionsOnUpdatedApns: tearDown=" + tearDown);
3014        if (mAllApnSettings.isEmpty()) {
3015            cleanUpAllConnections(tearDown, Phone.REASON_APN_CHANGED);
3016        } else {
3017            for (ApnContext apnContext : mApnContexts.values()) {
3018                if (VDBG) log("cleanUpConnectionsOnUpdatedApns for "+ apnContext);
3019
3020                boolean cleanUpApn = true;
3021                ArrayList<ApnSetting> currentWaitingApns = apnContext.getWaitingApns();
3022
3023                if ((currentWaitingApns != null) && (!apnContext.isDisconnected())) {
3024                    int radioTech = mPhone.getServiceState().getRilDataRadioTechnology();
3025                    ArrayList<ApnSetting> waitingApns = buildWaitingApns(
3026                            apnContext.getApnType(), radioTech);
3027                    if (VDBG) log("new waitingApns:" + waitingApns);
3028                    if (waitingApns.size() == currentWaitingApns.size()) {
3029                        cleanUpApn = false;
3030                        for (int i = 0; i < waitingApns.size(); i++) {
3031                            if (!currentWaitingApns.get(i).equals(waitingApns.get(i))) {
3032                                if (VDBG) log("new waiting apn is different at " + i);
3033                                cleanUpApn = true;
3034                                apnContext.setWaitingApns(waitingApns);
3035                                break;
3036                            }
3037                        }
3038                    }
3039                }
3040
3041                if (cleanUpApn) {
3042                    apnContext.setReason(Phone.REASON_APN_CHANGED);
3043                    cleanUpConnection(true, apnContext);
3044                }
3045            }
3046        }
3047
3048        if (!isConnected()) {
3049            stopNetStatPoll();
3050            stopDataStallAlarm();
3051        }
3052
3053        mRequestedApnType = PhoneConstants.APN_TYPE_DEFAULT;
3054
3055        if (DBG) log("mDisconnectPendingCount = " + mDisconnectPendingCount);
3056        if (tearDown && mDisconnectPendingCount == 0) {
3057            notifyDataDisconnectComplete();
3058            notifyAllDataDisconnected();
3059        }
3060    }
3061}
3062