DroidFS/app/src/main/java/sushi/hardcore/droidfs/content_providers/ExternalProvider.kt

122 lines
5.0 KiB
Kotlin
Raw Normal View History

package sushi.hardcore.droidfs.content_providers
2020-07-17 16:35:39 +02:00
import android.content.Context
import android.content.Intent
import android.net.Uri
2020-09-05 11:37:47 +02:00
import android.webkit.MimeTypeMap
2020-07-28 22:25:10 +02:00
import androidx.appcompat.app.AppCompatActivity
2022-04-20 15:17:33 +02:00
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import sushi.hardcore.droidfs.GocryptfsVolume
import sushi.hardcore.droidfs.LoadingTask
2020-07-17 16:35:39 +02:00
import sushi.hardcore.droidfs.R
2021-11-09 11:12:09 +01:00
import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
2020-07-17 16:35:39 +02:00
import java.io.File
object ExternalProvider {
private const val content_type_all = "*/*"
2022-04-20 15:17:33 +02:00
private var storedFiles = HashSet<Uri>()
2021-11-09 11:12:09 +01:00
private fun getContentType(filename: String, previous_content_type: String?): String {
2020-07-17 16:35:39 +02:00
if (content_type_all != previous_content_type) {
2020-09-05 11:37:47 +02:00
var contentType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(File(filename).extension)
if (contentType == null) {
contentType = content_type_all
2020-07-17 16:35:39 +02:00
}
if (previous_content_type == null) {
return contentType
} else if (previous_content_type != contentType) {
2020-07-17 16:35:39 +02:00
return content_type_all
}
}
return previous_content_type
}
private fun exportFile(context: Context, gocryptfsVolume: GocryptfsVolume, file_path: String, previous_content_type: String?): Pair<Uri?, String?> {
val fileName = File(file_path).name
val tmpFileUri = RestrictedFileProvider.newFile(fileName)
if (tmpFileUri != null){
storedFiles.add(tmpFileUri)
2020-08-01 16:43:48 +02:00
if (gocryptfsVolume.exportFile(context, file_path, tmpFileUri)) {
return Pair(tmpFileUri, getContentType(fileName, previous_content_type))
}
2020-07-17 16:35:39 +02:00
}
return Pair(null, null)
2020-07-17 16:35:39 +02:00
}
2021-11-09 11:12:09 +01:00
fun share(activity: AppCompatActivity, themeValue: String, gocryptfsVolume: GocryptfsVolume, file_paths: List<String>) {
2022-04-20 15:17:33 +02:00
var contentType: String? = null
val uris = ArrayList<Uri>(file_paths.size)
object : LoadingTask<String?>(activity, themeValue, R.string.loading_msg_export) {
override suspend fun doTask(): String? {
2020-07-28 22:25:10 +02:00
for (path in file_paths) {
val result = exportFile(activity, gocryptfsVolume, path, contentType)
contentType = if (result.first != null) {
uris.add(result.first!!)
result.second
} else {
2022-04-20 15:17:33 +02:00
return path
2020-07-28 22:25:10 +02:00
}
}
2022-04-20 15:17:33 +02:00
return null
}
}.startTask(activity.lifecycleScope) { failedItem ->
if (failedItem == null) {
2020-07-28 22:25:10 +02:00
val shareIntent = Intent()
shareIntent.type = contentType
if (uris.size == 1) {
shareIntent.action = Intent.ACTION_SEND
shareIntent.putExtra(Intent.EXTRA_STREAM, uris[0])
} else {
shareIntent.action = Intent.ACTION_SEND_MULTIPLE
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
}
2022-04-20 15:17:33 +02:00
activity.startActivity(Intent.createChooser(shareIntent, activity.getString(R.string.share_chooser)))
} else {
CustomAlertDialogBuilder(activity, themeValue)
.setTitle(R.string.error)
.setMessage(activity.getString(R.string.export_failed, failedItem))
.setPositiveButton(R.string.ok, null)
.show()
2020-07-17 16:35:39 +02:00
}
}
}
2021-11-09 11:12:09 +01:00
fun open(activity: AppCompatActivity, themeValue: String, gocryptfsVolume: GocryptfsVolume, file_path: String) {
2022-04-20 15:17:33 +02:00
object : LoadingTask<Intent?>(activity, themeValue, R.string.loading_msg_export) {
override suspend fun doTask(): Intent? {
2020-07-28 22:25:10 +02:00
val result = exportFile(activity, gocryptfsVolume, file_path, null)
2022-04-20 15:17:33 +02:00
return if (result.first != null) {
Intent(Intent.ACTION_VIEW).apply {
setDataAndType(result.first, result.second)
2020-07-28 22:25:10 +02:00
}
2022-04-20 15:17:33 +02:00
} else {
null
2020-07-28 22:25:10 +02:00
}
}
2022-04-20 15:17:33 +02:00
}.startTask(activity.lifecycleScope) { openIntent ->
if (openIntent == null) {
CustomAlertDialogBuilder(activity, themeValue)
.setTitle(R.string.error)
.setMessage(activity.getString(R.string.export_failed, file_path))
.setPositiveButton(R.string.ok, null)
.show()
} else {
activity.startActivity(openIntent)
}
2020-07-17 16:35:39 +02:00
}
}
2022-04-20 15:17:33 +02:00
fun removeFilesAsync(context: Context) = GlobalScope.launch(Dispatchers.IO) {
val success = HashSet<Uri>(storedFiles.size)
for (uri in storedFiles) {
if (context.contentResolver.delete(uri, null, null) == 1) {
success.add(uri)
2020-07-28 22:25:10 +02:00
}
2022-04-20 15:17:33 +02:00
}
for (uri in success) {
storedFiles.remove(uri)
}
2020-07-17 16:35:39 +02:00
}
}