am e233fe31: Merge "Only allow prefetch when on a WiFi network" into honeycomb-mr1

* commit 'e233fe315b5fdc5050ea4e3428856a5370567062':
  Only allow prefetch when on a WiFi network
This commit is contained in:
Marc Blank 2011-03-09 16:13:14 -08:00 committed by Android Git Automerger
commit 4af17a654b
3 changed files with 30 additions and 4 deletions

View File

@ -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;

View File

@ -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) {

View File

@ -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