2020-07-17 16:35:39 +02:00
|
|
|
package sushi.hardcore.droidfs.util
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.net.Uri
|
|
|
|
import sushi.hardcore.droidfs.R
|
2020-07-26 21:16:06 +02:00
|
|
|
import sushi.hardcore.droidfs.provider.RestrictedFileProvider
|
2020-07-17 16:35:39 +02:00
|
|
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialog
|
|
|
|
import java.io.File
|
|
|
|
import java.net.URLConnection
|
|
|
|
import java.util.*
|
|
|
|
|
|
|
|
object ExternalProvider {
|
|
|
|
private const val content_type_all = "*/*"
|
2020-07-26 21:16:06 +02:00
|
|
|
private var storedFiles: MutableList<Uri> = ArrayList()
|
|
|
|
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-07-26 21:16:06 +02:00
|
|
|
var contentType = URLConnection.guessContentTypeFromName(filename)
|
|
|
|
if (contentType == null) {
|
|
|
|
contentType = content_type_all
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
if (previous_content_type == null) {
|
2020-07-26 21:16:06 +02:00
|
|
|
return contentType
|
|
|
|
} else if (previous_content_type != contentType) {
|
2020-07-17 16:35:39 +02:00
|
|
|
return content_type_all
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return previous_content_type
|
|
|
|
}
|
|
|
|
|
2020-07-26 21:16:06 +02:00
|
|
|
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)
|
|
|
|
if (gocryptfsVolume.export_file(context, file_path, tmpFileUri)) {
|
|
|
|
return Pair(tmpFileUri, getContentType(fileName, previous_content_type))
|
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
ColoredAlertDialog(context)
|
|
|
|
.setTitle(R.string.error)
|
|
|
|
.setMessage(context.getString(R.string.export_failed, file_path))
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show()
|
2020-07-26 21:16:06 +02:00
|
|
|
return Pair(null, null)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun share(context: Context, gocryptfsVolume: GocryptfsVolume, file_paths: List<String>) {
|
2020-07-26 21:16:06 +02:00
|
|
|
var contentType: String? = null
|
2020-07-17 16:35:39 +02:00
|
|
|
val uris = ArrayList<Uri>()
|
|
|
|
for (path in file_paths) {
|
2020-07-26 21:16:06 +02:00
|
|
|
val result = exportFile(context, gocryptfsVolume, path, contentType)
|
|
|
|
contentType = if (result.first != null) {
|
|
|
|
uris.add(result.first!!)
|
|
|
|
result.second
|
2020-07-17 16:35:39 +02:00
|
|
|
} else {
|
2020-07-26 21:16:06 +02:00
|
|
|
return
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
val shareIntent = Intent()
|
2020-07-26 21:16:06 +02:00
|
|
|
shareIntent.type = contentType
|
2020-07-17 16:35:39 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.share_chooser)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fun open(context: Context, gocryptfsVolume: GocryptfsVolume, file_path: String) {
|
2020-07-26 21:16:06 +02:00
|
|
|
val result = exportFile(context, gocryptfsVolume, file_path, null)
|
|
|
|
result.first?.let {
|
2020-07-17 16:35:39 +02:00
|
|
|
val openIntent = Intent()
|
|
|
|
openIntent.action = Intent.ACTION_VIEW
|
2020-07-26 21:16:06 +02:00
|
|
|
openIntent.setDataAndType(result.first, result.second)
|
2020-07-17 16:35:39 +02:00
|
|
|
context.startActivity(openIntent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-26 21:16:06 +02:00
|
|
|
fun removeFiles(context: Context) {
|
2020-07-17 16:35:39 +02:00
|
|
|
Thread{
|
2020-07-26 21:16:06 +02:00
|
|
|
for (uri in storedFiles) {
|
2020-07-17 16:35:39 +02:00
|
|
|
if (Wiper.wipe(context, uri)){
|
2020-07-26 21:16:06 +02:00
|
|
|
storedFiles.remove(uri)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.start()
|
|
|
|
}
|
|
|
|
}
|