From 071bd7acb3185f4f1e807855605c5e6018e9742f Mon Sep 17 00:00:00 2001 From: Steve Howard Date: Thu, 15 Jul 2010 19:59:40 -0700 Subject: Support for max download size that may go over mobile This change introduces support for a maximum download size that may go over a mobile connection. Downloads above this limit will wait for a wifi connection. To accomplish this, I moved a lot of the logic for checking connectivity info into DownloadInfo itself. I then moved the code to call these checks from DownloadService, where it would call the checks before spawning a DownloadThread, into DownloadThread itself. This makes it simpler to check connectivity after we get Content-Length info. It also eliminates the risk of a race condition where connectivity changes between the check and the actual request execution. I realize this change reduces efficiency, because we now call into ConnectivityManager/TelephonyManager twice per DownloadThread, rather than once per DownloadService "tick". I feel that it's OK since its a small amount of computation running relatively infrequently. If we feel that it's a serious concern, and that the efficiency issues outweigh the race problem, I can go easily back to the old approach. I've left out the code to actually fetch the limit. I think this will come from system settings, but I want to double-check, so I'll put it in a separate change. Other changes: * simplify SystemFacade's interface to get connectivity info - rather than returning all connected types, just return the active type, since that should be all we care about * adding @LargeTest to PublicApiFunctionalTest Change-Id: Id1faa2c45bf2dade9fe779440721a1d42cbdfcd1 --- .../android/providers/downloads/FakeSystemFacade.java | 15 +++++++-------- .../providers/downloads/PublicApiFunctionalTest.java | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/tests/src/com/android/providers/downloads/FakeSystemFacade.java b/tests/src/com/android/providers/downloads/FakeSystemFacade.java index c48f815d..4ff313ab 100644 --- a/tests/src/com/android/providers/downloads/FakeSystemFacade.java +++ b/tests/src/com/android/providers/downloads/FakeSystemFacade.java @@ -2,12 +2,11 @@ package com.android.providers.downloads; import android.net.ConnectivityManager; -import java.util.BitSet; - public class FakeSystemFacade implements SystemFacade { long mTimeMillis = 0; Integer mActiveNetworkType = ConnectivityManager.TYPE_WIFI; boolean mIsRoaming = false; + Integer mMaxBytesOverMobile = null; void incrementTimeMillis(long delta) { mTimeMillis += delta; @@ -17,15 +16,15 @@ public class FakeSystemFacade implements SystemFacade { return mTimeMillis; } - public BitSet getConnectedNetworkTypes() { - BitSet connectedTypes = new BitSet(); - if (mActiveNetworkType != null) { - connectedTypes.set(mActiveNetworkType); - } - return connectedTypes; + public Integer getActiveNetworkType() { + return mActiveNetworkType; } public boolean isNetworkRoaming() { return mIsRoaming; } + + public Integer getMaxBytesOverMobile() { + return mMaxBytesOverMobile ; + } } diff --git a/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java index e9195609..a9810fc1 100644 --- a/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java +++ b/tests/src/com/android/providers/downloads/PublicApiFunctionalTest.java @@ -17,10 +17,11 @@ package com.android.providers.downloads; import android.database.Cursor; +import android.net.ConnectivityManager; import android.net.DownloadManager; import android.net.Uri; import android.os.Environment; -import android.util.Log; +import android.test.suitebuilder.annotation.LargeTest; import tests.http.RecordedRequest; import java.io.File; @@ -28,6 +29,7 @@ import java.io.FileInputStream; import java.io.InputStream; import java.net.MalformedURLException; +@LargeTest public class PublicApiFunctionalTest extends AbstractDownloadManagerFunctionalTest { private static final String REQUEST_PATH = "/path"; @@ -293,6 +295,21 @@ public class PublicApiFunctionalTest extends AbstractDownloadManagerFunctionalTe } } + public void testSizeLimitOverMobile() throws Exception { + mSystemFacade.mMaxBytesOverMobile = FILE_CONTENT.length() - 1; + + mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_MOBILE; + enqueueResponse(HTTP_OK, FILE_CONTENT); + Download download = enqueueRequest(getRequest()); + download.runUntilStatus(DownloadManager.STATUS_PAUSED); + + mSystemFacade.mActiveNetworkType = ConnectivityManager.TYPE_WIFI; + // first response was read, but aborted after the DL manager processed the Content-Length + // header, so we need to enqueue a second one + enqueueResponse(HTTP_OK, FILE_CONTENT); + download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL); + } + private DownloadManager.Request getRequest() throws MalformedURLException { return getRequest(getServerUri(REQUEST_PATH)); } -- cgit v1.2.3