Skip to content

Commit b0eefdc

Browse files
committed
Add support image in share
1 parent 11eca72 commit b0eefdc

9 files changed

Lines changed: 185 additions & 120 deletions

File tree

core/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ android {
3535
dependencies {
3636
implementation fileTree(dir: 'libs', include: ['*.jar'])
3737
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
38-
api 'com.github.Omega-R:OmegaLaunchers:ef6c71e'
39-
api 'com.github.Omega-R.OmegaTypes:omegatypes:1.0.1'
38+
implementation 'com.github.Omega-R:OmegaLaunchers:1.0.2'
39+
api 'com.github.Omega-R.OmegaTypes:omegatypes:1.0.2'
4040

4141
compileOnly "androidx.appcompat:appcompat:$androidX"
4242
}

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
@@ -15,7 +15,9 @@ import android.graphics.Bitmap
1515
import android.net.Uri
1616
import com.omega_r.libs.omegaintentbuilder.builders.share.DownloadBuilder
1717
import com.omega_r.libs.omegaintentbuilder.downloader.Download
18+
import com.omega_r.libs.omegaintentbuilder.models.RemoteFileInfo
1819
import com.omega_r.libs.omegaintentbuilder.providers.FileProvider
20+
import com.omega_r.libs.omegatypes.Image
1921
import java.io.File
2022
import java.io.FileOutputStream
2123

@@ -24,7 +26,7 @@ abstract class BaseUriBuilder() : BaseActivityBuilder(), Download<BaseUriBuilder
2426
private val uriSet: MutableSet<Uri> = mutableSetOf()
2527
private val fileSet: MutableSet<File> = mutableSetOf()
2628

27-
private val downloadBuilder = DownloadBuilder(this)
29+
private val downloadBuilder by lazy { DownloadBuilder(this) }
2830
private var bitmapIndex = 0
2931
private var localFilesDir: File? = null
3032

@@ -100,6 +102,15 @@ abstract class BaseUriBuilder() : BaseActivityBuilder(), Download<BaseUriBuilder
100102
return downloadBuilder.fileUrlWithName(urlAddress, name)
101103
}
102104

105+
fun images(vararg image: Image): DownloadBuilder<BaseUriBuilder> {
106+
return downloadBuilder.images(image)
107+
}
108+
109+
@JvmOverloads
110+
fun image(image: Image, name: String? = null, mimeType: String? = null): DownloadBuilder<BaseUriBuilder> {
111+
return downloadBuilder.image(image, name, mimeType)
112+
}
113+
103114
/**
104115
* @param file File
105116
* @return BaseUriBuilder for method chaining

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@ 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.DownloadAsyncTask
1717
import com.omega_r.libs.omegaintentbuilder.downloader.DownloadCallback
18-
import com.omega_r.libs.omegaintentbuilder.models.FileInfo
18+
import com.omega_r.libs.omegaintentbuilder.models.RemoteFileInfo
19+
import com.omega_r.libs.omegatypes.Image
1920

2021
/**
2122
* DownloadBuilder is a helper for download files from internet and add it to createdIntent
2223
* to share content.
2324
*/
2425
class DownloadBuilder<T>(private val intentBuilder: T) where T : BaseUriBuilder, T : Download<BaseUriBuilder> {
2526

26-
val fileInfoSet: MutableSet<FileInfo> = mutableSetOf()
27+
private val fileInfoSet = mutableSetOf<RemoteFileInfo>()
28+
2729

2830
/**
2931
* Add a array of url addresses to download.
3032
*
3133
* @param urlAddresses Array of String addresses to download and share
3234
* @return This DownloadBuilder for method chaining
3335
*/
34-
fun filesUrls(vararg urlAddresses: String): DownloadBuilder<T> {
35-
urlAddresses.forEach { fileInfoSet.put(it) }
36-
return this
36+
fun filesUrls(vararg urlAddresses: String) = apply {
37+
fileInfoSet += urlAddresses.map { RemoteFileInfo(it) }
3738
}
3839

3940
/**
@@ -45,20 +46,31 @@ class DownloadBuilder<T>(private val intentBuilder: T) where T : BaseUriBuilder,
4546
*/
4647
@JvmOverloads
4748
fun fileUrlWithMimeType(urlAddress: String, mimeType: String? = null): DownloadBuilder<T> {
48-
fileInfoSet.put(urlAddress, mimeType)
49+
fileInfoSet += RemoteFileInfo(urlAddress, mimeType)
4950
return this
5051
}
5152

53+
fun images(images: Array<out Image>) = apply {
54+
fileInfoSet += images.map { RemoteFileInfo(image = it) }
55+
}
56+
57+
fun images(image: Image) = apply {
58+
fileInfoSet += RemoteFileInfo(image = image)
59+
}
60+
61+
fun image(image: Image, name: String? = null, mimeType: String? = null) = apply {
62+
fileInfoSet += RemoteFileInfo(image = image, mimeType = mimeType, originalName = name)
63+
}
64+
5265
/**
5366
* Add a String url address for downloading.
5467
*
5568
* @param urlAddress String address for downloading and share
5669
* @param name String - Your own file name with type ("example.mp3")
5770
* @return This DownloadBuilder for method chaining
5871
*/
59-
fun fileUrlWithName(urlAddress: String, name: String): DownloadBuilder<T> {
60-
fileInfoSet.put(urlAddress, null, name)
61-
return this
72+
fun fileUrlWithName(urlAddress: String, name: String) = apply {
73+
fileInfoSet += RemoteFileInfo(urlAddress, null, name)
6274
}
6375

6476
/**
@@ -67,13 +79,8 @@ class DownloadBuilder<T>(private val intentBuilder: T) where T : BaseUriBuilder,
6779
* @param collection Collection of String addresses to download and share
6880
* @return This DownloadBuilder for method chaining
6981
*/
70-
fun filesUrls(collection: Collection<String>): DownloadBuilder<T> {
71-
collection.forEach { fileInfoSet.put(it) }
72-
return this
73-
}
74-
75-
private fun MutableSet<FileInfo>.put(urlAddress: String, mimeType: String? = null, name: String? = null) {
76-
this.add(FileInfo(urlAddress, mimeType, name))
82+
fun filesUrls(collection: Collection<String>) = apply {
83+
fileInfoSet += collection.map { RemoteFileInfo(it) }
7784
}
7885

7986
/**
@@ -87,8 +94,9 @@ class DownloadBuilder<T>(private val intentBuilder: T) where T : BaseUriBuilder,
8794
callback.onDownloaded(true, intentBuilder.createIntentHandler(context))
8895
return
8996
}
90-
val downloader = DownloadAsyncTask(context, intentBuilder, intentBuilder.getLocalFilesDir(context), callback)
91-
downloader.execute(fileInfoSet)
97+
98+
DownloadAsyncTask(context, intentBuilder, intentBuilder.getLocalFilesDir(context), callback)
99+
.execute(fileInfoSet)
92100
}
93101

94102

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

Lines changed: 53 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,99 +3,80 @@ package com.omega_r.libs.omegaintentbuilder.downloader
33
import android.content.Context
44
import android.net.Uri
55
import android.os.AsyncTask
6-
import androidx.annotation.NonNull
76
import android.util.Log
7+
import androidx.annotation.NonNull
88
import 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
1110
import 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

1913
internal 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
}

core/src/main/java/com/omega_r/libs/omegaintentbuilder/models/FileInfo.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)