@@ -3,99 +3,80 @@ package com.omega_r.libs.omegaintentbuilder.downloader
33import android.content.Context
44import android.net.Uri
55import android.os.AsyncTask
6- import androidx.annotation.NonNull
76import android.util.Log
7+ import androidx.annotation.NonNull
88import com.omega_r.libs.omegaintentbuilder.builders.BaseUriBuilder
9- import com.omega_r.libs.omegaintentbuilder.models.FileInfo
10- import com.omega_r.libs.omegaintentbuilder.providers.FileProvider.getFileName
9+ import com.omega_r.libs.omegaintentbuilder.models.RemoteFileInfo
1110import com.omega_r.libs.omegaintentbuilder.providers.FileProvider.getLocalFileUri
12- import java.io.File
13- import java.io.FileOutputStream
14- import java.io.IOException
15- import java.io.InputStream
16- import java.net.HttpURLConnection
17- import java.net.URL
11+ import java.io.*
1812
1913internal class DownloadAsyncTask <T >(
2014 private val context : Context ,
2115 private val intentBuilder : T ,
2216 private val localDirFile : File ,
2317 private val downloadCallback : DownloadCallback
24- ) : AsyncTask<Set<FileInfo>, Void, List<Uri>>() where T : BaseUriBuilder {
25-
26- companion object {
27- private val TAG = DownloadAsyncTask ::class .java.simpleName
28- private const val BUFFER_SIZE = 8192
29- }
30-
31- override fun doInBackground (vararg params : Set <FileInfo >): List <Uri > {
32- val fileSet: MutableSet <File > = mutableSetOf ()
33- val fileInfoSet: MutableSet <FileInfo > = mutableSetOf ()
34- params.forEach { fileInfoSet.addAll(it) }
18+ ) : AsyncTask<Set<RemoteFileInfo>, Void, List<Uri>>() where T : BaseUriBuilder {
3519
36- fileInfoSet.forEach {
37- try {
38- downloadFile(it)?.let { fileSet.add(it) }
39- } catch (exc: IOException ) {
40- Log .e(TAG , exc.toString())
41- }
20+ companion object {
21+ private val TAG = DownloadAsyncTask ::class .java.simpleName
22+ private const val EOF = - 1
23+ private const val BUFFER_SIZE = 8192
4224 }
4325
44- val listUri = mutableListOf<Uri >();
45- fileSet.forEach { it -> listUri.add(getLocalFileUri(context, it)) }
46- return listUri
47- }
48-
49-
50- override fun onPostExecute (result : List <Uri >) {
51- super .onPostExecute(result)
52- if (result.isEmpty()) {
53- downloadCallback.onDownloaded(false , intentBuilder.createIntentHandler(context))
54- } else {
55- intentBuilder.uri(result)
56- downloadCallback.onDownloaded(true , intentBuilder.createIntentHandler(context))
57- }
58- }
26+ override fun doInBackground (vararg params : Set <RemoteFileInfo >): List <Uri > {
27+ val fileSet: MutableSet <File > = mutableSetOf ()
28+ val fileInfoSet: MutableSet <RemoteFileInfo > = mutableSetOf ()
29+ params.forEach { fileInfoSet.addAll(it) }
30+
31+ fileInfoSet.forEachIndexed { index: Int , remoteFileInfo: RemoteFileInfo ->
32+ try {
33+ downloadFile(remoteFileInfo, index)?.let { fileSet.add(it) }
34+ } catch (exc: IOException ) {
35+ Log .e(TAG , exc.toString())
36+ }
37+ }
5938
60- @Throws( IOException :: class )
61- private fun downloadFile (@NonNull fileInfo : FileInfo ): File ? {
62- val url = URL (fileInfo.urlAddress)
63- val httpConnection : HttpURLConnection = url.openConnection() as HttpURLConnection
39+ val listUri = mutableListOf< Uri >();
40+ fileSet.forEach { it -> listUri.add(getLocalFileUri(context, it)) }
41+ return listUri
42+ }
6443
65- val responseCode = httpConnection.responseCode
66- if (responseCode == HttpURLConnection .HTTP_OK ) {
67- val inputStream: InputStream = httpConnection.inputStream;
68- val file: File
6944
70- if (fileInfo.originalName.isNullOrEmpty()) {
71- if (fileInfo.mimeType.isNullOrEmpty()) {
72- file = File (localDirFile, getFileName(fileInfo.urlAddress))
45+ override fun onPostExecute (result : List <Uri >) {
46+ super .onPostExecute(result)
47+ if (result.isEmpty()) {
48+ downloadCallback.onDownloaded(false , intentBuilder.createIntentHandler(context))
7349 } else {
74- file = File (localDirFile, getFileName(fileInfo.urlAddress, fileInfo.mimeType))
50+ intentBuilder.uri(result)
51+ downloadCallback.onDownloaded(true , intentBuilder.createIntentHandler(context))
7552 }
76- } else {
77- file = File (localDirFile, fileInfo.originalName)
78- }
53+ }
7954
80- val fileOutputStream = FileOutputStream (file)
81- var bytesRead = 0 ;
82- val buffer = ByteArray (BUFFER_SIZE )
55+ @Throws(IOException ::class )
56+ private fun downloadFile (@NonNull fileInfo : RemoteFileInfo , index : Int ): File ? {
57+ return fileInfo.openStream(context) { inputStream ->
58+ File (localDirFile, fileInfo.getFileName(index)).also {
59+ FileOutputStream (it)
60+ .copy(inputStream)
61+ .flushAndClose()
62+ }
63+ }
64+ }
8365
84- do {
85- fileOutputStream.write(buffer, 0 , bytesRead)
86- bytesRead = inputStream.read(buffer)
87- } while (bytesRead > 0 )
66+ private fun OutputStream.copy (inputStream : InputStream ) = also {
67+ val byteArray = ByteArray (BUFFER_SIZE )
68+ while (true ) {
69+ val len = inputStream.read(byteArray)
70+ if (len == - 1 )
71+ break
72+ write(byteArray, 0 , len)
73+ }
8874
89- fileOutputStream.close()
90- inputStream.close()
91- httpConnection.disconnect()
75+ }
9276
93- return file
94- } else {
95- httpConnection.disconnect()
96- Log .e(TAG , " No file to download. Server replied HTTP code: " + responseCode);
97- return null
77+ private fun OutputStream.flushAndClose () {
78+ flush()
79+ close()
9880 }
99- }
10081
10182}
0 commit comments