summaryrefslogtreecommitdiffstats
path: root/src/com/android/providers/downloads/DownloadThread.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/providers/downloads/DownloadThread.java')
-rw-r--r--src/com/android/providers/downloads/DownloadThread.java102
1 files changed, 52 insertions, 50 deletions
diff --git a/src/com/android/providers/downloads/DownloadThread.java b/src/com/android/providers/downloads/DownloadThread.java
index 735946c9..7ddfe959 100644
--- a/src/com/android/providers/downloads/DownloadThread.java
+++ b/src/com/android/providers/downloads/DownloadThread.java
@@ -21,14 +21,13 @@ import org.apache.http.conn.params.ConnRouteParams;
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.net.TrafficStats;
import android.os.FileUtils;
import android.os.PowerManager;
import android.os.Process;
import android.provider.Downloads;
-import android.provider.DrmStore;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
@@ -56,6 +55,7 @@ public class DownloadThread extends Thread {
private final DownloadInfo mInfo;
private final SystemFacade mSystemFacade;
private final StorageManager mStorageManager;
+ private DrmConvertSession mDrmConvertSession;
public DownloadThread(Context context, SystemFacade systemFacade, DownloadInfo info,
StorageManager storageManager) {
@@ -138,13 +138,17 @@ public class DownloadThread extends Thread {
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
wakeLock.acquire();
-
if (Constants.LOGV) {
Log.v(Constants.TAG, "initiating download for " + mInfo.mUri);
}
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("android:DownloadManager");
+ TrafficStats.setThreadStatsUid(mInfo.mUid);
+
boolean finished = false;
while(!finished) {
Log.i(Constants.TAG, "Initiating request for download " + mInfo.mId);
@@ -171,23 +175,24 @@ public class DownloadThread extends Thread {
finalStatus = Downloads.Impl.STATUS_SUCCESS;
} catch (StopRequestException error) {
// remove the cause before printing, in case it contains PII
- errorMsg = "Aborting request for download " + mInfo.mId + ": " + error.getMessage();
- Log.w(Constants.TAG, errorMsg);
+ errorMsg = error.getMessage();
+ String msg = "Aborting request for download " + mInfo.mId + ": " + errorMsg;
+ Log.w(Constants.TAG, msg);
if (Constants.LOGV) {
- Log.w(Constants.TAG, errorMsg, error);
+ Log.w(Constants.TAG, msg, error);
}
finalStatus = error.mFinalStatus;
// fall through to finally block
} catch (Throwable ex) { //sometimes the socket code throws unchecked exceptions
- errorMsg = "Exception for id " + mInfo.mId + ": " + ex.getMessage();
- Log.w(Constants.TAG, errorMsg, ex);
+ errorMsg = ex.getMessage();
+ String msg = "Exception for id " + mInfo.mId + ": " + errorMsg;
+ Log.w(Constants.TAG, msg, ex);
finalStatus = Downloads.Impl.STATUS_UNKNOWN_ERROR;
// falls through to the code that reports an error
} finally {
- if (wakeLock != null) {
- wakeLock.release();
- wakeLock = null;
- }
+ TrafficStats.clearThreadStatsTag();
+ TrafficStats.clearThreadStatsUid();
+
if (client != null) {
client.close();
client = null;
@@ -196,7 +201,12 @@ public class DownloadThread extends Thread {
notifyDownloadCompleted(finalStatus, state.mCountRetry, state.mRetryAfter,
state.mGotData, state.mFilename,
state.mNewUri, state.mMimeType, errorMsg);
- mInfo.mHasActiveThread = false;
+ DownloadHandler.getInstance().dequeueDownload(mInfo.mId);
+
+ if (wakeLock != null) {
+ wakeLock.release();
+ wakeLock = null;
+ }
}
mStorageManager.incrementNumDownloadsSoFar();
}
@@ -279,13 +289,9 @@ public class DownloadThread extends Thread {
* Called after a successful completion to take any necessary action on the downloaded file.
*/
private void finalizeDestinationFile(State state) throws StopRequestException {
- if (isDrmFile(state)) {
- transferToDrm(state);
- } else {
- // make sure the file is readable
- FileUtils.setPermissions(state.mFilename, 0644, -1, -1);
- syncDestination(state);
- }
+ // make sure the file is readable
+ FileUtils.setPermissions(state.mFilename, 0644, -1, -1);
+ syncDestination(state);
}
/**
@@ -293,6 +299,10 @@ public class DownloadThread extends Thread {
* the downloaded file.
*/
private void cleanupDestination(State state, int finalStatus) {
+ if (mDrmConvertSession != null) {
+ finalStatus = mDrmConvertSession.close(state.mFilename);
+ }
+
closeDestination(state);
if (state.mFilename != null && Downloads.Impl.isStatusError(finalStatus)) {
new File(state.mFilename).delete();
@@ -330,30 +340,6 @@ public class DownloadThread extends Thread {
}
/**
- * @return true if the current download is a DRM file
- */
- private boolean isDrmFile(State state) {
- return DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(state.mMimeType);
- }
-
- /**
- * Transfer the downloaded destination file to the DRM store.
- */
- private void transferToDrm(State state) throws StopRequestException {
- File file = new File(state.mFilename);
- Intent item = DrmStore.addDrmFile(mContext.getContentResolver(), file, null);
- file.delete();
-
- if (item == null) {
- throw new StopRequestException(Downloads.Impl.STATUS_UNKNOWN_ERROR,
- "unable to add file to DrmProvider");
- } else {
- state.mFilename = item.getDataString();
- state.mMimeType = item.getType();
- }
- }
-
- /**
* Close the destination output stream.
*/
private void closeDestination(State state) {
@@ -418,10 +404,16 @@ public class DownloadThread extends Thread {
}
mStorageManager.verifySpaceBeforeWritingToFile(mInfo.mDestination, state.mFilename,
bytesRead);
- state.mStream.write(data, 0, bytesRead);
- if (mInfo.mDestination == Downloads.Impl.DESTINATION_EXTERNAL
- && !isDrmFile(state)) {
- closeDestination(state);
+ if (!DownloadDrmHelper.isDrmConvertNeeded(mInfo.mMimeType)) {
+ state.mStream.write(data, 0, bytesRead);
+ } else {
+ byte[] convertedData = mDrmConvertSession.convert(data, bytesRead);
+ if (convertedData != null) {
+ state.mStream.write(convertedData, 0, convertedData.length);
+ } else {
+ throw new StopRequestException(Downloads.Impl.STATUS_FILE_ERROR,
+ "Error converting drm data.");
+ }
}
return;
} catch (IOException ex) {
@@ -431,6 +423,10 @@ public class DownloadThread extends Thread {
if (state.mStream != null) {
mStorageManager.verifySpace(mInfo.mDestination, state.mFilename, bytesRead);
}
+ } finally {
+ if (mInfo.mDestination == Downloads.Impl.DESTINATION_EXTERNAL) {
+ closeDestination(state);
+ }
}
}
}
@@ -525,6 +521,13 @@ public class DownloadThread extends Thread {
}
readResponseHeaders(state, innerState, response);
+ if (DownloadDrmHelper.isDrmConvertNeeded(state.mMimeType)) {
+ mDrmConvertSession = DrmConvertSession.open(mContext, state.mMimeType);
+ if (mDrmConvertSession == null) {
+ throw new StopRequestException(Downloads.Impl.STATUS_NOT_ACCEPTABLE, "Mimetype "
+ + state.mMimeType + " can not be converted.");
+ }
+ }
state.mFilename = Helpers.generateSaveFile(
mContext,
@@ -850,8 +853,7 @@ public class DownloadThread extends Thread {
}
}
- if (state.mStream != null && mInfo.mDestination == Downloads.Impl.DESTINATION_EXTERNAL
- && !isDrmFile(state)) {
+ if (state.mStream != null && mInfo.mDestination == Downloads.Impl.DESTINATION_EXTERNAL) {
closeDestination(state);
}
}