Revision 7b773f94 F-Droid/src/org/fdroid/fdroid/net/AsyncDownloaderFromAndroid.java
F-Droid/src/org/fdroid/fdroid/net/AsyncDownloaderFromAndroid.java | ||
---|---|---|
28 | 28 |
private final DownloadManager dm; |
29 | 29 |
private File localFile; |
30 | 30 |
private String remoteAddress; |
31 |
private String appName;
|
|
32 |
private String appId;
|
|
31 |
private String downloadTitle;
|
|
32 |
private String uniqueDownloadId;
|
|
33 | 33 |
private Listener listener; |
34 | 34 |
|
35 |
private long downloadId = -1; |
|
35 |
private long downloadManagerId = -1;
|
|
36 | 36 |
|
37 | 37 |
/** |
38 | 38 |
* Normally the listener would be provided using a setListener method. |
... | ... | |
41 | 41 |
* world about completion. Therefore, we require the listener as a |
42 | 42 |
* parameter to the constructor. |
43 | 43 |
*/ |
44 |
public AsyncDownloaderFromAndroid(Context context, Listener listener, String appName, String appId, String remoteAddress, File localFile) {
|
|
44 |
public AsyncDownloaderFromAndroid(Context context, Listener listener, String downloadTitle, String downloadId, String remoteAddress, File localFile) {
|
|
45 | 45 |
this.context = context; |
46 |
this.appName = appName;
|
|
47 |
this.appId = appId;
|
|
46 |
this.downloadTitle = downloadTitle;
|
|
47 |
this.uniqueDownloadId = downloadId;
|
|
48 | 48 |
this.remoteAddress = remoteAddress; |
49 | 49 |
this.listener = listener; |
50 | 50 |
this.localFile = localFile; |
51 | 51 |
|
52 |
if (appName == null || appName.trim().length() == 0) {
|
|
53 |
this.appName = remoteAddress;
|
|
52 |
if (downloadTitle == null || downloadTitle.trim().length() == 0) {
|
|
53 |
this.downloadTitle = remoteAddress;
|
|
54 | 54 |
} |
55 | 55 |
|
56 | 56 |
dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |
... | ... | |
59 | 59 |
@Override |
60 | 60 |
public void download() { |
61 | 61 |
// Check if the download is complete |
62 |
if ((downloadId = isDownloadComplete(context, appId)) > 0) {
|
|
62 |
if ((downloadManagerId = isDownloadComplete(context, uniqueDownloadId)) > 0) {
|
|
63 | 63 |
// clear the notification |
64 |
dm.remove(downloadId); |
|
64 |
dm.remove(downloadManagerId);
|
|
65 | 65 |
|
66 | 66 |
try { |
67 | 67 |
// write the downloaded file to the expected location |
68 |
ParcelFileDescriptor fd = dm.openDownloadedFile(downloadId); |
|
68 |
ParcelFileDescriptor fd = dm.openDownloadedFile(downloadManagerId);
|
|
69 | 69 |
copyFile(fd.getFileDescriptor(), localFile); |
70 | 70 |
listener.onDownloadComplete(); |
71 | 71 |
} catch (IOException e) { |
... | ... | |
75 | 75 |
} |
76 | 76 |
|
77 | 77 |
// Check if the download is still in progress |
78 |
if (downloadId < 0) { |
|
79 |
downloadId = isDownloading(context, appId);
|
|
78 |
if (downloadManagerId < 0) {
|
|
79 |
downloadManagerId = isDownloading(context, uniqueDownloadId);
|
|
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
// Start a new download |
83 |
if (downloadId < 0) { |
|
83 |
if (downloadManagerId < 0) {
|
|
84 | 84 |
// set up download request |
85 | 85 |
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(remoteAddress)); |
86 |
request.setTitle(appName);
|
|
87 |
request.setDescription(appId); // we will retrieve this later from the description field
|
|
88 |
this.downloadId = dm.enqueue(request); |
|
86 |
request.setTitle(downloadTitle);
|
|
87 |
request.setDescription(uniqueDownloadId); // we will retrieve this later from the description field
|
|
88 |
this.downloadManagerId = dm.enqueue(request);
|
|
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
context.registerReceiver(receiver, |
... | ... | |
114 | 114 |
|
115 | 115 |
@Override |
116 | 116 |
public int getBytesRead() { |
117 |
if (downloadId < 0) return 0; |
|
117 |
if (downloadManagerId < 0) return 0;
|
|
118 | 118 |
|
119 | 119 |
DownloadManager.Query query = new DownloadManager.Query(); |
120 |
query.setFilterById(downloadId); |
|
120 |
query.setFilterById(downloadManagerId);
|
|
121 | 121 |
Cursor c = dm.query(query); |
122 | 122 |
|
123 | 123 |
try { |
124 | 124 |
if (c.moveToFirst()) { |
125 |
// we use the description column to store the app id
|
|
125 |
// we use the description column to store the unique id of this download
|
|
126 | 126 |
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR); |
127 | 127 |
return c.getInt(columnIndex); |
128 | 128 |
} |
... | ... | |
135 | 135 |
|
136 | 136 |
@Override |
137 | 137 |
public int getTotalBytes() { |
138 |
if (downloadId < 0) return 0; |
|
138 |
if (downloadManagerId < 0) return 0;
|
|
139 | 139 |
|
140 | 140 |
DownloadManager.Query query = new DownloadManager.Query(); |
141 |
query.setFilterById(downloadId); |
|
141 |
query.setFilterById(downloadManagerId);
|
|
142 | 142 |
Cursor c = dm.query(query); |
143 | 143 |
|
144 | 144 |
try { |
145 | 145 |
if (c.moveToFirst()) { |
146 |
// we use the description column to store the app id
|
|
146 |
// we use the description column to store the unique id for this download
|
|
147 | 147 |
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES); |
148 | 148 |
return c.getInt(columnIndex); |
149 | 149 |
} |
... | ... | |
162 | 162 |
// ignore if receiver already unregistered |
163 | 163 |
} |
164 | 164 |
|
165 |
if (userRequested && downloadId >= 0) { |
|
166 |
dm.remove(downloadId); |
|
165 |
if (userRequested && downloadManagerId >= 0) {
|
|
166 |
dm.remove(downloadManagerId);
|
|
167 | 167 |
} |
168 | 168 |
} |
169 | 169 |
|
170 | 170 |
/** |
171 |
* Extract the appId from a given download id.
|
|
172 |
* @return - appId or null if not found
|
|
171 |
* Extract the uniqueDownloadId from a given download id.
|
|
172 |
* @return - uniqueDownloadId or null if not found
|
|
173 | 173 |
*/ |
174 |
public static String getAppId(Context context, long downloadId) {
|
|
174 |
public static String getDownloadId(Context context, long downloadId) {
|
|
175 | 175 |
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |
176 | 176 |
DownloadManager.Query query = new DownloadManager.Query(); |
177 | 177 |
query.setFilterById(downloadId); |
... | ... | |
179 | 179 |
|
180 | 180 |
try { |
181 | 181 |
if (c.moveToFirst()) { |
182 |
// we use the description column to store the app id
|
|
182 |
// we use the description column to store the unique id for this download
|
|
183 | 183 |
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION); |
184 | 184 |
return c.getString(columnIndex); |
185 | 185 |
} |
... | ... | |
202 | 202 |
|
203 | 203 |
try { |
204 | 204 |
if (c.moveToFirst()) { |
205 |
// we use the description column to store the app id |
|
206 | 205 |
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_TITLE); |
207 | 206 |
return c.getString(columnIndex); |
208 | 207 |
} |
... | ... | |
214 | 213 |
} |
215 | 214 |
|
216 | 215 |
/** |
217 |
* Get the downloadId from an Intent sent by the DownloadManagerReceiver |
|
216 |
* Get the downloadManagerId from an Intent sent by the DownloadManagerReceiver
|
|
218 | 217 |
*/ |
219 | 218 |
public static long getDownloadId(Intent intent) { |
220 | 219 |
if (intent != null) { |
221 | 220 |
if (intent.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID)) { |
222 |
// we have been passed a DownloadManager download id, so get the app id for it
|
|
221 |
// we have been passed a DownloadManager download id, so get the unique id for that download
|
|
223 | 222 |
return intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); |
224 | 223 |
} |
225 | 224 |
|
... | ... | |
236 | 235 |
} |
237 | 236 |
|
238 | 237 |
/** |
239 |
* Check if a download is running for the app
|
|
240 |
* @return -1 if not downloading, else the downloadId
|
|
238 |
* Check if a download is running for the specified id
|
|
239 |
* @return -1 if not downloading, else the id from the Android download manager
|
|
241 | 240 |
*/ |
242 |
public static long isDownloading(Context context, String appId) {
|
|
241 |
public static long isDownloading(Context context, String uniqueDownloadId) {
|
|
243 | 242 |
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |
244 | 243 |
DownloadManager.Query query = new DownloadManager.Query(); |
245 | 244 |
Cursor c = dm.query(query); |
246 |
int columnAppId = c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION);
|
|
245 |
int columnUniqueDownloadId = c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION);
|
|
247 | 246 |
int columnId = c.getColumnIndex(DownloadManager.COLUMN_ID); |
248 | 247 |
|
249 | 248 |
try { |
250 | 249 |
while (c.moveToNext()) { |
251 |
if (appId.equals(c.getString(columnAppId))) {
|
|
250 |
if (uniqueDownloadId.equals(c.getString(columnUniqueDownloadId))) {
|
|
252 | 251 |
return c.getLong(columnId); |
253 | 252 |
} |
254 | 253 |
} |
... | ... | |
260 | 259 |
} |
261 | 260 |
|
262 | 261 |
/** |
263 |
* Check if a download for an app is complete.
|
|
262 |
* Check if a specific download is complete.
|
|
264 | 263 |
* @return -1 if download is not complete, otherwise the download id |
265 | 264 |
*/ |
266 |
public static long isDownloadComplete(Context context, String appId) {
|
|
265 |
public static long isDownloadComplete(Context context, String uniqueDownloadId) {
|
|
267 | 266 |
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |
268 | 267 |
DownloadManager.Query query = new DownloadManager.Query(); |
269 | 268 |
query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL); |
270 | 269 |
Cursor c = dm.query(query); |
271 |
int columnAppId = c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION);
|
|
270 |
int columnUniqueDownloadId = c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION);
|
|
272 | 271 |
int columnId = c.getColumnIndex(DownloadManager.COLUMN_ID); |
273 | 272 |
|
274 | 273 |
try { |
275 | 274 |
while (c.moveToNext()) { |
276 |
if (appId.equals(c.getString(columnAppId))) {
|
|
275 |
if (uniqueDownloadId.equals(c.getString(columnUniqueDownloadId))) {
|
|
277 | 276 |
return c.getLong(columnId); |
278 | 277 |
} |
279 | 278 |
} |
... | ... | |
292 | 291 |
public void onReceive(Context context, Intent intent) { |
293 | 292 |
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) { |
294 | 293 |
long dId = getDownloadId(intent); |
295 |
String appId = getAppId(context, dId);
|
|
296 |
if (listener != null && dId == downloadId && appId != null) {
|
|
294 |
String downloadId = getDownloadId(context, dId);
|
|
295 |
if (listener != null && dId == AsyncDownloaderFromAndroid.this.downloadManagerId && downloadId != null) {
|
|
297 | 296 |
// our current download has just completed, so let's throw up install dialog |
298 | 297 |
// immediately |
299 | 298 |
try { |
Also available in: Unified diff