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