Fix data state change callbacks

Initial state should be unknown or we miss the first connected change.
Don't send a disconnected msg when changing network types.
Filter out redundent disconnects.
Add some logging.

bug:3060742
Change-Id: Idc797c1276b7417337a91ed60b12b1bf392d57c0
diff --git a/services/java/com/android/server/TelephonyRegistry.java b/services/java/com/android/server/TelephonyRegistry.java
index a7a0c68..689b1c6 100644
--- a/services/java/com/android/server/TelephonyRegistry.java
+++ b/services/java/com/android/server/TelephonyRegistry.java
@@ -85,7 +85,7 @@
 
     private int mDataActivity = TelephonyManager.DATA_ACTIVITY_NONE;
 
-    private int mDataConnectionState = TelephonyManager.DATA_CONNECTED;
+    private int mDataConnectionState = TelephonyManager.DATA_UNKNOWN;
 
     private boolean mDataConnectionPossible = false;
 
@@ -401,13 +401,14 @@
                     }
                 }
             } else {
-                mConnectedApns.remove(apnType);
-                if (mConnectedApns.isEmpty()) {
-                    mDataConnectionState = state;
-                    modified = true;
-                } else {
-                    // leave mDataConnectionState as is and
-                    // send out the new status for the APN in question.
+                if (mConnectedApns.remove(apnType)) {
+                    if (mConnectedApns.isEmpty()) {
+                        mDataConnectionState = state;
+                        modified = true;
+                    } else {
+                        // leave mDataConnectionState as is and
+                        // send out the new status for the APN in question.
+                    }
                 }
             }
             mDataConnectionPossible = isDataConnectivityPossible;
@@ -416,9 +417,11 @@
             mDataConnectionLinkCapabilities = linkCapabilities;
             if (mDataConnectionNetworkType != networkType) {
                 mDataConnectionNetworkType = networkType;
+                // need to tell registered listeners about the new network type
                 modified = true;
             }
             if (modified) {
+                Slog.d(TAG, "onDataConnectionStateChanged(" + state + ", " + networkType + ")");
                 for (Record r : mRecords) {
                     if ((r.events & PhoneStateListener.LISTEN_DATA_CONNECTION_STATE) != 0) {
                         try {
diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java
index 1cd3679..9ef41f2 100644
--- a/telephony/java/android/telephony/TelephonyManager.java
+++ b/telephony/java/android/telephony/TelephonyManager.java
@@ -802,6 +802,10 @@
       }
     }
 
+    /** Data connection state: Unknown.  Used before we know the state.
+     * @hide
+     */
+    public static final int DATA_UNKNOWN        = -1;
     /** Data connection state: Disconnected. IP traffic not available. */
     public static final int DATA_DISCONNECTED   = 0;
     /** Data connection state: Currently setting up a data connection. */
diff --git a/telephony/java/com/android/internal/telephony/DataConnectionTracker.java b/telephony/java/com/android/internal/telephony/DataConnectionTracker.java
index f8812ec..2a21f6d 100644
--- a/telephony/java/com/android/internal/telephony/DataConnectionTracker.java
+++ b/telephony/java/com/android/internal/telephony/DataConnectionTracker.java
@@ -659,6 +659,9 @@
     protected abstract boolean isDataAllowed();
 
     public boolean isApnTypeEnabled(String apnType) {
+        if (apnType == null) {
+            apnType = getActiveApnString();
+        }
         return isApnIdEnabled(apnTypeToId(apnType));
     }
 
diff --git a/telephony/java/com/android/internal/telephony/DefaultPhoneNotifier.java b/telephony/java/com/android/internal/telephony/DefaultPhoneNotifier.java
index 14808b6..52cbd7c 100644
--- a/telephony/java/com/android/internal/telephony/DefaultPhoneNotifier.java
+++ b/telephony/java/com/android/internal/telephony/DefaultPhoneNotifier.java
@@ -94,10 +94,6 @@
         }
     }
 
-    public void notifyDataConnection(Phone sender, String reason, String apnType) {
-        doNotifyDataConnection(sender, reason, apnType, sender.getDataConnectionState(apnType));
-    }
-
     public void notifyDataConnection(Phone sender, String reason, String apnType,
             Phone.DataState state) {
         doNotifyDataConnection(sender, reason, apnType, state);
diff --git a/telephony/java/com/android/internal/telephony/PhoneBase.java b/telephony/java/com/android/internal/telephony/PhoneBase.java
index dddb493..83080ee 100644
--- a/telephony/java/com/android/internal/telephony/PhoneBase.java
+++ b/telephony/java/com/android/internal/telephony/PhoneBase.java
@@ -742,7 +742,12 @@
     }
 
     public void notifyDataConnection(String reason, String apnType) {
-        mNotifier.notifyDataConnection(this, reason, apnType);
+        mNotifier.notifyDataConnection(this, reason, apnType, getDataConnectionState(apnType));
+    }
+
+    public void notifyDataConnection() {
+        String apn = getActiveApn();
+        mNotifier.notifyDataConnection(this, null, apn, getDataConnectionState(apn));
     }
 
     public void notifyOtaspChanged(int otaspMode) {
diff --git a/telephony/java/com/android/internal/telephony/PhoneNotifier.java b/telephony/java/com/android/internal/telephony/PhoneNotifier.java
index b1cf953..28a8d22 100644
--- a/telephony/java/com/android/internal/telephony/PhoneNotifier.java
+++ b/telephony/java/com/android/internal/telephony/PhoneNotifier.java
@@ -33,8 +33,7 @@
 
     public void notifyCallForwardingChanged(Phone sender);
 
-    public void notifyDataConnection(Phone sender, String reason, String apnType);
-
+    /** TODO - reason should never be null */
     public void notifyDataConnection(Phone sender, String reason, String apnType,
             Phone.DataState state);
 
diff --git a/telephony/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java b/telephony/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java
index 11e04d4..c006fcf 100755
--- a/telephony/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java
+++ b/telephony/java/com/android/internal/telephony/cdma/CdmaServiceStateTracker.java
@@ -1159,7 +1159,7 @@
         }
 
         if (hasCdmaDataConnectionChanged || hasNetworkTypeChanged) {
-            phone.notifyDataConnection(null, null);
+            phone.notifyDataConnection();
         }
 
         if (hasRoamingOn) {
diff --git a/telephony/java/com/android/internal/telephony/gsm/GsmServiceStateTracker.java b/telephony/java/com/android/internal/telephony/gsm/GsmServiceStateTracker.java
index bc41b01..2cbd63c 100644
--- a/telephony/java/com/android/internal/telephony/gsm/GsmServiceStateTracker.java
+++ b/telephony/java/com/android/internal/telephony/gsm/GsmServiceStateTracker.java
@@ -1033,8 +1033,7 @@
         }
 
         if (hasNetworkTypeChanged) {
-            // TODO - do we really want this?
-            phone.notifyDataConnection(null, null);
+            phone.notifyDataConnection();
         }
 
         if (hasRoamingOn) {