diff --git a/presentation/src/main/java/common/widget/QkEditText.kt b/presentation/src/main/java/common/widget/QkEditText.kt index ee7c94dd..0fa1ac3a 100644 --- a/presentation/src/main/java/common/widget/QkEditText.kt +++ b/presentation/src/main/java/common/widget/QkEditText.kt @@ -21,15 +21,14 @@ 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.support.v13.view.inputmethod.InputContentInfoCompat 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 @@ -52,6 +51,8 @@ import util.tryOrNull import javax.inject.Inject + + /** * Custom implementation of EditText to allow for dynamic text colors * @@ -79,7 +80,7 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet @Inject lateinit var prefs: Preferences val backspaces: Subject = PublishSubject.create() - val inputContentSelected: Subject = PublishSubject.create() + val inputContentSelected: Subject = PublishSubject.create() var supportsInputContent: Boolean = false private var textColorObservable: Observable? = null @@ -188,34 +189,7 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet 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 - } - + val inputConnection = object : InputConnectionWrapper(super.onCreateInputConnection(editorInfo), true) { override fun sendKeyEvent(event: KeyEvent): Boolean { if (event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_DEL) { backspaces.onNext(Unit) @@ -232,8 +206,32 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet super.deleteSurroundingText(beforeLength, afterLength) } } - } + + if (supportsInputContent) { + EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf( + ContentType.IMAGE_JPEG, + ContentType.IMAGE_JPG, + ContentType.IMAGE_PNG, + ContentType.IMAGE_GIF)) + } + + val callback = InputConnectionCompat.OnCommitContentListener { inputContentInfo, flags, opts -> + val grantReadPermission = flags and InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION != 0 + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && grantReadPermission) { + return@OnCommitContentListener tryOrNull { + inputContentInfo.requestPermission() + inputContentSelected.onNext(inputContentInfo) + true + } ?: false + + } + + true + } + + return InputConnectionCompat.createWrapper(inputConnection, editorInfo, callback) } } \ No newline at end of file diff --git a/presentation/src/main/java/feature/compose/Attachment.kt b/presentation/src/main/java/feature/compose/Attachment.kt index c39ee6f3..0a5654c9 100644 --- a/presentation/src/main/java/feature/compose/Attachment.kt +++ b/presentation/src/main/java/feature/compose/Attachment.kt @@ -21,10 +21,10 @@ package feature.compose import android.content.Context import android.net.Uri import android.os.Build -import android.view.inputmethod.InputContentInfo +import android.support.v13.view.inputmethod.InputContentInfoCompat import com.google.android.mms.ContentType -data class Attachment(private val uri: Uri? = null, private val inputContent: InputContentInfo? = null) { +data class Attachment(private val uri: Uri? = null, private val inputContent: InputContentInfoCompat? = null) { fun getUri(): Uri? { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { diff --git a/presentation/src/main/java/feature/compose/ComposeView.kt b/presentation/src/main/java/feature/compose/ComposeView.kt index 3f38d5d2..79e4ea85 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.view.inputmethod.InputContentInfo +import android.support.v13.view.inputmethod.InputContentInfoCompat import common.MenuItem import common.base.QkView import io.reactivex.Observable @@ -46,7 +46,7 @@ interface ComposeView : QkView { val attachIntent: Observable val cameraIntent: Observable<*> val galleryIntent: Observable<*> - val inputContentIntent: Observable + val inputContentIntent: Observable val sendIntent: Observable fun showMenu(menuItems: List)