forked from upstream-mirrors/quik
Use MediaStore for saving attachments to device
This commit is contained in:
@@ -26,8 +26,10 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import android.media.MediaScannerConnection
|
import android.media.MediaScannerConnection
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
|
import android.provider.MediaStore
|
||||||
import android.provider.Telephony
|
import android.provider.Telephony
|
||||||
import android.provider.Telephony.Mms
|
import android.provider.Telephony.Mms
|
||||||
import android.provider.Telephony.Sms
|
import android.provider.Telephony.Sms
|
||||||
@@ -44,6 +46,8 @@ import com.klinker.android.send_message.Transaction
|
|||||||
import com.moez.QKSMS.common.util.extensions.now
|
import com.moez.QKSMS.common.util.extensions.now
|
||||||
import com.moez.QKSMS.compat.TelephonyCompat
|
import com.moez.QKSMS.compat.TelephonyCompat
|
||||||
import com.moez.QKSMS.extensions.anyOf
|
import com.moez.QKSMS.extensions.anyOf
|
||||||
|
import com.moez.QKSMS.extensions.isImage
|
||||||
|
import com.moez.QKSMS.extensions.isVideo
|
||||||
import com.moez.QKSMS.manager.ActiveConversationManager
|
import com.moez.QKSMS.manager.ActiveConversationManager
|
||||||
import com.moez.QKSMS.manager.KeyManager
|
import com.moez.QKSMS.manager.KeyManager
|
||||||
import com.moez.QKSMS.model.Attachment
|
import com.moez.QKSMS.model.Attachment
|
||||||
@@ -61,6 +65,8 @@ import io.realm.Case
|
|||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
|
import okio.buffer
|
||||||
|
import okio.source
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileNotFoundException
|
import java.io.FileNotFoundException
|
||||||
@@ -167,36 +173,53 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
.findAllAsync()
|
.findAllAsync()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun savePart(id: Long): File? {
|
override fun savePart(id: Long): Uri? {
|
||||||
val part = getPart(id) ?: return null
|
val part = getPart(id) ?: return null
|
||||||
|
|
||||||
val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(part.type) ?: return null
|
val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(part.type) ?: return null
|
||||||
val date = part.messages?.first()?.date
|
val date = part.messages?.first()?.date
|
||||||
val dir = File(Environment.getExternalStorageDirectory(), "QKSMS/Media").apply { mkdirs() }
|
|
||||||
val fileName = part.name?.takeIf { name -> name.endsWith(extension) }
|
val fileName = part.name?.takeIf { name -> name.endsWith(extension) }
|
||||||
?: "${part.type.split("/").last()}_$date.$extension"
|
?: "${part.type.split("/").last()}_$date.$extension"
|
||||||
var file: File
|
|
||||||
var index = 0
|
|
||||||
do {
|
|
||||||
file = File(dir, if (index == 0) fileName else fileName.replace(".$extension", " ($index).$extension"))
|
|
||||||
index++
|
|
||||||
} while (file.exists())
|
|
||||||
|
|
||||||
try {
|
val values = contentValuesOf(
|
||||||
FileOutputStream(file).use { outputStream ->
|
MediaStore.MediaColumns.DISPLAY_NAME to fileName,
|
||||||
|
MediaStore.MediaColumns.MIME_TYPE to part.type,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
|
values.put(MediaStore.MediaColumns.IS_PENDING, 1)
|
||||||
|
values.put(MediaStore.MediaColumns.RELATIVE_PATH, when {
|
||||||
|
part.isImage() -> "${Environment.DIRECTORY_PICTURES}/QKSMS"
|
||||||
|
part.isVideo() -> "${Environment.DIRECTORY_MOVIES}/QKSMS"
|
||||||
|
else -> "${Environment.DIRECTORY_DOWNLOADS}/QKSMS"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
val contentUri = when {
|
||||||
|
part.isImage() -> MediaStore.Images.Media.EXTERNAL_CONTENT_URI
|
||||||
|
part.isVideo() -> MediaStore.Video.Media.EXTERNAL_CONTENT_URI
|
||||||
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> MediaStore.Downloads.EXTERNAL_CONTENT_URI
|
||||||
|
else -> MediaStore.Files.getContentUri("external")
|
||||||
|
}
|
||||||
|
val resolver = context.contentResolver
|
||||||
|
val uri = resolver.insert(contentUri, values)
|
||||||
|
Timber.v("Saving $fileName (${part.type}) to $uri")
|
||||||
|
|
||||||
|
uri?.let {
|
||||||
|
resolver.openOutputStream(uri)?.use { outputStream ->
|
||||||
context.contentResolver.openInputStream(part.getUri())?.use { inputStream ->
|
context.contentResolver.openInputStream(part.getUri())?.use { inputStream ->
|
||||||
inputStream.copyTo(outputStream, 1024)
|
inputStream.copyTo(outputStream, 1024)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: FileNotFoundException) {
|
Timber.v("Saved $fileName (${part.type}) to $uri")
|
||||||
e.printStackTrace()
|
|
||||||
} catch (e: IOException) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
e.printStackTrace()
|
resolver.update(uri, contentValuesOf(MediaStore.MediaColumns.IS_PENDING to 0), null, null)
|
||||||
|
Timber.v("Marked $uri as not pending")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MediaScannerConnection.scanFile(context, arrayOf(file.path), null, null)
|
return uri
|
||||||
|
|
||||||
return file.takeIf { it.exists() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.repository
|
package com.moez.QKSMS.repository
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
import com.moez.QKSMS.model.Attachment
|
import com.moez.QKSMS.model.Attachment
|
||||||
import com.moez.QKSMS.model.Message
|
import com.moez.QKSMS.model.Message
|
||||||
import com.moez.QKSMS.model.MmsPart
|
import com.moez.QKSMS.model.MmsPart
|
||||||
@@ -40,7 +41,7 @@ interface MessageRepository {
|
|||||||
|
|
||||||
fun getPartsForConversation(threadId: Long): RealmResults<MmsPart>
|
fun getPartsForConversation(threadId: Long): RealmResults<MmsPart>
|
||||||
|
|
||||||
fun savePart(id: Long): File?
|
fun savePart(id: Long): Uri?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the list of messages which should be shown in the notification
|
* Retrieves the list of messages which should be shown in the notification
|
||||||
|
|||||||
@@ -260,23 +260,21 @@ class Navigator @Inject constructor(
|
|||||||
startActivityExternal(intent)
|
startActivityExternal(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun viewFile(file: File) {
|
fun viewFile(uri: Uri, mimeType: String) {
|
||||||
val data = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
|
|
||||||
val type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.name.split(".").last())
|
|
||||||
val intent = Intent(Intent.ACTION_VIEW)
|
val intent = Intent(Intent.ACTION_VIEW)
|
||||||
.setDataAndType(data, type)
|
.setDataAndType(uri, mimeType.lowercase())
|
||||||
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
|
.let { Intent.createChooser(it, null) }
|
||||||
|
|
||||||
startActivityExternal(intent)
|
startActivityExternal(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun shareFile(file: File) {
|
fun shareFile(uri: Uri, mimeType: String) {
|
||||||
val data = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
|
|
||||||
val type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.name.split(".").last())
|
|
||||||
val intent = Intent(Intent.ACTION_SEND)
|
val intent = Intent(Intent.ACTION_SEND)
|
||||||
.setType(type)
|
.setType(mimeType.lowercase())
|
||||||
.putExtra(Intent.EXTRA_STREAM, data)
|
.putExtra(Intent.EXTRA_STREAM, uri)
|
||||||
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
|
.let { Intent.createChooser(it, null) }
|
||||||
|
|
||||||
startActivityExternal(intent)
|
startActivityExternal(intent)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ import androidx.core.view.isVisible
|
|||||||
import com.jakewharton.rxbinding2.view.clicks
|
import com.jakewharton.rxbinding2.view.clicks
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.moez.QKSMS.common.base.QkController
|
import com.moez.QKSMS.common.base.QkController
|
||||||
import com.moez.QKSMS.common.util.DateFormatter
|
|
||||||
import com.moez.QKSMS.common.util.QkActivityResultContracts
|
import com.moez.QKSMS.common.util.QkActivityResultContracts
|
||||||
import com.moez.QKSMS.common.util.extensions.getLabel
|
import com.moez.QKSMS.common.util.extensions.getLabel
|
||||||
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
|
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
|
||||||
|
|||||||
@@ -433,7 +433,7 @@ class ComposeViewModel @Inject constructor(
|
|||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { part ->
|
.subscribe { part ->
|
||||||
if (permissionManager.hasStorage()) {
|
if (permissionManager.hasStorage()) {
|
||||||
messageRepo.savePart(part.id)?.let(navigator::viewFile)
|
messageRepo.savePart(part.id)?.let { navigator.viewFile(it, part.type) }
|
||||||
} else {
|
} else {
|
||||||
view.requestStoragePermission()
|
view.requestStoragePermission()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,9 +81,11 @@ class GalleryViewModel @Inject constructor(
|
|||||||
view.optionsItemSelected()
|
view.optionsItemSelected()
|
||||||
.filter { itemId -> itemId == R.id.share }
|
.filter { itemId -> itemId == R.id.share }
|
||||||
.filter { permissions.hasStorage().also { if (!it) view.requestStoragePermission() } }
|
.filter { permissions.hasStorage().also { if (!it) view.requestStoragePermission() } }
|
||||||
.withLatestFrom(view.pageChanged()) { _, part -> part.id }
|
.withLatestFrom(view.pageChanged()) { _, part -> part }
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { partId -> messageRepo.savePart(partId)?.let(navigator::shareFile) }
|
.subscribe { part ->
|
||||||
|
messageRepo.savePart(part.id)?.let { navigator.shareFile(it, part.type) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user