OSDN Git Service

Code drop from //branches/cupcake/...@124589
[android-x86/packages-providers-DownloadProvider.git] / src / com / android / providers / downloads / DownloadInfo.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.providers.downloads;
18
19 import android.net.Uri;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.provider.Downloads;
23
24 /**
25  * Stores information about an individual download.
26  */
27 public class DownloadInfo {
28     public int id;
29     public String uri;
30     public boolean noIntegrity;
31     public String hint;
32     public String filename;
33     public String mimetype;
34     public int destination;
35     public int visibility;
36     public int control;
37     public int status;
38     public int numFailed;
39     public int retryAfter;
40     public int redirectCount;
41     public long lastMod;
42     public String pckg;
43     public String clazz;
44     public String extras;
45     public String cookies;
46     public String userAgent;
47     public String referer;
48     public int totalBytes;
49     public int currentBytes;
50     public String etag;
51     public boolean mediaScanned;
52
53     public volatile boolean hasActiveThread;
54
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) {
62         this.id = id;
63         this.uri = uri;
64         this.noIntegrity = noIntegrity;
65         this.hint = hint;
66         this.filename = filename;
67         this.mimetype = mimetype;
68         this.destination = destination;
69         this.visibility = visibility;
70         this.control = control;
71         this.status = status;
72         this.numFailed = numFailed;
73         this.retryAfter = retryAfter;
74         this.redirectCount = redirectCount;
75         this.lastMod = lastMod;
76         this.pckg = pckg;
77         this.clazz = clazz;
78         this.extras = extras;
79         this.cookies = cookies;
80         this.userAgent = userAgent;
81         this.referer = referer;
82         this.totalBytes = totalBytes;
83         this.currentBytes = currentBytes;
84         this.etag = etag;
85         this.mediaScanned = mediaScanned;
86     }
87
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);
92             if (extras != null) {
93                 intent.putExtra(Downloads.NOTIFICATION_EXTRAS, extras);
94             }
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);
100         }
101     }
102
103     /**
104      * Returns the time when a download should be restarted. Must only
105      * be called when numFailed > 0.
106      */
107     public long restartTime() {
108         if (retryAfter > 0) {
109             return lastMod + retryAfter;
110         }
111         return lastMod +
112                 Constants.RETRY_FIRST_DELAY *
113                     (1000 + Helpers.rnd.nextInt(1001)) * (1 << (numFailed - 1));
114     }
115
116     /**
117      * Returns whether this download (which the download manager hasn't seen yet)
118      * should be started.
119      */
120     public boolean isReadyToStart(long now) {
121         if (control == Downloads.CONTROL_PAUSED) {
122             // the download is paused, so it's not going to start
123             return false;
124         }
125         if (status == 0) {
126             // status hasn't been initialized yet, this is a new download
127             return true;
128         }
129         if (status == Downloads.STATUS_PENDING) {
130             // download is explicit marked as ready to start
131             return true;
132         }
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
136             return true;
137         }
138         if (status == Downloads.STATUS_RUNNING_PAUSED) {
139             if (numFailed == 0) {
140                 // download is waiting for network connectivity to return before it can resume
141                 return true;
142             }
143             if (restartTime() < now) {
144                 // download was waiting for a delayed restart, and the delay has expired
145                 return true;
146             }
147         }
148         return false;
149     }
150
151     /**
152      * Returns whether this download (which the download manager has already seen
153      * and therefore potentially started) should be restarted.
154      *
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.
158      */
159     public boolean isReadyToRestart(long now) {
160         if (control == Downloads.CONTROL_PAUSED) {
161             // the download is paused, so it's not going to restart
162             return false;
163         }
164         if (status == 0) {
165             // download hadn't been initialized yet
166             return true;
167         }
168         if (status == Downloads.STATUS_PENDING) {
169             // download is explicit marked as ready to start
170             return true;
171         }
172         if (status == Downloads.STATUS_RUNNING_PAUSED) {
173             if (numFailed == 0) {
174                 // download is waiting for network connectivity to return before it can resume
175                 return true;
176             }
177             if (restartTime() < now) {
178                 // download was waiting for a delayed restart, and the delay has expired
179                 return true;
180             }
181         }
182         return false;
183     }
184
185     /**
186      * Returns whether this download has a visible notification after
187      * completion.
188      */
189     public boolean hasCompletionNotification() {
190         if (!Downloads.isStatusCompleted(status)) {
191             return false;
192         }
193         if (visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
194             return true;
195         }
196         return false;
197     }
198
199     /**
200      * Returns whether this download is allowed to use the network.
201      */
202     public boolean canUseNetwork(boolean available, boolean roaming) {
203         if (!available) {
204             return false;
205         }
206         if (destination == Downloads.DESTINATION_CACHE_PARTITION_NOROAMING) {
207             return !roaming;
208         } else {
209             return true;
210         }
211     }
212 }