Merge "Fix some apn settings issues." into jb-dev
diff --git a/telephony/java/com/android/internal/telephony/ApnContext.java b/telephony/java/com/android/internal/telephony/ApnContext.java
index 80d5044..e984a87 100644
--- a/telephony/java/com/android/internal/telephony/ApnContext.java
+++ b/telephony/java/com/android/internal/telephony/ApnContext.java
@@ -129,9 +129,9 @@
         return apn;
     }
 
-    public synchronized void removeNextWaitingApn() {
-        if ((mWaitingApns != null) && (!mWaitingApns.isEmpty())) {
-            mWaitingApns.remove(0);
+    public synchronized void removeWaitingApn(ApnSetting apn) {
+        if (mWaitingApns != null) {
+            mWaitingApns.remove(apn);
         }
     }
 
diff --git a/telephony/java/com/android/internal/telephony/gsm/GsmDataConnectionTracker.java b/telephony/java/com/android/internal/telephony/gsm/GsmDataConnectionTracker.java
index bfdb706..e2579e3 100644
--- a/telephony/java/com/android/internal/telephony/gsm/GsmDataConnectionTracker.java
+++ b/telephony/java/com/android/internal/telephony/gsm/GsmDataConnectionTracker.java
@@ -1970,19 +1970,11 @@
                 handleError = true;
             } else {
                 DataConnection dc = apnContext.getDataConnection();
-
-                if (DBG) {
-                    // TODO We may use apnContext.getApnSetting() directly
-                    // instead of getWaitingApns().get(0)
-                    String apnStr = "<unknown>";
-                    if (apnContext.getWaitingApns() != null
-                            && !apnContext.getWaitingApns().isEmpty()){
-                        apnStr = apnContext.getWaitingApns().get(0).apn;
-                    }
-                    log("onDataSetupComplete: success apn=" + apnStr);
-                }
                 ApnSetting apn = apnContext.getApnSetting();
-                if (apn.proxy != null && apn.proxy.length() != 0) {
+                if (DBG) {
+                    log("onDataSetupComplete: success apn=" + (apn == null ? "unknown" : apn.apn));
+                }
+                if (apn != null && apn.proxy != null && apn.proxy.length() != 0) {
                     try {
                         String port = apn.port;
                         if (TextUtils.isEmpty(port)) port = "8080";
@@ -2000,7 +1992,7 @@
                     SystemProperties.set("gsm.defaultpdpcontext.active", "true");
                     if (canSetPreferApn && mPreferredApn == null) {
                         if (DBG) log("onDataSetupComplete: PREFERED APN is null");
-                        mPreferredApn = apnContext.getApnSetting();
+                        mPreferredApn = apn;
                         if (mPreferredApn != null) {
                             setPreferredApn(mPreferredApn.id);
                         }
@@ -2011,16 +2003,11 @@
                 notifyDefaultData(apnContext);
             }
         } else {
-            String apnString;
-
             cause = (DataConnection.FailCause) (ar.result);
             if (DBG) {
-                try {
-                    apnString = apnContext.getWaitingApns().get(0).apn;
-                } catch (Exception e) {
-                    apnString = "<unknown>";
-                }
-                log(String.format("onDataSetupComplete: error apn=%s cause=%s", apnString, cause));
+                ApnSetting apn = apnContext.getApnSetting();
+                log(String.format("onDataSetupComplete: error apn=%s cause=%s",
+                        (apn == null ? "unknown" : apn.apn), cause));
             }
             if (cause.isEventLoggable()) {
                 // Log this failure to the Event Logs.
@@ -2032,7 +2019,7 @@
             // Count permanent failures and remove the APN we just tried
             if (cause.isPermanentFail()) apnContext.decWaitingApnsPermFailCount();
 
-            apnContext.removeNextWaitingApn();
+            apnContext.removeWaitingApn(apnContext.getApnSetting());
             if (DBG) {
                 log(String.format("onDataSetupComplete: WaitingApns.size=%d" +
                         " WaitingApnsPermFailureCountDown=%d",