-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathDownloadWorker.java
872 lines (767 loc) · 39.6 KB
/
DownloadWorker.java
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
package vn.hunghd.flutterdownloader;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.RequiresApi;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
//import java.net.HttpURLConnection;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.security.cert.X509Certificate;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterCallbackInformation;
public class DownloadWorker extends Worker implements MethodChannel.MethodCallHandler {
public static final String ARG_URL = "url";
public static final String ARG_FILE_NAME = "file_name";
public static final String ARG_SAVED_DIR = "saved_file";
public static final String ARG_HEADERS = "headers";
public static final String ARG_IS_RESUME = "is_resume";
public static final String ARG_SHOW_NOTIFICATION = "show_notification";
public static final String ARG_OPEN_FILE_FROM_NOTIFICATION = "open_file_from_notification";
public static final String ARG_NOTIFICATION_TITLE = "notification_title";
public static final String ARG_CALLBACK_HANDLE = "callback_handle";
public static final String ARG_DEBUG = "debug";
public static final String ARG_STEP = "step";
public static final String ARG_SAVE_IN_PUBLIC_STORAGE = "save_in_public_storage";
public static final String ARG_IGNORESSL = "ignoreSsl";
private static final String TAG = DownloadWorker.class.getSimpleName();
private static final int BUFFER_SIZE = 4096;
private static final String CHANNEL_ID = "FLUTTER_DOWNLOADER_NOTIFICATION";
private static final AtomicBoolean isolateStarted = new AtomicBoolean(false);
private static final ArrayDeque<List<Object>> isolateQueue = new ArrayDeque<>();
private static FlutterEngine backgroundFlutterEngine;
private final Pattern charsetPattern = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)");
private final Pattern filenameStarPattern = Pattern.compile("(?i)\\bfilename\\*=([^']+)'([^']*)'\"?([^\"]+)\"?");
private final Pattern filenamePattern = Pattern.compile("(?i)\\bfilename=\"?([^\"]+)\"?");
private MethodChannel backgroundChannel;
private TaskDbHelper dbHelper;
private TaskDao taskDao;
private boolean showNotification;
private boolean clickToOpenDownloadedFile;
private boolean debug;
private boolean ignoreSsl;
private int lastProgress = 0;
private int primaryId;
private String msgStarted, msgInProgress, msgCanceled, msgFailed, msgPaused, msgComplete;
private long lastCallUpdateNotification = 0;
private int step;
private boolean saveInPublicStorage;
public DownloadWorker(@NonNull final Context context, @NonNull WorkerParameters params) {
super(context, params);
new Handler(context.getMainLooper()).post(() -> startBackgroundIsolate(context));
}
private void startBackgroundIsolate(Context context) {
synchronized (isolateStarted) {
if (backgroundFlutterEngine == null) {
SharedPreferences pref = context.getSharedPreferences(FlutterDownloaderPlugin.SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE);
long callbackHandle = pref.getLong(FlutterDownloaderPlugin.CALLBACK_DISPATCHER_HANDLE_KEY, 0);
backgroundFlutterEngine = new FlutterEngine(getApplicationContext(), null, false);
// We need to create an instance of `FlutterEngine` before looking up the
// callback. If we don't, the callback cache won't be initialized and the
// lookup will fail.
FlutterCallbackInformation flutterCallback =
FlutterCallbackInformation.lookupCallbackInformation(callbackHandle);
if (flutterCallback == null) {
log("Fatal: failed to find callback");
return;
}
final String appBundlePath = FlutterInjector.instance().flutterLoader().findAppBundlePath();
final AssetManager assets = getApplicationContext().getAssets();
backgroundFlutterEngine.getDartExecutor().executeDartCallback(new DartExecutor.DartCallback(assets, appBundlePath, flutterCallback));
}
}
backgroundChannel = new MethodChannel(backgroundFlutterEngine.getDartExecutor(), "vn.hunghd/downloader_background");
backgroundChannel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(MethodCall call, @NonNull MethodChannel.Result result) {
if (call.method.equals("didInitializeDispatcher")) {
synchronized (isolateStarted) {
while (!isolateQueue.isEmpty()) {
backgroundChannel.invokeMethod("", isolateQueue.remove());
}
isolateStarted.set(true);
result.success(null);
}
} else {
result.notImplemented();
}
}
@Override
public void onStopped() {
Context context = getApplicationContext();
dbHelper = TaskDbHelper.getInstance(context);
taskDao = new TaskDao(dbHelper);
String url = getInputData().getString(ARG_URL);
String filename = getInputData().getString(ARG_FILE_NAME);
String notificationTitle = getInputData().getString(ARG_NOTIFICATION_TITLE);
DownloadTask task = taskDao.loadTask(getId().toString());
if (task != null && task.status == DownloadStatus.ENQUEUED) {
updateNotification(context, filename == null ? url : filename, DownloadStatus.CANCELED, -1, null, true, notificationTitle);
taskDao.updateTask(getId().toString(), DownloadStatus.CANCELED, lastProgress);
}
}
@NonNull
@Override
public Result doWork() {
Context context = getApplicationContext();
dbHelper = TaskDbHelper.getInstance(context);
taskDao = new TaskDao(dbHelper);
String url = getInputData().getString(ARG_URL);
String filename = getInputData().getString(ARG_FILE_NAME);
String savedDir = getInputData().getString(ARG_SAVED_DIR);
String headers = getInputData().getString(ARG_HEADERS);
boolean isResume = getInputData().getBoolean(ARG_IS_RESUME, false);
debug = getInputData().getBoolean(ARG_DEBUG, false);
step = getInputData().getInt(ARG_STEP, 10);
ignoreSsl = getInputData().getBoolean(ARG_IGNORESSL, false);
Resources res = getApplicationContext().getResources();
msgStarted = res.getString(R.string.flutter_downloader_notification_started);
msgInProgress = res.getString(R.string.flutter_downloader_notification_in_progress);
msgCanceled = res.getString(R.string.flutter_downloader_notification_canceled);
msgFailed = res.getString(R.string.flutter_downloader_notification_failed);
msgPaused = res.getString(R.string.flutter_downloader_notification_paused);
msgComplete = res.getString(R.string.flutter_downloader_notification_complete);
DownloadTask task = taskDao.loadTask(getId().toString());
log("DownloadWorker{url=" + url + ",filename=" + filename + ",savedDir=" + savedDir + ",header=" + headers + ",isResume=" + isResume + ",status=" + (task != null ? task.status : "GONE"));
// Task has been deleted or cancelled
if (task == null || task.status == DownloadStatus.CANCELED) {
return Result.success();
}
showNotification = getInputData().getBoolean(ARG_SHOW_NOTIFICATION, false);
clickToOpenDownloadedFile = getInputData().getBoolean(ARG_OPEN_FILE_FROM_NOTIFICATION, false);
String notificationTitle = getInputData().getString(ARG_NOTIFICATION_TITLE);
saveInPublicStorage = getInputData().getBoolean(ARG_SAVE_IN_PUBLIC_STORAGE, false);
primaryId = task.primaryId;
setupNotification(context);
updateNotification(context, filename == null ? url : filename, DownloadStatus.RUNNING, task.progress, null, false, notificationTitle);
taskDao.updateTask(getId().toString(), DownloadStatus.RUNNING, task.progress);
//automatic resume for partial files. (if the workmanager unexpectedly quited in background)
String saveFilePath = savedDir + File.separator + filename;
File partialFile = new File(saveFilePath);
if (partialFile.exists()) {
isResume = true;
log("exists file for " + filename + "automatic resuming...");
}
try {
downloadFile(context, url, savedDir, filename, notificationTitle, headers, isResume);
cleanUp();
dbHelper = null;
taskDao = null;
return Result.success();
} catch (Exception e) {
updateNotification(context, filename == null ? url : filename, DownloadStatus.FAILED, -1, null, true, notificationTitle);
taskDao.updateTask(getId().toString(), DownloadStatus.FAILED, lastProgress);
e.printStackTrace();
dbHelper = null;
taskDao = null;
return Result.failure();
}
}
private void setupHeaders(HttpURLConnection conn, String headers) {
if (!TextUtils.isEmpty(headers)) {
log("Headers = " + headers);
try {
JSONObject json = new JSONObject(headers);
for (Iterator<String> it = json.keys(); it.hasNext();) {
String key = it.next();
conn.setRequestProperty(key, json.getString(key));
}
conn.setDoInput(true);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private long setupPartialDownloadedDataHeader(HttpURLConnection conn, String filename, String savedDir) {
String saveFilePath = savedDir + File.separator + filename;
File partialFile = new File(saveFilePath);
long downloadedBytes = partialFile.length();
log("Resume download: Range: bytes=" + downloadedBytes + "-");
conn.setRequestProperty("Accept-Encoding", "identity");
conn.setRequestProperty("Range", "bytes=" + downloadedBytes + "-");
conn.setDoInput(true);
return downloadedBytes;
}
private void downloadFile(Context context, String fileURL, String savedDir, String filename, String notificationTitle, String headers, boolean isResume) throws IOException {
String url = fileURL;
URL resourceUrl, base, next;
Map<String, Integer> visited;
HttpURLConnection httpConn = null;
InputStream inputStream = null;
OutputStream outputStream = null;
String location;
long downloadedBytes = 0;
int responseCode;
int times;
visited = new HashMap<>();
try {
// handle redirection logic
while (true) {
if (!visited.containsKey(url)) {
times = 1;
visited.put(url, times);
} else {
times = visited.get(url) + 1;
}
if (times > 3)
throw new IOException("Stuck in redirect loop");
resourceUrl = new URL(url);
if (ignoreSsl) {
trustAllHosts();
if (resourceUrl.getProtocol().toLowerCase().equals("https")) {
HttpsURLConnection https = (HttpsURLConnection) resourceUrl.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
httpConn = https;
} else {
httpConn = (HttpURLConnection) resourceUrl.openConnection();
}
} else {
httpConn = (HttpsURLConnection) resourceUrl.openConnection();
}
log("Open connection to " + url);
httpConn.setConnectTimeout(15000);
httpConn.setReadTimeout(15000);
httpConn.setInstanceFollowRedirects(false); // Make the logic below easier to detect redirections
httpConn.setRequestProperty("User-Agent", "Mozilla/5.0...");
// setup request headers if it is set
setupHeaders(httpConn, headers);
// try to continue downloading a file from its partial downloaded data.
if (isResume) {
downloadedBytes = setupPartialDownloadedDataHeader(httpConn, filename, savedDir);
}
responseCode = httpConn.getResponseCode();
switch (responseCode) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_SEE_OTHER:
case HttpURLConnection.HTTP_MOVED_TEMP:
case 307: /* HTTP_TEMP_REDIRECT */
case 308: /* HTTP_PERM_REDIRECT */
log("Response with redirection code");
location = httpConn.getHeaderField("Location");
log("Location = " + location);
base = new URL(url);
next = new URL(base, location); // Deal with relative URLs
url = next.toExternalForm();
log("New url: " + url);
continue;
}
break;
}
httpConn.connect();
String contentType;
if ((responseCode == HttpURLConnection.HTTP_OK || (isResume && responseCode == HttpURLConnection.HTTP_PARTIAL)) && !isStopped()) {
contentType = httpConn.getContentType();
long contentLength = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N ? httpConn.getContentLengthLong() : httpConn.getContentLength();
log("Content-Type = " + contentType);
log("Content-Length = " + contentLength);
String charset = getCharsetFromContentType(contentType);
log("Charset = " + charset);
if (!isResume) {
// try to extract filename from HTTP headers if it is not given by user
if (filename == null) {
String disposition = httpConn.getHeaderField("Content-Disposition");
log("Content-Disposition = " + disposition);
if (disposition != null && !disposition.isEmpty()) {
filename = getFileNameFromContentDisposition(disposition, charset);
}
if (filename == null || filename.isEmpty()) {
filename = url.substring(url.lastIndexOf("/") + 1);
try {
filename = URLDecoder.decode(filename, "UTF-8");
} catch (IllegalArgumentException e) {
/* ok, just let filename be not encoded */
e.printStackTrace();
}
}
}
}
log("fileName = " + filename);
taskDao.updateTask(getId().toString(), filename, contentType);
// opens input stream from the HTTP connection
inputStream = httpConn.getInputStream();
String savedFilePath;
// opens an output stream to save into file
// there are two case:
if (isResume) {
// 1. continue downloading (append data to partial downloaded file)
savedFilePath = savedDir + File.separator + filename;
outputStream = new FileOutputStream(savedFilePath, true);
} else {
// 2. new download, create new file
// there are two case according to Android SDK version and save path
// From Android 11 onwards, file is only downloaded to app-specific directory (internal storage)
// or public shared download directory (external storage).
// The second option will ignore `savedDir` parameter.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && saveInPublicStorage) {
Uri uri = createFileInPublicDownloadsDir(filename, contentType);
savedFilePath = getMediaStoreEntryPathApi29(uri);
outputStream = context.getContentResolver().openOutputStream(uri, "w");
} else {
File file = createFileInAppSpecificDir(filename, savedDir);
if (file.getName() != filename) {
filename = file.getName();
taskDao.updateTask(getId().toString(), filename, contentType);
}
savedFilePath = file.getPath();
outputStream = new FileOutputStream(file, false);
}
}
long count = downloadedBytes;
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
// using isStopped() to monitor canceling task
while ((bytesRead = inputStream.read(buffer)) != -1 && !isStopped()) {
count += bytesRead;
int progress = (int) ((count * 100) / (contentLength + downloadedBytes));
outputStream.write(buffer, 0, bytesRead);
if ((lastProgress == 0 || progress > (lastProgress + step) || progress == 100)
&& progress != lastProgress) {
lastProgress = progress;
// This line possibly causes system overloaded because of accessing to DB too many ?!!!
// but commenting this line causes tasks loaded from DB missing current downloading progress,
// however, this missing data should be temporary and it will be updated as soon as
// a new bunch of data fetched and a notification sent
taskDao.updateTask(getId().toString(), DownloadStatus.RUNNING, progress);
updateNotification(context, filename, DownloadStatus.RUNNING, progress, null, false, notificationTitle);
}
}
DownloadTask task = taskDao.loadTask(getId().toString());
int progress = isStopped() && task.resumable ? lastProgress : 100;
int status = isStopped() ? (task.resumable ? DownloadStatus.PAUSED : DownloadStatus.CANCELED) : DownloadStatus.COMPLETE;
int storage = ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
PendingIntent pendingIntent = null;
if (status == DownloadStatus.COMPLETE) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
if (isImageOrVideoFile(contentType) && isExternalStoragePath(savedFilePath)) {
addImageOrVideoToGallery(filename, savedFilePath, getContentTypeWithoutCharset(contentType));
}
}
if (clickToOpenDownloadedFile) {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && storage != PackageManager.PERMISSION_GRANTED)
return;
Intent intent = IntentUtils.validatedFileIntent(getApplicationContext(), savedFilePath, contentType);
if (intent != null) {
log("Setting an intent to open the file " + savedFilePath);
int flags = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ? PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_CANCEL_CURRENT;
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, flags);
} else {
log("There's no application that can open the file " + savedFilePath);
}
}
}
taskDao.updateTask(getId().toString(), status, progress);
updateNotification(context, filename, status, progress, pendingIntent, true, notificationTitle);
log(isStopped() ? "Download canceled" : "File downloaded");
} else {
DownloadTask task = taskDao.loadTask(getId().toString());
int status = isStopped() ? (task.resumable ? DownloadStatus.PAUSED : DownloadStatus.CANCELED) : DownloadStatus.FAILED;
taskDao.updateTask(getId().toString(), status, lastProgress);
updateNotification(context, filename == null ? fileURL : filename, status, -1, null, true, notificationTitle);
log(isStopped() ? "Download canceled" : "Server replied HTTP code: " + responseCode);
}
} catch (IOException e) {
taskDao.updateTask(getId().toString(), DownloadStatus.FAILED, lastProgress);
updateNotification(context, filename == null ? fileURL : filename, DownloadStatus.FAILED, -1, null, true, notificationTitle);
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream.flush();
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpConn != null) {
httpConn.disconnect();
}
}
}
/**
* Create a file using java.io API
*/
private File createFileInAppSpecificDir(String filename, String savedDir) {
File newFile = new File(savedDir, filename);
try {
int deduplicationFileNumber = 0;
while (newFile.exists()) {
deduplicationFileNumber++;
int fileNameExtensionIndex = filename.lastIndexOf(".");
String fileNameWithoutExtension;
String fileExtension;
if (fileNameExtensionIndex != -1) {
fileNameWithoutExtension = filename.substring(0, fileNameExtensionIndex);
if (fileNameExtensionIndex + 1 < filename.length()) {
fileExtension = "." + filename.substring(fileNameExtensionIndex + 1);
} else if (fileNameExtensionIndex + 1 == filename.length()) {
fileExtension = ".";
} else {
fileExtension = "";
}
} else {
fileNameWithoutExtension = filename;
fileExtension = "";
}
newFile = new File(savedDir,
fileNameWithoutExtension + "(" + deduplicationFileNumber + ")" + fileExtension);
}
boolean rs = newFile.createNewFile();
if (rs) {
return newFile;
} else {
logError("It looks like you are trying to save file in public storage but not setting 'saveInPublicStorage' to 'true'");
}
} catch (IOException e) {
e.printStackTrace();
logError("Create a file using java.io API failed ");
}
return null;
}
/**
* Create a file inside the Download folder using MediaStore API
*/
@RequiresApi(Build.VERSION_CODES.Q)
private Uri createFileInPublicDownloadsDir(String filename, String mimeType) {
Uri collection = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
ContentValues values = new ContentValues();
values.put(MediaStore.Downloads.DISPLAY_NAME, filename);
values.put(MediaStore.Downloads.MIME_TYPE, mimeType);
values.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
ContentResolver contentResolver = getApplicationContext().getContentResolver();
try {
return contentResolver.insert(collection, values);
} catch (Exception e) {
e.printStackTrace();
logError("Create a file using MediaStore API failed.");
}
return null;
}
/**
* Get a path for a MediaStore entry as it's needed when calling MediaScanner
*/
private String getMediaStoreEntryPathApi29(Uri uri) {
try (Cursor cursor = getApplicationContext().getContentResolver().query(
uri,
new String[] { MediaStore.Files.FileColumns.DATA },
null,
null,
null)) {
if (cursor == null)
return null;
if (!cursor.moveToFirst())
return null;
return cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));
} catch (IllegalArgumentException e) {
e.printStackTrace();
logError("Get a path for a MediaStore failed");
return null;
}
}
void scanFilePath(String path, String mimeType, CallbackUri callback) {
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[] { path },
new String[] { mimeType },
(path1, uri) -> callback.invoke(uri));
}
private void cleanUp() {
DownloadTask task = taskDao.loadTask(getId().toString());
if (task != null && task.status != DownloadStatus.COMPLETE && !task.resumable) {
String filename = task.filename;
if (filename == null) {
filename = task.url.substring(task.url.lastIndexOf("/") + 1, task.url.length());
}
// check and delete uncompleted file
String saveFilePath = task.savedDir + File.separator + filename;
File tempFile = new File(saveFilePath);
if (tempFile.exists()) {
tempFile.delete();
}
}
}
private int getNotificationIconRes() {
try {
ApplicationInfo applicationInfo = getApplicationContext().getPackageManager().getApplicationInfo(getApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
int appIconResId = applicationInfo.icon;
return applicationInfo.metaData.getInt("vn.hunghd.flutterdownloader.NOTIFICATION_ICON", appIconResId);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 0;
}
private void setupNotification(Context context) {
if (!showNotification) return;
// Make a channel if necessary
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
Resources res = getApplicationContext().getResources();
String channelName = res.getString(R.string.flutter_downloader_notification_channel_name);
String channelDescription = res.getString(R.string.flutter_downloader_notification_channel_description);
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
channel.setDescription(channelDescription);
channel.setSound(null, null);
// Add the channel
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.createNotificationChannel(channel);
}
}
private void updateNotification(Context context, String title, int status, int progress, PendingIntent intent, boolean finalize, String notificationTitle) {
sendUpdateProcessEvent(status, progress);
// Show the notification
if (showNotification) {
// Create the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID).
setContentTitle(notificationTitle == null ? title : notificationTitle)
.setContentIntent(intent)
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_LOW);
if (status == DownloadStatus.RUNNING) {
if (progress <= 0) {
builder.setContentText(msgStarted)
.setProgress(0, 0, false);
builder.setOngoing(false)
.setSmallIcon(getNotificationIconRes());
} else if (progress < 100) {
builder.setContentText(msgInProgress)
.setProgress(100, progress, false);
builder.setOngoing(true)
.setSmallIcon(android.R.drawable.stat_sys_download);
} else {
builder.setContentText(msgComplete).setProgress(0, 0, false);
builder.setOngoing(false)
.setSmallIcon(android.R.drawable.stat_sys_download_done);
}
} else if (status == DownloadStatus.CANCELED) {
builder.setContentText(msgCanceled).setProgress(0, 0, false);
builder.setOngoing(false)
.setSmallIcon(android.R.drawable.stat_sys_download_done);
} else if (status == DownloadStatus.FAILED) {
builder.setContentText(msgFailed).setProgress(0, 0, false);
builder.setOngoing(false)
.setSmallIcon(android.R.drawable.stat_sys_download_done);
} else if (status == DownloadStatus.PAUSED) {
builder.setContentText(msgPaused).setProgress(0, 0, false);
builder.setOngoing(false)
.setSmallIcon(android.R.drawable.stat_sys_download_done);
} else if (status == DownloadStatus.COMPLETE) {
builder.setContentText(msgComplete).setProgress(0, 0, false);
builder.setOngoing(false)
.setSmallIcon(android.R.drawable.stat_sys_download_done);
} else {
builder.setProgress(0, 0, false);
builder.setOngoing(false).setSmallIcon(getNotificationIconRes());
}
// Note: Android applies a rate limit when updating a notification.
// If you post updates to a notification too frequently (many in less than one second),
// the system might drop some updates. (https://developer.android.com/training/notify-user/build-notification#Updating)
//
// If this is progress update, it's not much important if it is dropped because there're still incoming updates later
// If this is the final update, it must be success otherwise the notification will be stuck at the processing state
// In order to ensure the final one is success, we check and sleep a second if need.
if (System.currentTimeMillis() - lastCallUpdateNotification < 1000) {
if (finalize) {
log("Update too frequently!!!!, but it is the final update, we should sleep a second to ensure the update call can be processed");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
log("Update too frequently!!!!, this should be dropped");
return;
}
}
log("Update notification: {notificationId: " + primaryId + ", title: " + title + ", status: " + status + ", progress: " + progress + "}");
NotificationManagerCompat.from(context).notify(primaryId, builder.build());
lastCallUpdateNotification = System.currentTimeMillis();
}
}
private void sendUpdateProcessEvent(int status, int progress) {
final List<Object> args = new ArrayList<>();
long callbackHandle = getInputData().getLong(ARG_CALLBACK_HANDLE, 0);
args.add(callbackHandle);
args.add(getId().toString());
args.add(status);
args.add(progress);
synchronized (isolateStarted) {
if (!isolateStarted.get()) {
isolateQueue.add(args);
} else {
new Handler(getApplicationContext().getMainLooper()).post(new Runnable() {
@Override
public void run() {
backgroundChannel.invokeMethod("", args);
}
});
}
}
}
private String getCharsetFromContentType(String contentType) {
if (contentType == null)
return null;
Matcher m = charsetPattern.matcher(contentType);
if (m.find()) {
return m.group(1).trim().toUpperCase();
}
return null;
}
private String getFileNameFromContentDisposition(String disposition, String contentCharset) throws java.io.UnsupportedEncodingException {
if (disposition == null)
return null;
String name = null;
String charset = contentCharset;
//first, match plain filename, and then replace it with star filename, to follow the spec
Matcher plainMatcher = filenamePattern.matcher(disposition);
if (plainMatcher.find())
name = plainMatcher.group(1);
Matcher starMatcher = filenameStarPattern.matcher(disposition);
if (starMatcher.find()) {
name = starMatcher.group(3);
charset = starMatcher.group(1).toUpperCase();
}
if (name == null)
return null;
return URLDecoder.decode(name, charset != null ? charset : "ISO-8859-1");
}
private String getContentTypeWithoutCharset(String contentType) {
if (contentType == null)
return null;
return contentType.split(";")[0].trim();
}
private boolean isImageOrVideoFile(String contentType) {
contentType = getContentTypeWithoutCharset(contentType);
return (contentType != null && (contentType.startsWith("image/") || contentType.startsWith("video")));
}
private boolean isExternalStoragePath(String filePath) {
File externalStorageDir = Environment.getExternalStorageDirectory();
return filePath != null && externalStorageDir != null && filePath.startsWith(externalStorageDir.getPath());
}
private void addImageOrVideoToGallery(String fileName, String filePath, String contentType) {
if (contentType != null && filePath != null && fileName != null) {
if (contentType.startsWith("image/")) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "");
values.put(MediaStore.Images.Media.MIME_TYPE, contentType);
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.DATA, filePath);
log("insert " + values + " to MediaStore");
ContentResolver contentResolver = getApplicationContext().getContentResolver();
contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else if (contentType.startsWith("video")) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.TITLE, fileName);
values.put(MediaStore.Video.Media.DISPLAY_NAME, fileName);
values.put(MediaStore.Video.Media.DESCRIPTION, "");
values.put(MediaStore.Video.Media.MIME_TYPE, contentType);
values.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Video.Media.DATA, filePath);
log("insert " + values + " to MediaStore");
ContentResolver contentResolver = getApplicationContext().getContentResolver();
contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
}
}
private void log(String message) {
if (debug) {
Log.d(TAG, message);
}
}
private void logError(String message) {
if (debug) {
Log.e(TAG, message);
}
}
public interface CallbackUri {
void invoke(Uri uri);
}
final static HostnameVerifier DO_NOT_VERIFY = (hostname, session) -> true;
/**
* Trust every server - dont check for any certificate
*/
private static void trustAllHosts() {
final String TAG = "trustAllHosts";
// Create a trust manager that does not validate certificate chains
TrustManager[] trustManagers = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
Log.i(TAG, "checkClientTrusted");
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
Log.i(TAG, "checkServerTrusted");
}
} };
// Install the all-trusting trust manager
try {
SSLContext sslContent = SSLContext.getInstance("TLS");
sslContent.init(null, trustManagers, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContent.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
}