2023-08-20 14:56:46 +02:00
|
|
|
package sushi.hardcore.droidfs
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.net.Uri
|
|
|
|
import android.webkit.MimeTypeMap
|
2023-09-06 19:27:41 +02:00
|
|
|
import androidx.preference.PreferenceManager
|
2023-08-20 14:56:46 +02:00
|
|
|
import sushi.hardcore.droidfs.content_providers.TemporaryFileProvider
|
|
|
|
import java.io.File
|
|
|
|
|
2023-09-06 19:27:41 +02:00
|
|
|
class FileShare(context: Context) {
|
2023-08-20 14:56:46 +02:00
|
|
|
companion object {
|
2023-09-06 19:27:41 +02:00
|
|
|
private const val CONTENT_TYPE_ANY = "*/*"
|
|
|
|
private fun getContentType(filename: String, previousContentType: String?): String {
|
|
|
|
if (CONTENT_TYPE_ANY != previousContentType) {
|
2023-08-20 14:56:46 +02:00
|
|
|
var contentType = MimeTypeMap.getSingleton()
|
|
|
|
.getMimeTypeFromExtension(File(filename).extension)
|
|
|
|
if (contentType == null) {
|
2023-09-06 19:27:41 +02:00
|
|
|
contentType = CONTENT_TYPE_ANY
|
2023-08-20 14:56:46 +02:00
|
|
|
}
|
|
|
|
if (previousContentType == null) {
|
|
|
|
return contentType
|
|
|
|
} else if (previousContentType != contentType) {
|
2023-09-06 19:27:41 +02:00
|
|
|
return CONTENT_TYPE_ANY
|
2023-08-20 14:56:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return previousContentType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 19:27:41 +02:00
|
|
|
private val usfSafWrite = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("usf_saf_write", false)
|
2023-08-20 14:56:46 +02:00
|
|
|
|
2023-09-06 19:27:41 +02:00
|
|
|
private fun exportFile(exportedFile: EncryptedFileProvider.ExportedFile, size: Long, volumeId: Int, previousContentType: String? = null): Pair<Uri, String>? {
|
|
|
|
val uri = TemporaryFileProvider.instance.exportFile(exportedFile, size, volumeId) ?: return null
|
|
|
|
return Pair(uri, getContentType(File(exportedFile.path).name, previousContentType))
|
2023-08-20 14:56:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-06 19:27:41 +02:00
|
|
|
fun share(files: List<Pair<String, Long>>, volumeId: Int): Pair<Intent?, Int?> {
|
2023-08-20 14:56:46 +02:00
|
|
|
var contentType: String? = null
|
|
|
|
val uris = ArrayList<Uri>(files.size)
|
|
|
|
for ((path, size) in files) {
|
2023-09-06 19:27:41 +02:00
|
|
|
val exportedFile = TemporaryFileProvider.instance.encryptedFileProvider.createFile(path, size)
|
|
|
|
?: return Pair(null, R.string.export_failed_create)
|
|
|
|
val result = exportFile(exportedFile, size, volumeId, contentType)
|
|
|
|
contentType = if (result == null) {
|
|
|
|
return Pair(null, R.string.export_failed_export)
|
2023-08-20 14:56:46 +02:00
|
|
|
} else {
|
2023-09-06 19:27:41 +02:00
|
|
|
uris.add(result.first)
|
2023-08-20 14:56:46 +02:00
|
|
|
result.second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Pair(Intent().apply {
|
|
|
|
type = contentType
|
|
|
|
if (uris.size == 1) {
|
|
|
|
action = Intent.ACTION_SEND
|
|
|
|
putExtra(Intent.EXTRA_STREAM, uris[0])
|
|
|
|
} else {
|
|
|
|
action = Intent.ACTION_SEND_MULTIPLE
|
|
|
|
putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
|
|
|
|
}
|
|
|
|
}, null)
|
|
|
|
}
|
|
|
|
|
2023-09-06 19:27:41 +02:00
|
|
|
fun openWith(exportedFile: EncryptedFileProvider.ExportedFile, size: Long, volumeId: Int): Pair<Intent?, Int?> {
|
|
|
|
val result = exportFile(exportedFile, size, volumeId)
|
|
|
|
return if (result == null) {
|
|
|
|
Pair(null, R.string.export_failed_export)
|
|
|
|
} else {
|
|
|
|
Pair(Intent(Intent.ACTION_VIEW).apply {
|
2023-08-20 14:56:46 +02:00
|
|
|
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
2023-09-06 19:27:41 +02:00
|
|
|
if (usfSafWrite) {
|
|
|
|
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
|
|
|
}
|
2023-08-20 14:56:46 +02:00
|
|
|
setDataAndType(result.first, result.second)
|
2023-09-06 19:27:41 +02:00
|
|
|
}, null)
|
2023-08-20 14:56:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|