2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.providers.downloads;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.provider.Downloads;
25 * Stores information about an individual download.
27 public class DownloadInfo {
30 public boolean mNoIntegrity;
32 public String mFileName;
33 public String mMimeType;
34 public int mDestination;
35 public int mVisibility;
38 public int mNumFailed;
39 public int mRetryAfter;
40 public int mRedirectCount;
42 public String mPackage;
44 public String mExtras;
45 public String mCookies;
46 public String mUserAgent;
47 public String mReferer;
48 public int mTotalBytes;
49 public int mCurrentBytes;
51 public boolean mMediaScanned;
55 public volatile boolean mHasActiveThread;
57 public DownloadInfo(int id, String uri, boolean noIntegrity,
58 String hint, String fileName,
59 String mimeType, int destination, int visibility, int control,
60 int status, int numFailed, int retryAfter, int redirectCount, long lastMod,
61 String pckg, String clazz, String extras, String cookies,
62 String userAgent, String referer, int totalBytes, int currentBytes, String eTag,
63 boolean mediaScanned) {
66 mNoIntegrity = noIntegrity;
70 mDestination = destination;
71 mVisibility = visibility;
74 mNumFailed = numFailed;
75 mRetryAfter = retryAfter;
76 mRedirectCount = redirectCount;
82 mUserAgent = userAgent;
84 mTotalBytes = totalBytes;
85 mCurrentBytes = currentBytes;
87 mMediaScanned = mediaScanned;
88 mFuzz = Helpers.sRandom.nextInt(1001);
91 public void sendIntentIfRequested(Uri contentUri, Context context) {
92 if (mPackage != null && mClass != null) {
93 Intent intent = new Intent(Downloads.ACTION_DOWNLOAD_COMPLETED);
94 intent.setClassName(mPackage, mClass);
95 if (mExtras != null) {
96 intent.putExtra(Downloads.COLUMN_NOTIFICATION_EXTRAS, mExtras);
98 // We only send the content: URI, for security reasons. Otherwise, malicious
99 // applications would have an easier time spoofing download results by
100 // sending spoofed intents.
101 intent.setData(contentUri);
102 context.sendBroadcast(intent);
107 * Returns the time when a download should be restarted. Must only
108 * be called when numFailed > 0.
110 public long restartTime() {
111 if (mRetryAfter > 0) {
112 return mLastMod + mRetryAfter;
115 Constants.RETRY_FIRST_DELAY *
116 (1000 + mFuzz) * (1 << (mNumFailed - 1));
120 * Returns whether this download (which the download manager hasn't seen yet)
123 public boolean isReadyToStart(long now) {
124 if (mControl == Downloads.CONTROL_PAUSED) {
125 // the download is paused, so it's not going to start
129 // status hasn't been initialized yet, this is a new download
132 if (mStatus == Downloads.STATUS_PENDING) {
133 // download is explicit marked as ready to start
136 if (mStatus == Downloads.STATUS_RUNNING) {
137 // download was interrupted (process killed, loss of power) while it was running,
138 // without a chance to update the database
141 if (mStatus == Downloads.STATUS_RUNNING_PAUSED) {
142 if (mNumFailed == 0) {
143 // download is waiting for network connectivity to return before it can resume
146 if (restartTime() < now) {
147 // download was waiting for a delayed restart, and the delay has expired
155 * Returns whether this download (which the download manager has already seen
156 * and therefore potentially started) should be restarted.
158 * In a nutshell, this returns true if the download isn't already running
159 * but should be, and it can know whether the download is already running
160 * by checking the status.
162 public boolean isReadyToRestart(long now) {
163 if (mControl == Downloads.CONTROL_PAUSED) {
164 // the download is paused, so it's not going to restart
168 // download hadn't been initialized yet
171 if (mStatus == Downloads.STATUS_PENDING) {
172 // download is explicit marked as ready to start
175 if (mStatus == Downloads.STATUS_RUNNING_PAUSED) {
176 if (mNumFailed == 0) {
177 // download is waiting for network connectivity to return before it can resume
180 if (restartTime() < now) {
181 // download was waiting for a delayed restart, and the delay has expired
189 * Returns whether this download has a visible notification after
192 public boolean hasCompletionNotification() {
193 if (!Downloads.isStatusCompleted(mStatus)) {
196 if (mVisibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
203 * Returns whether this download is allowed to use the network.
205 public boolean canUseNetwork(boolean available, boolean roaming) {
209 if (mDestination == Downloads.DESTINATION_CACHE_PARTITION_NOROAMING) {