@@ -41,6 +41,7 @@ import java.net.URL
41
41
import java.net.URLDecoder
42
42
import java.security.SecureRandom
43
43
import java.security.cert.X509Certificate
44
+ import java.util.ArrayDeque
44
45
import java.util.Locale
45
46
import java.util.concurrent.atomic.AtomicBoolean
46
47
import java.util.regex.Pattern
@@ -49,9 +50,6 @@ import javax.net.ssl.HttpsURLConnection
49
50
import javax.net.ssl.SSLContext
50
51
import javax.net.ssl.TrustManager
51
52
import javax.net.ssl.X509TrustManager
52
- import java.util.ArrayDeque
53
- import kotlin.collections.ArrayList
54
- import kotlin.collections.HashMap
55
53
56
54
class DownloadWorker (context : Context , params : WorkerParameters ) :
57
55
Worker (context, params),
@@ -158,7 +156,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
158
156
val headers: String = inputData.getString(ARG_HEADERS )
159
157
? : throw IllegalArgumentException (" Argument '$ARG_HEADERS ' should not be null" )
160
158
var isResume: Boolean = inputData.getBoolean(ARG_IS_RESUME , false )
161
- var timeout: Int = inputData.getInt(ARG_TIMEOUT , 15000 )
159
+ val timeout: Int = inputData.getInt(ARG_TIMEOUT , 15000 )
162
160
debug = inputData.getBoolean(ARG_DEBUG , false )
163
161
step = inputData.getInt(ARG_STEP , 10 )
164
162
ignoreSsl = inputData.getBoolean(ARG_IGNORESSL , false )
@@ -172,9 +170,9 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
172
170
val task = taskDao?.loadTask(id.toString())
173
171
log(
174
172
" DownloadWorker{url=$url ,filename=$filename ,savedDir=$savedDir ,header=$headers ,isResume=$isResume ,status=" + (
175
- task?.status
176
- ? : " GONE"
177
- )
173
+ task?.status
174
+ ? : " GONE"
175
+ )
178
176
)
179
177
180
178
// Task has been deleted or cancelled
@@ -258,10 +256,10 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
258
256
savedDir : String ,
259
257
filename : String? ,
260
258
headers : String ,
261
- isResume : Boolean ,
262
- timeout : Int ,
259
+ isResume : Boolean ,
260
+ timeout : Int ,
263
261
) {
264
- var filename = filename
262
+ var actualFilename = filename
265
263
var url = fileURL
266
264
var resourceUrl: URL
267
265
var base: URL ?
@@ -274,7 +272,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
274
272
var downloadedBytes: Long = 0
275
273
var responseCode: Int
276
274
var times: Int
277
- var timeout = timeout
275
+ var actualTimeout = timeout
278
276
visited = HashMap ()
279
277
try {
280
278
val task = taskDao?.loadTask(id.toString())
@@ -306,16 +304,16 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
306
304
resourceUrl.openConnection() as HttpsURLConnection
307
305
}
308
306
log(" Open connection to $url " )
309
- httpConn.connectTimeout = timeout
310
- httpConn.readTimeout = timeout
307
+ httpConn.connectTimeout = actualTimeout
308
+ httpConn.readTimeout = actualTimeout
311
309
httpConn.instanceFollowRedirects = false // Make the logic below easier to detect redirections
312
310
httpConn.setRequestProperty(" User-Agent" , " Mozilla/5.0..." )
313
311
314
312
// setup request headers if it is set
315
313
setupHeaders(httpConn, headers)
316
314
// try to continue downloading a file from its partial downloaded data.
317
315
if (isResume) {
318
- downloadedBytes = setupPartialDownloadedDataHeader(httpConn, filename , savedDir)
316
+ downloadedBytes = setupPartialDownloadedDataHeader(httpConn, actualFilename , savedDir)
319
317
}
320
318
responseCode = httpConn.responseCode
321
319
when (responseCode) {
@@ -348,25 +346,25 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
348
346
log(" Charset = $charset " )
349
347
if (! isResume) {
350
348
// try to extract filename from HTTP headers if it is not given by user
351
- if (filename == null ) {
349
+ if (actualFilename == null ) {
352
350
val disposition: String? = httpConn.getHeaderField(" Content-Disposition" )
353
351
log(" Content-Disposition = $disposition " )
354
352
if (! disposition.isNullOrEmpty()) {
355
- filename = getFileNameFromContentDisposition(disposition, charset)
353
+ actualFilename = getFileNameFromContentDisposition(disposition, charset)
356
354
}
357
- if (filename .isNullOrEmpty()) {
358
- filename = url.substring(url.lastIndexOf(" /" ) + 1 )
355
+ if (actualFilename .isNullOrEmpty()) {
356
+ actualFilename = url.substring(url.lastIndexOf(" /" ) + 1 )
359
357
try {
360
- filename = URLDecoder .decode(filename , " UTF-8" )
358
+ actualFilename = URLDecoder .decode(actualFilename , " UTF-8" )
361
359
} catch (e: IllegalArgumentException ) {
362
360
/* ok, just let filename be not encoded */
363
361
e.printStackTrace()
364
362
}
365
363
}
366
364
}
367
365
}
368
- log(" fileName = $filename " )
369
- taskDao?.updateTask(id.toString(), filename , contentType)
366
+ log(" fileName = $actualFilename " )
367
+ taskDao?.updateTask(id.toString(), actualFilename , contentType)
370
368
371
369
// opens input stream from the HTTP connection
372
370
inputStream = httpConn.inputStream
@@ -375,7 +373,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
375
373
// there are two case:
376
374
if (isResume) {
377
375
// 1. continue downloading (append data to partial downloaded file)
378
- savedFilePath = savedDir + File .separator + filename
376
+ savedFilePath = savedDir + File .separator + actualFilename
379
377
outputStream = FileOutputStream (savedFilePath, true )
380
378
} else {
381
379
// 2. new download, create new file
@@ -384,11 +382,11 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
384
382
// or public shared download directory (external storage).
385
383
// The second option will ignore `savedDir` parameter.
386
384
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q && saveInPublicStorage) {
387
- val uri = createFileInPublicDownloadsDir(filename , contentType)
385
+ val uri = createFileInPublicDownloadsDir(actualFilename , contentType)
388
386
savedFilePath = getMediaStoreEntryPathApi29(uri!! )
389
387
outputStream = context.contentResolver.openOutputStream(uri, " w" )
390
388
} else {
391
- val file = createFileInAppSpecificDir(filename !! , savedDir)
389
+ val file = createFileInAppSpecificDir(actualFilename !! , savedDir)
392
390
savedFilePath = file!! .path
393
391
outputStream = FileOutputStream (file, false )
394
392
}
@@ -413,7 +411,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
413
411
taskDao!! .updateTask(id.toString(), DownloadStatus .RUNNING , progress)
414
412
updateNotification(
415
413
context,
416
- filename ,
414
+ actualFilename ,
417
415
DownloadStatus .RUNNING ,
418
416
progress,
419
417
null ,
@@ -434,7 +432,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
434
432
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .Q ) {
435
433
if (isImageOrVideoFile(contentType) && isExternalStoragePath(savedFilePath)) {
436
434
addImageOrVideoToGallery(
437
- filename ,
435
+ actualFilename ,
438
436
savedFilePath,
439
437
getContentTypeWithoutCharset(contentType)
440
438
)
@@ -459,19 +457,19 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
459
457
}
460
458
}
461
459
taskDao!! .updateTask(id.toString(), status, progress)
462
- updateNotification(context, filename , status, progress, pendingIntent, true )
460
+ updateNotification(context, actualFilename , status, progress, pendingIntent, true )
463
461
log(if (isStopped) " Download canceled" else " File downloaded" )
464
462
} else {
465
463
val task = taskDao!! .loadTask(id.toString())
466
464
val status =
467
465
if (isStopped) if (task!! .resumable) DownloadStatus .PAUSED else DownloadStatus .CANCELED else DownloadStatus .FAILED
468
466
taskDao!! .updateTask(id.toString(), status, lastProgress)
469
- updateNotification(context, filename ? : fileURL, status, - 1 , null , true )
467
+ updateNotification(context, actualFilename ? : fileURL, status, - 1 , null , true )
470
468
log(if (isStopped) " Download canceled" else " Server replied HTTP code: $responseCode " )
471
469
}
472
470
} catch (e: IOException ) {
473
471
taskDao!! .updateTask(id.toString(), DownloadStatus .FAILED , lastProgress)
474
- updateNotification(context, filename ? : fileURL, DownloadStatus .FAILED , - 1 , null , true )
472
+ updateNotification(context, actualFilename ? : fileURL, DownloadStatus .FAILED , - 1 , null , true )
475
473
e.printStackTrace()
476
474
} finally {
477
475
if (outputStream != null ) {
0 commit comments