summaryrefslogtreecommitdiffstats
path: root/src/com/android/providers/downloads/DownloadInfo.java
blob: b8cead65832fe5513369bdd4e1d9c17251ea61c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.downloads;

import android.net.Uri;
import android.content.Context;
import android.content.Intent;
import android.provider.Downloads;

/**
 * Stores information about an individual download.
 */
public class DownloadInfo {
    public int id;
    public String uri;
    public int method;
    public String entity;
    public boolean noIntegrity;
    public String hint;
    public String filename;
    public boolean otaUpdate;
    public String mimetype;
    public int destination;
    public boolean noSystem;
    public int visibility;
    public int control;
    public int status;
    public int numFailed;
    public long lastMod;
    public String pckg;
    public String clazz;
    public String extras;
    public String cookies;
    public String userAgent;
    public String referer;
    public int totalBytes;
    public int currentBytes;
    public String etag;
    public boolean mediaScanned;

    public volatile boolean hasActiveThread;

    public DownloadInfo(int id, String uri, int method, String entity, boolean noIntegrity,
            String hint, String filename, boolean otaUpdate,
            String mimetype, int destination, boolean noSystem, int visibility,
            int control, int status, int numFailed, long lastMod,
            String pckg, String clazz, String extras, String cookies,
            String userAgent, String referer, int totalBytes, int currentBytes, String etag,
            boolean mediaScanned) {
        this.id = id;
        this.uri = uri;
        this.method = method;
        this.entity = entity;
        this.noIntegrity = noIntegrity;
        this.hint = hint;
        this.filename = filename;
        this.otaUpdate = otaUpdate;
        this.mimetype = mimetype;
        this.destination = destination;
        this.noSystem = noSystem;
        this.visibility = visibility;
        this.control = control;
        this.status = status;
        this.numFailed = numFailed;
        this.lastMod = lastMod;
        this.pckg = pckg;
        this.clazz = clazz;
        this.extras = extras;
        this.cookies = cookies;
        this.userAgent = userAgent;
        this.referer = referer;
        this.totalBytes = totalBytes;
        this.currentBytes = currentBytes;
        this.etag = etag;
        this.mediaScanned = mediaScanned;
    }

    public void sendIntentIfRequested(Uri contentUri, Context context) {
        if (pckg != null && clazz != null) {
            Intent intent = new Intent(Downloads.DOWNLOAD_COMPLETED_ACTION);
            intent.setClassName(pckg, clazz);
            if (extras != null) {
                intent.putExtra(Downloads.NOTIFICATION_EXTRAS, extras);
            }
            // We only send the content: URI, for security reasons. Otherwise, malicious
            //     applications would have an easier time spoofing download results by
            //     sending spoofed intents.
            intent.setData(contentUri);
            context.sendBroadcast(intent);
        }
    }

    /**
     * Returns the time when a download should be restarted. Must only
     * be called when numFailed > 0.
     */
    public long restartTime() {
        return lastMod + Constants.RETRY_FIRST_DELAY * 1000 * (1 << (numFailed - 1));
    }

    /**
     * Returns whether this download should be started at the time when
     * it's first inserted in the database.
     */
    public boolean isReadyToStart(long now) {
        if (status == 0) {
            // status hasn't been initialized yet, this is a new download
            return true;
        }
        if (status == Downloads.STATUS_PENDING) {
            // download is explicit marked as ready to start
            return true;
        }
        if (status == Downloads.STATUS_RUNNING) {
            // download was interrupted (process killed, loss of power) while it was running,
            //     without a chance to update the database
            return true;
        }
        if (status == Downloads.STATUS_RUNNING_PAUSED) {
            if (numFailed == 0) {
                // download is waiting for network connectivity to return before it can resume
                return true;
            }
            if (restartTime() < now) {
                // download was waiting for a delayed restart, and the delay has expired
                return true;
            }
        }
        return false;
    }

    /**
     * Returns whether this download should be restarted at the time when
     * it was already known by the download manager
     */
    public boolean isReadyToRestart(long now) {
        if (status == 0) {
            // download hadn't been initialized yet
            return true;
        }
        if (status == Downloads.STATUS_PENDING) {
            // download is explicit marked as ready to start
            return true;
        }
        if (status == Downloads.STATUS_RUNNING_PAUSED) {
            if (numFailed == 0) {
                // download is waiting for network connectivity to return before it can resume
                return true;
            }
            if (restartTime() < now) {
                // download was waiting for a delayed restart, and the delay has expired
                return true;
            }
        }
        return false;
    }

    /**
     * Returns whether this download has a visible notification after
     * completion.
     */
    public boolean hasCompletionNotification() {
        if (!Downloads.isStatusCompleted(status)) {
            return false;
        }
        if (visibility == Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) {
            return true;
        }
        return false;
    }
}