Skip to content

Commit 8558f4f

Browse files
author
roman_tcaregorodtcev
committed
initial commit
1 parent f4f2b61 commit 8558f4f

4 files changed

Lines changed: 74 additions & 20 deletions

File tree

core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/BaseUriBuilder.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import java.io.FileOutputStream
2121

2222
abstract class BaseUriBuilder(private val context: Context): BaseActivityBuilder(context), Download<BaseUriBuilder> {
2323

24-
private var uriSet: MutableSet<Uri> = mutableSetOf()
24+
private val uriSet: MutableSet<Uri> = mutableSetOf()
2525
private val downloadBuilder = DownloadBuilder(context, this)
2626
internal var localFilesDir: File
2727

@@ -81,6 +81,17 @@ abstract class BaseUriBuilder(private val context: Context): BaseActivityBuilder
8181
return downloadBuilder.fileUrlWithMimeType(urlAddress, mimeType)
8282
}
8383

84+
/**
85+
* Add a String url address for downloading.
86+
*
87+
* @param urlAddress String address for downloading and share
88+
* @param name String - Your own file name with type ("example.mp3")
89+
* @return DownloadBuilder for method chaining
90+
*/
91+
fun fileUrlWithName(urlAddress: String, name: String): DownloadBuilder<BaseUriBuilder> {
92+
return downloadBuilder.fileUrlWithName(urlAddress, name)
93+
}
94+
8495
/**
8596
* @param file File
8697
* @return BaseUriBuilder for method chaining

core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/share/DownloadBuilder.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.omega_r.libs.omegaintentbuilder.builders.BaseUriBuilder
1515
import com.omega_r.libs.omegaintentbuilder.downloader.Download
1616
import com.omega_r.libs.omegaintentbuilder.downloader.DownloadCallback
1717
import com.omega_r.libs.omegaintentbuilder.downloader.DownloadAsyncTask
18+
import com.omega_r.libs.omegaintentbuilder.models.FileInfo
1819
import java.util.*
1920

2021
/**
@@ -24,7 +25,7 @@ import java.util.*
2425
class DownloadBuilder<T>(private val context: Context,
2526
private val intentBuilder: T) where T : BaseUriBuilder, T: Download<BaseUriBuilder> {
2627

27-
var urlsMap: MutableMap<String, String?> = TreeMap(String.CASE_INSENSITIVE_ORDER)
28+
val fileInfoSet: MutableSet<FileInfo> = mutableSetOf()
2829

2930
/**
3031
* Add a array of url addresses to download.
@@ -33,7 +34,7 @@ class DownloadBuilder<T>(private val context: Context,
3334
* @return This DownloadBuilder for method chaining
3435
*/
3536
fun filesUrls(vararg urlAddresses: String): DownloadBuilder<T> {
36-
urlAddresses.forEach { it -> urlsMap.put(it) }
37+
urlAddresses.forEach { fileInfoSet.put(it) }
3738
return this
3839
}
3940

@@ -46,7 +47,19 @@ class DownloadBuilder<T>(private val context: Context,
4647
*/
4748
@JvmOverloads
4849
fun fileUrlWithMimeType(urlAddress: String, mimeType: String? = null): DownloadBuilder<T> {
49-
urlsMap.put(urlAddress, mimeType)
50+
fileInfoSet.put(urlAddress, mimeType)
51+
return this
52+
}
53+
54+
/**
55+
* Add a String url address for downloading.
56+
*
57+
* @param urlAddress String address for downloading and share
58+
* @param name String - Your own file name with type ("example.mp3")
59+
* @return This DownloadBuilder for method chaining
60+
*/
61+
fun fileUrlWithName(urlAddress: String, name: String): DownloadBuilder<T> {
62+
fileInfoSet.put(urlAddress, null, name)
5063
return this
5164
}
5265

@@ -57,12 +70,12 @@ class DownloadBuilder<T>(private val context: Context,
5770
* @return This DownloadBuilder for method chaining
5871
*/
5972
fun filesUrls(collection: Collection<String>): DownloadBuilder<T> {
60-
collection.forEach { it -> urlsMap.put(it) }
73+
collection.forEach { fileInfoSet.put(it) }
6174
return this
6275
}
6376

64-
private fun MutableMap<String, String?>.put(key: String, value: String? = null) {
65-
this.put(key, value)
77+
private fun MutableSet<FileInfo>.put(urlAddress: String, mimeType: String? = null, name: String? = null) {
78+
this.add(FileInfo(urlAddress, mimeType, name))
6679
}
6780

6881
/**
@@ -72,12 +85,12 @@ class DownloadBuilder<T>(private val context: Context,
7285
* @return This ContextIntentHandler for method chaining
7386
*/
7487
fun download(callback: DownloadCallback) {
75-
if (urlsMap.isEmpty()) {
88+
if (fileInfoSet.isEmpty()) {
7689
callback.onDownloaded(true, intentBuilder.createIntentHandler())
7790
return
7891
}
7992
val downloader = DownloadAsyncTask(context, intentBuilder, intentBuilder.localFilesDir, callback)
80-
downloader.execute(urlsMap)
93+
downloader.execute(fileInfoSet)
8194
}
8295

8396

core/src/main/java/com/omega_r/libs/omegaintentbuilder/downloader/DownloadAsyncTask.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.os.AsyncTask
66
import android.support.annotation.NonNull
77
import android.util.Log
88
import com.omega_r.libs.omegaintentbuilder.builders.BaseUriBuilder
9+
import com.omega_r.libs.omegaintentbuilder.models.FileInfo
910
import com.omega_r.libs.omegaintentbuilder.providers.FileProvider.*
1011
import java.io.File
1112
import java.io.FileOutputStream
@@ -18,21 +19,21 @@ import java.util.*
1819
internal class DownloadAsyncTask<T>(private val context: Context,
1920
private val intentBuilder: T,
2021
private val localDirFile: File,
21-
private val downloadCallback: DownloadCallback) : AsyncTask<Map<String, String?>, Void, List<Uri>>() where T : BaseUriBuilder {
22+
private val downloadCallback: DownloadCallback) : AsyncTask<Set<FileInfo>, Void, List<Uri>>() where T : BaseUriBuilder {
2223

2324
companion object {
2425
private val TAG = DownloadAsyncTask::class.java.simpleName
2526
private const val BUFFER_SIZE = 8192
2627
}
2728

28-
override fun doInBackground(vararg maps: Map<String, String?>): List<Uri> {
29+
override fun doInBackground(vararg params: Set<FileInfo>): List<Uri> {
2930
val fileSet: MutableSet<File> = mutableSetOf()
30-
val urlsMap: MutableMap<String, String?> = TreeMap(String.CASE_INSENSITIVE_ORDER)
31-
maps.forEach { it -> urlsMap.putAll(it) }
31+
val fileInfoSet: MutableSet<FileInfo> = mutableSetOf()
32+
params.forEach { fileInfoSet.addAll(it) }
3233

33-
for (address in urlsMap) {
34+
fileInfoSet.forEach {
3435
try {
35-
downloadFile(address.key, address.value)?.let { it -> fileSet.add(it) }
36+
downloadFile(it)?.let { fileSet.add(it) }
3637
} catch (exc: IOException) {
3738
Log.e(TAG, exc.toString())
3839
}
@@ -55,18 +56,23 @@ internal class DownloadAsyncTask<T>(private val context: Context,
5556
}
5657

5758
@Throws(IOException::class)
58-
private fun downloadFile(@NonNull urlAddress: String, mimType: String? = null): File? {
59-
val url = URL(urlAddress)
59+
private fun downloadFile(@NonNull fileInfo: FileInfo): File? {
60+
val url = URL(fileInfo.urlAddress)
6061
val httpConnection: HttpURLConnection = url.openConnection() as HttpURLConnection
6162

6263
val responseCode = httpConnection.responseCode
6364
if (responseCode == HttpURLConnection.HTTP_OK) {
6465
val inputStream: InputStream = httpConnection.inputStream;
6566
val file: File
66-
if (mimType.isNullOrEmpty()) {
67-
file = File(localDirFile, getFileName(urlAddress))
67+
68+
if (fileInfo.originalName.isNullOrEmpty()) {
69+
if (fileInfo.mimeType.isNullOrEmpty()) {
70+
file = File(localDirFile, getFileName(fileInfo.urlAddress))
71+
} else {
72+
file = File(localDirFile, getFileName(fileInfo.urlAddress, fileInfo.mimeType))
73+
}
6874
} else {
69-
file = File(localDirFile, getFileName(urlAddress, mimType))
75+
file = File(localDirFile, fileInfo.originalName)
7076
}
7177

7278
val fileOutputStream = FileOutputStream(file)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.omega_r.libs.omegaintentbuilder.models
2+
3+
class FileInfo(
4+
val urlAddress: String,
5+
val mimeType: String? = null,
6+
val originalName: String? = null
7+
) {
8+
9+
override fun equals(other: Any?): Boolean {
10+
if (this === other) return true
11+
if (javaClass != other?.javaClass) return false
12+
13+
other as FileInfo
14+
15+
if (urlAddress != other.urlAddress) return false
16+
17+
return true
18+
}
19+
20+
override fun hashCode(): Int {
21+
return urlAddress.hashCode()
22+
}
23+
24+
}

0 commit comments

Comments
 (0)