Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.io.FileOutputStream

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

private var uriSet: MutableSet<Uri> = mutableSetOf()
private val uriSet: MutableSet<Uri> = mutableSetOf()
private val downloadBuilder = DownloadBuilder(context, this)
internal var localFilesDir: File

Expand Down Expand Up @@ -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<BaseUriBuilder> {
return downloadBuilder.fileUrlWithName(urlAddress, name)
}

/**
* @param file File
* @return BaseUriBuilder for method chaining
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

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

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

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

Expand All @@ -46,7 +47,19 @@ class DownloadBuilder<T>(private val context: Context,
*/
@JvmOverloads
fun fileUrlWithMimeType(urlAddress: String, mimeType: String? = null): DownloadBuilder<T> {
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<T> {
fileInfoSet.put(urlAddress, null, name)
return this
}

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

private fun MutableMap<String, String?>.put(key: String, value: String? = null) {
this.put(key, value)
private fun MutableSet<FileInfo>.put(urlAddress: String, mimeType: String? = null, name: String? = null) {
this.add(FileInfo(urlAddress, mimeType, name))
}

/**
Expand All @@ -72,12 +85,12 @@ class DownloadBuilder<T>(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)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,21 +19,21 @@ import java.util.*
internal class DownloadAsyncTask<T>(private val context: Context,
private val intentBuilder: T,
private val localDirFile: File,
private val downloadCallback: DownloadCallback) : AsyncTask<Map<String, String?>, Void, List<Uri>>() where T : BaseUriBuilder {
private val downloadCallback: DownloadCallback) : AsyncTask<Set<FileInfo>, Void, List<Uri>>() where T : BaseUriBuilder {

companion object {
private val TAG = DownloadAsyncTask::class.java.simpleName
private const val BUFFER_SIZE = 8192
}

override fun doInBackground(vararg maps: Map<String, String?>): List<Uri> {
override fun doInBackground(vararg params: Set<FileInfo>): List<Uri> {
val fileSet: MutableSet<File> = mutableSetOf()
val urlsMap: MutableMap<String, String?> = TreeMap(String.CASE_INSENSITIVE_ORDER)
maps.forEach { it -> urlsMap.putAll(it) }
val fileInfoSet: MutableSet<FileInfo> = 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())
}
Expand All @@ -55,18 +56,23 @@ internal class DownloadAsyncTask<T>(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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
public interface MimeTypes {

String ANY = "*/*";
String FILE = "file/*";

String IMAGE = "image/*";
Expand Down