Merge pull request #257 from Aga-C/save-mms

Added saving MMS (#106)
This commit is contained in:
Tibor Kaputa 2021-11-27 19:45:12 +01:00 committed by GitHub
commit 9c56922b75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 4 deletions

View File

@ -52,10 +52,13 @@ import kotlinx.android.synthetic.main.item_selected_contact.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.InputStream
import java.io.OutputStream
class ThreadActivity : SimpleActivity() {
private val MIN_DATE_TIME_DIFF_SECS = 300
private val PICK_ATTACHMENT_INTENT = 1
private val PICK_SAVE_FILE_INTENT = 11
private var threadId = 0L
private var currentSIMCardIndex = 0
@ -69,6 +72,7 @@ class ThreadActivity : SimpleActivity() {
private val availableSIMCards = ArrayList<SIMCard>()
private var attachmentSelections = mutableMapOf<String, AttachmentSelection>()
private val imageCompressor by lazy { ImageCompressor(this) }
private var lastAttachmentUri: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -171,6 +175,24 @@ class ThreadActivity : SimpleActivity() {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == PICK_ATTACHMENT_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
addAttachment(resultData.data!!)
} else if (requestCode == PICK_SAVE_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
applicationContext.contentResolver.takePersistableUriPermission(resultData.data!!, takeFlags)
var inputStream: InputStream? = null
var outputStream: OutputStream? = null
try {
inputStream = contentResolver.openInputStream(Uri.parse(lastAttachmentUri))
outputStream = contentResolver.openOutputStream(Uri.parse(resultData.dataString!!), "rwt")
inputStream!!.copyTo(outputStream!!)
outputStream.flush()
toast(R.string.file_saved)
} catch (e: Exception) {
showErrorToast(e)
} finally {
inputStream?.close()
outputStream?.close()
}
lastAttachmentUri = null
}
}
@ -872,6 +894,16 @@ class ThreadActivity : SimpleActivity() {
return participants
}
fun saveMMS(mimeType: String, path: String) {
lastAttachmentUri = path
Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
type = mimeType
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_TITLE, path.split("/").last())
startActivityForResult(this, PICK_SAVE_FILE_INTENT)
}
}
@SuppressLint("MissingPermission")
@Subscribe(threadMode = ThreadMode.ASYNC)
fun refreshMessages(event: Events.RefreshMessages) {

View File

@ -29,6 +29,7 @@ import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.activities.ThreadActivity
import com.simplemobiletools.smsmessenger.dialogs.SelectTextDialog
import com.simplemobiletools.smsmessenger.extensions.deleteMessage
import com.simplemobiletools.smsmessenger.extensions.updateLastConversationMessage
@ -60,10 +61,13 @@ class ThreadAdapter(
override fun prepareActionMode(menu: Menu) {
val isOneItemSelected = isOneItemSelected()
val selectedItem = getSelectedItems().firstOrNull() as? Message
val hasText = selectedItem?.body != null && selectedItem.body != ""
menu.apply {
findItem(R.id.cab_copy_to_clipboard).isVisible = isOneItemSelected
findItem(R.id.cab_share).isVisible = isOneItemSelected
findItem(R.id.cab_select_text).isVisible = isOneItemSelected
findItem(R.id.cab_copy_to_clipboard).isVisible = isOneItemSelected && hasText
findItem(R.id.cab_save_as).isVisible = isOneItemSelected && selectedItem?.attachment?.attachments?.size == 1
findItem(R.id.cab_share).isVisible = isOneItemSelected && hasText
findItem(R.id.cab_select_text).isVisible = isOneItemSelected && hasText
}
}
@ -74,6 +78,7 @@ class ThreadAdapter(
when (id) {
R.id.cab_copy_to_clipboard -> copyToClipboard()
R.id.cab_save_as -> saveAs()
R.id.cab_share -> shareText()
R.id.cab_select_text -> selectText()
R.id.cab_delete -> askConfirmDelete()
@ -140,6 +145,12 @@ class ThreadAdapter(
activity.copyToClipboard(firstItem.body)
}
private fun saveAs() {
val firstItem = getSelectedItems().firstOrNull() as? Message ?: return
val attachment = firstItem.attachment?.attachments?.first() ?: return
(activity as ThreadActivity).saveMMS(attachment.mimetype, attachment.uriString)
}
private fun shareText() {
val firstItem = getSelectedItems().firstOrNull() as? Message ?: return
activity.shareTextIntent(firstItem.body)

View File

@ -15,7 +15,12 @@
android:id="@+id/cab_delete"
android:icon="@drawable/ic_delete_vector"
android:title="@string/delete"
app:showAsAction="always" />
app:showAsAction="ifRoom" />
<item
android:id="@+id/cab_save_as"
android:icon="@drawable/ic_save_vector"
android:title="@string/save_as"
app:showAsAction="ifRoom" />
<item
android:id="@+id/cab_select_text"
android:title="@string/select_text"