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