DownloadInfo.java revision 59910f4a9ce953ea74c8db759448f227c96796b3
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
17package com.android.providers.downloads;
18
19import android.content.Context;
20import android.content.Intent;
21import android.net.Uri;
22import android.provider.Downloads;
23
24/**
25 * Stores information about an individual download.
26 */
27public 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 volatile boolean mHasActiveThread;
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        mId = id;
63        mUri = uri;
64        mNoIntegrity = noIntegrity;
65        mHint = hint;
66        mFileName = fileName;
67        mMimeType = mimeType;
68        mDestination = destination;
69        mVisibility = visibility;
70        mControl = control;
71        mStatus = status;
72        mNumFailed = numFailed;
73        mRetryAfter = retryAfter;
74        mRedirectCount = redirectCount;
75        mLastMod = lastMod;
76        mPackage = pckg;
77        mClass = clazz;
78        mExtras = extras;
79        mCookies = cookies;
80        mUserAgent = userAgent;
81        mReferer = referer;
82        mTotalBytes = totalBytes;
83        mCurrentBytes = currentBytes;
84        mETag = eTag;
85        mMediaScanned = mediaScanned;
86    }
87
88    public void sendIntentIfRequested(Uri contentUri, Context context) {
89        if (mPackage != null && mClass != null) {
90            Intent intent = new Intent(Downloads.ACTION_DOWNLOAD_COMPLETED);
91            intent.setClassName(mPackage, mClass);
92            if (mExtras != null) {
93                intent.putExtra(Downloads.COLUMN_NOTIFICATION_EXTRAS, mExtras);
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 (mRetryAfter > 0) {
109            return mLastMod + mRetryAfter;
110        }
111        return mLastMod +
112                Constants.RETRY_FIRST_DELAY *
113                    (1000 + Helpers.sRandom.nextInt(1001)) * (1 << (mNumFailed - 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 (mControl == Downloads.CONTROL_PAUSED) {
122            // the download is paused, so it's not going to start
123            return false;
124        }
125        if (mStatus == 0) {
126            // status hasn't been initialized yet, this is a new download
127            return true;
128        }
129        if (mStatus == Downloads.STATUS_PENDING) {
130            // download is explicit marked as ready to start
131            return true;
132        }
133        if (mStatus == 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 (mStatus == Downloads.STATUS_RUNNING_PAUSED) {
139            if (mNumFailed == 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 (mControl == Downloads.CONTROL_PAUSED) {
161            // the download is paused, so it's not going to restart
162            return false;
163        }
164        if (mStatus == 0) {
165            // download hadn't been initialized yet
166            return true;
167        }
168        if (mStatus == Downloads.STATUS_PENDING) {
169            // download is explicit marked as ready to start
170            return true;
171        }
172        if (mStatus == Downloads.STATUS_RUNNING_PAUSED) {
173            if (mNumFailed == 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(mStatus)) {
191            return false;
192        }
193        if (mVisibility == 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 (mDestination == Downloads.DESTINATION_CACHE_PARTITION_NOROAMING) {
207            return !roaming;
208        } else {
209            return true;
210        }
211    }
212}
213