DownloadInfo.java revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.content.browser;
6
7/**
8 * Class representing the state of a single download.
9 */
10public final class DownloadInfo {
11    private final String mUrl;
12    private final String mUserAgent;
13    private final String mMimeType;
14    private final String mCookie;
15    private final String mFileName;
16    private final String mDescription;
17    private final String mFilePath;
18    private final String mReferer;
19    private final long mContentLength;
20    private final boolean mHasDownloadId;
21    private final int mDownloadId;
22    private final boolean mHasUserGesture;
23    private final String mContentDisposition;
24    private final boolean mIsGETRequest;
25    private final boolean mIsSuccessful;
26    private final int mPercentCompleted;
27    private final long mTimeRemainingInMillis;
28
29    private DownloadInfo(Builder builder) {
30        mUrl = builder.mUrl;
31        mUserAgent = builder.mUserAgent;
32        mMimeType = builder.mMimeType;
33        mCookie = builder.mCookie;
34        mFileName = builder.mFileName;
35        mDescription = builder.mDescription;
36        mFilePath = builder.mFilePath;
37        mReferer = builder.mReferer;
38        mContentLength = builder.mContentLength;
39        mHasDownloadId = builder.mHasDownloadId;
40        mDownloadId = builder.mDownloadId;
41        mHasUserGesture = builder.mHasUserGesture;
42        mIsSuccessful = builder.mIsSuccessful;
43        mIsGETRequest = builder.mIsGETRequest;
44        mContentDisposition = builder.mContentDisposition;
45        mPercentCompleted = builder.mPercentCompleted;
46        mTimeRemainingInMillis = builder.mTimeRemainingInMillis;
47    }
48
49    public String getUrl() {
50        return mUrl;
51    }
52
53    public String getUserAgent() {
54        return mUserAgent;
55    }
56
57    public String getMimeType() {
58        return mMimeType;
59    }
60
61    public String getCookie() {
62        return mCookie;
63    }
64
65    public String getFileName() {
66        return mFileName;
67    }
68
69    public String getDescription() {
70        return mDescription;
71    }
72
73    public String getFilePath() {
74        return mFilePath;
75    }
76
77    public String getReferer() {
78        return mReferer;
79    }
80
81    public long getContentLength() {
82        return mContentLength;
83    }
84
85    public boolean isGETRequest() {
86        return mIsGETRequest;
87    }
88
89    public boolean hasDownloadId() {
90        return mHasDownloadId;
91    }
92
93    public int getDownloadId() {
94        return mDownloadId;
95    }
96
97    public boolean hasUserGesture() {
98        return mHasUserGesture;
99    }
100
101    public boolean isSuccessful() {
102        return mIsSuccessful;
103    }
104
105    public String getContentDisposition() {
106        return mContentDisposition;
107    }
108
109    /**
110     * @return percent completed as an integer, -1 if there is no download progress.
111     */
112    public int getPercentCompleted() {
113        return mPercentCompleted;
114    }
115
116    public long getTimeRemainingInMillis() {
117        return mTimeRemainingInMillis;
118    }
119
120    public static class Builder {
121        private String mUrl;
122        private String mUserAgent;
123        private String mMimeType;
124        private String mCookie;
125        private String mFileName;
126        private String mDescription;
127        private String mFilePath;
128        private String mReferer;
129        private long mContentLength;
130        private boolean mIsGETRequest;
131        private boolean mHasDownloadId;
132        private int mDownloadId;
133        private boolean mHasUserGesture;
134        private boolean mIsSuccessful;
135        private String mContentDisposition;
136        private int mPercentCompleted = -1;
137        private long mTimeRemainingInMillis;
138
139        public Builder setUrl(String url) {
140            mUrl = url;
141            return this;
142        }
143
144        public Builder setUserAgent(String userAgent) {
145            mUserAgent = userAgent;
146            return this;
147        }
148
149        public Builder setMimeType(String mimeType) {
150            mMimeType = mimeType;
151            return this;
152        }
153
154        public Builder setCookie(String cookie) {
155            mCookie = cookie;
156            return this;
157        }
158
159        public Builder setFileName(String fileName) {
160            mFileName = fileName;
161            return this;
162        }
163
164        public Builder setDescription(String description) {
165            mDescription = description;
166            return this;
167        }
168
169        public Builder setFilePath(String filePath) {
170            mFilePath = filePath;
171            return this;
172        }
173
174        public Builder setReferer(String referer) {
175            mReferer = referer;
176            return this;
177        }
178
179        public Builder setContentLength(long contentLength) {
180            mContentLength = contentLength;
181            return this;
182        }
183
184        public Builder setIsGETRequest(boolean isGETRequest) {
185            mIsGETRequest = isGETRequest;
186            return this;
187        }
188
189        public Builder setHasDownloadId(boolean hasDownloadId) {
190            mHasDownloadId = hasDownloadId;
191            return this;
192        }
193
194        public Builder setDownloadId(int downloadId) {
195            mDownloadId = downloadId;
196            return this;
197        }
198
199        public Builder setHasUserGesture(boolean hasUserGesture) {
200            mHasUserGesture = hasUserGesture;
201            return this;
202        }
203
204        public Builder setIsSuccessful(boolean isSuccessful) {
205            mIsSuccessful = isSuccessful;
206            return this;
207        }
208
209        public Builder setContentDisposition(String contentDisposition) {
210            mContentDisposition = contentDisposition;
211            return this;
212        }
213
214        public Builder setPercentCompleted(int percentCompleted) {
215            assert percentCompleted <= 100;
216            mPercentCompleted = percentCompleted;
217            return this;
218        }
219
220        public Builder setTimeRemainingInMillis(long timeRemainingInMillis) {
221            mTimeRemainingInMillis = timeRemainingInMillis;
222            return this;
223        }
224
225        public DownloadInfo build() {
226            return new DownloadInfo(this);
227        }
228
229        /**
230         * Create a builder from the DownloadInfo object.
231         * @param downloadInfo DownloadInfo object from which builder fields are populated.
232         * @return A builder initialized with fields from downloadInfo object.
233         */
234        public static Builder fromDownloadInfo(final DownloadInfo downloadInfo) {
235            Builder builder = new Builder();
236            builder
237                    .setUrl(downloadInfo.getUrl())
238                    .setUserAgent(downloadInfo.getUserAgent())
239                    .setMimeType(downloadInfo.getMimeType())
240                    .setCookie(downloadInfo.getCookie())
241                    .setFileName(downloadInfo.getFileName())
242                    .setDescription(downloadInfo.getDescription())
243                    .setFilePath(downloadInfo.getFilePath())
244                    .setReferer(downloadInfo.getReferer())
245                    .setContentLength(downloadInfo.getContentLength())
246                    .setHasDownloadId(downloadInfo.hasDownloadId())
247                    .setDownloadId(downloadInfo.getDownloadId())
248                    .setHasUserGesture(downloadInfo.hasUserGesture())
249                    .setContentDisposition(downloadInfo.getContentDisposition())
250                    .setIsGETRequest(downloadInfo.isGETRequest())
251                    .setIsSuccessful(downloadInfo.isSuccessful())
252                    .setPercentCompleted(downloadInfo.getPercentCompleted())
253                    .setTimeRemainingInMillis(downloadInfo.getTimeRemainingInMillis());
254            return builder;
255        }
256
257    }
258}
259