@@ -41,6 +41,7 @@ import java.net.URL
4141import java.net.URLDecoder
4242import java.security.SecureRandom
4343import java.security.cert.X509Certificate
44+ import java.util.ArrayDeque
4445import java.util.Locale
4546import java.util.concurrent.atomic.AtomicBoolean
4647import java.util.regex.Pattern
@@ -49,9 +50,6 @@ import javax.net.ssl.HttpsURLConnection
4950import javax.net.ssl.SSLContext
5051import javax.net.ssl.TrustManager
5152import javax.net.ssl.X509TrustManager
52- import java.util.ArrayDeque
53- import kotlin.collections.ArrayList
54- import kotlin.collections.HashMap
5553
5654class DownloadWorker (context : Context , params : WorkerParameters ) :
5755 Worker (context, params),
@@ -158,7 +156,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
158156 val headers: String = inputData.getString(ARG_HEADERS )
159157 ? : throw IllegalArgumentException (" Argument '$ARG_HEADERS ' should not be null" )
160158 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 )
162160 debug = inputData.getBoolean(ARG_DEBUG , false )
163161 step = inputData.getInt(ARG_STEP , 10 )
164162 ignoreSsl = inputData.getBoolean(ARG_IGNORESSL , false )
@@ -172,9 +170,9 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
172170 val task = taskDao?.loadTask(id.toString())
173171 log(
174172 " DownloadWorker{url=$url ,filename=$filename ,savedDir=$savedDir ,header=$headers ,isResume=$isResume ,status=" + (
175- task?.status
176- ? : " GONE"
177- )
173+ task?.status
174+ ? : " GONE"
175+ )
178176 )
179177
180178 // Task has been deleted or cancelled
@@ -258,10 +256,10 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
258256 savedDir : String ,
259257 filename : String? ,
260258 headers : String ,
261- isResume : Boolean ,
262- timeout : Int ,
259+ isResume : Boolean ,
260+ timeout : Int ,
263261 ) {
264- var filename = filename
262+ var actualFilename = filename
265263 var url = fileURL
266264 var resourceUrl: URL
267265 var base: URL ?
@@ -274,7 +272,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
274272 var downloadedBytes: Long = 0
275273 var responseCode: Int
276274 var times: Int
277- var timeout = timeout
275+ var actualTimeout = timeout
278276 visited = HashMap ()
279277 try {
280278 val task = taskDao?.loadTask(id.toString())
@@ -306,16 +304,16 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
306304 resourceUrl.openConnection() as HttpsURLConnection
307305 }
308306 log(" Open connection to $url " )
309- httpConn.connectTimeout = timeout
310- httpConn.readTimeout = timeout
307+ httpConn.connectTimeout = actualTimeout
308+ httpConn.readTimeout = actualTimeout
311309 httpConn.instanceFollowRedirects = false // Make the logic below easier to detect redirections
312310 httpConn.setRequestProperty(" User-Agent" , " Mozilla/5.0..." )
313311
314312 // setup request headers if it is set
315313 setupHeaders(httpConn, headers)
316314 // try to continue downloading a file from its partial downloaded data.
317315 if (isResume) {
318- downloadedBytes = setupPartialDownloadedDataHeader(httpConn, filename , savedDir)
316+ downloadedBytes = setupPartialDownloadedDataHeader(httpConn, actualFilename , savedDir)
319317 }
320318 responseCode = httpConn.responseCode
321319 when (responseCode) {
@@ -348,25 +346,25 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
348346 log(" Charset = $charset " )
349347 if (! isResume) {
350348 // try to extract filename from HTTP headers if it is not given by user
351- if (filename == null ) {
349+ if (actualFilename == null ) {
352350 val disposition: String? = httpConn.getHeaderField(" Content-Disposition" )
353351 log(" Content-Disposition = $disposition " )
354352 if (! disposition.isNullOrEmpty()) {
355- filename = getFileNameFromContentDisposition(disposition, charset)
353+ actualFilename = getFileNameFromContentDisposition(disposition, charset)
356354 }
357- if (filename .isNullOrEmpty()) {
358- filename = url.substring(url.lastIndexOf(" /" ) + 1 )
355+ if (actualFilename .isNullOrEmpty()) {
356+ actualFilename = url.substring(url.lastIndexOf(" /" ) + 1 )
359357 try {
360- filename = URLDecoder .decode(filename , " UTF-8" )
358+ actualFilename = URLDecoder .decode(actualFilename , " UTF-8" )
361359 } catch (e: IllegalArgumentException ) {
362360 /* ok, just let filename be not encoded */
363361 e.printStackTrace()
364362 }
365363 }
366364 }
367365 }
368- log(" fileName = $filename " )
369- taskDao?.updateTask(id.toString(), filename , contentType)
366+ log(" fileName = $actualFilename " )
367+ taskDao?.updateTask(id.toString(), actualFilename , contentType)
370368
371369 // opens input stream from the HTTP connection
372370 inputStream = httpConn.inputStream
@@ -375,7 +373,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
375373 // there are two case:
376374 if (isResume) {
377375 // 1. continue downloading (append data to partial downloaded file)
378- savedFilePath = savedDir + File .separator + filename
376+ savedFilePath = savedDir + File .separator + actualFilename
379377 outputStream = FileOutputStream (savedFilePath, true )
380378 } else {
381379 // 2. new download, create new file
@@ -384,11 +382,11 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
384382 // or public shared download directory (external storage).
385383 // The second option will ignore `savedDir` parameter.
386384 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q && saveInPublicStorage) {
387- val uri = createFileInPublicDownloadsDir(filename , contentType)
385+ val uri = createFileInPublicDownloadsDir(actualFilename , contentType)
388386 savedFilePath = getMediaStoreEntryPathApi29(uri!! )
389387 outputStream = context.contentResolver.openOutputStream(uri, " w" )
390388 } else {
391- val file = createFileInAppSpecificDir(filename !! , savedDir)
389+ val file = createFileInAppSpecificDir(actualFilename !! , savedDir)
392390 savedFilePath = file!! .path
393391 outputStream = FileOutputStream (file, false )
394392 }
@@ -413,7 +411,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
413411 taskDao!! .updateTask(id.toString(), DownloadStatus .RUNNING , progress)
414412 updateNotification(
415413 context,
416- filename ,
414+ actualFilename ,
417415 DownloadStatus .RUNNING ,
418416 progress,
419417 null ,
@@ -434,7 +432,7 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
434432 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .Q ) {
435433 if (isImageOrVideoFile(contentType) && isExternalStoragePath(savedFilePath)) {
436434 addImageOrVideoToGallery(
437- filename ,
435+ actualFilename ,
438436 savedFilePath,
439437 getContentTypeWithoutCharset(contentType)
440438 )
@@ -459,19 +457,19 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
459457 }
460458 }
461459 taskDao!! .updateTask(id.toString(), status, progress)
462- updateNotification(context, filename , status, progress, pendingIntent, true )
460+ updateNotification(context, actualFilename , status, progress, pendingIntent, true )
463461 log(if (isStopped) " Download canceled" else " File downloaded" )
464462 } else {
465463 val task = taskDao!! .loadTask(id.toString())
466464 val status =
467465 if (isStopped) if (task!! .resumable) DownloadStatus .PAUSED else DownloadStatus .CANCELED else DownloadStatus .FAILED
468466 taskDao!! .updateTask(id.toString(), status, lastProgress)
469- updateNotification(context, filename ? : fileURL, status, - 1 , null , true )
467+ updateNotification(context, actualFilename ? : fileURL, status, - 1 , null , true )
470468 log(if (isStopped) " Download canceled" else " Server replied HTTP code: $responseCode " )
471469 }
472470 } catch (e: IOException ) {
473471 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 )
475473 e.printStackTrace()
476474 } finally {
477475 if (outputStream != null ) {
0 commit comments