From 923e6202388e3626c6e37832f829e1a965abc90b Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Mon, 30 Apr 2018 17:44:21 -0400 Subject: [PATCH] #751 - Support GIFs inserted from keyboard --- .../src/main/java/common/widget/QkEditText.kt | 50 ++++++++++++++++--- .../main/java/feature/compose/Attachment.kt | 45 +++++++++++++++++ .../java/feature/compose/AttachmentAdapter.kt | 15 +++--- .../java/feature/compose/ComposeActivity.kt | 5 +- .../main/java/feature/compose/ComposeState.kt | 3 +- .../main/java/feature/compose/ComposeView.kt | 5 +- .../java/feature/compose/ComposeViewModel.kt | 14 +++++- .../src/main/java/interactor/SendMessage.kt | 12 +++-- 8 files changed, 122 insertions(+), 27 deletions(-) create mode 100644 presentation/src/main/java/feature/compose/Attachment.kt diff --git a/presentation/src/main/java/common/widget/QkEditText.kt b/presentation/src/main/java/common/widget/QkEditText.kt index 60c22990..ee7c94dd 100644 --- a/presentation/src/main/java/common/widget/QkEditText.kt +++ b/presentation/src/main/java/common/widget/QkEditText.kt @@ -20,12 +20,18 @@ package common.widget import android.content.Context import android.graphics.Typeface +import android.os.Build +import android.os.Bundle +import android.support.v13.view.inputmethod.EditorInfoCompat +import android.support.v13.view.inputmethod.InputConnectionCompat import android.util.AttributeSet import android.view.KeyEvent import android.view.inputmethod.EditorInfo import android.view.inputmethod.InputConnection import android.view.inputmethod.InputConnectionWrapper +import android.view.inputmethod.InputContentInfo import android.widget.EditText +import com.google.android.mms.ContentType import com.moez.QKSMS.R import com.uber.autodispose.android.scope import com.uber.autodispose.kotlin.autoDisposable @@ -42,8 +48,10 @@ import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.PublishSubject import io.reactivex.subjects.Subject import util.Preferences +import util.tryOrNull import javax.inject.Inject + /** * Custom implementation of EditText to allow for dynamic text colors * @@ -71,6 +79,8 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet @Inject lateinit var prefs: Preferences val backspaces: Subject = PublishSubject.create() + val inputContentSelected: Subject = PublishSubject.create() + var supportsInputContent: Boolean = false private var textColorObservable: Observable? = null private var textColorHintObservable: Observable? = null @@ -176,8 +186,35 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet .subscribe() } - override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection { - return object : InputConnectionWrapper(super.onCreateInputConnection(outAttrs), true) { + override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection { + + val inputConnection = super.onCreateInputConnection(editorInfo) + + if (supportsInputContent) { + EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf( + ContentType.IMAGE_JPEG, + ContentType.IMAGE_JPG, + ContentType.IMAGE_PNG, + ContentType.IMAGE_GIF)) + } + + return object : InputConnectionWrapper(inputConnection, true) { + + override fun commitContent(inputContentInfo: InputContentInfo, flags: Int, opts: Bundle?): Boolean { + val grantReadPermission = flags and InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION != 0 + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && grantReadPermission) { + return tryOrNull { + inputContentInfo.requestPermission() + inputContentSelected.onNext(inputContentInfo) + + true + } ?: false + + } + + return true + } override fun sendKeyEvent(event: KeyEvent): Boolean { if (event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_DEL) { @@ -188,11 +225,12 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet override fun deleteSurroundingText(beforeLength: Int, afterLength: Int): Boolean { - // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace return if (beforeLength == 1 && afterLength == 0) { - sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)) - } else super.deleteSurroundingText(beforeLength, afterLength) - + sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) + && sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)) + } else { + super.deleteSurroundingText(beforeLength, afterLength) + } } } diff --git a/presentation/src/main/java/feature/compose/Attachment.kt b/presentation/src/main/java/feature/compose/Attachment.kt new file mode 100644 index 00000000..c39ee6f3 --- /dev/null +++ b/presentation/src/main/java/feature/compose/Attachment.kt @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * This file is part of QKSMS. + * + * QKSMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QKSMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QKSMS. If not, see . + */ +package feature.compose + +import android.content.Context +import android.net.Uri +import android.os.Build +import android.view.inputmethod.InputContentInfo +import com.google.android.mms.ContentType + +data class Attachment(private val uri: Uri? = null, private val inputContent: InputContentInfo? = null) { + + fun getUri(): Uri? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { + inputContent?.contentUri ?: uri + } else { + uri + } + } + + fun isGif(context: Context): Boolean { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && inputContent != null) { + inputContent.description.hasMimeType(ContentType.IMAGE_GIF) + } else { + context.contentResolver.getType(uri) == ContentType.IMAGE_GIF + } + } + +} \ No newline at end of file diff --git a/presentation/src/main/java/feature/compose/AttachmentAdapter.kt b/presentation/src/main/java/feature/compose/AttachmentAdapter.kt index 1eb83aa3..8c6bd3d1 100644 --- a/presentation/src/main/java/feature/compose/AttachmentAdapter.kt +++ b/presentation/src/main/java/feature/compose/AttachmentAdapter.kt @@ -19,13 +19,14 @@ package feature.compose import android.content.Context -import android.net.Uri import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.ViewGroup import com.bumptech.glide.Glide import com.jakewharton.rxbinding2.view.clicks import com.moez.QKSMS.R +import common.base.QkAdapter +import common.base.QkViewHolder import common.util.Colors import common.util.extensions.setBackgroundTint import common.util.extensions.setTint @@ -34,16 +35,14 @@ import io.reactivex.rxkotlin.plusAssign import io.reactivex.subjects.PublishSubject import io.reactivex.subjects.Subject import kotlinx.android.synthetic.main.attachment_list_item.view.* -import common.base.QkAdapter -import common.base.QkViewHolder import javax.inject.Inject class AttachmentAdapter @Inject constructor( private val context: Context, private val colors: Colors -) : QkAdapter() { +) : QkAdapter() { - val attachmentDeleted: Subject = PublishSubject.create() + val attachmentDeleted: Subject = PublishSubject.create() private val disposables = CompositeDisposable() @@ -67,14 +66,14 @@ class AttachmentAdapter @Inject constructor( } override fun onBindViewHolder(holder: QkViewHolder, position: Int) { - val uri = getItem(position) + val attachment = getItem(position) val view = holder.itemView view.clicks().subscribe { - attachmentDeleted.onNext(uri) + attachmentDeleted.onNext(attachment) } - Glide.with(context).load(uri).into(view.thumbnail) + Glide.with(context).load(attachment.getUri()).into(view.thumbnail) } } \ No newline at end of file diff --git a/presentation/src/main/java/feature/compose/ComposeActivity.kt b/presentation/src/main/java/feature/compose/ComposeActivity.kt index f9ddee9a..11a4cb01 100644 --- a/presentation/src/main/java/feature/compose/ComposeActivity.kt +++ b/presentation/src/main/java/feature/compose/ComposeActivity.kt @@ -20,7 +20,6 @@ package feature.compose import android.content.res.ColorStateList import android.graphics.PorterDuff -import android.net.Uri import android.os.Bundle import android.support.v7.widget.LinearLayoutManager import android.view.Menu @@ -64,11 +63,12 @@ class ComposeActivity : QkThemedActivity(), ComposeView { override val messageLongClickIntent: Subject by lazy { messageAdapter.longClicks } override val cancelSendingIntent: Subject by lazy { messageAdapter.cancelSending } override val menuItemIntent: Subject by lazy { dialog.adapter.menuItemClicks } - override val attachmentDeletedIntent: Subject by lazy { attachmentAdapter.attachmentDeleted } + override val attachmentDeletedIntent: Subject by lazy { attachmentAdapter.attachmentDeleted } override val textChangedIntent by lazy { message.textChanges() } override val attachIntent by lazy { attach.clicks() } override val cameraIntent by lazy { camera.clicks() } override val galleryIntent by lazy { gallery.clicks() } + override val inputContentIntent by lazy { message.inputContentSelected } override val sendIntent by lazy { send.clicks() } @Inject lateinit var chipsAdapter: ChipsAdapter @@ -105,6 +105,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView { attachments.adapter = attachmentAdapter messageBackground.backgroundTintMode = PorterDuff.Mode.MULTIPLY + message.supportsInputContent = true val states = arrayOf( intArrayOf(android.R.attr.state_enabled), diff --git a/presentation/src/main/java/feature/compose/ComposeState.kt b/presentation/src/main/java/feature/compose/ComposeState.kt index 698a2872..4f0eb6c5 100644 --- a/presentation/src/main/java/feature/compose/ComposeState.kt +++ b/presentation/src/main/java/feature/compose/ComposeState.kt @@ -18,7 +18,6 @@ */ package feature.compose -import android.net.Uri import io.realm.RealmResults import model.Contact import model.Conversation @@ -33,7 +32,7 @@ data class ComposeState( val selectedContacts: List = ArrayList(), val title: String = "", val messages: Pair>? = null, - val attachments: List = ArrayList(), + val attachments: List = ArrayList(), val attaching: Boolean = false, val remaining: String = "", val canSend: Boolean = false diff --git a/presentation/src/main/java/feature/compose/ComposeView.kt b/presentation/src/main/java/feature/compose/ComposeView.kt index 83b1656b..3f38d5d2 100644 --- a/presentation/src/main/java/feature/compose/ComposeView.kt +++ b/presentation/src/main/java/feature/compose/ComposeView.kt @@ -18,7 +18,7 @@ */ package feature.compose -import android.net.Uri +import android.view.inputmethod.InputContentInfo import common.MenuItem import common.base.QkView import io.reactivex.Observable @@ -41,11 +41,12 @@ interface ComposeView : QkView { val messageLongClickIntent: Subject val cancelSendingIntent: Subject val menuItemIntent: Subject - val attachmentDeletedIntent: Subject + val attachmentDeletedIntent: Subject val textChangedIntent: Observable val attachIntent: Observable val cameraIntent: Observable<*> val galleryIntent: Observable<*> + val inputContentIntent: Observable val sendIntent: Observable fun showMenu(menuItems: List) diff --git a/presentation/src/main/java/feature/compose/ComposeViewModel.kt b/presentation/src/main/java/feature/compose/ComposeViewModel.kt index 605ef54a..6cccc192 100644 --- a/presentation/src/main/java/feature/compose/ComposeViewModel.kt +++ b/presentation/src/main/java/feature/compose/ComposeViewModel.kt @@ -82,7 +82,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel( @Inject lateinit var deleteMessage: DeleteMessage private var sharedText: String = "" - private val attachments: Subject> = BehaviorSubject.createDefault(ArrayList()) + private val attachments: Subject> = BehaviorSubject.createDefault(ArrayList()) private val contacts: Observable> by lazy { contactsRepo.getUnmanagedContacts().toObservable() } private val contactsReducer: Subject<(List) -> List> = PublishSubject.create() private val selectedContacts: Observable> @@ -103,7 +103,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel( val sharedImages = mutableListOf() intent.getParcelableExtra(Intent.EXTRA_STREAM)?.run { sharedImages += this } intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM)?.run { sharedImages += this } - attachments.onNext(sharedImages) + attachments.onNext(sharedImages.map { uri -> Attachment(uri) }) val threadId = intent.extras?.getLong("threadId") ?: 0L var address = "" @@ -358,6 +358,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel( // Attach a photo from camera view.cameraIntent .flatMap { RxImagePicker.with(context).requestImage(Sources.CAMERA) } + .map { uri -> Attachment(uri) } .withLatestFrom(attachments, { attachment, attachments -> attachments + attachment }) .doOnNext { attachments.onNext(it) } .autoDisposable(view.scope()) @@ -366,6 +367,15 @@ class ComposeViewModel(intent: Intent) : QkViewModel( // Attach a photo from gallery view.galleryIntent .flatMap { RxImagePicker.with(context).requestImage(Sources.GALLERY) } + .map { uri -> Attachment(uri) } + .withLatestFrom(attachments, { attachment, attachments -> attachments + attachment }) + .doOnNext { attachments.onNext(it) } + .autoDisposable(view.scope()) + .subscribe { newState { it.copy(attaching = false) } } + + // Attach media from the keyboard + view.inputContentIntent + .map { inputContent -> Attachment(uri = null, inputContent = inputContent) } .withLatestFrom(attachments, { attachment, attachments -> attachments + attachment }) .doOnNext { attachments.onNext(it) } .autoDisposable(view.scope()) diff --git a/presentation/src/main/java/interactor/SendMessage.kt b/presentation/src/main/java/interactor/SendMessage.kt index 4351f9b2..fc59be32 100644 --- a/presentation/src/main/java/interactor/SendMessage.kt +++ b/presentation/src/main/java/interactor/SendMessage.kt @@ -20,12 +20,12 @@ package interactor import android.content.Context import android.graphics.Bitmap -import android.net.Uri import com.google.android.mms.ContentType import com.klinker.android.send_message.Message import com.klinker.android.send_message.Settings import com.klinker.android.send_message.Transaction import com.mlsdev.rximagepicker.RxImageConverters +import feature.compose.Attachment import io.reactivex.Flowable import repository.MessageRepository import util.Preferences @@ -38,7 +38,7 @@ class SendMessage @Inject constructor( private val messageRepo: MessageRepository ) : Interactor() { - data class Params(val threadId: Long, val addresses: List, val body: String, val attachments: List = listOf()) + data class Params(val threadId: Long, val addresses: List, val body: String, val attachments: List = listOf()) override fun buildObservable(params: Params): Flowable { return Flowable.just(Unit) @@ -54,7 +54,7 @@ class SendMessage @Inject constructor( .doOnNext { messageRepo.markUnarchived(params.threadId) } } - private fun sendMms(threadId: Long, addresses: List, body: String, attachments: List) { + private fun sendMms(threadId: Long, addresses: List, body: String, attachments: List) { val settings = Settings() val message = Message(body, addresses.toTypedArray()) @@ -62,7 +62,8 @@ class SendMessage @Inject constructor( // in a lot of these messages failing to send // TODO Add support for GIF compression attachments - .filter { uri -> context.contentResolver.getType(uri) == ContentType.IMAGE_GIF } + .filter { attachment -> attachment.isGif(context) } + .map { attachment -> attachment.getUri() } .map { uri -> context.contentResolver.openInputStream(uri) } .map { inputStream -> inputStream.readBytes() } .forEach { bitmap -> message.addMedia(bitmap, ContentType.IMAGE_GIF) } @@ -70,7 +71,8 @@ class SendMessage @Inject constructor( // Compress the images and add them as attachments var totalImageBytes = 0 attachments - .filter { uri -> context.contentResolver.getType(uri) != ContentType.IMAGE_GIF } + .filter { attachment -> !attachment.isGif(context) } + .map { attachment -> attachment.getUri() } .map { uri -> RxImageConverters.uriToBitmap(context, uri).blockingFirst() } .also { totalImageBytes = it.sumBy { it.allocationByteCount } } .map { bitmap ->