forked from upstream-mirrors/quik
Merge pull request #303 from gavine99/scheduled-message-save-attachments-locally-295
Scheduled message save attachments locally 295
This commit is contained in:
@@ -25,6 +25,7 @@ import android.content.BroadcastReceiver
|
||||
import androidx.core.provider.FontRequest
|
||||
import androidx.emoji.text.EmojiCompat
|
||||
import androidx.emoji.text.FontRequestEmojiCompatConfig
|
||||
import com.moez.QKSMS.manager.HousekeepingWorkManager
|
||||
import com.moez.QKSMS.manager.SpeakManager
|
||||
import dev.octoshrimpy.quik.R
|
||||
import dev.octoshrimpy.quik.common.util.CrashlyticsTree
|
||||
@@ -114,6 +115,9 @@ class QKApplication : Application(), HasActivityInjector, HasBroadcastReceiverIn
|
||||
RxDogTag.builder()
|
||||
.configureWith(AutoDisposeConfigurer::configure)
|
||||
.install()
|
||||
|
||||
// register, or re-register, once-a-day-housekeeping work manager
|
||||
HousekeepingWorkManager.register(applicationContext)
|
||||
}
|
||||
|
||||
override fun activityInjector(): AndroidInjector<Activity> {
|
||||
|
||||
+18
-67
@@ -19,25 +19,24 @@
|
||||
package dev.octoshrimpy.quik.feature.compose
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.media.MediaMetadataRetriever
|
||||
import android.provider.MediaStore.MATCH_DEFAULT
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isVisible
|
||||
import com.bumptech.glide.Glide
|
||||
import com.moez.QKSMS.feature.extensions.LoadBestIconIntoImageView
|
||||
import com.moez.QKSMS.feature.extensions.loadBestIconIntoImageView
|
||||
import dev.octoshrimpy.quik.R
|
||||
import dev.octoshrimpy.quik.common.base.QkAdapter
|
||||
import dev.octoshrimpy.quik.common.base.QkViewHolder
|
||||
import dev.octoshrimpy.quik.common.util.extensions.getDisplayName
|
||||
import dev.octoshrimpy.quik.extensions.resourceExists
|
||||
import dev.octoshrimpy.quik.extensions.getName
|
||||
import dev.octoshrimpy.quik.model.Attachment
|
||||
import ezvcard.Ezvcard
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.attachment_file_list_item.*
|
||||
import kotlinx.android.synthetic.main.attachment_contact_list_item.*
|
||||
import kotlinx.android.synthetic.main.scheduled_message_image_list_item.fileName
|
||||
import kotlinx.android.synthetic.main.scheduled_message_image_list_item.thumbnail
|
||||
import javax.inject.Inject
|
||||
|
||||
|
||||
@@ -90,69 +89,21 @@ class ComposeAttachmentAdapter @Inject constructor(
|
||||
return
|
||||
}
|
||||
|
||||
holder.fileName.visibility = View.GONE
|
||||
|
||||
// if attachment uri is missing
|
||||
if (!attachment.uri.resourceExists(context)) {
|
||||
holder.thumbnail.setImageResource(android.R.drawable.ic_delete)
|
||||
holder.fileName.text = context.getString(R.string.attachment_missing)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
return
|
||||
}
|
||||
|
||||
val uri = attachment.uri
|
||||
val mimeType = attachment.getType(context)
|
||||
|
||||
// if attachment mime type is image/* or video/*, use image/frame
|
||||
if (attachment.hasDisplayableImage(context)) {
|
||||
Glide
|
||||
.with(context)
|
||||
.load(uri)
|
||||
.into(holder.thumbnail)
|
||||
return
|
||||
}
|
||||
|
||||
// if audio mime type, try and use embedded image if one exists
|
||||
if (attachment.isAudio(context)) {
|
||||
val metaDataRetriever = MediaMetadataRetriever()
|
||||
metaDataRetriever.setDataSource(context, uri)
|
||||
val embeddedPicture = metaDataRetriever.embeddedPicture
|
||||
if (embeddedPicture != null) {
|
||||
Glide
|
||||
.with(context)
|
||||
.load(embeddedPicture)
|
||||
.into(holder.thumbnail)
|
||||
return
|
||||
// set best image and text to use for icon
|
||||
when (attachment.uri.loadBestIconIntoImageView(context, holder.thumbnail)) {
|
||||
LoadBestIconIntoImageView.Missing -> {
|
||||
holder.fileName.text = context.getString(R.string.attachment_missing)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
}
|
||||
LoadBestIconIntoImageView.ActivityIcon,
|
||||
LoadBestIconIntoImageView.DefaultAudioIcon,
|
||||
LoadBestIconIntoImageView.GenericIcon -> {
|
||||
// generic style icon used, also show name
|
||||
holder.fileName.text = attachment.uri.getName(context)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
}
|
||||
else -> holder.fileName.visibility = View.GONE
|
||||
}
|
||||
|
||||
// try and use icon from default app for type
|
||||
val intent = Intent(Intent.ACTION_VIEW).setDataAndType(uri, mimeType)
|
||||
val appIcon = context
|
||||
.packageManager
|
||||
.resolveActivity(intent, MATCH_DEFAULT)
|
||||
?.loadIcon(context.packageManager)
|
||||
if (appIcon != null)
|
||||
Glide
|
||||
.with(context)
|
||||
.load(appIcon)
|
||||
.into(holder.thumbnail)
|
||||
else if (mimeType?.startsWith("audio/") == true)
|
||||
// else, if audio, use default local audio icon
|
||||
Glide
|
||||
.with(context)
|
||||
.load(R.drawable.ic_round_volume_up_24)
|
||||
.into(holder.thumbnail)
|
||||
else
|
||||
// else, use default attachment icon
|
||||
Glide
|
||||
.with(context)
|
||||
.load(R.drawable.ic_attachment_black_24dp)
|
||||
.into(holder.thumbnail)
|
||||
|
||||
// else, show file name
|
||||
holder.fileName.text = attachment.getName(context)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int) = when (getItem(position).isVCard(context)) {
|
||||
|
||||
@@ -1127,20 +1127,6 @@ class ComposeViewModel @Inject constructor(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// when activity changes visibility, delete old recording cache files in background thread
|
||||
// generally there won't be any, but under some circumstances some can be left behind
|
||||
view.activityVisibleIntent
|
||||
.subscribeOn(Schedulers.io())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe {
|
||||
context.cacheDir.listFiles()?.forEach {
|
||||
if (it.isFile &&
|
||||
it.name.startsWith(AUDIO_FILE_PREFIX) &&
|
||||
it.name.endsWith(MediaRecorderManager.AUDIO_FILE_SUFFIX))
|
||||
safeDeleteLocalFile(it.toUri())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun safeDeleteLocalFile(uri: Uri): Boolean {
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.moez.QKSMS.feature.extensions
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.media.MediaMetadataRetriever
|
||||
import android.net.Uri
|
||||
import android.widget.ImageView
|
||||
import androidx.appcompat.content.res.AppCompatResources
|
||||
import com.bumptech.glide.Glide
|
||||
import dev.octoshrimpy.quik.R
|
||||
import dev.octoshrimpy.quik.extensions.getDefaultActivityIconForMimeType
|
||||
import dev.octoshrimpy.quik.extensions.isAudio
|
||||
import dev.octoshrimpy.quik.extensions.isImage
|
||||
import dev.octoshrimpy.quik.extensions.isVideo
|
||||
import dev.octoshrimpy.quik.extensions.resourceExists
|
||||
|
||||
enum class LoadBestIconIntoImageView {
|
||||
Missing,
|
||||
ImageVideoIcon,
|
||||
EmbeddedIcon,
|
||||
DefaultAudioIcon,
|
||||
ActivityIcon,
|
||||
GenericIcon
|
||||
}
|
||||
|
||||
fun Uri.loadBestIconIntoImageView(context: Context, imageView: ImageView)
|
||||
: LoadBestIconIntoImageView {
|
||||
|
||||
// if resource is missing, use missing icon
|
||||
if (!resourceExists(context)) {
|
||||
Glide
|
||||
.with(context)
|
||||
.load(android.R.drawable.ic_delete)
|
||||
.into(imageView)
|
||||
return LoadBestIconIntoImageView.Missing
|
||||
}
|
||||
|
||||
// if attachment is image or video use image or video frame for icon
|
||||
if (isImage(context) || isVideo(context)) {
|
||||
Glide
|
||||
.with(context)
|
||||
.load(this)
|
||||
.into(imageView)
|
||||
return LoadBestIconIntoImageView.ImageVideoIcon
|
||||
}
|
||||
|
||||
var appIcon: Drawable? = null
|
||||
|
||||
var retVal = LoadBestIconIntoImageView.EmbeddedIcon
|
||||
|
||||
// if audio mime type, try and use embedded image (ie. id3 cover art) if one exists
|
||||
if (isAudio(context)) {
|
||||
MediaMetadataRetriever().apply {
|
||||
setDataSource(context, this@loadBestIconIntoImageView)
|
||||
val embeddedPicture = embeddedPicture
|
||||
if (embeddedPicture != null) {
|
||||
Glide
|
||||
.with(context)
|
||||
.load(embeddedPicture)
|
||||
.into(imageView)
|
||||
return retVal
|
||||
}
|
||||
}
|
||||
|
||||
// else, use applications built-in audio icon
|
||||
appIcon = AppCompatResources.getDrawable(context, R.drawable.ic_round_volume_up_24)
|
||||
retVal = LoadBestIconIntoImageView.DefaultAudioIcon
|
||||
}
|
||||
|
||||
// not audio, try and get icon from default activity for type
|
||||
if (appIcon == null) {
|
||||
appIcon = getDefaultActivityIconForMimeType(context)
|
||||
retVal = LoadBestIconIntoImageView.ActivityIcon
|
||||
}
|
||||
|
||||
// else, use default local generic attachment icon
|
||||
if (appIcon == null) {
|
||||
appIcon = AppCompatResources.getDrawable(context, R.drawable.ic_attachment_black_24dp)
|
||||
retVal = LoadBestIconIntoImageView.GenericIcon
|
||||
}
|
||||
|
||||
// load icon
|
||||
Glide
|
||||
.with(context)
|
||||
.load(appIcon)
|
||||
.into(imageView)
|
||||
|
||||
return retVal
|
||||
}
|
||||
+15
-76
@@ -19,19 +19,16 @@
|
||||
package dev.octoshrimpy.quik.feature.scheduled
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.media.MediaMetadataRetriever
|
||||
import android.net.Uri
|
||||
import android.provider.MediaStore.MATCH_DEFAULT
|
||||
import android.provider.OpenableColumns
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.bumptech.glide.Glide
|
||||
import com.moez.QKSMS.feature.extensions.LoadBestIconIntoImageView
|
||||
import com.moez.QKSMS.feature.extensions.loadBestIconIntoImageView
|
||||
import dev.octoshrimpy.quik.R
|
||||
import dev.octoshrimpy.quik.common.base.QkAdapter
|
||||
import dev.octoshrimpy.quik.common.base.QkViewHolder
|
||||
import dev.octoshrimpy.quik.extensions.resourceExists
|
||||
import dev.octoshrimpy.quik.extensions.getName
|
||||
import kotlinx.android.synthetic.main.scheduled_message_image_list_item.fileName
|
||||
import kotlinx.android.synthetic.main.scheduled_message_image_list_item.thumbnail
|
||||
import javax.inject.Inject
|
||||
@@ -52,78 +49,20 @@ class ScheduledMessageAttachmentAdapter @Inject constructor(
|
||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||
val uri = getItem(position)
|
||||
|
||||
// if uri target doesn't exist anymore
|
||||
if (!uri.resourceExists(context)) {
|
||||
holder.thumbnail.setImageResource(android.R.drawable.ic_delete)
|
||||
holder.fileName.text = context.getString(R.string.attachment_missing)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
return
|
||||
}
|
||||
|
||||
val mimeType = context.contentResolver.getType(uri)
|
||||
|
||||
// if attachment mime type is image/* or video/*, use image/video-frame
|
||||
if ((mimeType?.startsWith("image/") == true) ||
|
||||
(mimeType?.startsWith("video/") == true)
|
||||
) {
|
||||
Glide
|
||||
.with(context)
|
||||
.load(uri)
|
||||
.into(holder.thumbnail)
|
||||
return
|
||||
}
|
||||
|
||||
// if audio mime type, try and use embedded image if one exists
|
||||
if (mimeType?.startsWith("audio/") == true) {
|
||||
val metaDataRetriever = MediaMetadataRetriever()
|
||||
metaDataRetriever.setDataSource(context, uri)
|
||||
val embeddedPicture = metaDataRetriever.embeddedPicture
|
||||
if (embeddedPicture != null) {
|
||||
Glide
|
||||
.with(context)
|
||||
.load(embeddedPicture)
|
||||
.into(holder.thumbnail)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// try and use icon from default app for type
|
||||
val intent = Intent(Intent.ACTION_VIEW).setDataAndType(uri, mimeType)
|
||||
val appIcon = context
|
||||
.packageManager
|
||||
.resolveActivity(intent, MATCH_DEFAULT)
|
||||
?.loadIcon(context.packageManager)
|
||||
if (appIcon != null)
|
||||
Glide
|
||||
.with(context)
|
||||
.load(appIcon)
|
||||
.into(holder.thumbnail)
|
||||
else if (mimeType?.startsWith("audio/") == true)
|
||||
// else, if audio, use default local audio icon
|
||||
Glide
|
||||
.with(context)
|
||||
.load(R.drawable.ic_round_volume_up_24)
|
||||
.into(holder.thumbnail)
|
||||
else
|
||||
// else, use default attachment icon
|
||||
Glide
|
||||
.with(context)
|
||||
.load(R.drawable.ic_attachment_black_24dp)
|
||||
.into(holder.thumbnail)
|
||||
|
||||
// else, show file name
|
||||
val cursor = context.contentResolver.query(
|
||||
uri, arrayOf(OpenableColumns.DISPLAY_NAME), null, null, null
|
||||
)
|
||||
cursor?.use {
|
||||
try {
|
||||
cursor.moveToFirst()
|
||||
holder.fileName.text = cursor.getString(
|
||||
cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) ?: 0
|
||||
)
|
||||
// set best image and text to use for icon
|
||||
when (getItem(position).loadBestIconIntoImageView(context, holder.thumbnail)) {
|
||||
LoadBestIconIntoImageView.Missing -> {
|
||||
holder.fileName.text = context.getString(R.string.attachment_missing)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
LoadBestIconIntoImageView.ActivityIcon,
|
||||
LoadBestIconIntoImageView.DefaultAudioIcon,
|
||||
LoadBestIconIntoImageView.GenericIcon -> {
|
||||
// generic style icon used, also show name
|
||||
holder.fileName.text = uri.getName(context)
|
||||
holder.fileName.visibility = View.VISIBLE
|
||||
}
|
||||
else -> holder.fileName.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import dev.octoshrimpy.quik.repository.ScheduledMessageRepository
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.autoDisposable
|
||||
import dev.octoshrimpy.quik.common.util.ClipboardUtils
|
||||
import dev.octoshrimpy.quik.interactor.DeleteScheduledMessages
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
@@ -22,6 +23,7 @@ class ScheduledViewModel @Inject constructor(
|
||||
private val navigator: Navigator,
|
||||
private val scheduledMessageRepo: ScheduledMessageRepository,
|
||||
private val sendScheduledMessageInteractor: SendScheduledMessage,
|
||||
private val deleteScheduledMessagesInteractor: DeleteScheduledMessages,
|
||||
) : QkViewModel<ScheduledView, ScheduledState>(ScheduledState(
|
||||
scheduledMessages = scheduledMessageRepo.getScheduledMessages()
|
||||
)) {
|
||||
@@ -94,7 +96,7 @@ class ScheduledViewModel @Inject constructor(
|
||||
.subscribeOn(Schedulers.io())
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe {
|
||||
scheduledMessageRepo.deleteScheduledMessages(it)
|
||||
deleteScheduledMessagesInteractor.execute(it)
|
||||
view.clearSelection()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user