Merge "Fix folder list loading." into jb-ub-mail-ur10
diff --git a/src/com/android/email/service/AttachmentDownloadService.java b/src/com/android/email/service/AttachmentDownloadService.java
index 1e65f30..357e9e3 100644
--- a/src/com/android/email/service/AttachmentDownloadService.java
+++ b/src/com/android/email/service/AttachmentDownloadService.java
@@ -921,7 +921,15 @@
         // Loop until stopped, with a 30 minute wait loop
         while (!mStop) {
             // Here's where we run our attachment loading logic...
-            mConnectivityManager.waitForConnectivity();
+            // Make a local copy of the variable so we don't null-crash on service shutdown
+            final EmailConnectivityManager ecm = mConnectivityManager;
+            if (ecm != null) {
+                ecm.waitForConnectivity();
+            }
+            if (mStop) {
+                // We might be bailing out here due to the service shutting down
+                break;
+            }
             mDownloadSet.processQueue();
             if (mDownloadSet.isEmpty()) {
                 LogUtils.d(TAG, "*** All done; shutting down service");
@@ -938,8 +946,10 @@
         }
 
         // Unregister now that we're done
-        if (mConnectivityManager != null) {
-            mConnectivityManager.unregister();
+        // Make a local copy of the variable so we don't null-crash on service shutdown
+        final EmailConnectivityManager ecm = mConnectivityManager;
+        if (ecm != null) {
+            ecm.unregister();
         }
     }
 
@@ -975,6 +985,7 @@
         }
         if (mConnectivityManager != null) {
             mConnectivityManager.unregister();
+            mConnectivityManager.stopWait();
             mConnectivityManager = null;
         }
     }