summaryrefslogtreecommitdiffstats
path: root/src/com/android/providers/downloads/DownloadInfo.java
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2014-01-30 15:01:39 -0800
committerJeff Sharkey <jsharkey@android.com>2014-02-06 10:42:46 -0800
commitdffbb9c4567e9d29d19964a83129e38dceab7055 (patch)
tree773bb59bc04f75e19e3a39acba06de574f75a385 /src/com/android/providers/downloads/DownloadInfo.java
parent9b731a5521f569c91aeb419d43fa098a34cf78cb (diff)
downloadandroid_packages_providers_DownloadProvider-dffbb9c4567e9d29d19964a83129e38dceab7055.tar.gz
android_packages_providers_DownloadProvider-dffbb9c4567e9d29d19964a83129e38dceab7055.tar.bz2
android_packages_providers_DownloadProvider-dffbb9c4567e9d29d19964a83129e38dceab7055.zip
Many improvements to download storage management.
Change all data transfer to occur through FileDescriptors instead of relying on local files. This paves the way for downloading directly to content:// Uris in the future. Rewrite storage management logic to preflight download when size is known. If enough space is found, immediately reserve the space with fallocate(), advising the kernel block allocator to try giving us a contiguous block regions to reduce fragmentation. When preflighting on internal storage or emulated external storage, ask PackageManager to clear private app caches to free up space. Since we fallocate() the entire file, use the database as the source of truth for resume locations, which requires that we fsync() before each database update. Store in-progress downloads in separate directories to keep the OS from deleting out from under us. Clean up filename generation logic to break ties in this new dual-directory case. Clearer enforcement of successful download preconditions around content lengths and ETags. Move all database field mutations to clearer DownloadInfoDelta object, and write back through single code path. Catch and log uncaught exceptions from DownloadThread. Tests to verify new storage behaviors. Fixed existing test to reflect correct RFC behavior. Bug: 5287571, 3213677, 12663412 Change-Id: I6bb905eca7c7d1a6bc88df3db28b65d70f660221
Diffstat (limited to 'src/com/android/providers/downloads/DownloadInfo.java')
-rw-r--r--src/com/android/providers/downloads/DownloadInfo.java82
1 files changed, 49 insertions, 33 deletions
diff --git a/src/com/android/providers/downloads/DownloadInfo.java b/src/com/android/providers/downloads/DownloadInfo.java
index 7a912d5a..3571a781 100644
--- a/src/com/android/providers/downloads/DownloadInfo.java
+++ b/src/com/android/providers/downloads/DownloadInfo.java
@@ -36,6 +36,7 @@ import android.util.Pair;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.IndentingPrintWriter;
+import java.io.CharArrayWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -45,7 +46,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
/**
- * Stores information about an individual download.
+ * Details about a specific download. Fields should only be mutated by updating
+ * from database query.
*/
public class DownloadInfo {
// TODO: move towards these in-memory objects being sources of truth, and
@@ -60,10 +62,9 @@ public class DownloadInfo {
mCursor = cursor;
}
- public DownloadInfo newDownloadInfo(Context context, SystemFacade systemFacade,
- StorageManager storageManager, DownloadNotifier notifier) {
- final DownloadInfo info = new DownloadInfo(
- context, systemFacade, storageManager, notifier);
+ public DownloadInfo newDownloadInfo(
+ Context context, SystemFacade systemFacade, DownloadNotifier notifier) {
+ final DownloadInfo info = new DownloadInfo(context, systemFacade, notifier);
updateFromDatabase(info);
readRequestHeaders(info);
return info;
@@ -75,7 +76,7 @@ public class DownloadInfo {
info.mNoIntegrity = getInt(Downloads.Impl.COLUMN_NO_INTEGRITY) == 1;
info.mHint = getString(Downloads.Impl.COLUMN_FILE_NAME_HINT);
info.mFileName = getString(Downloads.Impl._DATA);
- info.mMimeType = getString(Downloads.Impl.COLUMN_MIME_TYPE);
+ info.mMimeType = Intent.normalizeMimeType(getString(Downloads.Impl.COLUMN_MIME_TYPE));
info.mDestination = getInt(Downloads.Impl.COLUMN_DESTINATION);
info.mVisibility = getInt(Downloads.Impl.COLUMN_VISIBILITY);
info.mStatus = getInt(Downloads.Impl.COLUMN_STATUS);
@@ -206,6 +207,7 @@ public class DownloadInfo {
public long mId;
public String mUri;
+ @Deprecated
public boolean mNoIntegrity;
public String mHint;
public String mFileName;
@@ -254,14 +256,11 @@ public class DownloadInfo {
private final Context mContext;
private final SystemFacade mSystemFacade;
- private final StorageManager mStorageManager;
private final DownloadNotifier mNotifier;
- private DownloadInfo(Context context, SystemFacade systemFacade, StorageManager storageManager,
- DownloadNotifier notifier) {
+ private DownloadInfo(Context context, SystemFacade systemFacade, DownloadNotifier notifier) {
mContext = context;
mSystemFacade = systemFacade;
- mStorageManager = storageManager;
mNotifier = notifier;
mFuzz = Helpers.sRandom.nextInt(1001);
}
@@ -270,6 +269,14 @@ public class DownloadInfo {
return Collections.unmodifiableList(mRequestHeaders);
}
+ public String getUserAgent() {
+ if (mUserAgent != null) {
+ return mUserAgent;
+ } else {
+ return Constants.DEFAULT_USER_AGENT;
+ }
+ }
+
public void sendIntentIfRequested() {
if (mPackage == null) {
return;
@@ -329,7 +336,7 @@ public class DownloadInfo {
case Downloads.Impl.STATUS_WAITING_FOR_NETWORK:
case Downloads.Impl.STATUS_QUEUED_FOR_WIFI:
- return checkCanUseNetwork() == NetworkState.OK;
+ return checkCanUseNetwork(mTotalBytes) == NetworkState.OK;
case Downloads.Impl.STATUS_WAITING_TO_RETRY:
// download was waiting for a delayed restart
@@ -362,7 +369,7 @@ public class DownloadInfo {
/**
* Returns whether this download is allowed to use the network.
*/
- public NetworkState checkCanUseNetwork() {
+ public NetworkState checkCanUseNetwork(long totalBytes) {
final NetworkInfo info = mSystemFacade.getActiveNetworkInfo(mUid);
if (info == null || !info.isConnected()) {
return NetworkState.NO_CONNECTION;
@@ -376,7 +383,7 @@ public class DownloadInfo {
if (mSystemFacade.isActiveNetworkMetered() && !mAllowMetered) {
return NetworkState.TYPE_DISALLOWED_BY_REQUESTOR;
}
- return checkIsNetworkTypeAllowed(info.getType());
+ return checkIsNetworkTypeAllowed(info.getType(), totalBytes);
}
private boolean isRoamingAllowed() {
@@ -392,7 +399,7 @@ public class DownloadInfo {
* @param networkType a constant from ConnectivityManager.TYPE_*.
* @return one of the NETWORK_* constants
*/
- private NetworkState checkIsNetworkTypeAllowed(int networkType) {
+ private NetworkState checkIsNetworkTypeAllowed(int networkType, long totalBytes) {
if (mIsPublicApi) {
final int flag = translateNetworkTypeToApiFlag(networkType);
final boolean allowAllNetworkTypes = mAllowedNetworkTypes == ~0;
@@ -400,7 +407,7 @@ public class DownloadInfo {
return NetworkState.TYPE_DISALLOWED_BY_REQUESTOR;
}
}
- return checkSizeAllowedForNetwork(networkType);
+ return checkSizeAllowedForNetwork(networkType, totalBytes);
}
/**
@@ -427,24 +434,27 @@ public class DownloadInfo {
* Check if the download's size prohibits it from running over the current network.
* @return one of the NETWORK_* constants
*/
- private NetworkState checkSizeAllowedForNetwork(int networkType) {
- if (mTotalBytes <= 0) {
- return NetworkState.OK; // we don't know the size yet
- }
- if (networkType == ConnectivityManager.TYPE_WIFI) {
- return NetworkState.OK; // anything goes over wifi
- }
- Long maxBytesOverMobile = mSystemFacade.getMaxBytesOverMobile();
- if (maxBytesOverMobile != null && mTotalBytes > maxBytesOverMobile) {
- return NetworkState.UNUSABLE_DUE_TO_SIZE;
- }
- if (mBypassRecommendedSizeLimit == 0) {
- Long recommendedMaxBytesOverMobile = mSystemFacade.getRecommendedMaxBytesOverMobile();
- if (recommendedMaxBytesOverMobile != null
- && mTotalBytes > recommendedMaxBytesOverMobile) {
- return NetworkState.RECOMMENDED_UNUSABLE_DUE_TO_SIZE;
+ private NetworkState checkSizeAllowedForNetwork(int networkType, long totalBytes) {
+ if (totalBytes <= 0) {
+ // we don't know the size yet
+ return NetworkState.OK;
+ }
+
+ if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
+ Long maxBytesOverMobile = mSystemFacade.getMaxBytesOverMobile();
+ if (maxBytesOverMobile != null && totalBytes > maxBytesOverMobile) {
+ return NetworkState.UNUSABLE_DUE_TO_SIZE;
+ }
+ if (mBypassRecommendedSizeLimit == 0) {
+ Long recommendedMaxBytesOverMobile = mSystemFacade
+ .getRecommendedMaxBytesOverMobile();
+ if (recommendedMaxBytesOverMobile != null
+ && totalBytes > recommendedMaxBytesOverMobile) {
+ return NetworkState.RECOMMENDED_UNUSABLE_DUE_TO_SIZE;
+ }
}
}
+
return NetworkState.OK;
}
@@ -467,8 +477,7 @@ public class DownloadInfo {
mContext.getContentResolver().update(getAllDownloadsUri(), values, null, null);
}
- mTask = new DownloadThread(
- mContext, mSystemFacade, this, mStorageManager, mNotifier);
+ mTask = new DownloadThread(mContext, mSystemFacade, mNotifier, this);
mSubmittedTask = executor.submit(mTask);
}
return isReady;
@@ -506,6 +515,13 @@ public class DownloadInfo {
return ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, mId);
}
+ @Override
+ public String toString() {
+ final CharArrayWriter writer = new CharArrayWriter();
+ dump(new IndentingPrintWriter(writer, " "));
+ return writer.toString();
+ }
+
public void dump(IndentingPrintWriter pw) {
pw.println("DownloadInfo:");
pw.increaseIndent();