You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.0 KiB

package xyz.myachin.saveto.ui
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
import android.provider.DocumentsContract
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.PermissionChecker
import xyz.myachin.saveto.R
import java.io.File
import java.io.InputStream
import java.nio.file.Paths
class SharingActivity : AppCompatActivity() {
companion object {
const val TAG = "SharingAct"
const val CREATE_FILE = 101
}
private var fromSaveScreen = false
private var inputStream: InputStream? = null
private var fileData: ByteArray? = null
override fun onCreate(savedInstanceState: Bundle?) {
val perm = android.Manifest.permission.READ_EXTERNAL_STORAGE
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sharing)
if (PermissionChecker.checkSelfPermission(
this,
perm
) != PermissionChecker.PERMISSION_GRANTED
) {
requestPermissions(arrayOf(perm), 110)
}
handleIntent(intent)
}
private fun handleIntent(intent: Intent?) {
when (intent?.action) {
Intent.ACTION_SEND -> {
if (intent.type?.startsWith("image/") == true) {
handleSendImage(intent)
}
}
}
}
private fun handleSendImage(intent: Intent) {
(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
val createIntent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
type = intent.type
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_TITLE, extractName(it, type!!))
putExtra(DocumentsContract.EXTRA_INITIAL_URI, it)
}
inputStream = contentResolver.openInputStream(it)
fromSaveScreen = true
startActivityForResult(createIntent, CREATE_FILE)
}
}
private fun extractName(uri: Uri, type: String): String {
Log.d(TAG, "extractName: uri= $uri")
Log.d(TAG, "extractName: type=$type")
Log.d(TAG, "extractName: ${uri.lastPathSegment}")
val typeMod = type.substringAfter("/")
val stub = "fileName.$typeMod"
val fn = "?filename="
var result = when {
uri.path == null -> stub
uri.path!!.contains(fn) -> uri.path!!.substringAfter(fn).substringBefore("/")
!uri.lastPathSegment.isNullOrEmpty() -> "${uri.lastPathSegment}"
else -> stub
}
File(result).extension.also {
if (it.isEmpty() || it.isBlank() || it.length > 5) {
result = result.plus(".$typeMod")
}
}
return result.replace(":", "_")
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
CREATE_FILE -> {
when (resultCode) {
Activity.RESULT_CANCELED -> return
Activity.RESULT_OK -> {
readFileContent()
writeFile(data?.data)
}
else -> Log.wtf(TAG, "onActivityResult: result code: $resultCode")
}
}
}
finish()
}
override fun onResume() {
super.onResume()
if (!fromSaveScreen) {
finish()
}
}
private fun readFileContent() {
fileData = inputStream?.readBytes()
inputStream?.close()
inputStream = null
}
private fun writeFile(data: Uri?) {
if (data == null) {
Log.e(TAG, "writeFile: Data is null")
return
}
val outputStream = contentResolver.openOutputStream(data)
outputStream?.write(fileData)
outputStream?.close()
fileData = null
}
}