am 66ad1169: Merge "Enhance Mms connectivity tracker workflow"

* commit '66ad116909c3fe1bb60af3f228dc7b06b9e2d1d7':
  Enhance Mms connectivity tracker workflow
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 469e517..09bcb1d 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -197,6 +197,9 @@
                 <action android:name="android.intent.action.CONTENT_CHANGED" />
             </intent-filter>
             <intent-filter>
+                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
+            </intent-filter>
+            <intent-filter>
                 <action android:name="android.intent.action.BOOT_COMPLETED" />
             </intent-filter>
         </receiver>
diff --git a/src/com/android/mms/transaction/MmsSystemEventReceiver.java b/src/com/android/mms/transaction/MmsSystemEventReceiver.java
index 2b70ba9..f5ed3ac 100644
--- a/src/com/android/mms/transaction/MmsSystemEventReceiver.java
+++ b/src/com/android/mms/transaction/MmsSystemEventReceiver.java
@@ -20,13 +20,12 @@
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
-import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
 import android.net.Uri;
 import android.provider.Telephony.Mms;
 import android.util.Log;
 
-import com.android.internal.telephony.PhoneConstants;
-import com.android.internal.telephony.TelephonyIntents;
 import com.android.mms.LogTag;
 import com.android.mms.MmsApp;
 
@@ -43,7 +42,7 @@
  */
 public class MmsSystemEventReceiver extends BroadcastReceiver {
     private static final String TAG = "MmsSystemEventReceiver";
-    private static MmsSystemEventReceiver sMmsSystemEventReceiver;
+    private static ConnectivityManager mConnMgr = null;
 
     private static void wakeUpService(Context context) {
         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
@@ -63,18 +62,23 @@
         if (action.equals(Mms.Intents.CONTENT_CHANGED_ACTION)) {
             Uri changed = (Uri) intent.getParcelableExtra(Mms.Intents.DELETED_CONTENTS);
             MmsApp.getApplication().getPduLoaderManager().removePdu(changed);
-        } else if (action.equals(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED)) {
-            String apnType = intent.getStringExtra(PhoneConstants.DATA_APN_TYPE_KEY);
-            boolean available = !(intent.getBooleanExtra(
-                                      PhoneConstants.NETWORK_UNAVAILABLE_KEY, false));
+        } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
+            if (mConnMgr == null) {
+                mConnMgr = (ConnectivityManager) context
+                        .getSystemService(Context.CONNECTIVITY_SERVICE);
+            }
+            NetworkInfo mmsNetworkInfo = mConnMgr
+                    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
+            boolean available = mmsNetworkInfo.isAvailable();
+            boolean isConnected = mmsNetworkInfo.isConnected();
 
             if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
-                Log.v(TAG, "ANY_DATA_STATE event received: apnType = " + apnType +
-                           ", available = " + available);
+                Log.v(TAG, "TYPE_MOBILE_MMS available = " + available +
+                           ", isConnected = " + isConnected);
             }
 
-            // Wake up transact service when MMS data is available.
-            if (apnType.equals(PhoneConstants.APN_TYPE_MMS) && available) {
+            // Wake up transact service when MMS data is available and isn't connected.
+            if (available && !isConnected) {
                 wakeUpService(context);
             }
         } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
@@ -89,32 +93,4 @@
             wakeUpService(context);
         }
     }
-
-    public static void registerForConnectionStateChanges(Context context) {
-        unRegisterForConnectionStateChanges(context);
-
-        IntentFilter intentFilter = new IntentFilter();
-        intentFilter.addAction(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
-        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
-            Log.v(TAG, "registerForConnectionStateChanges");
-        }
-        if (sMmsSystemEventReceiver == null) {
-            sMmsSystemEventReceiver = new MmsSystemEventReceiver();
-        }
-
-        context.registerReceiver(sMmsSystemEventReceiver, intentFilter);
-    }
-
-    public static void unRegisterForConnectionStateChanges(Context context) {
-        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
-            Log.v(TAG, "unRegisterForConnectionStateChanges");
-        }
-        if (sMmsSystemEventReceiver != null) {
-            try {
-                context.unregisterReceiver(sMmsSystemEventReceiver);
-            } catch (IllegalArgumentException e) {
-                // Allow un-matched register-unregister calls
-            }
-        }
-    }
 }
diff --git a/src/com/android/mms/transaction/TransactionService.java b/src/com/android/mms/transaction/TransactionService.java
index 4230b16..3de1f71 100644
--- a/src/com/android/mms/transaction/TransactionService.java
+++ b/src/com/android/mms/transaction/TransactionService.java
@@ -229,19 +229,6 @@
                     int columnIndexOfMsgType = cursor.getColumnIndexOrThrow(
                             PendingMessages.MSG_TYPE);
 
-                    if (noNetwork) {
-                        // Make sure we register for connection state changes.
-                        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
-                            Log.v(TAG, "onNewIntent: registerForConnectionStateChanges");
-                        }
-                        MmsSystemEventReceiver.registerForConnectionStateChanges(
-                                getApplicationContext());
-                    } else {
-                        // MMS data is available now, do not need listen to state change.
-                        MmsSystemEventReceiver.unRegisterForConnectionStateChanges(
-                                getApplicationContext());
-                    }
-
                     while (cursor.moveToNext()) {
                         int msgType = cursor.getInt(columnIndexOfMsgType);
                         int transactionType = getTransactionType(msgType);
@@ -303,11 +290,6 @@
                 if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
                     Log.v(TAG, "stopSelfIfIdle: STOP!");
                 }
-                // Make sure we're no longer listening for connection state changes.
-                if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
-                    Log.v(TAG, "stopSelfIfIdle: unRegisterForConnectionStateChanges");
-                }
-                MmsSystemEventReceiver.unRegisterForConnectionStateChanges(getApplicationContext());
 
                 stopSelf(startId);
             }
@@ -319,8 +301,12 @@
     }
 
     private boolean isNetworkAvailable() {
-        NetworkInfo ni = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
-        return (ni == null ? false : ni.isAvailable());
+        if (mConnMgr == null) {
+            return false;
+        } else {
+            NetworkInfo ni = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
+            return (ni == null ? false : ni.isAvailable());
+        }
     }
 
     private int getTransactionType(int msgType) {
@@ -473,7 +459,6 @@
             sendBroadcast(intent);
         } finally {
             transaction.detach(this);
-            MmsSystemEventReceiver.unRegisterForConnectionStateChanges(getApplicationContext());
             stopSelf(serviceId);
         }
     }
@@ -885,11 +870,15 @@
                 return;
             }
 
-            boolean noConnectivity =
-                intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
+            NetworkInfo mmsNetworkInfo = null;
 
-            NetworkInfo networkInfo = (NetworkInfo)
-                intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
+            if (mConnMgr != null) {
+                mmsNetworkInfo = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
+            } else {
+                if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
+                    Log.v(TAG, "mConnMgr is null, bail");
+                }
+            }
 
             /*
              * If we are being informed that connectivity has been established
@@ -898,47 +887,50 @@
              */
 
             if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
-                Log.v(TAG, "Handle ConnectivityBroadcastReceiver.onReceive(): " + networkInfo);
+                Log.v(TAG, "Handle ConnectivityBroadcastReceiver.onReceive(): " + mmsNetworkInfo);
             }
 
             // Check availability of the mobile network.
-            if ((networkInfo == null) || (networkInfo.getType() !=
-                    ConnectivityManager.TYPE_MOBILE_MMS)) {
+            if ((mmsNetworkInfo == null)) {
                 if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
-                    Log.v(TAG, "   type is not TYPE_MOBILE_MMS, bail");
+                    Log.v(TAG, "mms type is null, bail");
                 }
+            } else {
                 // This is a very specific fix to handle the case where the phone receives an
                 // incoming call during the time we're trying to setup the mms connection.
                 // When the call ends, restart the process of mms connectivity.
-                if (networkInfo != null &&
-                        Phone.REASON_VOICE_CALL_ENDED.equals(networkInfo.getReason())) {
+                if (Phone.REASON_VOICE_CALL_ENDED.equals(mmsNetworkInfo.getReason())) {
                     if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
                         Log.v(TAG, "   reason is " + Phone.REASON_VOICE_CALL_ENDED +
                                 ", retrying mms connectivity");
                     }
                     renewMmsConnectivity();
+                    return;
                 }
-                return;
-            }
 
-            if (!networkInfo.isConnected()) {
-                if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
-                    Log.v(TAG, "   TYPE_MOBILE_MMS not connected, bail");
+                if (mmsNetworkInfo.isConnected()) {
+                    TransactionSettings settings = new TransactionSettings(
+                            TransactionService.this, mmsNetworkInfo.getExtraInfo());
+                    // If this APN doesn't have an MMSC, wait for one that does.
+                    if (TextUtils.isEmpty(settings.getMmscUrl())) {
+                        Log.v(TAG, "   empty MMSC url, bail");
+                        return;
+                    }
+                    mServiceHandler.processPendingTransaction(null, settings);
+                } else {
+                    if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
+                        Log.v(TAG, "   TYPE_MOBILE_MMS not connected, bail");
+                    }
+
+                    // Retry mms connectivity once it's possible to connect
+                    if (mmsNetworkInfo.isAvailable()) {
+                        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
+                            Log.v(TAG, "   retrying mms connectivity for it's available");
+                        }
+                        renewMmsConnectivity();
+                    }
                 }
-                return;
             }
-
-            TransactionSettings settings = new TransactionSettings(
-                    TransactionService.this, networkInfo.getExtraInfo());
-
-            // If this APN doesn't have an MMSC, wait for one that does.
-            if (TextUtils.isEmpty(settings.getMmscUrl())) {
-                Log.v(TAG, "   empty MMSC url, bail");
-                return;
-            }
-
-            renewMmsConnectivity();
-            mServiceHandler.processPendingTransaction(null, settings);
         }
     };
 }