From 973702b30e8c2fb2f622f4ef37b42b3bdbd3ef17 Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Mon, 7 Mar 2011 18:01:58 -0800 Subject: [PATCH] Only allow prefetch when on a WiFi network Bug: 3515125 Change-Id: I0814ea20b1fc73807667e7f9531c1ea065bfa803 --- src/com/android/email/AttachmentInfo.java | 6 ++--- .../email/EmailConnectivityManager.java | 23 +++++++++++++++++++ .../service/AttachmentDownloadService.java | 5 ++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/com/android/email/AttachmentInfo.java b/src/com/android/email/AttachmentInfo.java index 22d632f60..c8d3c307d 100644 --- a/src/com/android/email/AttachmentInfo.java +++ b/src/com/android/email/AttachmentInfo.java @@ -151,10 +151,8 @@ public class AttachmentInfo { // Check for file size exceeded // The size limit is overridden when on a wifi connection - any size is OK if (mSize > AttachmentUtilities.MAX_ATTACHMENT_DOWNLOAD_SIZE) { - ConnectivityManager cm = (ConnectivityManager) - context.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo network = cm.getActiveNetworkInfo(); - if (network == null || network.getType() != ConnectivityManager.TYPE_WIFI) { + int networkType = EmailConnectivityManager.getActiveNetworkType(context); + if (networkType != ConnectivityManager.TYPE_WIFI) { canView = false; canSave = false; denyFlags |= DENY_WIFIONLY; diff --git a/src/com/android/email/EmailConnectivityManager.java b/src/com/android/email/EmailConnectivityManager.java index 2e80eac4d..7f549cbb1 100644 --- a/src/com/android/email/EmailConnectivityManager.java +++ b/src/com/android/email/EmailConnectivityManager.java @@ -45,6 +45,9 @@ public class EmailConnectivityManager extends BroadcastReceiver { // Loop time while waiting (stopgap in case we don't get a broadcast) private static final int CONNECTIVITY_WAIT_TIME = 10*60*1000; + // Sentinel value for "no active network" + public static final int NO_ACTIVE_NETWORK = -1; + // The name of this manager (used for logging) private final String mName; // The monitor lock we use while waiting for connectivity @@ -151,6 +154,26 @@ public class EmailConnectivityManager extends BroadcastReceiver { return (info != null); } + /** + * Get the type of the currently active data network + * @return the type of the active network (or NO_ACTIVE_NETWORK) + */ + public int getActiveNetworkType() { + return getActiveNetworkType(mConnectivityManager); + } + + static public int getActiveNetworkType(Context context) { + ConnectivityManager cm = + (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); + return getActiveNetworkType(cm); + } + + static public int getActiveNetworkType(ConnectivityManager cm) { + NetworkInfo info = cm.getActiveNetworkInfo(); + if (info == null) return NO_ACTIVE_NETWORK; + return info.getType(); + } + public void waitForConnectivity() { // If we're unregistered, throw an exception if (!mRegistered) { diff --git a/src/com/android/email/service/AttachmentDownloadService.java b/src/com/android/email/service/AttachmentDownloadService.java index 3b0ea1739..e52c2da72 100644 --- a/src/com/android/email/service/AttachmentDownloadService.java +++ b/src/com/android/email/service/AttachmentDownloadService.java @@ -40,6 +40,7 @@ import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.Cursor; +import android.net.ConnectivityManager; import android.net.Uri; import android.os.IBinder; import android.os.RemoteException; @@ -329,6 +330,10 @@ public class AttachmentDownloadService extends Service implements Runnable { // Don't prefetch if background downloading is disallowed if (!mConnectivityManager.isBackgroundDataAllowed()) return; + // Don't prefetch unless we're on a WiFi network + if (mConnectivityManager.getActiveNetworkType() != ConnectivityManager.TYPE_WIFI) { + return; + } // Then, try opportunistic download of appropriate attachments int backgroundDownloads = MAX_SIMULTANEOUS_DOWNLOADS - mDownloadsInProgress.size(); // Always leave one slot for user requested download