summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/service/download/DownloadStatus.java
blob: 62e54cbb41fd2014d8debb3907342326255cedfb (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
package de.danoeh.antennapod.service.download;

import java.util.Date;

import de.danoeh.antennapod.feed.FeedFile;
import de.danoeh.antennapod.util.DownloadError;

/** Contains status attributes for one download */
public class DownloadStatus {
	/**
	 * Downloaders should use this constant for the size attribute if necessary
	 * so that the listadapters etc. can react properly.
	 */
	public static final int SIZE_UNKNOWN = -1;

	// ----------------------------------- ATTRIBUTES STORED IN DB
	/** Unique id for storing the object in database. */
	protected long id;
	/**
	 * A human-readable string which is shown to the user so that he can
	 * identify the download. Should be the title of the item/feed/media or the
	 * URL if the download has no other title.
	 */
	protected String title;
	protected DownloadError reason;
	/**
	 * A message which can be presented to the user to give more information.
	 * Should be null if Download was successful.
	 */
	protected String reasonDetailed;
	protected boolean successful;
	protected Date completionDate;
	protected long feedfileId;
	/**
	 * Is used to determine the type of the feedfile even if the feedfile does
	 * not exist anymore. The value should be FEEDFILETYPE_FEED,
	 * FEEDFILETYPE_FEEDIMAGE or FEEDFILETYPE_FEEDMEDIA
	 */
	protected int feedfileType;

	// ------------------------------------ NOT STORED IN DB
	protected boolean done;
	protected boolean cancelled;

	/** Constructor for restoring Download status entries from DB. */
	public DownloadStatus(long id, String title, long feedfileId,
			int feedfileType, boolean successful, DownloadError reason,
			Date completionDate, String reasonDetailed) {
		this.id = id;
		this.title = title;
		this.done = true;
		this.feedfileId = feedfileId;
		this.reason = reason;
		this.successful = successful;
		this.completionDate = completionDate;
		this.reasonDetailed = reasonDetailed;
		this.feedfileType = feedfileType;
	}

	public DownloadStatus(DownloadRequest request, DownloadError reason,
			boolean successful, boolean cancelled, String reasonDetailed) {
		if (request == null) {
			throw new IllegalArgumentException("request must not be null");
		}
		this.title = request.getTitle();
		this.feedfileId = request.getFeedfileId();
		this.feedfileType = request.getFeedfileType();
		this.reason = reason;
		this.successful = successful;
		this.cancelled = cancelled;
		this.reasonDetailed = reasonDetailed;
		this.completionDate = new Date();
	}

	/** Constructor for creating new completed downloads. */
	public DownloadStatus(FeedFile feedfile, String title, DownloadError reason,
			boolean successful, String reasonDetailed) {
		if (feedfile == null) {
			throw new IllegalArgumentException("feedfile must not be null");
		}

		this.title = title;
		this.done = true;
		this.feedfileId = feedfile.getId();
		this.feedfileType = feedfile.getTypeAsInt();
		this.reason = reason;
		this.successful = successful;
		this.completionDate = new Date();
		this.reasonDetailed = reasonDetailed;
	}

	/** Constructor for creating new completed downloads. */
	public DownloadStatus(long feedfileId, int feedfileType, String title,
			DownloadError reason, boolean successful, String reasonDetailed) {
		this.title = title;
		this.done = true;
		this.feedfileId = feedfileId;
		this.feedfileType = feedfileType;
		this.reason = reason;
		this.successful = successful;
		this.completionDate = new Date();
		this.reasonDetailed = reasonDetailed;
	}

	@Override
	public String toString() {
		return "DownloadStatus [id=" + id + ", title=" + title + ", reason="
				+ reason + ", reasonDetailed=" + reasonDetailed
				+ ", successful=" + successful + ", completionDate="
				+ completionDate + ", feedfileId=" + feedfileId
				+ ", feedfileType=" + feedfileType + ", done=" + done
				+ ", cancelled=" + cancelled + "]";
	}

    public long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public DownloadError getReason() {
        return reason;
    }

    public String getReasonDetailed() {
        return reasonDetailed;
    }

    public boolean isSuccessful() {
        return successful;
    }

    public Date getCompletionDate() {
        return completionDate;
    }

    public long getFeedfileId() {
        return feedfileId;
    }

    public int getFeedfileType() {
        return feedfileType;
    }

    public boolean isDone() {
        return done;
    }

    public boolean isCancelled() {
        return cancelled;
    }

    public void setSuccessful() {
        this.successful = true;
        this.reason = DownloadError.SUCCESS;
        this.done = true;
    }

    public void setFailed(DownloadError reason, String reasonDetailed) {
        this.successful = false;
        this.reason = reason;
        this.reasonDetailed = reasonDetailed;
    }

    public void setCancelled() {
        this.successful = false;
        this.reason = DownloadError.ERROR_DOWNLOAD_CANCELLED;
        this.done = true;
        this.cancelled = true;
    }

    public void setCompletionDate(Date completionDate) {
        this.completionDate = completionDate;
    }

    public void setId(long id) {
        this.id = id;
    }
}