@@ -77,7 +77,6 @@ public class DownloadWorker extends Worker implements MethodChannel.MethodCallHa
77
77
public static final String ARG_IS_RESUME = "is_resume" ;
78
78
public static final String ARG_SHOW_NOTIFICATION = "show_notification" ;
79
79
public static final String ARG_OPEN_FILE_FROM_NOTIFICATION = "open_file_from_notification" ;
80
- public static final String ARG_NOTIFICATION_TITLE = "notification_title" ;
81
80
public static final String ARG_CALLBACK_HANDLE = "callback_handle" ;
82
81
public static final String ARG_DEBUG = "debug" ;
83
82
public static final String ARG_STEP = "step" ;
@@ -166,11 +165,10 @@ public void onStopped() {
166
165
167
166
String url = getInputData ().getString (ARG_URL );
168
167
String filename = getInputData ().getString (ARG_FILE_NAME );
169
- String notificationTitle = getInputData ().getString (ARG_NOTIFICATION_TITLE );
170
168
171
169
DownloadTask task = taskDao .loadTask (getId ().toString ());
172
170
if (task != null && task .status == DownloadStatus .ENQUEUED ) {
173
- updateNotification (context , filename == null ? url : filename , DownloadStatus .CANCELED , -1 , null , true , notificationTitle );
171
+ updateNotification (context , filename == null ? url : filename , DownloadStatus .CANCELED , -1 , null , true );
174
172
taskDao .updateTask (getId ().toString (), DownloadStatus .CANCELED , lastProgress );
175
173
}
176
174
}
@@ -210,32 +208,31 @@ public Result doWork() {
210
208
211
209
showNotification = getInputData ().getBoolean (ARG_SHOW_NOTIFICATION , false );
212
210
clickToOpenDownloadedFile = getInputData ().getBoolean (ARG_OPEN_FILE_FROM_NOTIFICATION , false );
213
- String notificationTitle = getInputData ().getString (ARG_NOTIFICATION_TITLE );
214
211
saveInPublicStorage = getInputData ().getBoolean (ARG_SAVE_IN_PUBLIC_STORAGE , false );
215
212
216
213
primaryId = task .primaryId ;
217
214
218
215
setupNotification (context );
219
216
220
- updateNotification (context , filename == null ? url : filename , DownloadStatus .RUNNING , task .progress , null , false , notificationTitle );
217
+ updateNotification (context , filename == null ? url : filename , DownloadStatus .RUNNING , task .progress , null , false );
221
218
taskDao .updateTask (getId ().toString (), DownloadStatus .RUNNING , task .progress );
222
219
223
220
//automatic resume for partial files. (if the workmanager unexpectedly quited in background)
224
221
String saveFilePath = savedDir + File .separator + filename ;
225
222
File partialFile = new File (saveFilePath );
226
223
if (partialFile .exists ()) {
227
224
isResume = true ;
228
- log ("exists file for " + filename + "automatic resuming..." );
225
+ log ("exists file for " + filename + "automatic resuming..." );
229
226
}
230
227
231
228
try {
232
- downloadFile (context , url , savedDir , filename , notificationTitle , headers , isResume );
229
+ downloadFile (context , url , savedDir , filename , headers , isResume );
233
230
cleanUp ();
234
231
dbHelper = null ;
235
232
taskDao = null ;
236
233
return Result .success ();
237
234
} catch (Exception e ) {
238
- updateNotification (context , filename == null ? url : filename , DownloadStatus .FAILED , -1 , null , true , notificationTitle );
235
+ updateNotification (context , filename == null ? url : filename , DownloadStatus .FAILED , -1 , null , true );
239
236
taskDao .updateTask (getId ().toString (), DownloadStatus .FAILED , lastProgress );
240
237
e .printStackTrace ();
241
238
dbHelper = null ;
@@ -249,7 +246,7 @@ private void setupHeaders(HttpURLConnection conn, String headers) {
249
246
log ("Headers = " + headers );
250
247
try {
251
248
JSONObject json = new JSONObject (headers );
252
- for (Iterator <String > it = json .keys (); it .hasNext ();) {
249
+ for (Iterator <String > it = json .keys (); it .hasNext (); ) {
253
250
String key = it .next ();
254
251
conn .setRequestProperty (key , json .getString (key ));
255
252
}
@@ -272,7 +269,7 @@ private long setupPartialDownloadedDataHeader(HttpURLConnection conn, String fil
272
269
return downloadedBytes ;
273
270
}
274
271
275
- private void downloadFile (Context context , String fileURL , String savedDir , String filename , String notificationTitle , String headers , boolean isResume ) throws IOException {
272
+ private void downloadFile (Context context , String fileURL , String savedDir , String filename , String headers , boolean isResume ) throws IOException {
276
273
String url = fileURL ;
277
274
URL resourceUrl , base , next ;
278
275
Map <String , Integer > visited ;
@@ -301,14 +298,14 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
301
298
302
299
resourceUrl = new URL (url );
303
300
304
- if (ignoreSsl ) {
301
+ if (ignoreSsl ) {
305
302
trustAllHosts ();
306
303
if (resourceUrl .getProtocol ().toLowerCase ().equals ("https" )) {
307
- HttpsURLConnection https = (HttpsURLConnection ) resourceUrl .openConnection ();
304
+ HttpsURLConnection https = (HttpsURLConnection )resourceUrl .openConnection ();
308
305
https .setHostnameVerifier (DO_NOT_VERIFY );
309
306
httpConn = https ;
310
307
} else {
311
- httpConn = (HttpURLConnection ) resourceUrl .openConnection ();
308
+ httpConn = (HttpURLConnection )resourceUrl .openConnection ();
312
309
}
313
310
} else {
314
311
httpConn = (HttpsURLConnection ) resourceUrl .openConnection ();
@@ -317,7 +314,7 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
317
314
log ("Open connection to " + url );
318
315
httpConn .setConnectTimeout (15000 );
319
316
httpConn .setReadTimeout (15000 );
320
- httpConn .setInstanceFollowRedirects (false ); // Make the logic below easier to detect redirections
317
+ httpConn .setInstanceFollowRedirects (false ); // Make the logic below easier to detect redirections
321
318
httpConn .setRequestProperty ("User-Agent" , "Mozilla/5.0..." );
322
319
323
320
// setup request headers if it is set
@@ -338,7 +335,7 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
338
335
location = httpConn .getHeaderField ("Location" );
339
336
log ("Location = " + location );
340
337
base = new URL (url );
341
- next = new URL (base , location ); // Deal with relative URLs
338
+ next = new URL (base , location ); // Deal with relative URLs
342
339
url = next .toExternalForm ();
343
340
log ("New url: " + url );
344
341
continue ;
@@ -384,6 +381,7 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
384
381
// opens input stream from the HTTP connection
385
382
inputStream = httpConn .getInputStream ();
386
383
384
+
387
385
String savedFilePath ;
388
386
// opens an output stream to save into file
389
387
// there are two case:
@@ -427,7 +425,7 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
427
425
// a new bunch of data fetched and a notification sent
428
426
taskDao .updateTask (getId ().toString (), DownloadStatus .RUNNING , progress );
429
427
430
- updateNotification (context , filename , DownloadStatus .RUNNING , progress , null , false , notificationTitle );
428
+ updateNotification (context , filename , DownloadStatus .RUNNING , progress , null , false );
431
429
}
432
430
}
433
431
@@ -457,19 +455,19 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
457
455
}
458
456
}
459
457
taskDao .updateTask (getId ().toString (), status , progress );
460
- updateNotification (context , filename , status , progress , pendingIntent , true , notificationTitle );
458
+ updateNotification (context , filename , status , progress , pendingIntent , true );
461
459
462
460
log (isStopped () ? "Download canceled" : "File downloaded" );
463
461
} else {
464
462
DownloadTask task = taskDao .loadTask (getId ().toString ());
465
463
int status = isStopped () ? (task .resumable ? DownloadStatus .PAUSED : DownloadStatus .CANCELED ) : DownloadStatus .FAILED ;
466
464
taskDao .updateTask (getId ().toString (), status , lastProgress );
467
- updateNotification (context , filename == null ? fileURL : filename , status , -1 , null , true , notificationTitle );
465
+ updateNotification (context , filename == null ? fileURL : filename , status , -1 , null , true );
468
466
log (isStopped () ? "Download canceled" : "Server replied HTTP code: " + responseCode );
469
467
}
470
468
} catch (IOException e ) {
471
469
taskDao .updateTask (getId ().toString (), DownloadStatus .FAILED , lastProgress );
472
- updateNotification (context , filename == null ? fileURL : filename , DownloadStatus .FAILED , -1 , null , true , notificationTitle );
470
+ updateNotification (context , filename == null ? fileURL : filename , DownloadStatus .FAILED , -1 , null , true );
473
471
e .printStackTrace ();
474
472
} finally {
475
473
if (outputStream != null ) {
@@ -538,10 +536,11 @@ private Uri createFileInPublicDownloadsDir(String filename, String mimeType) {
538
536
private String getMediaStoreEntryPathApi29 (Uri uri ) {
539
537
try (Cursor cursor = getApplicationContext ().getContentResolver ().query (
540
538
uri ,
541
- new String [] { MediaStore .Files .FileColumns .DATA },
539
+ new String []{ MediaStore .Files .FileColumns .DATA },
542
540
null ,
543
541
null ,
544
- null )) {
542
+ null
543
+ )) {
545
544
if (cursor == null )
546
545
return null ;
547
546
if (!cursor .moveToFirst ())
@@ -557,8 +556,8 @@ private String getMediaStoreEntryPathApi29(Uri uri) {
557
556
void scanFilePath (String path , String mimeType , CallbackUri callback ) {
558
557
MediaScannerConnection .scanFile (
559
558
getApplicationContext (),
560
- new String [] { path },
561
- new String [] { mimeType },
559
+ new String []{ path },
560
+ new String []{ mimeType },
562
561
(path1 , uri ) -> callback .invoke (uri ));
563
562
}
564
563
@@ -609,14 +608,14 @@ private void setupNotification(Context context) {
609
608
}
610
609
}
611
610
612
- private void updateNotification (Context context , String title , int status , int progress , PendingIntent intent , boolean finalize , String notificationTitle ) {
611
+ private void updateNotification (Context context , String title , int status , int progress , PendingIntent intent , boolean finalize ) {
613
612
sendUpdateProcessEvent (status , progress );
614
613
615
614
// Show the notification
616
615
if (showNotification ) {
617
616
// Create the notification
618
617
NotificationCompat .Builder builder = new NotificationCompat .Builder (context , CHANNEL_ID ).
619
- setContentTitle (notificationTitle == null ? title : notificationTitle )
618
+ setContentTitle (title )
620
619
.setContentIntent (intent )
621
620
.setOnlyAlertOnce (true )
622
621
.setAutoCancel (true )
0 commit comments