@@ -64,6 +64,7 @@ class TypeRichTextInputView : AppCompatEditText {
6464 private var lineHeightPx: Int? = null
6565 private var isSettingTextFromJS = false
6666 private var isInitialized = false
67+ private var disableImagePasting = false
6768
6869 constructor (context: Context ) : super (context) {
6970 prepareComponent()
@@ -140,23 +141,34 @@ class TypeRichTextInputView : AppCompatEditText {
140141 override fun onCreateInputConnection (outAttrs : EditorInfo ): InputConnection ? {
141142 val ic = super .onCreateInputConnection(outAttrs) ? : return null
142143
143- EditorInfoCompat .setContentMimeTypes(
144- outAttrs,
145- arrayOf(" image/png" , " image/jpg" , " image/jpeg" , " image/gif" , " image/webp" )
146- )
144+ if (! disableImagePasting) {
145+ EditorInfoCompat .setContentMimeTypes(
146+ outAttrs,
147+ arrayOf(" image/png" , " image/jpg" , " image/jpeg" , " image/gif" , " image/webp" )
148+ )
149+
150+ return InputConnectionCompat .createWrapper(ic, outAttrs, onCommitContent)
151+ }
147152
148- return InputConnectionCompat .createWrapper(ic, outAttrs, onCommitContent)
153+ return ic
149154 }
150155
151156 private val onCommitContent = InputConnectionCompat .OnCommitContentListener { info, flags, _ ->
152157 try {
153- // request permission if needed
154- if ((flags and InputConnectionCompat .INPUT_CONTENT_GRANT_READ_URI_PERMISSION ) != 0 ) {
158+ val hasPermission =
159+ (flags and InputConnectionCompat .INPUT_CONTENT_GRANT_READ_URI_PERMISSION ) != 0
160+
161+ if (hasPermission) {
155162 try {
156163 info.requestPermission()
157- } catch (ex: Exception ) {
158- // permission failed
164+ } catch (_: Exception ) {}
165+ }
166+
167+ if (disableImagePasting) {
168+ if (hasPermission) {
169+ try { info.releasePermission() } catch (_: Exception ) {}
159170 }
171+ return @OnCommitContentListener false
160172 }
161173
162174 val uri = info.contentUri
@@ -189,7 +201,7 @@ class TypeRichTextInputView : AppCompatEditText {
189201 }
190202 }
191203
192- // paste handler
204+ // context menu paste handler
193205 override fun onTextContextMenuItem (id : Int ): Boolean {
194206 if (id == android.R .id.paste || id == android.R .id.pasteAsPlainText) {
195207 val clipboard = context.getSystemService(Context .CLIPBOARD_SERVICE ) as ? ClipboardManager
@@ -198,6 +210,12 @@ class TypeRichTextInputView : AppCompatEditText {
198210 val clip = clipboard.primaryClip ? : return super .onTextContextMenuItem(id)
199211 val item = clip.getItemAt(0 )
200212
213+ if (disableImagePasting) {
214+ if (item.uri != null || item.intent?.data != null ) {
215+ return true
216+ }
217+ }
218+
201219 // uri
202220 item.uri?.let { uri ->
203221 val source = EnumPasteSource .CLIPBOARD .value
@@ -577,6 +595,10 @@ class TypeRichTextInputView : AppCompatEditText {
577595 }
578596 }
579597
598+ fun setDisableImagePasting (disabled : Boolean ){
599+ this .disableImagePasting = disabled
600+ }
601+
580602 override fun isLayoutRequested (): Boolean {
581603 return false
582604 }
0 commit comments