From 2eb144d8effed2dbb067957c5b25e735233bca89 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Tue, 19 Feb 2013 12:48:08 -0800 Subject: Retries shouldn't backoff when network changes. When a download fails due to a network change, treat it as waiting for network, instead of subjecting it to full retry backoff. Change-Id: Ifdae62fd7c2baad7422f68e09da94740b5f513d0 --- .../android/providers/downloads/DownloadInfo.java | 1 - .../providers/downloads/DownloadThread.java | 39 ++++++++++++---------- src/com/android/providers/downloads/Helpers.java | 8 ----- 3 files changed, 22 insertions(+), 26 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/providers/downloads/DownloadInfo.java b/src/com/android/providers/downloads/DownloadInfo.java index 65242227..e6ed059b 100644 --- a/src/com/android/providers/downloads/DownloadInfo.java +++ b/src/com/android/providers/downloads/DownloadInfo.java @@ -358,7 +358,6 @@ public class DownloadInfo { /** * Returns whether this download is allowed to use the network. - * @return one of the NETWORK_* constants */ public NetworkState checkCanUseNetwork() { final NetworkInfo info = mSystemFacade.getActiveNetworkInfo(mUid); diff --git a/src/com/android/providers/downloads/DownloadThread.java b/src/com/android/providers/downloads/DownloadThread.java index d19f71be..95754a03 100644 --- a/src/com/android/providers/downloads/DownloadThread.java +++ b/src/com/android/providers/downloads/DownloadThread.java @@ -20,7 +20,6 @@ import static android.provider.Downloads.Impl.STATUS_BAD_REQUEST; import static android.provider.Downloads.Impl.STATUS_CANNOT_RESUME; import static android.provider.Downloads.Impl.STATUS_FILE_ERROR; import static android.provider.Downloads.Impl.STATUS_HTTP_DATA_ERROR; -import static android.provider.Downloads.Impl.STATUS_QUEUED_FOR_WIFI; import static android.provider.Downloads.Impl.STATUS_TOO_MANY_REDIRECTS; import static android.provider.Downloads.Impl.STATUS_WAITING_FOR_NETWORK; import static android.provider.Downloads.Impl.STATUS_WAITING_TO_RETRY; @@ -39,7 +38,9 @@ import android.content.Context; import android.content.Intent; import android.drm.DrmManagerClient; import android.drm.DrmOutputStream; +import android.net.ConnectivityManager; import android.net.INetworkPolicyListener; +import android.net.NetworkInfo; import android.net.NetworkPolicyManager; import android.net.TrafficStats; import android.os.FileUtils; @@ -73,6 +74,9 @@ import libcore.io.IoUtils; */ public class DownloadThread implements Runnable { + // TODO: bind each download to a specific network interface to avoid state + // checking races once we have ConnectivityManager API + private static final int HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416; private static final int HTTP_TEMP_REDIRECT = 307; @@ -121,6 +125,7 @@ public class DownloadThread implements Runnable { public boolean mContinuingDownload = false; public long mBytesNotified = 0; public long mTimeLastNotification = 0; + public int mNetworkType = ConnectivityManager.TYPE_NONE; /** Historical bytes/second speed of this download. */ public long mSpeed; @@ -190,6 +195,13 @@ public class DownloadThread implements Runnable { Log.i(Constants.TAG, "Initiating download " + mInfo.mId); + // Remember which network this download started on; used to + // determine if errors were due to network changes. + final NetworkInfo info = mSystemFacade.getActiveNetworkInfo(mInfo.mUid); + if (info != null) { + state.mNetworkType = info.getType(); + } + // Network traffic on this thread should be counted against the // requesting UID, and is tagged with well-known value. TrafficStats.setThreadStatsTag(TrafficStats.TAG_SYSTEM_DOWNLOAD); @@ -234,7 +246,15 @@ public class DownloadThread implements Runnable { } if (numFailed < Constants.MAX_RETRIES) { - finalStatus = getFinalRetryStatus(); + final NetworkInfo info = mSystemFacade.getActiveNetworkInfo(mInfo.mUid); + if (info != null && info.getType() == state.mNetworkType + && info.isConnected()) { + // Underlying network is still intact, use normal backoff + finalStatus = STATUS_WAITING_TO_RETRY; + } else { + // Network changed, retry on any next available + finalStatus = STATUS_WAITING_FOR_NETWORK; + } } } @@ -430,21 +450,6 @@ public class DownloadThread implements Runnable { } } - /** - * Return retry status appropriate for current network conditions. - */ - private int getFinalRetryStatus() { - switch (mInfo.checkCanUseNetwork()) { - case OK: - return STATUS_WAITING_TO_RETRY; - case UNUSABLE_DUE_TO_SIZE: - case RECOMMENDED_UNUSABLE_DUE_TO_SIZE: - return STATUS_QUEUED_FOR_WIFI; - default: - return STATUS_WAITING_FOR_NETWORK; - } - } - /** * Transfer as much data as possible from the HTTP response to the * destination file. diff --git a/src/com/android/providers/downloads/Helpers.java b/src/com/android/providers/downloads/Helpers.java index 5c34ebe1..593e28b8 100644 --- a/src/com/android/providers/downloads/Helpers.java +++ b/src/com/android/providers/downloads/Helpers.java @@ -328,14 +328,6 @@ public class Helpers { "failed to generate an unused filename on internal download storage"); } - /** - * Returns whether the network is available - */ - public static boolean isNetworkAvailable(SystemFacade system, int uid) { - final NetworkInfo info = system.getActiveNetworkInfo(uid); - return info != null && info.isConnected(); - } - /** * Checks whether the filename looks legitimate */ -- cgit v1.2.3