InputConnectionCompat.createWrapper is deprecated

Permission should be granted, according to https://developer.android.com/reference/android/view/OnReceiveContentListener#uri-permissions
This commit is contained in:
Benoit Marty 2021-11-09 17:46:44 +01:00 committed by Benoit Marty
parent 0a9845af30
commit e9f53f6b35

View file

@ -17,13 +17,15 @@
package im.vector.app.features.home.room.detail.composer package im.vector.app.features.home.room.detail.composer
import android.content.ClipData
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Build
import android.text.Editable import android.text.Editable
import android.util.AttributeSet import android.util.AttributeSet
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection import android.view.inputmethod.InputConnection
import androidx.core.view.OnReceiveContentListener
import androidx.core.view.ViewCompat
import androidx.core.view.inputmethod.EditorInfoCompat import androidx.core.view.inputmethod.EditorInfoCompat
import androidx.core.view.inputmethod.InputConnectionCompat import androidx.core.view.inputmethod.InputConnectionCompat
import com.vanniktech.emoji.EmojiEditText import com.vanniktech.emoji.EmojiEditText
@ -43,23 +45,35 @@ class ComposerEditText @JvmOverloads constructor(context: Context, attrs: Attrib
var callback: Callback? = null var callback: Callback? = null
override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection? { override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection? {
val ic = super.onCreateInputConnection(editorInfo) ?: return null var ic = super.onCreateInputConnection(editorInfo) ?: return null
EditorInfoCompat.setContentMimeTypes(editorInfo, arrayOf("*/*")) val mimeTypes = ViewCompat.getOnReceiveContentMimeTypes(this) ?: arrayOf("*/*")
val callback = EditorInfoCompat.setContentMimeTypes(editorInfo, mimeTypes)
InputConnectionCompat.OnCommitContentListener { inputContentInfo, flags, _ -> ic = InputConnectionCompat.createWrapper(this, ic, editorInfo)
val lacksPermission = (flags and
InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0 val onReceiveContentListener = OnReceiveContentListener { _, payload ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1 && lacksPermission) { val split = payload.partition { item -> item.uri != null }
try { val uriContent = split.first
inputContentInfo.requestPermission() val remaining = split.second
} catch (e: Exception) {
return@OnCommitContentListener false if (uriContent != null) {
val clip: ClipData = uriContent.clip
for (i in 0 until clip.itemCount) {
val uri = clip.getItemAt(i).uri
// ... app-specific logic to handle the URI ...
callback?.onRichContentSelected(uri)
} }
} }
callback?.onRichContentSelected(inputContentInfo.contentUri) ?: false // Return anything that we didn't handle ourselves. This preserves the default platform
// behavior for text and anything else for which we are not implementing custom handling.
// Return anything that we didn't handle ourselves. This preserves the default platform
// behavior for text and anything else for which we are not implementing custom handling.
remaining
} }
return InputConnectionCompat.createWrapper(ic, editorInfo, callback)
ViewCompat.setOnReceiveContentListener(this, mimeTypes, onReceiveContentListener)
return ic
} }
init { init {