OSDN Git Service

eclair snapshot
[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.content.Context;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.provider.Downloads;
23
24 /**
25  * Stores information about an individual download.
26  */
27 public class DownloadInfo {
28     public int mId;
29     public String mUri;
30     public boolean mNoIntegrity;
31     public String mHint;
32     public String mFileName;
33     public String mMimeType;
34     public int mDestination;
35     public int mVisibility;
36     public int mControl;
37     public int mStatus;
38     public int mNumFailed;
39     public int mRetryAfter;
40     public int mRedirectCount;
41     public long mLastMod;
42     public String mPackage;
43     public String mClass;
44     public String mExtras;
45     public String mCookies;
46     public String mUserAgent;
47     public String mReferer;
48     public int mTotalBytes;
49     public int mCurrentBytes;
50     public String mETag;
51     public boolean mMediaScanned;
52
53     public int mFuzz;
54
55     public volatile boolean mHasActiveThread;
56
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) {
64         mId = id;
65         mUri = uri;
66         mNoIntegrity = noIntegrity;
67         mHint = hint;
68         mFileName = fileName;
69         mMimeType = mimeType;
70         mDestination = destination;
71         mVisibility = visibility;
72         mControl = control;
73         mStatus = status;
74         mNumFailed = numFailed;
75         mRetryAfter = retryAfter;
76         mRedirectCount = redirectCount;
77         mLastMod = lastMod;
78         mPackage = pckg;
79         mClass = clazz;
80         mExtras = extras;
81         mCookies = cookies;
82         mUserAgent = userAgent;
83         mReferer = referer;
84         mTotalBytes = totalBytes;
85         mCurrentBytes = currentBytes;
86         mETag = eTag;
87         mMediaScanned = mediaScanned;
88         mFuzz = Helpers.sRandom.nextInt(1001); 
89     }
90
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);
97             }
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);
103         }
104     }
105
106     /**
107      * Returns the time when a download should be restarted. Must only
108      * be called when numFailed > 0.
109      */
110     public long restartTime() {
111         if (mRetryAfter > 0) {
112             return mLastMod + mRetryAfter;
113         }
114         return mLastMod +
115                 Constants.RETRY_FIRST_DELAY *
116                     (1000 + mFuzz) * (1 << (mNumFailed - 1));
117     }
118
119     /**
120      * Returns whether this download (which the download manager hasn't seen yet)
121      * should be started.
122      */
123     public boolean isReadyToStart(long now) {
124         if (mControl == Downloads.CONTROL_PAUSED) {
125             // the download is paused, so it's not going to start
126             return false;
127         }
128         if (mStatus == 0) {
129             // status hasn't been initialized yet, this is a new download
130             return true;
131         }
132         if (mStatus == Downloads.STATUS_PENDING) {
133             // download is explicit marked as ready to start
134             return true;
135         }
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
139             return true;
140         }
141         if (mStatus == Downloads.STATUS_RUNNING_PAUSED) {
142             if (mNumFailed == 0) {
143                 // download is waiting for network connectivity to return before it can resume
144                 return true;
145             }
146             if (restartTime() < now) {
147                 // download was waiting for a delayed restart, and the delay has expired
148                 return true;
149             }
150         }
151         return false;
152     }
153
154     /**
155      * Returns whether this download (which the download manager has already seen
156      * and therefore potentially started) should be restarted.
157      *
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.
161      */
162     public boolean isReadyToRestart(long now) {
163         if (mControl == Downloads.CONTROL_PAUSED) {
164             // the download is paused, so it's not going to restart
165             return false;
166         }
167         if (mStatus == 0) {
168             // download hadn't been initialized yet
169             return true;
170         }
171         if (mStatus == Downloads.STATUS_PENDING) {
172             // download is explicit marked as ready to start
173             return true;
174         }
175         if (mStatus == Downloads.STATUS_RUNNING_PAUSED) {
176             if (mNumFailed == 0) {
177                 // download is waiting for network connectivity to return before it can resume
178                 return true;
179             }
180             if (restartTime() < now) {
181                 // download was waiting for a delayed restart, and the delay has expired
182                 return true;
183             }
184         }
185         return false;
186     }
187
188     /**
189      * Returns whether this download has a visible notification after
190      * completion.
191      */
192     public boolean hasCompletionNotification() {
193         if (!Downloads.isStatusCompleted(mStatus)) {
194             return false;
195         }
196         if (mVisibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
197             return true;
198         }
199         return false;
200     }
201
202     /**
203      * Returns whether this download is allowed to use the network.
204      */
205     public boolean canUseNetwork(boolean available, boolean roaming) {
206         if (!available) {
207             return false;
208         }
209         if (mDestination == Downloads.DESTINATION_CACHE_PARTITION_NOROAMING) {
210             return !roaming;
211         } else {
212             return true;
213         }
214     }
215 }