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.net.Uri;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.provider.Downloads;
25 * Stores information about an individual download.
27 public class DownloadInfo {
30 public boolean noIntegrity;
32 public String filename;
33 public String mimetype;
34 public int destination;
35 public int visibility;
39 public int retryAfter;
40 public int redirectCount;
45 public String cookies;
46 public String userAgent;
47 public String referer;
48 public int totalBytes;
49 public int currentBytes;
51 public boolean mediaScanned;
53 public volatile boolean hasActiveThread;
55 public DownloadInfo(int id, String uri, boolean noIntegrity,
56 String hint, String filename,
57 String mimetype, int destination, int visibility, int control,
58 int status, int numFailed, int retryAfter, int redirectCount, long lastMod,
59 String pckg, String clazz, String extras, String cookies,
60 String userAgent, String referer, int totalBytes, int currentBytes, String etag,
61 boolean mediaScanned) {
64 this.noIntegrity = noIntegrity;
66 this.filename = filename;
67 this.mimetype = mimetype;
68 this.destination = destination;
69 this.visibility = visibility;
70 this.control = control;
72 this.numFailed = numFailed;
73 this.retryAfter = retryAfter;
74 this.redirectCount = redirectCount;
75 this.lastMod = lastMod;
79 this.cookies = cookies;
80 this.userAgent = userAgent;
81 this.referer = referer;
82 this.totalBytes = totalBytes;
83 this.currentBytes = currentBytes;
85 this.mediaScanned = mediaScanned;
88 public void sendIntentIfRequested(Uri contentUri, Context context) {
89 if (pckg != null && clazz != null) {
90 Intent intent = new Intent(Downloads.DOWNLOAD_COMPLETED_ACTION);
91 intent.setClassName(pckg, clazz);
93 intent.putExtra(Downloads.NOTIFICATION_EXTRAS, extras);
95 // We only send the content: URI, for security reasons. Otherwise, malicious
96 // applications would have an easier time spoofing download results by
97 // sending spoofed intents.
98 intent.setData(contentUri);
99 context.sendBroadcast(intent);
104 * Returns the time when a download should be restarted. Must only
105 * be called when numFailed > 0.
107 public long restartTime() {
108 if (retryAfter > 0) {
109 return lastMod + retryAfter;
112 Constants.RETRY_FIRST_DELAY *
113 (1000 + Helpers.rnd.nextInt(1001)) * (1 << (numFailed - 1));
117 * Returns whether this download (which the download manager hasn't seen yet)
120 public boolean isReadyToStart(long now) {
121 if (control == Downloads.CONTROL_PAUSED) {
122 // the download is paused, so it's not going to start
126 // status hasn't been initialized yet, this is a new download
129 if (status == Downloads.STATUS_PENDING) {
130 // download is explicit marked as ready to start
133 if (status == Downloads.STATUS_RUNNING) {
134 // download was interrupted (process killed, loss of power) while it was running,
135 // without a chance to update the database
138 if (status == Downloads.STATUS_RUNNING_PAUSED) {
139 if (numFailed == 0) {
140 // download is waiting for network connectivity to return before it can resume
143 if (restartTime() < now) {
144 // download was waiting for a delayed restart, and the delay has expired
152 * Returns whether this download (which the download manager has already seen
153 * and therefore potentially started) should be restarted.
155 * In a nutshell, this returns true if the download isn't already running
156 * but should be, and it can know whether the download is already running
157 * by checking the status.
159 public boolean isReadyToRestart(long now) {
160 if (control == Downloads.CONTROL_PAUSED) {
161 // the download is paused, so it's not going to restart
165 // download hadn't been initialized yet
168 if (status == Downloads.STATUS_PENDING) {
169 // download is explicit marked as ready to start
172 if (status == Downloads.STATUS_RUNNING_PAUSED) {
173 if (numFailed == 0) {
174 // download is waiting for network connectivity to return before it can resume
177 if (restartTime() < now) {
178 // download was waiting for a delayed restart, and the delay has expired
186 * Returns whether this download has a visible notification after
189 public boolean hasCompletionNotification() {
190 if (!Downloads.isStatusCompleted(status)) {
193 if (visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
200 * Returns whether this download is allowed to use the network.
202 public boolean canUseNetwork(boolean available, boolean roaming) {
206 if (destination == Downloads.DESTINATION_CACHE_PARTITION_NOROAMING) {