-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathBackgroundDownload.java
More file actions
409 lines (342 loc) · 16.3 KB
/
Copy pathBackgroundDownload.java
File metadata and controls
409 lines (342 loc) · 16.3 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 org.apache.cordova.backgroundDownload;
import java.io.File;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
/**
* Based on DownloadManager which is intended to be used for long-running HTTP downloads. Support of Android 2.3. (API 9) and later
* http://developer.android.com/reference/android/app/DownloadManager.html TODO: concurrent downloads support
*/
public class BackgroundDownload extends CordovaPlugin {
private static final long DOWNLOAD_ID_UNDEFINED = -1;
private static final String TEMP_DOWNLOAD_FILE_EXTENSION = ".temp";
private static final long DOWNLOAD_PROGRESS_UPDATE_TIMEOUT = 1000;
protected class Download {
private String filePath;
private String tempFilePath;
private String uriString;
private CallbackContext callbackContext; // The callback context from which we were invoked.
private CallbackContext callbackContextDownloadStart; // The callback context from which we started file download command.
private long downloadId = DOWNLOAD_ID_UNDEFINED;
private Timer timerProgressUpdate = null;
public Download(String uriString, String filePath,
CallbackContext callbackContext) {
this.setUriString(uriString);
this.setFilePath(filePath);
this.setTempFilePath(filePath + TEMP_DOWNLOAD_FILE_EXTENSION);
this.setCallbackContext(callbackContext);
this.setCallbackContextDownloadStart(callbackContext);
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getUriString() {
return uriString;
}
public void setUriString(String uriString) {
this.uriString = uriString;
}
public String getTempFilePath() {
return tempFilePath;
}
public void setTempFilePath(String tempFilePath) {
this.tempFilePath = tempFilePath;
}
public CallbackContext getCallbackContext() {
return callbackContext;
}
public void setCallbackContext(CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}
public CallbackContext getCallbackContextDownloadStart() {
return callbackContextDownloadStart;
}
public void setCallbackContextDownloadStart(
CallbackContext callbackContextDownloadStart) {
this.callbackContextDownloadStart = callbackContextDownloadStart;
}
public long getDownloadId() {
return downloadId;
}
public void setDownloadId(long downloadId) {
this.downloadId = downloadId;
}
public Timer getTimerProgressUpdate() {
return timerProgressUpdate;
}
public void setTimerProgressUpdate(Timer TimerProgressUpdate) {
this.timerProgressUpdate = TimerProgressUpdate;
};
}
HashMap<String, Download> activDownloads = new HashMap<String, Download>();
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
if (action.equals("startAsync")) {
startAsync(args, callbackContext);
return true;
}
if (action.equals("stop")) {
stop(args, callbackContext);
return true;
}
return false; // invalid action
} catch (Exception ex) {
callbackContext.error(ex.getMessage());
}
return true;
}
private void startAsync(JSONArray args, CallbackContext callbackContext) throws JSONException {
if (activDownloads.size() == 0) {
// required to receive notification when download is completed
cordova.getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
Download curDownload = new Download(args.get(0).toString(), args.get(1).toString(), callbackContext);
if (activDownloads.containsKey(curDownload.getUriString())) {
return;
}
activDownloads.put(curDownload.getUriString(), curDownload);
Uri source = Uri.parse(curDownload.getUriString());
// Uri destination = Uri.parse(this.getTemporaryFilePath());
// attempt to attach to active download for this file (download started and we close/open the app)
curDownload.setDownloadId(findActiveDownload(curDownload.getUriString()));
if (curDownload.getDownloadId() == DOWNLOAD_ID_UNDEFINED) {
// make sure file does not exist, in other case DownloadManager will fail
File targetFile = new File(Uri.parse(curDownload.getTempFilePath()).getPath());
targetFile.delete();
DownloadManager mgr = (DownloadManager) this.cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(source);
request.setTitle("org.apache.cordova.backgroundDownload plugin");
request.setVisibleInDownloadsUi(false);
// hide notification. Not compatible with current android api.
// request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
// we use default settings for roaming and network type
// request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
// request.setAllowedOverRoaming(false);
request.setDestinationUri(Uri.parse(curDownload.getTempFilePath()));
curDownload.setDownloadId(mgr.enqueue(request));
} else if (checkDownloadCompleted(curDownload.getDownloadId())) {
return;
}
// custom logic to track file download progress
StartProgressTracking(curDownload);
}
private void StartProgressTracking(final Download curDownload) {
// already started
if (curDownload.getTimerProgressUpdate() != null) {
return;
}
final DownloadManager mgr = (DownloadManager) this.cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
curDownload.setTimerProgressUpdate(new Timer());
curDownload.getTimerProgressUpdate().schedule(new TimerTask() {
@Override
public void run() {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(curDownload.getDownloadId());
Cursor cursor = mgr.query(q);
if (cursor.moveToFirst()) {
long bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
long bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (bytesTotal != -1) {
try {
JSONObject jsonProgress = new JSONObject();
jsonProgress.put("bytesReceived", bytesDownloaded);
jsonProgress.put("totalBytesToReceive", bytesTotal);
JSONObject obj = new JSONObject();
obj.put("progress", jsonProgress);
PluginResult progressUpdate = new PluginResult(PluginResult.Status.OK, obj);
progressUpdate.setKeepCallback(true);
curDownload.getCallbackContextDownloadStart().sendPluginResult(progressUpdate);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
cursor.close();
}
}, DOWNLOAD_PROGRESS_UPDATE_TIMEOUT, DOWNLOAD_PROGRESS_UPDATE_TIMEOUT);
}
private void CleanUp(Download curDownload) {
if (curDownload.getTimerProgressUpdate() != null) {
curDownload.getTimerProgressUpdate().cancel();
}
if (curDownload.getDownloadId() != DOWNLOAD_ID_UNDEFINED) {
DownloadManager mgr = (DownloadManager) cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
mgr.remove(curDownload.getDownloadId());
}
activDownloads.remove(curDownload.getUriString());
if (activDownloads.size() == 0) {
try {
cordova.getActivity().unregisterReceiver(receiver);
} catch (IllegalArgumentException e) {
// this is fine, receiver was not registered
}
}
}
private String getUserFriendlyReason(int reason) {
String failedReason = "";
switch (reason) {
case DownloadManager.ERROR_CANNOT_RESUME:
failedReason = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
failedReason = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
failedReason = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
failedReason = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
failedReason = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
failedReason = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
failedReason = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
failedReason = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
failedReason = "ERROR_UNKNOWN";
break;
}
return failedReason;
}
private void stop(JSONArray args, CallbackContext callbackContext) throws JSONException {
Download curDownload = activDownloads.get(args.get(0).toString());
if (curDownload == null) {
callbackContext.error("download requst not found");
return;
}
DownloadManager mgr = (DownloadManager) cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
mgr.remove(curDownload.getDownloadId());
callbackContext.success();
}
private long findActiveDownload(String uri) {
DownloadManager mgr = (DownloadManager) cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = DOWNLOAD_ID_UNDEFINED;
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_SUCCESSFUL);
Cursor cur = mgr.query(query);
int idxId = cur.getColumnIndex(DownloadManager.COLUMN_ID);
int idxUri = cur.getColumnIndex(DownloadManager.COLUMN_URI);
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
if (uri.equals(cur.getString(idxUri))) {
downloadId = cur.getLong(idxId);
break;
}
}
cur.close();
return downloadId;
}
private Boolean checkDownloadCompleted(long id) {
DownloadManager mgr = (DownloadManager) this.cordova.getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(id);
Cursor cur = mgr.query(query);
int idxStatus = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
int idxURI = cur.getColumnIndex(DownloadManager.COLUMN_URI);
if (cur.moveToFirst()) {
int status = cur.getInt(idxStatus);
String uri = cur.getString(idxURI);
Download curDownload = activDownloads.get(uri);
if (status == DownloadManager.STATUS_SUCCESSFUL) { // TODO review what else we can have here
copyTempFileToActualFile(curDownload);
CleanUp(curDownload);
return true;
}
}
cur.close();
return false;
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor cursor = mgr.query(query);
int idxURI = cursor.getColumnIndex(DownloadManager.COLUMN_URI);
cursor.moveToFirst();
String uri;
try {
uri = cursor.getString(idxURI);
} catch (CursorIndexOutOfBoundsException e) {
// Already removed from the list / list is empty due to cancellation
// Can't do anything else since there's no DL to get
// context from.
return;
}
Download curDownload = activDownloads.get(uri);
try {
long receivedID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
query.setFilterById(receivedID);
int idxStatus = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int idxReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
if (cursor.moveToFirst()) {
int status = cursor.getInt(idxStatus);
int reason = cursor.getInt(idxReason);
if (status == DownloadManager.STATUS_SUCCESSFUL) {
copyTempFileToActualFile(curDownload);
} else {
curDownload.getCallbackContextDownloadStart().error("Download operation failed with status " + status + " and reason: " + getUserFriendlyReason(reason));
}
} else {
curDownload.getCallbackContextDownloadStart().error("cancelled or terminated");
}
cursor.close();
} catch (Exception ex) {
curDownload.getCallbackContextDownloadStart().error(ex.getMessage());
} finally {
CleanUp(curDownload);
}
}
};
public void copyTempFileToActualFile(Download curDownload) {
File sourceFile = new File(Uri.parse(curDownload.getTempFilePath()).getPath());
File destFile = new File(Uri.parse(curDownload.getFilePath()).getPath());
if (sourceFile.renameTo(destFile)) {
curDownload.getCallbackContextDownloadStart().success();
} else {
curDownload.getCallbackContextDownloadStart().error("Cannot copy from temporary path to actual path");
}
}
}