]> nv-tegra.nvidia Code Review - android/platform/packages/providers/DownloadProvider.git/blobdiff - src/com/android/providers/downloads/DownloadThread.java
Remove chmod(0644) for finished downloads.
[android/platform/packages/providers/DownloadProvider.git] / src / com / android / providers / downloads / DownloadThread.java
index e8252eef33f48ca93baf592bdf7d95a6a48be848..65142db6abd6e0dda1c5908d7073c7acc7722671 100644 (file)
 
 package com.android.providers.downloads;
 
-import org.apache.http.conn.params.ConnRouteParams;
+import static android.provider.Downloads.Impl.STATUS_BAD_REQUEST;
+import static android.provider.Downloads.Impl.STATUS_CANCELED;
+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_SUCCESS;
+import static android.provider.Downloads.Impl.STATUS_TOO_MANY_REDIRECTS;
+import static android.provider.Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE;
+import static android.provider.Downloads.Impl.STATUS_UNKNOWN_ERROR;
+import static android.provider.Downloads.Impl.STATUS_WAITING_FOR_NETWORK;
+import static android.provider.Downloads.Impl.STATUS_WAITING_TO_RETRY;
+import static android.text.format.DateUtils.SECOND_IN_MILLIS;
+import static com.android.providers.downloads.Constants.TAG;
+import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
+import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
+import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
+import static java.net.HttpURLConnection.HTTP_OK;
+import static java.net.HttpURLConnection.HTTP_PARTIAL;
+import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;
+import static java.net.HttpURLConnection.HTTP_SEE_OTHER;
+import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;
 
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.Intent;
-import android.drm.mobile1.DrmRawContent;
-import android.net.http.AndroidHttpClient;
-import android.net.Proxy;
+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.net.Uri;
-import android.os.FileUtils;
+import android.os.ParcelFileDescriptor;
 import android.os.PowerManager;
 import android.os.Process;
+import android.os.SystemClock;
+import android.os.WorkSource;
 import android.provider.Downloads;
-import android.provider.DrmStore;
-import android.util.Config;
+import android.system.ErrnoException;
+import android.system.Os;
+import android.system.OsConstants;
 import android.util.Log;
+import android.util.Pair;
 
-import org.apache.http.Header;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
+import com.android.providers.downloads.DownloadInfo.NetworkState;
+
+import libcore.io.IoUtils;
 
 import java.io.File;
+import java.io.FileDescriptor;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.SyncFailedException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.Locale;
-import java.util.Map;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
+import java.net.URL;
+import java.net.URLConnection;
 
 /**
- * Runs an actual download
+ * Task which executes a given {@link DownloadInfo}: making network requests,
+ * persisting data to disk, and updating {@link DownloadProvider}.
+ * <p>
+ * To know if a download is successful, we need to know either the final content
+ * length to expect, or the transfer to be chunked. To resume an interrupted
+ * download, we need an ETag.
+ * <p>
+ * Failed network requests are retried several times before giving up. Local
+ * disk errors fail immediately and are not retried.
  */
-public class DownloadThread extends Thread {
+public class DownloadThread implements Runnable {
 
-    private Context mContext;
-    private DownloadInfo mInfo;
-    private SystemFacade mSystemFacade;
+    // TODO: bind each download to a specific network interface to avoid state
+    // checking races once we have ConnectivityManager API
 
-    public DownloadThread(Context context, SystemFacade systemFacade, DownloadInfo info) {
-        mContext = context;
-        mSystemFacade = systemFacade;
-        mInfo = info;
-    }
+    // TODO: add support for saving to content://
+
+    private static final int HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
+    private static final int HTTP_TEMP_REDIRECT = 307;
+
+    private static final int DEFAULT_TIMEOUT = (int) (20 * SECOND_IN_MILLIS);
+
+    private final Context mContext;
+    private final SystemFacade mSystemFacade;
+    private final DownloadNotifier mNotifier;
+
+    private final long mId;
 
     /**
-     * Returns the user agent provided by the initiating app, or use the default one
+     * Info object that should be treated as read-only. Any potentially mutated
+     * fields are tracked in {@link #mInfoDelta}. If a field exists in
+     * {@link #mInfoDelta}, it must not be read from {@link #mInfo}.
      */
-    private String userAgent() {
-        String userAgent = mInfo.mUserAgent;
-        if (userAgent != null) {
-        }
-        if (userAgent == null) {
-            userAgent = Constants.DEFAULT_USER_AGENT;
-        }
-        return userAgent;
-    }
+    private final DownloadInfo mInfo;
+    private final DownloadInfoDelta mInfoDelta;
+
+    private volatile boolean mPolicyDirty;
 
     /**
-     * State for the entire run() method.
+     * Local changes to {@link DownloadInfo}. These are kept local to avoid
+     * racing with the thread that updates based on change notifications.
      */
-    private static class State {
-        public String mFilename;
-        public FileOutputStream mStream;
+    private class DownloadInfoDelta {
+        public String mUri;
+        public String mFileName;
         public String mMimeType;
-        public boolean mCountRetry = false;
-        public int mRetryAfter = 0;
-        public int mRedirectCount = 0;
-        public String mNewUri;
-        public boolean mGotData = false;
-        public String mRequestUri;
-
-        public State(DownloadInfo info) {
-            mMimeType = sanitizeMimeType(info.mMimeType);
-            mRedirectCount = info.mRedirectCount;
-            mRequestUri = info.mUri;
-            mFilename = info.mFileName;
+        public int mStatus;
+        public int mNumFailed;
+        public int mRetryAfter;
+        public long mTotalBytes;
+        public long mCurrentBytes;
+        public String mETag;
+
+        public String mErrorMsg;
+
+        public DownloadInfoDelta(DownloadInfo info) {
+            mUri = info.mUri;
+            mFileName = info.mFileName;
+            mMimeType = info.mMimeType;
+            mStatus = info.mStatus;
+            mNumFailed = info.mNumFailed;
+            mRetryAfter = info.mRetryAfter;
+            mTotalBytes = info.mTotalBytes;
+            mCurrentBytes = info.mCurrentBytes;
+            mETag = info.mETag;
+        }
+
+        private ContentValues buildContentValues() {
+            final ContentValues values = new ContentValues();
+
+            values.put(Downloads.Impl.COLUMN_URI, mUri);
+            values.put(Downloads.Impl._DATA, mFileName);
+            values.put(Downloads.Impl.COLUMN_MIME_TYPE, mMimeType);
+            values.put(Downloads.Impl.COLUMN_STATUS, mStatus);
+            values.put(Downloads.Impl.COLUMN_FAILED_CONNECTIONS, mNumFailed);
+            values.put(Constants.RETRY_AFTER_X_REDIRECT_COUNT, mRetryAfter);
+            values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, mTotalBytes);
+            values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, mCurrentBytes);
+            values.put(Constants.ETAG, mETag);
+
+            values.put(Downloads.Impl.COLUMN_LAST_MODIFICATION, mSystemFacade.currentTimeMillis());
+            values.put(Downloads.Impl.COLUMN_ERROR_MSG, mErrorMsg);
+
+            return values;
+        }
+
+        /**
+         * Blindly push update of current delta values to provider.
+         */
+        public void writeToDatabase() {
+            mContext.getContentResolver().update(mInfo.getAllDownloadsUri(), buildContentValues(),
+                    null, null);
+        }
+
+        /**
+         * Push update of current delta values to provider, asserting strongly
+         * that we haven't been paused or deleted.
+         */
+        public void writeToDatabaseOrThrow() throws StopRequestException {
+            if (mContext.getContentResolver().update(mInfo.getAllDownloadsUri(),
+                    buildContentValues(), Downloads.Impl.COLUMN_DELETED + " == '0'", null) == 0) {
+                throw new StopRequestException(STATUS_CANCELED, "Download deleted or missing!");
+            }
         }
     }
 
     /**
-     * State within executeDownload()
+     * Flag indicating if we've made forward progress transferring file data
+     * from a remote server.
      */
-    private static class InnerState {
-        public int mBytesSoFar = 0;
-        public String mHeaderETag;
-        public boolean mContinuingDownload = false;
-        public String mHeaderContentLength;
-        public String mHeaderContentDisposition;
-        public String mHeaderContentLocation;
-        public int mBytesNotified = 0;
-        public long mTimeLastNotification = 0;
-    }
+    private boolean mMadeProgress = false;
 
     /**
-     * Raised from methods called by run() to indicate that the current request should be stopped
-     * immediately.
+     * Details from the last time we pushed a database update.
      */
-    private class StopRequest extends Throwable {
-        public int mFinalStatus;
+    private long mLastUpdateBytes = 0;
+    private long mLastUpdateTime = 0;
 
-        public StopRequest(int finalStatus) {
-            mFinalStatus = finalStatus;
-        }
+    private int mNetworkType = ConnectivityManager.TYPE_NONE;
 
-        public StopRequest(int finalStatus, Throwable throwable) {
-            super(throwable);
-            mFinalStatus = finalStatus;
-        }
-    }
+    /** Historical bytes/second speed of this download. */
+    private long mSpeed;
+    /** Time when current sample started. */
+    private long mSpeedSampleStart;
+    /** Bytes transferred since current sample started. */
+    private long mSpeedSampleBytes;
 
-    /**
-     * Raised from methods called by executeDownload() to indicate that the download should be
-     * retried immediately.
-     */
-    private class RetryDownload extends Throwable {}
+    public DownloadThread(Context context, SystemFacade systemFacade, DownloadNotifier notifier,
+            DownloadInfo info) {
+        mContext = context;
+        mSystemFacade = systemFacade;
+        mNotifier = notifier;
 
-    /**
-     * Executes the download in a separate thread
-     */
+        mId = info.mId;
+        mInfo = info;
+        mInfoDelta = new DownloadInfoDelta(info);
+    }
+
+    @Override
     public void run() {
         Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
 
-        State state = new State(mInfo);
-        AndroidHttpClient client = null;
+        // Skip when download already marked as finished; this download was
+        // probably started again while racing with UpdateThread.
+        if (DownloadInfo.queryDownloadStatus(mContext.getContentResolver(), mId)
+                == Downloads.Impl.STATUS_SUCCESS) {
+            logDebug("Already finished; skipping");
+            return;
+        }
+
+        final NetworkPolicyManager netPolicy = NetworkPolicyManager.from(mContext);
         PowerManager.WakeLock wakeLock = null;
-        int finalStatus = Downloads.Impl.STATUS_UNKNOWN_ERROR;
-        mInfo.mPausedReason = null;
+        final PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
 
         try {
-            PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
             wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
+            wakeLock.setWorkSource(new WorkSource(mInfo.mUid));
             wakeLock.acquire();
 
+            // while performing download, register for rules updates
+            netPolicy.registerListener(mPolicyListener);
+
+            logDebug("Starting");
 
-            if (Constants.LOGV) {
-                Log.v(Constants.TAG, "initiating download for " + mInfo.mUri);
+            // 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) {
+                mNetworkType = info.getType();
             }
 
-            client = AndroidHttpClient.newInstance(userAgent(), mContext);
+            // 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);
+            TrafficStats.setThreadStatsUid(mInfo.mUid);
 
-            boolean finished = false;
-            while(!finished) {
-                // Set or unset proxy, which may have changed since last GET request.
-                // setDefaultProxy() supports null as proxy parameter.
-                ConnRouteParams.setDefaultProxy(client.getParams(),
-                        Proxy.getPreferredHttpHost(mContext, state.mRequestUri));
-                HttpGet request = new HttpGet(state.mRequestUri);
-                try {
-                    executeDownload(state, client, request);
-                    finished = true;
-                } catch (RetryDownload exc) {
-                    // fall through
-                } finally {
-                    request.abort();
-                    request = null;
-                }
-            }
+            executeDownload();
 
-            if (Constants.LOGV) {
-                Log.v(Constants.TAG, "download completed for " + mInfo.mUri);
+            mInfoDelta.mStatus = STATUS_SUCCESS;
+            TrafficStats.incrementOperationCount(1);
+
+            // If we just finished a chunked file, record total size
+            if (mInfoDelta.mTotalBytes == -1) {
+                mInfoDelta.mTotalBytes = mInfoDelta.mCurrentBytes;
             }
-            finalizeDestinationFile(state);
-            finalStatus = Downloads.Impl.STATUS_SUCCESS;
-        } catch (StopRequest error) {
-            if (Constants.LOGV) {
-                Log.v(Constants.TAG, "Aborting request for " + mInfo.mUri, error);
+
+        } catch (StopRequestException e) {
+            mInfoDelta.mStatus = e.getFinalStatus();
+            mInfoDelta.mErrorMsg = e.getMessage();
+
+            logWarning("Stop requested with status "
+                    + Downloads.Impl.statusToString(mInfoDelta.mStatus) + ": "
+                    + mInfoDelta.mErrorMsg);
+
+            // Nobody below our level should request retries, since we handle
+            // failure counts at this level.
+            if (mInfoDelta.mStatus == STATUS_WAITING_TO_RETRY) {
+                throw new IllegalStateException("Execution should always throw final error codes");
             }
-            finalStatus = error.mFinalStatus;
-            // fall through to finally block
-        } catch (FileNotFoundException ex) {
-            Log.d(Constants.TAG, "FileNotFoundException for " + state.mFilename + " : " +  ex);
-            finalStatus = Downloads.Impl.STATUS_FILE_ERROR;
-            // falls through to the code that reports an error
-        } catch (RuntimeException ex) { //sometimes the socket code throws unchecked exceptions
-            if (Constants.LOGV) {
-                Log.d(Constants.TAG, "Exception for " + mInfo.mUri, ex);
-            } else if (Config.LOGD) {
-                Log.d(Constants.TAG, "Exception for id " + mInfo.mId, ex);
+
+            // Some errors should be retryable, unless we fail too many times.
+            if (isStatusRetryable(mInfoDelta.mStatus)) {
+                if (mMadeProgress) {
+                    mInfoDelta.mNumFailed = 1;
+                } else {
+                    mInfoDelta.mNumFailed += 1;
+                }
+
+                if (mInfoDelta.mNumFailed < Constants.MAX_RETRIES) {
+                    final NetworkInfo info = mSystemFacade.getActiveNetworkInfo(mInfo.mUid);
+                    if (info != null && info.getType() == mNetworkType && info.isConnected()) {
+                        // Underlying network is still intact, use normal backoff
+                        mInfoDelta.mStatus = STATUS_WAITING_TO_RETRY;
+                    } else {
+                        // Network changed, retry on any next available
+                        mInfoDelta.mStatus = STATUS_WAITING_FOR_NETWORK;
+                    }
+
+                    if ((mInfoDelta.mETag == null && mMadeProgress)
+                            || DownloadDrmHelper.isDrmConvertNeeded(mInfoDelta.mMimeType)) {
+                        // However, if we wrote data and have no ETag to verify
+                        // contents against later, we can't actually resume.
+                        mInfoDelta.mStatus = STATUS_CANNOT_RESUME;
+                    }
+                }
             }
-            finalStatus = Downloads.Impl.STATUS_UNKNOWN_ERROR;
-            // falls through to the code that reports an error
+
+        } catch (Throwable t) {
+            mInfoDelta.mStatus = STATUS_UNKNOWN_ERROR;
+            mInfoDelta.mErrorMsg = t.toString();
+
+            logError("Failed: " + mInfoDelta.mErrorMsg, t);
+
         } finally {
-            mInfo.mHasActiveThread = false;
+            logDebug("Finished with status " + Downloads.Impl.statusToString(mInfoDelta.mStatus));
+
+            mNotifier.notifyDownloadSpeed(mId, 0);
+
+            finalizeDestination();
+
+            mInfoDelta.writeToDatabase();
+
+            if (Downloads.Impl.isStatusCompleted(mInfoDelta.mStatus)) {
+                mInfo.sendIntentIfRequested();
+            }
+
+            TrafficStats.clearThreadStatsTag();
+            TrafficStats.clearThreadStatsUid();
+
+            netPolicy.unregisterListener(mPolicyListener);
+
             if (wakeLock != null) {
                 wakeLock.release();
                 wakeLock = null;
             }
-            if (client != null) {
-                client.close();
-                client = null;
-            }
-            cleanupDestination(state, finalStatus);
-            notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter,
-                                    state.mRedirectCount, state.mGotData, state.mFilename,
-                                    state.mNewUri, state.mMimeType);
         }
     }
 
     /**
-     * Fully execute a single download request - setup and send the request, handle the response,
-     * and transfer the data to the destination file.
+     * Fully execute a single download request. Setup and send the request,
+     * handle the response, and transfer the data to the destination file.
      */
-    private void executeDownload(State state, AndroidHttpClient client, HttpGet request)
-            throws StopRequest, RetryDownload, FileNotFoundException {
-        InnerState innerState = new InnerState();
-        byte data[] = new byte[Constants.BUFFER_SIZE];
+    private void executeDownload() throws StopRequestException {
+        final boolean resuming = mInfoDelta.mCurrentBytes != 0;
 
-        setupDestinationFile(state, innerState);
-        addRequestHeaders(innerState, request);
+        URL url;
+        try {
+            // TODO: migrate URL sanity checking into client side of API
+            url = new URL(mInfoDelta.mUri);
+        } catch (MalformedURLException e) {
+            throw new StopRequestException(STATUS_BAD_REQUEST, e);
+        }
+
+        boolean cleartextTrafficPermitted = mSystemFacade.isCleartextTrafficPermitted(mInfo.mUid);
+        int redirectionCount = 0;
+        while (redirectionCount++ < Constants.MAX_REDIRECTS) {
+            // Enforce the cleartext traffic opt-out for the UID. This cannot be enforced earlier
+            // because of HTTP redirects which can change the protocol between HTTP and HTTPS.
+            if ((!cleartextTrafficPermitted) && ("http".equalsIgnoreCase(url.getProtocol()))) {
+                throw new StopRequestException(STATUS_BAD_REQUEST,
+                        "Cleartext traffic not permitted for UID " + mInfo.mUid + ": "
+                        + Uri.parse(url.toString()).toSafeString());
+            }
 
-        // check just before sending the request to avoid using an invalid connection at all
-        checkConnectivity(state);
+            // Open connection and follow any redirects until we have a useful
+            // response with body.
+            HttpURLConnection conn = null;
+            try {
+                checkConnectivity();
+                conn = (HttpURLConnection) url.openConnection();
+                conn.setInstanceFollowRedirects(false);
+                conn.setConnectTimeout(DEFAULT_TIMEOUT);
+                conn.setReadTimeout(DEFAULT_TIMEOUT);
+
+                addRequestHeaders(conn, resuming);
+
+                final int responseCode = conn.getResponseCode();
+                switch (responseCode) {
+                    case HTTP_OK:
+                        if (resuming) {
+                            throw new StopRequestException(
+                                    STATUS_CANNOT_RESUME, "Expected partial, but received OK");
+                        }
+                        parseOkHeaders(conn);
+                        transferData(conn);
+                        return;
+
+                    case HTTP_PARTIAL:
+                        if (!resuming) {
+                            throw new StopRequestException(
+                                    STATUS_CANNOT_RESUME, "Expected OK, but received partial");
+                        }
+                        transferData(conn);
+                        return;
+
+                    case HTTP_MOVED_PERM:
+                    case HTTP_MOVED_TEMP:
+                    case HTTP_SEE_OTHER:
+                    case HTTP_TEMP_REDIRECT:
+                        final String location = conn.getHeaderField("Location");
+                        url = new URL(url, location);
+                        if (responseCode == HTTP_MOVED_PERM) {
+                            // Push updated URL back to database
+                            mInfoDelta.mUri = url.toString();
+                        }
+                        continue;
 
-        HttpResponse response = sendRequest(state, client, request);
-        handleExceptionalStatus(state, innerState, response);
+                    case HTTP_PRECON_FAILED:
+                        throw new StopRequestException(
+                                STATUS_CANNOT_RESUME, "Precondition failed");
 
-        if (Constants.LOGV) {
-            Log.v(Constants.TAG, "received response for " + mInfo.mUri);
-        }
+                    case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
+                        throw new StopRequestException(
+                                STATUS_CANNOT_RESUME, "Requested range not satisfiable");
 
-        processResponseHeaders(state, innerState, response);
-        InputStream entityStream = openResponseEntity(state, response);
-        transferData(state, innerState, data, entityStream);
-    }
+                    case HTTP_UNAVAILABLE:
+                        parseUnavailableHeaders(conn);
+                        throw new StopRequestException(
+                                HTTP_UNAVAILABLE, conn.getResponseMessage());
 
-    /**
-     * Check if current connectivity is valid for this request.
-     */
-    private void checkConnectivity(State state) throws StopRequest {
-        int networkUsable = mInfo.checkCanUseNetwork();
-        if (networkUsable != DownloadInfo.NETWORK_OK) {
-            if (networkUsable == DownloadInfo.NETWORK_UNUSABLE_DUE_TO_SIZE) {
-                mInfo.notifyPauseDueToSize(true);
-            } else if (networkUsable == DownloadInfo.NETWORK_RECOMMENDED_UNUSABLE_DUE_TO_SIZE) {
-                mInfo.notifyPauseDueToSize(false);
-            }
-            throw new StopRequest(Downloads.Impl.STATUS_RUNNING_PAUSED);
-        }
-    }
+                    case HTTP_INTERNAL_ERROR:
+                        throw new StopRequestException(
+                                HTTP_INTERNAL_ERROR, conn.getResponseMessage());
 
-    /**
-     * Transfer as much data as possible from the HTTP response to the destination file.
-     * @param data buffer to use to read data
-     * @param entityStream stream for reading the HTTP response entity
-     */
-    private void transferData(State state, InnerState innerState, byte[] data,
-                                 InputStream entityStream) throws StopRequest {
-        for (;;) {
-            int bytesRead = readFromResponse(state, innerState, data, entityStream);
-            if (bytesRead == -1) { // success, end of stream already reached
-                handleEndOfStream(state, innerState);
-                return;
-            }
+                    default:
+                        StopRequestException.throwUnhandledHttpError(
+                                responseCode, conn.getResponseMessage());
+                }
 
-            state.mGotData = true;
-            writeDataToDestination(state, data, bytesRead);
-            innerState.mBytesSoFar += bytesRead;
-            reportProgress(state, innerState);
+            } catch (IOException e) {
+                if (e instanceof ProtocolException
+                        && e.getMessage().startsWith("Unexpected status line")) {
+                    throw new StopRequestException(STATUS_UNHANDLED_HTTP_CODE, e);
+                } else {
+                    // Trouble with low-level sockets
+                    throw new StopRequestException(STATUS_HTTP_DATA_ERROR, e);
+                }
 
-            if (Constants.LOGVV) {
-                Log.v(Constants.TAG, "downloaded " + innerState.mBytesSoFar + " for "
-                      + mInfo.mUri);
+            } finally {
+                if (conn != null) conn.disconnect();
             }
-
-            checkPausedOrCanceled(state);
         }
-    }
 
-    /**
-     * Called after a successful completion to take any necessary action on the downloaded file.
-     */
-    private void finalizeDestinationFile(State state) throws StopRequest {
-        if (isDrmFile(state)) {
-            transferToDrm(state);
-        } else {
-            // make sure the file is readable
-            FileUtils.setPermissions(state.mFilename, 0644, -1, -1);
-            syncDestination(state);
-        }
+        throw new StopRequestException(STATUS_TOO_MANY_REDIRECTS, "Too many redirects");
     }
 
     /**
-     * Called just before the thread finishes, regardless of status, to take any necessary action on
-     * the downloaded file.
+     * Transfer data from the given connection to the destination file.
      */
-    private void cleanupDestination(State state, int finalStatus) {
-        closeDestination(state);
-        if (state.mFilename != null && Downloads.Impl.isStatusError(finalStatus)) {
-            new File(state.mFilename).delete();
-            state.mFilename = null;
+    private void transferData(HttpURLConnection conn) throws StopRequestException {
+
+        // To detect when we're really finished, we either need a length, closed
+        // connection, or chunked encoding.
+        final boolean hasLength = mInfoDelta.mTotalBytes != -1;
+        final boolean isConnectionClose = "close".equalsIgnoreCase(
+                conn.getHeaderField("Connection"));
+        final boolean isEncodingChunked = "chunked".equalsIgnoreCase(
+                conn.getHeaderField("Transfer-Encoding"));
+
+        final boolean finishKnown = hasLength || isConnectionClose || isEncodingChunked;
+        if (!finishKnown) {
+            throw new StopRequestException(
+                    STATUS_CANNOT_RESUME, "can't know size of download, giving up");
         }
-    }
 
-    /**
-     * Sync the destination file to storage.
-     */
-    private void syncDestination(State state) {
-        FileOutputStream downloadedFileStream = null;
+        DrmManagerClient drmClient = null;
+        ParcelFileDescriptor outPfd = null;
+        FileDescriptor outFd = null;
+        InputStream in = null;
+        OutputStream out = null;
         try {
-            downloadedFileStream = new FileOutputStream(state.mFilename, true);
-            downloadedFileStream.getFD().sync();
-        } catch (FileNotFoundException ex) {
-            Log.w(Constants.TAG, "file " + state.mFilename + " not found: " + ex);
-        } catch (SyncFailedException ex) {
-            Log.w(Constants.TAG, "file " + state.mFilename + " sync failed: " + ex);
-        } catch (IOException ex) {
-            Log.w(Constants.TAG, "IOException trying to sync " + state.mFilename + ": " + ex);
-        } catch (RuntimeException ex) {
-            Log.w(Constants.TAG, "exception while syncing file: ", ex);
-        } finally {
-            if(downloadedFileStream != null) {
-                try {
-                    downloadedFileStream.close();
-                } catch (IOException ex) {
-                    Log.w(Constants.TAG, "IOException while closing synced file: ", ex);
-                } catch (RuntimeException ex) {
-                    Log.w(Constants.TAG, "exception while closing file: ", ex);
+            try {
+                in = conn.getInputStream();
+            } catch (IOException e) {
+                throw new StopRequestException(STATUS_HTTP_DATA_ERROR, e);
+            }
+
+            try {
+                outPfd = mContext.getContentResolver()
+                        .openFileDescriptor(mInfo.getAllDownloadsUri(), "rw");
+                outFd = outPfd.getFileDescriptor();
+
+                if (DownloadDrmHelper.isDrmConvertNeeded(mInfoDelta.mMimeType)) {
+                    drmClient = new DrmManagerClient(mContext);
+                    out = new DrmOutputStream(drmClient, outPfd, mInfoDelta.mMimeType);
+                } else {
+                    out = new ParcelFileDescriptor.AutoCloseOutputStream(outPfd);
                 }
+
+                // Pre-flight disk space requirements, when known
+                if (mInfoDelta.mTotalBytes > 0) {
+                    final long curSize = Os.fstat(outFd).st_size;
+                    final long newBytes = mInfoDelta.mTotalBytes - curSize;
+
+                    StorageUtils.ensureAvailableSpace(mContext, outFd, newBytes);
+
+                    try {
+                        // We found enough space, so claim it for ourselves
+                        Os.posix_fallocate(outFd, 0, mInfoDelta.mTotalBytes);
+                    } catch (ErrnoException e) {
+                        if (e.errno == OsConstants.ENOSYS || e.errno == OsConstants.ENOTSUP) {
+                            Log.w(TAG, "fallocate() not supported; falling back to ftruncate()");
+                            Os.ftruncate(outFd, mInfoDelta.mTotalBytes);
+                        } else {
+                            throw e;
+                        }
+                    }
+                }
+
+                // Move into place to begin writing
+                Os.lseek(outFd, mInfoDelta.mCurrentBytes, OsConstants.SEEK_SET);
+
+            } catch (ErrnoException e) {
+                throw new StopRequestException(STATUS_FILE_ERROR, e);
+            } catch (IOException e) {
+                throw new StopRequestException(STATUS_FILE_ERROR, e);
             }
-        }
-    }
 
-    /**
-     * @return true if the current download is a DRM file
-     */
-    private boolean isDrmFile(State state) {
-        return DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(state.mMimeType);
-    }
+            // Start streaming data, periodically watch for pause/cancel
+            // commands and checking disk space as needed.
+            transferData(in, out, outFd);
 
-    /**
-     * Transfer the downloaded destination file to the DRM store.
-     */
-    private void transferToDrm(State state) throws StopRequest {
-        File file = new File(state.mFilename);
-        Intent item = DrmStore.addDrmFile(mContext.getContentResolver(), file, null);
-        file.delete();
-
-        if (item == null) {
-            Log.w(Constants.TAG, "unable to add file " + state.mFilename + " to DrmProvider");
-            throw new StopRequest(Downloads.Impl.STATUS_UNKNOWN_ERROR);
-        } else {
-            state.mFilename = item.getDataString();
-            state.mMimeType = item.getType();
-        }
-    }
+            try {
+                if (out instanceof DrmOutputStream) {
+                    ((DrmOutputStream) out).finish();
+                }
+            } catch (IOException e) {
+                throw new StopRequestException(STATUS_FILE_ERROR, e);
+            }
 
-    /**
-     * Close the destination output stream.
-     */
-    private void closeDestination(State state) {
-        try {
-            // close the file
-            if (state.mStream != null) {
-                state.mStream.close();
-                state.mStream = null;
+        } finally {
+            if (drmClient != null) {
+                drmClient.release();
             }
-        } catch (IOException ex) {
-            if (Constants.LOGV) {
-                Log.v(Constants.TAG, "exception when closing the file after download : " + ex);
+
+            IoUtils.closeQuietly(in);
+
+            try {
+                if (out != null) out.flush();
+                if (outFd != null) outFd.sync();
+            } catch (IOException e) {
+            } finally {
+                IoUtils.closeQuietly(out);
             }
-            // nothing can really be done if the file can't be closed
         }
     }
 
     /**
-     * Check if the download has been paused or canceled, stopping the request appropriately if it
-     * has been.
+     * Transfer as much data as possible from the HTTP response to the
+     * destination file.
      */
-    private void checkPausedOrCanceled(State state) throws StopRequest {
-        synchronized (mInfo) {
-            if (mInfo.mControl == Downloads.Impl.CONTROL_PAUSED) {
-                if (Constants.LOGV) {
-                    Log.v(Constants.TAG, "paused " + mInfo.mUri);
-                }
-                throw new StopRequest(Downloads.Impl.STATUS_RUNNING_PAUSED);
+    private void transferData(InputStream in, OutputStream out, FileDescriptor outFd)
+            throws StopRequestException {
+        final byte buffer[] = new byte[Constants.BUFFER_SIZE];
+        while (true) {
+            checkPausedOrCanceled();
+
+            int len = -1;
+            try {
+                len = in.read(buffer);
+            } catch (IOException e) {
+                throw new StopRequestException(
+                        STATUS_HTTP_DATA_ERROR, "Failed reading response: " + e, e);
             }
-        }
-        if (mInfo.mStatus == Downloads.Impl.STATUS_CANCELED) {
-            if (Constants.LOGV) {
-                Log.d(Constants.TAG, "canceled " + mInfo.mUri);
+
+            if (len == -1) {
+                break;
+            }
+
+            try {
+                // When streaming, ensure space before each write
+                if (mInfoDelta.mTotalBytes == -1) {
+                    final long curSize = Os.fstat(outFd).st_size;
+                    final long newBytes = (mInfoDelta.mCurrentBytes + len) - curSize;
+
+                    StorageUtils.ensureAvailableSpace(mContext, outFd, newBytes);
+                }
+
+                out.write(buffer, 0, len);
+
+                mMadeProgress = true;
+                mInfoDelta.mCurrentBytes += len;
+
+                updateProgress(outFd);
+
+            } catch (ErrnoException e) {
+                throw new StopRequestException(STATUS_FILE_ERROR, e);
+            } catch (IOException e) {
+                throw new StopRequestException(STATUS_FILE_ERROR, e);
             }
-            throw new StopRequest(Downloads.Impl.STATUS_CANCELED);
         }
-    }
 
-    /**
-     * Report download progress through the database if necessary.
-     */
-    private void reportProgress(State state, InnerState innerState) {
-        long now = mSystemFacade.currentTimeMillis();
-        if (innerState.mBytesSoFar - innerState.mBytesNotified
-                        > Constants.MIN_PROGRESS_STEP
-                && now - innerState.mTimeLastNotification
-                        > Constants.MIN_PROGRESS_TIME) {
-            ContentValues values = new ContentValues();
-            values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, innerState.mBytesSoFar);
-            mContext.getContentResolver().update(mInfo.getAllDownloadsUri(), values, null, null);
-            innerState.mBytesNotified = innerState.mBytesSoFar;
-            innerState.mTimeLastNotification = now;
+        // Finished without error; verify length if known
+        if (mInfoDelta.mTotalBytes != -1 && mInfoDelta.mCurrentBytes != mInfoDelta.mTotalBytes) {
+            throw new StopRequestException(STATUS_HTTP_DATA_ERROR, "Content length mismatch");
         }
     }
 
     /**
-     * Write a data buffer to the destination file.
-     * @param data buffer containing the data to write
-     * @param bytesRead how many bytes to write from the buffer
+     * Called just before the thread finishes, regardless of status, to take any
+     * necessary action on the downloaded file.
      */
-    private void writeDataToDestination(State state, byte[] data, int bytesRead)
-            throws StopRequest {
-        for (;;) {
+    private void finalizeDestination() {
+        if (Downloads.Impl.isStatusError(mInfoDelta.mStatus)) {
+            // When error, free up any disk space
             try {
-                if (state.mStream == null) {
-                    state.mStream = new FileOutputStream(state.mFilename, true);
-                }
-                state.mStream.write(data, 0, bytesRead);
-                if (mInfo.mDestination == Downloads.Impl.DESTINATION_EXTERNAL
-                            && !isDrmFile(state)) {
-                    closeDestination(state);
-                }
-                return;
-            } catch (IOException ex) {
-                if (mInfo.isOnCache()) {
-                    if (Helpers.discardPurgeableFiles(mContext, Constants.BUFFER_SIZE)) {
-                        continue;
-                    }
-                } else if (!Helpers.isExternalMediaMounted()) {
-                    throw new StopRequest(Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR);
+                final ParcelFileDescriptor target = mContext.getContentResolver()
+                        .openFileDescriptor(mInfo.getAllDownloadsUri(), "rw");
+                try {
+                    Os.ftruncate(target.getFileDescriptor(), 0);
+                } catch (ErrnoException ignored) {
+                } finally {
+                    IoUtils.closeQuietly(target);
                 }
+            } catch (FileNotFoundException ignored) {
+            }
 
-                long availableBytes =
-                    Helpers.getAvailableBytes(Helpers.getFilesystemRoot(state.mFilename));
-                if (availableBytes < bytesRead) {
-                    throw new StopRequest(Downloads.Impl.STATUS_INSUFFICIENT_SPACE_ERROR, ex);
-                }
-                throw new StopRequest(Downloads.Impl.STATUS_FILE_ERROR, ex);
+            // Delete if local file
+            if (mInfoDelta.mFileName != null) {
+                new File(mInfoDelta.mFileName).delete();
+                mInfoDelta.mFileName = null;
             }
-        }
-    }
 
-    /**
-     * Called when we've reached the end of the HTTP response stream, to update the database and
-     * check for consistency.
-     */
-    private void handleEndOfStream(State state, InnerState innerState) throws StopRequest {
-        ContentValues values = new ContentValues();
-        values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, innerState.mBytesSoFar);
-        if (innerState.mHeaderContentLength == null) {
-            values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, innerState.mBytesSoFar);
-        }
-        mContext.getContentResolver().update(mInfo.getAllDownloadsUri(), values, null, null);
-
-        boolean lengthMismatched = (innerState.mHeaderContentLength != null)
-                && (innerState.mBytesSoFar != Integer.parseInt(innerState.mHeaderContentLength));
-        if (lengthMismatched) {
-            if (cannotResume(innerState)) {
-                if (Constants.LOGV) {
-                    Log.d(Constants.TAG, "mismatched content length " +
-                            mInfo.mUri);
-                } else if (Config.LOGD) {
-                    Log.d(Constants.TAG, "mismatched content length for " +
-                            mInfo.mId);
+        } else if (Downloads.Impl.isStatusSuccess(mInfoDelta.mStatus)) {
+            // When success, open access if local file
+            if (mInfoDelta.mFileName != null) {
+                if (mInfo.mDestination != Downloads.Impl.DESTINATION_FILE_URI) {
+                    try {
+                        // Move into final resting place, if needed
+                        final File before = new File(mInfoDelta.mFileName);
+                        final File beforeDir = Helpers.getRunningDestinationDirectory(
+                                mContext, mInfo.mDestination);
+                        final File afterDir = Helpers.getSuccessDestinationDirectory(
+                                mContext, mInfo.mDestination);
+                        if (!beforeDir.equals(afterDir)
+                                && before.getParentFile().equals(beforeDir)) {
+                            final File after = new File(afterDir, before.getName());
+                            if (before.renameTo(after)) {
+                                mInfoDelta.mFileName = after.getAbsolutePath();
+                            }
+                        }
+                    } catch (IOException ignored) {
+                    }
                 }
-                throw new StopRequest(Downloads.Impl.STATUS_CANNOT_RESUME);
-            } else {
-                throw new StopRequest(handleHttpError(state, "closed socket"));
             }
         }
     }
 
-    private boolean cannotResume(InnerState innerState) {
-        return innerState.mBytesSoFar > 0 && !mInfo.mNoIntegrity && innerState.mHeaderETag == null;
-    }
-
     /**
-     * Read some data from the HTTP response stream, handling I/O errors.
-     * @param data buffer to use to read data
-     * @param entityStream stream for reading the HTTP response entity
-     * @return the number of bytes actually read or -1 if the end of the stream has been reached
+     * Check if current connectivity is valid for this request.
      */
-    private int readFromResponse(State state, InnerState innerState, byte[] data,
-                                 InputStream entityStream) throws StopRequest {
-        try {
-            return entityStream.read(data);
-        } catch (IOException ex) {
-            logNetworkState();
-            ContentValues values = new ContentValues();
-            values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, innerState.mBytesSoFar);
-            mContext.getContentResolver().update(mInfo.getAllDownloadsUri(), values, null, null);
-            if (cannotResume(innerState)) {
-                Log.d(Constants.TAG, "download IOException for download " + mInfo.mId, ex);
-                Log.d(Constants.TAG, "can't resume interrupted download with no ETag");
-                throw new StopRequest(Downloads.Impl.STATUS_CANNOT_RESUME, ex);
-            } else {
-                throw new StopRequest(handleHttpError(state, "download IOException"), ex);
+    private void checkConnectivity() throws StopRequestException {
+        // checking connectivity will apply current policy
+        mPolicyDirty = false;
+
+        final NetworkState networkUsable = mInfo.checkCanUseNetwork(mInfoDelta.mTotalBytes);
+        if (networkUsable != NetworkState.OK) {
+            int status = Downloads.Impl.STATUS_WAITING_FOR_NETWORK;
+            if (networkUsable == NetworkState.UNUSABLE_DUE_TO_SIZE) {
+                status = Downloads.Impl.STATUS_QUEUED_FOR_WIFI;
+                mInfo.notifyPauseDueToSize(true);
+            } else if (networkUsable == NetworkState.RECOMMENDED_UNUSABLE_DUE_TO_SIZE) {
+                status = Downloads.Impl.STATUS_QUEUED_FOR_WIFI;
+                mInfo.notifyPauseDueToSize(false);
             }
+            throw new StopRequestException(status, networkUsable.name());
         }
     }
 
     /**
-     * Open a stream for the HTTP response entity, handling I/O errors.
-     * @return an InputStream to read the response entity
+     * Check if the download has been paused or canceled, stopping the request
+     * appropriately if it has been.
      */
-    private InputStream openResponseEntity(State state, HttpResponse response)
-            throws StopRequest {
-        try {
-            return response.getEntity().getContent();
-        } catch (IOException ex) {
-            logNetworkState();
-            throw new StopRequest(handleHttpError(state, "IOException getting entity"), ex);
+    private void checkPausedOrCanceled() throws StopRequestException {
+        synchronized (mInfo) {
+            if (mInfo.mControl == Downloads.Impl.CONTROL_PAUSED) {
+                throw new StopRequestException(
+                        Downloads.Impl.STATUS_PAUSED_BY_APP, "download paused by owner");
+            }
+            if (mInfo.mStatus == Downloads.Impl.STATUS_CANCELED || mInfo.mDeleted) {
+                throw new StopRequestException(Downloads.Impl.STATUS_CANCELED, "download canceled");
+            }
         }
-    }
 
-    private void logNetworkState() {
-        if (Constants.LOGX) {
-            Log.i(Constants.TAG,
-                    "Net " + (Helpers.isNetworkAvailable(mSystemFacade) ? "Up" : "Down"));
+        // if policy has been changed, trigger connectivity check
+        if (mPolicyDirty) {
+            checkConnectivity();
         }
     }
 
     /**
-     * Read HTTP response headers and take appropriate action, including setting up the destination
-     * file and updating the database.
+     * Report download progress through the database if necessary.
      */
-    private void processResponseHeaders(State state, InnerState innerState, HttpResponse response)
-            throws StopRequest, FileNotFoundException {
-        if (innerState.mContinuingDownload) {
-            // ignore response headers on resume requests
-            return;
-        }
+    private void updateProgress(FileDescriptor outFd) throws IOException, StopRequestException {
+        final long now = SystemClock.elapsedRealtime();
+        final long currentBytes = mInfoDelta.mCurrentBytes;
 
-        readResponseHeaders(state, innerState, response);
-
-        DownloadFileInfo fileInfo = Helpers.generateSaveFile(
-                mContext,
-                mInfo.mUri,
-                mInfo.mHint,
-                innerState.mHeaderContentDisposition,
-                innerState.mHeaderContentLocation,
-                state.mMimeType,
-                mInfo.mDestination,
-                (innerState.mHeaderContentLength != null) ?
-                        Long.parseLong(innerState.mHeaderContentLength) : 0,
-                mInfo.mIsPublicApi);
-        if (fileInfo.mFileName == null) {
-            throw new StopRequest(fileInfo.mStatus);
-        }
-        state.mFilename = fileInfo.mFileName;
-        state.mStream = fileInfo.mStream;
-        if (Constants.LOGV) {
-            Log.v(Constants.TAG, "writing " + mInfo.mUri + " to " + state.mFilename);
-        }
+        final long sampleDelta = now - mSpeedSampleStart;
+        if (sampleDelta > 500) {
+            final long sampleSpeed = ((currentBytes - mSpeedSampleBytes) * 1000)
+                    / sampleDelta;
 
-        updateDatabaseFromHeaders(state, innerState);
-        // check connectivity again now that we know the total size
-        checkConnectivity(state);
-    }
+            if (mSpeed == 0) {
+                mSpeed = sampleSpeed;
+            } else {
+                mSpeed = ((mSpeed * 3) + sampleSpeed) / 4;
+            }
 
-    /**
-     * Update necessary database fields based on values of HTTP response headers that have been
-     * read.
-     */
-    private void updateDatabaseFromHeaders(State state, InnerState innerState) {
-        ContentValues values = new ContentValues();
-        values.put(Downloads.Impl._DATA, state.mFilename);
-        if (innerState.mHeaderETag != null) {
-            values.put(Constants.ETAG, innerState.mHeaderETag);
+            // Only notify once we have a full sample window
+            if (mSpeedSampleStart != 0) {
+                mNotifier.notifyDownloadSpeed(mId, mSpeed);
+            }
+
+            mSpeedSampleStart = now;
+            mSpeedSampleBytes = currentBytes;
         }
-        if (state.mMimeType != null) {
-            values.put(Downloads.Impl.COLUMN_MIME_TYPE, state.mMimeType);
+
+        final long bytesDelta = currentBytes - mLastUpdateBytes;
+        final long timeDelta = now - mLastUpdateTime;
+        if (bytesDelta > Constants.MIN_PROGRESS_STEP && timeDelta > Constants.MIN_PROGRESS_TIME) {
+            // fsync() to ensure that current progress has been flushed to disk,
+            // so we can always resume based on latest database information.
+            outFd.sync();
+
+            mInfoDelta.writeToDatabaseOrThrow();
+
+            mLastUpdateBytes = currentBytes;
+            mLastUpdateTime = now;
         }
-        values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, mInfo.mTotalBytes);
-        mContext.getContentResolver().update(mInfo.getAllDownloadsUri(), values, null, null);
     }
 
     /**
-     * Read headers from the HTTP response and store them into local state.
+     * Process response headers from first server response. This derives its
+     * filename, size, and ETag.
      */
-    private void readResponseHeaders(State state, InnerState innerState, HttpResponse response)
-            throws StopRequest {
-        Header header = response.getFirstHeader("Content-Disposition");
-        if (header != null) {
-            innerState.mHeaderContentDisposition = header.getValue();
-        }
-        header = response.getFirstHeader("Content-Location");
-        if (header != null) {
-            innerState.mHeaderContentLocation = header.getValue();
-        }
-        if (state.mMimeType == null) {
-            header = response.getFirstHeader("Content-Type");
-            if (header != null) {
-                state.mMimeType = sanitizeMimeType(header.getValue());
+    private void parseOkHeaders(HttpURLConnection conn) throws StopRequestException {
+        if (mInfoDelta.mFileName == null) {
+            final String contentDisposition = conn.getHeaderField("Content-Disposition");
+            final String contentLocation = conn.getHeaderField("Content-Location");
+
+            try {
+                mInfoDelta.mFileName = Helpers.generateSaveFile(mContext, mInfoDelta.mUri,
+                        mInfo.mHint, contentDisposition, contentLocation, mInfoDelta.mMimeType,
+                        mInfo.mDestination);
+            } catch (IOException e) {
+                throw new StopRequestException(
+                        Downloads.Impl.STATUS_FILE_ERROR, "Failed to generate filename: " + e);
             }
         }
-        header = response.getFirstHeader("ETag");
-        if (header != null) {
-            innerState.mHeaderETag = header.getValue();
-        }
-        String headerTransferEncoding = null;
-        header = response.getFirstHeader("Transfer-Encoding");
-        if (header != null) {
-            headerTransferEncoding = header.getValue();
+
+        if (mInfoDelta.mMimeType == null) {
+            mInfoDelta.mMimeType = Intent.normalizeMimeType(conn.getContentType());
         }
-        if (headerTransferEncoding == null) {
-            header = response.getFirstHeader("Content-Length");
-            if (header != null) {
-                innerState.mHeaderContentLength = header.getValue();
-                mInfo.mTotalBytes = Long.parseLong(innerState.mHeaderContentLength);
-            }
+
+        final String transferEncoding = conn.getHeaderField("Transfer-Encoding");
+        if (transferEncoding == null) {
+            mInfoDelta.mTotalBytes = getHeaderFieldLong(conn, "Content-Length", -1);
         } else {
-            // Ignore content-length with transfer-encoding - 2616 4.4 3
-            if (Constants.LOGVV) {
-                Log.v(Constants.TAG,
-                        "ignoring content-length because of xfer-encoding");
-            }
-        }
-        if (Constants.LOGVV) {
-            Log.v(Constants.TAG, "Content-Disposition: " +
-                    innerState.mHeaderContentDisposition);
-            Log.v(Constants.TAG, "Content-Length: " + innerState.mHeaderContentLength);
-            Log.v(Constants.TAG, "Content-Location: " + innerState.mHeaderContentLocation);
-            Log.v(Constants.TAG, "Content-Type: " + state.mMimeType);
-            Log.v(Constants.TAG, "ETag: " + innerState.mHeaderETag);
-            Log.v(Constants.TAG, "Transfer-Encoding: " + headerTransferEncoding);
+            mInfoDelta.mTotalBytes = -1;
         }
 
-        boolean noSizeInfo = innerState.mHeaderContentLength == null
-                && (headerTransferEncoding == null
-                    || !headerTransferEncoding.equalsIgnoreCase("chunked"));
-        if (!mInfo.mNoIntegrity && noSizeInfo) {
-            Log.d(Constants.TAG, "can't know size of download, giving up");
-            throw new StopRequest(Downloads.Impl.STATUS_HTTP_DATA_ERROR);
-        }
-    }
+        mInfoDelta.mETag = conn.getHeaderField("ETag");
 
-    /**
-     * Check the HTTP response status and handle anything unusual (e.g. not 200/206).
-     */
-    private void handleExceptionalStatus(State state, InnerState innerState, HttpResponse response)
-            throws StopRequest, RetryDownload {
-        int statusCode = response.getStatusLine().getStatusCode();
-        if (statusCode == 503 && mInfo.mNumFailed < Constants.MAX_RETRIES) {
-            handleServiceUnavailable(state, response);
-        }
-        if (statusCode == 301 || statusCode == 302 || statusCode == 303 || statusCode == 307) {
-            handleRedirect(state, response, statusCode);
-        }
+        mInfoDelta.writeToDatabaseOrThrow();
 
-        int expectedStatus = innerState.mContinuingDownload ? 206 : Downloads.Impl.STATUS_SUCCESS;
-        if (statusCode != expectedStatus) {
-            handleOtherStatus(state, innerState, statusCode);
-        }
+        // Check connectivity again now that we know the total size
+        checkConnectivity();
     }
 
-    /**
-     * Handle a status that we don't know how to deal with properly.
-     */
-    private void handleOtherStatus(State state, InnerState innerState, int statusCode)
-            throws StopRequest {
-        if (Constants.LOGV) {
-            Log.d(Constants.TAG, "http error " + statusCode + " for " + mInfo.mUri);
-        } else if (Config.LOGD) {
-            Log.d(Constants.TAG, "http error " + statusCode + " for download " +
-                    mInfo.mId);
-        }
-        int finalStatus;
-        if (Downloads.Impl.isStatusError(statusCode)) {
-            finalStatus = statusCode;
-        } else if (statusCode >= 300 && statusCode < 400) {
-            finalStatus = Downloads.Impl.STATUS_UNHANDLED_REDIRECT;
-        } else if (innerState.mContinuingDownload && statusCode == Downloads.Impl.STATUS_SUCCESS) {
-            finalStatus = Downloads.Impl.STATUS_CANNOT_RESUME;
+    private void parseUnavailableHeaders(HttpURLConnection conn) {
+        long retryAfter = conn.getHeaderFieldInt("Retry-After", -1);
+        if (retryAfter < 0) {
+            retryAfter = 0;
         } else {
-            finalStatus = Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE;
-        }
-        throw new StopRequest(finalStatus);
-    }
-
-    /**
-     * Handle a 3xx redirect status.
-     */
-    private void handleRedirect(State state, HttpResponse response, int statusCode)
-            throws StopRequest, RetryDownload {
-        if (Constants.LOGVV) {
-            Log.v(Constants.TAG, "got HTTP redirect " + statusCode);
-        }
-        if (state.mRedirectCount >= Constants.MAX_REDIRECTS) {
-            if (Constants.LOGV) {
-                Log.d(Constants.TAG, "too many redirects for download " + mInfo.mId +
-                        " at " + mInfo.mUri);
-            } else if (Config.LOGD) {
-                Log.d(Constants.TAG, "too many redirects for download " + mInfo.mId);
+            if (retryAfter < Constants.MIN_RETRY_AFTER) {
+                retryAfter = Constants.MIN_RETRY_AFTER;
+            } else if (retryAfter > Constants.MAX_RETRY_AFTER) {
+                retryAfter = Constants.MAX_RETRY_AFTER;
             }
-            throw new StopRequest(Downloads.Impl.STATUS_TOO_MANY_REDIRECTS);
-        }
-        Header header = response.getFirstHeader("Location");
-        if (header == null) {
-            return;
-        }
-        if (Constants.LOGVV) {
-            Log.v(Constants.TAG, "Location :" + header.getValue());
+            retryAfter += Helpers.sRandom.nextInt(Constants.MIN_RETRY_AFTER + 1);
         }
 
-        String newUri;
-        try {
-            newUri = new URI(mInfo.mUri).resolve(new URI(header.getValue())).toString();
-        } catch(URISyntaxException ex) {
-            if (Constants.LOGV) {
-                Log.d(Constants.TAG, "Couldn't resolve redirect URI " + header.getValue()
-                        + " for " + mInfo.mUri);
-            } else if (Config.LOGD) {
-                Log.d(Constants.TAG,
-                        "Couldn't resolve redirect URI for download " +
-                        mInfo.mId);
-            }
-            throw new StopRequest(Downloads.Impl.STATUS_HTTP_DATA_ERROR);
-        }
-        ++state.mRedirectCount;
-        state.mRequestUri = newUri;
-        if (statusCode == 301 || statusCode == 303) {
-            // use the new URI for all future requests (should a retry/resume be necessary)
-            state.mNewUri = newUri;
-        }
-        throw new RetryDownload();
+        mInfoDelta.mRetryAfter = (int) (retryAfter * SECOND_IN_MILLIS);
     }
 
     /**
-     * Handle a 503 Service Unavailable status by processing the Retry-After header.
+     * Add custom headers for this download to the HTTP request.
      */
-    private void handleServiceUnavailable(State state, HttpResponse response) throws StopRequest {
-        if (Constants.LOGVV) {
-            Log.v(Constants.TAG, "got HTTP response code 503");
-        }
-        state.mCountRetry = true;
-        Header header = response.getFirstHeader("Retry-After");
-        if (header != null) {
-           try {
-               if (Constants.LOGVV) {
-                   Log.v(Constants.TAG, "Retry-After :" + header.getValue());
-               }
-               state.mRetryAfter = Integer.parseInt(header.getValue());
-               if (state.mRetryAfter < 0) {
-                   state.mRetryAfter = 0;
-               } else {
-                   if (state.mRetryAfter < Constants.MIN_RETRY_AFTER) {
-                       state.mRetryAfter = Constants.MIN_RETRY_AFTER;
-                   } else if (state.mRetryAfter > Constants.MAX_RETRY_AFTER) {
-                       state.mRetryAfter = Constants.MAX_RETRY_AFTER;
-                   }
-                   state.mRetryAfter += Helpers.sRandom.nextInt(Constants.MIN_RETRY_AFTER + 1);
-                   state.mRetryAfter *= 1000;
-               }
-           } catch (NumberFormatException ex) {
-               // ignored - retryAfter stays 0 in this case.
-           }
+    private void addRequestHeaders(HttpURLConnection conn, boolean resuming) {
+        for (Pair<String, String> header : mInfo.getHeaders()) {
+            conn.addRequestProperty(header.first, header.second);
         }
-        throw new StopRequest(Downloads.Impl.STATUS_RUNNING_PAUSED);
-    }
 
-    /**
-     * Send the request to the server, handling any I/O exceptions.
-     */
-    private HttpResponse sendRequest(State state, AndroidHttpClient client, HttpGet request)
-            throws StopRequest {
-        try {
-            return client.execute(request);
-        } catch (IllegalArgumentException ex) {
-            if (Constants.LOGV) {
-                Log.d(Constants.TAG, "Arg exception trying to execute request for " +
-                        mInfo.mUri + " : " + ex);
-            } else if (Config.LOGD) {
-                Log.d(Constants.TAG, "Arg exception trying to execute request for " +
-                        mInfo.mId + " : " +  ex);
-            }
-            throw new StopRequest(Downloads.Impl.STATUS_HTTP_DATA_ERROR, ex);
-        } catch (IOException ex) {
-            logNetworkState();
-            throw new StopRequest(handleHttpError(state, "IOException trying to execute request"),
-                    ex);
+        // Only splice in user agent when not already defined
+        if (conn.getRequestProperty("User-Agent") == null) {
+            conn.addRequestProperty("User-Agent", mInfo.getUserAgent());
         }
-    }
 
-    /**
-     * @return the final status for this attempt
-     */
-    private int handleHttpError(State state, String message) {
-        if (Constants.LOGV) {
-            Log.d(Constants.TAG, message + " for " + mInfo.mUri);
-        }
+        // Defeat transparent gzip compression, since it doesn't allow us to
+        // easily resume partial downloads.
+        conn.setRequestProperty("Accept-Encoding", "identity");
 
-        if (!Helpers.isNetworkAvailable(mSystemFacade)) {
-            return Downloads.Impl.STATUS_RUNNING_PAUSED;
-        } else if (mInfo.mNumFailed < Constants.MAX_RETRIES) {
-            state.mCountRetry = true;
-            return Downloads.Impl.STATUS_RUNNING_PAUSED;
-        } else {
-            Log.d(Constants.TAG, "reached max retries: " + message + " for " + mInfo.mId);
-            return Downloads.Impl.STATUS_HTTP_DATA_ERROR;
-        }
-    }
+        // Defeat connection reuse, since otherwise servers may continue
+        // streaming large downloads after cancelled.
+        conn.setRequestProperty("Connection", "close");
 
-    /**
-     * Prepare the destination file to receive data.  If the file already exists, we'll set up
-     * appropriately for resumption.
-     */
-    private void setupDestinationFile(State state, InnerState innerState)
-            throws StopRequest, FileNotFoundException {
-        if (state.mFilename != null) { // only true if we've already run a thread for this download
-            if (!Helpers.isFilenameValid(state.mFilename)) {
-                throw new StopRequest(Downloads.Impl.STATUS_FILE_ERROR);
-            }
-            // We're resuming a download that got interrupted
-            File f = new File(state.mFilename);
-            if (f.exists()) {
-                long fileLength = f.length();
-                if (fileLength == 0) {
-                    // The download hadn't actually started, we can restart from scratch
-                    f.delete();
-                    state.mFilename = null;
-                } else if (mInfo.mETag == null && !mInfo.mNoIntegrity) {
-                    // This should've been caught upon failure
-                    Log.wtf(Constants.TAG, "Trying to resume a download that can't be resumed");
-                    f.delete();
-                    throw new StopRequest(Downloads.Impl.STATUS_CANNOT_RESUME);
-                } else {
-                    // All right, we'll be able to resume this download
-                    state.mStream = new FileOutputStream(state.mFilename, true);
-                    innerState.mBytesSoFar = (int) fileLength;
-                    if (mInfo.mTotalBytes != -1) {
-                        innerState.mHeaderContentLength = Long.toString(mInfo.mTotalBytes);
-                    }
-                    innerState.mHeaderETag = mInfo.mETag;
-                    innerState.mContinuingDownload = true;
-                }
+        if (resuming) {
+            if (mInfoDelta.mETag != null) {
+                conn.addRequestProperty("If-Match", mInfoDelta.mETag);
             }
+            conn.addRequestProperty("Range", "bytes=" + mInfoDelta.mCurrentBytes + "-");
         }
+    }
 
-        if (state.mStream != null && mInfo.mDestination == Downloads.Impl.DESTINATION_EXTERNAL
-                && !isDrmFile(state)) {
-            closeDestination(state);
-        }
+    private void logDebug(String msg) {
+        Log.d(TAG, "[" + mId + "] " + msg);
     }
 
-    /**
-     * Add custom headers for this download to the HTTP request.
-     */
-    private void addRequestHeaders(InnerState innerState, HttpGet request) {
-        for (Map.Entry<String, String> header : mInfo.getHeaders().entrySet()) {
-            request.addHeader(header.getKey(), header.getValue());
-        }
+    private void logWarning(String msg) {
+        Log.w(TAG, "[" + mId + "] " + msg);
+    }
 
-        if (innerState.mContinuingDownload) {
-            if (innerState.mHeaderETag != null) {
-                request.addHeader("If-Match", innerState.mHeaderETag);
-            }
-            request.addHeader("Range", "bytes=" + innerState.mBytesSoFar + "-");
-        }
+    private void logError(String msg, Throwable t) {
+        Log.e(TAG, "[" + mId + "] " + msg, t);
     }
 
-    /**
-     * Stores information about the completed download, and notifies the initiating application.
-     */
-    private void notifyDownloadCompleted(
-            int status, boolean countRetry, int retryAfter, int redirectCount, boolean gotData,
-            String filename, String uri, String mimeType) {
-        notifyThroughDatabase(
-                status, countRetry, retryAfter, redirectCount, gotData, filename, uri, mimeType);
-        if (Downloads.Impl.isStatusCompleted(status)) {
-            mInfo.sendIntentIfRequested();
+    private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() {
+        @Override
+        public void onUidRulesChanged(int uid, int uidRules) {
+            // caller is NPMS, since we only register with them
+            if (uid == mInfo.mUid) {
+                mPolicyDirty = true;
+            }
         }
-    }
 
-    private void notifyThroughDatabase(
-            int status, boolean countRetry, int retryAfter, int redirectCount, boolean gotData,
-            String filename, String uri, String mimeType) {
-        ContentValues values = new ContentValues();
-        values.put(Downloads.Impl.COLUMN_STATUS, status);
-        values.put(Downloads.Impl._DATA, filename);
-        if (uri != null) {
-            values.put(Downloads.Impl.COLUMN_URI, uri);
+        @Override
+        public void onMeteredIfacesChanged(String[] meteredIfaces) {
+            // caller is NPMS, since we only register with them
+            mPolicyDirty = true;
         }
-        values.put(Downloads.Impl.COLUMN_MIME_TYPE, mimeType);
-        values.put(Downloads.Impl.COLUMN_LAST_MODIFICATION, mSystemFacade.currentTimeMillis());
-        values.put(Constants.RETRY_AFTER_X_REDIRECT_COUNT, retryAfter + (redirectCount << 28));
-        if (!countRetry) {
-            values.put(Constants.FAILED_CONNECTIONS, 0);
-        } else if (gotData) {
-            values.put(Constants.FAILED_CONNECTIONS, 1);
-        } else {
-            values.put(Constants.FAILED_CONNECTIONS, mInfo.mNumFailed + 1);
+
+        @Override
+        public void onRestrictBackgroundChanged(boolean restrictBackground) {
+            // caller is NPMS, since we only register with them
+            mPolicyDirty = true;
         }
+    };
 
-        mContext.getContentResolver().update(mInfo.getAllDownloadsUri(), values, null, null);
+    private static long getHeaderFieldLong(URLConnection conn, String field, long defaultValue) {
+        try {
+            return Long.parseLong(conn.getHeaderField(field));
+        } catch (NumberFormatException e) {
+            return defaultValue;
+        }
     }
 
     /**
-     * Clean up a mimeType string so it can be used to dispatch an intent to
-     * view a downloaded asset.
-     * @param mimeType either null or one or more mime types (semi colon separated).
-     * @return null if mimeType was null. Otherwise a string which represents a
-     * single mimetype in lowercase and with surrounding whitespaces trimmed.
+     * Return if given status is eligible to be treated as
+     * {@link android.provider.Downloads.Impl#STATUS_WAITING_TO_RETRY}.
      */
-    private static String sanitizeMimeType(String mimeType) {
-        try {
-            mimeType = mimeType.trim().toLowerCase(Locale.ENGLISH);
-
-            final int semicolonIndex = mimeType.indexOf(';');
-            if (semicolonIndex != -1) {
-                mimeType = mimeType.substring(0, semicolonIndex);
-            }
-            return mimeType;
-        } catch (NullPointerException npe) {
-            return null;
+    public static boolean isStatusRetryable(int status) {
+        switch (status) {
+            case STATUS_HTTP_DATA_ERROR:
+            case HTTP_UNAVAILABLE:
+            case HTTP_INTERNAL_ERROR:
+            case STATUS_FILE_ERROR:
+                return true;
+            default:
+                return false;
         }
     }
 }