From 8558f4f9a31fd2afaf103d938ab2b78b8fe5cbb8 Mon Sep 17 00:00:00 2001 From: roman_tcaregorodtcev Date: Mon, 14 May 2018 16:20:38 +0300 Subject: [PATCH 1/2] initial commit --- .../builders/BaseUriBuilder.kt | 13 ++++++++- .../builders/share/DownloadBuilder.kt | 29 ++++++++++++++----- .../downloader/DownloadAsyncTask.kt | 28 +++++++++++------- .../omegaintentbuilder/models/FileInfo.kt | 24 +++++++++++++++ 4 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 core/src/main/java/com/omega_r/libs/omegaintentbuilder/models/FileInfo.kt diff --git a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/BaseUriBuilder.kt b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/BaseUriBuilder.kt index 3411345..51b7cab 100644 --- a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/BaseUriBuilder.kt +++ b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/BaseUriBuilder.kt @@ -21,7 +21,7 @@ import java.io.FileOutputStream abstract class BaseUriBuilder(private val context: Context): BaseActivityBuilder(context), Download { - private var uriSet: MutableSet = mutableSetOf() + private val uriSet: MutableSet = mutableSetOf() private val downloadBuilder = DownloadBuilder(context, this) internal var localFilesDir: File @@ -81,6 +81,17 @@ abstract class BaseUriBuilder(private val context: Context): BaseActivityBuilder return downloadBuilder.fileUrlWithMimeType(urlAddress, mimeType) } + /** + * Add a String url address for downloading. + * + * @param urlAddress String address for downloading and share + * @param name String - Your own file name with type ("example.mp3") + * @return DownloadBuilder for method chaining + */ + fun fileUrlWithName(urlAddress: String, name: String): DownloadBuilder { + return downloadBuilder.fileUrlWithName(urlAddress, name) + } + /** * @param file File * @return BaseUriBuilder for method chaining diff --git a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/share/DownloadBuilder.kt b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/share/DownloadBuilder.kt index af720b9..71de9ea 100644 --- a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/share/DownloadBuilder.kt +++ b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/share/DownloadBuilder.kt @@ -15,6 +15,7 @@ import com.omega_r.libs.omegaintentbuilder.builders.BaseUriBuilder import com.omega_r.libs.omegaintentbuilder.downloader.Download import com.omega_r.libs.omegaintentbuilder.downloader.DownloadCallback import com.omega_r.libs.omegaintentbuilder.downloader.DownloadAsyncTask +import com.omega_r.libs.omegaintentbuilder.models.FileInfo import java.util.* /** @@ -24,7 +25,7 @@ import java.util.* class DownloadBuilder(private val context: Context, private val intentBuilder: T) where T : BaseUriBuilder, T: Download { - var urlsMap: MutableMap = TreeMap(String.CASE_INSENSITIVE_ORDER) + val fileInfoSet: MutableSet = mutableSetOf() /** * Add a array of url addresses to download. @@ -33,7 +34,7 @@ class DownloadBuilder(private val context: Context, * @return This DownloadBuilder for method chaining */ fun filesUrls(vararg urlAddresses: String): DownloadBuilder { - urlAddresses.forEach { it -> urlsMap.put(it) } + urlAddresses.forEach { fileInfoSet.put(it) } return this } @@ -46,7 +47,19 @@ class DownloadBuilder(private val context: Context, */ @JvmOverloads fun fileUrlWithMimeType(urlAddress: String, mimeType: String? = null): DownloadBuilder { - urlsMap.put(urlAddress, mimeType) + fileInfoSet.put(urlAddress, mimeType) + return this + } + + /** + * Add a String url address for downloading. + * + * @param urlAddress String address for downloading and share + * @param name String - Your own file name with type ("example.mp3") + * @return This DownloadBuilder for method chaining + */ + fun fileUrlWithName(urlAddress: String, name: String): DownloadBuilder { + fileInfoSet.put(urlAddress, null, name) return this } @@ -57,12 +70,12 @@ class DownloadBuilder(private val context: Context, * @return This DownloadBuilder for method chaining */ fun filesUrls(collection: Collection): DownloadBuilder { - collection.forEach { it -> urlsMap.put(it) } + collection.forEach { fileInfoSet.put(it) } return this } - private fun MutableMap.put(key: String, value: String? = null) { - this.put(key, value) + private fun MutableSet.put(urlAddress: String, mimeType: String? = null, name: String? = null) { + this.add(FileInfo(urlAddress, mimeType, name)) } /** @@ -72,12 +85,12 @@ class DownloadBuilder(private val context: Context, * @return This ContextIntentHandler for method chaining */ fun download(callback: DownloadCallback) { - if (urlsMap.isEmpty()) { + if (fileInfoSet.isEmpty()) { callback.onDownloaded(true, intentBuilder.createIntentHandler()) return } val downloader = DownloadAsyncTask(context, intentBuilder, intentBuilder.localFilesDir, callback) - downloader.execute(urlsMap) + downloader.execute(fileInfoSet) } diff --git a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/downloader/DownloadAsyncTask.kt b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/downloader/DownloadAsyncTask.kt index f6864dc..8c49e2a 100644 --- a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/downloader/DownloadAsyncTask.kt +++ b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/downloader/DownloadAsyncTask.kt @@ -6,6 +6,7 @@ import android.os.AsyncTask import android.support.annotation.NonNull import android.util.Log import com.omega_r.libs.omegaintentbuilder.builders.BaseUriBuilder +import com.omega_r.libs.omegaintentbuilder.models.FileInfo import com.omega_r.libs.omegaintentbuilder.providers.FileProvider.* import java.io.File import java.io.FileOutputStream @@ -18,21 +19,21 @@ import java.util.* internal class DownloadAsyncTask(private val context: Context, private val intentBuilder: T, private val localDirFile: File, - private val downloadCallback: DownloadCallback) : AsyncTask, Void, List>() where T : BaseUriBuilder { + private val downloadCallback: DownloadCallback) : AsyncTask, Void, List>() where T : BaseUriBuilder { companion object { private val TAG = DownloadAsyncTask::class.java.simpleName private const val BUFFER_SIZE = 8192 } - override fun doInBackground(vararg maps: Map): List { + override fun doInBackground(vararg params: Set): List { val fileSet: MutableSet = mutableSetOf() - val urlsMap: MutableMap = TreeMap(String.CASE_INSENSITIVE_ORDER) - maps.forEach { it -> urlsMap.putAll(it) } + val fileInfoSet: MutableSet = mutableSetOf() + params.forEach { fileInfoSet.addAll(it) } - for (address in urlsMap) { + fileInfoSet.forEach { try { - downloadFile(address.key, address.value)?.let { it -> fileSet.add(it) } + downloadFile(it)?.let { fileSet.add(it) } } catch (exc: IOException) { Log.e(TAG, exc.toString()) } @@ -55,18 +56,23 @@ internal class DownloadAsyncTask(private val context: Context, } @Throws(IOException::class) - private fun downloadFile(@NonNull urlAddress: String, mimType: String? = null): File? { - val url = URL(urlAddress) + private fun downloadFile(@NonNull fileInfo: FileInfo): File? { + val url = URL(fileInfo.urlAddress) val httpConnection: HttpURLConnection = url.openConnection() as HttpURLConnection val responseCode = httpConnection.responseCode if (responseCode == HttpURLConnection.HTTP_OK) { val inputStream: InputStream = httpConnection.inputStream; val file: File - if (mimType.isNullOrEmpty()) { - file = File(localDirFile, getFileName(urlAddress)) + + if (fileInfo.originalName.isNullOrEmpty()) { + if (fileInfo.mimeType.isNullOrEmpty()) { + file = File(localDirFile, getFileName(fileInfo.urlAddress)) + } else { + file = File(localDirFile, getFileName(fileInfo.urlAddress, fileInfo.mimeType)) + } } else { - file = File(localDirFile, getFileName(urlAddress, mimType)) + file = File(localDirFile, fileInfo.originalName) } val fileOutputStream = FileOutputStream(file) diff --git a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/models/FileInfo.kt b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/models/FileInfo.kt new file mode 100644 index 0000000..9d54385 --- /dev/null +++ b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/models/FileInfo.kt @@ -0,0 +1,24 @@ +package com.omega_r.libs.omegaintentbuilder.models + +class FileInfo( + val urlAddress: String, + val mimeType: String? = null, + val originalName: String? = null +) { + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as FileInfo + + if (urlAddress != other.urlAddress) return false + + return true + } + + override fun hashCode(): Int { + return urlAddress.hashCode() + } + +} \ No newline at end of file From d4269ef1aecd93b51e13431f8279e9b2b59deb23 Mon Sep 17 00:00:00 2001 From: roman_tcaregorodtcev Date: Thu, 17 May 2018 10:16:13 +0300 Subject: [PATCH 2/2] =?UTF-8?q?MimeType=20=C2=AB*/*=C2=BB=20was=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../libs/omegaintentbuilder/builders/pick/BasePickBuilder.kt | 2 +- .../com/omega_r/libs/omegaintentbuilder/types/MimeTypes.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/pick/BasePickBuilder.kt b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/pick/BasePickBuilder.kt index 0d10cc6..d971880 100644 --- a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/pick/BasePickBuilder.kt +++ b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/builders/pick/BasePickBuilder.kt @@ -23,7 +23,7 @@ import com.omega_r.libs.omegaintentbuilder.types.MimeTypes open class BasePickBuilder(context: Context): BaseActivityBuilder(context) { private var allowMultiply = false - protected var mimeType: String = MimeTypes.FILE + protected var mimeType: String = MimeTypes.ANY /** * Extra used to indicate that an intent can allow the user to select and diff --git a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/types/MimeTypes.java b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/types/MimeTypes.java index 8c33fbd..1db2c66 100644 --- a/core/src/main/java/com/omega_r/libs/omegaintentbuilder/types/MimeTypes.java +++ b/core/src/main/java/com/omega_r/libs/omegaintentbuilder/types/MimeTypes.java @@ -18,6 +18,7 @@ */ public interface MimeTypes { + String ANY = "*/*"; String FILE = "file/*"; String IMAGE = "image/*";